mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 23:27:09 +00:00
🔥 Nuke all mcschema related code
This commit is contained in:
@@ -1,20 +1,13 @@
|
||||
import type { CollectionRegistry } from '@mcschema/core'
|
||||
import config from '../Config.js'
|
||||
import { Store } from '../Store.js'
|
||||
import { message } from '../Utils.js'
|
||||
import type { BlockStateRegistry, VersionId } from './Schemas.js'
|
||||
import type { VersionId } from './Schemas.js'
|
||||
import { checkVersion } from './Schemas.js'
|
||||
|
||||
const CACHE_NAME = 'misode-v2'
|
||||
const CACHE_LATEST_VERSION = 'cached_latest_version'
|
||||
const CACHE_PATCH = 'misode_cache_patch'
|
||||
|
||||
type Version = {
|
||||
id: string,
|
||||
ref?: string,
|
||||
dynamic?: boolean,
|
||||
}
|
||||
|
||||
declare var __LATEST_VERSION__: string
|
||||
export const latestVersion = __LATEST_VERSION__ ?? ''
|
||||
const mcmetaUrl = 'https://raw.githubusercontent.com/misode/mcmeta'
|
||||
@@ -46,51 +39,10 @@ async function validateCache(version: RefInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchData(versionId: string, collectionTarget: CollectionRegistry, blockStateTarget: BlockStateRegistry) {
|
||||
const version = config.versions.find(v => v.id === versionId) as Version | undefined
|
||||
if (!version) {
|
||||
console.error(`[fetchData] Unknown version ${version} in ${JSON.stringify(config.versions)}`)
|
||||
return
|
||||
}
|
||||
|
||||
await validateCache(version)
|
||||
|
||||
await Promise.all([
|
||||
_fetchRegistries(version, collectionTarget),
|
||||
_fetchBlockStateMap(version, blockStateTarget),
|
||||
])
|
||||
}
|
||||
|
||||
async function _fetchRegistries(version: Version, target: CollectionRegistry) {
|
||||
console.debug(`[fetchRegistries] ${version.id}`)
|
||||
try {
|
||||
const data = await cachedFetch<any>(`${mcmeta(version, 'summary')}/registries/data.min.json`)
|
||||
for (const id in data) {
|
||||
target.register(id, data[id].map((e: string) => 'minecraft:' + e))
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error occurred while fetching registries:', message(e))
|
||||
}
|
||||
}
|
||||
|
||||
async function _fetchBlockStateMap(version: Version, target: BlockStateRegistry) {
|
||||
console.debug(`[fetchBlockStateMap] ${version.id}`)
|
||||
try {
|
||||
const data = await cachedFetch<any>(`${mcmeta(version, 'summary')}/blocks/data.min.json`)
|
||||
for (const id in data) {
|
||||
target['minecraft:' + id] = {
|
||||
properties: data[id][0],
|
||||
default: data[id][1],
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Error occurred while fetching block state map:', message(e))
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchRegistries(versionId: VersionId) {
|
||||
console.debug(`[fetchRegistries] ${versionId}`)
|
||||
const version = config.versions.find(v => v.id === versionId)!
|
||||
await validateCache(version)
|
||||
try {
|
||||
const data = await cachedFetch<any>(`${mcmeta(version, 'summary')}/registries/data.min.json`)
|
||||
const result = new Map<string, string[]>()
|
||||
@@ -103,10 +55,16 @@ export async function fetchRegistries(versionId: VersionId) {
|
||||
}
|
||||
}
|
||||
|
||||
export interface BlockStateData {
|
||||
properties: Record<string, string[]>
|
||||
default: Record<string, string>
|
||||
}
|
||||
|
||||
export async function fetchBlockStates(versionId: VersionId) {
|
||||
console.debug(`[fetchBlockStates] ${versionId}`)
|
||||
const version = config.versions.find(v => v.id === versionId)!
|
||||
const result = new Map<string, {properties: Record<string, string[]>, default: Record<string, string>}>()
|
||||
const result = new Map<string, BlockStateData>()
|
||||
await validateCache(version)
|
||||
try {
|
||||
const data = await cachedFetch<any>(`${mcmeta(version, 'summary')}/blocks/data.min.json`)
|
||||
for (const id in data) {
|
||||
@@ -128,6 +86,7 @@ export async function fetchItemComponents(versionId: VersionId) {
|
||||
if (!checkVersion(versionId, '1.20.5')) {
|
||||
return result
|
||||
}
|
||||
await validateCache(version)
|
||||
try {
|
||||
const data = await cachedFetch<Record<string, Record<string, unknown>>>(`${mcmeta(version, 'summary')}/item_components/data.min.json`)
|
||||
for (const [id, components] of Object.entries(data)) {
|
||||
@@ -152,6 +111,7 @@ export async function fetchItemComponents(versionId: VersionId) {
|
||||
export async function fetchPreset(versionId: VersionId, registry: string, id: string) {
|
||||
console.debug(`[fetchPreset] ${versionId} ${registry} ${id}`)
|
||||
const version = config.versions.find(v => v.id === versionId)!
|
||||
await validateCache(version)
|
||||
try {
|
||||
let url
|
||||
if (id.startsWith('immersive_weathering:')) {
|
||||
|
||||
@@ -1,77 +1,27 @@
|
||||
import type { CollectionRegistry, INode, SchemaRegistry } from '@mcschema/core'
|
||||
import { ChoiceNode, DataModel, Reference, StringNode } from '@mcschema/core'
|
||||
import config from '../Config.js'
|
||||
import { initPartners } from '../partners/index.js'
|
||||
import { message } from '../Utils.js'
|
||||
import { fetchData } from './DataFetcher.js'
|
||||
import type { BlockStateData } from './DataFetcher.js'
|
||||
import { fetchBlockStates, fetchRegistries } from './DataFetcher.js'
|
||||
|
||||
export const VersionIds = ['1.15', '1.16', '1.17', '1.18', '1.18.2', '1.19', '1.19.3', '1.19.4', '1.20', '1.20.2', '1.20.3', '1.20.5', '1.21', '1.21.2'] as const
|
||||
export type VersionId = typeof VersionIds[number]
|
||||
|
||||
export const DEFAULT_VERSION: VersionId = '1.21'
|
||||
|
||||
export type BlockStateRegistry = {
|
||||
[block: string]: {
|
||||
properties?: {
|
||||
[key: string]: string[],
|
||||
},
|
||||
default?: {
|
||||
[key: string]: string,
|
||||
},
|
||||
},
|
||||
interface VersionData {
|
||||
registries: Map<string, string[]>
|
||||
blockStates: Map<string, BlockStateData>
|
||||
}
|
||||
|
||||
type VersionData = {
|
||||
collections: CollectionRegistry,
|
||||
schemas: SchemaRegistry,
|
||||
blockStates: BlockStateRegistry,
|
||||
}
|
||||
const Versions: Record<string, VersionData | Promise<VersionData>> = {}
|
||||
|
||||
type ModelData = {
|
||||
model: DataModel,
|
||||
version: VersionId,
|
||||
}
|
||||
const Models: Record<string, ModelData> = {}
|
||||
|
||||
const versionGetter: {
|
||||
[versionId in VersionId]: () => Promise<{
|
||||
getCollections: () => CollectionRegistry,
|
||||
getSchemas: (collections: CollectionRegistry) => SchemaRegistry,
|
||||
}>
|
||||
} = {
|
||||
1.15: () => import('@mcschema/java-1.15'),
|
||||
1.16: () => import('@mcschema/java-1.16'),
|
||||
1.17: () => import('@mcschema/java-1.17'),
|
||||
1.18: () => import('@mcschema/java-1.18'),
|
||||
'1.18.2': () => import('@mcschema/java-1.18.2'),
|
||||
1.19: () => import('@mcschema/java-1.19'),
|
||||
'1.19.3': () => import('@mcschema/java-1.19.3'),
|
||||
'1.19.4': () => import('@mcschema/java-1.19.4'),
|
||||
'1.20': () => import('@mcschema/java-1.20'),
|
||||
'1.20.2': () => import('@mcschema/java-1.20.2'),
|
||||
'1.20.3': () => import('@mcschema/java-1.20.3'),
|
||||
'1.20.5': () => import('@mcschema/java-1.20.5'),
|
||||
1.21: () => import('@mcschema/java-1.21'),
|
||||
'1.21.2': () => import('@mcschema/java-1.21.2'),
|
||||
}
|
||||
|
||||
export let CachedDecorator: INode<any>
|
||||
export let CachedFeature: INode<any>
|
||||
export let CachedCollections: CollectionRegistry
|
||||
export let CachedSchemas: SchemaRegistry
|
||||
|
||||
async function getVersion(id: VersionId): Promise<VersionData> {
|
||||
if (!Versions[id]) {
|
||||
Versions[id] = (async () => {
|
||||
try {
|
||||
const mcschema = await versionGetter[id]()
|
||||
const collections = mcschema.getCollections()
|
||||
const blockStates: BlockStateRegistry = {}
|
||||
await fetchData(id, collections, blockStates)
|
||||
const schemas = mcschema.getSchemas(collections)
|
||||
initPartners(schemas, collections, id)
|
||||
Versions[id] = { collections, schemas, blockStates }
|
||||
const registries = await fetchRegistries(id)
|
||||
const blockStates= await fetchBlockStates(id)
|
||||
Versions[id] = { registries, blockStates }
|
||||
return Versions[id]
|
||||
} catch (e) {
|
||||
throw new Error(`Cannot get version "${id}": ${message(e)}`)
|
||||
@@ -82,65 +32,26 @@ async function getVersion(id: VersionId): Promise<VersionData> {
|
||||
return Versions[id]
|
||||
}
|
||||
|
||||
export async function getModel(version: VersionId, id: string): Promise<DataModel> {
|
||||
if (!Models[id] || Models[id].version !== version) {
|
||||
const versionData = await getVersion(version)
|
||||
|
||||
CachedDecorator = Reference(versionData.schemas, 'configured_decorator')
|
||||
CachedFeature = ChoiceNode([
|
||||
{
|
||||
type: 'string',
|
||||
node: StringNode(versionData.collections, { validator: 'resource', params: { pool: '$worldgen/configured_feature' } }),
|
||||
},
|
||||
{
|
||||
type: 'object',
|
||||
node: Reference(versionData.schemas, 'configured_feature'),
|
||||
},
|
||||
], { choiceContext: 'feature' })
|
||||
|
||||
const schemaName = config.generators.find(g => g.id === id)?.schema
|
||||
if (!schemaName) {
|
||||
throw new Error(`Cannot find model ${id}`)
|
||||
}
|
||||
try {
|
||||
const schema = versionData.schemas.get(schemaName)
|
||||
const model = new DataModel(schema, { wrapLists: true })
|
||||
if (Models[id]) {
|
||||
model.reset(Models[id].model.data, false)
|
||||
} else {
|
||||
model.validate(true)
|
||||
model.history = [JSON.stringify(model.data)]
|
||||
}
|
||||
Models[id] = { model, version }
|
||||
} catch (e) {
|
||||
const err = new Error(`Cannot get generator "${id}" for version "${version}": ${message(e)}`)
|
||||
if (e instanceof Error) err.stack = e.stack
|
||||
throw err
|
||||
}
|
||||
}
|
||||
return Models[id].model
|
||||
}
|
||||
|
||||
export async function getCollections(version: VersionId): Promise<CollectionRegistry> {
|
||||
const versionData = await getVersion(version)
|
||||
CachedCollections = versionData.collections
|
||||
return versionData.collections
|
||||
}
|
||||
|
||||
export async function getBlockStates(version: VersionId): Promise<BlockStateRegistry> {
|
||||
export async function getBlockStates(version: VersionId): Promise<Map<string, BlockStateData>> {
|
||||
const versionData = await getVersion(version)
|
||||
return versionData.blockStates
|
||||
}
|
||||
|
||||
export async function getSchemas(version: VersionId): Promise<SchemaRegistry> {
|
||||
const versionData = await getVersion(version)
|
||||
CachedSchemas = versionData.schemas
|
||||
return versionData.schemas
|
||||
}
|
||||
|
||||
export function checkVersion(versionId: string, minVersionId: string | undefined, maxVersionId?: string) {
|
||||
const version = config.versions.findIndex(v => v.id === versionId)
|
||||
const minVersion = minVersionId ? config.versions.findIndex(v => v.id === minVersionId) : 0
|
||||
const maxVersion = maxVersionId ? config.versions.findIndex(v => v.id === maxVersionId) : config.versions.length - 1
|
||||
return minVersion <= version && version <= maxVersion
|
||||
}
|
||||
|
||||
export interface FileModel {
|
||||
get text(): string
|
||||
get data(): any
|
||||
}
|
||||
|
||||
export function createMockFileModel(): FileModel {
|
||||
return {
|
||||
text: '{}',
|
||||
data: {},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user