Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: launch string too long in minecraft forge 1.19 #1384

Merged
merged 15 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions src/app/desktop/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../../../common/utils/constants';

import {
addQuotes,
removeDuplicates,
sortByForgeVersionDesc
} from '../../../common/utils';
Expand Down Expand Up @@ -483,13 +484,14 @@ export const getJVMArguments112 = (
hideAccessToken,
jvmOptions = []
) => {
const needsQuote = process.platform !== 'win32';
const args = [];
args.push('-cp');

args.push(
[...libraries, mcjar]
.filter(l => !l.natives)
.map(l => `"${l.path}"`)
.map(l => `${addQuotes(needsQuote, l.path)}`)
.join(process.platform === 'win32' ? ';' : ':')
);

Expand All @@ -501,8 +503,15 @@ export const getJVMArguments112 = (
args.push(`-Xmx${memory}m`);
args.push(`-Xms${memory}m`);
args.push(...jvmOptions);
args.push(`-Djava.library.path="${path.join(instancePath, 'natives')}"`);
args.push(`-Dminecraft.applet.TargetDirectory="${instancePath}"`);
args.push(
`-Djava.library.path=${addQuotes(
needsQuote,
path.join(instancePath, 'natives')
)}`
);
args.push(
`-Dminecraft.applet.TargetDirectory=${addQuotes(needsQuote, instancePath)}`
);
if (mcJson.logging) {
args.push(mcJson?.logging?.client?.argument || '');
}
Expand All @@ -524,13 +533,13 @@ export const getJVMArguments112 = (
val = mcJson.id;
break;
case 'game_directory':
val = `"${instancePath}"`;
val = `${addQuotes(needsQuote, instancePath)}`;
break;
case 'assets_root':
val = `"${assetsPath}"`;
val = `${addQuotes(needsQuote, assetsPath)}`;
break;
case 'game_assets':
val = `"${path.join(assetsPath, 'virtual', 'legacy')}"`;
val = `${path.join(assetsPath, 'virtual', 'legacy')}`;
break;
case 'assets_index_name':
val = mcJson.assets;
Expand Down Expand Up @@ -559,6 +568,9 @@ export const getJVMArguments112 = (
if (val != null) {
mcArgs[i] = val;
}
if (typeof args[i] === 'string' && !needsQuote) {
args[i] = args[i].replaceAll('"', '');
}
}
}

Expand Down Expand Up @@ -586,6 +598,7 @@ export const getJVMArguments113 = (
) => {
const argDiscovery = /\${*(.*)}/;
let args = mcJson.arguments.jvm.filter(v => !skipLibrary(v));
const needsQuote = process.platform !== 'win32';

// if (process.platform === "darwin") {
// args.push("-Xdock:name=instancename");
Expand All @@ -594,7 +607,9 @@ export const getJVMArguments113 = (

args.push(`-Xmx${memory}m`);
args.push(`-Xms${memory}m`);
args.push(`-Dminecraft.applet.TargetDirectory="${instancePath}"`);
args.push(
`-Dminecraft.applet.TargetDirectory=${addQuotes(needsQuote, instancePath)}`
);
if (mcJson.logging) {
args.push(mcJson?.logging?.client?.argument || '');
}
Expand All @@ -612,9 +627,9 @@ export const getJVMArguments113 = (
for (let i = 0; i < args.length; i += 1) {
if (typeof args[i] === 'object' && args[i].rules) {
if (typeof args[i].value === 'string') {
args[i] = `"${args[i].value}"`;
args[i] = `${addQuotes(needsQuote, args[i].value)}`;
} else if (typeof args[i].value === 'object') {
args.splice(i, 1, ...args[i].value.map(v => `"${v}"`));
args.splice(i, 1, ...args[i].value.map(v => `${v}`));
}
i -= 1;
} else if (typeof args[i] === 'string') {
Expand All @@ -629,10 +644,10 @@ export const getJVMArguments113 = (
val = mcJson.id;
break;
case 'game_directory':
val = `"${instancePath}"`;
val = `${addQuotes(needsQuote, instancePath)}`;
break;
case 'assets_root':
val = `"${assetsPath}"`;
val = `${addQuotes(needsQuote, assetsPath)}`;
break;
case 'assets_index_name':
val = mcJson.assets;
Expand All @@ -658,7 +673,7 @@ export const getJVMArguments113 = (
case 'natives_directory':
val = args[i].replace(
argDiscovery,
`"${path.join(instancePath, 'natives')}"`
`${addQuotes(needsQuote, path.join(instancePath, 'natives'))}`
);
break;
case 'launcher_name':
Expand All @@ -670,7 +685,7 @@ export const getJVMArguments113 = (
case 'classpath':
val = [...libraries, mcjar]
.filter(l => !l.natives)
.map(l => `"${l.path}"`)
.map(l => `${addQuotes(needsQuote, l.path)}`)
.join(process.platform === 'win32' ? ';' : ':');
break;
default:
Expand All @@ -680,6 +695,7 @@ export const getJVMArguments113 = (
args[i] = val;
}
}
if (!needsQuote) args[i] = args[i].replaceAll('"', '');
}
}

Expand Down
42 changes: 22 additions & 20 deletions src/common/reducers/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ import {
downloadInstanceFiles
} from '../../app/desktop/utils/downloader';
import {
addQuotes,
getFileMurmurHash2,
getSize,
makeInstanceRestorePoint,
removeDuplicates
removeDuplicates,
replaceLibraryDirectory
} from '../utils';
import { UPDATE_CONCURRENT_DOWNLOADS } from './settings/actionTypes';
import { UPDATE_MODAL } from './modals/actionTypes';
Expand Down Expand Up @@ -2966,20 +2968,18 @@ export function launchInstance(instanceName, forceQuit = false) {
mcJson.forge = { arguments: {} };
mcJson.forge.arguments.jvm = forgeJson.version.arguments.jvm.map(
arg => {
return arg
.replace(/\${version_name}/g, mcJson.id)
.replace(
/=\${library_directory}/g,
`="${_getLibrariesPath(state)}"`
)
.replace(
/\${library_directory}/g,
`${_getLibrariesPath(state)}`
)
.replace(
/\${classpath_separator}/g,
process.platform === 'win32' ? ';' : ':'
);
return replaceLibraryDirectory(
arg
.replace(/\${version_name}/g, mcJson.id)
.replace(
/=\${library_directory}/g,
`="${_getLibrariesPath(state)}"`
),
_getLibrariesPath(state)
).replace(
/\${classpath_separator}/g,
process.platform === 'win32' ? '";' : '":'
);
}
);
}
Expand Down Expand Up @@ -3082,8 +3082,10 @@ export function launchInstance(instanceName, forceQuit = false) {
loggingId || ''
);

const needsQuote = process.platform !== 'win32';

console.log(
`"${javaPath}" ${getJvmArguments(
`${addQuotes(needsQuote, javaPath)} ${getJvmArguments(
libraries,
mcMainFile,
instancePath,
Expand All @@ -3099,7 +3101,7 @@ export function launchInstance(instanceName, forceQuit = false) {
.replace(
// eslint-disable-next-line no-template-curly-in-string
'-Dlog4j.configurationFile=${path}',
`-Dlog4j.configurationFile="${loggingPath}"`
`-Dlog4j.configurationFile=${addQuotes(needsQuote, loggingPath)}`
)
);

Expand All @@ -3110,20 +3112,20 @@ export function launchInstance(instanceName, forceQuit = false) {
let closed = false;

const ps = spawn(
`"${javaPath}"`,
`${addQuotes(needsQuote, javaPath)}`,
jvmArguments.map(v =>
v
.toString()
.replace(...replaceRegex)
.replace(
// eslint-disable-next-line no-template-curly-in-string
'-Dlog4j.configurationFile=${path}',
`-Dlog4j.configurationFile="${loggingPath}"`
`-Dlog4j.configurationFile=${addQuotes(needsQuote, loggingPath)}`
)
),
{
cwd: instancePath,
shell: true
shell: process.platform !== 'win32'
}
);

Expand Down
19 changes: 19 additions & 0 deletions src/common/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,22 @@ export const getSize = async dir => {
.catch(e => reject(e));
});
};

export const addQuotes = (needsQuote, string) => {
return needsQuote ? `"${string}"` : string;
};

export const replaceLibraryDirectory = (arg, librariesDir) => {
const parsedArg = arg.replace(/\${library_directory}/g, `"${librariesDir}`);
const regex = /\${classpath_separator}/g;
const isLibrariesArgString = arg.match(regex);
const splittedString = parsedArg.split(regex);
splittedString[splittedString.length - 1] = `${
splittedString[splittedString.length - 1]
}"`;

return isLibrariesArgString
? // eslint-disable-next-line no-template-curly-in-string
splittedString.join('${classpath_separator}')
: arg;
};