import { AsyncUtils } from "../core/AsyncUtils" ; import { IFileInternal } from "./File/IFile" ; import { EngineSettingsInternal } from "../core/EngineSettings" ; import { AssetIdDatabaseInternal } from "../assets/AssetIdDatabase" ; hidden const JSZip = require ( "jszip" ) ; hidden export enum AssetChangeAction { update , delete , create } hidden export interface AssetChangeMetaData { changedPropertyName : string ; changedPropertyValue ? : string ; } hidden export interface AssetChangeInfo { timeStamp : string ; action : AssetChangeAction ; ids : string [ ] ; metadata ? : AssetChangeMetaData ; } hidden export class IOUtils { static assetChangeMetaData : AssetChangeMetaData | undefined = undefined ; static recordAssetChange ( action , ids ) { const timeStamp = JSON . stringify ( Date . now ( ) ) ; localStorage . setItem ( "last_asset_write_time" , timeStamp ) ; localStorage . setItem ( "asset_changed" , JSON . stringify ( { timeStamp : timeStamp , action : action , ids : ids , metadata : IOUtils . assetChangeMetaData } ) ) ; if ( IOUtils . assetChangeMetaData ) { IOUtils . assetChangeMetaData = undefined ; } } static parseFileName ( fullFileName , result ) { const tokens = fullFileName . split ( "." ) ; if ( tokens . length > 1 ) { const extension = tokens . pop ( ) ; const fileName = tokens . reduce ( ( previous , current ) => previous + "." + current ) ; result ( fileName , extension as string ) ; } else { result ( fullFileName , "" ) ; } } static parseFilePath ( filePath , result ) { const tokens = filePath . split ( /[/\\]+/ ) ; if ( tokens . length > 1 ) { const fullFileName = tokens . slice ( - 1 ) [ 0 ] ; IOUtils . parseFileName ( fullFileName , result ) ; } else { IOUtils . parseFileName ( filePath , result ) ; } } static parseFolderPath ( fullFilePath , result ) { const tokens = fullFilePath . split ( /[/\\]+/ ) ; if ( tokens . length > 1 ) { tokens . pop ( ) ; const folderPath = tokens . reduce ( ( previous , current ) => previous + "/" + current ) ; result ( folderPath ) ; } else { result ( fullFilePath ) ; } } static parseFolderName ( fullFolderPath , result ) { const tokens = fullFolderPath . split ( /[/\\]+/ ) ; result ( tokens . pop ( ) || "" ) ; } static importGameFromBlob ( blob , success , error ) { const finished = ( succeeded , withErrors ? ) => { if ( succeeded ) { success ( withErrors || false ) ; } else { error ( ) ; } } ; const newZip = new JSZip ( ) ; newZip . loadAsync ( blob ) . then ( ( zip ) => { if ( process . env . PLATFORM === "web" ) { const extractedFiles = { } ; const paths = Object . keys ( zip . files ) ; AsyncUtils . processBatch ( paths , ( path , extractSuccess , extractError ) => { const zipFile = zip . file ( path ) ; if ( zipFile ) { zipFile . async ( "string" ) . then ( ( fileData ) => { if ( [ "Assets/EngineSettings.json" ] . some ( p => p === path ) ) { path = EngineSettingsInternal . path ; } else if ( [ "idDatabase" ] . some ( p => p === path ) ) { path = AssetIdDatabaseInternal . path ; } extractedFiles [ path ] = fileData ; extractSuccess ( ) ; } , extractError ) ; } else { extractSuccess ( ) ; } } , extractionError => { const filePaths = Object . keys ( extractedFiles ) ; AsyncUtils . processBatch ( filePaths , ( filePath , writeSuccess , writeError ) => { const fileData = extractedFiles [ filePath ] ; IFileInternal . instance . write ( filePath , fileData ) . then ( ( ) => writeSuccess ( ) ) . catch ( ( ) => writeError ( ) ) ; } , writeError => { finished ( true , writeError ) ; } ) ; } ) ; } else { } } , ( ) => finished ( false ) ) . catch ( ( ) => finished ( false ) ) ; } static isDefaultAsset ( path ) { const tokens = path . split ( "/" ) ; if ( tokens . length > 1 ) { if ( process . env . NODE_ENV !== "development" ) { return tokens [ 0 ] === "Assets" && tokens [ 1 ] === "DefaultAssets" ; } } return false ; } }