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

Added support for packed mods #64

Merged
merged 19 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
3 changes: 2 additions & 1 deletion ccloader/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module.exports = {
"node": true
},
"globals": {
"semver": true
"semver": true,
"JSZip": true
},
"extends": "eslint:recommended",
"parserOptions": {
Expand Down
Empty file added ccloader/blank.html
Empty file.
3 changes: 2 additions & 1 deletion ccloader/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
</script>
<script type="text/javascript" src="js/lib/2.5.3-crypto-md5.js"></script>
<script type="text/javascript" src="js/lib/semver.browser.js"></script>
<script type="text/javascript" src="js/packed.js"></script>
<script type="module" src="js/main.js"></script>
</head>
<iframe id="frame"></iframe>
<iframe id="frame" src="blank.html"></iframe>
<div id="overlay"></div>
<h1 id="status" class="title">Initializing CCLoader</h1>
<div id="ui" class="ui"></div>
Expand Down
77 changes: 51 additions & 26 deletions ccloader/js/ccloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Loader } from './loader.js';
import { Plugin } from './plugin.js';
import { Greenworks } from './greenworks.js';

const CCLOADER_VERSION = '2.16.0';
const CCLOADER_VERSION = '2.17.0';

export class ModLoader {
constructor() {
Expand All @@ -25,15 +25,17 @@ export class ModLoader {
/** @type {{[name: string]: string}} */
this.versions = {};

this._buildCrosscodeVersion();
this._initializeLegacy();
}

/**
* Loads and starts the game. It then loads the definitions and mods
*/
async startGame() {
await this._buildCrosscodeVersion();
await this._initializeLegacy();

await this.loader.initialize();
await this._initializeServiceWorker();

await this._loadModPackages();
this._orderCheckMods();
Expand All @@ -52,17 +54,34 @@ export class ModLoader {
this.loader.continue(this.frame);
await this._waitForGame();

this._executeLegacy();
await this._executeLegacy();
await this._executeMain();


this._fireLoadEvent();
this._removeOverlay();
}

async _initializeServiceWorker() {
this._serviceWorker = await this.filemanager.loadServiceWorker('serviceworker.js', this._getGameWindow());
}

_loadPackedMods() {
const packedMods = this.filemanager.getAllModPackages();
const names = packedMods.map((m) => m.substring(12, m.length));

this._sendPackedModNames(names);
this.filemanager.setPackedMods(names);
return packedMods;
}

_sendPackedModNames(names) {
this._serviceWorker.postMessage(names);
}

_buildCrosscodeVersion(){
async _buildCrosscodeVersion(){
try {
const {changelog} = JSON.parse(this.filemanager.getResource('./assets/data/changelog.json'));
const {changelog} = JSON.parse(await this.filemanager.getResourceAsync('assets/data/changelog.json'));
this.ccVersion = changelog[0].version;
} catch (e) {
let ccVersion = localStorage.getItem('cc.version');
Expand Down Expand Up @@ -90,6 +109,12 @@ export class ModLoader {
*/
_getModPackages() {
const modFiles = this.filemanager.getAllModsFiles();

const packedMods = this._loadPackedMods();
for (const packed of packedMods) {
modFiles.push(packed.substring(0, packed.length) + '/package.json');
}

/** @type {Mod[]} */
const mods = [];
for (const modFile of modFiles) {
Expand Down Expand Up @@ -369,12 +394,12 @@ export class ModLoader {
}

_initializeLegacy() {
this._initializeTable();
return this._initializeTable();
}

_executeLegacy() {
this._executeMainDb();
this._executeDb();
return this._executeDb();
}

/**
Expand All @@ -394,9 +419,9 @@ export class ModLoader {
* Reloads all definitions
* @deprecated
*/
reloadTables() {
async reloadTables() {
this.modTables = {};
this._createTable(this.filemanager.getTableName());
this._createTable(await this.filemanager.getTableName());
this.table.execute(this._getGameWindow(), this._getGameWindow());
for (const mod of this.mods) {
mod.executeTable(this);
Expand All @@ -407,16 +432,16 @@ export class ModLoader {
* Loads a cached table if available and creates a new one otherwise
* @deprecated
*/
_initializeTable() {
async _initializeTable() {
if (this._isLegacy()) {
const tableName = this.filemanager.getTableName();
const tableName = await this.filemanager.getTableName();
if (this.filemanager.tableExists(tableName)) {
this._loadTable(tableName);
await this._loadTable(tableName);
} else {
this._createTable(tableName);
await this._createTable(tableName);
}
} else {
this._loadTable('final.table');
await this._loadTable('final.table');
}
}

Expand All @@ -425,11 +450,11 @@ export class ModLoader {
* @param {string} tableName
* @deprecated
*/
_createTable(tableName) {
async _createTable(tableName) {
this._setStatus('Initializing Mapping');
console.log('Reading files...');
const jscode = this.filemanager.getResource('assets/js/game.compiled.js');
const dbtext = this.filemanager.getResource('ccloader/data/definitions.db');
const jscode = await this.filemanager.getResourceAsync('assets/js/game.compiled.js');
const dbtext = await this.filemanager.getResourceAsync('ccloader/data/definitions.db');

try {
const dbdef = JSON.parse(dbtext);
Expand All @@ -438,7 +463,7 @@ export class ModLoader {
console.log('Analysing...');
this.table = this.acorn.analyse(dbdef);
console.log('Writing...');
this.filemanager.saveTable(tableName, this.table);
await this.filemanager.saveTable(tableName, this.table);
console.log('Finished!');
} catch (e) {
console.error('Could not load definitions.', e);
Expand All @@ -450,9 +475,9 @@ export class ModLoader {
* @param {string} tableName
* @deprecated
*/
_loadTable(tableName) {
const hash = this._isLegacy() ? this.filemanager.getDefintionHash() : 'final';
this.table = this.filemanager.loadTable(tableName, hash);
async _loadTable(tableName) {
const hash = this._isLegacy() ? await this.filemanager.getDefintionHash() : 'final';
this.table = await this.filemanager.loadTable(tableName, hash);
if(!this.table) {
this._createTable(tableName);
}
Expand All @@ -463,11 +488,11 @@ export class ModLoader {
* Applies all definitions and loads the mods
* @deprecated
*/
_executeDb() {
async _executeDb() {
const entries = Object.assign({}, this.table.entries);

try {
this._executeModTables(entries);
await this._executeModTables(entries);
this._finalizeEntries(entries);
} catch (err) {
console.error('An error occured while loading mod tables', err);
Expand All @@ -479,10 +504,10 @@ export class ModLoader {
* @param {{[key: string]: string}} entries
* @deprecated
*/
_executeModTables(entries) {
async _executeModTables(entries) {
for (const mod of this.mods) {
if (mod.isEnabled) {
const table = mod.initializeTable(this);
const table = await mod.initializeTable(this);
if (table) {
Object.assign(entries, table.entries);
mod.executeTable(this);
Expand Down
Loading