You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DMDirc.dpr 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. {*
  2. * This application launches DMDirc on windows and passes control to the
  3. * update engine as necessary.
  4. *
  5. * DMDirc - Open Source IRC Client
  6. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes,
  7. * Michael Nixon
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *}
  27. program DMDirc;
  28. {$MODE Delphi}
  29. {$APPTYPE GUI}
  30. {$R comctl.rc}
  31. {$R icon.rc}
  32. {$R version.rc}
  33. uses Vista, shared, Windows, SysUtils, classes, StrUtils;
  34. procedure InitCommonControls; stdcall; External 'comctl32.dll' name 'InitCommonControls';
  35. { ---------------------------------------------------------------------------- }
  36. { ----------------------------------------------------------------------------
  37. MAIN PROGRAM
  38. ---------------------------------------------------------------------------- }
  39. const
  40. launcherVersion: String = '3';
  41. var
  42. errorMessage: String;
  43. javaCommand: String = 'javaw.exe';
  44. cliParams: String = '';
  45. directory: String = '';
  46. i: integer;
  47. jarName: String;
  48. launcherUpdate: boolean = false;
  49. result: integer;
  50. s: string;
  51. handle: thandle;
  52. begin
  53. InitCommonControls;
  54. { Check for a mutex created by the DMDirc update process. Wait until it no
  55. longer exists before we continue. This prevents us from proceeding if any
  56. previous instance of the launcher is shutting down. }
  57. handle := OpenMutex(SYNCHRONIZE, False, 'DMDirc_Launcher_Restart_Wait');
  58. if handle <> 0 then begin
  59. { The mutex exists. Wait for it for up to 5 seconds. }
  60. if WaitForSingleObject(handle, 5000) = WAIT_TIMEOUT then begin
  61. { Timed out - we cannot continue, there is some kind of serious issue? }
  62. showError('Internal error: Timed out waiting for previous instance of launcher to stop during restart upgrade', 'DMDirc', true, true);
  63. exit;
  64. end;
  65. { If we get to here, the mutex has been released and we can continue }
  66. CloseHandle(handle);
  67. end;
  68. jarName := ExtractFileDir(paramstr(0))+'\DMDirc.jar';
  69. directory := GetEnvironmentVariable('APPDATA')+'\DMDirc';
  70. if ParamCount > 0 then begin
  71. for i := 1 to ParamCount do begin
  72. if AnsiContainsStr(cliParams, ' ') then cliParams := cliParams+' "'+paramstr(i)+'"'
  73. else cliParams := cliParams+' '+paramstr(i);
  74. if (paramstr(i) = '-d') or (paramstr(i) = '--directory') then begin
  75. if ParamCount > i then begin
  76. directory := paramstr(i+1);
  77. end;
  78. end
  79. end;
  80. end;
  81. // Update if needed.
  82. launcherUpdate := FileExists(directory+'\.DMDirc.exe') and FileExists(directory+'\.DMDircUpdater.exe');
  83. if FileExists(directory+'\.DMDirc.jar') or launcherUpdate then begin
  84. // Vista Sucks.
  85. if IsWindowsVista then begin
  86. // Vista >.<
  87. // Try and delete the old file, if it fails then the user needs to give
  88. // us permission to delete the file (UAC), otherwise we can just go ahead
  89. // and run the updater.
  90. if not DeleteFile(pchar(jarName)) then begin
  91. errorMessage := 'An update to DMDirc has been previously downloaded. ';
  92. errorMessage := errorMessage+#13#10;
  93. errorMessage := errorMessage+#13#10+'As you are running Windows Vista, DMDirc requires administrator access to ';
  94. errorMessage := errorMessage+#13#10+'complete the update. ';
  95. errorMessage := errorMessage+#13#10;
  96. errorMessage := errorMessage+#13#10+'Please click ''Allow'' on the UAC prompt to complete the update, or click no ';
  97. errorMessage := errorMessage+#13#10+'here to continue without updating. ';
  98. if askQuestion(errorMessage, 'DMDirc') then begin
  99. RunProgram('"'+ExtractFileDir(paramstr(0))+'\DMDircUpdater.exe" --UpdateSourceDir "'+directory+'"', not launcherUpdate);
  100. end;
  101. end
  102. else RunProgram('"'+ExtractFileDir(paramstr(0))+'\DMDircUpdater.exe" --UpdateSourceDir "'+directory+'"', not launcherUpdate);
  103. end
  104. else RunProgram('"'+ExtractFileDir(paramstr(0))+'\DMDircUpdater.exe" --UpdateSourceDir "'+directory+'"', not launcherUpdate);
  105. end;
  106. if not launcherUpdate then begin
  107. // Check JVM
  108. if (ExecAndWait(javaCommand+' -version') <> 0) then begin
  109. errorMessage := 'No JVM is currently installed.';
  110. errorMessage := errorMessage+#13#10;
  111. errorMessage := errorMessage+#13#10+'DMDirc requires a 1.6.0 compatible JVM, you can get one from:';
  112. errorMessage := errorMessage+#13#10+'http://java.com/';
  113. showError(errorMessage, 'DMDirc', true);
  114. end
  115. // Else try and run client. (This only asks for help output to check that client
  116. // runs on this OS, otherwise later segfaults or so would cause the error to
  117. // appear
  118. else if FileExists(jarName) then begin
  119. if (ExecAndWait(javaCommand+' -jar "'+jarName+'" --help') <> 0) then begin
  120. errorMessage := 'The currently installed version of java is not compatible with DMDirc.';
  121. errorMessage := errorMessage+#13#10;
  122. errorMessage := errorMessage+#13#10+'DMDirc requires a 1.6.0 compatible JVM, you can get one from:';
  123. errorMessage := errorMessage+#13#10+'http://java.com/';
  124. showError(errorMessage, 'DMDirc', True);
  125. end
  126. else begin
  127. //Launch(javaCommand+' -ea -jar "'+jarName+'"'+' -l windows-'+launcherVersion+' '+cliParams)
  128. { Need to wait so we can deal with exit code 42 to restart }
  129. result := ExecAndWait(javaCommand+' -ea -jar "'+jarName+'"'+' -l windows-'+launcherVersion+' '+cliParams);
  130. if result = 42 then begin
  131. { Need to restart DMDirc launcher
  132. We can't just rerun the EXE because it is possible for the new
  133. process to launch the updater and try to replace this EXE before
  134. the current thread ends - remote but possible. We deal with this
  135. case by creating a mutex that this program will spin on when it
  136. restarts until this instance ends. }
  137. handle := CreateMutex(nil, True, 'DMDirc_Launcher_Restart_Wait');
  138. if handle = 0 then begin
  139. showError('Internal error: Failed to create restart mutex', 'DMDirc', true, true);
  140. exit;
  141. end;
  142. { Build new command line }
  143. s := '';
  144. for i := 1 to paramcount do begin
  145. if pos(' ', paramstr(i)) <> 0 then s := s + '"' + paramstr(i) + '"' else
  146. s := s + paramstr(i);
  147. if i < paramcount then s := s + ' ';
  148. end;
  149. { Launch self }
  150. Launch('"' + paramstr(0)+ '" ' + s);
  151. end;
  152. end;
  153. end
  154. else begin
  155. errorMessage := 'A file required to start DMDirc is missing. Please re-install DMDirc to rectify the problem.';
  156. errorMessage := errorMessage+#13#10;
  157. errorMessage := errorMessage+#13#10 + 'File: DMDirc.jar';
  158. showError(errorMessage, 'DMDirc');
  159. end;
  160. end;
  161. end.