GXS.ScriptBase.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  3. //
  4. unit GXS.ScriptBase;
  5. (*
  6. An abstract scripting interface for GLScene
  7. This unit provides the base methods for compiling and executing scripts as
  8. well as calling scripted functions. No scripting APIs are implemented here,
  9. only abstracted functions.
  10. *)
  11. interface
  12. uses
  13. System.Classes,
  14. GXS.XCollection;
  15. type
  16. TgxScriptState = (ssUncompiled, // The script has yet to be compiled.
  17. ssCompileErrors, // Errors occurred while compiling.
  18. ssCompiled, // The script has been compiled and is
  19. // ready to be executed/started.
  20. ssRunningErrors, // Errors occured while the script was
  21. // running.
  22. ssRunning); // The script is currently active and
  23. // is running without error.
  24. (* The base script class that defines the abstract functions and properties.
  25. Don't use this class directly, use the script classes descended from this
  26. base class. *)
  27. TgxScriptBase = class(TXCollectionItem)
  28. private
  29. FText: TStringList;
  30. FDescription: String;
  31. FErrors: TStringList; // not persistent
  32. protected
  33. procedure WriteToFiler(writer: TWriter); override;
  34. procedure ReadFromFiler(reader: TReader); override;
  35. function GetState: TgxScriptState; virtual; abstract;
  36. procedure SetText(const Value: TStringList);
  37. procedure Notification(AComponent: TComponent;
  38. Operation: TOperation); virtual;
  39. public
  40. constructor Create(aOwner: TXCollection); override;
  41. destructor Destroy; override;
  42. procedure Assign(Source: TPersistent); override;
  43. procedure Compile; virtual; abstract;
  44. procedure Start; virtual; abstract;
  45. procedure Stop; virtual; abstract;
  46. procedure Execute; virtual; abstract;
  47. procedure Invalidate; virtual; abstract;
  48. function Call(aName: String; aParams: array of Variant): Variant;
  49. virtual; abstract;
  50. property Errors: TStringList read FErrors;
  51. property State: TgxScriptState read GetState;
  52. published
  53. property Text: TStringList read FText write SetText;
  54. property Description: String read FDescription write FDescription;
  55. end;
  56. // XCollection descendant for storing and handling scripts.
  57. TgxScripts = class(TXCollection)
  58. protected
  59. function GetItems(index: Integer): TgxScriptBase;
  60. public
  61. procedure Assign(Source: TPersistent); override;
  62. class function ItemsClass: TXCollectionItemClass; override;
  63. function CanAdd(aClass: TXCollectionItemClass): Boolean; override;
  64. property Items[index: Integer]: TgxScriptBase read GetItems; default;
  65. end;
  66. (* Encapsulation of the scripts XCollection to help with script handling at
  67. design-time. Links the scripts to Delphi's persistence model. *)
  68. TgxScriptLibrary = class(TComponent)
  69. private
  70. FScripts: TgxScripts;
  71. protected
  72. procedure DefineProperties(Filer: TFiler); override;
  73. procedure WriteScriptsData(Stream: TStream);
  74. procedure ReadScriptsData(Stream: TStream);
  75. procedure Loaded; override;
  76. procedure Notification(AComponent: TComponent;
  77. Operation: TOperation); override;
  78. public
  79. constructor Create(aOwner: TComponent); override;
  80. destructor Destroy; override;
  81. published
  82. property Scripts: TgxScripts read FScripts;
  83. end;
  84. implementation // ------------------------------------------------------------
  85. // ---------------
  86. // --------------- TgxScriptBase ---------------
  87. // ---------------
  88. constructor TgxScriptBase.Create(aOwner: TXCollection);
  89. begin
  90. inherited;
  91. FText := TStringList.Create;
  92. FErrors := TStringList.Create;
  93. end;
  94. destructor TgxScriptBase.Destroy;
  95. begin
  96. FText.Free;
  97. FErrors.Free;
  98. inherited;
  99. end;
  100. procedure TgxScriptBase.Assign(Source: TPersistent);
  101. begin
  102. inherited;
  103. if Source is TgxScriptBase then
  104. begin
  105. Text.Assign(TgxScriptBase(Source).Text);
  106. Description := TgxScriptBase(Source).Description;
  107. end;
  108. end;
  109. procedure TgxScriptBase.ReadFromFiler(reader: TReader);
  110. var
  111. archiveVersion: Integer;
  112. begin
  113. inherited;
  114. archiveVersion := reader.ReadInteger;
  115. Assert(archiveVersion = 0);
  116. with reader do
  117. begin
  118. FText.Text := ReadString;
  119. FDescription := ReadString;
  120. end;
  121. end;
  122. procedure TgxScriptBase.WriteToFiler(writer: TWriter);
  123. begin
  124. inherited;
  125. writer.WriteInteger(0);
  126. with writer do
  127. begin
  128. WriteString(FText.Text);
  129. WriteString(FDescription);
  130. end;
  131. end;
  132. procedure TgxScriptBase.SetText(const Value: TStringList);
  133. begin
  134. Text.Assign(Value);
  135. end;
  136. procedure TgxScriptBase.Notification(AComponent: TComponent;
  137. Operation: TOperation);
  138. begin
  139. // Virtual
  140. end;
  141. // ---------------
  142. // --------------- TgxScripts ---------------
  143. // ---------------
  144. procedure TgxScripts.Assign(Source: TPersistent);
  145. begin
  146. inherited;
  147. // Nothing yet
  148. end;
  149. function TgxScripts.GetItems(index: Integer): TgxScriptBase;
  150. begin
  151. Result := TgxScriptBase(inherited GetItems(index));
  152. end;
  153. class function TgxScripts.ItemsClass: TXCollectionItemClass;
  154. begin
  155. Result := TgxScriptBase;
  156. end;
  157. function TgxScripts.CanAdd(aClass: TXCollectionItemClass): Boolean;
  158. begin
  159. Result := aClass.InheritsFrom(TgxScriptBase);
  160. end;
  161. // ---------------
  162. // --------------- TgxScriptLibrary ---------------
  163. // ---------------
  164. constructor TgxScriptLibrary.Create(aOwner: TComponent);
  165. begin
  166. inherited;
  167. FScripts := TgxScripts.Create(Self);
  168. end;
  169. destructor TgxScriptLibrary.Destroy;
  170. begin
  171. FScripts.Free;
  172. inherited;
  173. end;
  174. procedure TgxScriptLibrary.DefineProperties(Filer: TFiler);
  175. begin
  176. inherited;
  177. Filer.DefineBinaryProperty('ScriptsData', ReadScriptsData, WriteScriptsData,
  178. (Scripts.Count > 0));
  179. end;
  180. procedure TgxScriptLibrary.WriteScriptsData(Stream: TStream);
  181. var
  182. writer: TWriter;
  183. begin
  184. writer := TWriter.Create(Stream, 16384);
  185. try
  186. Scripts.WriteToFiler(writer);
  187. finally
  188. writer.Free;
  189. end;
  190. end;
  191. procedure TgxScriptLibrary.ReadScriptsData(Stream: TStream);
  192. var
  193. reader: TReader;
  194. begin
  195. reader := TReader.Create(Stream, 16384);
  196. try
  197. Scripts.ReadFromFiler(reader);
  198. finally
  199. reader.Free;
  200. end;
  201. end;
  202. procedure TgxScriptLibrary.Loaded;
  203. begin
  204. inherited;
  205. Scripts.Loaded;
  206. end;
  207. procedure TgxScriptLibrary.Notification(AComponent: TComponent;
  208. Operation: TOperation);
  209. var
  210. i: Integer;
  211. begin
  212. if Assigned(Scripts) then
  213. for i := 0 to Scripts.Count - 1 do
  214. Scripts[i].Notification(AComponent, Operation);
  215. inherited;
  216. end;
  217. initialization //----------------------------------------------------------
  218. RegisterClasses([TgxScriptLibrary, TgxScripts, TgxScriptBase]);
  219. //-------------------------------------------------------------------------
  220. end.