1
0
mirror of https://github.com/moparisthebest/Simba synced 2024-11-05 08:55:15 -05:00

Fix for linux.

git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@137 3f818213-9676-44b0-a9b4-5e4c4e03d09d
This commit is contained in:
Wizzup? 2009-10-17 22:54:08 +00:00
parent 85bb6c6e1d
commit 3220330c90

View File

@ -1,311 +1,318 @@
{ {
This file is part of the Mufasa Macro Library (MML) This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009 by Raymond van Venetië and Merlijn Wajer Copyright (c) 2009 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
MML is distributed in the hope that it will be useful, MML is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>. along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution, See the file COPYING, included in this distribution,
for details about the copyright. for details about the copyright.
MMLPSThread for the Mufasa Macro Library MMLPSThread for the Mufasa Macro Library
} }
unit mmlpsthread; unit mmlpsthread;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
interface interface
uses uses
Classes, SysUtils, client, uPSComponent,uPSCompiler,uPSRuntime,stdCtrls, uPSPreProcessor; Classes, SysUtils, client, uPSComponent,uPSCompiler,uPSRuntime,stdCtrls, uPSPreProcessor;
type type
{ TMMLPSThread } { TMMLPSThread }
TMMLPSThread = class(TThread) TMMLPSThread = class(TThread)
procedure PSScriptProcessUnknowDirective(Sender: TPSPreProcessor; procedure PSScriptProcessUnknowDirective(Sender: TPSPreProcessor;
Parser: TPSPascalPreProcessorParser; const Active: Boolean; Parser: TPSPascalPreProcessorParser; const Active: Boolean;
const DirectiveName, DirectiveParam: string; var Continue: Boolean); const DirectiveName, DirectiveParam: string; var Continue: Boolean);
protected protected
DebugTo : TMemo; DebugTo : TMemo;
PluginsToload : Array of integer; PluginsToload : Array of integer;
procedure OnCompile(Sender: TPSScript); procedure OnCompile(Sender: TPSScript);
procedure AfterExecute(Sender : TPSScript); procedure AfterExecute(Sender : TPSScript);
function RequireFile(Sender: TObject; const OriginFileName: String; function RequireFile(Sender: TObject; const OriginFileName: String;
var FileName, OutPut: string): Boolean; var FileName, OutPut: string): Boolean;
procedure OnCompImport(Sender: TObject; x: TPSPascalCompiler); procedure OnCompImport(Sender: TObject; x: TPSPascalCompiler);
procedure OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter); procedure OnExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);
procedure OutputMessages; procedure OutputMessages;
procedure OnThreadTerminate(Sender: TObject); procedure OnThreadTerminate(Sender: TObject);
procedure Execute; override; procedure Execute; override;
public public
PSScript : TPSScript; // Moved to public, as we can't kill it otherwise. PSScript : TPSScript; // Moved to public, as we can't kill it otherwise.
Client : TClient; Client : TClient;
procedure SetPSScript(Script : string); procedure SetPSScript(Script : string);
procedure SetDebug( Strings : TMemo ); procedure SetDebug( Strings : TMemo );
constructor Create(CreateSuspended: Boolean); constructor Create(CreateSuspended: Boolean);
destructor Destroy; override; destructor Destroy; override;
end; end;
implementation implementation
uses uses
MufasaTypes, dtmutil, MufasaTypes, dtmutil,
{$ifdef mswindows}windows,{$endif} {$ifdef mswindows}windows,{$endif}
uPSC_std, uPSC_controls,uPSC_classes,uPSC_graphics,uPSC_stdctrls,uPSC_forms, uPSC_std, uPSC_controls,uPSC_classes,uPSC_graphics,uPSC_stdctrls,uPSC_forms,
uPSC_extctrls, //Compile-libs uPSC_extctrls, //Compile-libs
uPSR_std, uPSR_controls,uPSR_classes,uPSR_graphics,uPSR_stdctrls,uPSR_forms, uPSR_std, uPSR_controls,uPSR_classes,uPSR_graphics,uPSR_stdctrls,uPSR_forms,
uPSR_extctrls, //Runtime-libs uPSR_extctrls, //Runtime-libs
Graphics, //For Graphics types Graphics, //For Graphics types
math, //Maths! math, //Maths!
bitmaps, bitmaps,
lclintf; // for GetTickCount and others. lclintf; // for GetTickCount and others.
threadvar threadvar
CurrThread : TMMLPSThread; CurrThread : TMMLPSThread;
{Some General PS Functions here} {Some General PS Functions here}
procedure psWriteln(str : string); procedure psWriteln(str : string);
var {$IFDEF WINDOWS}
CriticalSec : TRTLCriticalSection; var
begin CriticalSec : TRTLCriticalSection;
System.InitCriticalSection(CriticalSec); begin
System.EnterCriticalSection(CriticalSec); System.InitCriticalSection(CriticalSec);
try System.EnterCriticalSection(CriticalSec);
if CurrThread.DebugTo <> nil then try
begin; if CurrThread.DebugTo <> nil then
CurrThread.DebugTo.lines.add(str); begin;
CurrThread.DebugTo.Refresh; CurrThread.DebugTo.lines.add(str);
end; CurrThread.DebugTo.Refresh;
finally end;
System.LeaveCriticalSection(CriticalSec); finally
end; System.LeaveCriticalSection(CriticalSec);
end; end;
end;
function ThreadSafeCall(ProcName: string; var V: TVariantArray): Variant; {$ELSE}
begin
begin; writeln(str);
Writeln('We have a length of: ' + inttostr(length(v))); end;
Try {$ENDIF}
Result := CurrThread.PSScript.Exec.RunProcPVar(v,CurrThread.PSScript.Exec.GetProc(Procname));
Except
Writeln('We has some errors :-('); function ThreadSafeCall(ProcName: string; var V: TVariantArray): Variant;
end;
end; begin;
Writeln('We have a length of: ' + inttostr(length(v)));
Try
{ Result := CurrThread.PSScript.Exec.RunProcPVar(v,CurrThread.PSScript.Exec.GetProc(Procname));
Note to Raymond: For PascalScript, Create it on the .Create, Except
Execute it on the .Execute, and don't forget to Destroy it on .Destroy. Writeln('We has some errors :-(');
end;
Furthermore, all the wrappers can be in the unit "implementation" section. end;
Better still to create an .inc for it, otherwise this unit will become huge.
(You can even split up the .inc's in stuff like color, bitmap, etc. )
{
Also, don't add PS to this unit, but make a seperate unit for it. Note to Raymond: For PascalScript, Create it on the .Create,
Unit "MMLPSThread", perhaps? Execute it on the .Execute, and don't forget to Destroy it on .Destroy.
See the TestUnit for use of this thread, it's pretty straightforward. Furthermore, all the wrappers can be in the unit "implementation" section.
Better still to create an .inc for it, otherwise this unit will become huge.
It may also be wise to turn the "Importing of wrappers" into an include as (You can even split up the .inc's in stuff like color, bitmap, etc. )
well, it will really make the unit more straightforward to use and read.
} Also, don't add PS to this unit, but make a seperate unit for it.
Unit "MMLPSThread", perhaps?
constructor TMMLPSThread.Create(CreateSuspended : boolean); See the TestUnit for use of this thread, it's pretty straightforward.
begin
SetLength(PluginsToLoad,0); It may also be wise to turn the "Importing of wrappers" into an include as
Client := TClient.Create; well, it will really make the unit more straightforward to use and read.
PSScript := TPSScript.Create(nil); }
PSScript.UsePreProcessor:= True;
PSScript.OnNeedFile := @RequireFile;
PSScript.OnProcessUnknowDirective:=@PSScriptProcessUnknowDirective; constructor TMMLPSThread.Create(CreateSuspended : boolean);
PSScript.OnCompile:= @OnCompile; begin
PSScript.OnCompImport:= @OnCompImport; SetLength(PluginsToLoad,0);
PSScript.OnExecImport:= @OnExecImport; Client := TClient.Create;
PSScript.OnAfterExecute:= @AfterExecute; PSScript := TPSScript.Create(nil);
PSScript.UsePreProcessor:= True;
// Set some defines PSScript.OnNeedFile := @RequireFile;
{$I PSInc/psdefines.inc} PSScript.OnProcessUnknowDirective:=@PSScriptProcessUnknowDirective;
PSScript.OnCompile:= @OnCompile;
PSScript.OnCompImport:= @OnCompImport;
FreeOnTerminate := True; PSScript.OnExecImport:= @OnExecImport;
Self.OnTerminate := @Self.OnThreadTerminate; PSScript.OnAfterExecute:= @AfterExecute;
inherited Create(CreateSuspended);
end; // Set some defines
{$I PSInc/psdefines.inc}
procedure TMMLPSThread.OnThreadTerminate(Sender: TObject);
begin
// Writeln('Terminating the thread'); FreeOnTerminate := True;
end; Self.OnTerminate := @Self.OnThreadTerminate;
inherited Create(CreateSuspended);
destructor TMMLPSThread.Destroy; end;
begin
SetLength(PluginsToLoad,0); procedure TMMLPSThread.OnThreadTerminate(Sender: TObject);
Client.Free; begin
PSScript.Free; // Writeln('Terminating the thread');
inherited; end;
end;
destructor TMMLPSThread.Destroy;
// include PS wrappers begin
{$I PSInc/Wrappers/other.inc} SetLength(PluginsToLoad,0);
{$I PSInc/Wrappers/bitmap.inc} Client.Free;
{$I PSInc/Wrappers/colour.inc} PSScript.Free;
{$I PSInc/Wrappers/math.inc} inherited;
{$I PSInc/Wrappers/mouse.inc} end;
{$I PSInc/Wrappers/dtm.inc}
// include PS wrappers
{$I PSInc/Wrappers/other.inc}
procedure TMMLPSThread.PSScriptProcessUnknowDirective(Sender: TPSPreProcessor; {$I PSInc/Wrappers/bitmap.inc}
Parser: TPSPascalPreProcessorParser; const Active: Boolean; {$I PSInc/Wrappers/colour.inc}
const DirectiveName, DirectiveParam: string; var Continue: Boolean); {$I PSInc/Wrappers/math.inc}
var {$I PSInc/Wrappers/mouse.inc}
TempNum : integer; {$I PSInc/Wrappers/dtm.inc}
I: integer;
begin
if DirectiveName= 'LOADDLL' then procedure TMMLPSThread.PSScriptProcessUnknowDirective(Sender: TPSPreProcessor;
if DirectiveParam <> '' then Parser: TPSPascalPreProcessorParser; const Active: Boolean;
begin; const DirectiveName, DirectiveParam: string; var Continue: Boolean);
TempNum := PluginsGlob.LoadPlugin(DirectiveParam); var
if TempNum < 0 then TempNum : integer;
Writeln(Format('Your DLL %s has not been found',[DirectiveParam])) I: integer;
else begin
begin; if DirectiveName= 'LOADDLL' then
for i := High(PluginsToLoad) downto 0 do if DirectiveParam <> '' then
if PluginsToLoad[i] = TempNum then begin;
Exit; TempNum := PluginsGlob.LoadPlugin(DirectiveParam);
SetLength(PluginsToLoad,Length(PluginsToLoad)+1); if TempNum < 0 then
PluginsToLoad[High(PluginsToLoad)] := TempNum; Writeln(Format('Your DLL %s has not been found',[DirectiveParam]))
end; else
end; begin;
Continue:= True; for i := High(PluginsToLoad) downto 0 do
end; if PluginsToLoad[i] = TempNum then
Exit;
procedure TMMLPSThread.OnCompile(Sender: TPSScript); SetLength(PluginsToLoad,Length(PluginsToLoad)+1);
var PluginsToLoad[High(PluginsToLoad)] := TempNum;
i,ii : integer; end;
begin end;
for i := high(PluginsToLoad) downto 0 do Continue:= True;
for ii := 0 to PluginsGlob.MPlugins[PluginsToLoad[i]].MethodLen - 1 do end;
PSScript.AddFunctionEx(PluginsGlob.MPlugins[PluginsToLoad[i]].Methods[i].FuncPtr,
PluginsGlob.MPlugins[PluginsToLoad[i]].Methods[i].FuncStr, cdStdCall); procedure TMMLPSThread.OnCompile(Sender: TPSScript);
// Here we add all the functions to the engine. var
{$I PSInc/pscompile.inc} i,ii : integer;
end; begin
for i := high(PluginsToLoad) downto 0 do
procedure TMMLPSThread.AfterExecute(Sender: TPSScript); for ii := 0 to PluginsGlob.MPlugins[PluginsToLoad[i]].MethodLen - 1 do
begin PSScript.AddFunctionEx(PluginsGlob.MPlugins[PluginsToLoad[i]].Methods[i].FuncPtr,
//Here we add all the Script-freeing-leftovers (like BMParray etc) PluginsGlob.MPlugins[PluginsToLoad[i]].Methods[i].FuncStr, cdStdCall);
// ^ This will all be done with Client.Destroy; // Here we add all the functions to the engine.
end; {$I PSInc/pscompile.inc}
end;
function TMMLPSThread.RequireFile(Sender: TObject;
const OriginFileName: String; var FileName, OutPut: string): Boolean; procedure TMMLPSThread.AfterExecute(Sender: TPSScript);
begin begin
//Here we add all the Script-freeing-leftovers (like BMParray etc)
Result := False; // ^ This will all be done with Client.Destroy;
end; end;
procedure TMMLPSThread.OnCompImport(Sender: TObject; x: TPSPascalCompiler); function TMMLPSThread.RequireFile(Sender: TObject;
begin const OriginFileName: String; var FileName, OutPut: string): Boolean;
SIRegister_Std(x); begin
SIRegister_Controls(x);
SIRegister_Classes(x, true); Result := False;
SIRegister_Graphics(x, true); end;
SIRegister_stdctrls(x);
SIRegister_Forms(x); procedure TMMLPSThread.OnCompImport(Sender: TObject; x: TPSPascalCompiler);
SIRegister_ExtCtrls(x); begin
end; SIRegister_Std(x);
SIRegister_Controls(x);
procedure TMMLPSThread.OnExecImport(Sender: TObject; se: TPSExec; SIRegister_Classes(x, true);
x: TPSRuntimeClassImporter); SIRegister_Graphics(x, true);
begin SIRegister_stdctrls(x);
RIRegister_Std(x); SIRegister_Forms(x);
RIRegister_Classes(x, True); SIRegister_ExtCtrls(x);
RIRegister_Controls(x); end;
RIRegister_Graphics(x, True);
RIRegister_stdctrls(x); procedure TMMLPSThread.OnExecImport(Sender: TObject; se: TPSExec;
RIRegister_Forms(x); x: TPSRuntimeClassImporter);
RIRegister_ExtCtrls(x); begin
end; RIRegister_Std(x);
RIRegister_Classes(x, True);
procedure TMMLPSThread.OutputMessages; RIRegister_Controls(x);
var RIRegister_Graphics(x, True);
l: Longint; RIRegister_stdctrls(x);
b: Boolean; RIRegister_Forms(x);
begin RIRegister_ExtCtrls(x);
b := False; end;
for l := 0 to PSScript.CompilerMessageCount - 1 do
begin procedure TMMLPSThread.OutputMessages;
psWriteln(PSScript.CompilerErrorToStr(l)); var
if (not b) and (PSScript.CompilerMessages[l] is TIFPSPascalCompilerError) then l: Longint;
begin b: Boolean;
b := True; begin
// FormMain.CurrSynEdit.SelStart := PSScript.CompilerMessages[l].Pos; b := False;
for l := 0 to PSScript.CompilerMessageCount - 1 do
end; begin
end; psWriteln(PSScript.CompilerErrorToStr(l));
end; if (not b) and (PSScript.CompilerMessages[l] is TIFPSPascalCompilerError) then
begin
procedure TMMLPSThread.Execute; b := True;
var // FormMain.CurrSynEdit.SelStart := PSScript.CompilerMessages[l].Pos;
time: DWord;
begin; end;
CurrThread := Self; end;
time := lclintf.GetTickCount; end;
try
if PSScript.Compile then procedure TMMLPSThread.Execute;
begin var
OutputMessages; time: DWord;
psWriteln('Compiled succesfully in ' + IntToStr(GetTickCount - time) + ' ms.'); begin;
// if not (ScriptState = SCompiling) then CurrThread := Self;
if not PSScript.Execute then time := lclintf.GetTickCount;
begin try
// FormMain.CurrSynEdit.SelStart := Script.PSScript.ExecErrorPosition; if PSScript.Compile then
psWriteln(PSScript.ExecErrorToString +' at '+Inttostr(PSScript.ExecErrorProcNo)+'.' begin
+Inttostr(PSScript.ExecErrorByteCodePosition)); OutputMessages;
end else psWriteln('Succesfully executed'); psWriteln('Compiled succesfully in ' + IntToStr(GetTickCount - time) + ' ms.');
end else // if not (ScriptState = SCompiling) then
begin if not PSScript.Execute then
OutputMessages; begin
psWriteln('Compiling failed'); // FormMain.CurrSynEdit.SelStart := Script.PSScript.ExecErrorPosition;
end; psWriteln(PSScript.ExecErrorToString +' at '+Inttostr(PSScript.ExecErrorProcNo)+'.'
except +Inttostr(PSScript.ExecErrorByteCodePosition));
on E : Exception do end else psWriteln('Succesfully executed');
psWriteln('Error: ' + E.Message); end else
end; begin
end; OutputMessages;
psWriteln('Compiling failed');
procedure TMMLPSThread.SetPSScript(Script: string); end;
begin except
PSScript.Script.Text:= Script; on E : Exception do
end; psWriteln('Error: ' + E.Message);
end;
procedure TMMLPSThread.SetDebug(Strings: TMemo); end;
begin
DebugTo := Strings; procedure TMMLPSThread.SetPSScript(Script: string);
end; begin
PSScript.Script.Text:= Script;
end;
{ Include stuff here? }
procedure TMMLPSThread.SetDebug(Strings: TMemo);
//{$I inc/colors.inc} begin
//{$I inc/bitmaps.inc} DebugTo := Strings;
end;
end.
{ Include stuff here? }
//{$I inc/colors.inc}
//{$I inc/bitmaps.inc}
end.