aboutsummaryrefslogtreecommitdiff
path: root/Source/pluginunit.pas
blob: d4fd51ed545c7302eb941ec3c0ec6a9c92a2bfec (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
{ Copyright (C) 2001 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;

  TPointerList = TList;
  TLibraryList = TList;

  TPlugins = class(TComponent)
  private
    Names: TStringList;
    EffectProcs: TPointerList;
    CopyProcs: TPointerList;
    Libs: TLibraryList;
    procedure LoadHandleFile(const SearchRec: TSearchRec;
     const FullPath: String);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Load;
    procedure Reload;
    procedure Clear;
    function IsPlugin(const s: String): Boolean;
    function CallEffect(const d: TDirectDrawSurface;
     const procname: String; const args: String): Boolean;
    function CallCopy(const d1: TDirectDrawSurface;
     const d2: TDirectDrawSurface;
     const procname: String; const args: String): Boolean;
  end;

implementation

uses
  mainunit,
  Filez;

type
  THandlePtr = ^THandle;

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

{ TPlugins }

constructor TPlugins.Create(AOwner: TComponent);
begin
  inherited;
  Names := TStringList.Create;
  EffectProcs := TPointerList.Create;
  CopyProcs := 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;
  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);
      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);
      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(ExtractFilePath(Application.ExeName)+'\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: '+s);
  end;
end;

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

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

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

function TPlugins.CallEffect(const d: TDirectDrawSurface;
  const procname: String; const args: String): Boolean;
var
  i: Integer;
  Proc: TEffectProc;
  sd: TDDSurfaceDesc;
  P: PChar;
begin
  Result := False;
  if (Names.Count=0) or (d.Width=0) or (d.Height=0) then Exit;
  i := Names.IndexOf(procname);
  if (i=-1) or (i>=EffectProcs.Count) then Exit;
  @Proc := EffectProcs[i];
  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 procname: String; const args: String): Boolean;
var
  i: Integer;
  Proc: TCopyProc;
  sd1: TDDSurfaceDesc;
  sd2: TDDSurfaceDesc;
  P: PChar;
begin
  Result := False;
  if (Names.Count=0) or
   (d1.Width=0) or (d1.Height=0) or
   (d2.Width=0) or (d2.Height=0) then Exit;
  i := Names.IndexOf(procname);
  if (i=-1) or (i>=CopyProcs.Count) then Exit;
  @Proc := CopyProcs[i];
  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;

end.