Skip to content

Commit

Permalink
Merge pull request #107 from CCDirectLink/multiple-patches
Browse files Browse the repository at this point in the history
Allow a mod to add multiple runtime patches
  • Loading branch information
2767mr committed May 30, 2024
2 parents 67a5b55 + bf7dbff commit 5aba109
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion assets/mods/simplify/ccmod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "Simplify",
"version": "2.13.0",
"version": "2.14.0",
"title": "Simplify",
"description": "Library mod for CCLoader2.",
"repository": "https://github.com/CCDirectLink/CCLoader",
Expand Down
8 changes: 6 additions & 2 deletions assets/mods/simplify/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
*
* @param {string|Mod} mod
* @param {string} name
* @returns {string}
* @returns {string|string[]}
*/
getAsset(mod, name) {
if(!mod) {
Expand All @@ -164,7 +164,11 @@
for (const mod of window.activeMods) {
const asset = mod.getAsset(name);
if(asset) {
result.push(asset);
if (typeof asset === 'string') {
result.push(asset);
} else {
result.push(...asset);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion assets/mods/simplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"prestart": "prestart.js",
"plugin": "plugin.js",
"hidden": true,
"version": "2.13.0",
"version": "2.14.0",
"ccmodDependencies": {
"ccloader": "^2.22.0",
"crosscode": "^1.0.0"
Expand Down
23 changes: 18 additions & 5 deletions assets/mods/simplify/postloadModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ import CustomDebugState from './lib/custom-debug-state.js';
for (const mod of window.activeMods) {
const asset = mod.getAsset(name);
if(asset) {
result.push(asset);
if (typeof asset === 'string') {
result.push(asset);
} else {
result.push(...asset);
}
}
}

Expand All @@ -425,10 +429,19 @@ import CustomDebugState from './lib/custom-debug-state.js';
for (const mod of window.activeMods) {
const asset = mod.getAsset(name);
if(asset) {
result.push({
mod: mod,
path: asset
});
if (typeof asset === 'string') {
result.push({
mod: mod,
path: asset
});
} else {
for (const subasset of asset) {
result.push({
mod: mod,
path: subasset
});
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ccloader/ccmod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "ccloader",
"version": "2.23.3",
"version": "2.24.0",
"title": "CCLoader",
"description": "Modloader for CrossCode. This or a similar modloader is needed for most mods.",
"repository": "https://github.com/CCDirectLink/CCLoader",
Expand Down
2 changes: 1 addition & 1 deletion ccloader/js/ccloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Plugin } from './plugin.js';
import { Greenworks } from './greenworks.js';
import { Package } from './package.js';

const CCLOADER_VERSION = '2.23.3';
const CCLOADER_VERSION = '2.24.0';
const KNOWN_EXTENSIONS = ["post-game", "manlea", "ninja-skin", "fish-gear", "flying-hedgehag", "scorpion-robo", "snowman-tank"]

export class ModLoader {
Expand Down
27 changes: 23 additions & 4 deletions ccloader/js/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export class Mod {

/** @type {string[]} */
this.assets = [];
/** @type {Record<string, string|string[]>} */
this.runtimeAssets = {};
/** @type {Record<string, string>} */
this.dependencies = {};
}
Expand Down Expand Up @@ -60,18 +62,19 @@ export class Mod {
/**
*
* @param {string} path
* @returns {string|string[]|undefined}
*/
getAsset(path){
path = path.replace(/\\/g, '/').trim();

if (this.runtimeAssets && this.runtimeAssets[path]) {
if (this.runtimeAssets[path]) {
return this.runtimeAssets[path];
}

const base = this.baseDirectory.substr(7) + 'assets/';
const base = this.baseDirectory.substring(7) + 'assets/';
for (const asset of this.assets) {
if (asset.startsWith(base)) {
if (asset.substr(base.length) === path) {
if (asset.substring(base.length) === path) {
return asset;
}
} else {
Expand All @@ -85,12 +88,28 @@ export class Mod {
/**
*
* @param {string} original
* @param {string} newPath
* @param {string|string[]} newPath
*/
setAsset(original, newPath){
this.runtimeAssets[original] = newPath;
}

/**
* Adds a patch to the mod.
* @param {string} original The original file path without .patch
* @param {string[]} patchPath The patch file path with .patch
*/
addPatch(original, ...patchPath) {
const originalPatchPath = original + '.patch';
let list = this.runtimeAssets[originalPatchPath] || [];
if (typeof patchPath === 'string') {
list = [list, ...patchPath];
} else {
list.push(...patchPath);
}
this.runtimeAssets[originalPatchPath] = list;
}


async _loadPlugin() {
window._tmp = this.plugin;
Expand Down
11 changes: 9 additions & 2 deletions ccloader/js/types/mod.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ declare namespace ccloader {
get baseDirectory(): string;
get isEnabled(): boolean;

getAsset(path: string): string | undefined;
setAsset(original: string, newPath: string): void;
getAsset(path: string): string | string[] | undefined;
setAsset(original: string, newPath: string | string[]): void;

/**
* Adds a patch to the mod.
* @param original The original file path without .patch
* @param patchPath The patch file path with .patch
*/
addPatch(original: string, ...patchPath: string[]): void;
}
}

0 comments on commit 5aba109

Please sign in to comment.