mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-26 08:26:51 +00:00
Switch to vite and preact
This commit is contained in:
93
src/app/Schemas.ts
Normal file
93
src/app/Schemas.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import type { CollectionRegistry, SchemaRegistry } from '@mcschema/core'
|
||||
import { DataModel } from '@mcschema/core'
|
||||
import * as java15 from '@mcschema/java-1.15'
|
||||
import * as java16 from '@mcschema/java-1.16'
|
||||
import * as java17 from '@mcschema/java-1.17'
|
||||
import config from '../config.json'
|
||||
import { fetchData } from './DataFetcher'
|
||||
|
||||
export const VersionIds = ['1.15', '1.16', '1.17'] as const
|
||||
export type VersionId = typeof VersionIds[number]
|
||||
|
||||
export type BlockStateRegistry = {
|
||||
[block: string]: {
|
||||
properties: {
|
||||
[key: string]: string[],
|
||||
},
|
||||
default: {
|
||||
[key: string]: string,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
type VersionData = {
|
||||
collections: CollectionRegistry,
|
||||
schemas: SchemaRegistry,
|
||||
blockStates: BlockStateRegistry,
|
||||
}
|
||||
const Versions: Record<string, VersionData> = {}
|
||||
|
||||
type ModelData = {
|
||||
model: DataModel,
|
||||
version: VersionId,
|
||||
}
|
||||
const Models: Record<string, ModelData> = {}
|
||||
|
||||
const versionGetter: {
|
||||
[versionId in VersionId]: {
|
||||
getCollections: () => CollectionRegistry,
|
||||
getSchemas: (collections: CollectionRegistry) => SchemaRegistry,
|
||||
}
|
||||
} = {
|
||||
1.15: java15,
|
||||
1.16: java16,
|
||||
1.17: java17,
|
||||
}
|
||||
|
||||
async function getVersion(id: VersionId): Promise<VersionData> {
|
||||
if (!Versions[id]) {
|
||||
const collections = versionGetter[id].getCollections()
|
||||
const blockStates: BlockStateRegistry = {}
|
||||
await fetchData(id, collections, blockStates)
|
||||
const schemas = versionGetter[id].getSchemas(collections)
|
||||
Versions[id] = { collections, schemas, blockStates }
|
||||
}
|
||||
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)
|
||||
const schemaName = config.models.find(m => m.id === id)?.schema
|
||||
if (!schemaName) {
|
||||
throw new Error(`Cannot find model ${id}`)
|
||||
}
|
||||
const schema = versionData.schemas.get(schemaName)
|
||||
const model = new DataModel(schema)
|
||||
if (Models[id]) {
|
||||
model.reset(Models[id].model.data, false)
|
||||
} else {
|
||||
model.validate(true)
|
||||
model.history = [JSON.stringify(model.data)]
|
||||
}
|
||||
Models[id] = { model, version }
|
||||
}
|
||||
return Models[id].model
|
||||
}
|
||||
|
||||
export async function getCollections(version: VersionId): Promise<CollectionRegistry> {
|
||||
const versionData = await getVersion(version)
|
||||
return versionData.collections
|
||||
}
|
||||
|
||||
export async function getBlockStates(version: VersionId): Promise<BlockStateRegistry> {
|
||||
const versionData = await getVersion(version)
|
||||
return versionData.blockStates
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user