minerender 2.0.0-alpha.7 → 2.0.0-alpha.9

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.
Files changed (157) hide show
  1. package/ava.config.js +10 -0
  2. package/build.mjs +2 -2
  3. package/dist/bundle.js +12824 -2556
  4. package/dist/bundle.js.map +4 -5
  5. package/dist/cjs/UVMapper.js +6 -2
  6. package/dist/cjs/UVMapper.js.map +1 -1
  7. package/dist/cjs/assets/AssetKey.js.map +1 -1
  8. package/dist/cjs/assets/AssetLoader.d.ts +6 -20
  9. package/dist/cjs/assets/AssetLoader.js +34 -123
  10. package/dist/cjs/assets/AssetLoader.js.map +1 -1
  11. package/dist/cjs/assets/BlockStates.d.ts +1 -0
  12. package/dist/cjs/assets/BlockStates.js +8 -4
  13. package/dist/cjs/assets/BlockStates.js.map +1 -1
  14. package/dist/cjs/assets/ModelTextures.js +3 -2
  15. package/dist/cjs/assets/ModelTextures.js.map +1 -1
  16. package/dist/cjs/assets/Models.js +3 -4
  17. package/dist/cjs/assets/Models.js.map +1 -1
  18. package/dist/cjs/assets/index.d.ts +1 -0
  19. package/dist/cjs/assets/index.js +1 -0
  20. package/dist/cjs/assets/index.js.map +1 -1
  21. package/dist/cjs/assets/source/AssetSource.d.ts +8 -0
  22. package/dist/cjs/assets/source/AssetSource.js +9 -0
  23. package/dist/cjs/assets/source/AssetSource.js.map +1 -0
  24. package/dist/cjs/assets/source/HostedAssetSource.d.ts +33 -0
  25. package/dist/cjs/assets/source/HostedAssetSource.js +190 -0
  26. package/dist/cjs/assets/source/HostedAssetSource.js.map +1 -0
  27. package/dist/cjs/assets/source/archive/ArchiveAssetSource.d.ts +17 -0
  28. package/dist/cjs/assets/source/archive/ArchiveAssetSource.js +126 -0
  29. package/dist/cjs/assets/source/archive/ArchiveAssetSource.js.map +1 -0
  30. package/dist/cjs/assets/source/archive/ArchiveEntry.d.ts +5 -0
  31. package/dist/cjs/assets/source/archive/ArchiveEntry.js +3 -0
  32. package/dist/cjs/assets/source/archive/ArchiveEntry.js.map +1 -0
  33. package/dist/cjs/assets/source/archive/ArchiveProxy.d.ts +4 -0
  34. package/dist/cjs/assets/source/archive/ArchiveProxy.js +3 -0
  35. package/dist/cjs/assets/source/archive/ArchiveProxy.js.map +1 -0
  36. package/dist/cjs/assets/source/archive/BrowserArchiveProxy.d.ts +9 -0
  37. package/dist/cjs/assets/source/archive/BrowserArchiveProxy.js +34 -0
  38. package/dist/cjs/assets/source/archive/BrowserArchiveProxy.js.map +1 -0
  39. package/dist/cjs/assets/source/archive/index.d.ts +4 -0
  40. package/dist/cjs/assets/source/archive/index.js +21 -0
  41. package/dist/cjs/assets/source/archive/index.js.map +1 -0
  42. package/dist/cjs/assets/source/index.d.ts +4 -0
  43. package/dist/cjs/assets/source/index.js +22 -0
  44. package/dist/cjs/assets/source/index.js.map +1 -0
  45. package/dist/cjs/assets/source/parser/AssetParsers.d.ts +9 -0
  46. package/dist/cjs/assets/source/parser/AssetParsers.js +14 -0
  47. package/dist/cjs/assets/source/parser/AssetParsers.js.map +1 -0
  48. package/dist/cjs/assets/source/parser/ResponseParser.d.ts +7 -0
  49. package/dist/cjs/assets/source/parser/ResponseParser.js +3 -0
  50. package/dist/cjs/assets/source/parser/ResponseParser.js.map +1 -0
  51. package/dist/cjs/assets/source/parser/index.d.ts +2 -0
  52. package/dist/cjs/assets/source/parser/index.js +20 -0
  53. package/dist/cjs/assets/source/parser/index.js.map +1 -0
  54. package/dist/cjs/cache/Caching.js +9 -0
  55. package/dist/cjs/cache/Caching.js.map +1 -1
  56. package/dist/cjs/index.d.ts +2 -0
  57. package/dist/cjs/index.js +2 -0
  58. package/dist/cjs/index.js.map +1 -1
  59. package/dist/cjs/model/scene/ModelObject.js +1 -1
  60. package/dist/cjs/model/scene/ModelObject.js.map +1 -1
  61. package/dist/cjs/renderer/MineRenderScene.d.ts +1 -1
  62. package/dist/cjs/renderer/MineRenderScene.js +5 -6
  63. package/dist/cjs/renderer/MineRenderScene.js.map +1 -1
  64. package/dist/cjs/renderer/SceneObject.js +1 -0
  65. package/dist/cjs/renderer/SceneObject.js.map +1 -1
  66. package/dist/cjs/three/index.d.ts +0 -2
  67. package/dist/cjs/three/index.js +0 -2
  68. package/dist/cjs/three/index.js.map +1 -1
  69. package/dist/esm/UVMapper.js +6 -2
  70. package/dist/esm/UVMapper.js.map +1 -1
  71. package/dist/esm/assets/AssetKey.js.map +1 -1
  72. package/dist/esm/assets/AssetLoader.d.ts +6 -20
  73. package/dist/esm/assets/AssetLoader.js +35 -124
  74. package/dist/esm/assets/AssetLoader.js.map +1 -1
  75. package/dist/esm/assets/BlockStates.d.ts +1 -0
  76. package/dist/esm/assets/BlockStates.js +8 -4
  77. package/dist/esm/assets/BlockStates.js.map +1 -1
  78. package/dist/esm/assets/ModelTextures.js +3 -2
  79. package/dist/esm/assets/ModelTextures.js.map +1 -1
  80. package/dist/esm/assets/Models.js +3 -4
  81. package/dist/esm/assets/Models.js.map +1 -1
  82. package/dist/esm/assets/index.d.ts +1 -0
  83. package/dist/esm/assets/index.js +1 -0
  84. package/dist/esm/assets/index.js.map +1 -1
  85. package/dist/esm/assets/source/AssetSource.d.ts +8 -0
  86. package/dist/esm/assets/source/AssetSource.js +5 -0
  87. package/dist/esm/assets/source/AssetSource.js.map +1 -0
  88. package/dist/esm/assets/source/HostedAssetSource.d.ts +33 -0
  89. package/dist/esm/assets/source/HostedAssetSource.js +183 -0
  90. package/dist/esm/assets/source/HostedAssetSource.js.map +1 -0
  91. package/dist/esm/assets/source/archive/ArchiveAssetSource.d.ts +17 -0
  92. package/dist/esm/assets/source/archive/ArchiveAssetSource.js +122 -0
  93. package/dist/esm/assets/source/archive/ArchiveAssetSource.js.map +1 -0
  94. package/dist/esm/assets/source/archive/ArchiveEntry.d.ts +5 -0
  95. package/dist/esm/assets/source/archive/ArchiveEntry.js +2 -0
  96. package/dist/esm/assets/source/archive/ArchiveEntry.js.map +1 -0
  97. package/dist/esm/assets/source/archive/ArchiveProxy.d.ts +4 -0
  98. package/dist/esm/assets/source/archive/ArchiveProxy.js +2 -0
  99. package/dist/esm/assets/source/archive/ArchiveProxy.js.map +1 -0
  100. package/dist/esm/assets/source/archive/BrowserArchiveProxy.d.ts +9 -0
  101. package/dist/esm/assets/source/archive/BrowserArchiveProxy.js +30 -0
  102. package/dist/esm/assets/source/archive/BrowserArchiveProxy.js.map +1 -0
  103. package/dist/esm/assets/source/archive/index.d.ts +4 -0
  104. package/dist/esm/assets/source/archive/index.js +5 -0
  105. package/dist/esm/assets/source/archive/index.js.map +1 -0
  106. package/dist/esm/assets/source/index.d.ts +4 -0
  107. package/dist/esm/assets/source/index.js +6 -0
  108. package/dist/esm/assets/source/index.js.map +1 -0
  109. package/dist/esm/assets/source/parser/AssetParsers.d.ts +9 -0
  110. package/dist/esm/assets/source/parser/AssetParsers.js +11 -0
  111. package/dist/esm/assets/source/parser/AssetParsers.js.map +1 -0
  112. package/dist/esm/assets/source/parser/ResponseParser.d.ts +7 -0
  113. package/dist/esm/assets/source/parser/ResponseParser.js +2 -0
  114. package/dist/esm/assets/source/parser/ResponseParser.js.map +1 -0
  115. package/dist/esm/assets/source/parser/index.d.ts +2 -0
  116. package/dist/esm/assets/source/parser/index.js +4 -0
  117. package/dist/esm/assets/source/parser/index.js.map +1 -0
  118. package/dist/esm/cache/Caching.js +9 -0
  119. package/dist/esm/cache/Caching.js.map +1 -1
  120. package/dist/esm/index.d.ts +2 -0
  121. package/dist/esm/index.js +2 -0
  122. package/dist/esm/index.js.map +1 -1
  123. package/dist/esm/model/scene/ModelObject.js +1 -1
  124. package/dist/esm/model/scene/ModelObject.js.map +1 -1
  125. package/dist/esm/renderer/MineRenderScene.d.ts +1 -1
  126. package/dist/esm/renderer/MineRenderScene.js +5 -6
  127. package/dist/esm/renderer/MineRenderScene.js.map +1 -1
  128. package/dist/esm/renderer/SceneObject.js +1 -0
  129. package/dist/esm/renderer/SceneObject.js.map +1 -1
  130. package/dist/esm/three/index.d.ts +0 -2
  131. package/dist/esm/three/index.js +0 -2
  132. package/dist/esm/three/index.js.map +1 -1
  133. package/package.json +11 -5
  134. package/src/UVMapper.ts +10 -6
  135. package/src/assets/AssetKey.ts +0 -1
  136. package/src/assets/AssetLoader.ts +42 -119
  137. package/src/assets/BlockStates.ts +9 -5
  138. package/src/assets/ModelTextures.ts +3 -2
  139. package/src/assets/Models.ts +5 -5
  140. package/src/assets/index.ts +9 -8
  141. package/src/assets/source/AssetSource.ts +13 -0
  142. package/src/assets/source/HostedAssetSource.ts +191 -0
  143. package/src/assets/source/archive/ArchiveAssetSource.ts +124 -0
  144. package/src/assets/source/archive/ArchiveEntry.ts +5 -0
  145. package/src/assets/source/archive/ArchiveProxy.ts +7 -0
  146. package/src/assets/source/archive/BrowserArchiveProxy.ts +27 -0
  147. package/src/assets/source/archive/index.ts +4 -0
  148. package/src/assets/source/index.ts +5 -0
  149. package/src/assets/source/parser/AssetParsers.ts +11 -0
  150. package/src/assets/source/parser/ResponseParser.ts +9 -0
  151. package/src/assets/source/parser/index.ts +3 -0
  152. package/src/cache/Caching.ts +9 -0
  153. package/src/index.ts +68 -66
  154. package/src/model/scene/ModelObject.ts +1 -1
  155. package/src/renderer/MineRenderScene.ts +4 -5
  156. package/src/renderer/SceneObject.ts +1 -0
  157. package/src/three/index.ts +3 -5
@@ -8,6 +8,8 @@ import defaultBlockStates from "../model/defaultBlockStates.json";
8
8
  import { BlockStateProperties, BlockStatePropertyDefaults } from "../model/block/BlockStateProperties";
9
9
  import { AssetKey } from "./AssetKey";
10
10
  import { PersistentCache } from "../cache/PersistentCache";
11
+ import { AssetParser } from "./source/parser/AssetParsers";
12
+ import { ListAsset } from "../ListAsset";
11
13
 
12
14
  export class BlockStates {
13
15
 
@@ -16,7 +18,7 @@ export class BlockStates {
16
18
  // BlockState names are hardcoded
17
19
  @Memoize()
18
20
  public static async getList(): Promise<string[]> {
19
- return AssetLoader.loadOrRetryWithDefaults(new AssetKey(
21
+ return AssetLoader.get<ListAsset>(new AssetKey(
20
22
  DEFAULT_NAMESPACE,
21
23
  "_list",
22
24
  "blockstates",
@@ -24,7 +26,7 @@ export class BlockStates {
24
26
  "assets",
25
27
  ".json",
26
28
  DEFAULT_ROOT
27
- ), AssetLoader.LIST).then(r => r?.files ?? []);
29
+ ), AssetParser.LIST).then(r => r?.files ?? []);
28
30
  }
29
31
 
30
32
  public static getDefaultState(key: AssetKey): Maybe<BlockStatePropertyDefaults> {
@@ -41,9 +43,7 @@ export class BlockStates {
41
43
  const keyStr = key.serialize();
42
44
  return Caching.blockStateCache.get(keyStr, k => {
43
45
  return this.PERSISTENT_CACHE.getOrLoad(keyStr, k1 => {
44
- return AssetLoader.loadOrRetryWithDefaults(key, AssetLoader.BLOCKSTATE).then(asset => {
45
- return asset;
46
- })
46
+ return AssetLoader.get<BlockState>(key, AssetParser.BLOCKSTATE);
47
47
  })
48
48
  }).then(asset => {
49
49
  if (asset) {
@@ -61,4 +61,8 @@ export class BlockStates {
61
61
  return Promise.all(promises);
62
62
  }
63
63
 
64
+ public static async clearCache() {
65
+ await this.PERSISTENT_CACHE.clear();
66
+ }
67
+
64
68
  }
@@ -9,6 +9,7 @@ import { MinecraftTextureMeta } from "../MinecraftTextureMeta";
9
9
  import { PersistentCache } from "../cache/PersistentCache";
10
10
  import { keys } from "node-persist";
11
11
  import { AssetKey } from "./AssetKey";
12
+ import { AssetParser } from "./source/parser/AssetParsers";
12
13
 
13
14
  export class ModelTextures {
14
15
 
@@ -25,7 +26,7 @@ export class ModelTextures {
25
26
  public static async preload(key: AssetKey): Promise<Maybe<TextureAsset>> {
26
27
  const keyStr = key.serialize();
27
28
  return Caching.textureAssetCache.get(keyStr, k => {
28
- return AssetLoader.loadOrRetryWithDefaults<TextureAsset>(key, AssetLoader.IMAGE).then(asset => {
29
+ return AssetLoader.get<TextureAsset>(key, AssetParser.IMAGE).then(asset => {
29
30
  if (asset)
30
31
  asset.key = key;
31
32
  return asset;
@@ -42,7 +43,7 @@ export class ModelTextures {
42
43
 
43
44
  return Caching.textureMetaCache.get(keyStr, k => {
44
45
  return this.PERSISTENT_META_CACHE.getOrLoad(keyStr, k1 => {
45
- return AssetLoader.loadOrRetryWithDefaults<MinecraftTextureMeta>(key, AssetLoader.META).then(asset => {
46
+ return AssetLoader.get<MinecraftTextureMeta>(key, AssetParser.META).then(asset => {
46
47
  if (asset)
47
48
  asset.key = key;
48
49
  return asset;
@@ -8,6 +8,8 @@ import { Memoize } from "typescript-memoize";
8
8
  import { DEFAULT_NAMESPACE, DEFAULT_ROOT } from "./Assets";
9
9
  import { PersistentCache } from "../cache/PersistentCache";
10
10
  import { AssetKey } from "./AssetKey";
11
+ import { ListAsset } from "../ListAsset";
12
+ import { AssetParser } from "./source/parser/AssetParsers";
11
13
 
12
14
  export class Models {
13
15
 
@@ -15,7 +17,7 @@ export class Models {
15
17
 
16
18
  @Memoize()
17
19
  public static async getItemList(): Promise<string[]> {
18
- return AssetLoader.loadOrRetryWithDefaults(new AssetKey(
20
+ return AssetLoader.get<ListAsset>(new AssetKey(
19
21
  DEFAULT_NAMESPACE,
20
22
  "_list",
21
23
  "models",
@@ -23,7 +25,7 @@ export class Models {
23
25
  "assets",
24
26
  ".json",
25
27
  DEFAULT_ROOT
26
- ), AssetLoader.LIST).then(r => r?.files ?? []);
28
+ ), AssetParser.LIST).then(r => r?.files ?? []);
27
29
  }
28
30
 
29
31
  public static async loadAndMerge(key: AssetKey): Promise<Maybe<Model>> {
@@ -44,9 +46,7 @@ export class Models {
44
46
  const keyStr = key.serialize();
45
47
  return Caching.rawModelCache.get(keyStr, k => {
46
48
  return this.PERSISTENT_CACHE.getOrLoad(keyStr, k1 => {
47
- return AssetLoader.loadOrRetryWithDefaults(key, AssetLoader.MODEL).then(asset => {
48
- return asset;
49
- })
49
+ return AssetLoader.get<Model>(key, AssetParser.MODEL);
50
50
  })
51
51
  }).then(asset => {
52
52
  if (asset) {
@@ -1,8 +1,9 @@
1
- // autogenerated
2
- export * from './AssetKey';
3
- export * from './AssetLoader';
4
- export * from './Assets';
5
- export * from './BlockStates';
6
- export * from './Entities';
7
- export * from './ModelTextures';
8
- export * from './Models';
1
+ // autogenerated
2
+ export * from './AssetKey';
3
+ export * from './AssetLoader';
4
+ export * from './Assets';
5
+ export * from './BlockStates';
6
+ export * from './Entities';
7
+ export * from './ModelTextures';
8
+ export * from './Models';
9
+ export * from './source';
@@ -0,0 +1,13 @@
1
+ import { MinecraftAsset } from "../../MinecraftAsset";
2
+ import { AssetKey } from "../AssetKey";
3
+ import { Maybe } from "../../util";
4
+ import { AssetParser } from "./parser/AssetParsers";
5
+
6
+ export abstract class AssetSource {
7
+
8
+ protected constructor() {
9
+ }
10
+
11
+ public abstract get<T extends MinecraftAsset>(key: AssetKey, parser: AssetParser | string): Promise<Maybe<T>>;
12
+
13
+ }
@@ -0,0 +1,191 @@
1
+ import { AssetSource } from ".";
2
+ import { BlockState, Model, TextureAsset } from "../../model";
3
+ import { AxiosRequestConfig, AxiosResponse } from "axios";
4
+ import { Maybe, prefix } from "../../util";
5
+ import { MinecraftTextureMeta } from "../../MinecraftTextureMeta";
6
+ import { NBTAsset, NBTHelper } from "../../nbt";
7
+ import { ImageLoader } from "../../image";
8
+ import { ListAsset } from "../../ListAsset";
9
+ import { MinecraftAsset } from "../../MinecraftAsset";
10
+ import { AssetKey } from "../AssetKey";
11
+ import { DEFAULT_NAMESPACE, DEFAULT_ROOT } from "../Assets";
12
+ import { Requests } from "../../request";
13
+ import { AssetLoader } from "../AssetLoader";
14
+ import { AssetParser } from "./parser";
15
+ import { ResponseParser } from "./parser";
16
+ import merge from "ts-deepmerge";
17
+
18
+ const p = prefix("HostedAssetSource");
19
+
20
+ const DEFAULT_OPTIONS: HostedAssetSourceOptions = {
21
+ retryDefaults: true
22
+ }
23
+
24
+ export class HostedAssetSource extends AssetSource {
25
+
26
+ static readonly MODEL: ResponseParser<Model> = {
27
+ config(request: AxiosRequestConfig) {
28
+ },
29
+ parse(response: AxiosResponse): Maybe<Model> {
30
+ return response.data as Model;
31
+ }
32
+ }
33
+ static readonly BLOCKSTATE: ResponseParser<BlockState> = {
34
+ config(request: AxiosRequestConfig) {
35
+ },
36
+ parse(response: AxiosResponse): Maybe<BlockState> {
37
+ return response.data as BlockState;
38
+ }
39
+ }
40
+ static readonly META: ResponseParser<MinecraftTextureMeta> = {
41
+ config(request: AxiosRequestConfig) {
42
+ // request.responseType = "arraybuffer";
43
+ },
44
+ parse(response: AxiosResponse): Maybe<MinecraftTextureMeta> {
45
+ return response.data as MinecraftTextureMeta;
46
+ }
47
+ }
48
+ static readonly NBT: ResponseParser<NBTAsset> = {
49
+ config(request: AxiosRequestConfig) {
50
+ request.responseType = "arraybuffer";
51
+ },
52
+ parse(response: AxiosResponse): Promise<Maybe<NBTAsset>> {
53
+ return NBTHelper.fromBuffer(Buffer.from(response.data));
54
+ }
55
+ }
56
+ static readonly IMAGE: ResponseParser<TextureAsset> = {
57
+ config(request: AxiosRequestConfig) {
58
+ request.responseType = "arraybuffer";
59
+ },
60
+ async parse(response: AxiosResponse): Promise<Maybe<TextureAsset>> {
61
+ return await ImageLoader.processResponse(response) as TextureAsset;
62
+ }
63
+ }
64
+ static readonly LIST: ResponseParser<ListAsset> = {
65
+ config(request: AxiosRequestConfig) {
66
+ },
67
+ parse(response: AxiosResponse): Maybe<ListAsset> {
68
+ return response.data as ListAsset;
69
+ }
70
+ }
71
+ static readonly JSON: ResponseParser<any> = {
72
+ config(request: AxiosRequestConfig) {
73
+ },
74
+ parse(response: AxiosResponse): Maybe<any> {
75
+ return response.data;
76
+ }
77
+ }
78
+
79
+ static readonly PARSER_MAP: Map<AssetParser, ResponseParser<any>> = new Map<AssetParser, ResponseParser<any>>([
80
+ [AssetParser.MODEL, HostedAssetSource.MODEL],
81
+ [AssetParser.BLOCKSTATE, HostedAssetSource.BLOCKSTATE],
82
+ [AssetParser.META, HostedAssetSource.META],
83
+ [AssetParser.NBT, HostedAssetSource.NBT],
84
+ [AssetParser.IMAGE, HostedAssetSource.IMAGE],
85
+ [AssetParser.LIST, HostedAssetSource.LIST],
86
+ [AssetParser.JSON, HostedAssetSource.JSON],
87
+ ]);
88
+
89
+ public async loadOrRetryWithDefaults<T extends MinecraftAsset>(key: AssetKey, parser: ResponseParser<T>): Promise<Maybe<T>> {
90
+ const direct = await this.load<T>(key, parser);
91
+ if (typeof direct !== "undefined") {
92
+ return direct;
93
+ }
94
+ if (key.namespace !== DEFAULT_NAMESPACE) {
95
+ console.info(p, "Retrying", key, "with default namespace");
96
+ // Try on the same host but with default minecraft: namespace
97
+ const namespaceKey = new AssetKey(DEFAULT_NAMESPACE, key.path, key.assetType, key.type, key.rootType, key.extension, key.root);
98
+ const namespaced = await this.load<T>(namespaceKey, parser);
99
+ if (typeof namespaced !== "undefined") {
100
+ return namespaced;
101
+ }
102
+ if ((typeof key.root !== "undefined" && key.root !== DEFAULT_ROOT) || (typeof this.root !== "undefined" && this.root !== DEFAULT_ROOT)) {
103
+ console.info(p, "Retrying", key, "with default root+namespace");
104
+ // Try both defaults
105
+ const namespacedRootedKey = new AssetKey(DEFAULT_NAMESPACE, key.path, key.assetType, key.type, key.rootType, key.extension, DEFAULT_ROOT);
106
+ const namespacedRooted = await this.load<T>(namespacedRootedKey, parser);
107
+ if (typeof namespacedRooted !== "undefined") {
108
+ return namespacedRooted;
109
+ }
110
+ }
111
+ } else if ((typeof key.root !== "undefined" && key.root !== DEFAULT_ROOT) || (typeof this.root !== "undefined" && this.root !== DEFAULT_ROOT)) {
112
+ console.info(p, "Retrying", key, "with default root");
113
+ // Try on default root
114
+ const rootKey = new AssetKey(key.namespace, key.path, key.assetType, key.type, key.rootType, key.extension, DEFAULT_ROOT);
115
+ const rooted = await this.load<T>(rootKey, parser);
116
+ if (typeof rooted !== "undefined") {
117
+ return rooted;
118
+ }
119
+ }
120
+ return undefined;
121
+ }
122
+
123
+
124
+ protected async load<T extends MinecraftAsset>(key: AssetKey, parser: ResponseParser<T>): Promise<Maybe<T>> {
125
+ console.info(p, "Loading", key);
126
+ const url = `${ this.assetBasePath(key) }${ key.type !== undefined ? key.type + '/' : '' }${ key.path }${ key.extension }`;
127
+ console.debug(p, url);
128
+ let req: AxiosRequestConfig = {
129
+ url: url
130
+ };
131
+ parser.config(req);
132
+ // @ts-ignore
133
+ return await Requests.mcAssetRequest(req)
134
+ .then(response => {
135
+ if (response && response.data) {
136
+ try {
137
+ return parser.parse(response);
138
+ } catch (e) {
139
+ console.warn("Parser failed to process response", response, e);
140
+ throw e;
141
+ }
142
+ }
143
+ return undefined;
144
+ })
145
+ .catch(err => {
146
+ if (err.response) {
147
+ let response = err.response as AxiosResponse;
148
+ if (response.status === 404) {
149
+ console.debug(p, key, "not found");
150
+ return undefined;
151
+ }
152
+ }
153
+ console.debug(p, "Failed to load", key, err?.message);
154
+ return undefined;
155
+ })
156
+ }
157
+
158
+ public assetBasePath(key: AssetKey) {
159
+ return `${ key.root ?? this.root ?? AssetLoader.ROOT }/${ key.rootType !== undefined ? key.rootType + '/' : '' }${ key.namespace !== undefined ? key.namespace + '/' : '' }${ key.assetType !== undefined ? key.assetType + '/' : '' }`;
160
+ }
161
+
162
+ private readonly _root: string;
163
+ private readonly _options: HostedAssetSourceOptions;
164
+
165
+ public constructor(root: string, options?: Partial<HostedAssetSourceOptions>) {
166
+ super();
167
+ this._root = root;
168
+ this._options = merge({}, DEFAULT_OPTIONS, options ?? {});
169
+ }
170
+
171
+ public get root(): string {
172
+ return this._root;
173
+ }
174
+
175
+ public get options(): HostedAssetSourceOptions {
176
+ return this._options;
177
+ }
178
+
179
+ public get<T extends MinecraftAsset>(key: AssetKey, parser: AssetParser): Promise<Maybe<T>> {
180
+ const responseParser = HostedAssetSource.PARSER_MAP.get(parser) as ResponseParser<T>;
181
+ if (this.options.retryDefaults) {
182
+ return this.loadOrRetryWithDefaults(key, responseParser);
183
+ }
184
+ return this.load(key, responseParser);
185
+ }
186
+
187
+ }
188
+
189
+ interface HostedAssetSourceOptions {
190
+ retryDefaults: boolean;
191
+ }
@@ -0,0 +1,124 @@
1
+ import { AssetSource } from "../AssetSource";
2
+ import { ArchiveProxy } from "./ArchiveProxy";
3
+ import { ArchiveEntry } from "./ArchiveEntry";
4
+ import { MinecraftAsset } from "../../../MinecraftAsset";
5
+ import { AssetKey } from "../../AssetKey";
6
+ import { AssetParser, ResponseParser } from "../parser";
7
+ import { Maybe, prefix } from "../../../util";
8
+ import { Requests } from "../../../request";
9
+ import { HostedAssetSource } from "../HostedAssetSource";
10
+ import { AssetLoader } from "../../AssetLoader";
11
+ import { AxiosRequestConfig, AxiosResponse } from "axios";
12
+ import { BrowserArchiveProxy } from "./BrowserArchiveProxy";
13
+
14
+ const p = prefix("ArchiveAssetSource");
15
+
16
+ export class ArchiveAssetSource extends AssetSource implements ArchiveProxy {
17
+
18
+ readonly _archiveProxy: ArchiveProxy;
19
+
20
+ constructor(archiveProxy: ArchiveProxy) {
21
+ super();
22
+ console.log(archiveProxy)
23
+ this._archiveProxy = archiveProxy;
24
+ }
25
+
26
+ public static blob(blob: Blob): ArchiveAssetSource {
27
+ return new ArchiveAssetSource(new BrowserArchiveProxy(blob));
28
+ }
29
+
30
+ public async getEntries(): Promise<ArchiveEntry[]> {
31
+ return this._archiveProxy.getEntries();
32
+ }
33
+
34
+ public async getEntry(path: string): Promise<Maybe<ArchiveEntry>> {
35
+ const entries = await this.getEntries();
36
+ return entries.find(e => e.filename === path);
37
+ }
38
+
39
+ async get<T extends MinecraftAsset>(key: AssetKey, parser: AssetParser | string): Promise<Maybe<T>> {
40
+ const responseParser = HostedAssetSource.PARSER_MAP.get(parser as AssetParser) as ResponseParser<T>;
41
+
42
+ console.log("ArchiveAssetSource.get", key, parser);
43
+ let entries = (await this.getEntries());
44
+
45
+ const path = `${this.assetBasePath(key)}${key.type !== undefined ? key.type + '/' : ''}${key.path}${key.extension}`;
46
+ console.log(path);
47
+ const entry = await this.getEntry(path);
48
+ console.log(entry);
49
+ if (!entry) {
50
+ return undefined;
51
+ }
52
+
53
+ let blob = await entry.getData();
54
+ const url = URL.createObjectURL(blob);
55
+ console.log(url);
56
+ let req = {
57
+ url: url,
58
+ };
59
+ responseParser.config(req);
60
+ return Requests.genericRequest(req)
61
+ .then(response => {
62
+ if (response && response.data) {
63
+ try {
64
+ return responseParser.parse(response);
65
+ } catch (e) {
66
+ console.warn("Parser failed to process response", response, e);
67
+ throw e;
68
+ }
69
+ }
70
+ return undefined;
71
+ })
72
+ .catch(err => {
73
+ if (err.response) {
74
+ let response = err.response as AxiosResponse;
75
+ if (response.status === 404) {
76
+ console.debug(p, key, "not found");
77
+ return undefined;
78
+ }
79
+ }
80
+ console.debug(p, "Failed to load", key, err?.message);
81
+ return undefined;
82
+ })
83
+ }
84
+
85
+ //TODO: abstract this
86
+ protected async load<T extends MinecraftAsset>(key: AssetKey, parser: ResponseParser<T>): Promise<Maybe<T>> {
87
+ console.info(p, "Loading", key);
88
+ const url = `${this.assetBasePath(key)}${key.type !== undefined ? key.type + '/' : ''}${key.path}${key.extension}`;
89
+ console.debug(p, url);
90
+ let req: AxiosRequestConfig = {
91
+ url: url
92
+ };
93
+ parser.config(req);
94
+ // @ts-ignore
95
+ return await Requests.genericRequest(req)
96
+ .then(response => {
97
+ if (response && response.data) {
98
+ try {
99
+ return parser.parse(response);
100
+ } catch (e) {
101
+ console.warn("Parser failed to process response", response, e);
102
+ throw e;
103
+ }
104
+ }
105
+ return undefined;
106
+ })
107
+ .catch(err => {
108
+ if (err.response) {
109
+ let response = err.response as AxiosResponse;
110
+ if (response.status === 404) {
111
+ console.debug(p, key, "not found");
112
+ return undefined;
113
+ }
114
+ }
115
+ console.debug(p, "Failed to load", key, err?.message);
116
+ return undefined;
117
+ })
118
+ }
119
+
120
+ public assetBasePath(key: AssetKey) {
121
+ return `${key.rootType !== undefined ? key.rootType + '/' : ''}${key.namespace !== undefined ? key.namespace + '/' : ''}${key.assetType !== undefined ? key.assetType + '/' : ''}`;
122
+ }
123
+
124
+ }
@@ -0,0 +1,5 @@
1
+ export interface ArchiveEntry {
2
+ filename: string;
3
+ directory: boolean;
4
+ getData(): Promise<Blob>;
5
+ }
@@ -0,0 +1,7 @@
1
+ import { ArchiveEntry } from "./ArchiveEntry";
2
+
3
+ export interface ArchiveProxy {
4
+
5
+ getEntries(): Promise<ArchiveEntry[]>;
6
+
7
+ }
@@ -0,0 +1,27 @@
1
+ import { ArchiveProxy } from "./ArchiveProxy";
2
+ import { BlobReader, BlobWriter, ZipReader } from "@zip.js/zip.js";
3
+ import { ArchiveEntry } from "./ArchiveEntry";
4
+
5
+ export class BrowserArchiveProxy implements ArchiveProxy {
6
+
7
+ readonly _blob: Blob;
8
+ readonly _reader: ZipReader<Blob>;
9
+
10
+ constructor(blob: Blob) {
11
+ this._blob = blob;
12
+ this._reader = new ZipReader(new BlobReader(this._blob));
13
+ }
14
+
15
+ public async getEntries(): Promise<ArchiveEntry[]> {
16
+ return (await this._reader.getEntries()).map(e => {
17
+ return {
18
+ filename: e.filename,
19
+ directory: e.directory,
20
+ getData(): Promise<Blob> {
21
+ return e.getData!(new BlobWriter())
22
+ }
23
+ }
24
+ })
25
+ }
26
+
27
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./ArchiveEntry";
2
+ export * from "./ArchiveProxy";
3
+ export * from "./BrowserArchiveProxy";
4
+ export * from "./ArchiveAssetSource";
@@ -0,0 +1,5 @@
1
+ // autogenerated
2
+ export * from './AssetSource';
3
+ export * from './HostedAssetSource';
4
+ export * from './parser';
5
+ export * from './archive';
@@ -0,0 +1,11 @@
1
+ export enum AssetParser {
2
+
3
+ MODEL = "model",
4
+ BLOCKSTATE = "blockstate",
5
+ META = "meta",
6
+ NBT = "nbt",
7
+ IMAGE = "image",
8
+ LIST = "list",
9
+ JSON = "json",
10
+
11
+ }
@@ -0,0 +1,9 @@
1
+ import { AxiosRequestConfig, AxiosResponse } from "axios";
2
+ import { Maybe } from "../../../util";
3
+ import { MinecraftAsset } from "../../../MinecraftAsset";
4
+
5
+ export interface ResponseParser<T extends MinecraftAsset> {
6
+ config(request: AxiosRequestConfig);
7
+
8
+ parse(response: AxiosResponse): Maybe<T> | Promise<Maybe<T>>;
9
+ }
@@ -0,0 +1,3 @@
1
+ // autogenerated
2
+ export * from './AssetParsers';
3
+ export * from './ResponseParser';
@@ -97,6 +97,15 @@ export class Caching {
97
97
  public static clear() {
98
98
  this.rawImageCache.invalidateAll();
99
99
  this.textureCache.invalidateAll();
100
+ this.imageDataCache.invalidateAll();
101
+ this.wrappedImageCache.invalidateAll();
102
+ this.canvasImageDataCache.invalidateAll();
103
+ this.materialCache.invalidateAll();
104
+ this.modelTextureAtlasCache.invalidateAll();
105
+ this.textureAssetCache.invalidateAll();
106
+ //TODO: add all
107
+ this.rawModelCache.invalidateAll();
108
+ this.mergedModelCache.invalidateAll();
100
109
  }
101
110
 
102
111
  public static end() {