Skip to content

Commit

Permalink
Get project data for worldgen previews
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Nov 27, 2024
1 parent 2bc0fc2 commit 1abc28f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/app/components/previews/BiomeSourcePreview.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 },
Expand Down
5 changes: 3 additions & 2 deletions src/app/components/previews/DensityFunctionPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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])
Expand Down
5 changes: 3 additions & 2 deletions src/app/components/previews/NoiseSettingsPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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()
Expand Down
24 changes: 23 additions & 1 deletion src/app/contexts/Project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

0 comments on commit 1abc28f

Please sign in to comment.