Skip to content

Commit

Permalink
When plugins AddCode it should be added like an include would (not …
Browse files Browse the repository at this point in the history
…with delayedcode)
  • Loading branch information
ollydev committed Jul 19, 2024
1 parent 7efbd3c commit 5596afc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 80 deletions.
71 changes: 41 additions & 30 deletions Source/script/simba.script.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

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

Expand All @@ -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
Expand All @@ -265,31 +273,34 @@ 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;

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;

ESimbaScriptState.STATE_PAUSED:
begin
FCodeRunner.Pause();
FPlugins.CallOnPause();
for I := 0 to FPlugins.Count - 1 do
FPlugins[I].CallOnPause();
FCompiler.CallProc('_CallOnPause');
end;

Expand Down
7 changes: 7 additions & 0 deletions Source/script/simba.script_compiler.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
86 changes: 36 additions & 50 deletions Source/script/simba.script_plugin.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TSimbaScriptPlugin>;

implementation

Expand All @@ -109,7 +107,7 @@ procedure TSimbaScriptPlugin.Load;
end;
end;

function LoadMethod(Index: Integer): TSimbaScriptPluginMethod;
function LoadMethod(Index: Int32): TSimbaScriptPluginMethod;
var
Buffer: PChar;
begin
Expand Down Expand Up @@ -137,7 +135,7 @@ procedure TSimbaScriptPlugin.Load;
end;
end;

function LoadType(Index: Integer): TSimbaScriptPluginType;
function LoadType(Index: Int32): TSimbaScriptPluginType;
var
Buffer: record
Name: PChar;
Expand All @@ -162,7 +160,7 @@ procedure TSimbaScriptPlugin.Load;
end;

var
Index: Integer;
Index: Int32;
begin
with FExports do
begin
Expand Down Expand Up @@ -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();

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

0 comments on commit 5596afc

Please sign in to comment.