Skip to content

Commit

Permalink
added mod-launching capability
Browse files Browse the repository at this point in the history
  • Loading branch information
Davis-Software committed Oct 8, 2022
1 parent c0b6fd1 commit a8a4f6a
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 569 deletions.
87 changes: 83 additions & 4 deletions back/mc-connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const {auth} = require('msmc')
const settings = require("./settings");
const {registerIpcListener, invoke} = require("./ipc-handler");
const {Client} = require('minecraft-launcher-core')
const path = require("path");
const fs = require("fs");
const {compare} = require("compare-versions");
const fsx = require("fs-extra");


const runningClients = []
Expand Down Expand Up @@ -30,7 +34,7 @@ function launchVanilla(version) {

const opts = {
authorization: settings.get("credentials"),
root: settings.get("mcPath"),
root: path.join(settings.get("mcPath"), "vanilla"),
version: version,
customArgs: settings.get("launch-args"),
memory: {
Expand Down Expand Up @@ -62,15 +66,90 @@ function launchVanilla(version) {
runningClients.splice(runningClients.indexOf(launcher), 1)
invoke("mc:close", e)
})
launcher.on('package-extract', (e) => invoke("mc:package-extract", e))
launcher.on('download', (e) => invoke("mc:download", e))
launcher.on('download-status', (e) => invoke("mc:download-status", e))
launcher.on('debug', (e) => invoke("mc:debug", e))
launcher.on('progress', (e) => invoke("mc:progress", e))
}

function launchModded(modPack) {
invoke("mc:gameLaunchError", "Modded launch is not supported yet.")
function launchModded(manifest) {
const rootPath = path.join(settings.get("mcPath"), "mod-packs", manifest.id)

let currentManifest
let installNeeded = false
let mcPackage = `https://projects.software-city.org/resources/minecraft/modded/modpacks/${manifest.id}.zip`

if(!fs.existsSync(rootPath)){
fs.mkdirSync(rootPath, {recursive: true})
installNeeded = true
}
if(!fs.existsSync(path.join(rootPath, "manifest.json"))) {
currentManifest = manifest
installNeeded = true
} else {
currentManifest = JSON.parse(fs.readFileSync(path.join(rootPath, "manifest.json")).toString())
}
if(compare(manifest.version, currentManifest.version, ">")){
installNeeded = true
}

invoke("mc:initGame")

if(installNeeded){
invoke("mc:packageMode")
fs.readdirSync(rootPath).filter(n => n !== "manifest.json").forEach(file => {
fsx.removeSync(path.join(rootPath, file))
})
invoke("mc:packageInstall")
}

const launcher = new Client()
runningClients.push(launcher)

const opts = {
clientPackage: installNeeded ? mcPackage : null,
forge: path.join(rootPath, "bin", `forge-${manifest.mcVersion}.jar`),

authorization: settings.get("credentials"),
root: rootPath,
version: {
number: manifest.mcVersion,
type: manifest.type
},
customArgs: settings.get("launch-args"),
memory: {
max: settings.get("ram"),
min: settings.get("ram")
},
javaPath: settings.get("javaPath"),
window: {
width: settings.get("splash-width"),
height: settings.get("splash-height")
},
overrides:{
detached: false
}
}

launcher.launch(opts).then(() => {
if(installNeeded) fs.writeFileSync(path.join(rootPath, "manifest.json"), JSON.stringify(manifest))
invoke("mc:gameLaunched")
}).catch((e) => {
runningClients.splice(runningClients.indexOf(launcher), 1)
invoke("mc:gameLaunchError", e)
})

launcher.on('arguments', (e) => invoke("mc:arguments", e))
launcher.on('data', (e) => invoke("mc:data", e))
launcher.on('close', (e) => {
runningClients.splice(runningClients.indexOf(launcher), 1)
invoke("mc:close", e)
})
launcher.on('package-extract', () => invoke("mc:package-extract"))
launcher.on('download', (e) => invoke("mc:download", e))
launcher.on('download-status', (e) => invoke("mc:download-status", e))
launcher.on('debug', (e) => invoke("mc:debug", e))
launcher.on('progress', (e) => invoke("mc:progress", e))
}

registerIpcListener("dialog:askLogin", askLogin)
Expand Down
33 changes: 29 additions & 4 deletions front_src/src/components/LaunchProgressPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,26 @@ function LaunchProgressPane(props: LaunchProgressPaneProps){
}

useEffect(() => {
const logger = new Logger()

exposedFunctions("mc").on("initGame", () => setState(true))
exposedFunctions("mc").on("packageMode", () => {
setLoadingState("query")
setInfo("Preparing package install...")
})
exposedFunctions("mc").on("packageInstall", () => {
setLoadingState("determinate")
setInfo("Getting package...")
})
exposedFunctions("mc").on("package-extract", () => setState(true))
exposedFunctions("mc").on("gameLaunched", () => {
setLoadingState("determinate")
setProgress(100)
setInfo("Game launched!")
setTimeout(() => setState(false), 2500)
})
exposedFunctions("mc").on("gameLaunchError", (e: string) => {
Logger.error(e)
logger.error(e)
setError(<>
Game launch error.<br />{e} <br /><br /><br />Please check the console for more information. (Ctrl + Shift + I)
</>)
Expand All @@ -54,6 +65,14 @@ function LaunchProgressPane(props: LaunchProgressPaneProps){

exposedFunctions("mc").on("download-status", (e: McLcDownloadStatus) => {
if(e.type === "native") return
if(e.type === "client-package"){
setProgress(e.current / e.total * 100)
if(e.current === e.total) {
setLoadingState("indeterminate")
setInfo("Extracting package...")
}
return
}
setBuffer(e.current / e.total * 100)
})
exposedFunctions("mc").on("progress", (e: McLcProgress) => {
Expand All @@ -62,18 +81,24 @@ function LaunchProgressPane(props: LaunchProgressPaneProps){
setLoadingState("query")
setInfo("Downloading natives...")
break
case "classes-custom":
setLoadingState("buffer")
setInfo(`Downloading custom classes... (${e.task}/${e.total})`)
setProgress(e.task / e.total * 100)
break
case "classes":
case "assets":
case "forge":
setLoadingState("buffer")
setInfo(`Downloading ${e.type}... (${e.task}/${e.total})`)
setProgress(e.task / e.total * 100)
break
}
})

exposedFunctions("mc").on("arguments", Logger.info)
exposedFunctions("mc").on("debug", Logger.debug)
exposedFunctions("mc").on("data", Logger.log)
exposedFunctions("mc").on("arguments", logger.info)
exposedFunctions("mc").on("debug", logger.debug)
exposedFunctions("mc").on("data", logger.log)
}, [])

return (
Expand Down
1 change: 0 additions & 1 deletion front_src/src/pages/ModPack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, {useEffect} from "react"
import PageBase from "./PageBase";
import {LaunchBarCustomContent, LaunchBarListContent} from "../components/LaunchBarComponents";
import {Button} from "@mui/material";
import {McLcVersionType} from "../types/mcVersionType";
import {getSetting} from "../utils/settings";
import {exposedFunctions} from "../utils/constants";
import {ModPackType} from "../types/modPackType";
Expand Down
12 changes: 10 additions & 2 deletions front_src/src/pages/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function Settings(){
const [showSnapshots, setShowSnapshots] = React.useState<boolean>(false)
const [showBetaAndAlpha, setShowBetaAndAlpha] = React.useState<boolean>(false)
const [launchArgs, setLaunchArgs] = React.useState<string>("")
const [debugLogging, setDebugLogging] = React.useState<boolean>(false)


useEffect(() => {
Expand All @@ -126,6 +127,7 @@ function Settings(){
getSettingBoolean("show-snapshots").then(setShowSnapshots)
getSettingBoolean("show-beta-and-alpha").then(setShowBetaAndAlpha)
getSetting("launch-args").then(setLaunchArgs)
getSettingBoolean("loggingActive").then(setDebugLogging)
}, [])


Expand All @@ -134,7 +136,7 @@ function Settings(){
setSetting("ram", ramSelected)
}, [ramSelected])
function valuetext(value: number) {
return `${value}MB`;
return `${(value / 1024).toFixed(1)}GB`;
}

useEffect(() => {
Expand Down Expand Up @@ -169,6 +171,9 @@ function Settings(){
useEffect(() => {
setSetting("launch-args", launchArgs)
}, [launchArgs])
useEffect(() => {
setSetting("loggingActive", debugLogging)
}, [debugLogging])

return (
<PageBase>
Expand All @@ -182,6 +187,7 @@ function Settings(){
value={ramSelected!}
defaultValue={1024}
getAriaValueText={valuetext}
valueLabelFormat={valuetext}
step={512}
valueLabelDisplay="auto"
min={1024}
Expand Down Expand Up @@ -221,13 +227,15 @@ function Settings(){
<SettingsCheckbox name="Show Vanilla Snapshots Versions" value={showSnapshots} onChange={setShowSnapshots} />
<SettingsCheckbox name="Show Vanilla Beta and Alpha Versions" value={showBetaAndAlpha} onChange={setShowBetaAndAlpha} />

<table className="table table-borderless mt-3">
<table className="table table-borderless mt-3 mb-3">
<tbody>
<tr>
<SettingsInput name="Java Launch Arguments" type="text" value={launchArgs} onChange={(v) => setLaunchArgs(v as string)} />
</tr>
</tbody>
</table>

<SettingsCheckbox name="Debug logging" value={debugLogging} onChange={setDebugLogging} />
</div>
</PageBase>
)
Expand Down
2 changes: 1 addition & 1 deletion front_src/src/types/McLcResponses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
interface McLcProgress {
type: "natives" | "classes" | "assets";
type: "natives" | "classes" | "assets" | "forge" | "classes-custom";
task: number;
total: number;
}
Expand Down
35 changes: 22 additions & 13 deletions front_src/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import {getSettingSync} from "./settings";

function printLog(message: string, level: string, active: boolean) {
if(!active) return;
// @ts-ignore
console[level](`[${level.toUpperCase()}] ${message}`)
}

class Logger{
private static _log(message: string, level: string){
// @ts-ignore
console[level](`[${level.toUpperCase()}] ${message}`)
_loggingActive: boolean;

constructor() {
this._loggingActive = (getSettingSync("loggingActive") === true);
}
static debug(message: string){
Logger._log(message, 'debug')
debug(message: string){
printLog(message, 'debug', this._loggingActive)
}
static log(message: string){
Logger._log(message, 'log')
log(message: string){
printLog(message, 'log', this._loggingActive)
}
static info(message: string){
Logger._log(message, 'info')
info(message: string){
printLog(message, 'info', this._loggingActive)
}
static warn(message: string){
Logger._log(message, 'warn')
warn(message: string){
printLog(message, 'warn', this._loggingActive)
}
static error(message: string){
Logger._log(message, 'error')
error(message: string){
printLog(message, 'error', this._loggingActive)
}
}

Expand Down
Loading

0 comments on commit a8a4f6a

Please sign in to comment.