From b790fb7c125b46511a9d2c7fb9ca1fbe17a5d3cb Mon Sep 17 00:00:00 2001 From: Alice <120576330+sillycodergirl@users.noreply.github.com> Date: Fri, 31 May 2024 15:11:54 -0400 Subject: [PATCH] Base folder asset scanning (because no mod support breaks freeplay) (#292) * Update AssetsLibraryList.hx * Update AssetsLibraryList.hx --- .../backend/assets/AssetsLibraryList.hx | 195 +++++++++++++----- 1 file changed, 142 insertions(+), 53 deletions(-) diff --git a/source/funkin/backend/assets/AssetsLibraryList.hx b/source/funkin/backend/assets/AssetsLibraryList.hx index 494479970..2303f5c68 100644 --- a/source/funkin/backend/assets/AssetsLibraryList.hx +++ b/source/funkin/backend/assets/AssetsLibraryList.hx @@ -1,40 +1,55 @@ package funkin.backend.assets; +import sys.FileSystem; import funkin.backend.assets.IModsAssetLibrary; import lime.utils.AssetLibrary; -class AssetsLibraryList extends AssetLibrary { +using StringTools; + +class AssetsLibraryList extends AssetLibrary +{ public var libraries:Array = []; @:allow(funkin.backend.system.Main) @:allow(funkin.backend.system.MainState) private var __defaultLibraries:Array = []; + public var base:AssetLibrary; - public function removeLibrary(lib:AssetLibrary) { - if (lib == null) return lib; + public function removeLibrary(lib:AssetLibrary) + { + if (lib == null) + return lib; libraries.remove(lib); return lib; } - public function existsSpecific(id:String, type:String, source:AssetSource = BOTH) { + + public function existsSpecific(id:String, type:String, source:AssetSource = BOTH) + { if (!id.startsWith("assets/") && exists('assets/$id', type)) return true; - for(k=>e in libraries) { - if (shouldSkipLib(k, source)) continue; + for (k => e in libraries) + { + if (shouldSkipLib(k, source)) + continue; if (e.exists(id, type)) return true; } return false; } + public override inline function exists(id:String, type:String):Bool return existsSpecific(id, type, BOTH); - public function getSpecificPath(id:String, source:AssetSource = BOTH) { - for(k=>e in libraries) { - if (shouldSkipLib(k, source)) continue; - + public function getSpecificPath(id:String, source:AssetSource = BOTH) + { + for (k => e in libraries) + { + if (shouldSkipLib(k, source)) + continue; @:privateAccess - if (e.exists(id, e.types.get(id))) { + if (e.exists(id, e.types.get(id))) + { var path = e.getPath(id); if (path != null) return path; @@ -46,95 +61,165 @@ class AssetsLibraryList extends AssetLibrary { public override inline function getPath(id:String) return getSpecificPath(id, BOTH); - public function getFiles(folder:String, source:AssetSource = BOTH):Array { + public function getFiles(folder:String, source:AssetSource = BOTH):Array + { var content:Array = []; - for(k=>e in libraries) { - if (shouldSkipLib(k, source)) continue; + for (k => e in libraries) + { + if (shouldSkipLib(k, source)) + continue; var l = e; - if (l is openfl.utils.AssetLibrary) { + if (l is openfl.utils.AssetLibrary) + { @:privateAccess l = cast(l, openfl.utils.AssetLibrary).__proxy; } - // TODO: do base folder scanning #if MOD_SUPPORT - if (l is IModsAssetLibrary) { - var lib = cast(l, IModsAssetLibrary); - for(e in lib.getFiles(folder)) - content.push(e); + if (source == MODS || source == BOTH) + { + if (l is IModsAssetLibrary) + { + var lib = cast(l, IModsAssetLibrary); + for (e in lib.getFiles(folder)) + content.push(e); + } } + #else + #if sys + if (source == SOURCE || source == BOTH) + { + var fileStuffs = FileSystem.readDirectory(folder); + if (fileStuffs != null && fileStuffs.length > 0) + { + for (e in fileStuffs) + { + if (!FileSystem.isDirectory(folder + e.toString())) + { + content.push(e); + } + } + } + else + { + Logs.trace('No files/folders found in the requested directory \'${folder}\'', WARNING, YELLOW); + } + } + #end #end } return content; } - public function getFolders(folder:String, source:AssetSource = BOTH):Array { + public function getFolders(folder:String, source:AssetSource = BOTH):Array + { var content:Array = []; - for(k=>e in libraries) { - if (shouldSkipLib(k, source)) continue; + for (k => e in libraries) + { + if (shouldSkipLib(k, source)) + continue; var l = e; - if (l is openfl.utils.AssetLibrary) { + if (l is openfl.utils.AssetLibrary) + { @:privateAccess l = cast(l, openfl.utils.AssetLibrary).__proxy; } - // TODO: do base folder scanning #if MOD_SUPPORT - if (l is IModsAssetLibrary) { - var lib = cast(l, IModsAssetLibrary); - for(e in lib.getFolders(folder)) - content.push(e); + if (source == MODS || source == BOTH) + { + if (l is IModsAssetLibrary) + { + var lib = cast(l, IModsAssetLibrary); + for (e in lib.getFolders(folder)) + content.push(e); + } + } + #else + #if sys + if (source == SOURCE || source == BOTH) + { + var fileStuffs = FileSystem.readDirectory(folder); + if (fileStuffs != null && fileStuffs.length > 0) + { + for (e in fileStuffs) + { + if (FileSystem.isDirectory(folder + e.toString())) + { + content.push(e); + } + } + } + else + { + Logs.trace('No files/folders found in the requested directory \'${folder}\'', WARNING, YELLOW); + } } #end + #end } return content; } - public function getSpecificAsset(id:String, type:String, source:AssetSource = BOTH):Dynamic { - try { - if (!id.startsWith("assets/")) { + public function getSpecificAsset(id:String, type:String, source:AssetSource = BOTH):Dynamic + { + try + { + if (!id.startsWith("assets/")) + { var ass = getSpecificAsset('assets/$id', type, source); - if (ass != null) { + if (ass != null) + { return ass; } } - for(k=>e in libraries) { - if (shouldSkipLib(k, source)) continue; - + for (k => e in libraries) + { + if (shouldSkipLib(k, source)) + continue; @:privateAccess - if (e.exists(id, e.types.get(id))) { + if (e.exists(id, e.types.get(id))) + { var asset = e.getAsset(id, type); - if (asset != null) { + if (asset != null) + { return asset; } } } return null; - } catch(e) { + } + catch (e) + { throw e; } return null; } - private function shouldSkipLib(k:Int, source:AssetSource) { - return switch(source) { - case BOTH: false; - case SOURCE: k < libraries.length - __defaultLibraries.length; - case MODS: k >= libraries.length - __defaultLibraries.length; + private function shouldSkipLib(k:Int, source:AssetSource) + { + return switch (source) + { + case BOTH: false; + case SOURCE: k < libraries.length - __defaultLibraries.length; + case MODS: k >= libraries.length - __defaultLibraries.length; }; } + public override inline function getAsset(id:String, type:String):Dynamic return getSpecificAsset(id, type, BOTH); - public override function isLocal(id:String, type:String) { + public override function isLocal(id:String, type:String) + { return true; } - public function new(?base:AssetLibrary) { + public function new(?base:AssetLibrary) + { super(); if (base == null) base = Assets.getLibrary("default"); @@ -142,30 +227,34 @@ class AssetsLibraryList extends AssetLibrary { __defaultLibraries.push(base); } - public function unloadLibraries() { - for(l in libraries) + public function unloadLibraries() + { + for (l in libraries) if (!__defaultLibraries.contains(l)) l.unload(); } - public function reset() { + public function reset() + { unloadLibraries(); libraries = []; // adds default libraries in again - for(d in __defaultLibraries) + for (d in __defaultLibraries) addLibrary(d); } - public function addLibrary(lib:AssetLibrary) { + public function addLibrary(lib:AssetLibrary) + { libraries.insert(0, lib); return lib; } } -enum abstract AssetSource(Null) from Bool from Null to Null { +enum abstract AssetSource(Null) from Bool from Null to Null +{ var SOURCE = true; var MODS = false; var BOTH = null; -} \ No newline at end of file +}