diff --git a/package-lock.json b/package-lock.json index 9fdaf09b..fa7aebc7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,14 @@ "@mcschema/core": "^0.11.0" } }, + "@mcschema/java-1.17": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@mcschema/java-1.17/-/java-1.17-0.1.0.tgz", + "integrity": "sha512-k2lIq7PxokOBXj/7bX0x7XPDQWXUqFIcdt9VP6n9+RDefabNSUDQh07qPFAyjOqMUU/eDyibyZSMnAzSLuLuMg==", + "requires": { + "@mcschema/core": "^0.11.0" + } + }, "@mcschema/locales": { "version": "0.1.11", "resolved": "https://registry.npmjs.org/@mcschema/locales/-/locales-0.1.11.tgz", diff --git a/package.json b/package.json index 93d1e6f8..6865815f 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dependencies": { "@mcschema/core": "^0.11.0", "@mcschema/java-1.16": "^0.5.12", + "@mcschema/java-1.17": "^0.1.0", "@mcschema/locales": "^0.1.11", "@types/google.analytics": "0.0.40", "@types/split.js": "^1.4.0", diff --git a/src/app/RegistryFetcher.ts b/src/app/RegistryFetcher.ts index 2a5dbc4d..0876b861 100644 --- a/src/app/RegistryFetcher.ts +++ b/src/app/RegistryFetcher.ts @@ -1,17 +1,26 @@ import { CollectionRegistry } from '@mcschema/core' +import config from '../config.json' -export const mcdata = (version: string, registry: string) => { - return `https://raw.githubusercontent.com/Arcensoth/mcdata/${version}/processed/reports/registries/${registry}/data.min.json` +const baseUrl = 'https://raw.githubusercontent.com/Arcensoth/mcdata' +export const mcdata = (ref: string, registry: string) => { + return `${baseUrl}/${ref}/processed/reports/registries/${registry}/data.min.json` } -export const RegistryFetcher = async (target: CollectionRegistry, registries: string[], version = 'master') => { - await Promise.all(registries.map(async r => { +export const RegistryFetcher = async (target: CollectionRegistry, versionId: string) => { + const version = config.versions.find(v => v.id === versionId) + if (!version) return + await Promise.all(config.registries.map(async r => { + const id = typeof r === 'string' ? r : r.id + const url = typeof r === 'string' + ? mcdata(version.mcdata_ref, r) + : `${baseUrl}/${version.mcdata_ref}/${r.path}` try { - const res = await fetch(mcdata(version, r)) + const res = await fetch(url) const data = await res.json() - target.register(r, data.values) + console.log(r, url, data) + target.register(id, data.values) } catch (e) { - console.error(`Error occurred while fetching registry for ${r}.`, e) + console.error(`Error occurred while fetching registry "${id}":`, e) } })) } diff --git a/src/app/app.ts b/src/app/app.ts index 40deb0cf..1e0fa518 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -1,6 +1,7 @@ import Split from 'split.js' -import { Base, DataModel, ModelPath, Path } from '@mcschema/core' -import { getCollections, getSchemas } from '@mcschema/java-1.16' +import { Base, CollectionRegistry, DataModel, ModelPath, Path, SchemaRegistry } from '@mcschema/core' +import * as java16 from '@mcschema/java-1.16' +import * as java17 from '@mcschema/java-1.17' import { VisualizerView } from './visualization/VisualizerView' import { RegistryFetcher } from './RegistryFetcher' import { TreeView } from './TreeView' @@ -11,8 +12,19 @@ import { BiomeNoiseVisualizer } from './visualization/BiomeNoiseVisualizer' import { Mounter } from './Mounter' import { getLanguage, hasLocale, locale, registerLocale, setLanguage } from './locales' +const versionSchemas: { + [versionId: string]: { + getCollections: () => CollectionRegistry, + getSchemas: (collections: CollectionRegistry) => SchemaRegistry, + } +} = { + '1.16': java16, + '1.17': java17 +} + const LOCAL_STORAGE_THEME = 'theme' const LOCAL_STORAGE_LANGUAGE = 'language' +const LOCAL_STORAGE_VERSION = 'schema_version' const publicPath = '/'; @@ -112,6 +124,9 @@ const sourceControlsShare = document.getElementById('source-controls-share')! const sourceToggle = document.getElementById('source-toggle')! const treeControlsToggle = document.getElementById('tree-controls-toggle')! const treeControlsMenu = document.getElementById('tree-controls-menu')! +const treeVersionToggle = document.getElementById('tree-version-toggle')! +const treeVersionMenu = document.getElementById('tree-version-menu')! +const treeVersionLabel = document.getElementById('tree-version-label')! const treeControlsReset = document.getElementById('tree-controls-reset')! const treeControlsUndo = document.getElementById('tree-controls-undo')! const treeControlsRedo = document.getElementById('tree-controls-redo')! @@ -142,15 +157,18 @@ const views = { 'visualizer': new VisualizerView(dummyModel, visualizerContent) } -const COLLECTIONS = getCollections() +let version = localStorage.getItem(LOCAL_STORAGE_VERSION) ?? config.versions[0].id +treeVersionLabel.textContent = version + +let COLLECTIONS = versionSchemas[version].getCollections() Promise.all([ fetchLocale(getLanguage()), ...(getLanguage() === 'en' ? [] : [fetchLocale('en')]), - RegistryFetcher(COLLECTIONS, config.registries) -]).then(responses => { + RegistryFetcher(COLLECTIONS, version) +]).then(() => { - const SCHEMAS = getSchemas(COLLECTIONS) + let SCHEMAS = versionSchemas[version].getSchemas(COLLECTIONS) let models: { [key: string]: DataModel } = {} const buildModel = (model: any) => { @@ -207,6 +225,21 @@ Promise.all([ } } + const updateVersion = (id: string) => { + localStorage.setItem(LOCAL_STORAGE_VERSION, id) + if (id === version) return + + const newCollections = versionSchemas[id].getCollections() + RegistryFetcher(COLLECTIONS, id).then(() => { + SCHEMAS = versionSchemas[id].getSchemas(COLLECTIONS) + COLLECTIONS = newCollections + + treeVersionLabel.textContent = id + version = id + updateModel() + }) + } + homeLink.addEventListener('click', evt => { reload(publicPath) }) @@ -297,6 +330,21 @@ Promise.all([ }, { capture: true, once: true }) }) + treeVersionToggle.addEventListener('click', evt => { + treeVersionMenu.style.visibility = 'visible' + document.body.addEventListener('click', evt => { + treeVersionMenu.style.visibility = 'hidden' + }, { capture: true, once: true }) + }) + + config.versions.forEach(v => { + const entry = document.createElement('button') + entry.classList.add('btn') + entry.textContent = v.id + entry.addEventListener('click', () => updateVersion(v.id)) + treeVersionMenu.append(entry) + }) + treeControlsReset.addEventListener('click', evt => { models[selected].reset(models[selected].schema.default(), true) addChecked(treeControlsReset) diff --git a/src/app/hooks/renderHtml.ts b/src/app/hooks/renderHtml.ts index 6f99dc88..13a7abde 100644 --- a/src/app/hooks/renderHtml.ts +++ b/src/app/hooks/renderHtml.ts @@ -179,7 +179,6 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = { (el as HTMLSelectElement).value = value ?? '' el.addEventListener('change', evt => { const newValue = (el as HTMLSelectElement).value - console.log("UPDATING NEW VALUE!", path.toString(), newValue) path.model.set(path, newValue.length === 0 ? undefined : newValue) evt.stopPropagation() }) diff --git a/src/config.json b/src/config.json index e5f6fe6a..228220f5 100644 --- a/src/config.json +++ b/src/config.json @@ -41,6 +41,16 @@ "name": "正體中文" } ], + "versions": [ + { + "id": "1.17", + "mcdata_ref": "master" + }, + { + "id": "1.16", + "mcdata_ref": "1.16.4" + } + ], "models": [ { "id": "loot-table", @@ -148,6 +158,10 @@ "worldgen/structure_processor", "worldgen/surface_builder", "worldgen/tree_decorator_type", - "worldgen/trunk_placer_type" + "worldgen/trunk_placer_type", + { + "id": "worldgen/biome", + "path": "/processed/reports/biomes/data.min.json" + } ] } diff --git a/src/index.html b/src/index.html index fabe1102..3b24a3ef 100644 --- a/src/index.html +++ b/src/index.html @@ -54,6 +54,10 @@
+
+
diff --git a/src/locales/en.json b/src/locales/en.json index a08352b6..ea5279cb 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -7,11 +7,13 @@ "language": "Language", "loot-table": "Loot Table", "predicate": "Predicate", + "redo": "Redo", "reset": "Reset", "share": "Share", "title.generator": "%0% Generator", "title.home": "Data Pack Generators", "visualize": "Visualize", + "undo": "Undo", "world": "World Settings", "worldgen/biome": "Biome", "worldgen/carver": "Carver",