1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-13 18:21:51 +00:00

Allow overriding existing methods

This commit is contained in:
tobspr 2022-01-16 11:40:01 +01:00
parent 1bd569f89c
commit 93f269d62d
4 changed files with 68 additions and 53 deletions

View File

@ -0,0 +1,36 @@
/**
* This shows how to modify an existing building
*/
registerMod(() => {
return class ModImpl extends shapez.Mod {
constructor(app, modLoader) {
super(
app,
{
website: "https://tobspr.io",
author: "tobspr",
name: "Mod Example: Modify existing building",
version: "1",
id: "modify-existing-building",
description: "Shows how to modify an existing building",
},
modLoader
);
}
init() {
// Make Rotator always unlocked
this.modInterface.replaceMethod(shapez.MetaRotaterBuilding, "getIsUnlocked", function () {
return true;
});
// Add some custom stats to the info panel when selecting the building
this.modInterface.replaceMethod(shapez.MetaRotaterBuilding, "getAdditionalStatistics", function (
root,
variant
) {
return [["Awesomeness", 5]];
});
}
};
});

View File

@ -22,31 +22,34 @@ import { ItemProducerComponent } from "./components/item_producer";
import { GoalAcceptorComponent } from "./components/goal_acceptor";
export function initComponentRegistry() {
gComponentRegistry.register(StaticMapEntityComponent);
gComponentRegistry.register(BeltComponent);
gComponentRegistry.register(ItemEjectorComponent);
gComponentRegistry.register(ItemAcceptorComponent);
gComponentRegistry.register(MinerComponent);
gComponentRegistry.register(ItemProcessorComponent);
gComponentRegistry.register(UndergroundBeltComponent);
gComponentRegistry.register(HubComponent);
gComponentRegistry.register(StorageComponent);
gComponentRegistry.register(WiredPinsComponent);
gComponentRegistry.register(BeltUnderlaysComponent);
gComponentRegistry.register(WireComponent);
gComponentRegistry.register(ConstantSignalComponent);
gComponentRegistry.register(LogicGateComponent);
gComponentRegistry.register(LeverComponent);
gComponentRegistry.register(WireTunnelComponent);
gComponentRegistry.register(DisplayComponent);
gComponentRegistry.register(BeltReaderComponent);
gComponentRegistry.register(FilterComponent);
gComponentRegistry.register(ItemProducerComponent);
gComponentRegistry.register(GoalAcceptorComponent);
const components = [
StaticMapEntityComponent,
BeltComponent,
ItemEjectorComponent,
ItemAcceptorComponent,
MinerComponent,
ItemProcessorComponent,
UndergroundBeltComponent,
HubComponent,
StorageComponent,
WiredPinsComponent,
BeltUnderlaysComponent,
WireComponent,
ConstantSignalComponent,
LogicGateComponent,
LeverComponent,
WireTunnelComponent,
DisplayComponent,
BeltReaderComponent,
FilterComponent,
ItemProducerComponent,
GoalAcceptorComponent,
];
components.forEach(component => gComponentRegistry.register(component));
// IMPORTANT ^^^^^ UPDATE ENTITY COMPONENT STORAGE AFTERWARDS
// Sanity check - If this is thrown, you (=me, lol) forgot to add a new component here
// Sanity check - If this is thrown, you forgot to add a new component here
assert(
// @ts-ignore

View File

@ -21,37 +21,6 @@ if (window.coreThreadLoadedCb) {
window.coreThreadLoadedCb();
}
// Logrocket
// if (!G_IS_DEV && !G_IS_STANDALONE) {
// const monthlyUsers = 300; // thousand
// const logrocketLimit = 10; // thousand
// const percentageOfUsers = logrocketLimit / monthlyUsers;
// if (Math.random() <= percentageOfUsers) {
// logger.log("Analyzing this session with logrocket");
// const logrocket = require("logrocket");
// logrocket.init("p1x9zh/shapezio");
// try {
// logrocket.getSessionURL(function (sessionURL) {
// logger.log("Connected lockrocket to GA");
// // @ts-ignore
// try {
// window.ga("send", {
// hitType: "event",
// eventCategory: "LogRocket",
// eventAction: sessionURL,
// });
// } catch (ex) {
// logger.warn("Logrocket connection to analytics failed:", ex);
// }
// });
// } catch (ex) {
// logger.warn("Logrocket connection to analytics failed:", ex);
// }
// }
// }
console.log(
`%cshapez.io %c\n© 2022 tobspr Games\nCommit %c${G_BUILD_COMMIT_HASH}%c on %c${new Date(
G_BUILD_TIME

View File

@ -355,4 +355,11 @@ export class ModInterface {
}
});
}
/**
* Patches a method on a given object
*/
replaceMethod(classHandle, methodName, override) {
classHandle.prototype[methodName] = override;
}
}