From dbd2219b20fcfc6b6005eda9ad19c0260db7d4d3 Mon Sep 17 00:00:00 2001 From: Ne_Eo Date: Fri, 2 Aug 2024 00:42:47 +0200 Subject: [PATCH] Cleanup + Optimization --- source/flx3d/Flx3DCamera.hx | 2 +- source/flx3d/Flx3DView.hx | 2 +- source/funkin/backend/assets/ModsFolder.hx | 2 +- source/funkin/backend/scripting/Script.hx | 2 +- source/funkin/backend/system/MainState.hx | 10 +- .../backend/system/framerate/Framerate.hx | 2 +- source/funkin/backend/utils/CoolUtil.hx | 5 +- source/funkin/backend/utils/DrawUtil.hx | 10 +- source/funkin/backend/utils/MathUtil.hx | 109 ++++++++++++++++++ source/funkin/editors/stage/StageEditor.hx | 12 +- source/funkin/editors/ui/UISliceSprite.hx | 8 +- .../editors/ui/old/OldUISpliceSprite.hx | 8 +- source/funkin/menus/FreeplayState.hx | 2 +- 13 files changed, 147 insertions(+), 27 deletions(-) create mode 100644 source/funkin/backend/utils/MathUtil.hx diff --git a/source/flx3d/Flx3DCamera.hx b/source/flx3d/Flx3DCamera.hx index 4e1da93d..c2405144 100644 --- a/source/flx3d/Flx3DCamera.hx +++ b/source/flx3d/Flx3DCamera.hx @@ -73,7 +73,7 @@ class Flx3DCamera extends FlxCamera { var context = new AssetLoaderContext(); var noExt = Path.withoutExtension(assetPath); trace(noExt); - context.mapUrlToData('${Path.withoutDirectory(noExt)}.mtl', '$noExt.mtl'); + context.mapUrlToData(Path.withoutDirectory(noExt) + '.mtl', noExt + '.mtl'); var material:TextureMaterial = null; if (texturePath != null) diff --git a/source/flx3d/Flx3DView.hx b/source/flx3d/Flx3DView.hx index e76500c3..1856c507 100644 --- a/source/flx3d/Flx3DView.hx +++ b/source/flx3d/Flx3DView.hx @@ -46,7 +46,7 @@ class Flx3DView extends FlxView3D { var context = new AssetLoaderContext(); var noExt = Path.withoutExtension(assetPath); trace(noExt); - context.mapUrlToData('${Path.withoutDirectory(noExt)}.mtl', '$noExt.mtl'); + context.mapUrlToData(Path.withoutDirectory(noExt) + '.mtl', noExt + '.mtl'); var material:TextureMaterial = null; if (texturePath != null) diff --git a/source/funkin/backend/assets/ModsFolder.hx b/source/funkin/backend/assets/ModsFolder.hx index 9f761b85..68074ea8 100644 --- a/source/funkin/backend/assets/ModsFolder.hx +++ b/source/funkin/backend/assets/ModsFolder.hx @@ -86,7 +86,7 @@ class ModsFolder { var mods:Array = []; #if MOD_SUPPORT for(modFolder in FileSystem.readDirectory(modsPath)) { - if (FileSystem.isDirectory('${modsPath}${modFolder}')) { + if (FileSystem.isDirectory(modsPath + modFolder)) { mods.push(modFolder); } else { var ext = Path.extension(modFolder).toLowerCase(); diff --git a/source/funkin/backend/scripting/Script.hx b/source/funkin/backend/scripting/Script.hx index 1ea7e123..2e8f9f47 100644 --- a/source/funkin/backend/scripting/Script.hx +++ b/source/funkin/backend/scripting/Script.hx @@ -239,7 +239,7 @@ class Script extends FlxBasic implements IFlxDestroyable { if(remappedNames.exists(fileName)) fileName = remappedNames.get(fileName); Logs.traceColored([ - Logs.logText('${fileName}: ', GREEN), + Logs.logText(fileName + ': ', GREEN), Logs.logText(Std.string(v)) ], TRACE); } diff --git a/source/funkin/backend/system/MainState.hx b/source/funkin/backend/system/MainState.hx index adc5ae00..eef71604 100644 --- a/source/funkin/backend/system/MainState.hx +++ b/source/funkin/backend/system/MainState.hx @@ -36,20 +36,20 @@ class MainState extends FlxState { var _highPriorityAddons:Array = []; var _noPriorityAddons:Array = []; if (FileSystem.exists(ModsFolder.addonsPath) && FileSystem.isDirectory(ModsFolder.addonsPath)) { - for(i=>addon in [for(dir in FileSystem.readDirectory(ModsFolder.addonsPath)) if (FileSystem.isDirectory('${ModsFolder.addonsPath}$dir')) dir]) { + for(i=>addon in [for(dir in FileSystem.readDirectory(ModsFolder.addonsPath)) if (FileSystem.isDirectory(ModsFolder.addonsPath + dir)) dir]) { if (addon.startsWith("[LOW]")) _lowPriorityAddons.insert(0, addon); else if (addon.startsWith("[HIGH]")) _highPriorityAddons.insert(0, addon); else _noPriorityAddons.insert(0, addon); } for (addon in _lowPriorityAddons) - Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon', StringTools.ltrim(addon.substr("[LOW]".length)))); + Paths.assetsTree.addLibrary(ModsFolder.loadModLib(ModsFolder.addonsPath + addon, StringTools.ltrim(addon.substr("[LOW]".length)))); } if (ModsFolder.currentModFolder != null) - Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.modsPath}${ModsFolder.currentModFolder}', ModsFolder.currentModFolder)); + Paths.assetsTree.addLibrary(ModsFolder.loadModLib(ModsFolder.modsPath + ModsFolder.currentModFolder, ModsFolder.currentModFolder)); if (FileSystem.exists(ModsFolder.addonsPath) && FileSystem.isDirectory(ModsFolder.addonsPath)){ - for (addon in _noPriorityAddons) Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon', addon)); - for (addon in _highPriorityAddons) Paths.assetsTree.addLibrary(ModsFolder.loadModLib('${ModsFolder.addonsPath}$addon', StringTools.ltrim(addon.substr("[HIGH]".length)))); + for (addon in _noPriorityAddons) Paths.assetsTree.addLibrary(ModsFolder.loadModLib(ModsFolder.addonsPath + addon, addon)); + for (addon in _highPriorityAddons) Paths.assetsTree.addLibrary(ModsFolder.loadModLib(ModsFolder.addonsPath + addon, StringTools.ltrim(addon.substr("[HIGH]".length)))); } #end diff --git a/source/funkin/backend/system/framerate/Framerate.hx b/source/funkin/backend/system/framerate/Framerate.hx index 8faa09ee..2f1f39e8 100644 --- a/source/funkin/backend/system/framerate/Framerate.hx +++ b/source/funkin/backend/system/framerate/Framerate.hx @@ -108,7 +108,7 @@ class Framerate extends Sprite { x = 10 + offset.x; y = 2 + offset.y; - var width = Math.max(fpsCounter.width, #if SHOW_BUILD_ON_FPS Math.max(memoryCounter.width, codenameBuildField.width) #else memoryCounter.width #end) + (x*2); + var width = MathUtil.maxSmart(fpsCounter.width, memoryCounter.width #if SHOW_BUILD_ON_FPS , codenameBuildField.width #end) + (x*2); var height = #if SHOW_BUILD_ON_FPS codenameBuildField.y + codenameBuildField.height #else memoryCounter.y + memoryCounter.height #end; bgSprite.x = -x; bgSprite.y = offset.x; diff --git a/source/funkin/backend/utils/CoolUtil.hx b/source/funkin/backend/utils/CoolUtil.hx index 4d25ce74..a9acf042 100644 --- a/source/funkin/backend/utils/CoolUtil.hx +++ b/source/funkin/backend/utils/CoolUtil.hx @@ -271,7 +271,8 @@ class CoolUtil public static function getSizeString(size:Float):String { var rSize:Float = size; var label:Int = 0; - while(rSize > 1024 && label < sizeLabels.length-1) { + var len = sizeLabels.length; + while(rSize > 1024 && label < len-1) { label++; rSize /= 1024; } @@ -711,7 +712,7 @@ class CoolUtil * Gets the macro class created by hscript-improved for an abstract / enum */ @:noUsing public static inline function getMacroAbstractClass(className:String) { - return Type.resolveClass('${className}_HSC'); + return Type.resolveClass(className + '_HSC'); } /** diff --git a/source/funkin/backend/utils/DrawUtil.hx b/source/funkin/backend/utils/DrawUtil.hx index d2f158f3..1d0bc481 100644 --- a/source/funkin/backend/utils/DrawUtil.hx +++ b/source/funkin/backend/utils/DrawUtil.hx @@ -56,7 +56,13 @@ class DrawUtil { } public static function destroyDrawers() { - dot.destroy(); line.destroy(); - dot = null; line = null; + if(dot != null) { + dot.destroy(); + dot = null; + } + if(line != null) { + line.destroy(); + line = null; + } } } \ No newline at end of file diff --git a/source/funkin/backend/utils/MathUtil.hx b/source/funkin/backend/utils/MathUtil.hx new file mode 100644 index 00000000..46fe3559 --- /dev/null +++ b/source/funkin/backend/utils/MathUtil.hx @@ -0,0 +1,109 @@ +package funkin.backend.utils; + +import haxe.macro.Expr; + +class MathUtil { + public static function maxInt(...args:Int):Int { + var max = args[0]; + for(i in 1...args.length) { + var arg = args[i]; + if(arg > max) + max = arg; + } + return max; + } + + public static function minInt(...args:Int):Int { + var min = args[0]; + for(i in 1...args.length) { + var arg = args[i]; + if(arg < min) + min = arg; + } + return min; + } + + public static function maxFloat(...args:Float):Float { + var max = args[0]; + for(i in 1...args.length) { + var arg = args[i]; + if(arg > max) + max = arg; + } + return max; + } + + public static function minFloat(...args:Float):Float { + var min = args[0]; + for(i in 1...args.length) { + var arg = args[i]; + if(arg < min) + min = arg; + } + return min; + } + + /** + * Shortcut to `Math.max` but with infinite amount of arguments + * Uses `CoolUtil.maxInt` if theres only int arguments + * + * Might not preserve the order of arguments, please test this. + * + * Dont use this in hscript, it doesnt work, it only works on compile time + **/ + @:dox(hide) public static macro function maxSmart(..._args:Expr):Expr { + return genericMinMaxSmart(_args.toArray(), true); + } + + /** + * Shortcut to `Math.min` but with infinite amount of arguments + * Uses `CoolUtil.minInt` if theres only int arguments + * + * Might not preserve the order of arguments, please test this. + * + * Dont use this in hscript, it doesnt work, it only works on compile time + **/ + @:dox(hide) public static macro function minSmart(..._args:Expr):Expr { + return genericMinMaxSmart(_args.toArray(), false); + } + + #if macro + @:dox(hide) private static function genericMinMaxSmart(_args:Array, isMax:Bool):Expr { + var args = _args.copy(); + if (args.length == 0) return macro 0; + + var isFloat = true; + var isInt = true; + + for(arg in args) { + if(isInt) + isInt = arg.expr.match(EConst(CInt(_))); + if(isFloat) + isFloat = arg.expr.match(EConst(CFloat(_))); + } + + var funcPath = isInt ? "funkin.backend.utils.CoolUtil.maxInt" : "Math.max"; + if (!isMax) funcPath = isInt ? "funkin.backend.utils.CoolUtil.minInt" : "Math.min"; + + var func = funcPath.split("."); + + function nested(lst:Array):Expr { + if (lst.length == 1) { + return macro ${lst[0]}; + } else if (lst.length == 2) { + return macro $p{func}(${lst[0]}, ${lst[1]}); + } else { + var mid = Std.int(lst.length / 2); + return macro $p{func}(${nested(lst.slice(0, mid))}, ${nested(lst.slice(mid, lst.length))}); + } + } + + var expr = nested(args); + + //var printer = new haxe.macro.Printer(); + //trace(printer.printExpr(expr)); + + return macro $expr; + } + #end +} \ No newline at end of file diff --git a/source/funkin/editors/stage/StageEditor.hx b/source/funkin/editors/stage/StageEditor.hx index b47b2819..090be4e3 100644 --- a/source/funkin/editors/stage/StageEditor.hx +++ b/source/funkin/editors/stage/StageEditor.hx @@ -1038,10 +1038,14 @@ class StageEditor extends UIState { if(sprite is FunkinSprite) { var sprite:FunkinSprite = cast sprite; - var maxX = Math.max(Math.max(corners[0].x, corners[1].x), Math.max(corners[2].x, corners[3].x)); - var maxY = Math.max(Math.max(corners[0].y, corners[1].y), Math.max(corners[2].y, corners[3].y)); - var minX = Math.min(Math.min(corners[0].x, corners[1].x), Math.min(corners[2].x, corners[3].x)); - var minY = Math.min(Math.min(corners[0].y, corners[1].y), Math.min(corners[2].y, corners[3].y)); + var corner0 = corners[0]; + var corner1 = corners[1]; + var corner2 = corners[2]; + var corner3 = corners[3]; + var maxX = MathUtil.maxSmart(corner0.x, corner1.x, corner2.x, corner3.x); + var maxY = MathUtil.maxSmart(corner0.y, corner1.y, corner2.y, corner3.y); + var minX = MathUtil.minSmart(corner0.x, corner1.x, corner2.x, corner3.x); + var minY = MathUtil.minSmart(corner0.y, corner1.y, corner2.y, corner3.y); if(!sprite.extra.exists(exID("bounds"))) { sprite.extra.set(exID("bounds"), new FlxRect()); diff --git a/source/funkin/editors/ui/UISliceSprite.hx b/source/funkin/editors/ui/UISliceSprite.hx index 5374c4d7..24a4d560 100644 --- a/source/funkin/editors/ui/UISliceSprite.hx +++ b/source/funkin/editors/ui/UISliceSprite.hx @@ -109,10 +109,10 @@ class UISliceSprite extends UISprite { bottom = frames.frames[framesOffset + 7]; bottomright = frames.frames[framesOffset + 8]; - leftWidth = Std.int(Math.max(topleft.frame.width, Math.max(middleleft.frame.width, bottomleft.frame.width))); - rightWidth = Std.int(Math.max(topright.frame.width, Math.max(middleright.frame.width, bottomright.frame.width))); - topHeight = Std.int(Math.max(topleft.frame.height, Math.max(top.frame.height, topright.frame.height))); - bottomHeight = Std.int(Math.max(topleft.frame.height, Math.max(top.frame.height, topright.frame.height))); + leftWidth = Std.int(MathUtil.maxSmart(topleft.frame.width, middleleft.frame.width, bottomleft.frame.width)); + rightWidth = Std.int(MathUtil.maxSmart(topright.frame.width, middleright.frame.width, bottomright.frame.width)); + topHeight = Std.int(MathUtil.maxSmart(topleft.frame.height, top.frame.height, topright.frame.height)); + bottomHeight = Std.int(MathUtil.maxSmart(topleft.frame.height, top.frame.height, topright.frame.height)); __meshDirty = true; } diff --git a/source/funkin/editors/ui/old/OldUISpliceSprite.hx b/source/funkin/editors/ui/old/OldUISpliceSprite.hx index fb01c80d..67d24fd7 100644 --- a/source/funkin/editors/ui/old/OldUISpliceSprite.hx +++ b/source/funkin/editors/ui/old/OldUISpliceSprite.hx @@ -80,10 +80,10 @@ class OldUISliceSprite extends UISprite { bottom = frames.frames[framesOffset + 7]; bottomright = frames.frames[framesOffset + 8]; - leftWidth = Std.int(Math.max(topleft.frame.width, Math.max(middleleft.frame.width, bottomleft.frame.width))); - rightWidth = Std.int(Math.max(topright.frame.width, Math.max(middleright.frame.width, bottomright.frame.width))); - topHeight = Std.int(Math.max(topleft.frame.height, Math.max(top.frame.height, topright.frame.height))); - bottomHeight = Std.int(Math.max(topleft.frame.height, Math.max(top.frame.height, topright.frame.height))); + leftWidth = Std.int(MathUtil.maxSmart(topleft.frame.width, middleleft.frame.width, bottomleft.frame.width)); + rightWidth = Std.int(MathUtil.maxSmart(topright.frame.width, middleright.frame.width, bottomright.frame.width)); + topHeight = Std.int(MathUtil.maxSmart(topleft.frame.height, top.frame.height, topright.frame.height)); + bottomHeight = Std.int(MathUtil.maxSmart(topleft.frame.height, top.frame.height, topright.frame.height)); } public override function draw() @:privateAccess { diff --git a/source/funkin/menus/FreeplayState.hx b/source/funkin/menus/FreeplayState.hx index 7eeef342..24e93b4f 100644 --- a/source/funkin/menus/FreeplayState.hx +++ b/source/funkin/menus/FreeplayState.hx @@ -221,7 +221,7 @@ class FreeplayState extends MusicBeatState } scoreText.text = "PERSONAL BEST:" + lerpScore; - scoreBG.scale.set(Math.max(Math.max(diffText.width, scoreText.width), coopText.width) + 8, (coopText.visible ? coopText.y + coopText.height : 66)); + scoreBG.scale.set(MathUtil.maxSmart(diffText.width, scoreText.width, coopText.width) + 8, (coopText.visible ? coopText.y + coopText.height : 66)); scoreBG.updateHitbox(); scoreBG.x = FlxG.width - scoreBG.width;