11using Common . Logging ;
22using NuGet . Versioning ;
3- using System ;
43using System . Diagnostics ;
5- using System . IO ;
4+ using System . Net . Http . Headers ;
65using System . Reflection ;
6+ using System . Runtime . InteropServices ;
77using System . Text ;
8-
9- #if ! DEBUG
10- using Common ;
11- using System . Net . Http ;
12- using System . Net . Http . Headers ;
138using System . Text . Json ;
14- using System ;
15- #endif
169
17- namespace Gui ;
10+ namespace Common ;
1811
1912public static class VersionHelpers
2013{
21- public const string GithubApplicationName = "ObjectEditor" ;
14+ public const string ObjectEditorName = "ObjectEditor" ;
2215 public const string ObjectEditorUpdaterName = "ObjectEditorUpdater" ;
2316 public const string GithubIssuePage = "https://github.com/OpenLoco/ObjectEditor/issues" ;
2417 public const string GithubLatestReleaseDownloadPage = "https://github.com/OpenLoco/ObjectEditor/releases" ;
2518 public const string GithubLatestReleaseAPI = "https://api.github.com/repos/OpenLoco/ObjectEditor/releases/latest" ;
2619
27- // todo: instead of going to downloads, start the auto-updater (ObjectEditorUpdater.exe) with the right args
2820 public static Process ? OpenDownloadPage ( )
2921 => Process . Start ( new ProcessStartInfo ( GithubLatestReleaseDownloadPage ) { UseShellExecute = true } ) ;
3022
31- public static void StartAutoUpdater ( ILogger logger , SemanticVersion latestVersion )
23+ // win: object-editor-5.3.5-win-x64.zip
24+ // osx: object-editor-5.3.5-osx-x64.tar
25+ // linux: object-editor-5.3.5-linux-x64.tar
26+
27+ static string DownloadFilename ( SemanticVersion latestVersion , OSPlatform platform )
28+ => $ "object-editor-{ latestVersion } -{ PlatformSpecific . EditorPlatformZipName ( platform ) } ";
29+
30+ public static string UrlForDownload ( SemanticVersion latestVersion , OSPlatform platform )
31+ => $ "{ GithubLatestReleaseDownloadPage } /download/{ latestVersion } /{ DownloadFilename ( latestVersion , platform ) } ";
32+
33+ public static void StartAutoUpdater ( ILogger logger , SemanticVersion currentVersion , SemanticVersion latestVersion )
3234 {
35+ logger . Debug ( "Attempting to kill existing updater processes" ) ;
3336 try
3437 {
3538 // kill any existing processes of the updater
@@ -46,38 +49,52 @@ public static void StartAutoUpdater(ILogger logger, SemanticVersion latestVersio
4649 }
4750 }
4851
49- // win: object-editor-5.3.5-win-x64.zip
50- // osx: object-editor-5.3.5-osx-x64.tar
51- // linux: object-editor-5.3.5-linux-x64.tar
52- var platform = PlatformSpecific . EditorPlatformExtension ;
53- var filename = $ "object-editor-{ latestVersion } -{ platform } ";
52+ var editorExe = $ "{ ObjectEditorUpdaterName } .exe";
53+ if ( ! File . Exists ( editorExe ) )
54+ {
55+ logger . Error ( $ "Cannot find the auto-updater executable: { editorExe } . You'll need to manually download the update.") ;
56+ return ;
57+ }
5458
55- var startInfo = new ProcessStartInfo ( $ " { ObjectEditorUpdaterName } .exe" ,
59+ var startInfo = new ProcessStartInfo ( editorExe ,
5660 [
5761 "--pid" ,
5862 $ "{ Environment . ProcessId } ",
59- "--url " ,
60- $ " { GithubLatestReleaseDownloadPage } /download/ { latestVersion } / { filename } " ,
63+ "--current-version " ,
64+ currentVersion . ToString ( ) ,
6165 "--app-path" ,
6266 $ "{ Environment . ProcessPath } ",
6367 ] )
6468 {
69+ // updater process will log to file
6570 UseShellExecute = false ,
6671 CreateNoWindow = true ,
6772 } ;
6873
74+ logger . Debug ( $ "CurrentProcessId: { Environment . ProcessId } ") ;
75+ logger . Debug ( $ "CurrentProcessPath: { Environment . ProcessPath } ") ;
76+ logger . Debug ( $ "Attempting to start auto-updater \" { startInfo } \" ") ;
6977 var process = Process . Start ( startInfo ) ;
70- Environment . Exit ( 0 ) ;
78+
79+ if ( process != null )
80+ {
81+ logger . Info ( $ "Started auto-updater process (PID { process . Id } ) to update from { currentVersion } to { latestVersion } . Editor will now close") ;
82+ Environment . Exit ( 0 ) ;
83+ }
84+ else
85+ {
86+ logger . Error ( "Failed to start auto-updater process. You'll need to manually download the update." ) ;
87+ }
7188 }
7289 catch ( Exception ex )
7390 {
74- Debug . WriteLine ( $ "Failed to start auto-updater: { ex } ") ;
91+ logger . Error ( $ "Failed to start auto-updater: { ex } ") ;
7592 }
7693 }
7794
7895 public static SemanticVersion GetCurrentAppVersion ( )
7996 {
80- var assembly = Assembly . GetExecutingAssembly ( ) ;
97+ var assembly = Assembly . GetCallingAssembly ( ) ;
8198 if ( assembly == null )
8299 {
83100 return UnknownVersion ;
@@ -94,12 +111,11 @@ public static SemanticVersion GetCurrentAppVersion()
94111 }
95112 }
96113
97- #if ! DEBUG
98114 // thanks for this one @IntelOrca, https://github.com/IntelOrca/PeggleEdit/blob/master/src/peggleedit/Forms/MainMDIForm.cs#L848-L861
99- public static SemanticVersion GetLatestAppVersion ( SemanticVersion currentVersion )
115+ public static SemanticVersion GetLatestAppVersion ( string productName , SemanticVersion ? currentVersion = null )
100116 {
101117 var client = new HttpClient ( ) ;
102- client . DefaultRequestHeaders . UserAgent . Add ( new ProductInfoHeaderValue ( GithubApplicationName , currentVersion . ToString ( ) ) ) ;
118+ client . DefaultRequestHeaders . UserAgent . Add ( new ProductInfoHeaderValue ( productName , currentVersion ? . ToString ( ) ) ) ;
103119 var response = client . GetAsync ( GithubLatestReleaseAPI ) . Result ;
104120 if ( response . IsSuccessStatusCode )
105121 {
@@ -109,11 +125,8 @@ public static SemanticVersion GetLatestAppVersion(SemanticVersion currentVersion
109125 return GetVersionFromText ( versionText ) ;
110126 }
111127
112- #pragma warning disable CA2201 // Do not raise reserved exception types
113- throw new Exception ( $ "Unable to get latest version. Error={ response . StatusCode } ") ;
114- #pragma warning restore CA2201 // Do not raise reserved exception types
128+ return UnknownVersion ;
115129 }
116- #endif
117130
118131 public static readonly SemanticVersion UnknownVersion = new ( 0 , 0 , 0 , "unknown" ) ;
119132
0 commit comments