nsis.pas 5.6 KB

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