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.0KB

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