

[Code]
procedure LoadSkin(lpszPath: String; lpszIniFileName: String);
external 'LoadSkin@files:ISSkin.dll stdcall';

procedure UnloadSkin();
external 'UnloadSkin@files:ISSkin.dll stdcall';

function ShowWindow(hWnd: Integer; uType: Integer): Integer;
external 'ShowWindow@user32.dll stdcall';

function InitializeSetup(): Boolean;
var
  InstalledClient, InstalledUpdate, MaxDateClient, MinDateClient, MaxDateUpdate, MinDateUpdate: String;
begin
  //  InstalledClient  
  if RegQueryStringValue(HKLM32, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{'+'{#RegKeyID}'+'}_is1', 'InstalledClient', InstalledClient) then
  begin //  
    //   
    InstalledClient:=InstalledClient[5]+InstalledClient[6]+InstalledClient[3]+InstalledClient[4]+InstalledClient[1]+InstalledClient[2];
    MaxDateClient:='{#MaxDateClient}';
    MaxDateClient:=MaxDateClient[5]+MaxDateClient[6]+MaxDateClient[3]+MaxDateClient[4]+MaxDateClient[1]+MaxDateClient[2]; 
    MinDateClient:='{#MinDateClient}';
    MinDateClient:=MinDateClient[5]+MinDateClient[6]+MinDateClient[3]+MinDateClient[4]+MinDateClient[1]+MinDateClient[2];
    // 
    if (StrToInt(InstalledClient)>=StrToInt(MinDateClient)) and (StrToInt(InstalledClient)<=StrToInt(MaxDateClient)) then
      Result := True //     [MinDateClient; MaxDateClient]
    else
      begin  //      [MinDateClient; MaxDateClient]
        MsgBox('  .', mbError, MB_OK);
        Result := False;
      end;
  end else //   
      begin
        MsgBox('  .', mbError, MB_OK);
        Result := False;
      end;
  // InstalledClient    
  if Result=True then
    begin
      //    
      MaxDateUpdate:='{#MaxDateUpdate}';
      MaxDateUpdate:=MaxDateClient[5]+MaxDateUpdate[6]+MaxDateUpdate[3]+MaxDateUpdate[4]+MaxDateUpdate[1]+MaxDateUpdate[2]; 
      MinDateUpdate:='{#MinDateUpdate}';
      MinDateUpdate:=MinDateUpdate[5]+MinDateUpdate[6]+MinDateUpdate[3]+MinDateUpdate[4]+MinDateUpdate[1]+MinDateUpdate[2];    
      //  InstalledUpdate  
      if RegQueryStringValue(HKLM32, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{'+'{#RegKeyID}'+'}_is1', 'InstalledUpdate', InstalledUpdate) then
       begin //  
        //    
        InstalledUpdate:=InstalledUpdate[5]+InstalledUpdate[6]+InstalledUpdate[3]+InstalledUpdate[4]+InstalledUpdate[1]+InstalledUpdate[2];
        // 
        if StrToInt(InstalledUpdate)>=StrToInt(MinDateUpdate) then
          if StrToInt(InstalledUpdate)<=StrToInt(MaxDateUpdate) then
            Result := True //     [MinDateUpdate; MaxDateUpdate]
          else
            begin //      [MinDateUpdate; MaxDateUpdate]
              MsgBox('      .', mbError, MB_OK);  
              Result := False;
            end
        else
          begin //      [MinDateUpdate; MaxDateUpdate]
            MsgBox('       .', mbError, MB_OK);  
            Result := False;
          end;
       end
      else  //   
        // InstalledUpdate  InstalledClient
        if StrToInt(InstalledClient)>=StrToInt(MinDateUpdate) then
          if StrToInt(InstalledClient)<=StrToInt(MaxDateUpdate) then
            Result := True //     [MinDateUpdate; MaxDateUpdate]
          else
            begin //      [MinDateUpdate; MaxDateUpdate]
              MsgBox('      .', mbError, MB_OK);  
              Result := False;
            end
        else
          begin //      [MinDateUpdate; MaxDateUpdate]
            MsgBox('       .', mbError, MB_OK);  
            Result := False;
          end;
    end;
    if Result=True then
    begin  
      ExtractTemporaryFile('Steam.cjstyles');
      LoadSkin(ExpandConstant('{tmp}\Steam.cjstyles'), '');
    end;
end;

procedure DeinitializeSetup();
begin
	ShowWindow(StrToInt(ExpandConstant('{wizardhwnd}')), 0);
	UnloadSkin();
end;

procedure URLLabelOnClick(Sender: TObject);
var
	ErrorCode: Integer;
begin
	ShellExec('open', '{#URL}', '', '', SW_SHOWNORMAL, ewNoWait, ErrorCode);
end;

procedure InitializeWizard();
var
	MD5: String;
	URLLabel: TNewStaticText;
begin
	URLLabel := TNewStaticText.Create(WizardForm);
	URLLabel.Caption := 'Powered by CSmania.RU';
	URLLabel.Parent := WizardForm;
	URLLabel.Cursor := crHand;
	URLLabel.OnClick := @URLLabelOnClick;
	URLLabel.Font.Color := $ffffff;
	URLLabel.Font.Style := [fsUnderline];
	URLLabel.Top := ScaleY(330);
	URLLabel.Left := ScaleX(23);

	MD5 := GetMD5OfString('CSmania.RU - Update patch');
	WizardForm.PasswordEdit.Visible := False;
	WizardForm.PasswordEdit.Text := MD5;
end;

procedure Xdelta3Patch(folder: String);
var
	Name, Format: String;
	Error, i, j: Integer;
	FindRec: TFindRec;
begin
	if FindFirst(ExpandConstant('{tmp}\xd3\')+Folder+'*.*', FindRec) then
begin try repeat
	if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0) then
begin
	Format :=
	FindRec.Name[Length(FindRec.Name)-3]+FindRec.Name[Length(FindRec.Name)-2]+FindRec.Name[Length(FindRec.Name)-1]+FindRec.Name[Length(FindRec.Name)];
          Name := '';
	for i := 1 to (Length(FindRec.Name) - 8) do
	Name := Name+FindRec.Name[i];
	RenameFile(ExpandConstant('{app}\')+Folder + Name +Format, ExpandConstant('{app}\')+Folder + Name  +'.oldfile');
	Exec(ExpandConstant('{tmp}\') + 'xdelta3.exe','-n -d -s '+ Chr(34)+ExpandConstant('{app}\')+Folder+ Name+'.oldfile'+Chr(34)+' '+Chr(34)+ExpandConstant('{tmp}\xd3\')+Folder+Name+Format+'.xd3'+Chr(34)+' '+ Chr(34)+ ExpandConstant('{app}\')+Folder + Name +Format+Chr(34),'',0,ewWaitUntilTerminated,Error);
	DeleteFile(ExpandConstant('{app}\')+Folder + Name +'.oldfile');
end else
	if (FindRec.Name <> '..') and (FindRec.Name <> '.') then
	Xdelta3Patch(Folder + FindRec.Name + '\');
until not FindNext(FindRec);
finally
	FindClose(FindRec);
end;
end;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
	if CurPageID = wpPassword then
begin
	WizardForm.NextButton.OnClick(WizardForm.NextButton);
end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
	if CurStep = ssInstall then
begin
	DelTree(ExpandConstant('{tmp}\xd3'), True, True, True);
	DeleteFile(ExpandConstant('{tmp}\xdelta3.exe'));
end;
	if CurStep = ssPostInstall then
begin
	Xdelta3Patch('');
	MsgBox('Hello.', mbInformation, MB_OK);
	RegDeleteKeyIncludingSubkeys(HKCU, 'Software\L2j Community Network\LameUpdater');
	RegWriteStringValue(HKLM32, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{'+'{#RegKeyID}'+'}_is1', 'InstalledUpdate', ExpandConstant('{#UpdateDate}'));
end;
	if CurStep = ssDone then
begin
	DelTree(ExpandConstant('{tmp}\xdelta3'), True, True, True);
	DeleteFile(ExpandConstant('{tmp}\xdelta3.exe'));
end;
end;