@@ -2,13 +2,14 @@ import { Identifier } from 'deepslate'
22import type { ComponentChildren } from 'preact'
33import { createContext } from 'preact'
44import { useCallback , useContext , useState } from 'preact/hooks'
5+ import type { ProjectData } from '../components/previews/Deepslate.js'
56import config from '../Config.js'
67import { useAsync } from '../hooks/useAsync.js'
78import type { VersionId } from '../services/index.js'
89import { DEFAULT_VERSION } from '../services/index.js'
910import { DRAFTS_URI , PROJECTS_URI , SpyglassClient } from '../services/Spyglass.js'
1011import { Store } from '../Store.js'
11- import { genPath , hexId , message } from '../Utils.js'
12+ import { genPath , hexId , message , safeJsonParse } from '../Utils.js'
1213
1314export type ProjectMeta = {
1415 name : string ,
@@ -167,3 +168,24 @@ export function getProjectRoot(project: ProjectMeta) {
167168 }
168169 throw new Error ( `Unsupported project storage ${ project . storage ?. type } ` )
169170}
171+
172+ export async function getWorldgenProjectData ( project : ProjectMeta ) : Promise < ProjectData > {
173+ const projectRoot = getProjectRoot ( project )
174+ const categories = [ 'worldgen/noise_settings' , 'worldgen/noise' , 'worldgen/density_function' ]
175+ const result : ProjectData = Object . fromEntries ( categories . map ( c => [ c , { } ] ) )
176+ const entries = await SpyglassClient . FS . readdir ( projectRoot )
177+ for ( const entry of entries ) {
178+ for ( const category of categories ) {
179+ if ( entry . name . includes ( category ) ) {
180+ const pattern = RegExp ( `data/([a-z0-9_.-]+)/${ category } /([a-z0-9_./-]+).json$` )
181+ const match = entry . name . match ( pattern )
182+ if ( match ) {
183+ const data = await SpyglassClient . FS . readFile ( entry . name )
184+ const text = new TextDecoder ( ) . decode ( data )
185+ result [ category ] [ `${ match [ 1 ] } :${ match [ 2 ] } ` ] = safeJsonParse ( text )
186+ }
187+ }
188+ }
189+ }
190+ return result
191+ }
0 commit comments