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.
@@ -28762,6 +28762,134 @@ var AnvilParser = class {
28762
28762
  }
28763
28763
  };
28764
28764
 
28765
+ // src/world/AnvilWorldSource.ts
28766
+ init_MineRenderError();
28767
+ async function abortable2(promise, signal) {
28768
+ if (!signal) return promise;
28769
+ let abort;
28770
+ const cancelled = new Promise((_, reject) => {
28771
+ abort = () => reject(signal.reason);
28772
+ signal.addEventListener("abort", abort, { once: true });
28773
+ if (signal.aborted) abort();
28774
+ });
28775
+ try {
28776
+ return await Promise.race([promise, cancelled]);
28777
+ } finally {
28778
+ signal.removeEventListener("abort", abort);
28779
+ }
28780
+ }
28781
+ var AnvilWorldSource = class {
28782
+ constructor(readRegion, options = {}) {
28783
+ this.readRegion = readRegion;
28784
+ this.regions = /* @__PURE__ */ new Map();
28785
+ this.pending = /* @__PURE__ */ new Map();
28786
+ this.cachedBytes = 0;
28787
+ this.maxCachedRegions = options.maxCachedRegions ?? 4;
28788
+ this.maxCachedBytes = options.maxCachedBytes ?? 64 * 1024 * 1024;
28789
+ for (const [name, value] of Object.entries({ maxCachedRegions: this.maxCachedRegions, maxCachedBytes: this.maxCachedBytes })) {
28790
+ if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${name} must be a nonnegative safe integer`);
28791
+ }
28792
+ }
28793
+ /** Decodes one column at absolute chunk coordinates. Aborting a caller leaves other shared readers active. */
28794
+ async getChunk(x, z, signal) {
28795
+ if (!Number.isSafeInteger(x) || !Number.isSafeInteger(z)) {
28796
+ throw new RangeError("Chunk column coordinates must be safe integers");
28797
+ }
28798
+ const regionX = Math.floor(x / 32), regionZ = Math.floor(z / 32);
28799
+ const data = await this.getRegion(regionX, regionZ, signal);
28800
+ signal?.throwIfAborted();
28801
+ if (!data) return void 0;
28802
+ const chunk = await abortable2(AnvilParser.parseChunk(data, x - regionX * 32, z - regionZ * 32), signal);
28803
+ signal?.throwIfAborted();
28804
+ if (chunk && (chunk.x !== x || chunk.z !== z)) {
28805
+ throw new MineRenderError(`Anvil chunk coordinates ${chunk.x},${chunk.z} do not match requested column ${x},${z}`);
28806
+ }
28807
+ return chunk;
28808
+ }
28809
+ /** Releases cached regions. Reads already in progress finish without repopulating this cache. */
28810
+ clearCache() {
28811
+ this.regions.clear();
28812
+ this.pending.clear();
28813
+ this.cachedBytes = 0;
28814
+ }
28815
+ getRegion(x, z, signal) {
28816
+ signal?.throwIfAborted();
28817
+ const key = `${x},${z}`;
28818
+ const cached2 = this.regions.get(key);
28819
+ if (cached2) {
28820
+ this.regions.delete(key);
28821
+ this.regions.set(key, cached2);
28822
+ return Promise.resolve(cached2.data);
28823
+ }
28824
+ let loading = this.pending.get(key);
28825
+ if (!loading) {
28826
+ const controller = new AbortController();
28827
+ loading = {
28828
+ controller,
28829
+ waiters: 0,
28830
+ settled: false,
28831
+ promise: Promise.resolve().then(() => {
28832
+ controller.signal.throwIfAborted();
28833
+ return this.readRegion(x, z, controller.signal);
28834
+ }).then((data) => {
28835
+ controller.signal.throwIfAborted();
28836
+ let bytes = data instanceof Uint8Array ? data : data === void 0 ? void 0 : new Uint8Array(data);
28837
+ if (bytes && bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
28838
+ if (bytes) AnvilParser.getChunkList(bytes);
28839
+ loading.settled = true;
28840
+ if (this.pending.get(key) === loading) this.cacheRegion(key, bytes);
28841
+ return bytes;
28842
+ }).finally(() => {
28843
+ loading.settled = true;
28844
+ if (this.pending.get(key) === loading) this.pending.delete(key);
28845
+ })
28846
+ };
28847
+ this.pending.set(key, loading);
28848
+ }
28849
+ const read = loading;
28850
+ read.waiters++;
28851
+ return new Promise((resolve, reject) => {
28852
+ let finished = false;
28853
+ const release = () => {
28854
+ if (finished) return false;
28855
+ finished = true;
28856
+ signal?.removeEventListener("abort", abort);
28857
+ read.waiters--;
28858
+ return true;
28859
+ };
28860
+ const abort = () => {
28861
+ if (!release()) return;
28862
+ if (!read.waiters && !read.settled) {
28863
+ if (this.pending.get(key) === read) this.pending.delete(key);
28864
+ read.controller.abort(signal.reason);
28865
+ }
28866
+ reject(signal.reason);
28867
+ };
28868
+ signal?.addEventListener("abort", abort, { once: true });
28869
+ read.promise.then(
28870
+ (data) => {
28871
+ if (release()) resolve(data);
28872
+ },
28873
+ (error) => {
28874
+ if (release()) reject(error);
28875
+ }
28876
+ );
28877
+ if (signal?.aborted) abort();
28878
+ });
28879
+ }
28880
+ cacheRegion(key, data) {
28881
+ const size = data?.byteLength ?? 0;
28882
+ if (!this.maxCachedRegions || size > this.maxCachedBytes) return;
28883
+ this.regions.set(key, { data, size });
28884
+ this.cachedBytes += size;
28885
+ while (this.regions.size > this.maxCachedRegions || this.cachedBytes > this.maxCachedBytes) {
28886
+ const oldest = this.regions.keys().next().value;
28887
+ this.cachedBytes -= this.regions.get(oldest).size;
28888
+ this.regions.delete(oldest);
28889
+ }
28890
+ }
28891
+ };
28892
+
28765
28893
  // src/world/Chunk.ts
28766
28894
  import { Vector3 as Vector317 } from "three";
28767
28895
  init_BlockStates();
@@ -29436,9 +29564,184 @@ var Chunk = class _Chunk {
29436
29564
  );
29437
29565
  }
29438
29566
  };
29567
+
29568
+ // src/world/WorldStreamer.ts
29569
+ var WorldStreamer = class {
29570
+ constructor(world, source, options = {}) {
29571
+ this.world = world;
29572
+ this.source = source;
29573
+ this.loaded = /* @__PURE__ */ new Map();
29574
+ this.owned = /* @__PURE__ */ new Map();
29575
+ this.missing = /* @__PURE__ */ new Map();
29576
+ this.failed = /* @__PURE__ */ new Map();
29577
+ this.desired = [];
29578
+ this.disposed = false;
29579
+ this.loadRadius = options.loadRadius ?? 2;
29580
+ this.unloadRadius = options.unloadRadius ?? Math.min(32, this.loadRadius + 1);
29581
+ if (![this.loadRadius, this.unloadRadius].every((value) => Number.isInteger(value) && value >= 0 && value <= 32) || this.unloadRadius < this.loadRadius) {
29582
+ throw new RangeError("Chunk radii must be integers from 0 to 32, with unloadRadius at least loadRadius");
29583
+ }
29584
+ }
29585
+ /** Returns absolute coordinates of successfully placed columns, including retained columns. */
29586
+ get loadedChunks() {
29587
+ return [...this.loaded.values()].map((position) => ({ ...position }));
29588
+ }
29589
+ /** Returns source failures retained within the unload radius. Failed columns do not block other loads. */
29590
+ get failedChunks() {
29591
+ return [...this.failed.values()].map((failure) => ({ ...failure }));
29592
+ }
29593
+ /** Counts desired columns still waiting for a source read or placement, including the active column. */
29594
+ get pendingChunks() {
29595
+ return this.desired.filter((position) => this.needsLoad(position)).length;
29596
+ }
29597
+ /**
29598
+ * Updates the center in absolute chunk coordinates. Repeated calls share the active drain.
29599
+ * Source failures are recorded in failedChunks; other columns continue loading. Missing and failed
29600
+ * columns are remembered within the retention radius. Call retryFailedChunks to retry source failures.
29601
+ * Placement and unload failures reject the drain; call update again to retry those operations.
29602
+ * Reads outside the latest load radius receive an abort signal; placement finishes before unloading.
29603
+ * Sources should represent an unchanged world during streaming.
29604
+ */
29605
+ async update(x, z) {
29606
+ if (this.disposed) throw new Error("WorldStreamer is disposed");
29607
+ if (![x, z].every((value) => Number.isSafeInteger(value) && Number.isSafeInteger(value - this.unloadRadius) && Number.isSafeInteger(value + this.unloadRadius))) {
29608
+ throw new RangeError("Chunk coordinates must be safe integers within the streaming radius");
29609
+ }
29610
+ if (!this.center || this.center.x !== x || this.center.z !== z) {
29611
+ this.center = { x, z };
29612
+ this.desired = [];
29613
+ for (let dz = -this.loadRadius; dz <= this.loadRadius; dz++) {
29614
+ for (let dx = -this.loadRadius; dx <= this.loadRadius; dx++) {
29615
+ this.desired.push({ x: x + dx, z: z + dz });
29616
+ }
29617
+ }
29618
+ this.desired.sort((a, b) => (a.x - x) ** 2 + (a.z - z) ** 2 - (b.x - x) ** 2 - (b.z - z) ** 2);
29619
+ if (this.loading && !this.within(this.loading.position, this.loadRadius)) {
29620
+ this.loading.controller.abort();
29621
+ }
29622
+ }
29623
+ return this.running ?? (this.running = Promise.resolve().then(() => this.drain()));
29624
+ }
29625
+ /** Updates from a camera or view-center position in scene units. One chunk spans 256 scene units. */
29626
+ async updatePosition(position) {
29627
+ if (![position.x, position.z].every(Number.isFinite)) throw new RangeError("Scene coordinates must be finite");
29628
+ return this.update(Math.floor(position.x / 256), Math.floor(position.z / 256));
29629
+ }
29630
+ /** Forgets recorded source failures and retries desired columns at the latest center. */
29631
+ async retryFailedChunks() {
29632
+ if (this.disposed) throw new Error("WorldStreamer is disposed");
29633
+ this.failed.clear();
29634
+ if (this.center) await this.update(this.center.x, this.center.z);
29635
+ }
29636
+ /** Aborts source reads, waits for active work, then unloads owned columns. The world and source remain caller-owned. */
29637
+ dispose() {
29638
+ this.disposed = true;
29639
+ this.desired = [];
29640
+ this.center = void 0;
29641
+ this.loading?.controller.abort();
29642
+ return this.disposal ?? (this.disposal = Promise.resolve().then(async () => {
29643
+ await this.running?.catch(() => void 0);
29644
+ let failure;
29645
+ let failed = false;
29646
+ for (const position of this.owned.values()) {
29647
+ try {
29648
+ await this.unload(position);
29649
+ } catch (error) {
29650
+ failed = true;
29651
+ failure ?? (failure = error);
29652
+ }
29653
+ }
29654
+ this.missing.clear();
29655
+ this.failed.clear();
29656
+ if (failed) throw failure;
29657
+ }).catch((error) => {
29658
+ this.disposal = void 0;
29659
+ throw error;
29660
+ }));
29661
+ }
29662
+ async drain() {
29663
+ try {
29664
+ while (!this.disposed) {
29665
+ for (const [key, position2] of this.missing) {
29666
+ if (!this.within(position2, this.unloadRadius)) this.missing.delete(key);
29667
+ }
29668
+ for (const [key, position2] of this.failed) {
29669
+ if (!this.within(position2, this.unloadRadius)) this.failed.delete(key);
29670
+ }
29671
+ const obsolete = [...this.owned.values()].find((position2) => !this.loaded.has(this.key(position2)) || !this.within(position2, this.unloadRadius));
29672
+ if (obsolete) {
29673
+ await this.unload(obsolete);
29674
+ continue;
29675
+ }
29676
+ const position = this.desired.find((position2) => this.needsLoad(position2));
29677
+ if (!position) return;
29678
+ let chunk;
29679
+ const controller = new AbortController();
29680
+ this.loading = { position, controller };
29681
+ try {
29682
+ chunk = await this.source.getChunk(position.x, position.z, controller.signal);
29683
+ if (chunk) this.validateChunk(chunk, position);
29684
+ } catch (error) {
29685
+ if (!controller.signal.aborted && !this.disposed && this.within(position, this.loadRadius)) {
29686
+ this.failed.set(this.key(position), { ...position, error });
29687
+ }
29688
+ continue;
29689
+ } finally {
29690
+ this.loading = void 0;
29691
+ }
29692
+ if (controller.signal.aborted || this.disposed || !this.within(position, this.loadRadius)) continue;
29693
+ if (!chunk) {
29694
+ this.missing.set(this.key(position), position);
29695
+ continue;
29696
+ }
29697
+ this.owned.set(this.key(position), position);
29698
+ try {
29699
+ await this.world.placeChunk(chunk);
29700
+ this.loaded.set(this.key(position), position);
29701
+ } catch (error) {
29702
+ try {
29703
+ await this.unload(position);
29704
+ } catch (cleanupError) {
29705
+ const failure = error instanceof Error ? error : new Error(String(error));
29706
+ try {
29707
+ Object.defineProperty(failure, "cause", { value: cleanupError, configurable: true });
29708
+ } catch {
29709
+ throw Object.assign(new Error(failure.message), { cause: cleanupError });
29710
+ }
29711
+ throw failure;
29712
+ }
29713
+ throw error;
29714
+ }
29715
+ }
29716
+ } finally {
29717
+ this.running = void 0;
29718
+ }
29719
+ }
29720
+ needsLoad(position) {
29721
+ const key = this.key(position);
29722
+ return !this.loaded.has(key) && !this.missing.has(key) && !this.failed.has(key);
29723
+ }
29724
+ async unload(position) {
29725
+ await this.world.unloadChunkColumn(position.x, position.z);
29726
+ this.owned.delete(this.key(position));
29727
+ this.loaded.delete(this.key(position));
29728
+ }
29729
+ validateChunk(chunk, position) {
29730
+ if (chunk.x !== position.x || chunk.z !== position.z) {
29731
+ throw new Error(`World source returned chunk ${chunk.x},${chunk.z} for ${position.x},${position.z}`);
29732
+ }
29733
+ }
29734
+ within(position, radius) {
29735
+ return !!this.center && Math.abs(position.x - this.center.x) <= radius && Math.abs(position.z - this.center.z) <= radius;
29736
+ }
29737
+ key(position) {
29738
+ return `${position.x},${position.z}`;
29739
+ }
29740
+ };
29439
29741
  export {
29440
29742
  AXES,
29441
29743
  AnvilParser,
29744
+ AnvilWorldSource,
29442
29745
  ArchiveAssetSource,
29443
29746
  AssetKey,
29444
29747
  AssetLoadError,
@@ -29542,6 +29845,7 @@ export {
29542
29845
  TintColors,
29543
29846
  UVMapper,
29544
29847
  VideoExporter,
29848
+ WorldStreamer,
29545
29849
  WrappedImage,
29546
29850
  addBox3WireframeToObject,
29547
29851
  addWireframeToMesh,