Skip to content

armgit / WSM

Public Imported from GitHub
0

Run macOS applications on your Windows desktop, with a background runtime and integrated app windows.

C++
7.2 KB
Raw Blame
; Build with ISCC /DStageDir="..." /DMyAppVersion="0.2.4" WSM-Setup.iss
#ifndef MyAppVersion
  #define MyAppVersion "0.2.4"
#endif
#ifndef AppIdValue
  #define AppIdValue "{{FC777B63-3C0A-4B29-9E05-817D50AA8BA9}"
#endif
#ifndef StageDir
  #define StageDir SourcePath + "..\dist\WSM-" + MyAppVersion + "-win64"
#endif
#ifndef OutputName
  #define OutputName "WSM-Setup-" + MyAppVersion
#endif

[Setup]
AppId={#AppIdValue}
AppName=WSM
AppVersion={#MyAppVersion}
AppPublisher=mxhm11cr
DefaultDirName={localappdata}\Programs\WSM
DefaultGroupName=WSM
UsePreviousAppDir=yes
DisableProgramGroupPage=yes
DisableReadyPage=no
PrivilegesRequired=lowest
ArchitecturesAllowed=x64compatible
ArchitecturesInstallIn64BitMode=x64compatible
OutputDir={#SourcePath}..\dist
OutputBaseFilename={#OutputName}
SetupIconFile={#SourcePath}..\assets\brand\wsm.ico
UninstallDisplayIcon={app}\bin\WsmManager.exe
Compression=lzma2/fast
SolidCompression=yes
WizardStyle=modern
; Do not let Restart Manager shut down a VM or restart an old daemon mid-upgrade.
CloseApplications=no
RestartApplications=no

[Files]
Source: "{#StageDir}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

#ifndef UpgradeSmokeTest
[Tasks]
Name: "desktopicon"; Description: "Create a desktop shortcut"; GroupDescription: "Additional shortcuts:"; Flags: unchecked
[Icons]
Name: "{autoprograms}\WSM"; Filename: "{app}\bin\WsmManager.exe"; WorkingDir: "{app}\bin"
Name: "{autodesktop}\WSM"; Filename: "{app}\bin\WsmManager.exe"; WorkingDir: "{app}\bin"; Tasks: desktopicon
[Run]
Filename: "{app}\bin\WsmManager.exe"; Description: "Launch WSM"; Flags: nowait postinstall skipifsilent unchecked
#endif

[Code]
var
  ConfirmedDirectory: String;
  RegisteredDirectory: String;

function NormalPath(Path: String): String;
begin
  Result := Lowercase(RemoveBackslashUnlessRoot(ExpandFileName(Path)));
end;

function ExistingInstallation: Boolean;
begin
  Result := FileExists(ExpandConstant('{app}\bin\wsmd.exe')) or
    FileExists(ExpandConstant('{app}\bin\WsmManager.exe')) or
    ((RegisteredDirectory <> '') and
     (NormalPath(RegisteredDirectory) = NormalPath(ExpandConstant('{app}'))));
end;

procedure InitializeWizard;
var
  Key, Id: String;
begin
  Id := '{#AppIdValue}';
  StringChangeEx(Id, '{{', '{', True);
  Key := 'Software\Microsoft\Windows\CurrentVersion\Uninstall\' + Id + '_is1';
  if not RegQueryStringValue(HKCU, Key, 'Inno Setup: App Path', RegisteredDirectory) then
    RegQueryStringValue(HKLM, Key, 'Inno Setup: App Path', RegisteredDirectory);
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := True;
  if CurPageID <> wpReady then exit;
  if (RegisteredDirectory <> '') and
     (NormalPath(RegisteredDirectory) <> NormalPath(ExpandConstant('{app}'))) then begin
    SuppressibleMsgBox('WSM is already installed at ' + RegisteredDirectory + '.' + #13#10 +
      'Update that installation to keep using its macOS runtime and avoid running conflicting copies.',
      mbError, MB_OK, IDOK);
    Result := False;
    exit;
  end;
  if ExistingInstallation and (ConfirmedDirectory <> NormalPath(ExpandConstant('{app}'))) then begin
    if WizardSilent then
      Result := ExpandConstant('{param:UPGRADE|0}') = '1'
    else
      Result := MsgBox('An existing WSM installation was found.' + #13#10#13#10 +
        'Update or repair it by replacing its program files?' + #13#10#13#10 +
        'Your macOS runtime, installed Mac apps, passwords and settings will be kept.' + #13#10 +
        'WSM Manager and its background service will close during the update. Stop the macOS runtime in WSM Settings first.',
        mbConfirmation, MB_YESNO or MB_DEFBUTTON1) = IDYES;
    if Result then ConfirmedDirectory := NormalPath(ExpandConstant('{app}'));
  end;
end;

function FileInUse(const FileName: String): Boolean;
var
  Stream: TFileStream;
begin
  Result := False;
  if not FileExists(FileName) then exit;
  try
    Stream := TFileStream.Create(FileName, fmOpenReadWrite or fmShareDenyNone);
    Stream.Free;
  except
    Result := True;
  end;
end;

function CloseInstalledPrograms: Boolean;
var
  Locator, Services, Processes, Process: Variant;
  I, Code: Integer;
  FileName, TargetBin: String;
begin
  Result := False;
  TargetBin := NormalPath(ExpandConstant('{app}\bin')) + '\';
  try
    Locator := CreateOleObject('WbemScripting.SWbemLocator');
    Services := Locator.ConnectServer('', 'root\CIMV2');
    Processes := Services.ExecQuery('SELECT ExecutablePath, ProcessId FROM Win32_Process WHERE ' +
      'Name="wsmd.exe" OR Name="WsmManager.exe" OR Name="wsm.exe" OR Name="wsmlaunch.exe"');
    for I := 0 to Processes.Count - 1 do begin
      Process := Processes.ItemIndex(I);
      if not VarIsNull(Process.ExecutablePath) then begin
        FileName := NormalPath(Process.ExecutablePath);
        if (FileName = TargetBin + 'wsmd.exe') or
           (FileName = TargetBin + 'wsmmanager.exe') or
           (FileName = TargetBin + 'wsm.exe') or
           (FileName = TargetBin + 'wsmlaunch.exe') then begin
          Log('Closing this installation''s WSM program: ' + FileName);
          Code := Process.Terminate(0);
          if Code <> 0 then exit;
        end;
      end;
    end;
    Result := True;
  except
    Log('Could not close WSM programs automatically: ' + GetExceptionMessage);
  end;
end;

function PrepareToInstall(var NeedsRestart: Boolean): String;
var
  I: Integer;
begin
  Result := '';
  if not ExistingInstallation then exit;
  if ConfirmedDirectory <> NormalPath(ExpandConstant('{app}')) then begin
    Result := 'The update was not confirmed. Existing files have not been replaced.';
    exit;
  end;
  if FileInUse(ExpandConstant('{app}\runtime\reims-ctl\qemu\current\bin\qemu-system-x86_64.exe')) or
     FileInUse(ExpandConstant('{app}\runtime\reims-ctl\qemu\current\bin\qemu-system-x86_64w.exe')) or
     FileInUse(ExpandConstant('{app}\runtime\reims-ctl\qemu\current\bin\qemu-img.exe')) or
     FileInUse(ExpandConstant('{app}\cache\cargo\bin\reims-ctl.exe')) or
     FileInUse(ExpandConstant('{app}\cache\cargo\bin\reims-util.exe')) then begin
    Result := 'The macOS runtime or its installation is still running. Save your Mac documents, stop macOS in WSM Settings and close setup, then try again. Nothing has been replaced.';
    exit;
  end;
  if not CloseInstalledPrograms then begin
    Result := 'Close WSM Manager and its background service, then try again. Nothing has been replaced.';
    exit;
  end;
  for I := 0 to 19 do begin
    if not FileInUse(ExpandConstant('{app}\bin\wsmd.exe')) and
       not FileInUse(ExpandConstant('{app}\bin\WsmManager.exe')) and
       not FileInUse(ExpandConstant('{app}\bin\wsm.exe')) and
       not FileInUse(ExpandConstant('{app}\bin\wsmlaunch.exe')) then exit;
    Sleep(100);
  end;
  Result := 'WSM is still closing. Please try again in a moment. Nothing has been replaced.';
end;

function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo,
  MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
begin
  Result := MemoDirInfo;
  if ExistingInstallation then
    Result := 'Update/repair existing WSM program files.' + NewLine +
      'Keep the macOS runtime, Mac apps and settings.' + NewLine + NewLine + Result;
  Result := Result + NewLine + MemoGroupInfo + NewLine + MemoTasksInfo;
end;