Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- Original Code from
- (C) 2001 - Peter Windridge
- Code in seperate unit and some changes
- 2003 by Bernhard Mayer
- Fixed and formatted by Brett Dever
- http://editor.nfscheats.com/
- Adjustments for Delphi XE complation made
- 2012 by Linards Liepins
- simply include this unit in your plugin project and export
- functions as needed
- }
- unit nsis;
- interface
- uses
- windows, Messages, CommCtrl, SysUtils, {madExcept,} FileCtrl;
- type
- VarConstants = (
- INST_0, // $0
- INST_1, // $1
- INST_2, // $2
- INST_3, // $3
- INST_4, // $4
- INST_5, // $5
- INST_6, // $6
- INST_7, // $7
- INST_8, // $8
- INST_9, // $9
- INST_R0, // $R0
- INST_R1, // $R1
- INST_R2, // $R2
- INST_R3, // $R3
- INST_R4, // $R4
- INST_R5, // $R5
- INST_R6, // $R6
- INST_R7, // $R7
- INST_R8, // $R8
- INST_R9, // $R9
- INST_CMDLINE, // $CMDLINE
- INST_INSTDIR, // $INSTDIR
- INST_OUTDIR, // $OUTDIR
- INST_EXEDIR, // $EXEDIR
- INST_LANG, // $LANGUAGE
- __INST_LAST
- );
- TVariableList = INST_0..__INST_LAST;
- TExecuteCodeSegment = function (const funct_id: Integer; const parent: HWND): Integer; stdcall;
- Tvalidate_filename = procedure (const filename: PChar); cdecl;
- TRegisterPluginCallback = function (const unknow: Integer; const uknown2: Integer): Integer; cdecl;
- pexec_flags_t = ^exec_flags_t;
- exec_flags_t = record
- autoclose: Integer;
- all_user_var: Integer;
- exec_error: Integer;
- abort: Integer;
- exec_reboot: Integer;
- reboot_called: Integer;
- XXX_cur_insttype: Integer;
- plugin_api_version: Integer;
- silent: Integer;
- instdir_error: Integer;
- rtl: Integer;
- errlvl: Integer;
- alter_reg_view: Integer;
- status_update: Integer;
- end;
- pextrap_t = ^extrap_t;
- extrap_t = record
- exec_flags: Pointer; // exec_flags_t;
- exec_code_segment: Pointer; // TFarProc;
- validate_filename: Pointer; // Tvalidate_filename;
- RegisterPluginCallback: Pointer; //TRegisterPluginCallback;
- end;
- pstack_t = ^stack_t;
- stack_t = record
- next: pstack_t;
- text: PChar;
- end;
- var
- g_stringsize: integer;
- g_stacktop: ^pstack_t;
- g_variables: PChar;
- g_hwndParent: HWND;
- g_hwndList: HWND;
- g_hwndIntroText: HWND;
- g_hwndLogList: HWND;
- g_extraparameters: pextrap_t;
- func : TExecuteCodeSegment;
- extrap : extrap_t;
- loghook: HHOOK;
- LogFileName: String;
- procedure Init(const hwndParent: HWND; const string_size: integer; const variables: PChar; const stacktop: pointer; const extraparameters: pointer = nil);
- function LogMessage(Msg : String): BOOL;
- function LogStatusText(Msg : String): BOOL;
- function GetLogText(): String;
- function Call(NSIS_func : String) : Integer;
- function PopString(): string;
- procedure PushString(const str: string='');
- function GetUserVariable(const varnum: TVariableList): string;
- procedure SetUserVariable(const varnum: TVariableList; const value: string);
- procedure NSISDialog(const text, caption: string; const buttons: integer);
- procedure SetHook;
- procedure Unhook;
- function LogHookProc(code : Integer; wParam, lParam : LongInt) : LongInt; stdcall;
- implementation
- procedure Init(const hwndParent: HWND; const string_size: integer; const variables: PChar; const stacktop: pointer; const extraparameters: pointer = nil);
- begin
- g_stringsize := string_size;
- g_hwndParent := hwndParent;
- g_stacktop := stacktop;
- g_variables := variables;
- g_hwndList := 0;
- g_hwndList := FindWindowEx(FindWindowEx(g_hwndParent, 0, '#32770', nil), 0,'SysListView32', nil);
- g_hwndIntroText := 0;
- g_hwndIntroText := FindWindowEx(FindWindowEx(g_hwndParent, 0, '#32770', nil), 0,'Static', nil);
- g_extraparameters := extraparameters;
- if extraparameters <> nil then
- extrap := g_extraparameters^;
- end;
- function LogHookProc(code : Integer; wParam, lParam : LongInt) : LongInt; stdcall;
- var
- cwp: PCWPStruct;
- item: PLVItem;
- myFile : TextFile;
- HasDir : Boolean;
- begin
- cwp := PCWPSTRUCT(lParam);
- if (cwp.message = $1007) then
- begin
- try
- item := PLVItem(cwp.lParam);
- if (item.mask = LVIF_TEXT) and (LogFileName>' ') then
- begin
- HasDir := True;
- if not SysUtils.DirectoryExists(ExtractFileDir(LogFileName)) then
- HasDir := CreateDir(ExtractFileDir(LogFileName));
- if HasDir then
- begin
- AssignFile(myFile, LogFileName);
- If FileExists(LogFileName) then
- Append(myFile)
- else
- ReWrite(myFile);
- WriteLn(myFile, item.pszText);
- CloseFile(myFile);
- end;
- end;
- except
- //HandleException;
- end;
- end;
- CallNextHookEx(loghook,code,wParam,lparam);
- end;
- procedure SetHook;
- var
- pid : Pointer;
- threadid : DWORD;
- begin
- loghook := SetWindowsHookExA(WH_CALLWNDPROC, @LogHookProc, 0, GetCurrentThreadID())
- end;
- procedure Unhook;
- begin
- UnhookWindowsHookEx(loghook);
- end;
- function Call(NSIS_func : String) : Integer;
- var
- NSISFun: Integer; //The ID of nsis function
- begin
- Result := 0;
- NSISFun := StrToIntDef(NSIS_func, 0);
- if (NSISFun <> 0) and (g_extraparameters <> nil) then
- begin
- @func := extrap.exec_code_segment;
- NSISFun := NSISFun - 1;
- Result := func(NSISFun, g_hwndParent);
- end;
- end;
- function LogStatusText(Msg : String): BOOL;
- begin
- SendMessage(g_hwndIntroText, WM_SETTEXT, 0, Integer(PCHAR(Msg)));
- end;
- function LogMessage(Msg : String): BOOL;
- var
- ItemCount : Integer;
- item: TLVItem;
- begin
- Result := FAlse;
- if g_hwndList = 0 then exit;
- FillChar( item, sizeof(item), 0 );
- ItemCount := SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
- item.iItem := ItemCount;
- item.mask := LVIF_TEXT;
- {$IFDEF VER230}
- item.pszText := PWideChar(Msg);
- //{$ELSE}
- // item.pszText := PAnsiChar(Msg);
- {$ENDIF}
- ListView_InsertItem(g_hwndList, item );
- ListView_EnsureVisible(g_hwndList, ItemCount, TRUE);
- end;
- function PopString(): string;
- var
- th: pstack_t;
- begin
- if integer(g_stacktop^) <> 0 then begin
- th := g_stacktop^;
- Result := PChar(@th.text);
- g_stacktop^ := th.next;
- GlobalFree(HGLOBAL(th));
- end;
- end;
- procedure PushString(const str: string='');
- var
- th: pstack_t;
- begin
- if integer(g_stacktop) <> 0 then begin
- th := pstack_t(GlobalAlloc(GPTR, SizeOf(stack_t) + g_stringsize));
- lstrcpyn(@th.text, PChar(str), g_stringsize);
- th.next := g_stacktop^;
- g_stacktop^ := th;
- end;
- end;
- function GetUserVariable(const varnum: TVariableList): string;
- begin
- if (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then
- Result := g_variables + integer(varnum) * g_stringsize
- else
- Result := '';
- end;
- procedure SetUserVariable(const varnum: TVariableList; const value: string);
- var
- shortenval : String;
- begin
- shortenval := Copy(value, 1, g_stringsize - 1); // We cat string with #0 at end to fit in string buffer
- // if (value <> '') and (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then
- if (integer(varnum) >= 0) and (integer(varnum) < integer(__INST_LAST)) then // Can't imagine why not return zero strings
- lstrcpy(g_variables + integer(varnum) * g_stringsize, PChar(shortenval))
- end;
- procedure NSISDialog(const text, caption: string; const buttons: integer);
- begin
- MessageBox(g_hwndParent, PChar(text), PChar(caption), buttons);
- end;
- function GetLogText(): String;
- var
- ItemCount, i: Integer;
- item: TLVItem;
- Temp: Array [0..MAX_PATH] of Char;
- begin
- Result := '';
- if g_hwndList = 0 then exit;
- FillChar( item, sizeof(item), 0 );
- ItemCount := SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0);
- item.mask := LVIF_TEXT;
- For i := 0 to ItemCount - 1 do
- begin
- ListView_GetItemText(g_hwndList, i, 0, Temp, MAX_PATH);
- Result := Result + Temp + #13#10;
- end;
- end;
- begin
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement