Skip to content

Commit f7b8528

Browse files
committed
feat: allow passing an alternative client URL function
1 parent 4c378e7 commit f7b8528

File tree

5 files changed

+672
-557
lines changed

5 files changed

+672
-557
lines changed

README.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ renderSchematic(document.querySelector('#schematicRenderer'), SCHEMATIC_FILE, {
1919
size: 500,
2020
renderArrow: false,
2121
renderBars: false,
22-
corsBypassUrl: 'https://url-to-cors-anywhere/'
22+
corsBypassUrl: 'https://url-to-cors-anywhere/',
2323
});
2424
```
2525

@@ -33,14 +33,50 @@ The final argument is an options object that allows configuring various settings
3333

3434
```typescript
3535
interface SchematicRenderOptions {
36-
size?: number; // Force the size of the canvas viewport, if not present use canvas size
37-
corsBypassUrl: string; // A url of a cors-anywhere instance to allow access to MC server jars
38-
resourcePacks?: string[]; // A list of resource pack URLs in priority order
39-
renderBars?: boolean; // Whether a grid should be rendered
40-
renderArrow?: boolean; // Whether an arrow to show direction should be rendered
41-
orbit?: boolean; // Whether the view should automatically rotate when not being dragged by the user
42-
antialias?: boolean; // Whether antialiasing should be enabled
43-
backgroundColor?: number | 'transparent'; // Background color of the canvas (default: 0xffffff)
36+
/**
37+
* Usage as number is deprecated and will be removed
38+
*/
39+
size?: number | { width: number; height: number };
40+
/**
41+
* A url of a cors-anywhere instance to allow access to MC server jars. Required by the default `getClientJarUrl` function
42+
*/
43+
corsBypassUrl?: string;
44+
/**
45+
* A function that returns the url of the client jar to use. Defaults to using the EngineHub Cassette Deck service
46+
*/
47+
getClientJarUrl?: (props: GetClientJarUrlProps) => Promise<string>;
48+
/**
49+
* A list of resource pack URLs in priority order
50+
*/
51+
resourcePacks?: string[];
52+
/**
53+
* Whether a grid should be rendered
54+
*/
55+
renderBars?: boolean;
56+
/**
57+
* Whether an arrow to show direction should be rendered
58+
*/
59+
renderArrow?: boolean;
60+
/**
61+
* Whether the view should automatically rotate when not being dragged by the user
62+
*/
63+
orbit?: boolean;
64+
/**
65+
* Whether antialiasing should be enabled
66+
*/
67+
antialias?: boolean;
68+
/**
69+
* Background color of the canvas (default: 0xffffff), or if it should be transparent
70+
*/
71+
backgroundColor?: number | 'transparent';
72+
/**
73+
* Whether to enable further debug information
74+
*/
75+
debug?: boolean;
76+
/**
77+
* Only update the view when {@link SchematicHandles#render} is called. This is useful if you want to control the rendering yourself
78+
*/
79+
disableAutoRender?: boolean;
4480
}
4581
```
4682

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"homepage": "https://github.com/EngineHub/SchematicWebViewer#readme",
2929
"dependencies": {
3030
"@enginehub/schematicjs": "^0.8.0",
31-
"babylonjs": "5.22.1",
31+
"babylonjs": "5.32.1",
3232
"buffer": "^6.0.3",
3333
"deepmerge": "^4.2.2",
3434
"gzip-js": "^0.3.2",
@@ -37,15 +37,15 @@
3737
},
3838
"devDependencies": {
3939
"@types/gzip-js": "^0.3.3",
40-
"@typescript-eslint/eslint-plugin": "^5.36.1",
41-
"@typescript-eslint/parser": "^5.36.1",
42-
"eslint": "^8.23.0",
40+
"@typescript-eslint/eslint-plugin": "^5.42.1",
41+
"@typescript-eslint/parser": "^5.42.1",
42+
"eslint": "^8.27.0",
4343
"eslint-config-prettier": "^8.5.0",
4444
"eslint-plugin-prettier": "^4.2.1",
45-
"parcel": "^2.7.0",
45+
"parcel": "^2.8.0",
4646
"prettier": "^2.6.2",
4747
"prettier-eslint": "^15.0.1",
48-
"typescript": "^4.8.2"
48+
"typescript": "^4.8.4"
4949
},
5050
"peerDependencies": {
5151
"@enginehub/schematicjs": "^0.7.0"

src/renderer/renderer.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Block } from '@enginehub/schematicjs';
22
import { loadSchematic } from '@enginehub/schematicjs';
33
import type { SchematicHandles } from '.';
4-
import type { SchematicRenderOptions } from './types';
4+
import type { GetClientJarUrlProps, SchematicRenderOptions } from './types';
55
import { getModelLoader } from './model/loader';
66
import { getResourceLoader } from '../resource/resourceLoader';
77
import type { BlockModelData } from './model/types';
@@ -29,11 +29,27 @@ const CASSETTE_DECK_URL = `https://services.enginehub.org/cassette-deck/minecraf
2929
const URL_1_13 =
3030
'https://launcher.mojang.com/v1/objects/c0b970952cdd279912da384cdbfc0c26e6c6090b/client.jar';
3131

32+
async function getClientJarUrlDefault({
33+
dataVersion,
34+
corsBypassUrl,
35+
}: GetClientJarUrlProps): Promise<string> {
36+
const versionManifestFile = dataVersion
37+
? await (
38+
await fetch(`${corsBypassUrl}${CASSETTE_DECK_URL}${dataVersion}`)
39+
).json()
40+
: undefined;
41+
42+
return `${corsBypassUrl}${
43+
versionManifestFile?.[0]?.clientJarUrl ?? URL_1_13
44+
}`;
45+
}
46+
3247
export async function renderSchematic(
3348
canvas: HTMLCanvasElement,
3449
schematic: string,
3550
{
3651
corsBypassUrl,
52+
getClientJarUrl = getClientJarUrlDefault,
3753
resourcePacks,
3854
size,
3955
orbit = true,
@@ -111,16 +127,11 @@ export async function renderSchematic(
111127
const cameraOffset = Math.max(worldWidth, worldLength, worldHeight) / 2 + 1;
112128
camera.radius = cameraOffset * 3;
113129

114-
const versionManifestFile = loadedSchematic.dataVersion
115-
? await (
116-
await fetch(
117-
`${corsBypassUrl}${CASSETTE_DECK_URL}${loadedSchematic.dataVersion}`
118-
)
119-
).json()
120-
: undefined;
121-
122130
const resourceLoader = await getResourceLoader([
123-
`${corsBypassUrl}${versionManifestFile?.[0]?.clientJarUrl ?? URL_1_13}`,
131+
await getClientJarUrl({
132+
dataVersion: loadedSchematic.dataVersion,
133+
corsBypassUrl,
134+
}),
124135
...(resourcePacks ?? []),
125136
]);
126137
const modelLoader = getModelLoader(resourceLoader);

src/renderer/types.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,54 @@ export interface SchematicHandles {
3030
getEngine(): Engine;
3131
}
3232

33+
export interface GetClientJarUrlProps {
34+
dataVersion: number;
35+
corsBypassUrl: string;
36+
}
37+
3338
export interface SchematicRenderOptions {
3439
/**
35-
* Usage as number is deprecated and will be removed.
40+
* Usage as number is deprecated and will be removed
3641
*/
3742
size?: number | { width: number; height: number };
38-
corsBypassUrl: string;
43+
/**
44+
* A url of a cors-anywhere instance to allow access to MC server jars. Required by the default `getClientJarUrl` function
45+
*/
46+
corsBypassUrl?: string;
47+
/**
48+
* A function that returns the url of the client jar to use. Defaults to using the EngineHub Cassette Deck service
49+
*/
50+
getClientJarUrl?: (props: GetClientJarUrlProps) => Promise<string>;
51+
/**
52+
* A list of resource pack URLs in priority order
53+
*/
3954
resourcePacks?: string[];
55+
/**
56+
* Whether a grid should be rendered
57+
*/
4058
renderBars?: boolean;
59+
/**
60+
* Whether an arrow to show direction should be rendered
61+
*/
4162
renderArrow?: boolean;
63+
/**
64+
* Whether the view should automatically rotate when not being dragged by the user
65+
*/
4266
orbit?: boolean;
67+
/**
68+
* Whether antialiasing should be enabled
69+
*/
4370
antialias?: boolean;
71+
/**
72+
* Background color of the canvas (default: 0xffffff), or if it should be transparent
73+
*/
4474
backgroundColor?: number | 'transparent';
75+
/**
76+
* Whether to enable further debug information
77+
*/
4578
debug?: boolean;
79+
/**
80+
* Only update the view when {@link SchematicHandles#render} is called. This is useful if you want to control the rendering yourself
81+
*/
4682
disableAutoRender?: boolean;
4783
}

0 commit comments

Comments
 (0)