From 8e38ef070869cb3ac0c3f0dd1405d11b8d322ec0 Mon Sep 17 00:00:00 2001 From: "Thomas (DJ1TJOO)" <44841260+DJ1TJOO@users.noreply.github.com> Date: Thu, 3 Feb 2022 20:03:02 +0100 Subject: [PATCH 1/2] Modloader custom items fix (#1369) * Added item register and resolver for savegames * Changed new item type example to register * Fixed typings --- mod_examples/new_item_type.js | 26 ++++++++++++++------------ src/js/game/base_item.js | 2 +- src/js/game/item_resolver.js | 6 ++++++ src/js/mods/mod_interface.js | 13 ++++++++++++- src/js/savegame/serialization.js | 2 +- 5 files changed, 34 insertions(+), 15 deletions(-) diff --git a/mod_examples/new_item_type.js b/mod_examples/new_item_type.js index 3cd52cef..3f47d4d2 100644 --- a/mod_examples/new_item_type.js +++ b/mod_examples/new_item_type.js @@ -120,19 +120,21 @@ class Mod extends shapez.Mod { this.modInterface.registerSprite("sprites/fluids/water.png", RESOURCES["water.png"]); // Make the item spawn on the map - this.modInterface.runAfterMethod(shapez.MapChunk, "generatePatches", function ({ - rng, - chunkCenter, - distanceToOriginInChunks, - }) { - // Generate a simple patch - // ALWAYS use rng and NEVER use Math.random() otherwise the map will look different - // every time you resume the game - if (rng.next() > 0.8) { - const fluidType = rng.choice(Array.from(Object.keys(enumFluidType))); - this.internalGeneratePatch(rng, 4, FLUID_ITEM_SINGLETONS[fluidType]); + this.modInterface.runAfterMethod( + shapez.MapChunk, + "generatePatches", + function ({ rng, chunkCenter, distanceToOriginInChunks }) { + // Generate a simple patch + // ALWAYS use rng and NEVER use Math.random() otherwise the map will look different + // every time you resume the game + if (rng.next() > 0.8) { + const fluidType = rng.choice(Array.from(Object.keys(enumFluidType))); + this.internalGeneratePatch(rng, 4, FLUID_ITEM_SINGLETONS[fluidType]); + } } - }); + ); + + this.modInterface.registerItem(FluidItem, itemData => FLUID_ITEM_SINGLETONS[itemData]); } } diff --git a/src/js/game/base_item.js b/src/js/game/base_item.js index ae569ddf..f6ed1672 100644 --- a/src/js/game/base_item.js +++ b/src/js/game/base_item.js @@ -15,7 +15,7 @@ export class BaseItem extends BasicSerializableObject { return "base_item"; } - /** @returns {object} */ + /** @returns {import("../savegame/serialization").Schema} */ static getSchema() { return {}; } diff --git a/src/js/game/item_resolver.js b/src/js/game/item_resolver.js index da15fa0c..ff91b0a3 100644 --- a/src/js/game/item_resolver.js +++ b/src/js/game/item_resolver.js @@ -4,6 +4,8 @@ import { BooleanItem, BOOL_TRUE_SINGLETON, BOOL_FALSE_SINGLETON } from "./items/ import { ShapeItem } from "./items/shape_item"; import { ColorItem, COLOR_ITEM_SINGLETONS } from "./items/color_item"; +export const MODS_ADDITIONAL_ITEMS = {}; + /** * Resolves items so we share instances * @param {import("../savegame/savegame_serializer").GameRoot} root @@ -13,6 +15,10 @@ export function itemResolverSingleton(root, data) { const itemType = data.$; const itemData = data.data; + if (MODS_ADDITIONAL_ITEMS[itemType]) { + return MODS_ADDITIONAL_ITEMS[itemType](itemData); + } + switch (itemType) { case BooleanItem.getId(): { return itemData ? BOOL_TRUE_SINGLETON : BOOL_FALSE_SINGLETON; diff --git a/src/js/mods/mod_interface.js b/src/js/mods/mod_interface.js index 13bdb9d2..295014ca 100644 --- a/src/js/mods/mod_interface.js +++ b/src/js/mods/mod_interface.js @@ -17,7 +17,7 @@ import { Loader } from "../core/loader"; import { LANGUAGES } from "../languages"; import { matchDataRecursive, T } from "../translations"; import { gBuildingVariants, registerBuildingVariant } from "../game/building_codes"; -import { gComponentRegistry, gMetaBuildingRegistry } from "../core/global_registries"; +import { gComponentRegistry, gItemRegistry, gMetaBuildingRegistry } from "../core/global_registries"; import { MODS_ADDITIONAL_SHAPE_MAP_WEIGHTS } from "../game/map_chunk"; import { MODS_ADDITIONAL_SYSTEMS } from "../game/game_system_manager"; import { MOD_CHUNK_DRAW_HOOKS } from "../game/map_chunk_view"; @@ -28,6 +28,8 @@ import { ModMetaBuilding } from "./mod_meta_building"; import { BaseHUDPart } from "../game/hud/base_hud_part"; import { Vector } from "../core/vector"; import { GameRoot } from "../game/root"; +import { BaseItem } from "../game/base_item"; +import { MODS_ADDITIONAL_ITEMS } from "../game/item_resolver"; /** * @typedef {{new(...args: any[]): any, prototype: any}} constructable @@ -190,6 +192,15 @@ export class ModInterface { } } + /** + * @param {typeof BaseItem} item + * @param {(itemData: any) => BaseItem} resolver + */ + registerItem(item, resolver) { + gItemRegistry.register(item); + MODS_ADDITIONAL_ITEMS[item.getId()] = resolver; + } + /** * * @param {typeof Component} component diff --git a/src/js/savegame/serialization.js b/src/js/savegame/serialization.js index 770f166f..682558b6 100644 --- a/src/js/savegame/serialization.js +++ b/src/js/savegame/serialization.js @@ -192,7 +192,7 @@ export class BasicSerializableObject { return schema; } - /** @returns {object} */ + /** @returns {object | string | number} */ serialize() { return serializeSchema( this, From aa8d105e141f2183a1447a79e47a2d868dde9110 Mon Sep 17 00:00:00 2001 From: tobspr Date: Thu, 3 Feb 2022 20:26:29 +0100 Subject: [PATCH 2/2] Revert being able to override exports since it breaks in prod --- gulp/webpack.config.js | 3 --- gulp/webpack.production.config.js | 3 --- src/js/mods/modloader.js | 5 ++--- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/gulp/webpack.config.js b/gulp/webpack.config.js index 3c07d7cc..7db0bf0b 100644 --- a/gulp/webpack.config.js +++ b/gulp/webpack.config.js @@ -93,9 +93,6 @@ module.exports = ({ watch = false, standalone = false, chineseVersion = false, w end: "typehints:end", }, }, - { - loader: path.resolve(__dirname, "mod.js"), - }, ], }, { diff --git a/gulp/webpack.production.config.js b/gulp/webpack.production.config.js index 567ca38c..fdd477b9 100644 --- a/gulp/webpack.production.config.js +++ b/gulp/webpack.production.config.js @@ -230,9 +230,6 @@ module.exports = ({ { pattern: /globalConfig\.debug/g, replacement: () => "''" }, ], }), - { - loader: path.resolve(__dirname, "mod.js"), - }, ], }, { diff --git a/src/js/mods/modloader.js b/src/js/mods/modloader.js index 356b7d6b..daf0766e 100644 --- a/src/js/mods/modloader.js +++ b/src/js/mods/modloader.js @@ -112,8 +112,7 @@ export class ModLoader { // @ts-ignore const module = modules(key); for (const member in module) { - if (member === "default" || member === "$s") { - // Setter + if (member === "default") { continue; } if (exports[member]) { @@ -125,7 +124,7 @@ export class ModLoader { return module[member]; }, set(v) { - module["$s"](member, v); + throw new Error("Overriding the shapez exports is currently not possible"); }, }); }