1 | # Resource Manager
|
2 |
|
3 | [![npm version](https://img.shields.io/npm/v/@xmcl/resource-manager.svg)](https://www.npmjs.com/package/@xmcl/resource-manager)
|
4 | [![npm](https://img.shields.io/npm/l/@xmcl/minecraft-launcher-core.svg)](https://github.com/voxelum/minecraft-launcher-core-node/blob/master/LICENSE)
|
5 | [![Build Status](https://github.com/voxelum/minecraft-launcher-core-node/workflows/Release%20Pre-Check/badge.svg)](https://github.com/voxelum/minecraft-launcher-core-node/workflows/Release%20Pre-Check/badge.svg)
|
6 |
|
7 | ## Usage
|
8 |
|
9 | ### Load Minecraft Resource
|
10 |
|
11 | You can use this module in nodejs/electron:
|
12 |
|
13 | ```ts
|
14 | import { ResourceManager, ResourceLocation } from "@xmcl/resource-manager"
|
15 | const manager: ResourceManager<Buffer> = new ResourceManager();
|
16 |
|
17 | // add a resource source which load resource from file
|
18 | await manager.addResourceSource(new MyFileSystemResourceSource('/base/path'));
|
19 |
|
20 | // load grass block model resource; it will load file at `assets/${location.domain}/${location.path}`
|
21 | // which is '/base/path/assets/minecraft/models/block/grass.json'
|
22 | // same logic with minecraft
|
23 | const resource = await manager.load(ResourceLocation.ofModelPath('block/grass'));
|
24 |
|
25 | const url: string = resource.url; // your resource url which is file:///base/path/assets/minecraft/models/block/grass.json
|
26 | const content: Buffer = resource.content; // your resource content
|
27 | const modelJSON = JSON.parse(content.toString());
|
28 | ```
|
29 |
|
30 | You can also use this module in browser:
|
31 |
|
32 | ```ts
|
33 | import { ResourceManager, ResourceLocation } from "@xmcl/resource-manager"
|
34 | const manager: ResourceManager<string> = new ResourceManager();
|
35 |
|
36 | // add a resource source which load resource from an remote url
|
37 | await manager.addResourceSource(new MyRemoteWhateverResourceSource('https://my-domain/xxx'));
|
38 |
|
39 | // load grass block model resource; it will load file at `assets/${location.domain}/${location.path}`
|
40 | // which is 'https://my-domain/xxx/assets/minecraft/models/block/grass.json'
|
41 | // same logic with minecraft
|
42 | const resource = await manager.load(ResourceLocation.ofModelPath('block/grass'));
|
43 |
|
44 | const url: string = resource.url; // your resource url which is https://my-domain/xxx/assets/minecraft/models/block/grass.json
|
45 | const content: string = resource.content; // your resource content string
|
46 | const modelJSON = JSON.parse(content);
|
47 | ```
|
48 |
|
49 | Please notice that in the sample above, all the `ResourceSource` should be implemented by yourself.
|
50 |
|
51 | The resource manager will do the simplest cache for same resource location.
|
52 |
|
53 | You can clear the cache by:
|
54 |
|
55 | ```ts
|
56 | manager.clearCache();
|
57 | ```
|
58 |
|
59 | ### Load Minecraft Block Model
|
60 |
|
61 | You can use this to load Minecraft block model and texture.
|
62 |
|
63 | ```ts
|
64 | import { ResourceManager, ModelLoader, TextureRegistry, ModelRegistry } from "@xmcl/resource-manager";
|
65 | import { BlockModel } from "@xmcl/common";
|
66 |
|
67 | const man = new ResourceManager();
|
68 | // setup resource manager
|
69 | man.addResourceSource(new YourCustomizedResourceSource());
|
70 |
|
71 | const loader = new ModelLoader(man);
|
72 |
|
73 | await loader.loadModel("block/grass"); // load grass model
|
74 | await loader.loadModel("block/stone"); // load stone model
|
75 | // ... load whatever you want model
|
76 |
|
77 | const textures: TextureRegistry = loader.textures;
|
78 | const models: ModelRegistry = loader.models;
|
79 |
|
80 | const resolvedModel: BlockModel.Resolved = models["block/grass"];
|
81 | ```
|
82 |
|
83 |
|