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.

DMDircUpdater.dpr 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. program DMDircUpdater;
  2. {$MODE Delphi}
  3. {$APPTYPE GUI}
  4. {$R UAC.rc}
  5. uses Windows, SysUtils, classes, StrUtils, Vista;
  6. function askQuestion(Question: String): boolean;
  7. begin
  8. Result := TaskDialog(0, 'DMDirc Updater', 'Question', Question, TD_ICON_QUESTION, TD_BUTTON_YES + TD_BUTTON_NO) = mrYes;
  9. end;
  10. procedure showError(ErrorMessage: String; addFooter: boolean = true);
  11. begin
  12. if addFooter then begin
  13. ErrorMessage := ErrorMessage+#13#10;
  14. ErrorMessage := ErrorMessage+#13#10+'If you feel this is incorrect, or you require some further assistance,';
  15. ErrorMessage := ErrorMessage+#13#10+'please feel free to contact us.';
  16. end;
  17. TaskDialog(0, 'DMDirc Updater', 'There was an error starting DMDirc', ErrorMessage, TD_ICON_ERROR, TD_BUTTON_OK, false, false);
  18. end;
  19. procedure showmessage(message: String; context:String = 'Information');
  20. begin
  21. TaskDialog(0, 'DMDirc Updater', context, message, TD_ICON_INFORMATION, TD_BUTTON_OK);
  22. end;
  23. // Run an application and don't wait for it to finish.
  24. procedure Launch(sProgramToRun: String);
  25. var
  26. StartupInfo: TStartupInfo;
  27. ProcessInfo: TProcessInformation;
  28. begin
  29. FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  30. with StartupInfo do begin
  31. cb := SizeOf(TStartupInfo);
  32. dwFlags := STARTF_USESHOWWINDOW;
  33. wShowWindow := SW_SHOWNORMAL;
  34. end;
  35. // MessageBox(0, pchar('Foo: '+sProgramToRun), 'Update Failed', MB_ICONSTOP);
  36. CreateProcess(nil, PChar(sProgramToRun), nil, nil, False, NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
  37. end;
  38. var
  39. sourceDir: String = '';
  40. thisDir: String;
  41. cliParams: String = '';
  42. i: integer;
  43. jarName: String;
  44. launcherUpdate: boolean = false;
  45. myName: String;
  46. canDoMore: boolean = true;
  47. begin
  48. myName := ExtractFileName(paramstr(0));
  49. thisDir := ExtractFileDir(paramstr(0));
  50. jarName := thisDir+'\DMDirc.jar';
  51. if ParamCount > 0 then begin
  52. for i := 1 to ParamCount do begin
  53. if AnsiContainsStr(cliParams, ' ') then cliParams := cliParams+' "'+paramstr(i)+'"'
  54. else cliParams := cliParams+' '+paramstr(i);
  55. if (paramstr(i) = '--UpdateSourceDir') then begin // Source Directory
  56. if ParamCount > i then begin
  57. sourceDir := paramstr(i+1);
  58. end;
  59. end
  60. end;
  61. // Look for a launcher update.
  62. if FileExists(pchar(sourceDir+'\.DMDirc.exe')) and FileExists(pchar(sourceDir+'\.DMDircUpdater.exe')) then begin
  63. if myName = 'DMDircUpdater.exe' then begin
  64. // Windows won't let us overwrite ourself, so we need to copy ourself
  65. // to another name, and run the new us.
  66. if CopyFile(pchar(thisDir+'\DMDircUpdater.exe'), pchar(thisDir+'\DMDircLauncherUpdater.exe'), False) then begin
  67. canDoMore := false;
  68. Launch('"'+thisDir+'\DMDircLauncherUpdater.exe" '+cliParams);
  69. end
  70. else begin
  71. showmessage('Unable to overwrite launcher', 'Update Failed');
  72. end;
  73. end
  74. else begin
  75. launcherUpdate := true;
  76. if FileExists(pchar(thisDir+'\DMDirc.exe')) then begin
  77. if not DeleteFile(pchar(thisDir+'\DMDirc.exe')) then begin
  78. showmessage('Unable to delete DMDirc.exe', 'Launcher Update Failed');
  79. end;
  80. end;
  81. if not FileExists(pchar(thisDir+'\DMDirc.exe')) and MoveFile(pchar(sourceDir+'\.DMDirc.exe'), pchar(thisDir+'\DMDirc.exe')) then begin
  82. if FileExists(pchar(thisDir+'\DMDircUpdater.exe')) then begin
  83. if not DeleteFile(pchar(thisDir+'\DMDircUpdater.exe')) then begin
  84. showmessage('Unable to delete DMDircUpdater.exe', 'Launcher Update Failed');
  85. end;
  86. end;
  87. if not FileExists(pchar(thisDir+'\DMDircUpdater.exe')) and MoveFile(pchar(sourceDir+'\.DMDircUpdater.exe'), pchar(thisDir+'\DMDircUpdater.exe')) then begin
  88. showmessage('Launcher update was successful');
  89. end
  90. else begin
  91. showmessage('Unable to update DMDircUpdater.exe', 'Launcher Update Failed');
  92. end;
  93. end
  94. else begin
  95. showmessage('Unable to update DMDirc.exe', 'Launcher Update Failed');
  96. end;
  97. end;
  98. end;
  99. // Look for client update
  100. if canDoMore then begin
  101. if FileExists(pchar(sourceDir+'\.DMDirc.jar')) then begin
  102. if FileExists(pchar(jarName)) then begin
  103. if not DeleteFile(pchar(jarName)) then begin
  104. showmessage('Unable to update DMDirc.jar', 'Launcher Update Failed');
  105. end;
  106. end;
  107. if MoveFile(pchar(sourceDir+'\.DMDirc.jar'), pchar(jarName)) then begin
  108. showmessage('Client update was successful');
  109. end
  110. else begin
  111. showmessage('Unable to move '''+sourceDir+'\.DMDirc.jar'' to '+jarName, 'Update Failed');
  112. end;
  113. end;
  114. if launcherUpdate then begin
  115. showmessage('The DMDirc launcher has been updated, to complete the update please relaunch DMDirc.', 'Restart Required');
  116. end;
  117. end;
  118. end
  119. else begin
  120. showError('This program can not be run on its own.');
  121. end;
  122. end.