From 1981e29dc26705fb408d39ad8ff1824637dadcca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=8D=9A=7ENex?= <87421482+NexIsDumb@users.noreply.github.com> Date: Sun, 7 Apr 2024 16:58:29 +0200 Subject: [PATCH] this should be better (still a wip) i'll brb later to continue --- source/funkin/backend/FunkinText.hx | 2 +- .../backend/assets/ModsFolderLibrary.hx | 7 +- source/funkin/backend/assets/Paths.hx | 2 +- .../backend/assets/TranslatedAssetLibrary.hx | 32 + source/funkin/backend/system/MainState.hx | 2 +- ...TranslationsUtil.hx => TranslationUtil.hx} | 14 +- source/funkin/menus/FreeplayState.hx | 8 +- source/funkin/menus/ui/Alphabet.hx | 2 +- source/funkin/options/Options.hx | 2 +- .../funkin/options/categories/MiscOptions.hx | 12 +- source/lime/utils/AssetLibrary.hx | 985 ------------------ source/openfl/utils/AssetLibrary.hx | 453 -------- 12 files changed, 50 insertions(+), 1471 deletions(-) create mode 100644 source/funkin/backend/assets/TranslatedAssetLibrary.hx rename source/funkin/backend/utils/{TranslationsUtil.hx => TranslationUtil.hx} (91%) delete mode 100644 source/lime/utils/AssetLibrary.hx delete mode 100644 source/openfl/utils/AssetLibrary.hx diff --git a/source/funkin/backend/FunkinText.hx b/source/funkin/backend/FunkinText.hx index 84cfe407..a3c8c473 100644 --- a/source/funkin/backend/FunkinText.hx +++ b/source/funkin/backend/FunkinText.hx @@ -5,7 +5,7 @@ import flixel.text.FlxText; class FunkinText extends FlxText { public function new(X:Float = 0, Y:Float = 0, FieldWidth:Float = 0, ?Text:String, Size:Int = 16, Border:Bool = true, ?TranslationID:String, ?TranslParameters:Array) { - super(X, Y, FieldWidth, TranslationsUtil.get(Text, TranslationID, TranslParameters), Size); + super(X, Y, FieldWidth, TranslationUtil.get(Text, TranslationID, TranslParameters), Size); setFormat(Paths.font("vcr.ttf"), Size, FlxColor.WHITE); if (Border) { borderStyle = OUTLINE; diff --git a/source/funkin/backend/assets/ModsFolderLibrary.hx b/source/funkin/backend/assets/ModsFolderLibrary.hx index eb8c8755..a196d16f 100644 --- a/source/funkin/backend/assets/ModsFolderLibrary.hx +++ b/source/funkin/backend/assets/ModsFolderLibrary.hx @@ -1,8 +1,6 @@ package funkin.backend.assets; import openfl.utils.AssetLibrary; -//import lime.utils.Assets as LimeAssets; - import lime.media.AudioBuffer; import lime.graphics.Image; import lime.text.Font; @@ -26,10 +24,7 @@ class ModsFolderLibrary extends AssetLibrary implements IModsAssetLibrary { this.basePath = basePath; this.libName = libName; this.prefix = 'assets/$libName/'; - if(modName == null) - this.modName = libName; - else - this.modName = modName; + this.modName = modName == null ? libName : modName; super(); } diff --git a/source/funkin/backend/assets/Paths.hx b/source/funkin/backend/assets/Paths.hx index e0482944..1d526b9b 100644 --- a/source/funkin/backend/assets/Paths.hx +++ b/source/funkin/backend/assets/Paths.hx @@ -42,7 +42,7 @@ class Paths return getPath('$translFolderName/$key'); public static inline function translations(key:String) - return translationsMain('${TranslationsUtil.curLanguage}/$key'); + return translationsMain('${TranslationUtil.curLanguage}/$key'); public static inline function video(key:String, ?ext:String = "mp4") return getPath('videos/$key.$ext'); diff --git a/source/funkin/backend/assets/TranslatedAssetLibrary.hx b/source/funkin/backend/assets/TranslatedAssetLibrary.hx new file mode 100644 index 00000000..2a62c632 --- /dev/null +++ b/source/funkin/backend/assets/TranslatedAssetLibrary.hx @@ -0,0 +1,32 @@ +package funkin.backend.assets; + +import openfl.utils.AssetLibrary; +import lime.utils.Bytes; + +class TranslatedAssetLibrary extends AssetLibrary { + public var langFolder:String; + public var langPath:String; + + public function new(langFolder:String) { + this.langFolder = langFolder; + this.langPath = '${Paths.translFolderName}/$langFolder'; + super(); + } + + function toString():String + return '(TranslatedAssetLibrary: The labguage folder is $langFolder)'; + + public override function getBytes(id:String):Bytes { + for(lib in ModsFolder.getLoadedModsLibs()) { + if(!(lib is AssetLibrary)) continue; + + var mainPath = lib.prefix; + if(!mainPath.endsWith('/')) mainPath += '/'; + var val = cast(lib, AssetLibrary).getBytes('$mainPath$langPath/$id'); + if(val != null) return val; + } + return null; + } + + // I'll brb later and continue - Nex +} \ No newline at end of file diff --git a/source/funkin/backend/system/MainState.hx b/source/funkin/backend/system/MainState.hx index f9aef3a0..147c5193 100644 --- a/source/funkin/backend/system/MainState.hx +++ b/source/funkin/backend/system/MainState.hx @@ -55,7 +55,7 @@ class MainState extends FlxState { Main.refreshAssets(); ModsFolder.onModSwitch.dispatch(ModsFolder.currentModFolder); - TranslationsUtil.setLanguage(); + TranslationUtil.setLanguage(); DiscordUtil.init(); EventsData.reloadEvents(); TitleState.initialized = false; diff --git a/source/funkin/backend/utils/TranslationsUtil.hx b/source/funkin/backend/utils/TranslationUtil.hx similarity index 91% rename from source/funkin/backend/utils/TranslationsUtil.hx rename to source/funkin/backend/utils/TranslationUtil.hx index 5946ed63..a1304ee8 100644 --- a/source/funkin/backend/utils/TranslationsUtil.hx +++ b/source/funkin/backend/utils/TranslationUtil.hx @@ -14,7 +14,7 @@ import haxe.Exception; * * Made by @NexIsDumb originally for the Poldhub mod. */ -class TranslationsUtil +class TranslationUtil { /** * The current language selected translation map; If the current language it's english, this map will be empty. @@ -68,17 +68,7 @@ class TranslationsUtil public static function setLanguage(?name:String) { if(name == null) name = curLanguage; transMap = loadLanguage(name); - - for(mod in ModsFolder.getLoadedModsLibs()) { - if(!(mod is IModsAssetLibrary) || mod is ZipFolderLibrary) continue; // TODO: add support for zipped mods - // TODO: clean this code - var _mod = cast(mod, openfl.utils.AssetLibrary); - var mod = cast(mod, IModsAssetLibrary); - var mainPath = mod.basePath; - if(!mainPath.endsWith('/')) mainPath += '/'; - trace("Set sub library to " + mainPath + Paths.translFolderName + "/" + name + "/assets/"); - _mod.subLibraries = [ModsFolder.loadLibraryFromFolder(name, '$mainPath${Paths.translFolderName}/$name/assets/', true)]; - } + // To add the switch for the language in TranslatedAssetLibrary here! - Nex } /** diff --git a/source/funkin/menus/FreeplayState.hx b/source/funkin/menus/FreeplayState.hx index 78f54f25..d555d747 100644 --- a/source/funkin/menus/FreeplayState.hx +++ b/source/funkin/menus/FreeplayState.hx @@ -347,10 +347,10 @@ class FreeplayState extends MusicBeatState * Array containing all labels for Co-Op / Opponent modes. */ public var coopLabels:Array = [ - TranslationsUtil.get("[TAB] Solo"), - TranslationsUtil.get("[TAB] Opponent Mode"), - TranslationsUtil.get("[TAB] Co-Op Mode"), - TranslationsUtil.get("[TAB] Co-Op Mode (Switched)") + TranslationUtil.get("[TAB] Solo"), + TranslationUtil.get("[TAB] Opponent Mode"), + TranslationUtil.get("[TAB] Co-Op Mode"), + TranslationUtil.get("[TAB] Co-Op Mode (Switched)") ]; /** diff --git a/source/funkin/menus/ui/Alphabet.hx b/source/funkin/menus/ui/Alphabet.hx index becebce1..59540fef 100644 --- a/source/funkin/menus/ui/Alphabet.hx +++ b/source/funkin/menus/ui/Alphabet.hx @@ -81,7 +81,7 @@ class Alphabet extends FlxSpriteGroup { super(x, y); - _finalText = this.text = TranslationsUtil.get(text, transID, transParams); + _finalText = this.text = TranslationUtil.get(text, transID, transParams); isBold = bold; var alphabetPath = Paths.xml("alphabet"); diff --git a/source/funkin/options/Options.hx b/source/funkin/options/Options.hx index 79eda33f..c9e248b6 100644 --- a/source/funkin/options/Options.hx +++ b/source/funkin/options/Options.hx @@ -35,7 +35,7 @@ class Options public static var songOffset:Float = 0; public static var framerate:Int = 120; public static var gpuOnlyBitmaps:Bool = #if (mac || web) false #else true #end; // causes issues on mac and web - public static var language = funkin.backend.utils.TranslationsUtil.DEFAULT_LANGUAGE; + public static var language = funkin.backend.utils.TranslationUtil.DEFAULT_LANGUAGE; public static var lastLoadedMod:String = null; diff --git a/source/funkin/options/categories/MiscOptions.hx b/source/funkin/options/categories/MiscOptions.hx index 858524b6..de543cb4 100644 --- a/source/funkin/options/categories/MiscOptions.hx +++ b/source/funkin/options/categories/MiscOptions.hx @@ -6,17 +6,17 @@ class MiscOptions extends OptionsScreen { super("Miscellaneous", "Use this menu to reset save data or engine settings."); { // Language Option - var lanArray:Array = TranslationsUtil.getLanguages(); - if (lanArray.contains(TranslationsUtil.DEFAULT_LANGUAGE)) - lanArray.remove(TranslationsUtil.DEFAULT_LANGUAGE); - lanArray.insert(0, TranslationsUtil.DEFAULT_LANGUAGE); + var lanArray:Array = TranslationUtil.getLanguages(); + if (lanArray.contains(TranslationUtil.DEFAULT_LANGUAGE)) + lanArray.remove(TranslationUtil.DEFAULT_LANGUAGE); + lanArray.insert(0, TranslationUtil.DEFAULT_LANGUAGE); add(new ArrayOption( "Language", - 'The language that the engine currently uses (the default one is ${TranslationsUtil.DEFAULT_LANGUAGE}).', + 'The language that the engine currently uses (the default one is ${TranslationUtil.DEFAULT_LANGUAGE}).', lanArray, [for(lan in lanArray) lan.split("/").last()], "language", function(path:String) { - TranslationsUtil.setLanguage(path); + TranslationUtil.setLanguage(path); parent.remove(this); // Reload the current screen // todo add parent.reload(); diff --git a/source/lime/utils/AssetLibrary.hx b/source/lime/utils/AssetLibrary.hx deleted file mode 100644 index 010d18e8..00000000 --- a/source/lime/utils/AssetLibrary.hx +++ /dev/null @@ -1,985 +0,0 @@ -package lime.utils; - -import haxe.io.Path; -import lime.app.Event; -import lime.app.Future; -import lime.app.Promise; -import lime.media.AudioBuffer; -import lime.graphics.Image; -import lime.net.HTTPRequest; -import lime.text.Font; -import lime.utils.AssetType; -#if flash -import flash.display.BitmapData; -import flash.media.Sound; -#end - -#if !lime_debug -@:fileXml('tags="haxe,release"') -@:noDebug -#end -@:access(lime.text.Font) -@:access(lime.utils.Assets) -class AssetLibrary -{ - public var onChange = new EventVoid>(); - - @:noCompletion private var assetsLoaded:Int; - @:noCompletion private var assetsTotal:Int; - @:noCompletion private var bytesLoaded:Int; - @:noCompletion private var bytesLoadedCache:Map; - @:noCompletion private var bytesTotal:Int; - @:noCompletion private var cachedAudioBuffers = new Map(); - @:noCompletion private var cachedBytes = new Map(); - @:noCompletion private var cachedFonts = new Map(); - @:noCompletion private var cachedImages = new Map(); - @:noCompletion private var cachedText = new Map(); - @:noCompletion private var classTypes = new Map>(); - @:noCompletion private var loaded:Bool; - @:noCompletion private var pathGroups = new Map>(); - @:noCompletion private var paths = new Map(); - @:noCompletion private var preload = new Map(); - @:noCompletion private var promise:Promise; - @:noCompletion private var sizes = new Map(); - @:noCompletion private var types = new Map(); - - public function new() - { - bytesLoaded = 0; - bytesTotal = 0; - } - - public function exists(id:String, type:String):Bool - { - var requestedType = type != null ? cast(type, AssetType) : null; - var assetType = types.get(id); - - if (assetType != null) - { - if (assetType == requestedType - || ((requestedType == SOUND || requestedType == MUSIC) && (assetType == MUSIC || assetType == SOUND))) - { - return true; - } - - #if flash - if (requestedType == BINARY && (assetType == BINARY || assetType == TEXT || assetType == IMAGE)) - { - return true; - } - else if (requestedType == TEXT && assetType == BINARY) - { - return true; - } - else if (requestedType == null || paths.exists(id)) - { - return true; - } - #else - if (requestedType == BINARY || requestedType == null || (assetType == BINARY && requestedType == TEXT)) - { - return true; - } - #end - } - - return false; - } - - public function checkSpecificAsset(id:String, type:String):Bool - { - return switch (type) - { - case BINARY: cachedBytes.exists(id) || classTypes.exists(id) || paths.exists(id); - case FONT: cachedFonts.exists(id) || classTypes.exists(id) || paths.exists(id); - case IMAGE: cachedImages.exists(id) || classTypes.exists(id) || paths.exists(id); - case MUSIC, SOUND: cachedAudioBuffers.exists(id) || classTypes.exists(id) || paths.exists(id); - case TEXT: cachedText.exists(id) || cachedBytes.exists(id) || paths.exists(id); - - case TEMPLATE: false; - default: throw "Unknown asset type: " + type; - } - } - - public static function fromBytes(bytes:Bytes, rootPath:String = null):AssetLibrary - { - return fromManifest(AssetManifest.fromBytes(bytes, rootPath)); - } - - public static function fromFile(path:String, rootPath:String = null):AssetLibrary - { - return fromManifest(AssetManifest.fromFile(path, rootPath)); - } - - public static function fromBundle(bundle:AssetBundle):AssetLibrary - { - if (bundle.data.exists("library.json")) - { - var manifest = AssetManifest.fromBytes(bundle.data.get("library.json")); - if (manifest != null) - { - var library:AssetLibrary = null; - - if (manifest.libraryType == null) - { - library = new AssetLibrary(); - } - else - { - var libraryClass = Type.resolveClass(manifest.libraryType); - - if (libraryClass != null) - { - library = Type.createInstance(libraryClass, manifest.libraryArgs); - } - else - { - Log.warn("Could not find library type: " + manifest.libraryType); - return null; - } - } - - library.__fromBundle(bundle, manifest); - return library; - } - } - else - { - var library = new AssetLibrary(); - library.__fromBundle(bundle); - return library; - } - return null; - } - - public static function fromManifest(manifest:AssetManifest):AssetLibrary - { - if (manifest == null) return null; - - var library:AssetLibrary = null; - - if (manifest.libraryType == null) - { - library = new AssetLibrary(); - } - else - { - var libraryClass = Type.resolveClass(manifest.libraryType); - - if (libraryClass != null) - { - library = Type.createInstance(libraryClass, manifest.libraryArgs); - } - else - { - Log.warn("Could not find library type: " + manifest.libraryType); - return null; - } - } - - library.__fromManifest(manifest); - - return library; - } - - public function getAsset(id:String, type:String):Dynamic - { - return switch (type) - { - case BINARY: getBytes(id); - case FONT: getFont(id); - case IMAGE: getImage(id); - case MUSIC, SOUND: getAudioBuffer(id); - case TEXT: getText(id); - - case TEMPLATE: throw "Not sure how to get template: " + id; - default: throw "Unknown asset type: " + type; - } - } - - public function getAudioBuffer(id:String):AudioBuffer - { - if (cachedAudioBuffers.exists(id)) - { - return cachedAudioBuffers.get(id); - } - else if (classTypes.exists(id)) - { - #if flash - var buffer = new AudioBuffer(); - buffer.src = cast(Type.createInstance(classTypes.get(id), []), Sound); - return buffer; - #else - return AudioBuffer.fromBytes(cast(Type.createInstance(classTypes.get(id), []), Bytes)); - #end - } - else - { - return AudioBuffer.fromFile(getPath(id)); - } - } - - public function getBytes(id:String):Bytes - { - if (cachedBytes.exists(id)) - { - return cachedBytes.get(id); - } - else if (cachedText.exists(id)) - { - var bytes = Bytes.ofString(cachedText.get(id)); - cachedBytes.set(id, bytes); - return bytes; - } - else if (classTypes.exists(id)) - { - #if flash - var data = Type.createInstance(classTypes.get(id), []); - - switch (types.get(id)) - { - case TEXT, BINARY: - return Bytes.ofData(cast(Type.createInstance(classTypes.get(id), []), flash.utils.ByteArray)); - - case IMAGE: - var bitmapData = cast(Type.createInstance(classTypes.get(id), []), BitmapData); - return Bytes.ofData(bitmapData.getPixels(bitmapData.rect)); - - default: - return null; - } - #else - return cast(Type.createInstance(classTypes.get(id), []), Bytes); - #end - } - else - { - return Bytes.fromFile(getPath(id)); - } - } - - public function getFont(id:String):Font - { - if (cachedFonts.exists(id)) - { - return cachedFonts.get(id); - } - else if (classTypes.exists(id)) - { - #if flash - var src = Type.createInstance(classTypes.get(id), []); - - var font = new Font(src.fontName); - font.src = src; - return font; - #else - return cast(Type.createInstance(classTypes.get(id), []), Font); - #end - } - else - { - return Font.fromFile(getPath(id)); - } - } - - public function getImage(id:String):Image - { - if (cachedImages.exists(id)) - { - return cachedImages.get(id); - } - else if (classTypes.exists(id)) - { - #if flash - return Image.fromBitmapData(cast(Type.createInstance(classTypes.get(id), []), BitmapData)); - #else - return cast(Type.createInstance(classTypes.get(id), []), Image); - #end - } - else - { - return Image.fromFile(getPath(id)); - } - } - - public function getPath(id:String):String - { - if (paths.exists(id)) - { - return paths.get(id); - } - else if (pathGroups.exists(id)) - { - return pathGroups.get(id)[0]; - } - else - { - return null; - } - } - - public function getText(id:String):String - { - if (cachedText.exists(id)) - { - return cachedText.get(id); - } - else - { - var bytes = getBytes(id); - - if (bytes == null) - { - return null; - } - else - { - return bytes.getString(0, bytes.length); - } - } - } - - public function isLocal(id:String, type:String):Bool - { - #if sys - return true; - #else - if (classTypes.exists(id)) - { - return true; - } - - return switch (cast(type, AssetType)) - { - case null: - cachedBytes.exists(id) || cachedText.exists(id) || cachedImages.exists(id) - || cachedAudioBuffers.exists(id) || cachedFonts.exists(id); - - case IMAGE: - cachedImages.exists(id); - - case MUSIC, SOUND: - cachedAudioBuffers.exists(id); - - case FONT: - cachedFonts.exists(id); - - default: - cachedBytes.exists(id) || cachedText.exists(id); - } - #end - } - - public function list(type:String):Array - { - var requestedType = type != null ? cast(type, AssetType) : null; - var items = []; - - for (id in types.keys()) - { - if (requestedType == null || exists(id, type)) - { - items.push(id); - } - } - - return items; - } - - public function loadAsset(id:String, type:String):Future - { - return switch (type) - { - case BINARY: loadBytes(id); - case FONT: loadFont(id); - case IMAGE: loadImage(id); - case MUSIC, SOUND: loadAudioBuffer(id); - case TEXT: loadText(id); - - case TEMPLATE: throw "Not sure how to load template: " + id; - default: throw "Unknown asset type: " + type; - } - } - - public function load():Future - { - if (loaded) - { - return Future.withValue(this); - } - - if (promise == null) - { - promise = new Promise(); - bytesLoadedCache = new Map(); - - assetsLoaded = 0; - assetsTotal = 1; - - for (id in preload.keys()) - { - if (!preload.get(id)) continue; - - Log.verbose("Preloading asset: " + id + " [" + types.get(id) + "]"); - - switch (types.get(id)) - { - case BINARY: - assetsTotal++; - - var future = loadBytes(id); - future.onProgress(load_onProgress.bind(id)); - future.onError(load_onError.bind(id)); - future.onComplete(loadBytes_onComplete.bind(id)); - - case FONT: - assetsTotal++; - - var future = loadFont(id); - future.onProgress(load_onProgress.bind(id)); - future.onError(load_onError.bind(id)); - future.onComplete(loadFont_onComplete.bind(id)); - - case IMAGE: - assetsTotal++; - - var future = loadImage(id); - future.onProgress(load_onProgress.bind(id)); - future.onError(load_onError.bind(id)); - future.onComplete(loadImage_onComplete.bind(id)); - - case MUSIC, SOUND: - assetsTotal++; - - var future = loadAudioBuffer(id); - future.onProgress(load_onProgress.bind(id)); - future.onError(loadAudioBuffer_onError.bind(id)); - future.onComplete(loadAudioBuffer_onComplete.bind(id)); - - case TEXT: - assetsTotal++; - - var future = loadText(id); - future.onProgress(load_onProgress.bind(id)); - future.onError(load_onError.bind(id)); - future.onComplete(loadText_onComplete.bind(id)); - - default: - } - } - - __assetLoaded(null); - } - - return promise.future; - } - - public function loadAudioBuffer(id:String):Future - { - if (cachedAudioBuffers.exists(id)) - { - return Future.withValue(cachedAudioBuffers.get(id)); - } - else if (classTypes.exists(id)) - { - return Future.withValue(AudioBuffer.fromBytes(cast(Type.createInstance(classTypes.get(id), []), Bytes))); - } - else - { - if (pathGroups.exists(id)) - { - return AudioBuffer.loadFromFiles(pathGroups.get(id)); - } - else - { - return AudioBuffer.loadFromFile(paths.get(id)); - } - } - } - - public function loadBytes(id:String):Future - { - if (cachedBytes.exists(id)) - { - return Future.withValue(cachedBytes.get(id)); - } - else if (classTypes.exists(id)) - { - #if flash - return Future.withValue(Bytes.ofData(Type.createInstance(classTypes.get(id), []))); - #else - return Future.withValue(Type.createInstance(classTypes.get(id), [])); - #end - } - else - { - return Bytes.loadFromFile(getPath(id)); - } - } - - public function loadFont(id:String):Future - { - if (cachedFonts.exists(id)) - { - return Future.withValue(cachedFonts.get(id)); - } - else if (classTypes.exists(id)) - { - var font:Font = Type.createInstance(classTypes.get(id), []); - - #if (js && html5) - return font.__loadFromName(font.name); - #else - return Future.withValue(font); - #end - } - else - { - #if (js && html5) - return Font.loadFromName(getPath(id)); - #else - return Font.loadFromFile(getPath(id)); - #end - } - } - - public static function loadFromBytes(bytes:Bytes, rootPath:String = null):Future - { - return AssetManifest.loadFromBytes(bytes, rootPath).then(function(manifest) - { - return loadFromManifest(manifest); - }); - } - - public static function loadFromFile(path:String, rootPath:String = null):Future - { - return AssetManifest.loadFromFile(path, rootPath).then(function(manifest) - { - return loadFromManifest(manifest); - }); - } - - public static function loadFromManifest(manifest:AssetManifest):Future - { - var library = fromManifest(manifest); - - if (library != null) - { - return library.load(); - } - else - { - return cast Future.withError("Could not load asset manifest"); - } - } - - public function loadImage(id:String):Future - { - if (cachedImages.exists(id)) - { - return Future.withValue(cachedImages.get(id)); - } - else if (classTypes.exists(id)) - { - return Future.withValue(Type.createInstance(classTypes.get(id), [])); - } - else if (cachedBytes.exists(id)) - { - return Image.loadFromBytes(cachedBytes.get(id)).then(function(image) - { - cachedBytes.remove(id); - cachedImages.set(id, image); - return Future.withValue(image); - }); - } - else - { - return Image.loadFromFile(getPath(id)); - } - } - - public function loadText(id:String):Future - { - if (cachedText.exists(id)) - { - return Future.withValue(cachedText.get(id)); - } - else if (cachedBytes.exists(id) || classTypes.exists(id)) - { - var bytes = getBytes(id); - - if (bytes == null) - { - return cast Future.withValue(null); - } - else - { - var text = bytes.getString(0, bytes.length); - cachedText.set(id, text); - return Future.withValue(text); - } - } - else - { - var request = new HTTPRequest(); - return request.load(getPath(id)); - } - } - - public function unload():Void - { - #if haxe4 - cachedBytes.clear(); - cachedFonts.clear(); - cachedImages.clear(); - cachedAudioBuffers.clear(); - cachedText.clear(); - #else - cachedBytes = new Map(); - cachedFonts = new Map(); - cachedImages = new Map(); - cachedText = new Map(); - classTypes = new Map>(); - #end - } - - @:noCompletion private function __assetLoaded(id:String):Void - { - assetsLoaded++; - - if (id != null) - { - Log.verbose("Loaded asset: " + id + " [" + types.get(id) + "] (" + (assetsLoaded - 1) + "/" + (assetsTotal - 1) + ")"); - } - - if (id != null) - { - var size = sizes.exists(id) ? sizes.get(id) : 0; - - if (!bytesLoadedCache.exists(id)) - { - bytesLoaded += size; - } - else - { - var cache = bytesLoadedCache.get(id); - - if (cache < size) - { - bytesLoaded += (size - cache); - } - } - - bytesLoadedCache.set(id, size); - } - - if (assetsLoaded < assetsTotal) - { - promise.progress(bytesLoaded, bytesTotal); - } - else - { - loaded = true; - promise.progress(bytesTotal, bytesTotal); - promise.complete(this); - } - } - - @:noCompletion private function __cacheBreak(path:String):String - { - return Assets.__cacheBreak(path); - } - - @:noCompletion private function __fromBundle(bundle:AssetBundle, manifest:AssetManifest = null):Void - { - if (manifest != null) - { - var id, data, type:AssetType; - for (asset in manifest.assets) - { - id = Reflect.hasField(asset, "id") ? asset.id : asset.path; - data = bundle.data.get(asset.path); - - if (Reflect.hasField(asset, "type")) - { - type = asset.type; - switch (type) - { - #if !web - case IMAGE: - cachedImages.set(id, Image.fromBytes(data)); - case MUSIC, SOUND: - cachedAudioBuffers.set(id, AudioBuffer.fromBytes(data)); - case FONT: - cachedFonts.set(id, Font.fromBytes(data)); - #end - case TEXT: - cachedText.set(id, data != null ? Std.string(data) : null); - default: - cachedBytes.set(id, data); - } - types.set(id, asset.type); - } - else - { - cachedBytes.set(id, data); - types.set(id, BINARY); - } - } - } - else - { - for (path in bundle.paths) - { - cachedBytes.set(path, bundle.data.get(path)); - types.set(path, BINARY); - } - } - } - - @:noCompletion private function __fromManifest(manifest:AssetManifest):Void - { - var hasSize = (manifest.version >= 2); - var size, id, pathGroup:Array, classRef; - - var basePath = manifest.rootPath; - if (basePath == null) basePath = ""; - if (basePath != "") basePath += "/"; - - for (asset in manifest.assets) - { - size = hasSize && Reflect.hasField(asset, "size") ? asset.size : 100; - id = Reflect.hasField(asset, "id") ? asset.id : asset.path; - - if (Reflect.hasField(asset, "path")) - { - paths.set(id, __cacheBreak(__resolvePath(basePath + Reflect.field(asset, "path")))); - } - - if (Reflect.hasField(asset, "pathGroup")) - { - pathGroup = Reflect.field(asset, "pathGroup"); - - for (i in 0...pathGroup.length) - { - pathGroup[i] = __cacheBreak(__resolvePath(basePath + pathGroup[i])); - } - - pathGroups.set(id, pathGroup); - } - - sizes.set(id, size); - types.set(id, asset.type); - - if (Reflect.hasField(asset, "preload")) - { - preload.set(id, Reflect.field(asset, "preload")); - } - - if (Reflect.hasField(asset, "className")) - { - classRef = Type.resolveClass(Reflect.field(asset, "className")); - - #if (js && html5 && modular) - if (classRef == null) - { - classRef = untyped $hx_exports[asset.className]; - } - #end - - classTypes.set(id, classRef); - } - } - - bytesTotal = 0; - - for (asset in manifest.assets) - { - id = Reflect.hasField(asset, "id") ? asset.id : asset.path; - - if (preload.exists(id) && preload.get(id) && sizes.exists(id)) - { - bytesTotal += sizes.get(id); - } - } - } - - @:noCompletion private function __resolvePath(path:String):String - { - path = StringTools.replace(path, "\\", "/"); - - var colonIdx:Int = path.indexOf(":"); - if (StringTools.startsWith(path, "http") && colonIdx > 0) - { - var lastSlashIdx:Int = colonIdx + 3; - var httpSection:String = path.substr(0, lastSlashIdx); - path = httpSection + StringTools.replace(path.substr(lastSlashIdx), "//", "/"); - } - else - { - path = StringTools.replace(path, "//", "/"); - } - - #if android - if (StringTools.startsWith(path, "./")) - { - path = path.substr(2); - } - #end - - if (path.indexOf("./") > -1) - { - var split = path.split("/"); - var newPath = []; - - for (i in 0...split.length) - { - if (split[i] == "..") - { - if (i == 0 || newPath[i - 1] == "..") - { - newPath.push(".."); - } - else - { - newPath.pop(); - } - } - else if (split[i] == ".") - { - if (i == 0) - { - newPath.push("."); - } - } - else - { - newPath.push(split[i]); - } - } - path = newPath.join("/"); - } - - return path; - } - - // Event Handlers - @:noCompletion private function loadAudioBuffer_onComplete(id:String, audioBuffer:AudioBuffer):Void - { - cachedAudioBuffers.set(id, audioBuffer); - - if (pathGroups.exists(id)) - { - var pathGroup = pathGroups.get(id); - - for (otherID in pathGroups.keys()) - { - if (otherID == id) continue; - - for (path in pathGroup) - { - if (pathGroups.get(otherID).indexOf(path) > -1) - { - cachedAudioBuffers.set(otherID, audioBuffer); - break; - } - } - } - } - - __assetLoaded(id); - } - - @:noCompletion private function loadAudioBuffer_onError(id:String, message:Dynamic):Void - { - #if (js && html5) - if (message != null && message != "") - { - Log.warn("Could not load \"" + id + "\": " + Std.string(message)); - } - else - { - Log.warn("Could not load \"" + id + "\""); - } - - loadAudioBuffer_onComplete(id, new AudioBuffer()); - #else - load_onError(id, message); - #end - } - - @:noCompletion private function loadBytes_onComplete(id:String, bytes:Bytes):Void - { - cachedBytes.set(id, bytes); - __assetLoaded(id); - } - - @:noCompletion private function loadFont_onComplete(id:String, font:Font):Void - { - cachedFonts.set(id, font); - __assetLoaded(id); - } - - @:noCompletion private function loadImage_onComplete(id:String, image:Image):Void - { - cachedImages.set(id, image); - __assetLoaded(id); - } - - @:noCompletion private function loadText_onComplete(id:String, text:String):Void - { - cachedText.set(id, text); - __assetLoaded(id); - } - - @:noCompletion private function load_onError(id:String, message:Dynamic):Void - { - if (message != null && message != "") - { - promise.error("Error loading asset \"" + id + "\": " + Std.string(message)); - } - else - { - promise.error("Error loading asset \"" + id + "\""); - } - } - - @:noCompletion private function load_onProgress(id:String, bytesLoaded:Int, bytesTotal:Int):Void - { - if (bytesLoaded > 0) - { - var size = sizes.get(id); - var percent; - - if (bytesTotal > 0) - { - // Use a ratio in case the real bytesTotal is different than our precomputed total - - percent = (bytesLoaded / bytesTotal); - if (percent > 1) percent = 1; - bytesLoaded = Math.floor(percent * size); - } - else if (bytesLoaded > size) - { - bytesLoaded = size; - } - - if (bytesLoadedCache.exists(id)) - { - var cache = bytesLoadedCache.get(id); - - if (bytesLoaded != cache) - { - this.bytesLoaded += (bytesLoaded - cache); - } - } - else - { - this.bytesLoaded += bytesLoaded; - } - - bytesLoadedCache.set(id, bytesLoaded); - promise.progress(this.bytesLoaded, this.bytesTotal); - } - } -} diff --git a/source/openfl/utils/AssetLibrary.hx b/source/openfl/utils/AssetLibrary.hx deleted file mode 100644 index e6022182..00000000 --- a/source/openfl/utils/AssetLibrary.hx +++ /dev/null @@ -1,453 +0,0 @@ -package openfl.utils; - -import openfl.display.MovieClip; -#if lime -import lime.graphics.Image; -import lime.media.AudioBuffer; -import lime.text.Font; -import lime.utils.AssetLibrary as LimeAssetLibrary; -import lime.utils.AssetType; -import lime.utils.AssetManifest; -import lime.utils.Bytes; -#end - -#if !openfl_debug -@:fileXml('tags="haxe,release"') -@:noDebug -#end -class AssetLibrary #if lime extends LimeAssetLibrary #end -{ - #if lime - @:noCompletion private var __proxy:LimeAssetLibrary; - - // Had to edit this file for mainly this, its similar to proxy but doesnt cover everything - Nex - public var subLibraries:Array = []; - - @:noCompletion private function set_subLibraries(value:Array):Array { - if (value == null) value = []; - return subLibraries = value; - } - #end - - public function new() - { - #if lime - super(); - #end - } - - public function bind(className:String, instance:Dynamic = null):Bool - { - return false; - } - - #if lime - public override function exists(id:String, type:String):Bool - { - if (__proxy != null) - { - return __proxy.exists(id, type); - } - else - { - for(lib in subLibraries) { - trace(lib, id, type); - if(lib != null && lib.exists(id, type)) return true; - } - trace(this, id, type); - return super.exists(id, type); - } - } - #end - - public static function fromBundle(bundle:AssetBundle):AssetLibrary - { - #if lime - var library = LimeAssetLibrary.fromBundle(bundle); - - if (library != null) - { - if ((library is AssetLibrary)) - { - return cast library; - } - else - { - var _library = new AssetLibrary(); - _library.__proxy = library; - return _library; - } - } - else - { - return null; - } - #else - return null; - #end - } - - public static function fromBytes(bytes:ByteArray, rootPath:String = null):AssetLibrary - { - #if lime - return cast fromManifest(AssetManifest.fromBytes(bytes, rootPath)); - #else - return null; - #end - } - - public static function fromFile(path:String, rootPath:String = null):AssetLibrary - { - #if lime - return cast fromManifest(AssetManifest.fromFile(path, rootPath)); - #else - return null; - #end - } - - public static function fromManifest(manifest:AssetManifest):#if (java && lime) LimeAssetLibrary #else AssetLibrary #end - { - #if lime - var library = LimeAssetLibrary.fromManifest(manifest); - - if (library != null) - { - if ((library is AssetLibrary)) - { - return cast library; - } - else - { - var _library = new AssetLibrary(); - _library.__proxy = library; - return _library; - } - } - else - { - return null; - } - #else - return null; - #end - } - - #if lime - public override function getAsset(id:String, type:String):Dynamic - { - if (__proxy != null) - { - return __proxy.getAsset(id, type); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getAsset(id, type)) != null) return val; - return super.getAsset(id, type); - } - } - #end - - #if lime - public override function getAudioBuffer(id:String):AudioBuffer - { - if (__proxy != null) - { - return __proxy.getAudioBuffer(id); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getAudioBuffer(id)) != null) return val; - return super.getAudioBuffer(id); - } - } - #end - - #if lime - public override function getBytes(id:String):Bytes - { - if (__proxy != null) - { - return __proxy.getBytes(id); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getBytes(id)) != null) return val; - return super.getBytes(id); - } - } - #end - - #if lime - public override function getFont(id:String):Font - { - if (__proxy != null) - { - return __proxy.getFont(id); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getFont(id)) != null) return val; - return super.getFont(id); - } - } - #end - - #if lime - public override function getImage(id:String):Image - { - if (__proxy != null) - { - return __proxy.getImage(id); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getImage(id)) != null) return val; - return super.getImage(id); - } - } - #end - - public function getMovieClip(id:String):MovieClip - { - return null; - } - - #if lime - public override function getPath(id:String):String - { - if (__proxy != null) - { - return __proxy.getPath(id); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getPath(id)) != null) return val; - return super.getPath(id); - } - } - #end - - #if lime - public override function getText(id:String):String - { - if (__proxy != null) - { - return __proxy.getText(id); - } - else - { - var val:Dynamic = null; - for(lib in subLibraries) if(lib != null && (val = lib.getText(id)) != null) return val; - return super.getText(id); - } - } - #end - - #if lime - public override function isLocal(id:String, type:String):Bool - { - if (__proxy != null) - { - return __proxy.isLocal(id, type); - } - else - { - for(lib in subLibraries) if(lib != null && lib.isLocal(id, type)) return true; - return super.isLocal(id, type); - } - } - #end - - #if lime - public override function list(type:String):Array - { - if (__proxy != null) - { - return __proxy.list(type); - } - else - { - var finalArr:Array = []; - for(lib in subLibraries.concat([this])) if(lib != null) for(f in lib.list(type)) if(!finalArr.contains(f)) finalArr.push(f); - return finalArr; - } - } - #end - - #if lime - public override function loadAsset(id:String, type:String):Future - { - if (__proxy != null) - { - return __proxy.loadAsset(id, type); - } - else - { - for(lib in subLibraries) if(lib != null && lib.checkSpecificAsset(id, type)) return lib.loadAsset(id, type); - return super.loadAsset(id, type); - } - } - #end - - #if lime - public override function load():Future - { - if (__proxy != null) - { - return __proxy.load(); - } - else - { - // TODO: Make this work by using multiple Futures - for(lib in subLibraries) if(lib != null) lib.load(); - return super.load(); - } - } - #end - - #if lime - public override function loadAudioBuffer(id:String):Future - { - if (__proxy != null) - { - return __proxy.loadAudioBuffer(id); - } - else - { - for(lib in subLibraries) if(lib != null && lib.checkSpecificAsset(id, MUSIC)) return lib.loadAudioBuffer(id); - return super.loadAudioBuffer(id); - } - } - #end - - #if lime - public override function loadBytes(id:String):Future - { - if (__proxy != null) - { - return __proxy.loadBytes(id); - } - else - { - for(lib in subLibraries) if(lib != null && lib.checkSpecificAsset(id, BINARY)) return lib.loadBytes(id); - return super.loadBytes(id); - } - } - #end - - #if lime - public override function loadFont(id:String):Future - { - if (__proxy != null) - { - return __proxy.loadFont(id); - } - else - { - for(lib in subLibraries) if(lib != null && lib.checkSpecificAsset(id, FONT)) return lib.loadFont(id); - return super.loadFont(id); - } - } - #end - - public static function loadFromBytes(bytes:ByteArray, rootPath:String = null):#if (java && lime) Future #else Future #end - { - #if lime - return AssetManifest.loadFromBytes(bytes, rootPath).then(function(manifest) - { - return loadFromManifest(manifest); - }); - #else - return cast Future.withValue(null); - #end - } - - public static function loadFromFile(path:String, rootPath:String = null):#if (java && lime) Future #else Future #end - { - #if lime - return AssetManifest.loadFromFile(path, rootPath).then(function(manifest) - { - return loadFromManifest(manifest); - }); - #else - return cast Future.withValue(null); - #end - } - - public static function loadFromManifest(manifest:AssetManifest):#if (java && lime) Future #else Future #end - { - #if lime - var library:AssetLibrary = cast fromManifest(manifest); - - if (library != null) - { - return library.load().then(function(library) - { - return Future.withValue(cast library); - }); - } - else - { - return cast Future.withError("Could not load asset manifest"); - } - #else - return cast Future.withValue(null); - #end - } - - #if lime - public override function loadImage(id:String):Future - { - if (__proxy != null) - { - return __proxy.loadImage(id); - } - else - { - for(lib in subLibraries) if(lib != null && lib.checkSpecificAsset(id, IMAGE)) return lib.loadImage(id); - return super.loadImage(id); - } - } - #end - - public function loadMovieClip(id:String):Future - { - return Future.withValue(getMovieClip(id)); - } - - #if lime - public override function loadText(id:String):Future - { - if (__proxy != null) - { - return __proxy.loadText(id); - } - else - { - for(lib in subLibraries) if(lib != null && lib.checkSpecificAsset(id, TEXT)) return lib.loadText(id); - return super.loadText(id); - } - } - #end - - #if lime - public override function unload():Void - { - if (__proxy != null) - { - return __proxy.unload(); - } - else - { - for(lib in subLibraries) if(lib != null) lib.unload(); - return super.unload(); - } - } - #end -}