minerender 2.1.0-alpha.86 → 2.1.0-alpha.87

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.
@@ -28819,6 +28819,134 @@ var AnvilParser = class {
28819
28819
  }
28820
28820
  };
28821
28821
 
28822
+ // src/world/AnvilWorldSource.ts
28823
+ init_MineRenderError();
28824
+ async function abortable2(promise, signal) {
28825
+ if (!signal) return promise;
28826
+ let abort;
28827
+ const cancelled = new Promise((_, reject) => {
28828
+ abort = () => reject(signal.reason);
28829
+ signal.addEventListener("abort", abort, { once: true });
28830
+ if (signal.aborted) abort();
28831
+ });
28832
+ try {
28833
+ return await Promise.race([promise, cancelled]);
28834
+ } finally {
28835
+ signal.removeEventListener("abort", abort);
28836
+ }
28837
+ }
28838
+ var AnvilWorldSource = class {
28839
+ constructor(readRegion, options = {}) {
28840
+ this.readRegion = readRegion;
28841
+ this.regions = /* @__PURE__ */ new Map();
28842
+ this.pending = /* @__PURE__ */ new Map();
28843
+ this.cachedBytes = 0;
28844
+ this.maxCachedRegions = options.maxCachedRegions ?? 4;
28845
+ this.maxCachedBytes = options.maxCachedBytes ?? 64 * 1024 * 1024;
28846
+ for (const [name, value] of Object.entries({ maxCachedRegions: this.maxCachedRegions, maxCachedBytes: this.maxCachedBytes })) {
28847
+ if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${name} must be a nonnegative safe integer`);
28848
+ }
28849
+ }
28850
+ /** Decodes one column at absolute chunk coordinates. Aborting a caller leaves other shared readers active. */
28851
+ async getChunk(x, z, signal) {
28852
+ if (!Number.isSafeInteger(x) || !Number.isSafeInteger(z)) {
28853
+ throw new RangeError("Chunk column coordinates must be safe integers");
28854
+ }
28855
+ const regionX = Math.floor(x / 32), regionZ = Math.floor(z / 32);
28856
+ const data = await this.getRegion(regionX, regionZ, signal);
28857
+ signal?.throwIfAborted();
28858
+ if (!data) return void 0;
28859
+ const chunk = await abortable2(AnvilParser.parseChunk(data, x - regionX * 32, z - regionZ * 32), signal);
28860
+ signal?.throwIfAborted();
28861
+ if (chunk && (chunk.x !== x || chunk.z !== z)) {
28862
+ throw new MineRenderError(`Anvil chunk coordinates ${chunk.x},${chunk.z} do not match requested column ${x},${z}`);
28863
+ }
28864
+ return chunk;
28865
+ }
28866
+ /** Releases cached regions. Reads already in progress finish without repopulating this cache. */
28867
+ clearCache() {
28868
+ this.regions.clear();
28869
+ this.pending.clear();
28870
+ this.cachedBytes = 0;
28871
+ }
28872
+ getRegion(x, z, signal) {
28873
+ signal?.throwIfAborted();
28874
+ const key = `${x},${z}`;
28875
+ const cached2 = this.regions.get(key);
28876
+ if (cached2) {
28877
+ this.regions.delete(key);
28878
+ this.regions.set(key, cached2);
28879
+ return Promise.resolve(cached2.data);
28880
+ }
28881
+ let loading = this.pending.get(key);
28882
+ if (!loading) {
28883
+ const controller = new AbortController();
28884
+ loading = {
28885
+ controller,
28886
+ waiters: 0,
28887
+ settled: false,
28888
+ promise: Promise.resolve().then(() => {
28889
+ controller.signal.throwIfAborted();
28890
+ return this.readRegion(x, z, controller.signal);
28891
+ }).then((data) => {
28892
+ controller.signal.throwIfAborted();
28893
+ let bytes = data instanceof Uint8Array ? data : data === void 0 ? void 0 : new Uint8Array(data);
28894
+ if (bytes && bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
28895
+ if (bytes) AnvilParser.getChunkList(bytes);
28896
+ loading.settled = true;
28897
+ if (this.pending.get(key) === loading) this.cacheRegion(key, bytes);
28898
+ return bytes;
28899
+ }).finally(() => {
28900
+ loading.settled = true;
28901
+ if (this.pending.get(key) === loading) this.pending.delete(key);
28902
+ })
28903
+ };
28904
+ this.pending.set(key, loading);
28905
+ }
28906
+ const read = loading;
28907
+ read.waiters++;
28908
+ return new Promise((resolve, reject) => {
28909
+ let finished = false;
28910
+ const release = () => {
28911
+ if (finished) return false;
28912
+ finished = true;
28913
+ signal?.removeEventListener("abort", abort);
28914
+ read.waiters--;
28915
+ return true;
28916
+ };
28917
+ const abort = () => {
28918
+ if (!release()) return;
28919
+ if (!read.waiters && !read.settled) {
28920
+ if (this.pending.get(key) === read) this.pending.delete(key);
28921
+ read.controller.abort(signal.reason);
28922
+ }
28923
+ reject(signal.reason);
28924
+ };
28925
+ signal?.addEventListener("abort", abort, { once: true });
28926
+ read.promise.then(
28927
+ (data) => {
28928
+ if (release()) resolve(data);
28929
+ },
28930
+ (error) => {
28931
+ if (release()) reject(error);
28932
+ }
28933
+ );
28934
+ if (signal?.aborted) abort();
28935
+ });
28936
+ }
28937
+ cacheRegion(key, data) {
28938
+ const size = data?.byteLength ?? 0;
28939
+ if (!this.maxCachedRegions || size > this.maxCachedBytes) return;
28940
+ this.regions.set(key, { data, size });
28941
+ this.cachedBytes += size;
28942
+ while (this.regions.size > this.maxCachedRegions || this.cachedBytes > this.maxCachedBytes) {
28943
+ const oldest = this.regions.keys().next().value;
28944
+ this.cachedBytes -= this.regions.get(oldest).size;
28945
+ this.regions.delete(oldest);
28946
+ }
28947
+ }
28948
+ };
28949
+
28822
28950
  // src/world/Chunk.ts
28823
28951
  import { Vector3 as Vector317 } from "three";
28824
28952
  init_BlockStates();
@@ -29493,9 +29621,184 @@ var Chunk = class _Chunk {
29493
29621
  );
29494
29622
  }
29495
29623
  };
29624
+
29625
+ // src/world/WorldStreamer.ts
29626
+ var WorldStreamer = class {
29627
+ constructor(world, source, options = {}) {
29628
+ this.world = world;
29629
+ this.source = source;
29630
+ this.loaded = /* @__PURE__ */ new Map();
29631
+ this.owned = /* @__PURE__ */ new Map();
29632
+ this.missing = /* @__PURE__ */ new Map();
29633
+ this.failed = /* @__PURE__ */ new Map();
29634
+ this.desired = [];
29635
+ this.disposed = false;
29636
+ this.loadRadius = options.loadRadius ?? 2;
29637
+ this.unloadRadius = options.unloadRadius ?? Math.min(32, this.loadRadius + 1);
29638
+ if (![this.loadRadius, this.unloadRadius].every((value) => Number.isInteger(value) && value >= 0 && value <= 32) || this.unloadRadius < this.loadRadius) {
29639
+ throw new RangeError("Chunk radii must be integers from 0 to 32, with unloadRadius at least loadRadius");
29640
+ }
29641
+ }
29642
+ /** Returns absolute coordinates of successfully placed columns, including retained columns. */
29643
+ get loadedChunks() {
29644
+ return [...this.loaded.values()].map((position) => ({ ...position }));
29645
+ }
29646
+ /** Returns source failures retained within the unload radius. Failed columns do not block other loads. */
29647
+ get failedChunks() {
29648
+ return [...this.failed.values()].map((failure) => ({ ...failure }));
29649
+ }
29650
+ /** Counts desired columns still waiting for a source read or placement, including the active column. */
29651
+ get pendingChunks() {
29652
+ return this.desired.filter((position) => this.needsLoad(position)).length;
29653
+ }
29654
+ /**
29655
+ * Updates the center in absolute chunk coordinates. Repeated calls share the active drain.
29656
+ * Source failures are recorded in failedChunks; other columns continue loading. Missing and failed
29657
+ * columns are remembered within the retention radius. Call retryFailedChunks to retry source failures.
29658
+ * Placement and unload failures reject the drain; call update again to retry those operations.
29659
+ * Reads outside the latest load radius receive an abort signal; placement finishes before unloading.
29660
+ * Sources should represent an unchanged world during streaming.
29661
+ */
29662
+ async update(x, z) {
29663
+ if (this.disposed) throw new Error("WorldStreamer is disposed");
29664
+ if (![x, z].every((value) => Number.isSafeInteger(value) && Number.isSafeInteger(value - this.unloadRadius) && Number.isSafeInteger(value + this.unloadRadius))) {
29665
+ throw new RangeError("Chunk coordinates must be safe integers within the streaming radius");
29666
+ }
29667
+ if (!this.center || this.center.x !== x || this.center.z !== z) {
29668
+ this.center = { x, z };
29669
+ this.desired = [];
29670
+ for (let dz = -this.loadRadius; dz <= this.loadRadius; dz++) {
29671
+ for (let dx = -this.loadRadius; dx <= this.loadRadius; dx++) {
29672
+ this.desired.push({ x: x + dx, z: z + dz });
29673
+ }
29674
+ }
29675
+ this.desired.sort((a, b) => (a.x - x) ** 2 + (a.z - z) ** 2 - (b.x - x) ** 2 - (b.z - z) ** 2);
29676
+ if (this.loading && !this.within(this.loading.position, this.loadRadius)) {
29677
+ this.loading.controller.abort();
29678
+ }
29679
+ }
29680
+ return this.running ??= Promise.resolve().then(() => this.drain());
29681
+ }
29682
+ /** Updates from a camera or view-center position in scene units. One chunk spans 256 scene units. */
29683
+ async updatePosition(position) {
29684
+ if (![position.x, position.z].every(Number.isFinite)) throw new RangeError("Scene coordinates must be finite");
29685
+ return this.update(Math.floor(position.x / 256), Math.floor(position.z / 256));
29686
+ }
29687
+ /** Forgets recorded source failures and retries desired columns at the latest center. */
29688
+ async retryFailedChunks() {
29689
+ if (this.disposed) throw new Error("WorldStreamer is disposed");
29690
+ this.failed.clear();
29691
+ if (this.center) await this.update(this.center.x, this.center.z);
29692
+ }
29693
+ /** Aborts source reads, waits for active work, then unloads owned columns. The world and source remain caller-owned. */
29694
+ dispose() {
29695
+ this.disposed = true;
29696
+ this.desired = [];
29697
+ this.center = void 0;
29698
+ this.loading?.controller.abort();
29699
+ return this.disposal ??= Promise.resolve().then(async () => {
29700
+ await this.running?.catch(() => void 0);
29701
+ let failure;
29702
+ let failed = false;
29703
+ for (const position of this.owned.values()) {
29704
+ try {
29705
+ await this.unload(position);
29706
+ } catch (error) {
29707
+ failed = true;
29708
+ failure ??= error;
29709
+ }
29710
+ }
29711
+ this.missing.clear();
29712
+ this.failed.clear();
29713
+ if (failed) throw failure;
29714
+ }).catch((error) => {
29715
+ this.disposal = void 0;
29716
+ throw error;
29717
+ });
29718
+ }
29719
+ async drain() {
29720
+ try {
29721
+ while (!this.disposed) {
29722
+ for (const [key, position2] of this.missing) {
29723
+ if (!this.within(position2, this.unloadRadius)) this.missing.delete(key);
29724
+ }
29725
+ for (const [key, position2] of this.failed) {
29726
+ if (!this.within(position2, this.unloadRadius)) this.failed.delete(key);
29727
+ }
29728
+ const obsolete = [...this.owned.values()].find((position2) => !this.loaded.has(this.key(position2)) || !this.within(position2, this.unloadRadius));
29729
+ if (obsolete) {
29730
+ await this.unload(obsolete);
29731
+ continue;
29732
+ }
29733
+ const position = this.desired.find((position2) => this.needsLoad(position2));
29734
+ if (!position) return;
29735
+ let chunk;
29736
+ const controller = new AbortController();
29737
+ this.loading = { position, controller };
29738
+ try {
29739
+ chunk = await this.source.getChunk(position.x, position.z, controller.signal);
29740
+ if (chunk) this.validateChunk(chunk, position);
29741
+ } catch (error) {
29742
+ if (!controller.signal.aborted && !this.disposed && this.within(position, this.loadRadius)) {
29743
+ this.failed.set(this.key(position), { ...position, error });
29744
+ }
29745
+ continue;
29746
+ } finally {
29747
+ this.loading = void 0;
29748
+ }
29749
+ if (controller.signal.aborted || this.disposed || !this.within(position, this.loadRadius)) continue;
29750
+ if (!chunk) {
29751
+ this.missing.set(this.key(position), position);
29752
+ continue;
29753
+ }
29754
+ this.owned.set(this.key(position), position);
29755
+ try {
29756
+ await this.world.placeChunk(chunk);
29757
+ this.loaded.set(this.key(position), position);
29758
+ } catch (error) {
29759
+ try {
29760
+ await this.unload(position);
29761
+ } catch (cleanupError) {
29762
+ const failure = error instanceof Error ? error : new Error(String(error));
29763
+ try {
29764
+ Object.defineProperty(failure, "cause", { value: cleanupError, configurable: true });
29765
+ } catch {
29766
+ throw Object.assign(new Error(failure.message), { cause: cleanupError });
29767
+ }
29768
+ throw failure;
29769
+ }
29770
+ throw error;
29771
+ }
29772
+ }
29773
+ } finally {
29774
+ this.running = void 0;
29775
+ }
29776
+ }
29777
+ needsLoad(position) {
29778
+ const key = this.key(position);
29779
+ return !this.loaded.has(key) && !this.missing.has(key) && !this.failed.has(key);
29780
+ }
29781
+ async unload(position) {
29782
+ await this.world.unloadChunkColumn(position.x, position.z);
29783
+ this.owned.delete(this.key(position));
29784
+ this.loaded.delete(this.key(position));
29785
+ }
29786
+ validateChunk(chunk, position) {
29787
+ if (chunk.x !== position.x || chunk.z !== position.z) {
29788
+ throw new Error(`World source returned chunk ${chunk.x},${chunk.z} for ${position.x},${position.z}`);
29789
+ }
29790
+ }
29791
+ within(position, radius) {
29792
+ return !!this.center && Math.abs(position.x - this.center.x) <= radius && Math.abs(position.z - this.center.z) <= radius;
29793
+ }
29794
+ key(position) {
29795
+ return `${position.x},${position.z}`;
29796
+ }
29797
+ };
29496
29798
  export {
29497
29799
  AXES,
29498
29800
  AnvilParser,
29801
+ AnvilWorldSource,
29499
29802
  ArchiveAssetSource,
29500
29803
  AssetKey,
29501
29804
  AssetLoadError,
@@ -29599,6 +29902,7 @@ export {
29599
29902
  TintColors,
29600
29903
  UVMapper,
29601
29904
  VideoExporter,
29905
+ WorldStreamer,
29602
29906
  WrappedImage,
29603
29907
  addBox3WireframeToObject,
29604
29908
  addWireframeToMesh,