"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const modloader64_1 = __importDefault(require("./modloader/modloader64"));
const commander_1 = __importDefault(require("commander"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const log4js_1 = require("log4js");
const process_1 = require("process");
const asar_node_1 = require("asar-node");
require('source-map-support').install();
const projectID = 'ModLoader64';
const authors = ['denoflions', 'Sylvain'];
const contributors = [];
let data = JSON.parse(fs_1.default.readFileSync(path_1.default.resolve(__dirname, "contributors1.json")).toString());
let dontdisplay = ["denoflionsx", "dependabot[bot]"];
for (let i = 0; i < data.length; i++) {
    let p = data[i];
    if (dontdisplay.indexOf(p.login) === -1) {
        contributors.push(p.login);
    }
}
const version = require('./version');
const logger = (0, log4js_1.getLogger)("Core");
const log_arr = ['ML64Core_stdout', 'ML64Core_file'];
if (process_1.argv.indexOf("--forceclientmode") > -1) {
    log_arr.pop();
}
const logConfig = {
    appenders: { ML64Core_stdout: { type: 'stdout' }, ML64Core_file: { type: 'file', filename: 'console.log' } },
    categories: { default: { appenders: log_arr, level: 'all' } }
};
(0, log4js_1.configure)(logConfig);
const logger_ovl = (0, log4js_1.getLogger)("console.log");
console.log = (message, ...optionalParams) => {
    logger_ovl.debug(message);
};
global.ModLoader = {};
global.ModLoader['version'] = version;
console.log(process.argv);
commander_1.default.option('-d, --dir <dir>', 'set directory');
commander_1.default.option("-m, --mods <dir>", "change mod folder");
commander_1.default.option("-r, --roms <dir>", "change rom folder");
commander_1.default.option("-c, --cores <dir>", "change core folder");
commander_1.default.option("-o, --config <file>, change config file");
commander_1.default.option("-s, --startdir <dir>", "the start dir for sdk usage");
commander_1.default.option("-l, --logginglevel <level>", "the logging level");
commander_1.default.option("-i, --discord <user>", "discord id");
commander_1.default.option("-w, --ScreenWidth <width>", "screen width");
commander_1.default.option("-h, --ScreenHeight <height>", "screen height");
commander_1.default.option("-t, --windowTitle <title>", 'set window title');
commander_1.default.option("-x, --extradata <data>", 'extra data to pass to bindings');
commander_1.default.allowUnknownOption(true);
commander_1.default.parse(process.argv);
let opts = commander_1.default.opts();
if (global.hasOwnProperty("MLASARSUPPORT")) {
    // The entry point was the SDK executable so ASAR loading is injected.
    global.ModLoader["ASAR_SUPPORT"] = true;
}
else {
    (0, asar_node_1.register)();
    (0, asar_node_1.addAsarToLookupPaths)();
    global.ModLoader["ASAR_SUPPORT"] = true;
}
if (opts.mods) {
    global.ModLoader["OVERRIDE_MODS_FOLDER"] = opts.mods;
}
if (opts.roms) {
    global.ModLoader["OVERRIDE_ROMS_FOLDER"] = opts.roms;
}
if (opts.cores) {
    global.ModLoader["OVERRIDE_CORES_FOLDER"] = opts.cores;
}
if (opts.config) {
    global.ModLoader["OVERRIDE_CONFIG_FILE"] = opts.config;
}
if (opts.extradata) {
    global.ModLoader["EXTRA_DATA"] = opts.extradata;
}
global.ModLoader["WINDOW_TITLE"] = "ModLoader64";
if (opts.windowTitle) {
    global.ModLoader["WINDOW_TITLE"] = global.ModLoader["WINDOW_TITLE"] + " " + opts.windowTitle;
}
let discord_id = "";
if (commander_1.default.discord) {
    discord_id = commander_1.default.discord;
}
if (opts.dir) {
    process.chdir(path_1.default.resolve(path_1.default.join(process.cwd(), opts.dir)));
}
if (opts.startdir) {
    global.ModLoader["startdir"] = opts.startdir;
}
else {
    global.ModLoader['startdir'] = process.cwd();
}
if (opts.ScreenWidth) {
    global.ModLoader["ScreenWidth"] = parseInt(opts.ScreenWidth);
}
if (opts.ScreenHeight) {
    global.ModLoader["ScreenHeight"] = parseInt(opts.ScreenHeight);
}
if (fs_1.default.existsSync('./console.log')) {
    fs_1.default.unlinkSync('./console.log');
}
if (fs_1.default.existsSync('../README.md')) {
    logger.level = 'all';
}
if (opts.logginglevel !== undefined) {
    logger.level = (opts.logginglevel);
}
logger.info(projectID);
logger.info('Authors: ', authors.toString());
logger.info('Version: ', version);
logger.info(`Github Contributors: ${contributors.toString()}`);
logger.info('Setting running directory: ' + process.cwd());
class logwrapper {
    logger;
    constructor(logger) {
        this.logger = logger;
    }
    info(...msg) {
        this.logger.info(msg.toString());
    }
    warn(...msg) {
        this.logger.warn(msg.toString());
    }
    error(...msg) {
        this.logger.error(msg.toString());
    }
    debug(...msg) {
        this.logger.debug(msg.toString());
    }
    setLevel(level) {
        this.logger.level = level;
    }
    getLogger(id) {
        let wrap = new logwrapper((0, log4js_1.getLogger)(id));
        return wrap;
    }
}
global.ModLoader["FIRST_RUN"] = !fs_1.default.existsSync("./ModLoader64-config.json");
if (global.ModLoader["OVERRIDE_CONFIG_FILE"]) {
    global.ModLoader["FIRST_RUN"] = !fs_1.default.existsSync(global.ModLoader["OVERRIDE_CONFIG_FILE"]);
}
const instance = new modloader64_1.default(new logwrapper(logger), discord_id);
instance.start();
//# sourceMappingURL=loader.js.map