Advertisement
Guest User

Untitled

a guest
Jun 29th, 2012
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.99 KB | None | 0 0
  1. {
  2.     Original Code from
  3.     (C) 2001 - Peter Windridge
  4.  
  5.     Code in seperate unit and some changes
  6.     2003 by Bernhard Mayer
  7.  
  8.     Fixed and formatted by Brett Dever
  9.     http://editor.nfscheats.com/
  10.  
  11.     Adjustments for Delphi XE complation made
  12.     2012 by Linards Liepins
  13.  
  14.     simply include this unit in your plugin project and export
  15.     functions as needed
  16. }
  17.  
  18. unit nsis;
  19.  
  20. interface
  21.  
  22. uses
  23.   windows, Messages, CommCtrl, SysUtils, {madExcept,} FileCtrl;
  24.  
  25. type
  26.   VarConstants = (
  27.     INST_0,       // $0
  28.     INST_1,       // $1
  29.     INST_2,       // $2
  30.     INST_3,       // $3
  31.     INST_4,       // $4
  32.     INST_5,       // $5
  33.     INST_6,       // $6
  34.     INST_7,       // $7
  35.     INST_8,       // $8
  36.     INST_9,       // $9
  37.     INST_R0,      // $R0
  38.     INST_R1,      // $R1
  39.     INST_R2,      // $R2
  40.     INST_R3,      // $R3
  41.     INST_R4,      // $R4
  42.     INST_R5,      // $R5
  43.     INST_R6,      // $R6
  44.     INST_R7,      // $R7
  45.     INST_R8,      // $R8
  46.     INST_R9,      // $R9
  47.     INST_CMDLINE, // $CMDLINE
  48.     INST_INSTDIR, // $INSTDIR
  49.     INST_OUTDIR,  // $OUTDIR
  50.     INST_EXEDIR,  // $EXEDIR
  51.     INST_LANG,    // $LANGUAGE
  52.     __INST_LAST
  53.     );
  54.   TVariableList = INST_0..__INST_LAST;
  55.  
  56.   TExecuteCodeSegment = function (const funct_id: Integer; const parent: HWND): Integer;  stdcall;
  57.   Tvalidate_filename = procedure (const filename: PChar); cdecl;
  58.   TRegisterPluginCallback = function (const unknow: Integer; const uknown2: Integer): Integer; cdecl;
  59.  
  60.   pexec_flags_t = ^exec_flags_t;
  61.   exec_flags_t = record
  62.     autoclose: Integer;
  63.     all_user_var: Integer;
  64.     exec_error: Integer;
  65.     abort: Integer;
  66.     exec_reboot: Integer;
  67.     reboot_called: Integer;
  68.     XXX_cur_insttype: Integer;
  69.     plugin_api_version: Integer;
  70.     silent: Integer;
  71.     instdir_error: Integer;
  72.     rtl: Integer;
  73.     errlvl: Integer;
  74.     alter_reg_view: Integer;
  75.     status_update: Integer;
  76.   end;
  77.  
  78.   pextrap_t = ^extrap_t;
  79.   extrap_t = record
  80.     exec_flags: Pointer; // exec_flags_t;
  81.     exec_code_segment: Pointer; //  TFarProc;
  82.     validate_filename: Pointer; // Tvalidate_filename;
  83.     RegisterPluginCallback: Pointer; //TRegisterPluginCallback;
  84.   end;
  85.  
  86.   pstack_t = ^stack_t;
  87.   stack_t = record
  88.     next: pstack_t;
  89.     text: PChar;
  90.   end;
  91.  
  92. var
  93.   g_stringsize: integer;
  94.   g_stacktop: ^pstack_t;
  95.   g_variables: PChar;
  96.   g_hwndParent: HWND;
  97.   g_hwndList: HWND;
  98.   g_hwndIntroText: HWND;
  99.   g_hwndLogList: HWND;
  100.  
  101.   g_extraparameters: pextrap_t;
  102.   func : TExecuteCodeSegment;
  103.   extrap : extrap_t;
  104.  
  105.   loghook: HHOOK;
  106.   LogFileName: String;
  107.  
  108. procedure Init(const hwndParent: HWND; const string_size: integer; const variables: PChar; const stacktop: pointer; const extraparameters: pointer = nil);
  109.  
  110. function LogMessage(Msg : String): BOOL;
  111. function LogStatusText(Msg : String): BOOL;
  112. function GetLogText(): String;
  113. function Call(NSIS_func : String) : Integer;
  114. function PopString(): string;
  115. procedure PushString(const str: string='');
  116. function GetUserVariable(const varnum: TVariableList): string;
  117. procedure SetUserVariable(const varnum: TVariableList; const value: string);
  118. procedure NSISDialog(const text, caption: string; const buttons: integer);
  119.  
  120. procedure SetHook;
  121. procedure Unhook;
  122. function LogHookProc(code : Integer; wParam, lParam : LongInt) : LongInt; stdcall;
  123.  
  124. implementation
  125.  
  126. procedure Init(const hwndParent: HWND; const string_size: integer; const variables: PChar; const stacktop: pointer; const extraparameters: pointer = nil);
  127. begin
  128.   g_stringsize := string_size;
  129.   g_hwndParent := hwndParent;
  130.   g_stacktop   := stacktop;
  131.   g_variables  := variables;
  132.   g_hwndList := 0;
  133.   g_hwndList := FindWindowEx(FindWindowEx(g_hwndParent, 0, '#32770', nil), 0,'SysListView32', nil);
  134.   g_hwndIntroText := 0;
  135.   g_hwndIntroText := FindWindowEx(FindWindowEx(g_hwndParent, 0, '#32770', nil), 0,'Static', nil);
  136.   g_extraparameters := extraparameters;
  137.   if extraparameters <> nil then
  138.     extrap := g_extraparameters^;
  139. end;
  140.  
  141. function LogHookProc(code : Integer; wParam, lParam : LongInt) : LongInt; stdcall;
  142. var
  143.   cwp: PCWPStruct;
  144.   item: PLVItem;
  145.   myFile : TextFile;
  146.   HasDir : Boolean;
  147. begin
  148.   cwp := PCWPSTRUCT(lParam);
  149.   if (cwp.message = $1007) then
  150.   begin
  151.     try
  152.       item := PLVItem(cwp.lParam);
  153.       if (item.mask = LVIF_TEXT) and (LogFileName>' ') then
  154.       begin
  155.         HasDir := True;
  156.         if not SysUtils.DirectoryExists(ExtractFileDir(LogFileName)) then
  157.           HasDir := CreateDir(ExtractFileDir(LogFileName));
  158.         if HasDir then
  159.         begin
  160.           AssignFile(myFile, LogFileName);
  161.           If FileExists(LogFileName) then
  162.             Append(myFile)
  163.           else
  164.             ReWrite(myFile);
  165.           WriteLn(myFile, item.pszText);
  166.           CloseFile(myFile);
  167.         end;
  168.       end;
  169.     except
  170.         //HandleException;
  171.     end;
  172.   end;
  173.   CallNextHookEx(loghook,code,wParam,lparam);
  174. end;
  175.  
  176. procedure SetHook;
  177. var
  178.   pid : Pointer;
  179.   threadid : DWORD;
  180. begin
  181.   loghook := SetWindowsHookExA(WH_CALLWNDPROC, @LogHookProc, 0, GetCurrentThreadID())
  182. end;
  183.  
  184. procedure Unhook;
  185. begin
  186.   UnhookWindowsHookEx(loghook);
  187. end;
  188.  
  189. function Call(NSIS_func : String) : Integer;
  190. var
  191.   NSISFun: Integer; //The ID of nsis function
  192. begin
  193.   Result := 0;
  194.   NSISFun := StrToIntDef(NSIS_func, 0);
  195.   if (NSISFun <> 0) and (g_extraparameters <> nil) then
  196.     begin
  197.     @func := extrap.exec_code_segment;
  198.     NSISFun := NSISFun - 1;
  199.     Result := func(NSISFun, g_hwndParent);
  200.     end;
  201. end;
  202.  
  203. function LogStatusText(Msg : String): BOOL;
  204. begin
  205.   SendMessage(g_hwndIntroText, WM_SETTEXT, 0, Integer(PCHAR(Msg)));
  206. end;
  207.  
  208. function LogMessage(Msg : String): BOOL;
  209. var
  210.   ItemCount : Integer;
  211.   item: TLVItem;
  212. begin
  213.   Result := FAlse;
  214.   if g_hwndList = 0 then exit;
  215.   FillChar( item, sizeof(item), 0 );
  216.   ItemCount := SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
  217.   item.iItem := ItemCount;
  218.   item.mask := LVIF_TEXT;
  219.  {$IFDEF VER230}
  220.     item.pszText := PWideChar(Msg);
  221.  //{$ELSE}
  222.  //   item.pszText := PAnsiChar(Msg);
  223.  {$ENDIF}
  224.   ListView_InsertItem(g_hwndList, item );
  225.   ListView_EnsureVisible(g_hwndList, ItemCount, TRUE);
  226. end;
  227.  
  228. function PopString(): string;
  229. var
  230.   th: pstack_t;
  231. begin
  232.   if integer(g_stacktop^) <> 0 then begin
  233.     th := g_stacktop^;
  234.     Result := PChar(@th.text);
  235.     g_stacktop^ := th.next;
  236.     GlobalFree(HGLOBAL(th));
  237.   end;
  238. end;
  239.  
  240. procedure PushString(const str: string='');
  241. var
  242.   th: pstack_t;
  243. begin
  244.   if integer(g_stacktop) <> 0 then begin
  245.     th := pstack_t(GlobalAlloc(GPTR, SizeOf(stack_t) + g_stringsize));
  246.     lstrcpyn(@th.text, PChar(str), g_stringsize);
  247.     th.next := g_stacktop^;
  248.     g_stacktop^ := th;
  249.   end;
  250. end;
  251.  
  252. function GetUserVariable(const varnum: TVariableList): string;
  253. begin
  254.   if (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then
  255.     Result := g_variables + integer(varnum) * g_stringsize
  256.   else
  257.     Result := '';
  258. end;
  259.  
  260. procedure SetUserVariable(const varnum: TVariableList; const value: string);
  261. var
  262.   shortenval : String;
  263. begin
  264.   shortenval := Copy(value, 1, g_stringsize - 1); // We cat string with #0 at end to fit in string buffer
  265. //  if (value <> '') and (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then
  266.   if (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then // Can't imagine why not return zero strings
  267.     lstrcpy(g_variables + integer(varnum) * g_stringsize, PChar(shortenval))
  268. end;
  269.  
  270. procedure NSISDialog(const text, caption: string; const buttons: integer);
  271. begin
  272.   MessageBox(g_hwndParent, PChar(text), PChar(caption), buttons);
  273. end;
  274.  
  275. function GetLogText(): String;
  276. var
  277.   ItemCount, i: Integer;
  278.   item: TLVItem;
  279.   Temp: Array [0..MAX_PATH] of Char;
  280. begin
  281.   Result := '';
  282.   if g_hwndList = 0 then exit;
  283.   FillChar( item, sizeof(item), 0 );
  284.   ItemCount := SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
  285.   item.mask := LVIF_TEXT;
  286.   For i := 0 to ItemCount - 1 do
  287.     begin
  288.     ListView_GetItemText(g_hwndList, i, 0, Temp, MAX_PATH);
  289.     Result := Result + Temp + #13#10;
  290.     end;
  291. end;
  292.  
  293. begin
  294.  
  295. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement