Compare commits

...

1 Commits

Author SHA1 Message Date
563f4f3fb4 classes 2021-02-08 00:08:36 +01:00
5 changed files with 195 additions and 184 deletions

View File

@ -1,5 +1,6 @@
import { https } from "follow-redirects"; import { https } from "follow-redirects";
import fs from "fs"; import fs from "fs";
import {modBase, config} from "./modBase";
interface modLockElement { interface modLockElement {
fileId: number; fileId: number;
url: string; url: string;
@ -10,16 +11,14 @@ interface Dependency {
readonly addonId: number; readonly addonId: number;
readonly type: number; readonly type: number;
} }
let mods_lock: modLockMap; class curse extends modBase {
let downloadStarted: boolean = false; private dep: modLockMap = new Map();
let globCallback; private downloadStarted: boolean = false;
let config; constructor(private modsLock: modLockMap, config: config, callback: Function) {
let dep: modLockMap = new Map(); super(config, callback);
function main() { if(config.mods.curse.length === 0) callback("curse", {});
mods_lock = new Map(Object.entries(mods_lock));
if(config.mods.curse.length === 0) globCallback("curse", {});
config.mods.curse.forEach((mod: string) => { config.mods.curse.forEach((mod: string) => {
getData(`search?categoryId=0&gameId=432&gameVersion=${encodeURI(config.gameVersion)}&index=0&pageSize=15&searchFilter=${encodeURI(mod)}&sectionId=6&sort=0`, (result: Array<any>) => { // resolve projectID this.getData(`search?categoryId=0&gameId=432&gameVersion=${encodeURI(config.gameVersion)}&index=0&pageSize=15&searchFilter=${encodeURI(mod)}&sectionId=6&sort=0`, (result: Array<any>) => { // resolve projectID
let i = 0; let i = 0;
while(result[i].name !== mod) { while(result[i].name !== mod) {
++i; ++i;
@ -28,39 +27,41 @@ function main() {
return; return;
} }
} }
resolveDep(result[i].id, downloadMods); this.resolveDep(result[i].id, () => {
this.downloadMods();
}); });
}); });
} });
}
var resolveDepRecursionCount = 0; private resolveDepRecursionCount = 0;
function resolveDep(modId: number, callback: Function) { private resolveDep(modId: number, callback: Function):void {
++resolveDepRecursionCount; ++this.resolveDepRecursionCount;
getData(`${modId}/files`, (files: Array<any>) => { this.getData(`${modId}/files`, (files: Array<any>) => {
let rightVersion: any; let rightVersion;
for(let i = files.length - 1; i >= 0; --i) { for(let i = files.length - 1; i >= 0; --i) {
let versionArray = files[i].gameVersion; let versionArray = files[i].gameVersion;
if(versionMatch(versionArray, config.gameVersion) && (versionMatch(versionArray, config.modloader) || modloaderMatch(versionArray, config.modloader))) { if(this.versionMatch(versionArray, this.config.gameVersion) && (this.versionMatch(versionArray, this.config.modloader) || this.modloaderMatch(versionArray, this.config.modloader))) {
rightVersion = files[i]; rightVersion = files[i];
} }
} }
if(rightVersion === undefined) { if(rightVersion === undefined) {
getData(String(modId), (mod: any) => { this.getData(String(modId), (mod: any) => {
console.log(`cursemod ${mod.name}: no version for the correct minecraft version and modloader found`); console.log(`cursemod ${mod.name}: no version for the correct minecraft version and modloader found`);
}); });
return; return;
} }
dep.set(String(modId), {fileId: rightVersion.id, url: rightVersion.downloadUrl, filename: rightVersion.fileName}); this.dep.set(String(modId), {fileId: rightVersion.id, url: rightVersion.downloadUrl, filename: rightVersion.fileName});
rightVersion.dependencies.forEach((mod: Dependency) => { rightVersion.dependencies.forEach((mod: Dependency) => {
if(mod.type !== 3) return; if(mod.type !== 3) return;
resolveDep(mod.addonId, callback); this.resolveDep(mod.addonId, callback);
}); });
--resolveDepRecursionCount; --this.resolveDepRecursionCount;
if(resolveDepRecursionCount == 0) callback(); if(this.resolveDepRecursionCount == 0) callback();
}); });
} }
function getData(url: string, callback: Function) { private getData(url: string, callback: Function):void {
https.get(`https://addons-ecs.forgesvc.net/api/v2/addon/${url}`, (resp) => { https.get(`https://addons-ecs.forgesvc.net/api/v2/addon/${url}`, (resp) => {
let data: string = ''; let data: string = '';
resp.on('data', (chunk) => { resp.on('data', (chunk) => {
@ -70,42 +71,42 @@ function getData(url: string, callback: Function) {
callback(JSON.parse(data)); callback(JSON.parse(data));
}); });
}); });
} }
function versionMatch(versionArray, gameVersion) { versionMatch(versionArray, gameVersion):boolean {
let gameVersionMatch = false; let gameVersionMatch = false;
versionArray.forEach(version => { versionArray.forEach(version => {
gameVersionMatch = gameVersionMatch || version == gameVersion; gameVersionMatch = gameVersionMatch || version == gameVersion;
}); });
return gameVersionMatch; return gameVersionMatch;
} }
function modloaderMatch(versionArray, modloaderVersion) { private modloaderMatch(versionArray: Array<string>, modloaderVersion: string):boolean {
for(let i = 0; i < versionArray.length; ++i) { for(let i = 0; i < versionArray.length; ++i) {
if(/^[a-zA-Z]+$/.test(versionArray[i]) && versionArray[i] != modloaderVersion) return false; if(/^[a-zA-Z]+$/.test(versionArray[i]) && versionArray[i] != modloaderVersion) return false;
} }
return true; return true;
} }
function downloadMods() { private downloadMods():void {
if(!downloadStarted) { if(!this.downloadStarted) {
downloadStarted = true; this.downloadStarted = true;
globCallback("curse", Object.fromEntries(dep)); this.callback("curse", Object.fromEntries(this.dep));
dep.forEach((mod: modLockElement, modId: string) => { this.dep.forEach((mod: modLockElement, modId: string) => {
let path = modPath(mod) let path = this.modPath(mod)
if(mods_lock.has(modId)) { if(this.modsLock.has(modId)) {
if(mods_lock.get(modId)!.fileId === mod.fileId) return; if(this.modsLock.get(modId)!.fileId === mod.fileId) return;
fs.unlink(path, () => {}); fs.unlink(path, () => {});
} }
downloadFile(mod.url, path); this.downloadFile(mod.url, path);
}); });
mods_lock.forEach((mod: modLockElement, modId: string) => { this.modsLock.forEach((mod: modLockElement, modId: string) => {
if(!dep.has(modId)) fs.unlink(modPath(mod), () => {}) if(!this.dep.has(modId)) fs.unlink(this.modPath(mod), () => {})
}); });
} }
} }
function downloadFile(url: string, dest: string) { private downloadFile(url: string, dest: string):void {
let file = fs.createWriteStream(dest); let file = fs.createWriteStream(dest);
console.log(`downloading ${url}`); console.log(`downloading ${url}`);
let request = https.get(url, (response) => { let request = https.get(url, (response) => {
@ -131,10 +132,6 @@ function downloadFile(url: string, dest: string) {
throw err; throw err;
}); });
} }
}
module.exports = function(mods_lock_p, _config, callback) { export = curse;
mods_lock = mods_lock_p;
globCallback = callback;
config = _config;
main();
};

View File

@ -2,6 +2,7 @@ import git from "gift";
import childProcess from "child_process"; import childProcess from "child_process";
import fs from "fs"; import fs from "fs";
import g2js from "gradle-to-js/lib/parser"; import g2js from "gradle-to-js/lib/parser";
import {modBase, config} from "./modBase";
interface modLockElement { interface modLockElement {
commitId: string; commitId: string;
url: string; url: string;
@ -11,18 +12,16 @@ type modLockMap = Map<string, modLockElement>;
interface commit { interface commit {
id: string; id: string;
} }
let modsLock: modLockMap; class gitMods extends modBase {
let globCallback: Function; private loopCounter: number = 0;
let loopCounter: number = 0; constructor(private modsLock: modLockMap, config: config, callback: Function) {
let config; super(config, callback);
function main () { let deleteMods: modLockMap = new Map(this.modsLock);
modsLock = new Map(Object.entries(modsLock));
let deleteMods: modLockMap = new Map(modsLock);
let cacheDir = process.env.XDG_CACHE_HOME; let cacheDir = process.env.XDG_CACHE_HOME;
if(cacheDir == null) cacheDir = `${process.env.HOME}/.cache`; if(cacheDir == null) cacheDir = `${process.env.HOME}/.cache`;
let repoPathRoot = `${cacheDir}/minecraft-mod-packager`; let repoPathRoot = `${cacheDir}/minecraft-mod-packager`;
config.mods.git.forEach((gitRepo) => { config.mods.git.forEach((gitRepo) => {
++loopCounter; ++this.loopCounter;
deleteMods.delete(gitRepo.url); deleteMods.delete(gitRepo.url);
let repoPath = `${repoPathRoot}/${gitRepo.url.replace("://", "+")}/${gitRepo.branch}`; let repoPath = `${repoPathRoot}/${gitRepo.url.replace("://", "+")}/${gitRepo.branch}`;
fs.access(repoPath, (err) => { fs.access(repoPath, (err) => {
@ -31,7 +30,7 @@ function main () {
console.log(`cloning git repo ${gitRepo.url}`); console.log(`cloning git repo ${gitRepo.url}`);
git.clone(gitRepo.url, repoPath, undefined, gitRepo.branch, (err: Error, repo) => { git.clone(gitRepo.url, repoPath, undefined, gitRepo.branch, (err: Error, repo) => {
if(err) throw err; if(err) throw err;
build(repo, repoPath, gitRepo); this.build(repo, repoPath, gitRepo);
}); });
} }
else { else {
@ -39,27 +38,27 @@ function main () {
console.log(`pulling git repo ${gitRepo.url}`); console.log(`pulling git repo ${gitRepo.url}`);
repo.pull(["origin"], gitRepo.branch, (err: Error) => { repo.pull(["origin"], gitRepo.branch, (err: Error) => {
if(err) throw err; if(err) throw err;
build(repo, repoPath, gitRepo); this.build(repo, repoPath, gitRepo);
}); });
} }
}); });
}); });
deleteMods.forEach((mod, url) => { deleteMods.forEach((mod, url) => {
modsLock.delete(url); this.modsLock.delete(url);
fs.unlink(modPath(mod), (err) => { fs.unlink(this.modPath(mod), (err) => {
if(err) throw err; if(err) throw err;
}); });
}); });
mayCb(); this.mayCb();
} }
function build(repo, repoPath, gitRepo):void { private build(repo, repoPath, gitRepo):void {
let newLock: modLockElement; let newLock: modLockElement;
newLock = modsLock.get(gitRepo.url) ?? {} as modLockElement; newLock = this.modsLock.get(gitRepo.url) ?? {} as modLockElement;
modsLock.set(gitRepo.url, newLock); this.modsLock.set(gitRepo.url, newLock);
repo.current_commit((err: Error, commit: commit) => { repo.current_commit((err: Error, commit: commit) => {
if(err) throw err; if(err) throw err;
if(commit.id === newLock.commitId) cbDecrease(); if(commit.id === newLock.commitId) this.cbDecrease();
else { else {
newLock.commitId = commit.id; newLock.commitId = commit.id;
console.log(`buidling ${gitRepo.url}`); console.log(`buidling ${gitRepo.url}`);
@ -69,33 +68,28 @@ function build(repo, repoPath, gitRepo):void {
fs.readFile(`${repoPath}/gradle.properties`, "utf-8", (err, data) => { fs.readFile(`${repoPath}/gradle.properties`, "utf-8", (err, data) => {
if(err) throw err; if(err) throw err;
g2js.parseText(data).then((gradleProp) => { g2js.parseText(data).then((gradleProp) => {
if(newLock.filename != null) fs.unlink(modPath(newLock), (err) => { if(newLock.filename != null) fs.unlink(this.modPath(newLock), (err) => {
if(err) throw err; if(err) throw err;
}); });
newLock.filename = `${gradleProp.archives_base_name}-${gradleProp.mod_version}.jar`; newLock.filename = `${gradleProp.archives_base_name}-${gradleProp.mod_version}.jar`;
fs.copyFile(`${buildPath}/${newLock.filename}`, modPath(newLock), (err) => { fs.copyFile(`${buildPath}/${newLock.filename}`, this.modPath(newLock), (err) => {
if(err) throw err; if(err) throw err;
}); });
cbDecrease(); this.cbDecrease();
}); });
}); });
}); });
} }
}); });
} }
function cbDecrease():void { private cbDecrease():void {
--loopCounter; --this.loopCounter;
mayCb(); this.mayCb();
} }
function mayCb():void { private mayCb():void {
if(loopCounter <= 0) globCallback("git", Object.fromEntries(modsLock)); if(this.loopCounter <= 0) this.callback("git", Object.fromEntries(this.modsLock));
} }
module.exports = (mods_lock_p, _config, cb) => {
globCallback = cb;
modsLock = mods_lock_p;
config = _config;
main()
} }
export = gitMods;

View File

@ -1,6 +1,10 @@
const jsonMinify = require("node-json-minify"); import jsonMinify from "node-json-minify";
const fs = require("fs"); import fs from "fs";
let config; import {config} from "./modBase";
import curse from "./curse";
import git from "./git";
let config: config;
let modules = ["curse", "git"];
switch(process.argv.length) { switch(process.argv.length) {
case 3: case 3:
process.chdir(process.argv[2]); process.chdir(process.argv[2]);
@ -18,14 +22,14 @@ try {
} }
catch(err) { catch(err) {
if(err.errno !== -2) throw err; if(err.errno !== -2) throw err;
modsLock = {curse: {}, git: {}}; modsLock = {};
} }
const numberCallback = 2; const numberCallback = modules.length;
let callbackCounter = 0; let callbackCounter = 0;
require("./curse")(modsLock.curse, config, finnish); new git(new Map(Object.entries(modsLock.git ?? {})), config, finnish);
require("./git.js")(modsLock.git, config, finnish); new curse(new Map(Object.entries(modsLock.curse ?? {})), config, finnish);
function finnish(name, data) { function finnish(name: string, data) {
save_mods_lock[name] = data; save_mods_lock[name] = data;
if(++callbackCounter == numberCallback) { if(++callbackCounter == numberCallback) {
fs.writeFile(modLockPath, JSON.stringify(save_mods_lock), (err) => { fs.writeFile(modLockPath, JSON.stringify(save_mods_lock), (err) => {

19
src/modBase.ts Normal file
View File

@ -0,0 +1,19 @@
export interface config {
gameVersion: string;
modloader: string;
mods: {
curse: Array<string>;
git: Array<{
url: string;
branch: string;
}>
}
}
export class modBase {
constructor(protected config: config, protected callback: Function) {
console.log(callback)
}
protected modPath(mod: any) {
return `mods/${mod.filename}`;
}
}

View File

@ -1,3 +0,0 @@
function modPath(mod: any) {
return `mods/${mod.filename}`;
}