Skip to content

Commit

Permalink
v0.11.10-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
blarfoon committed Apr 25, 2019
1 parent 61c538c commit 4d8c5ad
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 86 deletions.
202 changes: 138 additions & 64 deletions app/actions/downloadManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import axios from 'axios';
import makeDir from 'make-dir';
import fse, { outputFile } from 'fs-extra';
import { cpus } from 'os';
import dirTree from 'directory-tree';
import { spawn, exec } from 'child_process';
import { exec } from 'child_process';
import jarAnalyzer from 'jarfile';
import log from 'electron-log';
import Promise from 'bluebird';
Expand All @@ -27,12 +26,21 @@ import {
extractNatives,
computeVanillaAndForgeLibraries
} from '../utils/getMCFilesList';
import { downloadMod, getModsList, createDoNotTouchFile } from '../utils/mods';
import { downloadMod, createDoNotTouchFile } from '../utils/mods';
import { findJavaHome } from '../utils/javaHelpers';
import { arraify } from '../utils/strings';
import { copyAssetsToLegacy, copyAssetsToResources } from '../utils/assets';
import { getAddonFile, getAddon } from '../utils/cursemeta';
import { getForgeVersionJSON, checkForgeMeta, checkForgeDownloaded } from '../utils/forgeHelpers';
import {
getAddonFile,
getAddon,
getAddonFileIDFromVersion
} from '../utils/cursemeta';
import {
getForgeVersionJSON,
checkForgeMeta,
checkForgeDownloaded
} from '../utils/forgeHelpers';
import { readConfig } from '../utils/instances';

export const START_DOWNLOAD = 'START_DOWNLOAD';
export const CLEAR_QUEUE = 'CLEAR_QUEUE';
Expand All @@ -41,7 +49,47 @@ export const DOWNLOAD_COMPLETED = 'DOWNLOAD_COMPLETED';
export const UPDATE_TOTAL_FILES_TO_DOWNLOAD = 'UPDATE_TOTAL_FILES_TO_DOWNLOAD';
export const UPDATE_PROGRESS = 'UPDATE_PROGRESS';

export function addToQueue(pack, version, forgeVersion = null) {
export function repairInstance(pack) {
return async (dispatch, getState) => {
const { packCreator } = getState();
const config = await readConfig(pack);
if (config.projectID) {
const fileID = await getAddonFileIDFromVersion(
config.projectID,
config.modpackVersion
);
if (fileID)
dispatch(addCursePackToQueue(pack, config.projectID, fileID, true));
else {
message.error('Could not repair');
log.error(
'Could not find fileID for',
pack,
config.projectID,
config.modpackVersion
);
}
} else {
dispatch(
addToQueue(
pack,
config.version,
config.forgeVersion
? config.forgeVersion.replace('forge-', '')
: null,
true
)
);
}
};
}

export function addToQueue(
pack,
version,
forgeVersion = null,
isRepair = false
) {
return (dispatch, getState) => {
const { downloadManager } = getState();
dispatch({
Expand All @@ -55,7 +103,7 @@ export function addToQueue(pack, version, forgeVersion = null) {
type: START_DOWNLOAD,
payload: pack
});
dispatch(downloadPack(pack));
dispatch(downloadPack(pack, isRepair));
}
};
}
Expand Down Expand Up @@ -96,7 +144,7 @@ export function importTwitchProfile(pack, filePath) {
};
}

export function addCursePackToQueue(pack, addonID, fileID) {
export function addCursePackToQueue(pack, addonID, fileID, isRepair = false) {
return async (dispatch, getState) => {
const { downloadManager } = getState();
const packURL = (await getAddonFile(addonID, fileID)).downloadUrl;
Expand All @@ -105,7 +153,7 @@ export function addCursePackToQueue(pack, addonID, fileID) {
'temp',
path.basename(packURL)
);
await downloadFile(tempPackPath, packURL, () => { });
await downloadFile(tempPackPath, packURL, () => {});
await compressing.zip.uncompress(
tempPackPath,
path.join(INSTANCES_PATH, 'temp', pack)
Expand Down Expand Up @@ -134,7 +182,7 @@ export function addCursePackToQueue(pack, addonID, fileID) {
type: START_DOWNLOAD,
payload: pack
});
dispatch(downloadPack(pack));
dispatch(downloadPack(pack, isRepair));
}
};
}
Expand All @@ -156,12 +204,14 @@ export function clearQueue() {
};
}

export function downloadPack(pack) {
export function downloadPack(pack, isRepair = false) {
return async (dispatch, getState) => {
const { downloadManager, packCreator } = getState();
const currPack = downloadManager.downloadQueue[pack];
let vnlJSON = null;
try {
// If is repair, skip this and download it again
if (isRepair) throw new Error();
vnlJSON = JSON.parse(
await promisify(fs.readFile)(
path.join(
Expand Down Expand Up @@ -194,8 +244,10 @@ export function downloadPack(pack) {
const assets = await extractAssets(vnlJSON, pack);
const mainJar = await extractMainJar(vnlJSON);

if (currPack.forgeVersion !== null) {
if (currPack.forgeVersion) {
try {
// If is repair, skip this and download it again
if (isRepair) throw new Error();
forgeJSON = await checkForgeMeta(currPack.forgeVersion);
await checkForgeDownloaded(forgeJSON.mavenVersionString);
} catch (err) {
Expand All @@ -206,27 +258,30 @@ export function downloadPack(pack) {
...arraify(forgeJSON.mavenVersionString)
);

await downloadFile(
forgeBinPath,
forgeJSON.downloadUrl,
p => {
dispatch({
type: UPDATE_PROGRESS,
payload: { pack, percentage: ((p * 18) / 100).toFixed(0) }
});
}
);
await downloadFile(forgeBinPath, forgeJSON.downloadUrl, p => {
dispatch({
type: UPDATE_PROGRESS,
payload: { pack, percentage: ((p * 18) / 100).toFixed(0) }
});
});

await outputFile(path.join(
META_PATH,
'net.minecraftforge',
`forge-${currPack.forgeVersion}`,
`forge-${currPack.forgeVersion}.json`
), JSON.stringify(forgeJSON))
await outputFile(
path.join(
META_PATH,
'net.minecraftforge',
`forge-${currPack.forgeVersion}`,
`forge-${currPack.forgeVersion}.json`
),
JSON.stringify(forgeJSON)
);
}
}

const libraries = await computeVanillaAndForgeLibraries(vnlJSON, forgeJSON, false);
const libraries = await computeVanillaAndForgeLibraries(
vnlJSON,
forgeJSON,
false
);

// This is the main config file for the instance
await makeDir(path.join(PACKS_PATH, pack));
Expand All @@ -242,9 +297,9 @@ export function downloadPack(pack) {
const legacyJavaFixer =
vCompare(currPack.forgeVersion, '10.13.1.1217') === -1
? {
url: GDL_LEGACYJAVAFIXER_MOD_URL,
path: path.join(PACKS_PATH, pack, 'mods', 'LJF.jar')
}
url: GDL_LEGACYJAVAFIXER_MOD_URL,
path: path.join(PACKS_PATH, pack, 'mods', 'LJF.jar')
}
: null;

// Here we work on the mods
Expand Down Expand Up @@ -334,7 +389,7 @@ export function downloadPack(pack) {
await downloadFile(
path.join(PACKS_PATH, pack, 'thumbnail.png'),
thumbnailURL,
() => { }
() => {}
);

// Copy the thumbnail as icon
Expand All @@ -344,22 +399,25 @@ export function downloadPack(pack) {
);
}

await promisify(fs.writeFile)(
path.join(PACKS_PATH, pack, 'config.json'),
JSON.stringify({
version: currPack.version,
forgeVersion:
currPack.forgeVersion === null
? null
: `forge-${currPack.forgeVersion}`,
...(currPack.addonID && { projectID: currPack.addonID }),
...(modpackVersion && { modpackVersion }),
...(thumbnailURL && { icon: 'icon.png' }),
timePlayed: 0,
mods: modsManifest,
overrideFiles: overrideFilesList
})
);
// Don't write a new config if it's repairing
if (!isRepair) {
await promisify(fs.writeFile)(
path.join(PACKS_PATH, pack, 'config.json'),
JSON.stringify({
version: currPack.version,
forgeVersion:
currPack.forgeVersion === null
? null
: `forge-${currPack.forgeVersion}`,
...(currPack.addonID && { projectID: currPack.addonID }),
...(modpackVersion && { modpackVersion }),
...(thumbnailURL && { icon: 'icon.png' }),
timePlayed: 0,
mods: modsManifest,
overrideFiles: overrideFilesList
})
);
}

const totalFiles = libraries.length + assets.length + mainJar.length;

Expand Down Expand Up @@ -398,49 +456,65 @@ export function downloadPack(pack) {
await extractNatives(libraries.filter(lib => 'natives' in lib), pack);

// Finish forge patches >= 1.13
const installProfileJson = JSON.parse(forgeJSON.installProfileJson);
const installProfileJson =
forgeJSON && JSON.parse(forgeJSON.installProfileJson);
if (installProfileJson) {
const { processors } = installProfileJson;
const replaceIfPossible = arg => {
const finalArg = arg.replace('{', '').replace('}', '');
if (installProfileJson.data[finalArg]) {
// Handle special case
if (finalArg === 'BINPATCH') {
return path.join(
INSTANCES_PATH,
'libraries',
...arraify(installProfileJson.path)
).replace('.jar', "-clientdata.lzma")
return path
.join(
INSTANCES_PATH,
'libraries',
...arraify(installProfileJson.path)
)
.replace('.jar', '-clientdata.lzma');
}
// Return replaced string
return installProfileJson.data[finalArg].client;
}
// Return original string (checking for MINECRAFT_JAR)
return arg.replace('{MINECRAFT_JAR}', mainJar[0].path);
}
};
const computePathIfPossible = arg => {
if (arg[0] === '[') {
return path.join(INSTANCES_PATH, 'libraries', arraify(arg.replace('[', '').replace(']', '')).join('/'));
return path.join(
INSTANCES_PATH,
'libraries',
arraify(arg.replace('[', '').replace(']', '')).join('/')
);
}
return arg;
}
};
const javaPath = await findJavaHome();
for (const p in processors) {
const filePath = path.join(INSTANCES_PATH, 'libraries', ...arraify(processors[p].jar));
const filePath = path.join(
INSTANCES_PATH,
'libraries',
...arraify(processors[p].jar)
);
const args = processors[p].args
.map(arg => replaceIfPossible(arg))
.map(arg => computePathIfPossible(arg));

const classPaths = processors[p].classpath.map(cp => path.join(INSTANCES_PATH, 'libraries', arraify(cp).join('/')));
const classPaths = processors[p].classpath.map(cp =>
path.join(INSTANCES_PATH, 'libraries', arraify(cp).join('/'))
);

const jarFile = await promisify(jarAnalyzer.fetchJarAtPath)(filePath);
const mainClass = jarFile.valueForManifestEntry("Main-Class");
const mainClass = jarFile.valueForManifestEntry('Main-Class');

const { stderr, stdout } = await promisify(exec)(
`"${javaPath}" -classpath "${filePath}${CLASSPATH_DIVIDER_CHAR}${classPaths.join(CLASSPATH_DIVIDER_CHAR)}" ${mainClass} ${args.join(' ')}`
, { maxBuffer: 10000000000 })
`"${javaPath}" -classpath "${filePath}${CLASSPATH_DIVIDER_CHAR}${classPaths.join(
CLASSPATH_DIVIDER_CHAR
)}" ${mainClass} ${args.join(' ')}`,
{ maxBuffer: 10000000000 }
);

console.log(stderr, stdout)
console.log(stderr, stdout);
}
}

Expand Down
7 changes: 4 additions & 3 deletions app/components/DInstance/DInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Props = {
selectedInstance: ?string,
startInstance: () => void,
selectInstance: () => void,
repairInstance: () => void,
playing: array
};

Expand Down Expand Up @@ -352,19 +353,19 @@ export default class DInstance extends Component<Props> {
</span>{' '}
Export
</MenuItem>
{/* <MenuItem
<MenuItem
disabled={
this.isInstalling() ||
!isValid ||
playing.find(el => el.name === name)
}
onClick={() => this.props.addToQueue(name, version, forgeVersion)}
onClick={() => this.props.repairInstance(name)}
>
<span>
<FontAwesomeIcon icon={faWrench} />
</span>{' '}
Repair
</MenuItem> */}
</MenuItem>
<MenuItem
disabled={
this.isInstalling() || playing.find(el => el.name === name)
Expand Down
Loading

0 comments on commit 4d8c5ad

Please sign in to comment.