From 72c995614e6c10dd29b65d1173cd4fed7f23550c Mon Sep 17 00:00:00 2001 From: Torwent Date: Sat, 2 Mar 2024 14:23:28 +0100 Subject: [PATCH] fix(TWaspConfig): Fixed some pointer issues with arrays - TWaspConfig is not indented by default --- utils/config.simba | 141 ++++++++++++++++++++++++++-------- utils/forms/scriptform.simba | 7 ++ utils/recorder/recorder.simba | 4 - 3 files changed, 116 insertions(+), 36 deletions(-) diff --git a/utils/config.simba b/utils/config.simba index a04c1bbb..ec0021a1 100644 --- a/utils/config.simba +++ b/utils/config.simba @@ -8,6 +8,41 @@ A full featured JSON config writer and reader. {$I WaspLib/utils.simba} {$ENDIF} +type + TJSONArrayObject = record + Key: String; + JSONArray: TJSONArray; + end; + + TJSONArrayObjectArray = array of TJSONArrayObject; + +function TJSONArrayObjectArray.Find(key: String): Int32; +begin + for Result := 0 to High(Self) do + if Self[Result].Key = key then + Exit; + Result := -1; +end; + +function TJSONArrayObjectArray.Put(key: String): Int32; +begin + Result := Length(Self); + Self += [key, nil]; +end; + +function TJSONArrayObjectArray.Get(key: String): Int32; +begin + Result := Self.Find(key); + if Result > -1 then + begin + Self[Result].JSONArray.Free(); + Self[Result].JSONArray := nil; + Exit; + end; + Result := Self.Put(key); +end; + + type (* (Config)= @@ -22,6 +57,8 @@ end; TWaspConfig = record Path: String; JSON: TJSONObject; + _ArrayHelper: TJSONArrayObjectArray; + IsSetup, OnTerminate: Boolean; end; (* @@ -49,6 +86,27 @@ begin Result.Init(); end; +(* +## Config.Free() +```pascal +procedure TWaspConfig.Free(); +``` +Used to free your `TWaspConfig`. +It's automatically called on script termination, but you may call it sooner if +you wish to unlock the ram used by this (which should be minimal). +*) +procedure TWaspConfig.Free(); +begin + if not Self.IsSetup then + Exit; + + Self.JSON.Free(); + Self.JSON := nil; + Self._ArrayHelper := []; + + Self.IsSetup := False; +end; + (* ## Config.Setup() ```pascal @@ -67,7 +125,26 @@ begin Self.Path += jsonFile; Self.JSON := Self.GetConfig(); - AddOnTerminate(@Self.JSON.Free); + Self.IsSetup := True; + + if not Self.OnTerminate then + begin + AddOnTerminate(@Self.Free); + Self.OnTerminate := True; + end; +end; + +(* +## Config.DeleteConfig() +```pascal +procedure TWaspConfig.DeleteConfig(); +``` +Delete your `TWaspConfig` from disk. +*) +procedure TWaspConfig.DeleteConfig(); +begin + if not DeleteFile(Self.Path) then + TerminateScript('Failed to delete: ' + Self.Path); end; (* @@ -82,7 +159,7 @@ procedure TWaspConfig.SaveConfig(); begin if Self.JSON = nil then TerminateScript('You need to use TWaspConfig.Setup() before trying to save it into a config file.'); - if not WriteFileContents(Self.Path, json.toString(), False) then + if not WriteFileContents(Self.Path, json.toString(2), False) then TerminateScript('Failed to save config.'); end; @@ -146,82 +223,82 @@ end; procedure TWaspConfig.Put(key: String; values: TStringArray; save: Boolean = True); overload; var - tmp: TJSONArray; - i: Int32; + i, j: Int32; begin if Self.JSON = nil then Exit; - tmp.Init(); - for i := 0 to High(values) do - tmp.Put(values[i]); + i := Self._ArrayHelper.Get(key); + Self._ArrayHelper[i].JSONArray.Init(); + + for j := 0 to High(values) do + Self._ArrayHelper[i].JSONArray.Put(values[j]); Self.JSON.Remove(key); - Self.JSON.Put(key, tmp); + Self.JSON.Put(key, Self._ArrayHelper[i].JSONArray); if save then Self.SaveConfig(); - tmp.Free(); end; procedure TWaspConfig.Put(key: String; values: TIntegerArray; save: Boolean = True); overload; var - tmp: TJSONArray; - i: Int32; + i, j: Int32; begin if Self.JSON = nil then Exit; - tmp.Init(); - for i := 0 to High(values) do - tmp.Put(values[i]); + i := Self._ArrayHelper.Get(key); + Self._ArrayHelper[i].JSONArray.Init(); + + for j := 0 to High(values) do + Self._ArrayHelper[i].JSONArray.Put(values[j]); Self.JSON.Remove(key); - Self.JSON.Put(key, tmp); + Self.JSON.Put(key, Self._ArrayHelper[i].JSONArray); if save then Self.SaveConfig(); - tmp.Free(); end; procedure TWaspConfig.Put(key: String; values: TDoubleArray; save: Boolean = True); overload; var - tmp: TJSONArray; - i: Int32; + i, j: Int32; begin if Self.JSON = nil then Exit; - tmp.Init(); - for i := 0 to High(values) do - tmp.Put(values[i]); + i := Self._ArrayHelper.Get(key); + Self._ArrayHelper[i].JSONArray.Init(); + + for j := 0 to High(values) do + Self._ArrayHelper[i].JSONArray.Put(values[j]); Self.JSON.Remove(key); - Self.JSON.Put(key, tmp); + Self.JSON.Put(key, Self._ArrayHelper[i].JSONArray); if save then Self.SaveConfig(); - tmp.Free(); end; procedure TWaspConfig.Put(key: String; values: TBoolArray; save: Boolean = True); overload; var - tmp: TJSONArray; - i: Int32; + i, j: Int32; begin if Self.JSON = nil then Exit; - tmp.Init(); - for i := 0 to High(values) do - tmp.Put(values[i]); + i := Self._ArrayHelper.Get(key); + Self._ArrayHelper[i].JSONArray.Init(); + + for j := 0 to High(values) do + Self._ArrayHelper[i].JSONArray.Put(values[j]); Self.JSON.Remove(key); - Self.JSON.Put(key, tmp); + Self.JSON.Put(key, Self._ArrayHelper[i].JSONArray); if save then Self.SaveConfig(); - tmp.Free(); end; procedure TWaspConfig.Put(key: String; value: Pointer; save: Boolean = True); overload; @@ -400,7 +477,7 @@ end; (* ## Config.ToString() ```pascal -function TWaspConfig.ToString(indentFactor: Int32 = 0): String; +function TWaspConfig.ToString(indentFactor: Int32 = 2): String; ``` Returns a string version of your `TWaspConfig`. @@ -409,7 +486,7 @@ Example: WriteLn MyConfig.ToString(); ``` *) -function TWaspConfig.ToString(indentFactor: Int32 = 0): String; +function TWaspConfig.ToString(indentFactor: Int32 = 2): String; begin if Self.JSON = nil then Exit; diff --git a/utils/forms/scriptform.simba b/utils/forms/scriptform.simba index 410eec4b..e6a2677d 100644 --- a/utils/forms/scriptform.simba +++ b/utils/forms/scriptform.simba @@ -1480,6 +1480,7 @@ function TScriptForm.CreateBankSettings(): TTabSheet; imgBox: TSimbaImageBox; combobox: TComboBox; begin + TForm(sender).EnsureVisible(False); imgBox := TForm(sender).GetChild('map_selector'); combobox := form.GetChild('bank_selector_combobox'); @@ -2054,6 +2055,11 @@ procedure TScriptForm.Setup(caption: String = 'Script Form'; size: TPoint = [750 TerminateScript(); end; + procedure TScriptForm._OnShow(sender: TObject); + begin + TForm(sender).EnsureVisible(False); + end; + begin Self.Size := [TControl.AdjustToDPI(size.X), TControl.AdjustToDPI(size.Y)]; @@ -2067,6 +2073,7 @@ begin if not allowResize then getConstraints().SetInterfaceConstraints(Self.Size.X, Self.Size.Y, Self.Size.X, Self.Size.Y); setPosition(poScreenCenter); + setOnShow(@Self._OnShow); setOnClose(@Self._OnClose); getFont().setQuality(TFontQuality.fqAntialiased); end; diff --git a/utils/recorder/recorder.simba b/utils/recorder/recorder.simba index 81da1790..e5d4b6ed 100644 --- a/utils/recorder/recorder.simba +++ b/utils/recorder/recorder.simba @@ -47,11 +47,7 @@ Start the recorder. procedure TRecorder.Start(window: PtrUInt; seconds: Int32; directory: String; userBox, expBox: TBox); overload; function _ScriptPID(): Integer; begin - {$IFDEF SIMBA1400} Result := GetProcessID() - {$ELSE} - Result := GetScriptPID(); - {$ENDIF} end; function _RecorderScript(): String;