mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
Get project data for worldgen previews
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { clampedMap } from 'deepslate'
|
||||
import { mat3 } from 'gl-matrix'
|
||||
import { useCallback, useRef, useState } from 'preact/hooks'
|
||||
import { useLocale, useProject, useStore, useVersion } from '../../contexts/index.js'
|
||||
import { getWorldgenProjectData, useLocale, useProject, useStore, useVersion } from '../../contexts/index.js'
|
||||
import { useAsync } from '../../hooks/index.js'
|
||||
import { checkVersion } from '../../services/Versions.js'
|
||||
import { Store } from '../../Store.js'
|
||||
@@ -37,7 +37,8 @@ export const BiomeSourcePreview = ({ docAndNode, shown }: PreviewProps) => {
|
||||
const hasRandomness = type === 'multi_noise' || type === 'the_end'
|
||||
|
||||
const { value } = useAsync(async function loadBiomeSource() {
|
||||
await DEEPSLATE.loadVersion(version, {}) // TODO: get project data
|
||||
const projectData = await getWorldgenProjectData(project)
|
||||
await DEEPSLATE.loadVersion(version, projectData)
|
||||
await DEEPSLATE.loadChunkGenerator(data?.generator?.settings, data?.generator?.biome_source, seed)
|
||||
return {
|
||||
biomeSource: { loaded: true },
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Voxel } from 'deepslate/render'
|
||||
import { clampedMap, VoxelRenderer } from 'deepslate/render'
|
||||
import type { mat3, mat4 } from 'gl-matrix'
|
||||
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
|
||||
import { useLocale, useProject, useVersion } from '../../contexts/index.js'
|
||||
import { getWorldgenProjectData, useLocale, useProject, useVersion } from '../../contexts/index.js'
|
||||
import { useAsync } from '../../hooks/useAsync.js'
|
||||
import { useLocalStorage } from '../../hooks/useLocalStorage.js'
|
||||
import { Store } from '../../Store.js'
|
||||
@@ -32,7 +32,8 @@ export const DensityFunctionPreview = ({ docAndNode, shown }: PreviewProps) => {
|
||||
const text = docAndNode.doc.getText()
|
||||
|
||||
const { value: df } = useAsync(async () => {
|
||||
await DEEPSLATE.loadVersion(version, {}) // TODO: get project data
|
||||
const projectData = await getWorldgenProjectData(project)
|
||||
await DEEPSLATE.loadVersion(version, projectData)
|
||||
const df = DEEPSLATE.loadDensityFunction(safeJsonParse(text) ?? {}, minY, height, seed)
|
||||
return df
|
||||
}, [version, project, minY, height, seed, text])
|
||||
|
||||
@@ -2,7 +2,7 @@ import { clampedMap } from 'deepslate'
|
||||
import type { mat3 } from 'gl-matrix'
|
||||
import { vec2 } from 'gl-matrix'
|
||||
import { useCallback, useRef, useState } from 'preact/hooks'
|
||||
import { useLocale, useProject, useVersion } from '../../contexts/index.js'
|
||||
import { getWorldgenProjectData, useLocale, useProject, useVersion } from '../../contexts/index.js'
|
||||
import { useAsync } from '../../hooks/index.js'
|
||||
import { fetchRegistries } from '../../services/index.js'
|
||||
import { Store } from '../../Store.js'
|
||||
@@ -27,7 +27,8 @@ export const NoiseSettingsPreview = ({ docAndNode, shown }: PreviewProps) => {
|
||||
|
||||
const { value, error } = useAsync(async () => {
|
||||
const data = safeJsonParse(text) ?? {}
|
||||
await DEEPSLATE.loadVersion(version, {}) // TODO: get project data
|
||||
const projectData = await getWorldgenProjectData(project)
|
||||
await DEEPSLATE.loadVersion(version, projectData)
|
||||
const biomeSource = { type: 'fixed', biome }
|
||||
await DEEPSLATE.loadChunkGenerator(data, biomeSource, seed)
|
||||
const noiseSettings = DEEPSLATE.getNoiseSettings()
|
||||
|
||||
@@ -2,13 +2,14 @@ import { Identifier } from 'deepslate'
|
||||
import type { ComponentChildren } from 'preact'
|
||||
import { createContext } from 'preact'
|
||||
import { useCallback, useContext, useState } from 'preact/hooks'
|
||||
import type { ProjectData } from '../components/previews/Deepslate.js'
|
||||
import config from '../Config.js'
|
||||
import { useAsync } from '../hooks/useAsync.js'
|
||||
import type { VersionId } from '../services/index.js'
|
||||
import { DEFAULT_VERSION } from '../services/index.js'
|
||||
import { DRAFTS_URI, PROJECTS_URI, SpyglassClient } from '../services/Spyglass.js'
|
||||
import { Store } from '../Store.js'
|
||||
import { genPath, hexId, message } from '../Utils.js'
|
||||
import { genPath, hexId, message, safeJsonParse } from '../Utils.js'
|
||||
|
||||
export type ProjectMeta = {
|
||||
name: string,
|
||||
@@ -167,3 +168,24 @@ export function getProjectRoot(project: ProjectMeta) {
|
||||
}
|
||||
throw new Error(`Unsupported project storage ${project.storage?.type}`)
|
||||
}
|
||||
|
||||
export async function getWorldgenProjectData(project: ProjectMeta): Promise<ProjectData> {
|
||||
const projectRoot = getProjectRoot(project)
|
||||
const categories = ['worldgen/noise_settings', 'worldgen/noise', 'worldgen/density_function']
|
||||
const result: ProjectData = Object.fromEntries(categories.map(c => [c, {}]))
|
||||
const entries = await SpyglassClient.FS.readdir(projectRoot)
|
||||
for (const entry of entries) {
|
||||
for (const category of categories) {
|
||||
if (entry.name.includes(category)) {
|
||||
const pattern = RegExp(`data/([a-z0-9_.-]+)/${category}/([a-z0-9_./-]+).json$`)
|
||||
const match = entry.name.match(pattern)
|
||||
if (match) {
|
||||
const data = await SpyglassClient.FS.readFile(entry.name)
|
||||
const text = new TextDecoder().decode(data)
|
||||
result[category][`${match[1]}:${match[2]}`] = safeJsonParse(text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user