From 5596afca67cf9232e73d66d1f1edcfcc552659c2 Mon Sep 17 00:00:00 2001 From: Olly Date: Fri, 19 Jul 2024 15:08:53 +0100 Subject: [PATCH] When plugins `AddCode` it should be added like an include would (not with delayedcode) --- Source/script/simba.script.pas | 71 +++++++++++--------- Source/script/simba.script_compiler.pas | 7 ++ Source/script/simba.script_plugin.pas | 86 +++++++++++-------------- 3 files changed, 84 insertions(+), 80 deletions(-) diff --git a/Source/script/simba.script.pas b/Source/script/simba.script.pas index 4996b1b0b..0d49a3b49 100644 --- a/Source/script/simba.script.pas +++ b/Source/script/simba.script.pas @@ -13,7 +13,9 @@ interface Classes, SysUtils, lptypes, lpvartypes, lpcompiler, lpparser, lpinterpreter, lpmessages, simba.base, - simba.script_compiler, simba.script_communication, simba.script_plugin; + simba.script_compiler, + simba.script_communication, + simba.script_plugin; type PSimbaScript = ^TSimbaScript; @@ -31,7 +33,7 @@ TSimbaScript = class(TObject) FCompileTime: Double; FRunningTime: Double; - FPlugins: TSimbaScriptPluginArray; + FPlugins: TSimbaScriptPluginList; FSimbaCommunication: TSimbaScriptCommunication; function DoCompilerPreprocessorFunc(Sender: TLapeCompiler; Name, Argument: lpString; out Value: lpString): Boolean; @@ -61,8 +63,9 @@ TSimbaScript = class(TObject) property Script: String read FScript write FScript; property ScriptFileName: String read FScriptFileName write FScriptFileName; - constructor Create(Communication: TSimbaScriptCommunication); reintroduce; overload; - constructor Create(FileName: String; Communication: TSimbaScriptCommunication = nil); reintroduce; overload; + constructor Create; overload; + constructor Create(Communication: TSimbaScriptCommunication); overload; + constructor Create(FileName: String; Communication: TSimbaScriptCommunication = nil); overload; destructor Destroy; override; end; @@ -74,12 +77,12 @@ implementation function TSimbaScript.DoCompilerPreprocessorFunc(Sender: TLapeCompiler; Name, Argument: lpString; out Value: lpString): Boolean; begin + Value := ''; + case Name of 'LIBLOADED': Value := BoolToStr(FindLoadedPlugin(SimbaEnv.FindPlugin(Argument, [Sender.Tokenizer.FileName])) <> '', True); 'LIBEXISTS': Value := BoolToStr(SimbaEnv.HasPlugin(Argument, [Sender.Tokenizer.FileName]), True); end; - - Result := Value <> ''; end; function TSimbaScript.DoCompilerMacro(Sender: TLapeCompiler; Name, Argument: lpString; out Value: lpString): Boolean; @@ -144,7 +147,7 @@ function TSimbaScript.DoCompilerHandleDirective(Sender: TLapeCompiler; Directive Plugin := TSimbaScriptPlugin.Create(Argument, [ExtractFileDir(Sender.Tokenizer.FileName)]); Plugin.Import(FCompiler); - FPlugins := FPlugins + [Plugin]; + FPlugins.Add(Plugin); end; end; except @@ -155,14 +158,15 @@ function TSimbaScript.DoCompilerHandleDirective(Sender: TLapeCompiler; Directive function TSimbaScript.GetState: ESimbaScriptState; begin - if (FCodeRunner = nil) then - Result := ESimbaScriptState.STATE_NONE - else if FCodeRunner.isRunning then - Result := ESimbaScriptState.STATE_RUNNING - else if FCodeRunner.isStopped then - Result := ESimbaScriptState.STATE_STOP - else if FCodeRunner.isPaused then - Result := ESimbaScriptState.STATE_PAUSED; + Result := ESimbaScriptState.STATE_NONE; + + if (FCodeRunner <> nil) then + if FCodeRunner.isRunning then + Result := ESimbaScriptState.STATE_RUNNING + else if FCodeRunner.isStopped then + Result := ESimbaScriptState.STATE_STOP + else if FCodeRunner.isPaused then + Result := ESimbaScriptState.STATE_PAUSED; end; procedure TSimbaScript.SetTargetWindow(Value: String); @@ -203,6 +207,8 @@ function TSimbaScript.Compile: Boolean; end; function TSimbaScript.Run: Boolean; +var + I: Integer; begin if (FTargetWindow = 0) or (not FTargetWindow.IsValid()) then FTargetWindow := GetDesktopWindow(); @@ -220,7 +226,8 @@ function TSimbaScript.Run: Boolean; finally FRunningTime := HighResolutionTime() - FRunningTime; - FPlugins.CallOnStop(); + for I := 0 to FPlugins.Count - 1 do + FPlugins[I].CallOnStop(); if FUserTerminated then FCompiler.CallProc('_CallOnUserTerminate'); @@ -230,17 +237,24 @@ function TSimbaScript.Run: Boolean; Result := True; end; -constructor TSimbaScript.Create(Communication: TSimbaScriptCommunication); +constructor TSimbaScript.Create; begin inherited Create(); + FPlugins := TSimbaScriptPluginList.Create(True); +end; + +constructor TSimbaScript.Create(Communication: TSimbaScriptCommunication); +begin + Create(); + FSimbaCommunication := Communication; FScript := FSimbaCommunication.GetScript(FScriptFileName); end; constructor TSimbaScript.Create(FileName: String; Communication: TSimbaScriptCommunication); begin - inherited Create(); + Create(); FSimbaCommunication := Communication; @@ -249,13 +263,7 @@ constructor TSimbaScript.Create(FileName: String; Communication: TSimbaScriptCom end; destructor TSimbaScript.Destroy; -var - Plugin: TSimbaScriptPlugin; begin - for Plugin in FPlugins do - Plugin.Free(); - FPlugins := nil; - if (FCompiler <> nil) then begin if (FCompiler.Globals['HTTPClient'] <> nil) then @@ -265,15 +273,16 @@ destructor TSimbaScript.Destroy; FreeAndNil(FCompiler); end; - if (FSimbaCommunication <> nil) then - FreeAndNil(FSimbaCommunication); - if (FCodeRunner <> nil) then - FreeAndNil(FCodeRunner); + FreeAndNil(FPlugins); + FreeAndNil(FSimbaCommunication); + FreeAndNil(FCodeRunner); inherited Destroy(); end; procedure TSimbaScript.SetState(Value: ESimbaScriptState); +var + I: Integer; begin if (FCodeRunner = nil) then Exit; @@ -281,7 +290,8 @@ procedure TSimbaScript.SetState(Value: ESimbaScriptState); case Value of ESimbaScriptState.STATE_RUNNING: begin - FPlugins.CallOnResume(); + for I := 0 to FPlugins.Count - 1 do + FPlugins[I].CallOnResume(); FCompiler.CallProc('_CallOnResume'); FCodeRunner.Resume(); end; @@ -289,7 +299,8 @@ procedure TSimbaScript.SetState(Value: ESimbaScriptState); ESimbaScriptState.STATE_PAUSED: begin FCodeRunner.Pause(); - FPlugins.CallOnPause(); + for I := 0 to FPlugins.Count - 1 do + FPlugins[I].CallOnPause(); FCompiler.CallProc('_CallOnPause'); end; diff --git a/Source/script/simba.script_compiler.pas b/Source/script/simba.script_compiler.pas index 13b605fb5..6061f21e4 100644 --- a/Source/script/simba.script_compiler.pas +++ b/Source/script/simba.script_compiler.pas @@ -32,6 +32,8 @@ TSimbaScript_Compiler = class(TLapeCompiler) procedure InitBaseString; override; procedure InitBaseDateTime; override; public + procedure pushCode(Code: String); + procedure addDelayedCode(Code: TStringArray; AFileName: lpString = ''); virtual; overload; function addGlobalFunc(Header: lpString; Body: TStringArray): TLapeTree_Method; virtual; overload; @@ -379,6 +381,11 @@ procedure TSimbaScript_Compiler.InitBaseDateTime; ImportingSection := ''; end; +procedure TSimbaScript_Compiler.pushCode(Code: String); +begin + pushTokenizer(TLapeTokenizerString.Create(Code)); +end; + procedure TSimbaScript_Compiler.addDelayedCode(Code: TStringArray; AFileName: lpString); begin addDelayedCode(LapeDelayedFlags + LineEnding.Join(Code), AFileName); diff --git a/Source/script/simba.script_plugin.pas b/Source/script/simba.script_plugin.pas index a410a8636..e014e2b46 100644 --- a/Source/script/simba.script_plugin.pas +++ b/Source/script/simba.script_plugin.pas @@ -11,28 +11,29 @@ interface uses Classes, SysUtils, dynlibs, - simba.base, simba.script_compiler, simba.script_pluginmethods; + simba.base, + simba.containers, + simba.script_compiler, + simba.script_pluginmethods; type PSimbaPluginInfo = ^TSimbaPluginInfo; TSimbaPluginInfo = packed record - SimbaVersion: Integer; - SimbaMajor: Integer; - + SimbaVersion: Int32; + SimbaMajor: Int32; FileName: PChar; - Compiler: Pointer; // Extend this but do not remove, reorder or change datatypes. end; TSimbaPluginExports = packed record - GetFunctionInfo: function(Index: Integer; var Address: Pointer; var Header: PChar): Integer; cdecl; - GetFunctionCount: function: Integer; cdecl; - GetTypeInfo: function(Index: Integer; var Name: PChar; var Str: PChar): Integer; cdecl; - GetTypeCount: function: Integer; cdecl; + GetFunctionInfo: function(Index: Int32; var Address: Pointer; var Header: PChar): Int32; cdecl; + GetFunctionCount: function: Int32; cdecl; + GetTypeInfo: function(Index: Int32; var Name: PChar; var Str: PChar): Int32; cdecl; + GetTypeCount: function: Int32; cdecl; GetCode: procedure(var Code: PChar); cdecl; - GetCodeLength: function: Integer; cdecl; + GetCodeLength: function: Int32; cdecl; SetPluginMemManager: procedure(MemoryManager: TMemoryManager); cdecl; RegisterSimbaPlugin: procedure(Info: PSimbaPluginInfo; Methods: PSimbaPluginMethods); cdecl; @@ -74,17 +75,14 @@ TSimbaScriptPlugin = class procedure Import(Compiler: TSimbaScript_Compiler); procedure Load; - constructor Create(FileName: String; ExtraSearchDirs: TStringArray = nil); - destructor Destroy; override; - end; - - TSimbaScriptPluginArray = array of TSimbaScriptPlugin; - TSimbaScriptPluginArrayHelper = type helper for TSimbaScriptPluginArray - public procedure CallOnPause; procedure CallOnResume; procedure CallOnStop; + + constructor Create(FileName: String; ExtraSearchDirs: TStringArray = nil); + destructor Destroy; override; end; + TSimbaScriptPluginList = specialize TSimbaObjectList; implementation @@ -109,7 +107,7 @@ procedure TSimbaScriptPlugin.Load; end; end; - function LoadMethod(Index: Integer): TSimbaScriptPluginMethod; + function LoadMethod(Index: Int32): TSimbaScriptPluginMethod; var Buffer: PChar; begin @@ -137,7 +135,7 @@ procedure TSimbaScriptPlugin.Load; end; end; - function LoadType(Index: Integer): TSimbaScriptPluginType; + function LoadType(Index: Int32): TSimbaScriptPluginType; var Buffer: record Name: PChar; @@ -162,7 +160,7 @@ procedure TSimbaScriptPlugin.Load; end; var - Index: Integer; + Index: Int32; begin with FExports do begin @@ -205,9 +203,24 @@ procedure TSimbaScriptPlugin.Load; FExports.OnAttach(nil); end; +procedure TSimbaScriptPlugin.CallOnPause; +begin + if Assigned(FExports.OnPause) then FExports.OnPause(); +end; + +procedure TSimbaScriptPlugin.CallOnResume; +begin + if Assigned(FExports.OnResume) then FExports.OnResume(); +end; + +procedure TSimbaScriptPlugin.CallOnStop; +begin + if Assigned(FExports.OnStop) then FExports.OnStop(); +end; + function TSimbaScriptPlugin.Dump: TStringList; var - I: Integer; + I: Int32; begin Result := TStringList.Create(); @@ -222,7 +235,7 @@ function TSimbaScriptPlugin.Dump: TStringList; procedure TSimbaScriptPlugin.Import(Compiler: TSimbaScript_Compiler); var - I: Integer; + I: Int32; begin for I := 0 to High(FTypes) do Compiler.addGlobalType(FTypes[I].Str, FTypes[I].Name); @@ -236,7 +249,7 @@ procedure TSimbaScriptPlugin.Import(Compiler: TSimbaScript_Compiler); end; if (FCode <> '') then - Compiler.addDelayedCode(FCode, '!' + FFileName, False); + Compiler.pushCode(FCode); GetMemoryManager(FMemoryManager); if Assigned(FExports.SetPluginMemManager) then @@ -275,32 +288,5 @@ destructor TSimbaScriptPlugin.Destroy; inherited Destroy(); end; -procedure TSimbaScriptPluginArrayHelper.CallOnPause; -var - I: Integer; -begin - for I := 0 to High(Self) do - if Assigned(Self[I].FExports.OnPause) then - Self[I].FExports.OnPause(); -end; - -procedure TSimbaScriptPluginArrayHelper.CallOnResume; -var - I: Integer; -begin - for I := 0 to High(Self) do - if Assigned(Self[I].FExports.OnResume) then - Self[I].FExports.OnResume(); -end; - -procedure TSimbaScriptPluginArrayHelper.CallOnStop; -var - I: Integer; -begin - for I := 0 to High(Self) do - if Assigned(Self[I].FExports.OnStop) then - Self[I].FExports.OnStop(); -end; - end.