@grimmdev/retrokit 0.2.0 → 0.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/retrokit.js CHANGED
@@ -10,6 +10,12 @@ class $ {
10
10
  async init(a) {
11
11
  this.context = a;
12
12
  }
13
+ setCoresDirectory(a) {
14
+ this.context && (this.context.assetBaseUrl = a);
15
+ }
16
+ setCanvas(a) {
17
+ this.context && (this.context.canvas = a);
18
+ }
13
19
  start() {
14
20
  this.isRunningInternal = !0, this.isPausedInternal = !1;
15
21
  }
@@ -5923,7 +5929,15 @@ class _e {
5923
5929
  c(this, "_masterGain");
5924
5930
  c(this, "_volume", 1);
5925
5931
  c(this, "_isMuted", !1);
5926
- this._ctx = a, this._getAdapter = e, this._masterGain = this._ctx.createGain(), this._masterGain.gain.value = this._volume, this._masterGain.connect(this._ctx.destination);
5932
+ this._ctx = a, this._getAdapter = e, this._ctx && typeof this._ctx.createGain == "function" ? (this._masterGain = this._ctx.createGain(), this._masterGain.gain.value = this._volume, this._ctx.destination && this._masterGain.connect(this._ctx.destination)) : this._masterGain = {
5933
+ gain: {
5934
+ value: this._volume,
5935
+ setValueAtTime: () => {
5936
+ }
5937
+ },
5938
+ connect: () => {
5939
+ }
5940
+ };
5927
5941
  }
5928
5942
  getVolume() {
5929
5943
  return this._volume;
@@ -5996,7 +6010,7 @@ class ye {
5996
6010
  }
5997
6011
  }
5998
6012
  }
5999
- class be {
6013
+ class ge {
6000
6014
  constructor(a) {
6001
6015
  c(this, "_cheats", /* @__PURE__ */ new Map());
6002
6016
  c(this, "_counter", 0);
@@ -6072,7 +6086,7 @@ class be {
6072
6086
  a.rawSetCheat(e, t.enabled, t.code), e++;
6073
6087
  }
6074
6088
  }
6075
- class we {
6089
+ class be {
6076
6090
  constructor(a) {
6077
6091
  c(this, "_optionOverrides", /* @__PURE__ */ new Map());
6078
6092
  c(this, "_listeners", /* @__PURE__ */ new Set());
@@ -6115,7 +6129,7 @@ class we {
6115
6129
  return this._listeners.add(a), () => this._listeners.delete(a);
6116
6130
  }
6117
6131
  }
6118
- class ge {
6132
+ class we {
6119
6133
  constructor(a) {
6120
6134
  c(this, "_currentDisc", 0);
6121
6135
  c(this, "_discs", []);
@@ -6482,23 +6496,24 @@ class ke {
6482
6496
  this._pollTimer && (clearInterval(this._pollTimer), this._pollTimer = null), this._modifiedListeners.clear();
6483
6497
  }
6484
6498
  _startDirtyWatchdog() {
6485
- this._pollTimer = setInterval(async () => {
6486
- const a = this._getAdapter();
6487
- if (!a || !a.capabilities.sram) return;
6488
- const e = await a.rawGetSram();
6489
- if (e && this._hasChanged(this._lastData, e)) {
6490
- this._isDirty = !0, this._lastData = e;
6491
- for (const t of this._modifiedListeners)
6499
+ const a = setInterval(async () => {
6500
+ const e = this._getAdapter();
6501
+ if (!e || !e.capabilities.sram) return;
6502
+ const t = await e.rawGetSram();
6503
+ if (t && this._hasChanged(this._lastData, t)) {
6504
+ this._isDirty = !0, this._lastData = t;
6505
+ for (const s of this._modifiedListeners)
6492
6506
  try {
6493
- t(e);
6494
- } catch (s) {
6507
+ s(t);
6508
+ } catch (r) {
6495
6509
  console.error(
6496
6510
  "[SramManager] Error in onSramModified listener:",
6497
- s
6511
+ r
6498
6512
  );
6499
6513
  }
6500
6514
  }
6501
6515
  }, 2500);
6516
+ typeof a == "object" && a !== null && "unref" in a && a.unref(), this._pollTimer = a;
6502
6517
  }
6503
6518
  _hasChanged(a, e) {
6504
6519
  if (!a && !e) return !1;
@@ -6650,7 +6665,7 @@ class De {
6650
6665
  }
6651
6666
  updateLayout() {
6652
6667
  const a = this._getCanvas();
6653
- if (!a) return;
6668
+ if (!a || !a.style) return;
6654
6669
  if (typeof ResizeObserver < "u" && a.parentElement && !this._resizeObserver)
6655
6670
  try {
6656
6671
  this._resizeObserver = new ResizeObserver(() => this.updateLayout()), this._resizeObserver.observe(a.parentElement);
@@ -6979,7 +6994,7 @@ class Oe extends Me {
6979
6994
  c(this, "_fpsTimer", null);
6980
6995
  this._config = e, e.audioContext ? this._audioCtx = e.audioContext : typeof AudioContext < "u" ? this._audioCtx = new AudioContext({ latencyHint: "interactive" }) : this._audioCtx = {}, this.video = new De(
6981
6996
  () => this._adapter,
6982
- () => this._config.canvas
6997
+ () => this._config.canvas || null
6983
6998
  ), this.state = new Pe(
6984
6999
  () => this._adapter,
6985
7000
  async () => {
@@ -6989,7 +7004,7 @@ class Oe extends Me {
6989
7004
  return null;
6990
7005
  }
6991
7006
  }
6992
- ), this.sram = new ke(() => this._adapter), this.cheats = new be(() => this._adapter), this.options = new we(() => this._adapter), this.discs = new ge(() => this._adapter), this.input = new Ce(() => this._adapter), this.audio = new _e(this._audioCtx, () => this._adapter), this.timing = new Fe(() => this._adapter), this.netplay = new Ae(), this.bios = new ye(), this.achievements = new pe(
7007
+ ), this.sram = new ke(() => this._adapter), this.cheats = new ge(() => this._adapter), this.options = new be(() => this._adapter), this.discs = new we(() => this._adapter), this.input = new Ce(() => this._adapter), this.audio = new _e(this._audioCtx, () => this._adapter), this.timing = new Fe(() => this._adapter), this.netplay = new Ae(), this.bios = new ye(), this.achievements = new pe(
6993
7008
  () => this._adapter,
6994
7009
  (r, i) => this.emit(r, i)
6995
7010
  ), (t = e.retroAchievements) != null && t.username && ((s = e.retroAchievements) != null && s.token) && (this.achievements.login(
@@ -7009,8 +7024,11 @@ class Oe extends Me {
7009
7024
  return this._adapter ? this._adapter.capabilities : te;
7010
7025
  }
7011
7026
  async load(e, t) {
7012
- var d, u;
7013
- this._activeRom = e, this._lastLoadOptions = t;
7027
+ var u, m;
7028
+ if (this._activeRom = e, this._lastLoadOptions = t, t != null && t.canvas && (this._config.canvas = t.canvas), (t == null ? void 0 : t.preserveCanvas) !== void 0 ? this._config.preserveCanvas = t.preserveCanvas : (t == null ? void 0 : t.useExistingCanvas) !== void 0 && (this._config.preserveCanvas = t.useExistingCanvas), !this._config.canvas)
7029
+ throw new Error(
7030
+ "[RetroKit] No canvas provided. Pass a canvas in constructor, load options, or via setCanvas()."
7031
+ );
7014
7032
  const s = (t == null ? void 0 : t.gameId) || (typeof e == "string" ? e : (t == null ? void 0 : t.fileName) || "game");
7015
7033
  this.state.setGameId(s);
7016
7034
  const r = this._resolveCore(t);
@@ -7020,67 +7038,69 @@ class Oe extends Me {
7020
7038
  );
7021
7039
  if (t != null && t.bios)
7022
7040
  if (Array.isArray(t.bios))
7023
- for (const m of t.bios)
7024
- this.bios.register(m.name, m.data);
7041
+ for (const _ of t.bios)
7042
+ this.bios.register(_.name, _.data);
7025
7043
  else
7026
- for (const [m, _] of Object.entries(t.bios))
7027
- this.bios.register(m, _);
7044
+ for (const [_, g] of Object.entries(t.bios))
7045
+ this.bios.register(_, g);
7028
7046
  const n = e instanceof Blob && e.size > 150 * 1024 * 1024;
7029
7047
  let o = e, l = null;
7030
7048
  if (n)
7031
7049
  try {
7032
- const m = await e.slice(0, 65536).arrayBuffer(), _ = this.achievements.computeGameHash(
7033
- new Uint8Array(m)
7050
+ const _ = await e.slice(0, 65536).arrayBuffer(), g = this.achievements.computeGameHash(
7051
+ new Uint8Array(_)
7034
7052
  );
7035
- this.achievements.loadAchievements(_, t == null ? void 0 : t.fileName).catch(() => {
7053
+ this.achievements.loadAchievements(g, t == null ? void 0 : t.fileName).catch(() => {
7036
7054
  });
7037
7055
  } catch {
7038
7056
  }
7039
7057
  else {
7040
7058
  if (typeof e == "string") {
7041
- const g = await (await fetch(e, { headers: this._config.fetchHeaders })).arrayBuffer();
7042
- l = new Uint8Array(g);
7059
+ const h = await (await fetch(e, { headers: this._config.fetchHeaders })).arrayBuffer();
7060
+ l = new Uint8Array(h);
7043
7061
  } else if (e instanceof Blob) {
7044
- const _ = await e.arrayBuffer();
7045
- l = new Uint8Array(_);
7062
+ const g = await e.arrayBuffer();
7063
+ l = new Uint8Array(g);
7046
7064
  } else e instanceof Uint8Array ? l = e : l = new Uint8Array(e);
7047
7065
  if (o = l, t != null && t.patch) {
7048
- let _;
7066
+ let g;
7049
7067
  if (t.patch instanceof Blob) {
7050
- const h = await t.patch.arrayBuffer();
7051
- _ = new Uint8Array(h);
7052
- } else t.patch instanceof ArrayBuffer ? _ = new Uint8Array(t.patch) : _ = t.patch;
7053
- const g = Ee.applyPatch(
7068
+ const b = await t.patch.arrayBuffer();
7069
+ g = new Uint8Array(b);
7070
+ } else t.patch instanceof ArrayBuffer ? g = new Uint8Array(t.patch) : g = t.patch;
7071
+ const h = Ee.applyPatch(
7054
7072
  l,
7055
- _,
7073
+ g,
7056
7074
  t.verifyPatchChecksum
7057
7075
  );
7058
- l = g.patchedRom, o = l, this.emit("romPatched", {
7059
- format: g.format,
7076
+ l = h.patchedRom, o = l, this.emit("romPatched", {
7077
+ format: h.format,
7060
7078
  originalSize: l.length,
7061
- patchedSize: g.patchedRom.length
7079
+ patchedSize: h.patchedRom.length
7062
7080
  });
7063
7081
  }
7064
- const m = this.achievements.computeGameHash(l);
7065
- this.achievements.loadAchievements(m, t == null ? void 0 : t.fileName).catch((_) => {
7066
- console.warn("[RetroKit] RetroAchievements lookup failed:", _);
7082
+ const _ = this.achievements.computeGameHash(l);
7083
+ this.achievements.loadAchievements(_, t == null ? void 0 : t.fileName).catch((g) => {
7084
+ console.warn("[RetroKit] RetroAchievements lookup failed:", g);
7067
7085
  });
7068
7086
  }
7069
- this._adapter && (await this._adapter.destroy(), this._adapter = null), this._resetCanvas(), this._adapter = this._createAdapter(r), await this._adapter.init({
7087
+ this._adapter && (await this._adapter.destroy(), this._adapter = null), this._resetCanvas(), this._adapter = this._createAdapter(r);
7088
+ const d = (t == null ? void 0 : t.coresDirectory) || (t == null ? void 0 : t.assetBaseUrl) || this._config.assetBaseUrl || "/cores";
7089
+ await this._adapter.init({
7070
7090
  canvas: this._config.canvas,
7071
- assetBaseUrl: this._config.assetBaseUrl || "/cores",
7091
+ assetBaseUrl: d,
7072
7092
  audioContext: this._audioCtx,
7073
7093
  fetchHeaders: this._config.fetchHeaders,
7074
7094
  debug: this._config.debug,
7075
- onFpsUpdate: (m) => this.emit("fps", { fps: m }),
7076
- onSramDirty: (m) => this.emit("sramModified", { data: m }),
7077
- onLog: (m, _ = "info") => this.emit("log", { message: m, level: _ }),
7095
+ onFpsUpdate: (_) => this.emit("fps", { fps: _ }),
7096
+ onSramDirty: (_) => this.emit("sramModified", { data: _ }),
7097
+ onLog: (_, g = "info") => this.emit("log", { message: _, level: g }),
7078
7098
  retroAchievements: this.achievements.isLoggedIn && this.achievements.user ? {
7079
7099
  username: this.achievements.user.username,
7080
7100
  token: this.achievements.user.token,
7081
7101
  hardcore: this.achievements.hardcore
7082
7102
  } : void 0
7083
- }), await this.bios.injectIntoAdapter(this._adapter), await this._adapter.loadGame(o, t), this._isRunning = !0, this._isPaused = !1, this._startFpsMonitoring(), (u = (d = this.video).updateLayout) == null || u.call(d), this.emit("ready", { core: r.id, gameId: s });
7103
+ }), await this.bios.injectIntoAdapter(this._adapter), await this._adapter.loadGame(o, t), this._isRunning = !0, this._isPaused = !1, this._startFpsMonitoring(), (m = (u = this.video).updateLayout) == null || m.call(u), this.emit("ready", { core: r.id, gameId: s });
7084
7104
  }
7085
7105
  async play() {
7086
7106
  if (!this._isRunning && this._activeRom) {
@@ -7096,6 +7116,65 @@ class Oe extends Me {
7096
7116
  async stop() {
7097
7117
  this._adapter && (this._fpsTimer && (clearInterval(this._fpsTimer), this._fpsTimer = null), await this._adapter.stop(), this._isRunning = !1, this._isPaused = !1, this.video.clear(), this.emit("stop"));
7098
7118
  }
7119
+ /**
7120
+ * Sets the base directory or URL where emulator core binaries and runtime assets are located.
7121
+ * Updates internal configuration and the active adapter context.
7122
+ *
7123
+ * @param directory Base URL or directory path (e.g. '/cores', 'https://cdn.example.com/cores')
7124
+ */
7125
+ setCoresDirectory(e) {
7126
+ var t, s;
7127
+ this._config.assetBaseUrl = e, (s = (t = this._adapter) == null ? void 0 : t.setCoresDirectory) == null || s.call(t, e);
7128
+ }
7129
+ /**
7130
+ * Alias for setCoresDirectory.
7131
+ *
7132
+ * @param url Base URL or directory path
7133
+ */
7134
+ setAssetBaseUrl(e) {
7135
+ this.setCoresDirectory(e);
7136
+ }
7137
+ /**
7138
+ * Returns the currently configured cores directory or asset base URL.
7139
+ */
7140
+ getCoresDirectory() {
7141
+ return this._config.assetBaseUrl || "/cores";
7142
+ }
7143
+ /**
7144
+ * Returns the currently configured cores directory or asset base URL.
7145
+ */
7146
+ get coresDirectory() {
7147
+ return this.getCoresDirectory();
7148
+ }
7149
+ /**
7150
+ * Alias for getCoresDirectory.
7151
+ */
7152
+ getAssetBaseUrl() {
7153
+ return this.getCoresDirectory();
7154
+ }
7155
+ /**
7156
+ * Alias for coresDirectory.
7157
+ */
7158
+ get assetBaseUrl() {
7159
+ return this.getCoresDirectory();
7160
+ }
7161
+ /**
7162
+ * Binds or updates the rendering canvas element.
7163
+ *
7164
+ * @param canvas The target HTML5 Canvas element (can be an existing WebGL canvas).
7165
+ * @param options.preserveCanvas When true, reuses this canvas without replacing its DOM node.
7166
+ * @param options.useExistingCanvas Alias for preserveCanvas.
7167
+ */
7168
+ setCanvas(e, t) {
7169
+ var s, r, i, n;
7170
+ this._config.canvas = e, (t == null ? void 0 : t.preserveCanvas) !== void 0 ? this._config.preserveCanvas = t.preserveCanvas : (t == null ? void 0 : t.useExistingCanvas) !== void 0 && (this._config.preserveCanvas = t.useExistingCanvas), (r = (s = this._adapter) == null ? void 0 : s.setCanvas) == null || r.call(s, e), (n = (i = this.video).updateLayout) == null || n.call(i);
7171
+ }
7172
+ /**
7173
+ * Returns the currently bound canvas element, if any.
7174
+ */
7175
+ get canvas() {
7176
+ return this._config.canvas;
7177
+ }
7099
7178
  /**
7100
7179
  * Updates the active target platform hint for subsequent game launches.
7101
7180
  */
@@ -7121,12 +7200,14 @@ class Oe extends Me {
7121
7200
  this._fpsTimer && (clearInterval(this._fpsTimer), this._fpsTimer = null), await this.stop(), this._adapter && (await this._adapter.destroy(), this._adapter = null), this.video.destroy(), this.input.destroy(), this.sram.destroy(), this.audio.destroy(), this.netplay.disconnect(), this.removeAllListeners(), this._activeRom = null;
7122
7201
  }
7123
7202
  _startFpsMonitoring() {
7124
- this._fpsTimer && clearInterval(this._fpsTimer), this._fpsTimer = setInterval(() => {
7203
+ this._fpsTimer && clearInterval(this._fpsTimer);
7204
+ const e = setInterval(() => {
7125
7205
  if (this._isRunning && !this._isPaused && this._adapter) {
7126
- const e = this._adapter.rawGetFps() || 60;
7127
- this.emit("fps", { fps: e });
7206
+ const t = this._adapter.rawGetFps() || 60;
7207
+ this.emit("fps", { fps: t });
7128
7208
  }
7129
7209
  }, 1e3);
7210
+ typeof e == "object" && e !== null && "unref" in e && e.unref(), this._fpsTimer = e;
7130
7211
  }
7131
7212
  async dispose() {
7132
7213
  await this.destroy();
@@ -7197,13 +7278,15 @@ class Oe extends Me {
7197
7278
  /**
7198
7279
  * Replaces the canvas DOM element with a clean canvas surface to prevent
7199
7280
  * WebGL, WebGL2, or 2D context collisions when switching emulator cores.
7281
+ * If preserveCanvas or useExistingCanvas is enabled, preserves the existing
7282
+ * canvas element and clears its frame buffer instead of replacing the DOM node.
7200
7283
  */
7201
7284
  _resetCanvas() {
7202
- const e = this._config.canvas;
7203
- if (!e || typeof document > "u" || !e.parentNode)
7204
- return e || null;
7205
- const t = document.createElement("canvas");
7206
- return e.id && (t.id = e.id), e.className && (t.className = e.className), t.width = e.width || 640, t.height = e.height || 480, t.tabIndex = e.tabIndex >= 0 ? e.tabIndex : 1, e.style.cssText && (t.style.cssText = e.style.cssText), t.style.display = "block", e.parentNode.replaceChild(t, e), this._config.canvas = t, t;
7285
+ const e = this._config.canvas, t = this._config.preserveCanvas || this._config.useExistingCanvas;
7286
+ if (!e || typeof document > "u" || !e.parentNode || t)
7287
+ return e && this.video.clear(), e || null;
7288
+ const s = document.createElement("canvas");
7289
+ return e.id && (s.id = e.id), e.className && (s.className = e.className), s.width = e.width || 640, s.height = e.height || 480, s.tabIndex = e.tabIndex >= 0 ? e.tabIndex : 1, e.style.cssText && (s.style.cssText = e.style.cssText), s.style.display = "block", e.parentNode.replaceChild(s, e), this._config.canvas = s, s;
7207
7290
  }
7208
7291
  _createAdapter(e) {
7209
7292
  const t = T.getAdapterFactory(e.id);
@@ -7242,11 +7325,11 @@ export {
7242
7325
  _e as AudioManager,
7243
7326
  $ as BaseCoreBridge,
7244
7327
  ye as BiosManager,
7245
- be as CheatManager,
7246
- we as CoreOptionsManager,
7328
+ ge as CheatManager,
7329
+ be as CoreOptionsManager,
7247
7330
  T as CoreRegistry,
7248
7331
  te as DEFAULT_CAPABILITIES,
7249
- ge as DiscManager,
7332
+ we as DiscManager,
7250
7333
  Be as EmuHeadless,
7251
7334
  oe as EmulatorJSAdapter,
7252
7335
  Me as EventEmitter,