1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
unit uRavnosScriptEngine;
interface
uses Classes, SysUtils, Controls,
IniFiles;
type
TRavnosScriptStep = function(const AStepName: string): boolean of object;
TRavnosScriptAction = record
ID: Integer;
Text: string;
Next: string;
end;
TRavnosScriptActionDynArray = array of TRavnosScriptAction;
TRavnosScriptActionEvent = function(const Actions: TRavnosScriptActionDynArray): integer of object;
TRavnosScriptTextEvent = procedure(const Text: TStringList) of object;
TRavnosScriptQuestionEvent = function(const Text: TStringList; const Actions: TRavnosScriptActionDynArray): integer of object;
TRavnosScriptEngine = class(TPersistent)
private
FIniFile: TIniFile;
FOnAction: TRavnosScriptActionEvent;
FOnText: TRavnosScriptTextEvent;
FOnQuestion: TRavnosScriptQuestionEvent;
function ProcessStep(var AStepName: string): boolean;
public
destructor Destroy();
procedure Start(const ScriptFileName: TFileName);
property OnAction: TRavnosScriptActionEvent read FOnAction write FOnAction;
property OnText: TRavnosScriptTextEvent read FOnText write FOnText;
property OnQuestion: TRavnosScriptQuestionEvent read FOnQuestion write FOnQuestion;
published
function ProcessActions(var AStepName: string): boolean;
function ProcessText(var AStepName: string): boolean;
function ProcessQuestion(var AStepName: string): boolean;
end;
implementation
destructor TRavnosScriptEngine.Destroy();
begin
FreeAndNil(FIniFile);
inherited;
end;
procedure TRavnosScriptEngine.Start(const ScriptFileName: TFileName);
var
StepName: string;
begin
FIniFile.Free();
FIniFile := TIniFile.Create(ScriptFileName) ;
StepName := 'EntryPoint';
while ProcessStep(StepName) do;
end;
function TRavnosScriptEngine.ProcessStep(var AStepName: string): boolean;
var
StepMethodName: string;
AddressMethod: TMethod;
begin
Result := False;
if FIniFile.SectionExists(AStepName) then
begin
StepMethodName := FIniFile.ReadString(AStepName, 'MethodName', '');
AddressMethod.Code := Self.MethodAddress(StepMethodName);
AddressMethod.Data := Self;
if Assigned(AddressMethod.Code) then
Result := TRavnosScriptStep(AddressMethod)(AStepName);
end;
end;
function TRavnosScriptEngine.ProcessActions(var AStepName: string): boolean;
var
ActionCount: Integer;
iAction: Integer;
ActionName: string;
ActionLabel: string;
Actions: TRavnosScriptActionDynArray;
begin
Result := False;
if Assigned(FOnAction) then
begin
ActionCount := FIniFile.ReadInteger(AStepName, 'ActionCount', 0);
if ActionCount > 0 then
begin
SetLength(Actions, ActionCount);
for iAction := 0 to ActionCount - 1 do
begin
ActionName := Format('A%d', [iAction]);
Actions[iAction].ID := iAction;
Actions[iAction].Text := FIniFile.ReadString(AStepName, ActionName + '.Text', '');
Actions[iAction].Next := FIniFile.ReadString(AStepName, ActionName + '.Next', '');
end;
iAction := FOnAction(Actions);
if (Low(Actions) <= iAction) and (iAction <= High(Actions)) then
begin
AStepName := Actions[iAction].Next;
Result = AStepName <> '';
end;
end;
end;
end;
function TRavnosScriptEngine.ProcessText(var AStepName: string): boolean;
var
TextCount: Integer;
iText: Integer;
TextName: string;
Text: TStringList;
begin
Result := False;
if Assigned(FOnText) then
begin
TextCount := FIniFile.ReadInteger(AStepName, 'TextCount', 0);
if TextCount > 0 then
begin
Text := TStringList.Create();
try
Text.Capacity := TextCount;
for iText := 0 to TextCount - 1 do
begin
TextName := Format('T%d', [iText]);
Text.Add(FIniFile.ReadString(AStepName, TextName, ''));
end;
FOnText(Text);
AStepName = FIniFile.ReadString(AStepName, 'Next', '')
Result := AStepName <> '';
finally
Text.Free();
end;
end;
end;
end;
function TRavnosScriptEngine.ProcessQuestion(var AStepName: string): boolean;
var
QuestionCount: Integer;
iQuestion: Integer;
QuestionName: string;
Question: TStringList;
ResponseCount: Integer;
iResponse: Integer;
ResponseName: string;
ResponseLabel: string;
Responses: TRavnosScriptResponseDynArray;
begin
Result := False;
if Assigned(FOnQuestion) then
begin
QuestionCount := FIniFile.ReadInteger(AStepName, 'QuestionCount', 0);
ResponseCount := FIniFile.ReadInteger(AStepName, 'ResponseCount', 0);
if (QuestionCount > 0) and ( ResponseCount > 0) then
begin
Question := TStringList.Create();
try
Question.Capacity := QuestionCount;
for iQuestion := 0 to QuestionCount - 1 do
begin
QuestionName := Format('Q%d', [iQuestion]);
Question.Add(FIniFile.ReadString(AStepName, QuestionName, ''));
end;
SetLength(Responses, ResponseCount);
for iResponse := 0 to ResponseCount - 1 do
begin
ResponseName := Format('R%d', [iResponse]);
Responses[iResponse].ID := iResponse;
Responses[iResponse].Text := FIniFile.ReadString(AStepName, ResponseName + '.Text', '');
Responses[iResponse].Next := FIniFile.ReadString(AStepName, ResponseName + '.Next', '');
end;
iResponse := FOnResponse(Responses);
if (Low(Responses) <= iResponse) and (iResponse <= High(Responses)) then
begin
AStepName := Responses[iResponse].Next;
Result = AStepName <> '';
end;
finally
Text.Free();
end;
end;
end;
end;
end. |
Partager