UNPKG

3.3 kBMarkdownView Raw
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
11You can use this module in nodejs/electron:
12
13```ts
14import { ResourceManager, ResourceLocation } from "@xmcl/resource-manager"
15const manager: ResourceManager<Buffer> = new ResourceManager();
16
17// add a resource source which load resource from file
18await 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
23const resource = await manager.load(ResourceLocation.ofModelPath('block/grass'));
24
25const url: string = resource.url; // your resource url which is file:///base/path/assets/minecraft/models/block/grass.json
26const content: Buffer = resource.content; // your resource content
27const modelJSON = JSON.parse(content.toString());
28```
29
30You can also use this module in browser:
31
32```ts
33import { ResourceManager, ResourceLocation } from "@xmcl/resource-manager"
34const manager: ResourceManager<string> = new ResourceManager();
35
36// add a resource source which load resource from an remote url
37await 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
42const resource = await manager.load(ResourceLocation.ofModelPath('block/grass'));
43
44const url: string = resource.url; // your resource url which is https://my-domain/xxx/assets/minecraft/models/block/grass.json
45const content: string = resource.content; // your resource content string
46const modelJSON = JSON.parse(content);
47```
48
49Please notice that in the sample above, all the `ResourceSource` should be implemented by yourself.
50
51The resource manager will do the simplest cache for same resource location.
52
53You can clear the cache by:
54
55```ts
56manager.clearCache();
57```
58
59### Load Minecraft Block Model
60
61You 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