bc-deeplib 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/deeplib.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare module 'bc-deeplib/base/base_module' {
2
- import { BaseSettingsModel, Subscreen } from 'bc-deeplib/deeplib';
2
+ import { BaseSettingsModel, SettingsModel, Subscreen } from 'bc-deeplib/deeplib';
3
3
  /**
4
4
  * An abstract foundation for modular systems that require:
5
5
  * - Optional settings screens
@@ -57,7 +57,7 @@ declare module 'bc-deeplib/base/base_module' {
57
57
  * If some settings already exist, they will be merged with defaults.
58
58
  * Existing values will NOT be overwritten.
59
59
  */
60
- registerDefaultSettings(): void;
60
+ registerDefaultSettings(target: SettingsModel): void;
61
61
  /**
62
62
  * Provides default settings for this module.
63
63
  * Subclasses should override this getter to return their defaults.
@@ -152,7 +152,7 @@ declare module 'bc-deeplib/base/base_subscreen' {
152
152
  */
153
153
  export type Subscreen = new (module?: BaseModule) => BaseSubscreen;
154
154
  /** Switches the active subscreen in the global `GUI` instance. */
155
- export function setSubscreen(subscreen: BaseSubscreen | string | null): Promise<void>;
155
+ export function setSubscreen(subscreen: BaseSubscreen | string): Promise<void>;
156
156
  /**
157
157
  * Abstract base class for creating settings/configuration subscreens in a module.
158
158
  *
@@ -184,7 +184,7 @@ declare module 'bc-deeplib/base/base_subscreen' {
184
184
  protected static readonly subscreenOptions: SubscreenOptions;
185
185
  constructor(module?: BaseModule);
186
186
  /** Changes the currently active subscreen. */
187
- setSubscreen(screen: BaseSubscreen | string | null): Promise<void>;
187
+ setSubscreen(screen: BaseSubscreen | string): Promise<void>;
188
188
  /** Gets this subscreen's settings object from its parent module. */
189
189
  get settings(): BaseSettingsModel;
190
190
  /** Updates this subscreen's settings in its parent module. */
@@ -310,7 +310,7 @@ declare module 'bc-deeplib/base/elements_typings' {
310
310
 
311
311
  }
312
312
  declare module 'bc-deeplib/base/initialization' {
313
- import { BaseModule, ModSdkManager, BaseMigrator, ModStorage, MainMenuOptions, TranslationOptions } from 'bc-deeplib/deeplib';
313
+ import { BaseModule, ModSdkManager, ModStorage, MainMenuOptions, TranslationOptions, Logger } from 'bc-deeplib/deeplib';
314
314
  /** Configuration object for initializing a mod via `initMod`. */
315
315
  interface InitOptions {
316
316
  /**
@@ -327,11 +327,6 @@ declare module 'bc-deeplib/base/initialization' {
327
327
  * Modules are initialized, loaded, and run in order.
328
328
  */
329
329
  modules?: BaseModule[];
330
- /**
331
- * List of data migration handlers to register with the `VersionModule`.
332
- * Each `BaseMigrator` handles upgrading data from one version to another.
333
- */
334
- migrators?: BaseMigrator[];
335
330
  /** Configuration for customizing the main menu when the mod is active. */
336
331
  mainMenuOptions?: MainMenuOptions;
337
332
  /**
@@ -357,6 +352,11 @@ declare module 'bc-deeplib/base/initialization' {
357
352
  * Initialized by `initMod()`.
358
353
  */
359
354
  export let sdk: ModSdkManager;
355
+ /**
356
+ * Mod specific logger instance.
357
+ * Initialized by `initMod()`.
358
+ */
359
+ export let logger: Logger;
360
360
  /**
361
361
  * Entry point for initializing a mod. Handles:
362
362
  * - Setting up the Mod SDK
@@ -584,10 +584,20 @@ declare module 'bc-deeplib/modules/gui' {
584
584
  declare module 'bc-deeplib/modules/version' {
585
585
  import { BaseMigrator, BaseModule } from 'bc-deeplib/deeplib';
586
586
  export type VersionModuleOptions = {
587
+ /**
588
+ * List of data migration handlers to register with the `VersionModule`.
589
+ * Each `BaseMigrator` handles upgrading data from one version to another.
590
+ */
591
+ migrators?: BaseMigrator[];
592
+ /** Message to display when a new version is detected */
587
593
  newVersionMessage?: string;
594
+ /** Optional lifecycle hook. Runs before each migration */
588
595
  beforeEach?: () => void;
596
+ /** Optional lifecycle hook. Runs after each migration */
589
597
  afterEach?: () => void;
598
+ /** Optional lifecycle hook. Runs before all migrations */
590
599
  beforeAll?: () => void;
600
+ /** Optional lifecycle hook. Runs after all migrations */
591
601
  afterAll?: () => void;
592
602
  };
593
603
  /**
@@ -606,17 +616,12 @@ declare module 'bc-deeplib/modules/version' {
606
616
  private static isItNewVersion;
607
617
  /** The current mod version (retrieved from `ModSdkManager.ModInfo.version`) */
608
618
  private static version;
609
- /** Message to display when a new version is detected */
610
619
  private static newVersionMessage?;
611
620
  /** List of registered migration handlers, sorted by version */
612
621
  private static migrators;
613
- /** Optional lifecycle hook. Runs before each migration */
614
622
  private static beforeEach?;
615
- /** Optional lifecycle hook. Runs after each migration */
616
623
  private static afterEach?;
617
- /** Optional lifecycle hook. Runs before all migrations */
618
624
  private static beforeAll?;
619
- /** Optional lifecycle hook. Runs after all migrations */
620
625
  private static afterAll?;
621
626
  constructor(options: VersionModuleOptions);
622
627
  /**
@@ -639,11 +644,6 @@ declare module 'bc-deeplib/modules/version' {
639
644
  * is newer than the previously stored version.
640
645
  */
641
646
  private static checkVersionMigration;
642
- /**
643
- * Registers a new migrator for handling version-specific changes.
644
- * Migrators are sorted by their `MigrationVersion` in ascending order.
645
- */
646
- static registerMigrator(migrator: BaseMigrator): void;
647
647
  /** Sends the currently configured "new version" message to the local player. */
648
648
  static sendNewVersionMessage(): void;
649
649
  /**
@@ -705,7 +705,7 @@ declare module 'bc-deeplib/screens/import_export' {
705
705
  /** Imports mod data using the specified method. */
706
706
  dataImport(transferMethod: DataTransferMethod): Promise<void>;
707
707
  /** Saves data to a file using the browser's save dialog. */
708
- exportToFile(data: string, defaultFileName: string): Promise<void>;
708
+ exportToFile(data: string, defaultFileName: string): Promise<boolean>;
709
709
  /** Opens a file picker and reads the selected file's contents, importing the data. */
710
710
  importFromFile(): Promise<string | null>;
711
711
  /** Copies the given data to the clipboard. */
@@ -850,7 +850,7 @@ declare module 'bc-deeplib/utilities/elements/elements' {
850
850
  };
851
851
  function elementCreateButton(options: Omit<Button, 'type'>): HTMLButtonElement;
852
852
  function elementCreateCheckbox(options: Omit<Checkbox, 'type'>): HTMLElement;
853
- function elementCreateCustom(options: Omit<Custom, 'type'>): HTMLElement;
853
+ function elementCreateCustom(options: Omit<Custom, 'type'>): HTMLHeadElement;
854
854
  function elementCreateInput(options: Input): HTMLElement;
855
855
  function elementCreateLabel(options: Omit<Label, 'type'>): HTMLElement;
856
856
  function elementCreateDropdown(options: Omit<Dropdown, 'type'>): HTMLDivElement;
package/dist/deeplib.js CHANGED
@@ -230,8 +230,10 @@ var BaseModule = class {
230
230
  if (!this.settingsStorage) return {};
231
231
  if (!modStorage.playerStorage) {
232
232
  Player[modName] = {};
233
- this.registerDefaultSettings();
234
- } else if (!modStorage.playerStorage[this.settingsStorage]) this.registerDefaultSettings();
233
+ this.registerDefaultSettings(modStorage.playerStorage);
234
+ } else if (!modStorage.playerStorage[this.settingsStorage]) {
235
+ this.registerDefaultSettings(modStorage.playerStorage);
236
+ }
235
237
  return modStorage.playerStorage[this.settingsStorage];
236
238
  }
237
239
  /**
@@ -244,8 +246,10 @@ var BaseModule = class {
244
246
  if (!this.settingsStorage) return;
245
247
  if (!storage.playerStorage) {
246
248
  Player[modName] = {};
247
- this.registerDefaultSettings();
248
- } else if (!storage.playerStorage[this.settingsStorage]) this.registerDefaultSettings();
249
+ this.registerDefaultSettings(modStorage.playerStorage);
250
+ } else if (!storage.playerStorage[this.settingsStorage]) {
251
+ this.registerDefaultSettings(modStorage.playerStorage);
252
+ }
249
253
  storage.playerStorage[this.settingsStorage] = value;
250
254
  }
251
255
  /**
@@ -254,7 +258,7 @@ var BaseModule = class {
254
258
  * Subclasses can override to perform additional setup.
255
259
  */
256
260
  init() {
257
- this.registerDefaultSettings();
261
+ this.registerDefaultSettings(modStorage.playerStorage);
258
262
  }
259
263
  /**
260
264
  * Registers default settings for this module in persistent storage.
@@ -263,11 +267,11 @@ var BaseModule = class {
263
267
  * If some settings already exist, they will be merged with defaults.
264
268
  * Existing values will NOT be overwritten.
265
269
  */
266
- registerDefaultSettings() {
270
+ registerDefaultSettings(target) {
267
271
  const storage = this.settingsStorage;
268
272
  const defaults = this.defaultSettings;
269
273
  if (!storage || !defaults) return;
270
- Player[ModSdkManager.ModInfo.name][storage] = Object.assign(defaults, Player[ModSdkManager.ModInfo.name][storage] ?? {});
274
+ target[storage] = Object.assign(defaults, target[storage] ?? {});
271
275
  }
272
276
  /**
273
277
  * Provides default settings for this module.
@@ -422,7 +426,7 @@ var BaseSubscreen = class _BaseSubscreen {
422
426
  load() {
423
427
  for (const module of modules()) {
424
428
  if (!module.settingsScreen) continue;
425
- if (!module.settings || !Object.keys(module.settings).length) module.registerDefaultSettings();
429
+ if (!module.settings || !Object.keys(module.settings).length) module.registerDefaultSettings(modStorage.playerStorage);
426
430
  }
427
431
  _BaseSubscreen.currentPage = 1;
428
432
  layout.getSubscreen();
@@ -568,8 +572,10 @@ var BaseSubscreen = class _BaseSubscreen {
568
572
  ElementSetFontSize(subscreen, "auto");
569
573
  ElementSetPosition(settingsDiv, 530 - offset, 170);
570
574
  ElementSetSize(settingsDiv, this.options.settingsWidth ?? 1e3 + offset, 660);
571
- ElementSetPosition("deeplib-subscreen-title", 530 - offset, 75);
572
- ElementSetSize("deeplib-subscreen-title", 800, 60);
575
+ if (this.options.doShowTitle) {
576
+ ElementSetPosition("deeplib-subscreen-title", 530 - offset, 75);
577
+ ElementSetSize("deeplib-subscreen-title", 800, 60);
578
+ }
573
579
  ElementSetPosition("deeplib-nav-menu", 1905, 75, "top-right");
574
580
  ElementSetSize("deeplib-nav-menu", null, 90);
575
581
  ElementSetPosition(advElement.getTooltip() || "", 250, 850);
@@ -938,16 +944,18 @@ var styles_default = `.deeplib-subscreen,
938
944
  // src/base/initialization.ts
939
945
  var modStorage;
940
946
  var sdk;
947
+ var logger;
941
948
  function initMod(options) {
942
949
  sdk = new ModSdkManager(options.modInfo.info, options.modInfo.options);
943
950
  const MOD_NAME = ModSdkManager.ModInfo.name;
944
951
  modStorage = new ModStorage(ModSdkManager.ModInfo.name);
952
+ logger = new Logger(MOD_NAME);
945
953
  Style.injectInline("deeplib-style", styles_default);
946
- deepLibLogger.debug(`Init wait for ${MOD_NAME}`);
954
+ logger.debug("Init wait");
947
955
  if (CurrentScreen == null || CurrentScreen === "Login") {
948
956
  options.beforeLogin?.();
949
957
  const removeHook = sdk.hookFunction("LoginResponse", 0, (args, next) => {
950
- deepLibLogger.debug(`Init for ${MOD_NAME}! LoginResponse caught: `, args);
958
+ logger.debug("Init! LoginResponse caught: ", args);
951
959
  next(args);
952
960
  const response = args[0];
953
961
  if (response === "InvalidNamePassword") return next(args);
@@ -957,7 +965,7 @@ function initMod(options) {
957
965
  }
958
966
  });
959
967
  } else {
960
- deepLibLogger.debug(`Already logged in, initing ${MOD_NAME}`);
968
+ logger.debug(`Already logged in, initing ${MOD_NAME}`);
961
969
  init(options);
962
970
  }
963
971
  }
@@ -972,11 +980,6 @@ async function init(options) {
972
980
  unloadMod();
973
981
  return;
974
982
  }
975
- if (options.migrators) {
976
- for (const m of options.migrators) {
977
- VersionModule.registerMigrator(m);
978
- }
979
- }
980
983
  await options.initFunction?.();
981
984
  if (options.mainMenuOptions)
982
985
  MainMenu.setOptions(options.mainMenuOptions);
@@ -987,11 +990,10 @@ async function init(options) {
987
990
  }
988
991
  }
989
992
  window[MOD_NAME + "Loaded"] = true;
990
- deepLibLogger.log(`Loaded ${MOD_NAME}! Version: ${MOD_VERSION}`);
993
+ logger.log(`Loaded! Version: ${MOD_VERSION}`);
991
994
  }
992
995
  __name(init, "init");
993
996
  function initModules(modulesToRegister) {
994
- const MOD_NAME = ModSdkManager.ModInfo.name;
995
997
  for (const module of modulesToRegister) {
996
998
  registerModule(module);
997
999
  }
@@ -1004,7 +1006,7 @@ function initModules(modulesToRegister) {
1004
1006
  for (const module of modules()) {
1005
1007
  module.run();
1006
1008
  }
1007
- deepLibLogger.debug(`Modules Loaded for ${MOD_NAME}.`);
1009
+ logger.debug("Modules Loaded.");
1008
1010
  return true;
1009
1011
  }
1010
1012
  __name(initModules, "initModules");
@@ -1012,7 +1014,7 @@ function unloadMod() {
1012
1014
  const MOD_NAME = ModSdkManager.ModInfo.name;
1013
1015
  unloadModules();
1014
1016
  delete window[MOD_NAME + "Loaded"];
1015
- deepLibLogger.debug(`Unloaded ${MOD_NAME}.`);
1017
+ logger.debug("Unloaded.");
1016
1018
  return true;
1017
1019
  }
1018
1020
  __name(unloadMod, "unloadMod");
@@ -1040,7 +1042,7 @@ function getModule(moduleType) {
1040
1042
  __name(getModule, "getModule");
1041
1043
 
1042
1044
  // src/migrators/base_migrator.ts
1043
- var BaseMigrator2 = class {
1045
+ var BaseMigrator = class {
1044
1046
  static {
1045
1047
  __name(this, "BaseMigrator");
1046
1048
  }
@@ -1125,21 +1127,20 @@ var VersionModule = class _VersionModule extends BaseModule {
1125
1127
  static isItNewVersion = false;
1126
1128
  /** The current mod version (retrieved from `ModSdkManager.ModInfo.version`) */
1127
1129
  static version;
1128
- /** Message to display when a new version is detected */
1129
1130
  static newVersionMessage = "";
1130
1131
  /** List of registered migration handlers, sorted by version */
1131
1132
  static migrators = [];
1132
- /** Optional lifecycle hook. Runs before each migration */
1133
1133
  static beforeEach;
1134
- /** Optional lifecycle hook. Runs after each migration */
1135
1134
  static afterEach;
1136
- /** Optional lifecycle hook. Runs before all migrations */
1137
1135
  static beforeAll;
1138
- /** Optional lifecycle hook. Runs after all migrations */
1139
1136
  static afterAll;
1140
1137
  constructor(options) {
1141
1138
  super();
1142
1139
  _VersionModule.newVersionMessage = options.newVersionMessage;
1140
+ if (options.migrators) {
1141
+ _VersionModule.migrators = options.migrators;
1142
+ _VersionModule.migrators.sort((a, b) => a.migrationVersion.localeCompare(b.migrationVersion));
1143
+ }
1143
1144
  _VersionModule.beforeEach = options.beforeEach;
1144
1145
  _VersionModule.afterEach = options.afterEach;
1145
1146
  _VersionModule.beforeAll = options.beforeAll;
@@ -1196,14 +1197,6 @@ var VersionModule = class _VersionModule extends BaseModule {
1196
1197
  }
1197
1198
  _VersionModule.afterAll?.();
1198
1199
  }
1199
- /**
1200
- * Registers a new migrator for handling version-specific changes.
1201
- * Migrators are sorted by their `MigrationVersion` in ascending order.
1202
- */
1203
- static registerMigrator(migrator) {
1204
- _VersionModule.migrators.push(migrator);
1205
- _VersionModule.migrators.sort((a, b) => a.migrationVersion.localeCompare(b.migrationVersion));
1206
- }
1207
1200
  /** Sends the currently configured "new version" message to the local player. */
1208
1201
  static sendNewVersionMessage() {
1209
1202
  if (!_VersionModule.newVersionMessage) return;
@@ -2250,7 +2243,7 @@ var MainMenu = class _MainMenu extends BaseSubscreen {
2250
2243
  PreferenceSubscreenExtensionsClear();
2251
2244
  });
2252
2245
  } else if (returnScreen instanceof BaseSubscreen) {
2253
- setSubscreen(returnScreen ?? null).then(() => {
2246
+ setSubscreen(returnScreen).then(() => {
2254
2247
  });
2255
2248
  } else if (Array.isArray(returnScreen)) {
2256
2249
  CommonSetScreen(...returnScreen);
@@ -2340,7 +2333,10 @@ var GuiImportExport = class extends BaseSubscreen {
2340
2333
  if (transferMethod === "clipboard") {
2341
2334
  await this.exportToClipboard(data);
2342
2335
  } else if (transferMethod === "file") {
2343
- await this.exportToFile(data, "themed_settings");
2336
+ if (!await this.exportToFile(data, "settings")) {
2337
+ return;
2338
+ }
2339
+ ;
2344
2340
  }
2345
2341
  this.importExportOptions.onExport?.();
2346
2342
  ToastManager.success("Data exported successfully.");
@@ -2365,6 +2361,9 @@ var GuiImportExport = class extends BaseSubscreen {
2365
2361
  if (!data) {
2366
2362
  throw new Error("Invalid data.");
2367
2363
  }
2364
+ for (const module of modules()) {
2365
+ module.registerDefaultSettings(data);
2366
+ }
2368
2367
  modStorage.playerStorage = data;
2369
2368
  this.importExportOptions.onImport?.();
2370
2369
  ToastManager.success("Data imported successfully.");
@@ -2391,13 +2390,14 @@ var GuiImportExport = class extends BaseSubscreen {
2391
2390
  const writable = await handle.createWritable();
2392
2391
  await writable.write(data);
2393
2392
  await writable.close();
2393
+ return true;
2394
2394
  } catch (error) {
2395
2395
  throw new Error("File save cancelled or failed: " + error.message);
2396
2396
  }
2397
2397
  } else {
2398
2398
  const fileName = await Modal.prompt("Enter file name", suggestedName);
2399
2399
  if (fileName === null) {
2400
- return;
2400
+ return false;
2401
2401
  } else if (fileName === "") {
2402
2402
  throw new Error("File name cannot be empty.");
2403
2403
  }
@@ -2411,6 +2411,7 @@ var GuiImportExport = class extends BaseSubscreen {
2411
2411
  });
2412
2412
  link.click();
2413
2413
  URL.revokeObjectURL(link.href);
2414
+ return true;
2414
2415
  }
2415
2416
  }
2416
2417
  /** Opens a file picker and reads the selected file's contents, importing the data. */
@@ -3132,7 +3133,7 @@ var EventChannel = class {
3132
3133
  }
3133
3134
  };
3134
3135
  export {
3135
- BaseMigrator2 as BaseMigrator,
3136
+ BaseMigrator,
3136
3137
  BaseModule,
3137
3138
  BaseSubscreen,
3138
3139
  EventChannel,
@@ -3161,6 +3162,7 @@ export {
3161
3162
  hasSetter,
3162
3163
  initMod,
3163
3164
  layout,
3165
+ logger,
3164
3166
  modStorage,
3165
3167
  modules,
3166
3168
  modulesMap,