Skip to content

Commit

Permalink
fix(TWaspConfig): Fixed some pointer issues with arrays
Browse files Browse the repository at this point in the history
- TWaspConfig is not indented by default
  • Loading branch information
Torwent committed Mar 2, 2024
1 parent 53a9c5a commit 72c9956
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 36 deletions.
141 changes: 109 additions & 32 deletions utils/config.simba
Original file line number Diff line number Diff line change
Expand Up @@ -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)=
Expand All @@ -22,6 +57,8 @@ end;
TWaspConfig = record
Path: String;
JSON: TJSONObject;
_ArrayHelper: TJSONArrayObjectArray;
IsSetup, OnTerminate: Boolean;
end;

(*
Expand Down Expand Up @@ -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
Expand All @@ -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;

(*
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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`.

Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions utils/forms/scriptform.simba
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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)];

Expand All @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions utils/recorder/recorder.simba
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 72c9956

Please sign in to comment.