From 3e99ae2a65c15e2447f4bc530ce7ac0865c76256 Mon Sep 17 00:00:00 2001 From: Yoshman29 Date: Thu, 6 Oct 2022 12:49:03 +0200 Subject: [PATCH] NOTE TO MYSELF: new idea for banger assetslibrary --- buildnumber.txt | 2 +- .../introMod/{_append => }/data/introText.txt | 0 source/Main.hx | 1 + source/funkin/game/Character.hx | 2 +- source/funkin/mods/ModsAssetLibrary.hx | 41 ++++++++++ source/funkin/mods/ModsFolder.hx | 79 ++++++++++++++----- 6 files changed, 104 insertions(+), 21 deletions(-) rename example_mods/introMod/{_append => }/data/introText.txt (100%) create mode 100644 source/funkin/mods/ModsAssetLibrary.hx diff --git a/buildnumber.txt b/buildnumber.txt index 4701cc79..7b27b251 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -150 \ No newline at end of file +173 \ No newline at end of file diff --git a/example_mods/introMod/_append/data/introText.txt b/example_mods/introMod/data/introText.txt similarity index 100% rename from example_mods/introMod/_append/data/introText.txt rename to example_mods/introMod/data/introText.txt diff --git a/source/Main.hx b/source/Main.hx index 7fd48811..6809e703 100644 --- a/source/Main.hx +++ b/source/Main.hx @@ -94,6 +94,7 @@ class Main extends Sprite FlxG.save.bind('Save'); Highscore.load(); + ModsFolder.init(); ModsFolder.loadMod("introMod"); ModsFolder.currentModFolder = "introMod"; diff --git a/source/funkin/game/Character.hx b/source/funkin/game/Character.hx index d202a665..a8dc2553 100644 --- a/source/funkin/game/Character.hx +++ b/source/funkin/game/Character.hx @@ -611,7 +611,7 @@ class Character extends FlxSprite implements IBeatReceiver implements IOffsetCom playAnim('danceLeft'); default: if (isDanceLeftDanceRight) { - playAnim((danced != danced) ? 'danceLeft' : 'danceRight'); + playAnim((danced = !danced) ? 'danceLeft' : 'danceRight'); } else playAnim('idle'); } diff --git a/source/funkin/mods/ModsAssetLibrary.hx b/source/funkin/mods/ModsAssetLibrary.hx new file mode 100644 index 00000000..6c8cf445 --- /dev/null +++ b/source/funkin/mods/ModsAssetLibrary.hx @@ -0,0 +1,41 @@ +package funkin.mods; + +import sys.FileStat; +import sys.FileSystem; +import openfl.utils.AssetLibrary; +import lime.utils.AssetManifest; + +class ModsAssetLibrary extends AssetLibrary { + private var editedTimes:Map = []; + #if sys + public override function __fromManifest(manifest:AssetManifest) { + super.__fromManifest(manifest); + + var basePath = manifest.rootPath; + if (basePath == null) basePath = ""; + if (basePath != "") basePath += "/"; + + var stat:FileStat; + for(asset in manifest.assets) { + var id = asset.id; + var assetPath = __cacheBreak(basePath + Reflect.field(asset, "path")); + try { + stat = FileSystem.stat(assetPath); + if (stat != null) { + editedTimes.set(id, stat.mtime.getTime()); + } + } catch(e) { + trace(e); + } + } + } + #end + + public function getEditedTime(asset:String):Null { + #if sys + return editedTimes[asset]; + #else + return null; + #end + } +} \ No newline at end of file diff --git a/source/funkin/mods/ModsFolder.hx b/source/funkin/mods/ModsFolder.hx index f591481b..417f8178 100644 --- a/source/funkin/mods/ModsFolder.hx +++ b/source/funkin/mods/ModsFolder.hx @@ -1,14 +1,23 @@ package funkin.mods; +import openfl.utils.Assets; import openfl.utils.AssetManifest; import openfl.utils.AssetLibrary; import sys.FileSystem; import flixel.FlxG; +import flixel.FlxState; import haxe.io.Path; using StringTools; class ModsFolder { + #if sys + /** + * Last time the folder was modified. + */ + public static var lastFolderEditTime:Date = null; + #end + /** * Current mod folder. Will affect `Paths`. */ @@ -22,6 +31,13 @@ class ModsFolder { */ public static var modsPath:String = "./mods/"; + /** + * [Description] Initialises `mods` folder by adding callbacks and such. + */ + public static function init() { + FlxG.signals.preStateCreate.add(onStateSwitch); + } + /** * [Description] Loads a mod with the specified name. * @param modName Name of the mod @@ -30,27 +46,36 @@ class ModsFolder { public static function loadMod(mod:String, ?force:Bool = false) { var libName = 'mods/$mod'.toLowerCase(); - if (openfl.utils.Assets.hasLibrary(libName)) { + if (Assets.hasLibrary(libName)) { if (force) - openfl.utils.Assets.unloadLibrary(libName); + Assets.unloadLibrary(libName); else - return; + checkForOutdatedAssets(cast Assets.getLibrary(libName)); } var assets:AssetManifest = new AssetManifest(); assets.name = libName;// for case sensitive shit & correct linux support - assets.libraryType = null; + assets.libraryType = 'funkin.mods.ModsAssetLibrary'; assets.version = 2; assets.libraryArgs = []; assets.rootPath = '${modsPath}/$mod/'; assets.assets = []; getAssetFiles(assets.assets, '${modsPath}/$mod/', '', libName); - checkForOutdatedAssets(assets); - openfl.utils.Assets.registerLibrary(libName, AssetLibrary.fromManifest(assets)); + var lib = AssetLibrary.fromManifest(assets); + openfl.utils.Assets.registerLibrary(libName, lib); loadedMods.push(mod); } + /** + * [Description] Gets all assets in folders and put them in the `assets` array. + * @param assets Array of assets + * @param rootPath Root path + * @param path Path inside of the rootPath (ex: `root path/path/path2/`) + * @param libraryName Name of the library (ex: `mods/my mod/`) + * @param prefix Prefix for the asset names (ex: `assets/mods/my mod/`) + * @param addRoot Whenever the root should be added to the paths. Defaults to false + */ public static function getAssetFiles(assets:Array, rootPath:String, path:String, libraryName:String, prefix:String = "", addRoot:Bool = false) { for(f in FileSystem.readDirectory('$rootPath$path')) { if (FileSystem.isDirectory('$rootPath$path$f')) { @@ -84,19 +109,35 @@ class ModsFolder { } } - public static function checkForOutdatedAssets(assets:AssetManifest) { + public static function checkForOutdatedAssets(assets:ModsAssetLibrary) { // TODO: Fix this - // for(e in assets.assets) { - // if (e == null) continue; - // if (Reflect.hasField(e, "id") && Reflect.hasField(e, "edited")) { - // var id = '${assets.name}:${e.id}'; - // if (getEditedTime(id) < e.edited) { - // Assets.cache.clear(id); - // @:privateAccess - // FlxG.bitmap.removeKey(id); - // } - // assetEditTimes[id] = e.edited; - // } - // } + #if sys + @:privateAccess + for(asset=>path in assets.paths) { + if (asset == null || path == null) continue; + var editedTime:Null = assets.getEditedTime(asset); + if (editedTime == null) continue; + try { + var stat = FileSystem.stat(path); + if (stat.mtime.getTime() > editedTime) { + // refresh + trace('Refreshing ${asset}'); + Assets.cache.clear(asset); + @:privateAccess + FlxG.bitmap.removeKey(asset); + } + } + } + #end + } + + private static function onStateSwitch(newState:FlxState) { + // TODO: assets reloading + Assets.cache.clear(); + + if (currentModFolder == null) return; + #if sys + loadMod(currentModFolder, false); // hot reloading 🔥 + #end } } \ No newline at end of file