aboutsummaryrefslogtreecommitdiff
path: root/Source/pluginunit.pas
blob: 12d7465adb1e2452e29ef8e44f7a627f81d1f456 (plain)
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
{ Copyright (C) 2001-2002 Juha Vehviläinen
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.}

unit pluginunit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DirectX, DXDraws;

type
  TEffectProc = procedure(const Bits: Pointer;
   const lPitch, Width, Height, BitCount: Integer;
   const args: PChar;
   const ret: PChar); cdecl;

  TCopyProc = procedure(
   const Bits1: Pointer;
   const lPitch1, Width1, Height1, BitCount1: Integer;
   const Bits2: Pointer;
   const lPitch2, Width2, Height2, BitCount2: Integer;
   const args: PChar;
   const ret: PChar
   ); cdecl;

  TInfoProc = procedure(const str: PChar); cdecl;

  TPointerList = TList;
  TLibraryList = TList;

  TPlugins = class(TComponent)
  private
    EffectProcs: TPointerList;
    CopyProcs: TPointerList;
    InfoProcs: TPointerList;
    Libs: TLibraryList;
    procedure LoadHandleFile(const SearchRec: TSearchRec;
     const FullPath: String);
  public
    Names: TStringList;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Load;
    procedure Reload;
    procedure Clear;
    // IsPlugins sets Index if S is a plugin
    function IsPlugin(var Index: Integer; const s: String): Boolean;
    function CallEffect(const d: TDirectDrawSurface;
     const procIndex: Integer; const args: String): Boolean;
    function CallCopy(const d1: TDirectDrawSurface;
     const d2: TDirectDrawSurface;
     const procIndex: Integer; const args: String): Boolean;
    function Info(const procIndex: Integer): String;
  end;

implementation

uses
  mainunit,
  Filez;

type
  THandlePtr = ^THandle;

const
  EffectProcName = 'perform_effect';
  CopyProcName = 'perform_copy';
  InfoProcName = 'info';

{ TPlugins }

constructor TPlugins.Create(AOwner: TComponent);
begin
  inherited;
  Names := TStringList.Create;
  EffectProcs := TPointerList.Create;
  CopyProcs := TPointerList.Create;
  InfoProcs := TPointerList.Create;
  Libs := TLibraryList.Create;
end;

destructor TPlugins.Destroy;
begin
  Clear;
  Names.Free;
  EffectProcs.Free;
  CopyProcs.Free;
  Libs.Free;
  inherited;
end;

procedure TPlugins.Clear;
var
  hp: THandlePtr;
begin
  while Names.Count>0 do Names.Delete(0);
  while EffectProcs.Count>0 do EffectProcs.Delete(0);
  while CopyProcs.Count>0 do CopyProcs.Delete(0);
  while Libs.Count>0 do begin
    hp := Libs[0];
    FreeLibrary(hp^);
    Libs.Delete(0);
  end;
end;

procedure TPlugins.LoadHandleFile(const SearchRec: TSearchRec;
  const FullPath: String);
var
  h: THandle;
  p: PChar;
  b: array[0..255] of Char;
  EffectProc: TEffectProc;
  CopyProc: TCopyProc;
  InfoProc: TInfoProc;
  s: String;
  i: Integer;
begin
  p:=@b;
  if UpperCase(ExtractFileExt(FullPath))='.DLL' then begin
    h := LoadLibrary(StrPCopy(p, FullPath));
    if h<>0 then begin
      @EffectProc := GetProcAddress(h, EffectProcName);
      @CopyProc := GetProcAddress(h, CopyProcName);
      @InfoProc := GetProcAddress(h, InfoProcName);
      s := ExtractFileName(FullPath);
      i := Pos('.DLL', UpperCase(s));
      if i>0 then Delete(s, i, 255);
      Names.Add(s);
      EffectProcs.Add(@EffectProc);
      CopyProcs.Add(@CopyProc);
      InfoProcs.Add(@InfoProc);
      Libs.Add(@h);
    end;
  end;
end;

procedure TPlugins.Load;
var
  sd: TScanDir;
  s: String;
  i: Integer;
begin
  sd := TScanDir.Create(Self);
  sd.OnHandleFile := LoadHandleFile;
  sd.Scan(main.FSFolder+'\Plugins');
  sd.Free;
  if Names.Count>0 then begin
    s := '';
    for i:=0 to Names.Count-1 do begin
      if s<>'' then s:=s+' ';
      s:=s+Names[i];
    end;
//    main.Post('Plugins ('+IntToStr(Names.Count)+' loaded): '+s);
  end;
end;

procedure TPlugins.Reload;
begin
  Clear;
  Load;
end;

function TPlugins.IsPlugin(var Index: Integer; const s: String): Boolean;
begin
  Index := Names.IndexOf(s);
  Result := Index<>-1;
end;

var
  argsBuf: array[0..255] of Char;

function TPlugins.CallEffect(const d: TDirectDrawSurface;
  const procIndex: Integer; const args: String): Boolean;
var
  Proc: TEffectProc;
  sd: TDDSurfaceDesc;
  P: PChar;
begin
  Result := False;
  if (procIndex=-1) or (procIndex>=EffectProcs.Count) then Exit;
  @Proc := EffectProcs[procIndex];
  if @Proc=nil then Exit;
  P:=@argsBuf;
  P[0]:=#0;
  d.Lock(sd);
  Proc(sd.lpSurface, sd.lPitch, d.Width, d.Height, d.BitCount,
  PChar(args), P);
  d.UnLock;
  if P[0]<>#0 then
    main.SendReturnValues(StrPas(P));
  Result := True;
end;

function TPlugins.CallCopy(const d1, d2: TDirectDrawSurface;
  const procIndex: Integer; const args: String): Boolean;
var
  Proc: TCopyProc;
  sd1: TDDSurfaceDesc;
  sd2: TDDSurfaceDesc;
  P: PChar;
begin
  Result := False;
  if (procIndex=-1) or (procIndex>=CopyProcs.Count) then Exit;
  @Proc := CopyProcs[procIndex];
  if @Proc=nil then Exit;
  P:=@argsBuf;
  P[0]:=#0;
  d1.Lock(sd1);
  d2.Lock(sd2);
  Proc(sd1.lpSurface, sd1.lPitch, d1.Width, d1.Height, d1.BitCount,
   sd2.lpSurface, sd2.lPitch, d2.Width, d2.Height, d2.BitCount,
   PChar(args), P);
  d1.UnLock;
  d2.UnLock;
  if P[0]<>#0 then
    main.SendReturnValues(StrPas(P));
  Result := True;
end;

function TPlugins.Info(const procIndex: Integer): String;
var
  Proc: TInfoProc;
  buf: array[0..255] of Char;
  p: PChar;
begin
  if (procIndex=-1) or (procIndex>=InfoProcs.Count) then Exit;
  @Proc := InfoProcs[procIndex];
  if @Proc=nil then begin
    Result := '';
    Exit;
  end;
  buf[0]:=#0;
  p := @buf;
  Proc(p);
  Result := StrPas(p);
end;

end.