-
Notifications
You must be signed in to change notification settings - Fork 0
/
mv.PluginManager.pas
309 lines (260 loc) · 8.53 KB
/
mv.PluginManager.pas
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
unit mv.PluginManager;
interface
uses
System.SysUtils, System.StrUtils, System.Classes, System.IniFiles,
System.Generics.Collections, System.IOUtils,
FMX.Dialogs, mv.Logger, mv.WebView.BrowserForm, mv.PluginService;
type
TPluginManager = class;
TPlugin = class
private
FDirectory: string;
FSettings: TStringList;
FDirName: string;
FServices: TStringList;
FActivePluginManager: TPluginManager;
FServiceContext: string;
public
constructor Create;
destructor Destroy; override;
procedure LoadFromDirectory(const Dir: string);
property Settings: TStringList read FSettings;
property DirName: string read FDirName;
function ReadFile(AFilename: string): string;
function GetFilePath(AFilename: string): string;
procedure WriteFile(AFilename, AValue: string);
//procedure AttachService(AServiceName: string; AService: TPluginService);
function HasPluginPageForCommand(ACommand: TArray<System.string>): Boolean;
function LoadPluginPageForCommand(ACommand: TArray<System.string>; ABrowserForm: TBrowserForm; WebPort: integer): Boolean;
property ActivePluginManager: TPluginManager read FActivePluginManager write FActivePluginManager;
property ServiceContext: string read FServiceContext write FServiceContext;
function GetService(const ClassName: string): TPluginService;
end;
TPluginManager = class
private
FPluginsDirectory: string;
FPlugins: TObjectList<TPlugin>;
FActiveLayoutFilename: string;
function GetPlugin(Index: Integer): TPlugin;
public
constructor Create;
destructor Destroy; override;
function LoadPlugins: integer;
property PluginsDirectory: string read FPluginsDirectory;
function PluginCount: integer;
property Plugins[Index: Integer]: TPlugin read GetPlugin; default;
function FindPluginByCommandWord(ACommand: String): TPlugin;
function FindPluginInstance(ACommand: string): TPlugin;
property ActiveLayoutFilename: string read FActiveLayoutFilename write FActiveLayoutFilename;
end;
implementation
uses
mv.StringUtils, mv.PluginStorageService;
{ TPlugin }
//procedure TPlugin.AttachService(AServiceName: string; AService: TPluginService);
//begin
// FServices.AddObject(AServiceName, AService);
//end;
constructor TPlugin.Create;
begin
FSettings := TStringList.Create;
FServices := TStringList.Create;
end;
destructor TPlugin.Destroy;
begin
FSettings.Free;
inherited;
end;
procedure TPlugin.LoadFromDirectory(const Dir: string);
var
IniFile: String;
Ini: TIniFile;
Rec: TSearchRec;
Section, Key, Value: string;
Sections, Keys: TStringList;
begin
FDirectory := Dir;
FDirName := ExtractFileName(Dir);
// Load settings from INI
IniFile := Dir + '\' + LowerCase(ExtractFileName(Dir)) + '.ini';
if not FileExists(IniFile) then begin
ShowMessage('Plugin config file does not exist: ' + IniFile);
Exit;
end;
Ini := TIniFile.Create(IniFile);
try
Sections := TStringList.Create;
Keys := TStringList.Create;
try
Ini.ReadSections(Sections);
for Section in Sections do
begin
Keys.Clear;
Ini.ReadSection(Section, Keys);
for Key in Keys do
begin
Value := Ini.ReadString(Section, Key, '');
FSettings.Values[Section + '/' + Key] := Value;
end;
end;
finally
Keys.Free;
Sections.Free;
end;
finally
Ini.Free;
end;
end;
function TPlugin.HasPluginPageForCommand(ACommand: TArray<System.string>): Boolean;
begin
Result := '' <> Settings.Values['plugin/command_' + ACommand[0]];
end;
function TPlugin.LoadPluginPageForCommand(ACommand: TArray<System.string>; ABrowserForm: TBrowserForm; WebPort: integer): Boolean;
var
PluginPage: string;
PluginPageHTML: string;
AssetsDir: string;
PluginsDir: string;
FileURL: string;
oService: TObject;
Service: TPluginService;
ServiceName: string;
begin
PluginPage := Settings.Values['plugin/command_' + ACommand[0]];
(*
PluginPageHTML := Self.ReadFile(PluginPage);
// Inject StorageService JS function
{PluginPageHTML := ReplaceStr(PluginPageHTML, '</head>',
'<script>'
+TTMSFNCWebBrowser(FCommandControl).GetBridgeCommunicationLayer('storageservice')
+'</script></head>');}
// Convert line endings to be consistent so we can use the JS debuffer
PluginPageHTML := StandardizeLineEndings(PluginPageHTML);
// Find Assets directory, in installed environment or in debug environment
AssetsDir := '/Assets';
PluginsDir := '/Plugins';
// Swap in template tokens
PluginPageHTML := ReplaceStr(PluginPageHTML, '{assets}', AssetsDir);
PluginPageHTML := ReplaceStr(PluginPageHTML, '{plugins}', PluginsDir);
PluginPageHTML := ReplaceStr(PluginPageHTML, '{plugin_dir}',
PluginsDir+'/'+Self.DirName);
// Store next to the original plugin file with a prefix to the filename
Self.WriteFile('eval_' + PluginPage, PluginPageHTML);
*)
// Setup URL to the prepared HTML file via the Deno service.
FileURL := 'http://127.0.0.1:' + IntToStr(WebPort) + '/plugins/'
+Self.DirName
+'/'
+PluginPage
+'?C='
+String.Join(';',ACommand);
// Initialize services if they haven't already been loaded (this function can
// be called for the same plugin many times to re/load it).
if -1 = FServices.IndexOf('TPluginStorageService') then
begin
FServices.AddObject('TPluginStorageService', TPluginStorageService.Create(JoinString(ACommand)));
end;
//
// // Attach services
// for oService in FServices.ToObjectArray do
// begin
// Service := oService as TPluginService;
// ServiceName := Service.ClassName;
// ABrowserForm.AddService('TPluginStorageService', TPluginStorageService.Create(JoinString(ACommand)).OleObject);
// end;
// Load it! (eventually!)
ABrowserForm.WVFMXBrowser1.DefaultURL := FileURL;
end;
function TPlugin.ReadFile(AFilename: string): string;
begin
Result := TFile.ReadAllText(FDirectory + '\' + AFilename);
end;
function TPlugin.GetFilePath(AFilename: string): string;
begin
Result := FDirectory + '\' + AFilename;
end;
function TPlugin.GetService(const ClassName: string): TPluginService;
var
I: integer;
begin
Result := nil;
I := FServices.IndexOf(ClassName);
if I > -1 then
Result := FServices.Objects[I] as TPluginService;
end;
procedure TPlugin.WriteFile(AFilename: string; AValue: string);
begin
TFile.WriteAllText(FDirectory + '\' + AFilename, AValue);
end;
{ TPluginManager }
constructor TPluginManager.Create;
begin
FPlugins := TObjectList<TPlugin>.Create;
end;
destructor TPluginManager.Destroy;
begin
FPlugins.Free;
inherited;
end;
function TPluginManager.GetPlugin(Index: Integer): TPlugin;
begin
Result := FPlugins[Index];
end;
function TPluginManager.LoadPlugins: integer;
var
Rec: TSearchRec;
Plugin: TPlugin;
Count: integer;
Pattern: string;
begin
Count := 0;
FPluginsDirectory := 'Plugins';
if DirectoryExists('..\Plugins') then FPluginsDirectory := '..\Plugins';
if DirectoryExists('..\..\Plugins') then FPluginsDirectory := '..\..\Plugins';
Pattern := FPluginsDirectory + '\*';
if FindFirst(Pattern, faDirectory, Rec) = 0 then
begin
repeat
if (Rec.Name <> '.') and (Rec.Name <> '..') then
begin
Plugin := TPlugin.Create;
try
Plugin.LoadFromDirectory(ReplaceStr(Pattern, '*', Rec.Name));
Plugin.ActivePluginManager := Self;
FPlugins.Add(Plugin);
mv.Logger.Log(Format('Load plugin "%s"', [Rec.Name]));
Inc(Count);
except
mv.Logger.Log(Format('Error loading plugin "%s"', [Rec.Name]));
Plugin.Free;
raise;
end;
end;
until FindNext(Rec) <> 0;
FindClose(Rec);
end;
Result := Count;
end;
function TPluginManager.PluginCount: integer;
begin
Result := FPlugins.Count;
end;
function TPluginManager.FindPluginByCommandWord(ACommand: String): TPlugin;
var
I: Integer;
Value: String;
begin
Result := nil;
for I := 0 to FPlugins.Count - 1 do
begin
Value := FPlugins[I].Settings.Values['plugin/command'];
if Value = ACommand then
begin
Result := FPlugins[I];
end;
end;
end;
function TPluginManager.FindPluginInstance(ACommand: string): TPlugin;
begin
end;
end.