Fetch dynamic registries

This commit is contained in:
Misode
2020-12-27 03:50:16 +01:00
parent a386773c9f
commit a6b93f5118
8 changed files with 108 additions and 65 deletions

View File

@@ -20,10 +20,18 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get latest vanilla-datapack commit hash
uses: octokit/request-action@v2.x
id: get_vanilla_datapack_summary_hash
with:
route: GET /repos/SPGoding/vanilla-datapack/branches/summary
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Build
run: |
npm install
npm run build -- --env.hash='${{ fromJson(steps.get_mcdata_hash.outputs.data).commit.sha }}'
npm run build -- --env.mcdata_hash='${{ fromJson(steps.get_mcdata_hash.outputs.data).commit.sha }}' --env.vanilla_datapack_summary_hash='${{ fromJson(steps.get_vanilla_datapack_summary_hash.outputs.data).commit.sha }}'
- name: Deploy
uses: JamesIves/github-pages-deploy-action@releases/v3

View File

@@ -6,66 +6,62 @@ const CACHE_FORMAT = 1
type VersionConfig = {
id: string,
mcdata_ref: string
}
type RegistryConfig = {
id: string
minVersion?: string
maxVersion?: string
path?: string
mcdata_ref: string,
vanilla_datapack_summary_ref?: string
}
const localStorageCache = (version: string) => `cache_${version}`
declare var __MCDATA_MASTER_HASH__: string;
declare var __VANILLA_DATAPACK_SUMMARY_HASH__: string;
const baseUrl = 'https://raw.githubusercontent.com/Arcensoth/mcdata'
const mcdata = (ref: string, registry: string) => {
return `${baseUrl}/${ref}/processed/reports/registries/${registry}/data.min.json`
}
const mcdataUrl = 'https://raw.githubusercontent.com/Arcensoth/mcdata'
const vanillaDatapackUrl = 'https://raw.githubusercontent.com/SPGoding/vanilla-datapack'
export const fetchData = async (target: CollectionRegistry, versionId: string) => {
const version = config.versions.find(v => v.id === versionId)
if (!version) return
const cache = JSON.parse(localStorage.getItem(localStorageCache(versionId)) ?? '{}')
const cacheValid = cache.format === CACHE_FORMAT && (version.mcdata_ref !== 'master' || cache.mcdata_hash === __MCDATA_MASTER_HASH__)
const mcdataCacheValid = cache.format === CACHE_FORMAT && (version.mcdata_ref !== 'master' || cache.mcdata_hash === __MCDATA_MASTER_HASH__)
const vanillaDataCacheValid = cache.format === CACHE_FORMAT && (version.vanilla_datapack_summary_ref !== 'master' || cache.vanilla_data_summary_hash === __VANILLA_DATAPACK_SUMMARY_HASH__)
await Promise.all([
fetchRegistries(target, version, cache, cacheValid),
fetchBlockStateMap(version, cache, cacheValid)
fetchRegistries(target, version, cache, mcdataCacheValid),
fetchBlockStateMap(version, cache, mcdataCacheValid),
fetchDynamicRegistries(target, version, cache, vanillaDataCacheValid)
])
if (!cacheValid) {
if (!mcdataCacheValid || !vanillaDataCacheValid) {
if (version.mcdata_ref === 'master') {
cache.mcdata_hash = __MCDATA_MASTER_HASH__
}
if (version.mcdata_ref === 'master') {
cache.vanilla_data_summary_hash = __VANILLA_DATAPACK_SUMMARY_HASH__
}
cache.format = CACHE_FORMAT
localStorage.setItem(localStorageCache(versionId), JSON.stringify(cache))
}
}
const fetchRegistries = async (target: CollectionRegistry, version: VersionConfig, cache: any, cacheValid: boolean) => {
if (cacheValid && cache.registries) {
config.registries.forEach((r: string | RegistryConfig) => {
if (typeof r === 'string') r = { id: r }
if (!checkVersion(version.id, r.minVersion, r.maxVersion)) return
const registries = config.registries
.filter(r => !r.dynamic)
.filter(r => checkVersion(version.id, r.minVersion, r.maxVersion))
if (cacheValid && cache.registries) {
registries.forEach(r => {
target.register(r.id, cache.registries[r.id])
})
return
}
cache.registries = {}
if (checkVersion('1.15', version.id)) {
const url = `${baseUrl}/${version.mcdata_ref}/generated/reports/registries.json`
if (checkVersion(version.id, undefined, '1.15')) {
const url = `${mcdataUrl}/${version.mcdata_ref}/generated/reports/registries.json`
try {
const res = await fetch(url)
const data = await res.json()
config.registries.forEach(async (r: string | RegistryConfig) => {
if (typeof r === 'string') r = { id: r }
if (!checkVersion(version.id, r.minVersion, r.maxVersion)) return
registries.forEach(async r => {
const values = Object.keys(data[`minecraft:${r.id}`].entries)
target.register(r.id, values)
cache.registries[r.id] = values
@@ -74,13 +70,10 @@ const fetchRegistries = async (target: CollectionRegistry, version: VersionConfi
console.warn(`Error occurred while fetching registries for version ${version.id}`)
}
} else {
await Promise.all(config.registries.map(async (r: string | RegistryConfig) => {
if (typeof r === 'string') r = { id: r }
if (!checkVersion(version.id, r.minVersion, r.maxVersion)) return
await Promise.all(registries.map(async r => {
const url = r.path
? `${baseUrl}/${version.mcdata_ref}/${r.path}/data.min.json`
: mcdata(version.mcdata_ref, typeof r === 'string' ? r : r.id)
? `${mcdataUrl}/${version.mcdata_ref}/${r.path}/data.min.json`
: `${mcdataUrl}/${version.mcdata_ref}/processed/reports/registries/${r.id}/data.min.json`
try {
const res = await fetch(url)
@@ -105,8 +98,8 @@ const fetchBlockStateMap = async (version: VersionConfig, cache: any, cacheValid
cache.block_state_map = {}
const url = (checkVersion(version.id, undefined, '1.15'))
? `${baseUrl}/${version.mcdata_ref}/generated/reports/blocks.json`
: `${baseUrl}/${version.mcdata_ref}/processed/reports/blocks/data.min.json`
? `${mcdataUrl}/${version.mcdata_ref}/generated/reports/blocks.json`
: `${mcdataUrl}/${version.mcdata_ref}/processed/reports/blocks/data.min.json`
const res = await fetch(url)
const data = await res.json()
@@ -120,3 +113,26 @@ const fetchBlockStateMap = async (version: VersionConfig, cache: any, cacheValid
cache.block_state_map[block] = res
})
}
const fetchDynamicRegistries = async (target: CollectionRegistry, version: VersionConfig, cache: any, cacheValid: boolean) => {
const registries = config.registries
.filter(r => r.dynamic)
.filter(r => checkVersion(version.id, r.minVersion, r.maxVersion))
if (cacheValid) {
registries.forEach(r => {
target.register(r.id, cache.dynamic_registries[r.id])
})
return
}
cache.dynamic_registries = {}
if (checkVersion(version.id, '1.16')) {
const res = await fetch(`${vanillaDatapackUrl}/${version.vanilla_datapack_summary_ref}/summary/flattened.min.json`)
const data = await res.json()
registries.forEach(r => {
target.register(r.id, data[r.id])
cache.dynamic_registries[r.id] = data[r.id]
})
}
}

View File

@@ -8,7 +8,6 @@ export const Dropdown = (view: View, icon: keyof typeof Octicon, entries: [strin
state.set((el as HTMLSelectElement).value)
})
state.watchRun(v => (el as HTMLSelectElement).value = v, 'dropdown')
watcher?.(state.get())
})
return `
<div class="dropdown">

View File

@@ -10,7 +10,6 @@ export const Toggle = <T>(view: View, entries: [T, keyof typeof Octicon][], stat
state.set(entries[(i + 1) % entries.length][0])
})
state.watch(_ => el.innerHTML = activeOcticon(), 'toggle')
watcher?.(state.get())
})
return `<div class="toggle" data-id="${toggle}">${activeOcticon()}</div>`
}

View File

@@ -47,12 +47,6 @@ export const TreePanel = (view: View, model: DataModel) => {
})
return `<div class="panel tree-panel">
<div class="panel-controls">
<div class="btn" data-id="${view.onClick(() => {
Tracker.reset(); model.reset(model.schema.default())
})}">
${Octicon.history}
<span data-i18n="reset"></span>
</div>
<div class="panel-menu">
<div class="btn" data-id="${view.onClick(toggleMenu)}">
${Octicon.tag}
@@ -78,15 +72,17 @@ export const TreePanel = (view: View, model: DataModel) => {
${Octicon.kebab_horizontal}
</div>
<div class="panel-menu-list btn-group">
<div class="btn" data-id="${view.onClick(() => {
Tracker.reset(); model.reset(model.schema.default())
})}">
${Octicon.history}<span data-i18n="reset"></span>
</div>
<div class="btn" data-id="${view.onClick(() => {Tracker.undo(); model.undo()})}">
${Octicon.arrow_left}<span data-i18n="undo"></span>
</div>
<div class="btn" data-id="${view.onClick(() => {Tracker.redo(); model.redo()})}">
${Octicon.arrow_right}<span data-i18n="redo"></span>
</div>
<div class="btn">
${Octicon.gear}<a data-link href="/settings/fields/" data-i18n="fields"></a>
</div>
</div>
</div>
</div>

View File

@@ -111,14 +111,14 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
const key = keyPath.get()
path.model.set(path.push(key), children.default())
})
let suffix = ''
const blockState = (config.validation?.validator === 'block_state_map' ? BlockStateRegistry[relativePath(path, config.validation.params.id).get()] : null)
if (blockState && !blockState.properties) {
return ['', '', '']
if (!blockState || blockState.properties) {
const keyRendered = (blockState
? StringNode(null!, { enum: Object.keys(blockState.properties ?? {}) })
: keys).hook(this, keyPath, keyPath.get() ?? '', mounter)
suffix = keyRendered[1] + `<button class="add" data-id="${onAdd}">${Octicon.plus_circle}</button>`
}
const keyRendered = (blockState
? StringNode(null!, { enum: Object.keys(blockState.properties ?? {}) })
: keys).hook(this, keyPath, keyPath.get() ?? '', mounter)
const suffix = keyRendered[1] + `<button class="add" data-id="${onAdd}">${Octicon.plus_circle}</button>`
let body = ''
if (typeof value === 'object' && value !== undefined) {
body = Object.keys(value)
@@ -127,7 +127,7 @@ export const renderHtml: Hook<[any, Mounter], [string, string, string]> = {
const childPath = path.modelPush(key)
const category = children.category(childPath)
const [cPrefix, cSuffix, cBody] = (blockState
? StringNode(null!, { enum: blockState.properties[key] })
? StringNode(null!, blockState.properties && { enum: blockState.properties[key] })
: children).hook(this, childPath, value[key], mounter)
return `<div class="node-entry"><div class="node ${children.type(childPath)}-node" ${category ? `data-category="${htmlEncode(category)}"` : ''}>
<div class="node-header">

View File

@@ -48,11 +48,13 @@
},
{
"id": "1.16",
"mcdata_ref": "1.16.4"
"mcdata_ref": "1.16.4",
"vanilla_datapack_summary_ref": "1.16.4-summary"
},
{
"id": "1.17",
"mcdata_ref": "master"
"mcdata_ref": "master",
"vanilla_datapack_summary_ref": "summary"
}
],
"models": [
@@ -159,36 +161,58 @@
}
],
"registries": [
{ "id": "advancement", "dynamic": true },
{ "id": "attribute", "minVersion": "1.16" },
"block",
"enchantment",
"entity_type",
"fluid",
"item",
{ "id": "block" },
{ "id": "dimension", "dynamic": true },
{ "id": "dimension_type", "dynamic": true },
{ "id": "enchantment" },
{ "id": "entity_type" },
{ "id": "fluid" },
{ "id": "function", "dynamic": true },
{ "id": "item" },
{ "id": "loot_condition_type", "minVersion": "1.16" },
{ "id": "loot_function_type", "minVersion": "1.16" },
{ "id": "loot_nbt_provider_type", "minVersion": "1.17" },
{ "id": "loot_number_provider_type", "minVersion": "1.17" },
{ "id": "loot_pool_entry_type", "minVersion": "1.16" },
{ "id": "loot_score_provider_type", "minVersion": "1.17" },
"mob_effect",
{ "id": "rule_test", "minVersion": "1.16" },
{ "id": "loot_table", "dynamic": true },
{ "id": "mob_effect" },
{ "id": "pos_rule_test", "minVersion": "1.16" },
"sound_event",
"stat_type",
{ "id": "predicate", "dynamic": true },
{ "id": "recipe", "dynamic": true },
{ "id": "rule_test", "minVersion": "1.16" },
{ "id": "sound_event" },
{ "id": "stat_type" },
{ "id": "structure", "dynamic": true },
{ "id": "tag/block", "dynamic": true },
{ "id": "tag/entity_type", "dynamic": true },
{ "id": "tag/fluid", "dynamic": true },
{ "id": "tag/function", "dynamic": true },
{ "id": "tag/item", "dynamic": true },
{ "id": "worldgen/biome", "dynamic": true },
{ "id": "worldgen/block_state_provider_type", "minVersion": "1.16" },
{ "id": "worldgen/block_placer_type", "minVersion": "1.16" },
{ "id": "worldgen/biome_source", "minVersion": "1.16" },
{ "id": "worldgen/carver", "minVersion": "1.16" },
{ "id": "worldgen/chunk_generator", "minVersion": "1.16" },
{ "id": "worldgen/configured_carver", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/configured_decorator", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/configured_feature", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/configured_structure_feature", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/configured_surface_builder", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/decorator", "minVersion": "1.16" },
{ "id": "worldgen/feature", "minVersion": "1.16" },
{ "id": "worldgen/feature_size_type", "minVersion": "1.16" },
{ "id": "worldgen/foliage_placer_type", "minVersion": "1.16" },
{ "id": "worldgen/noise_settings", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/processor_list", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/structure_feature", "minVersion": "1.16" },
{ "id": "worldgen/structure_pool_element", "minVersion": "1.16" },
{ "id": "worldgen/structure_processor", "minVersion": "1.16" },
{ "id": "worldgen/surface_builder", "minVersion": "1.16" },
{ "id": "worldgen/template_pool", "minVersion": "1.16" , "dynamic": true },
{ "id": "worldgen/tree_decorator_type", "minVersion": "1.16" },
{ "id": "worldgen/trunk_placer_type", "minVersion": "1.16" },
{ "id": "biome", "maxVersion": "1.15" },

View File

@@ -20,7 +20,8 @@ module.exports = (env, argv) => ({
},
plugins: [
new webpack.DefinePlugin({
__MCDATA_MASTER_HASH__: JSON.stringify(env ? env.hash : '')
__MCDATA_MASTER_HASH__: JSON.stringify(env ? env.mcdata_hash : ''),
__VANILLA_DATAPACK_SUMMARY_HASH__: JSON.stringify(env ? env.vanilla_datapack_summary_hash : '')
}),
new CopyWebpackPlugin({
patterns: [