Debian Codegraph @

Contrib/ExDLL/nsis.pas

raw
1 {
2 Original Code from
3 (C) 2001 - Peter Windridge
4
5 Code in separate unit and some changes
6 2003 by Bernhard Mayer
7
8 Fixed and formatted by Brett Dever
9 http://editor.nfscheats.com/
10
11 simply include this unit in your plugin project and export
12 functions as needed
13 }
14
15 unit nsis;
16
17 interface
18
19 uses
20 windows, SysUtils
21 {$IFNDEF FPC}
22 ,CommCtrl
23 {$ENDIF}
24 {$IF FPC_FULLVERSION < 30000} ; This is probably wrong
25 ,CommCtrl
26 {$ENDIF}
27 ;
28
29 {$IFDEF UNICODE}
30 type NSISTString = System.WideString; // UnicodeString?
31 type NSISPTChar = PWideChar;
32 {$ELSE}
33 type NSISTString = AnsiString;
34 type NSISPTChar = PAnsiChar;
35 {$ENDIF}
36
37 type
38 VarConstants = (
39 INST_0, // $0
40 INST_1, // $1
41 INST_2, // $2
42 INST_3, // $3
43 INST_4, // $4
44 INST_5, // $5
45 INST_6, // $6
46 INST_7, // $7
47 INST_8, // $8
48 INST_9, // $9
49 INST_R0, // $R0
50 INST_R1, // $R1
51 INST_R2, // $R2
52 INST_R3, // $R3
53 INST_R4, // $R4
54 INST_R5, // $R5
55 INST_R6, // $R6
56 INST_R7, // $R7
57 INST_R8, // $R8
58 INST_R9, // $R9
59 INST_CMDLINE, // $CMDLINE
60 INST_INSTDIR, // $INSTDIR
61 INST_OUTDIR, // $OUTDIR
62 INST_EXEDIR, // $EXEDIR
63 INST_LANG, // $LANGUAGE
64 __INST_LAST
65 );
66 TVariableList = INST_0..__INST_LAST;
67
68 type
69 PluginCallbackMessages = (
70 NSPIM_UNLOAD, // This is the last message a plugin gets, do final cleanup
71 NSPIM_GUIUNLOAD // Called after .onGUIEnd
72 );
73 TNSPIM = NSPIM_UNLOAD..NSPIM_GUIUNLOAD;
74
75 //TPluginCallback = function (const NSPIM: Integer): Pointer; cdecl;
76
77 TExecuteCodeSegment = function (const funct_id: Integer; const parent: HWND): Integer; stdcall;
78 Tvalidate_filename = procedure (const filename: NSISPTChar); stdcall;
79 TRegisterPluginCallback = function (const DllInstance: HMODULE; const CallbackFunction: Pointer): Integer; stdcall;
80
81 pexec_flags_t = ^exec_flags_t;
82 exec_flags_t = record
83 autoclose: Integer;
84 all_user_var: Integer;
85 exec_error: Integer;
86 abort: Integer;
87 exec_reboot: Integer;
88 reboot_called: Integer;
89 XXX_cur_insttype: Integer;
90 plugin_api_version: Integer;
91 silent: Integer;
92 instdir_error: Integer;
93 rtl: Integer;
94 errlvl: Integer;
95 alter_reg_view: Integer;
96 status_update: Integer;
97 end;
98
99 pextrap_t = ^extrap_t;
100 extrap_t = record
101 exec_flags: Pointer; // exec_flags_t;
102 exec_code_segment: TExecuteCodeSegment; // TFarProc;
103 validate_filename: Pointer; // Tvalidate_filename;
104 RegisterPluginCallback: Pointer; //TRegisterPluginCallback;
105 end;
106
107 pstack_t = ^stack_t;
108 stack_t = record
109 next: pstack_t;
110 text: NSISPTChar;
111 end;
112
113 var
114 g_stringsize: integer;
115 g_stacktop: ^pstack_t;
116 g_variables: NSISPTChar;
117 g_hwndParent: HWND;
118 g_hwndList: HWND;
119 g_hwndLogList: HWND;
120 g_extraparameters: pextrap_t;
121
122 procedure Init(const hwndParent: HWND; const string_size: integer; const variables: NSISPTChar; const stacktop: pointer; const extraparameters: pointer = nil);
123 function LogMessage(Msg : String): BOOL;
124 function Call(NSIS_func : String) : Integer;
125 function PopString(): NSISTString;
126 procedure PushString(const str: NSISTString='');
127 function GetUserVariable(const varnum: TVariableList): NSISTString;
128 procedure SetUserVariable(const varnum: TVariableList; const value: NSISTString);
129 procedure NSISDialog(const text, caption: NSISTString; const buttons: integer);
130
131 implementation
132
133 procedure Init(const hwndParent: HWND; const string_size: integer; const variables: NSISPTChar; const stacktop: pointer; const extraparameters: pointer = nil);
134 begin
135 g_stringsize := string_size;
136 g_hwndParent := hwndParent;
137 g_stacktop := stacktop;
138 g_variables := variables;
139 g_hwndList := FindWindowEx(FindWindowEx(g_hwndParent, 0, '#32770', nil), 0,'SysListView32', nil);
140 g_extraparameters := extraparameters;
141 end;
142
143
144 function Call(NSIS_func : String) : Integer;
145 var
146 codeoffset: Integer; //The ID of nsis function
147 begin
148 Result := 0;
149 codeoffset := StrToIntDef(NSIS_func, 0);
150 if (codeoffset <> 0) and (g_extraparameters <> nil) then
151 begin
152 codeoffset := codeoffset - 1;
153 Result := g_extraparameters.exec_code_segment(codeoffset, g_hwndParent);
154 end;
155 end;
156
157 function LogMessage(Msg : String): BOOL;
158 var
159 ItemCount : Integer;
160 item: TLVItem;
161 begin
162 Result := FAlse;
163 if g_hwndList = 0 then exit;
164 FillChar( item, sizeof(item), 0 );
165 ItemCount := SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
166 item.iItem := ItemCount;
167 item.mask := LVIF_TEXT;
168 item.pszText := PChar(Msg); // Unicode bug?
169 ListView_InsertItem(g_hwndList, item);
170 ListView_EnsureVisible(g_hwndList, ItemCount, not 0);
171 end;
172
173 function PopString(): NSISTString;
174 var
175 th: pstack_t;
176 begin
177 if NativeUInt(g_stacktop^) <> 0 then begin
178 th := g_stacktop^;
179 Result := NSISPTChar(@th.text);
180 g_stacktop^ := th.next;
181 GlobalFree(HGLOBAL(th));
182 end;
183 end;
184
185 procedure PushString(const str: NSISTString='');
186 var
187 th: pstack_t;
188 begin
189 if NativeUInt(g_stacktop) <> 0 then begin
190 {$IFDEF UNICODE}
191 th := pstack_t(GlobalAlloc(GPTR, SizeOf(stack_t) + (g_stringsize * 2)));
192 lstrcpynW(@th.text, PWideChar(str), g_stringsize);
193 {$ELSE}
194 th := pstack_t(GlobalAlloc(GPTR, SizeOf(stack_t) + (g_stringsize )));
195 lstrcpynA(@th.text, PAnsiChar(str), g_stringsize);
196 {$ENDIF}
197 th.next := g_stacktop^;
198 g_stacktop^ := th;
199 end;
200 end;
201
202 function GetUserVariable(const varnum: TVariableList): NSISTString;
203 begin
204 if (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then
205 Result := g_variables + integer(varnum) * g_stringsize
206 else
207 Result := '';
208 end;
209
210 procedure SetUserVariable(const varnum: TVariableList; const value: NSISTString);
211 begin
212 if (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then
213 {$IFDEF UNICODE}
214 lstrcpyW(g_variables + integer(varnum) * (g_stringsize), PWideChar(value))
215 {$ELSE}
216 lstrcpyA(g_variables + integer(varnum) * (g_stringsize), PAnsiChar(value))
217 {$ENDIF}
218 end;
219
220 procedure NSISDialog(const text, caption: NSISTString; const buttons: integer);
221 var
222 hwndOwner: HWND;
223 begin
224 hwndOwner := g_hwndParent;
225 if not IsWindow(g_hwndParent) then hwndOwner := 0; // g_hwndParent is not valid in NSPIM_[GUI]UNLOAD
226 {$IFDEF UNICODE}
227 MessageBoxW(hwndOwner, PWideChar(text), PWideChar(caption), buttons);
228 {$ELSE}
229 MessageBoxA(hwndOwner, PAnsiChar(text), PAnsiChar(caption), buttons);
230 {$ENDIF}
231 end;
232
233 begin
234
235 end.
236