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