UNPKG

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