From f34c274326654211282ca4d423f5ca80405547ca Mon Sep 17 00:00:00 2001 From: Ralty <78720179+Raltyro@users.noreply.github.com> Date: Sat, 19 Jul 2025 22:51:27 +0700 Subject: [PATCH] IniUtil Section Supports, uh whatever --- source/funkin/backend/system/Flags.hx | 31 +++---------- source/funkin/backend/utils/CoolUtil.hx | 34 ++++++-------- source/funkin/backend/utils/IniUtil.hx | 54 ++++++++++------------- source/funkin/editors/ModConfigWarning.hx | 5 ++- 4 files changed, 46 insertions(+), 78 deletions(-) diff --git a/source/funkin/backend/system/Flags.hx b/source/funkin/backend/system/Flags.hx index e525a2e2..934919f1 100644 --- a/source/funkin/backend/system/Flags.hx +++ b/source/funkin/backend/system/Flags.hx @@ -5,6 +5,7 @@ import funkin.backend.assets.ModsFolder; import funkin.backend.assets.IModsAssetLibrary; import funkin.backend.assets.ScriptedAssetLibrary; import funkin.backend.system.macros.GitCommitMacro; +import funkin.backend.utils.IniUtil; import lime.app.Application; import lime.utils.AssetLibrary as LimeAssetLibrary; import lime.utils.AssetType; @@ -21,7 +22,7 @@ class Flags { public static var MOD_NAME:String = ""; public static var MOD_DESCRIPTION:String = ""; public static var MOD_API_VERSION:Int = 1; - public static var MOD_DOWNLOADED_LINK:String = ""; + public static var MOD_DOWNLOAD_LINK:String = ""; public static var MOD_DEPENDENCIES:Array = []; public static var MOD_ICON64:String = ""; @@ -179,29 +180,9 @@ class Flags { @:bypass public static var customFlags:Map = []; public static function loadFromData(flags:Map, data:String) { - var trimmed:String; - var splitContent = [for(e in data.split("\n")) if ((trimmed = e.trim()) != "") trimmed]; + var res = IniUtil.parseString(data); - for(line in splitContent) { - if(line.startsWith(";")) continue; - if(line.startsWith("#")) continue; - if(line.startsWith("//")) continue; - if(line.length == 0) continue; - if(line.charAt(0) == "[" && line.charAt(line.length-1) == "]") continue; - - var index = line.indexOf("="); - if(index == -1) continue; - var name = line.substr(0, index).trim(); - var value = line.substr(index+1).trim(); - - var wasQuoted = value.length > 1 && value.charCodeAt(0) == '"'.code && value.charCodeAt(value.length-1) == '"'.code; - if(wasQuoted) value = value.substr(1, value.length - 2); - if((!wasQuoted && value.length == 0) || name.length == 0) - continue; - - if(!flags.exists(name)) - flags[name] = value; - } + for (section in res) for (key => value in section) flags[key] = value; } public static function loadFromDatas(datas:Array) { @@ -238,8 +219,8 @@ class Flags { if (l is IModsAssetLibrary) { var flagsTxt = ""; - if (l.exists(Paths.getPath("data/config/modpack.ini"), AssetType.TEXT)) - flagsTxt = l.getAsset(Paths.getPath("data/config/modpack.ini"), AssetType.TEXT); + if (l.exists(Paths.ini("config/modpack"), AssetType.TEXT)) + flagsTxt = l.getAsset(Paths.ini("config/modpack"), AssetType.TEXT); if (cast(l, IModsAssetLibrary).modName == "assets") continue; if (cast(l, IModsAssetLibrary).modName == ModsFolder.currentModFolder) { diff --git a/source/funkin/backend/utils/CoolUtil.hx b/source/funkin/backend/utils/CoolUtil.hx index b72003c0..82de2d1c 100644 --- a/source/funkin/backend/utils/CoolUtil.hx +++ b/source/funkin/backend/utils/CoolUtil.hx @@ -421,30 +421,22 @@ class CoolUtil FlxG.sound.music.persist = Persist; } - var infoPath = '${Path.withoutExtension(path)}.ini'; - if (Assets.exists(infoPath)) { - var musicInfo = IniUtil.parseAsset(infoPath, [ - "BPM" => null, - "TimeSignature" => Flags.DEFAULT_BEATS_PER_MEASURE + "/" + Flags.DEFAULT_STEPS_PER_BEAT, - "LoopTime" => '${Flags.DEFAULT_LOOP_TIME}' - ]); + var iniPath = '${Path.withoutExtension(path)}.ini'; + var musicData = Assets.exists(iniPath) ? IniUtil.parseAsset(iniPath)["Global"] : null; + if (musicData != null) { + if (musicData["LoopTime"] != null) FlxG.sound.music.loopTime = Std.parseFloat(musicData["LoopTime"]) * 1000; + var timeSignParsed:Array> = musicData["TimeSignature"] == null ? [] : [for(s in musicData["TimeSignature"].split("/")) Std.parseFloat(s)]; + var beatsPerMeasure:Float = Flags.DEFAULT_BEATS_PER_MEASURE, stepsPerBeat:Float = Flags.DEFAULT_STEPS_PER_BEAT; - if (FlxG.sound.music != null) - FlxG.sound.music.loopTime = Std.parseFloat(musicInfo["LoopTime"]) * 1000; - - var timeSignParsed:Array> = musicInfo["TimeSignature"] == null ? [] : [for(s in musicInfo["TimeSignature"].split("/")) Std.parseFloat(s)]; - var beatsPerMeasure:Float = Flags.DEFAULT_BEATS_PER_MEASURE; - var stepsPerBeat:Int = Flags.DEFAULT_STEPS_PER_BEAT; - - // Check later, i don't think timeSignParsed can contain null, only nan - if (timeSignParsed.length == 2 && !timeSignParsed.contains(null)) { - beatsPerMeasure = timeSignParsed[0] == null || timeSignParsed[0] <= 0 ? Flags.DEFAULT_BEATS_PER_MEASURE : cast timeSignParsed[0]; - stepsPerBeat = timeSignParsed[1] == null || timeSignParsed[1] <= 0 ? Flags.DEFAULT_STEPS_PER_BEAT : cast timeSignParsed[1]; + if (timeSignParsed.length == 2) { + beatsPerMeasure = Math.isNaN(timeSignParsed[0]) ? Flags.DEFAULT_BEATS_PER_MEASURE : timeSignParsed[0]; + stepsPerBeat = Math.isNaN(timeSignParsed[1]) ? Flags.DEFAULT_STEPS_PER_BEAT : (16 / timeSignParsed[1]); // from denominator } - var bpm:Null = Std.parseFloat(musicInfo["BPM"]).getDefault(DefaultBPM); - Conductor.changeBPM(bpm, beatsPerMeasure, stepsPerBeat); - } else + var bpm:Float = Std.parseFloat(musicData["BPM"]).getDefault(DefaultBPM); + Conductor.changeBPM(bpm, beatsPerMeasure, Math.floor(stepsPerBeat)); + } + else Conductor.changeBPM(DefaultBPM); } diff --git a/source/funkin/backend/utils/IniUtil.hx b/source/funkin/backend/utils/IniUtil.hx index 5de61303..22cecee4 100644 --- a/source/funkin/backend/utils/IniUtil.hx +++ b/source/funkin/backend/utils/IniUtil.hx @@ -1,39 +1,33 @@ package funkin.backend.utils; -/** - * DOESN'T SUPPORT CATEGORIES YET!! - */ class IniUtil { - public static inline function parseAsset(assetPath:String, ?defaultVariables:Map) - return parseString(Assets.getText(assetPath), defaultVariables); + public static inline function parseAsset(assetPath:String) + return parseString(Assets.getText(assetPath)); - public static function parseString(data:String, ?defaultVariables:Map) { - var trimmed:String; - var splitContent = [for(e in data.split("\n")) if ((trimmed = e.trim()) != "") trimmed]; + public static function parseString(data:String):Map> { + var res:Map> = []; + parseStringToMap(res, data); + return res; + } - var finalMap:Map = []; - if (defaultVariables != null) - for(k=>e in defaultVariables) - finalMap[k] = e; + public static function parseStringToMap(map:Map>, data:String) { + var section:Map = null; - for(line in splitContent) { - // comment - if (line.startsWith(";")) continue; - // categories; not supported yet - if (line.startsWith("[") && line.endsWith("]")) continue; + var regexSec = ~/\\[(.+)\\]/g, regexVal = ~/(.+)=(.+)/g, quote = ~/[\\'"](.+)[\\'"]/g, iln = 0; + do { + var line = data.substring(iln, ((iln = data.indexOf('\n', iln) + 1) == 0 ? data.length : iln - 1)); + if (regexSec.match(line)) { + if ((section = map.get(regexSec.matched(1))) == null) map.set(regexSec.matched(1), section = []); + } + else if (regexVal.match(line)) { + var s = regexVal.matched(2).trim(); + if (quote.match(s)) s = quote.matched(1); - var index = line.indexOf("="); - var name = line.substr(0, index).trim(); - var value = line.substr(index+1).trim(); - - if (value.length > 1 && value.charCodeAt(0) == '"'.code && value.charCodeAt(value.length-1) == '"'.code) - value = value.substr(1, value.length - 2); - - if (value.length == 0 || name.length == 0) - continue; - - finalMap[name] = value; - } - return finalMap; + if (section == null) { + if ((section = map.get("Global")) == null) map.set("Global", section = []); + } + section.set(regexVal.matched(1).trim(), s); + } + } while (iln != 0); } } \ No newline at end of file diff --git a/source/funkin/editors/ModConfigWarning.hx b/source/funkin/editors/ModConfigWarning.hx index 6e2c4bac..ca1fc34f 100644 --- a/source/funkin/editors/ModConfigWarning.hx +++ b/source/funkin/editors/ModConfigWarning.hx @@ -17,7 +17,7 @@ MOD_DESCRIPTION="YOUR MOD DESCRIPTION HERE" # DO NOT EDIT!! this is used to check for version compatibility! MOD_API_VERSION=1 -MOD_DOWNLOADED_LINK="YOUR MOD PAGE LINK HERE" +MOD_DOWNLOAD_LINK="YOUR MOD PAGE LINK HERE" # Not supported yet ;MOD_ICON64="path/to/icon64.png" @@ -30,7 +30,8 @@ DISABLE_BETA_WARNING_SCREEN=true [Discord] MOD_DISCORD_CLIENT_ID="" -MOD_DISCORD_LOGO_KEY=""'; +MOD_DISCORD_LOGO_KEY="" +MOD_DISCORD_LOGO_TEXT=""'; public function new(library:ModsFolderLibrary, ?goToState:Class) { super();