Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import path from "path"; import { ActorPrefabResource, ActorResource, AvatarResource, CompressedBackgroundResource, CompressedProjectResources, CompressedSceneResource, CompressedSceneResourceWithChildren, EmoteResource, EngineFieldValuesResource, FontResource, MinimalResource, MusicResource, PaletteResource, ProjectMetadataResource, ScriptResource, SettingsResource, SoundResource, SpriteResource, TilesetResource, TriggerPrefabResource, TriggerResource, VariablesResource, } from "shared/lib/resources/types"; import glob from "glob"; import { promisify } from "util"; import promiseLimit from "lib/helpers/promiseLimit"; import groupBy from "lodash/groupBy"; import { identity } from "lodash"; import { defaultProjectSettings } from "consts"; import { readJson } from "lib/helpers/fs/readJson"; import { Value } from "@sinclair/typebox/value"; import { TSchema } from "@sinclair/typebox/build/cjs/type/schema"; import { Static } from "@sinclair/typebox"; import { naturalSortPaths, pathToPosix } from "shared/lib/helpers/path"; const globAsync = promisify(glob); const CONCURRENT_RESOURCE_LOAD_COUNT = 8; const sortByIndex = ( a: { data: { _index: number } }, b: { data: { _index: number } } ) => { Iif (a.data._index < b.data._index) { return -1; } Iif (a.data._index > b.data._index) { return 1; } return 0; }; interface RawResource { path: string; data: unknown; } type ResourceWithPath<T> = { path: string; data: T; }; interface ResourceLookup { actors: ResourceWithPath<ActorResource>[]; triggers: ResourceWithPath<TriggerResource>[]; scenes: ResourceWithPath<CompressedSceneResource>[]; actorPrefabs: ResourceWithPath<ActorPrefabResource>[]; triggerPrefabs: ResourceWithPath<TriggerPrefabResource>[]; scripts: ResourceWithPath<ScriptResource>[]; backgrounds: ResourceWithPath<CompressedBackgroundResource>[]; sprites: ResourceWithPath<SpriteResource>[]; palettes: ResourceWithPath<PaletteResource>[]; music: ResourceWithPath<MusicResource>[]; emotes: ResourceWithPath<EmoteResource>[]; avatars: ResourceWithPath<AvatarResource>[]; tilesets: ResourceWithPath<TilesetResource>[]; fonts: ResourceWithPath<FontResource>[]; sounds: ResourceWithPath<SoundResource>[]; variables: ResourceWithPath<VariablesResource>[]; engineFieldValues: ResourceWithPath<EngineFieldValuesResource>[]; settings: ResourceWithPath<Partial<SettingsResource>>[]; } export const loadProjectResources = async ( projectRoot: string, metadataResource: ProjectMetadataResource ): Promise<CompressedProjectResources> => { const projectResources = naturalSortPaths( await globAsync( path.join(projectRoot, "{project,assets,plugins}", "**/*.gbsres") ) ); const resources = ( await promiseLimit( CONCURRENT_RESOURCE_LOAD_COUNT, projectResources.map((projectResourcePath) => async () => { try { const resourceData = await readJson(projectResourcePath); return { path: pathToPosix(path.relative(projectRoot, projectResourcePath)), data: resourceData, }; } catch (e) { console.error("Failed to load resource: " + projectResourcePath); return undefined; } }) ) ).filter(identity) as RawResource[]; const resourcesLookup: ResourceLookup = { actors: [], triggers: [], scenes: [], actorPrefabs: [], triggerPrefabs: [], scripts: [], backgrounds: [], sprites: [], palettes: [], music: [], emotes: [], avatars: [], tilesets: [], fonts: [], sounds: [], variables: [], engineFieldValues: [], settings: [], }; const cast = <T extends TSchema, D extends MinimalResource>( schema: T, arr: ResourceWithPath<Static<T>>[] ) => ({ path, data }: { path: string; data: D }): boolean => { Iif (data._resourceType === schema?.properties?._resourceType?.const) { const castData = Value.Cast(schema, data); arr.push({ path, data: castData, }); return true; } return false; }; const partial = <T extends TSchema, D extends MinimalResource>( schema: T, arr: ResourceWithPath<Partial<Static<T>>>[] ) => ({ path, data }: { path: string; data: D }): boolean => { Iif (data._resourceType === schema?.properties?._resourceType?.const) { // Filter out any invalid keys, returning a valid partial resource const keys = Object.keys(data) as (keyof D)[]; const newResource: Partial<Static<T>> = {}; for (const key of keys) { const keySchema = schema?.properties?.[key] as TSchema; const keyData = data[key] as Static<T>[keyof Static<T>]; Iif (keySchema && Value.Check(keySchema, keyData)) { newResource[key as keyof Static<T>] = keyData; } } arr.push({ path, data: newResource, }); return true; } return false; }; const castFns = [ cast(ActorResource, resourcesLookup.actors), cast(TriggerResource, resourcesLookup.triggers), cast(CompressedSceneResource, resourcesLookup.scenes), cast(ActorPrefabResource, resourcesLookup.actorPrefabs), cast(TriggerPrefabResource, resourcesLookup.triggerPrefabs), cast(ScriptResource, resourcesLookup.scripts), cast(CompressedBackgroundResource, resourcesLookup.backgrounds), cast(SpriteResource, resourcesLookup.sprites), cast(PaletteResource, resourcesLookup.palettes), cast(MusicResource, resourcesLookup.music), cast(EmoteResource, resourcesLookup.emotes), cast(AvatarResource, resourcesLookup.avatars), cast(TilesetResource, resourcesLookup.tilesets), cast(FontResource, resourcesLookup.fonts), cast(SoundResource, resourcesLookup.sounds), cast(VariablesResource, resourcesLookup.variables), cast(EngineFieldValuesResource, resourcesLookup.engineFieldValues), partial(SettingsResource, resourcesLookup.settings), ] as const; for (const resource of resources) { Iif (!Value.Check(MinimalResource, resource.data)) { // Invalid resource - skipping continue; } for (const castFunction of castFns) { Iif (castFunction(resource as ResourceWithPath<MinimalResource>)) { break; } } } resourcesLookup.scenes.sort(sortByIndex); resourcesLookup.actors.sort(sortByIndex); resourcesLookup.triggers.sort(sortByIndex); const actorSubFolder = `${path.posix.sep}actors${path.posix.sep}`; const actorsBySceneFolderLookup = groupBy(resourcesLookup.actors, (row) => { const actorFolderIndex = row.path.lastIndexOf(actorSubFolder); return row.path.substring(0, actorFolderIndex); }); const triggerSubFolder = `${path.posix.sep}triggers${path.posix.sep}`; const triggersBySceneFolderLookup = groupBy( resourcesLookup.triggers, (row) => { const triggerFolderIndex = row.path.lastIndexOf(triggerSubFolder); return row.path.substring(0, triggerFolderIndex); } ); const extractData = <T>(value: { data: T }): T => value.data; const extractDataArray = <T>(arr: { data: T }[] | undefined): T[] => arr?.map(extractData) ?? []; const sceneResources: CompressedSceneResourceWithChildren[] = resourcesLookup.scenes.map((row) => { const sceneDir = path.posix.dirname(row.path); return { ...row.data, actors: (actorsBySceneFolderLookup[sceneDir] ?? []).map(extractData), triggers: (triggersBySceneFolderLookup[sceneDir] ?? []).map( extractData ), }; }); const variablesResource: VariablesResource = resourcesLookup.variables[0]?.data ?? ({ _resourceType: "variables", variables: [], constants: [], } as VariablesResource); const engineFieldValuesResource: EngineFieldValuesResource = (resourcesLookup.engineFieldValues ?? [])[0]?.data ?? { _resourceType: "engineFieldValues", engineFieldValues: [], }; const settingsResource: SettingsResource = ( resourcesLookup.settings ?? [] ).reduce( (memo, resource) => { return { ...memo, ...resource.data, }; }, { _resourceType: "settings", ...defaultProjectSettings } ); return { scenes: sceneResources, actorPrefabs: extractDataArray(resourcesLookup.actorPrefabs), triggerPrefabs: extractDataArray(resourcesLookup.triggerPrefabs), scripts: extractDataArray(resourcesLookup.scripts), sprites: extractDataArray(resourcesLookup.sprites), backgrounds: extractDataArray(resourcesLookup.backgrounds), emotes: extractDataArray(resourcesLookup.emotes), avatars: extractDataArray(resourcesLookup.avatars), tilesets: extractDataArray(resourcesLookup.tilesets), fonts: extractDataArray(resourcesLookup.fonts), sounds: extractDataArray(resourcesLookup.sounds), music: extractDataArray(resourcesLookup.music), palettes: extractDataArray(resourcesLookup.palettes), variables: variablesResource, engineFieldValues: engineFieldValuesResource, settings: settingsResource, metadata: metadataResource, }; }; |