2010-03-19 18:11:26 -04:00
|
|
|
unit pseventextension;
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
{$mode objfpc}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, virtualextension,
|
2010-03-15 19:31:03 -04:00
|
|
|
uPSComponent,uPSCompiler, uPSRuntime, uPSPreProcessor,forms;
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
TSimbaPSEventExtension = class(TVirtualSimbaExtension)
|
|
|
|
public
|
|
|
|
constructor Create(FileName: String);
|
|
|
|
destructor Destroy; override;
|
|
|
|
private
|
|
|
|
PSInstance: TPSScript;
|
|
|
|
FEnabled: Boolean;
|
2010-03-15 19:31:03 -04:00
|
|
|
Script: TStringList;
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
function InitScript: Boolean;
|
|
|
|
procedure OutputMessages;
|
|
|
|
|
|
|
|
public
|
|
|
|
function HookExists(HookName: String): Boolean; override;
|
2010-03-19 18:11:26 -04:00
|
|
|
function ExecuteHook(HookName: String; fArgs: Array of Variant; out OutVariant): Integer;
|
2010-03-15 18:42:39 -04:00
|
|
|
protected
|
|
|
|
procedure RegisterPSCComponents(Sender: TObject; x: TPSPascalCompiler);
|
|
|
|
procedure RegisterPSRComponents(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
|
2010-03-15 19:31:03 -04:00
|
|
|
procedure RegisterMyMethods(Sender: TPSScript);
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
end;
|
|
|
|
|
2010-03-18 19:41:32 -04:00
|
|
|
|
2010-03-15 18:42:39 -04:00
|
|
|
implementation
|
|
|
|
uses
|
|
|
|
uPSC_std, uPSC_controls,uPSC_classes,uPSC_graphics,uPSC_stdctrls,uPSC_forms,
|
|
|
|
uPSC_extctrls, //Compile libs
|
|
|
|
uPSR_std, uPSR_controls,uPSR_classes,uPSR_graphics,uPSR_stdctrls,uPSR_forms,
|
2010-03-19 18:11:26 -04:00
|
|
|
uPSR_extctrls //Runtime-libs
|
2010-03-15 18:42:39 -04:00
|
|
|
;
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
procedure createf;
|
2010-03-15 18:42:39 -04:00
|
|
|
var
|
2010-03-15 19:31:03 -04:00
|
|
|
a: TForm;
|
2010-03-15 18:42:39 -04:00
|
|
|
begin
|
2010-03-15 19:31:03 -04:00
|
|
|
a:=TForm.Create(nil);
|
|
|
|
a.ShowModal;
|
|
|
|
a.Free;
|
|
|
|
end;
|
2010-03-15 18:42:39 -04:00
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
|
|
|
|
function TSimbaPSEventExtension.HookExists(HookName: String): Boolean;
|
|
|
|
begin
|
2010-03-19 18:11:26 -04:00
|
|
|
{ FIXME: Free the .data ? }
|
|
|
|
Exit(PSInstance.GetProcMethod('init').Data <> nil);
|
2010-03-15 19:31:03 -04:00
|
|
|
end;
|
|
|
|
|
2010-03-19 18:11:26 -04:00
|
|
|
function TSimbaPSEventExtension.ExecuteHook(HookName: String; fArgs: Array of Variant; out OutVariant): Integer;
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
begin
|
2010-03-19 18:11:26 -04:00
|
|
|
|
|
|
|
result := 0;
|
|
|
|
try
|
|
|
|
PSInstance.ExecuteFunction([], HookName);
|
|
|
|
except
|
|
|
|
result := 1;
|
|
|
|
end;
|
2010-03-15 18:42:39 -04:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TSimbaPSEventExtension.Create(FileName: String);
|
|
|
|
var
|
|
|
|
fStr:TFileStream;
|
|
|
|
|
|
|
|
begin
|
2010-03-15 19:31:03 -04:00
|
|
|
inherited create;
|
|
|
|
|
2010-03-15 18:42:39 -04:00
|
|
|
try
|
2010-03-15 19:31:03 -04:00
|
|
|
Script := TStringList.Create;
|
2010-03-15 18:42:39 -04:00
|
|
|
fStr := TFileStream.Create(FileName, fmOpenRead);
|
2010-03-15 19:31:03 -04:00
|
|
|
Script.LoadFromStream(fStr);
|
|
|
|
fStr.Free;
|
2010-03-15 18:42:39 -04:00
|
|
|
except
|
|
|
|
raise Exception.CreateFmt('File %s could not be read', [FileName]);
|
|
|
|
end;
|
|
|
|
|
|
|
|
FEnabled := False;
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
{ Create script, and see if the extension is valid. (If it compiles) }
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
PSInstance := TPSScript.Create(nil);
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
PSInstance.Script := Self.Script;
|
2010-03-15 18:42:39 -04:00
|
|
|
PSInstance.OnCompImport:=@RegisterPSCComponents;
|
|
|
|
PSInstance.OnExecImport:=@RegisterPSRComponents;
|
2010-03-15 19:31:03 -04:00
|
|
|
PSInstance.OnCompile:=@RegisterMyMethods;
|
2010-03-15 18:42:39 -04:00
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
Writeln(Format('%s: Script: %s', [FileName, Self.Script.Text]));
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
FEnabled := PSInstance.Compile;
|
|
|
|
finally
|
|
|
|
if FEnabled then
|
|
|
|
writeln('Extension Enabled')
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
writeln('Extension Disabled - Did not compile');
|
|
|
|
OutputMessages;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
FEnabled := InitScript();
|
|
|
|
if FEnabled then
|
|
|
|
writeln('It exists')
|
|
|
|
else
|
2010-03-15 19:31:03 -04:00
|
|
|
writeln('It does not exist - or something went wrong while executing it.');
|
2010-03-19 18:11:26 -04:00
|
|
|
//writeln(PSInstance.ExecuteFunction([], 'test'));
|
2010-03-15 18:42:39 -04:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TSimbaPSEventExtension.InitScript: Boolean;
|
|
|
|
begin
|
2010-03-19 18:11:26 -04:00
|
|
|
if not HookExists('init') then
|
2010-03-15 18:42:39 -04:00
|
|
|
exit(false);
|
|
|
|
|
2010-03-19 18:11:26 -04:00
|
|
|
result := true;
|
2010-03-15 18:42:39 -04:00
|
|
|
try
|
|
|
|
PSInstance.ExecuteFunction([], 'init');
|
|
|
|
except
|
|
|
|
result := false;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
|
|
|
|
procedure TSimbaPSEventExtension.RegisterMyMethods(Sender: TPSScript);
|
|
|
|
begin
|
|
|
|
Sender.AddFunction(@createf, 'procedure createf;');
|
|
|
|
end;
|
|
|
|
|
2010-03-15 18:42:39 -04:00
|
|
|
procedure TSimbaPSEventExtension.RegisterPSCComponents(Sender: TObject; x: TPSPascalCompiler);
|
|
|
|
begin
|
|
|
|
SIRegister_Std(x);
|
|
|
|
SIRegister_Classes(x, True);
|
|
|
|
SIRegister_Controls(x);
|
|
|
|
SIRegister_Graphics(x, True);
|
|
|
|
SIRegister_stdctrls(x);
|
|
|
|
SIRegister_Forms(x);
|
|
|
|
SIRegister_ExtCtrls(x);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSimbaPSEventExtension.RegisterPSRComponents(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
|
|
|
|
begin
|
|
|
|
RIRegister_Std(x);
|
|
|
|
RIRegister_Classes(x, True);
|
|
|
|
RIRegister_Controls(x);
|
|
|
|
RIRegister_Graphics(x, True);
|
|
|
|
RIRegister_stdctrls(x);
|
|
|
|
RIRegister_Forms(x);
|
|
|
|
RIRegister_ExtCtrls(x);
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TSimbaPSEventExtension.Destroy;
|
|
|
|
begin
|
|
|
|
if Assigned(PSInstance) then
|
|
|
|
FreeAndNil(PSInstance);
|
|
|
|
|
|
|
|
|
|
|
|
WriteLn('Closing extension');
|
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
|
|
|
|
procedure TSimbaPSEventExtension.OutputMessages;
|
|
|
|
var
|
|
|
|
l: Longint;
|
|
|
|
b: Boolean;
|
2010-03-15 18:42:39 -04:00
|
|
|
begin
|
2010-03-15 19:31:03 -04:00
|
|
|
b := False;
|
|
|
|
for l := 0 to PSInstance.CompilerMessageCount - 1 do
|
|
|
|
begin
|
|
|
|
if (not b) and (PSInstance.CompilerMessages[l] is TIFPSPascalCompilerError) then
|
|
|
|
begin
|
|
|
|
b := True;
|
|
|
|
with PSInstance.CompilerMessages[l] do
|
|
|
|
writeln(MessageToString);
|
|
|
|
{if OnError <> nil then
|
|
|
|
with PSInstance.CompilerMessages[l] do
|
|
|
|
HandleError(Row, Col, Pos, MessageToString,errCompile, ModuleName)
|
|
|
|
else }
|
|
|
|
writeln(PSInstance.CompilerErrorToStr(l) + ' at line ' + inttostr(PSInstance.CompilerMessages[l].Row));
|
|
|
|
end else
|
|
|
|
Writeln(PSInstance.CompilerErrorToStr(l) + ' at line ' + inttostr(PSInstance.CompilerMessages[l].Row));
|
2010-03-15 18:42:39 -04:00
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
end;
|
2010-03-15 18:42:39 -04:00
|
|
|
end;
|
|
|
|
|
2010-03-15 19:31:03 -04:00
|
|
|
|
2010-03-15 18:42:39 -04:00
|
|
|
|
|
|
|
end.
|
|
|
|
|