From 59164cbb1dff6e993e915028d80bd1ca207e403e Mon Sep 17 00:00:00 2001 From: NeeEoo Date: Fri, 12 Apr 2024 12:37:17 +0200 Subject: [PATCH] Cleanup --- .../backend/assets/AssetsLibraryList.hx | 2 +- .../funkin/backend/assets/ZipFolderLibrary.hx | 3 +- source/funkin/backend/system/Controls.hx | 13 ++----- source/funkin/backend/utils/CoolUtil.hx | 34 +++++++++++++++++-- source/funkin/editors/charter/Charter.hx | 2 +- .../editors/charter/CharterNoteTypesList.hx | 2 +- .../editors/charter/CharterStrumlineScreen.hx | 9 +++-- .../editors/ui/UIAutoCompleteTextBox.hx | 1 + source/funkin/game/Note.hx | 5 ++- source/funkin/game/PlayState.hx | 2 +- source/funkin/game/Stage.hx | 2 +- 11 files changed, 50 insertions(+), 25 deletions(-) diff --git a/source/funkin/backend/assets/AssetsLibraryList.hx b/source/funkin/backend/assets/AssetsLibraryList.hx index 49447997..d7ff2737 100644 --- a/source/funkin/backend/assets/AssetsLibraryList.hx +++ b/source/funkin/backend/assets/AssetsLibraryList.hx @@ -87,7 +87,7 @@ class AssetsLibraryList extends AssetLibrary { if (l is IModsAssetLibrary) { var lib = cast(l, IModsAssetLibrary); for(e in lib.getFolders(folder)) - content.push(e); + content.pushOnce(e); } #end } diff --git a/source/funkin/backend/assets/ZipFolderLibrary.hx b/source/funkin/backend/assets/ZipFolderLibrary.hx index 736cb3ea..805d05fc 100644 --- a/source/funkin/backend/assets/ZipFolderLibrary.hx +++ b/source/funkin/backend/assets/ZipFolderLibrary.hx @@ -140,8 +140,7 @@ class ZipFolderLibrary extends AssetLibrary implements IModsAssetLibrary { var fileName = k.substr(_parsedAsset.length); if (fileName.contains("/")) { var s = fileName.split("/")[0]; - if (!content.contains(s)) - content.push(s); + content.pushOnce(s); } } } diff --git a/source/funkin/backend/system/Controls.hx b/source/funkin/backend/system/Controls.hx index e70648cb..d1a11142 100644 --- a/source/funkin/backend/system/Controls.hx +++ b/source/funkin/backend/system/Controls.hx @@ -576,15 +576,8 @@ class Controls extends FlxActionSet { case null: // add all - #if (haxe >= "4.0.0") for (gamepad in controls.gamepadsAdded) - if (!gamepadsAdded.contains(gamepad)) - gamepadsAdded.push(gamepad); - #else - for (gamepad in controls.gamepadsAdded) - if (gamepadsAdded.indexOf(gamepad) == -1) - gamepadsAdded.push(gamepad); - #end + gamepadsAdded.pushOnce(gamepad); mergeKeyboardScheme(controls.keyboardScheme); @@ -652,7 +645,7 @@ class Controls extends FlxActionSet while (i-- > 0) { var input = action.inputs[i]; - if (input.device == KEYBOARD && keys.indexOf(cast input.inputID) != -1) + if (input.device == KEYBOARD && keys.contains(cast input.inputID)) action.remove(input); } } @@ -837,7 +830,7 @@ class Controls extends FlxActionSet while (i-- > 0) { var input = action.inputs[i]; - if (isGamepad(input, gamepadID) && buttons.indexOf(cast input.inputID) != -1) + if (isGamepad(input, gamepadID) && buttons.contains(cast input.inputID)) action.remove(input); } } diff --git a/source/funkin/backend/utils/CoolUtil.hx b/source/funkin/backend/utils/CoolUtil.hx index c441ebe0..36f817e8 100644 --- a/source/funkin/backend/utils/CoolUtil.hx +++ b/source/funkin/backend/utils/CoolUtil.hx @@ -605,13 +605,14 @@ class CoolUtil * @return Index, or -1 if unsuccessful. */ public static inline function indexOfFromLast(array:Array, element:T):Int { - var i = array.length - 1; + /*var i = array.length - 1; while(i >= 0) { if (array[i] == element) break; i--; } - return i; + return i;*/ + return array.lastIndexOf(element); } /** @@ -703,6 +704,35 @@ class CoolUtil @:noUsing public static inline function flxeaseFromString(mainEase:String, suffix:String) return Reflect.field(FlxEase, mainEase + (mainEase == "linear" ? "" : suffix)); + + public static function sortAlphabetically(array:Array, ?lowercase:Bool=false) { + array.sort(function(a1, a2):Int { + if(lowercase) { + a1 = a1.toLowerCase(); + a2 = a2.toLowerCase(); + } + if (a1 < a2) return -1; + if (a1 > a2) return 1; + return 0; + }); + return array; + } + + public static inline function pushOnce(array:Array, element:T) { + #if (haxe >= "4.0.0") + if (!array.contains(element)) + array.push(element); + #else + if (array.indexOf(element) == -1) + array.push(element); + #end + } + + #if !(haxe >= "4.0.0") + public static inline function contains(array:Array, element:T) { + return array.indexOf(element) != -1; + } + #end } /** diff --git a/source/funkin/editors/charter/Charter.hx b/source/funkin/editors/charter/Charter.hx index acaf9367..ddb6db51 100644 --- a/source/funkin/editors/charter/Charter.hx +++ b/source/funkin/editors/charter/Charter.hx @@ -1802,7 +1802,7 @@ class Charter extends UIState { public inline function __fixSelection(selection:Selection):Selection { var newSelection:Selection = new Selection(); - for (s in selection) if (!newSelection.contains(s)) newSelection.push(s); + for (s in selection) newSelection.pushOnce(s); return newSelection.filter((s:ICharterSelectable) -> {return s != null;}); } diff --git a/source/funkin/editors/charter/CharterNoteTypesList.hx b/source/funkin/editors/charter/CharterNoteTypesList.hx index b1cc28b6..50f23a2e 100644 --- a/source/funkin/editors/charter/CharterNoteTypesList.hx +++ b/source/funkin/editors/charter/CharterNoteTypesList.hx @@ -51,7 +51,7 @@ class CharterNoteTypesList extends UISubstateWindow { var list:Array = []; for (path in Paths.getFolderContent(pathString, true, mods ? MODS : BOTH)) if(Path.extension(path) == "hx") { var file:String = Path.withoutDirectory(path); - if (!list.contains(file)) list.push(file); + list.pushOnce(file); } return list; diff --git a/source/funkin/editors/charter/CharterStrumlineScreen.hx b/source/funkin/editors/charter/CharterStrumlineScreen.hx index ad8f9972..997a294f 100644 --- a/source/funkin/editors/charter/CharterStrumlineScreen.hx +++ b/source/funkin/editors/charter/CharterStrumlineScreen.hx @@ -146,11 +146,10 @@ class CharterStrumlineScreen extends UISubstateWindow { i = haxe.io.Path.withoutExtension(i.substr("Voices".length)); if (i == "") continue; for (j in PlayState.SONG.meta.difficulties) { - if (i.endsWith('-${j.toLowerCase()}')) { - if (suffixlist.contains(i.substring(0, i.length - j.length))) continue; - else suffixlist.push(i.substring(0, i.length - j.length)); - } - else if (!suffixlist.contains(i)) suffixlist.push(i); + if (i.endsWith('-${j.toLowerCase()}')) + suffixlist.pushOnce(i.substring(0, i.length - j.length)); + else + suffixlist.pushOnce(i); } } diff --git a/source/funkin/editors/ui/UIAutoCompleteTextBox.hx b/source/funkin/editors/ui/UIAutoCompleteTextBox.hx index 556e5522..60259c41 100644 --- a/source/funkin/editors/ui/UIAutoCompleteTextBox.hx +++ b/source/funkin/editors/ui/UIAutoCompleteTextBox.hx @@ -59,6 +59,7 @@ class UIAutoCompleteTextBox extends UITextBox { var text = label.text; _suggestions = []; if(text.length > 0) { + // for(i in suggestItems) if(i.startsWith(text)) _suggestions.pushOnce(i); for(i in suggestItems) if(!_suggestions.contains(i) && i.startsWith(text)) _suggestions.push(i); // Clean up suggestions diff --git a/source/funkin/game/Note.hx b/source/funkin/game/Note.hx index b7251a13..21e70a6e 100644 --- a/source/funkin/game/Note.hx +++ b/source/funkin/game/Note.hx @@ -98,6 +98,9 @@ class Note extends FlxSprite return __customNoteTypeExists[path]; return __customNoteTypeExists[path] = Assets.exists(path); } + + static var DEFAULT_FIELDS:Array = ["time", "id", "type", "sLen"]; + public function new(strumLine:StrumLine, noteData:ChartNote, sustain:Bool = false, sustainLength:Float = 0, sustainOffset:Float = 0, ?prev:Note) { super(); @@ -115,7 +118,7 @@ class Note extends FlxSprite this.sustainLength = sustainLength; this.strumLine = strumLine; for(field in Reflect.fields(noteData)) { - if(!["time", "id", "type", "sLen"].contains(field)) { + if(!DEFAULT_FIELDS.contains(field)) { this.extra.set(field, Reflect.field(noteData, field)); } } diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index ee8203ee..a4de7511 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -584,7 +584,7 @@ class PlayState extends MusicBeatState } var songEvents:Array = []; - for (event in SONG.events) if (!songEvents.contains(event.name)) songEvents.push(event.name); + for (event in SONG.events) songEvents.pushOnce(event.name); for (file in Paths.getFolderContent('data/events/', true, fromMods ? MODS : BOTH)) { var fileName:String = Path.withoutExtension(Path.withoutDirectory(file)); diff --git a/source/funkin/game/Stage.hx b/source/funkin/game/Stage.hx index 0c98960d..f42cc90e 100644 --- a/source/funkin/game/Stage.hx +++ b/source/funkin/game/Stage.hx @@ -244,7 +244,7 @@ class Stage extends FlxBasic implements IBeatReceiver { for (path in Paths.getFolderContent('data/stages/', true, mods ? MODS : BOTH)) if (Path.extension(path) == "xml" || Path.extension(path) == "hx") { var file:String = Path.withoutDirectory(Path.withoutExtension(path)); - if (!list.contains(file)) list.push(file); + list.pushOnce(file); } return list;