diff --git a/source/funkin/menus/TitleState.hx b/source/funkin/menus/TitleState.hx index f4f26fae..6b43c02b 100644 --- a/source/funkin/menus/TitleState.hx +++ b/source/funkin/menus/TitleState.hx @@ -28,6 +28,7 @@ import haxe.xml.Access; using StringTools; @:allow(funkin.mods.ModsFolder) +@:allow(funkin.system.MainState) class TitleState extends MusicBeatState { static var initialized:Bool = false; diff --git a/source/funkin/mods/ModsFolder.hx b/source/funkin/mods/ModsFolder.hx index 4783a930..be212b94 100644 --- a/source/funkin/mods/ModsFolder.hx +++ b/source/funkin/mods/ModsFolder.hx @@ -1,5 +1,6 @@ package funkin.mods; +import funkin.system.MainState; import funkin.menus.TitleState; import funkin.system.Main; import openfl.utils.AssetCache; @@ -28,15 +29,19 @@ class ModsFolder { * Current mod folder. Will affect `Paths`. */ public static var currentModFolder:String = null; - /** - * Map all loaded mods' folder libraries. - */ - public static var loadedMods:Map = []; /** * Path to the `mods` folder. */ public static var modsPath:String = "./mods/"; + /** + * Path to the `addons` folder. + */ + public static var addonsPath:String = "./addons/"; + /** + * Whenever its the first time mods has been reloaded. + */ + private static var __firstTime:Bool = true; /** * Initialises `mods` folder by adding callbacks and such. */ @@ -44,56 +49,36 @@ class ModsFolder { } - /** - * Loads a mod with the specified name. - * @param modName Name of the mod - * @param force Whenever the mod should be reloaded if it has already been loaded - */ - public static function loadMod(mod:String, force:Bool = false) { - #if MOD_SUPPORT - if (mod == null) return null; // may be loading base game - - if (FileSystem.exists('${modsPath}$mod.zip')) - return loadedMods[mod] = loadLibraryFromZip('mods/$mod'.toLowerCase(), '${modsPath}$mod.zip', force); - else - return loadedMods[mod] = loadLibraryFromFolder('mods/$mod'.toLowerCase(), '${modsPath}$mod', force); - - #else - return null; - #end - } - /** * Switches mod - unloads all the other mods, then load this one. * @param libName */ public static function switchMod(mod:String) { - unloadMod(ModsFolder.currentModFolder); - Options.lastLoadedMod = ModsFolder.currentModFolder = mod; - Options.save(); - if (ModsFolder.currentModFolder == null) return; - - Paths.assetsTree.addLibrary(loadMod(ModsFolder.currentModFolder)); - - Main.refreshAssets(); - onModSwitch.dispatch(ModsFolder.currentModFolder); - if (FlxG.sound.music != null && FlxG.sound.music.playing) - FlxG.sound.music.fadeOut(0.25, 0, function(t) { - FlxG.sound.music.stop(); - }); - DiscordUtil.reloadJsonData(); - TitleState.initialized = false; - FlxG.switchState(new TitleState()); + Options.lastLoadedMod = currentModFolder = mod; + reloadMods(); } - public static function unloadMod(mod:String) { - if (mod == null) return; - - Paths.assetsTree.clearCache(); - FlxG.bitmap.reset(); - Paths.assetsTree.removeLibrary(loadedMods[mod]); + public static function reloadMods() { + if (!__firstTime) + FlxG.switchState(new MainState()); + __firstTime = false; + } - loadedMods[mod] = null; + /** + * Loads a mod library from the specified path. Supports folders and zips. + * @param modName Name of the mod + * @param force Whenever the mod should be reloaded if it has already been loaded + */ + public static function loadModLib(path:String, force:Bool = false) { + #if MOD_SUPPORT + if (FileSystem.exists('$path.zip')) + return loadLibraryFromZip('$path'.toLowerCase(), '$path.zip', force); + else + return loadLibraryFromFolder('$path'.toLowerCase(), '$path', force); + + #else + return null; + #end } public static function prepareLibrary(libName:String, force:Bool = false) { diff --git a/source/funkin/system/AssetsLibraryList.hx b/source/funkin/system/AssetsLibraryList.hx index c94baca3..a8be955e 100644 --- a/source/funkin/system/AssetsLibraryList.hx +++ b/source/funkin/system/AssetsLibraryList.hx @@ -5,9 +5,11 @@ import lime.utils.AssetLibrary; class AssetsLibraryList extends AssetLibrary { public var libraries:Array = []; - public var base:AssetLibrary; - public var sourceLibsAmount:Int = 0; + @:allow(funkin.system.Main) + @:allow(funkin.system.MainState) + private var __defaultLibraries:Array = []; + public var base:AssetLibrary; public function removeLibrary(lib:AssetLibrary) { if (lib == null) return lib; @@ -118,40 +120,13 @@ class AssetsLibraryList extends AssetLibrary { private function shouldSkipLib(k:Int, source:AssetSource) { return switch(source) { case BOTH: false; - case SOURCE: k < libraries.length - sourceLibsAmount; - case MODS: k >= libraries.length - sourceLibsAmount; + 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 function clearCache() { - var libs:Array = [for(lib in libraries) lib]; - libs.push(this); - for(l in libs) { - var lib:AssetLibrary = l; - if (lib is openfl.utils.AssetLibrary) { - var openflLib = cast(lib, openfl.utils.AssetLibrary); - @:privateAccess - if (openflLib.__proxy != null) lib = openflLib.__proxy; - } - - @:privateAccess var cachedAudioBuffers = lib.cachedAudioBuffers; - @:privateAccess var cachedBytes = lib.cachedBytes; - @:privateAccess var cachedFonts = lib.cachedFonts; - @:privateAccess var cachedImages = lib.cachedImages; - @:privateAccess var cachedText = lib.cachedText; - - - for(buff in cachedAudioBuffers) buff.dispose(); - cachedAudioBuffers.clear(); - - cachedBytes.clear(); - cachedFonts.clear(); - cachedText.clear(); - } - } - public override function isLocal(id:String, type:String) { return true; } @@ -161,7 +136,19 @@ class AssetsLibraryList extends AssetLibrary { if (base == null) base = Assets.getLibrary("default"); addLibrary(this.base = base); - sourceLibsAmount++; + __defaultLibraries.push(base); + } + + public function reset() { + for(l in libraries) + if (!__defaultLibraries.contains(l)) + l.unload(); + + libraries = []; + + // adds default libraries in again + for(d in __defaultLibraries) + addLibrary(d); } public function addLibrary(lib:AssetLibrary) { diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index b898e344..8793aa15 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -102,13 +102,9 @@ class Main extends Sprite } - addChild(new FunkinGame(gameWidth, gameHeight, null, zoom, framerate, framerate, skipSplash, startFullscreen)); - loadGameSettings(); - // FlxG.switchState(new TitleState()); - FlxG.switchState(new funkin.menus.BetaWarningState()); + addChild(new FunkinGame(gameWidth, gameHeight, MainState, zoom, framerate, framerate, skipSplash, startFullscreen)); #if !mobile - // addChild(new FramerateField(10, 3, 0xFFFFFF)); addChild(new funkin.system.framerate.Framerate()); SystemInfo.init(); #end @@ -144,7 +140,7 @@ class Main extends Sprite return time = Lib.getTimer(); } - public function loadGameSettings() { + public static function loadGameSettings() { MemoryUtil.init(); @:privateAccess FlxG.game.getTimer = getTimer; @@ -177,13 +173,11 @@ class Main extends Sprite #elseif linux trace("Used lime test linux. Switching into source assets."); #end - Paths.assetsTree.addLibrary(ModsFolder.loadLibraryFromFolder('assets', './${pathBack}assets/', true)); - Paths.assetsTree.sourceLibsAmount++; + Paths.assetsTree.__defaultLibraries.push(ModsFolder.loadLibraryFromFolder('assets', './${pathBack}assets/', true)); #end } else { #if USE_ADAPTED_ASSETS - Paths.assetsTree.addLibrary(ModsFolder.loadLibraryFromFolder('assets', './assets/', true)); - Paths.assetsTree.sourceLibsAmount++; + Paths.assetsTree.__defaultLibraries.push(ModsFolder.loadLibraryFromFolder('assets', './assets/', true)); #end } #end @@ -201,7 +195,6 @@ class Main extends Sprite FlxG.fixedTimestep = false; - FlxG.scaleMode = scaleMode = new FunkinRatioScaleMode(); Conductor.init(); @@ -215,6 +208,7 @@ class Main extends Sprite #if MOD_SUPPORT ModsFolder.switchMod(modToLoad.getDefault(Options.lastLoadedMod)); #end + initTransition(); } @@ -253,8 +247,6 @@ class Main extends Sprite openfl.display3D.utils.UInt8Buff._pools.clear(); } - Paths.assetsTree.clearCache(); - MemoryUtil.clearMajor(); } } diff --git a/source/funkin/system/MainState.hx b/source/funkin/system/MainState.hx new file mode 100644 index 00000000..e55c3fe7 --- /dev/null +++ b/source/funkin/system/MainState.hx @@ -0,0 +1,50 @@ +package funkin.system; + +import sys.FileSystem; +import funkin.mods.ModsFolder; +import funkin.menus.TitleState; +import funkin.menus.BetaWarningState; +import flixel.FlxState; + +/** + * Simple state used for loading the game + */ +class MainState extends FlxState { + public static var initiated:Bool = false; + public static var betaWarningShown:Bool = false; + public override function create() { + super.create(); + if (!initiated) + Main.loadGameSettings(); + initiated = true; + + Options.save(); + + FlxG.bitmap.reset(); + FlxG.sound.destroy(true); + + Paths.assetsTree.reset(); + + #if MOD_SUPPORT + if (ModsFolder.currentModFolder != null) + Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.modsPath}${ModsFolder.currentModFolder}')); + + if (FileSystem.exists(ModsFolder.addonsPath) && FileSystem.isDirectory(ModsFolder.addonsPath)) + for(addon in [for(dir in FileSystem.readDirectory(ModsFolder.addonsPath)) if (FileSystem.isDirectory('${ModsFolder.addonsPath}$dir')) dir]) + Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon')); + #end + + Main.refreshAssets(); + ModsFolder.onModSwitch.dispatch(ModsFolder.currentModFolder); + DiscordUtil.reloadJsonData(); + TitleState.initialized = false; + + + if (betaWarningShown) + FlxG.switchState(new TitleState()); + else { + FlxG.switchState(new BetaWarningState()); + betaWarningShown = true; + } + } +} \ No newline at end of file diff --git a/source/funkin/system/OptimizedBitmapData.hx b/source/funkin/system/OptimizedBitmapData.hx index 98270d2a..cfcf6b12 100644 --- a/source/funkin/system/OptimizedBitmapData.hx +++ b/source/funkin/system/OptimizedBitmapData.hx @@ -35,6 +35,8 @@ class OptimizedBitmapData extends BitmapData { readable = true; this.image = null; + + MemoryUtil.clearMinor(); } #end } diff --git a/source/funkin/system/framerate/FlixelInfo.hx b/source/funkin/system/framerate/FlixelInfo.hx index 6f423c81..4f2bc63b 100644 --- a/source/funkin/system/framerate/FlixelInfo.hx +++ b/source/funkin/system/framerate/FlixelInfo.hx @@ -7,11 +7,18 @@ class FlixelInfo extends FramerateCategory { public override function __enterFrame(t:Int) { if (alpha <= 0.05) return; - _text = 'State: ${Type.getClassName(Type.getClass(FlxG.state))}'; - _text += '\nObject Count: ${FlxG.state.members.length}'; - _text += '\nCamera Count: ${FlxG.cameras.list.length}'; - @:privateAccess - _text += '\nFlxG.game Childs Count: ${FlxG.game.numChildren}'; + @:privateAccess { + var c:Int = 0; + for(_ in FlxG.bitmap._cache.keys()) + c++; + + _text = 'State: ${Type.getClassName(Type.getClass(FlxG.state))}'; + _text += '\nObject Count: ${FlxG.state.members.length}'; + _text += '\nCamera Count: ${FlxG.cameras.list.length}'; + _text += '\nBitmaps Count: ${c}'; + _text += '\nSounds Count: ${FlxG.sound.list.length}'; + _text += '\nFlxG.game Childs Count: ${FlxG.game.numChildren}'; + } this.text.text = _text; super.__enterFrame(t);