@ -100,147 +100,146 @@ public static class LauncherUpdater
// Check settings if autoUpdateLauncher is set to true
// Check settings if autoUpdateLauncher is set to true
bool autoUpdate = Boolean . Parse ( MainForm . ReadFromConfig ( "AutoUpdateLauncher" ) ) ;
bool autoUpdate = Boolean . Parse ( MainForm . ReadFromConfig ( "AutoUpdateLauncher" ) ) ;
if ( autoUpdate )
if ( ! autoUpdate )
{
{
log . Info ( "AutoUpdate Launcher set to true!" ) ;
log . Info ( "AutoUpdate Launcher set to false. Exiting update check." ) ;
return ;
}
// This is supposed to fix the updater throwing an exception on windows 7 and earlier(?)
log . Info ( "AutoUpdate Launcher set to true!" ) ;
// See this for information: https://stackoverflow.com/q/2859790 and https://stackoverflow.com/a/50977774
if ( OS . IsWindows )
{
ServicePointManager . Expect100Continue = true ;
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
}
HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( "https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest" ) ;
// This is supposed to fix the updater throwing an exception on windows 7 and earlier(?)
HttpWebResponse response ;
// See this for information: https://stackoverflow.com/q/2859790 and https://stackoverflow.com/a/50977774
try
if ( OS . IsWindows )
{
{
response = ( HttpWebResponse ) request . GetResponse ( ) ;
ServicePointManager . Expect100Continue = true ;
}
ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
catch ( WebException )
}
{
log . Error ( "WebException caught! Displaying MessageBox." ) ;
HttpWebRequest request = ( HttpWebRequest ) WebRequest . Create ( "https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest" ) ;
MessageBox . Show ( Language . Text . NoInternetConnection ) ;
HttpWebResponse response ;
return ;
try
}
{
response = ( HttpWebResponse ) request . GetResponse ( ) ;
}
catch ( WebException )
{
log . Error ( "WebException caught! Displaying MessageBox." ) ;
MessageBox . Show ( Language . Text . NoInternetConnection ) ;
return ;
}
Uri realUri = response . ResponseUri ;
Uri realUri = response . ResponseUri ;
string onlineVersion = realUri . AbsoluteUri . Substring ( realUri . AbsoluteUri . LastIndexOf ( '/' ) + 1 ) ;
string onlineVersion = realUri . AbsoluteUri . Substring ( realUri . AbsoluteUri . LastIndexOf ( '/' ) + 1 ) ;
bool isCurrentVersionOutdated = false ;
bool isCurrentVersionOutdated = false ;
string [ ] localVersionArray = VERSION . Split ( '.' ) ;
string [ ] localVersionArray = VERSION . Split ( '.' ) ;
string [ ] onlineVersionArray = onlineVersion . Split ( '.' ) ;
string [ ] onlineVersionArray = onlineVersion . Split ( '.' ) ;
for ( int i = 0 ; i < localVersionArray . Length ; i + + )
for ( int i = 0 ; i < localVersionArray . Length ; i + + )
{
int onlineNum = Int32 . Parse ( onlineVersionArray [ i ] ) ;
int localNum = Int32 . Parse ( localVersionArray [ i ] ) ;
if ( onlineNum > localNum )
{
{
int onlineNum = Int32 . Parse ( onlineVersionArray [ i ] ) ;
isCurrentVersionOutdated = true ;
int localNum = Int32 . Parse ( localVersionArray [ i ] ) ;
break ;
if ( onlineNum > localNum )
{
isCurrentVersionOutdated = true ;
break ;
}
if ( localNum > onlineNum )
break ;
}
}
if ( localNum > onlineNum )
break ;
}
log . Info ( ( isCurrentVersionOutdated ? "Updating" : "Not Updating" ) + " from " + VERSION + " to " + onlineVersion ) ;
log . Info ( ( isCurrentVersionOutdated ? "Updating" : "Not Updating" ) + " from " + VERSION + " to " + onlineVersion ) ;
// No new update, exiting
// No new update, exiting
if ( ! isCurrentVersionOutdated )
if ( ! isCurrentVersionOutdated )
return ;
return ;
// For mac, we just show a message box that a new version is available, because I don't want to support it yet.
// For mac, we just show a message box that a new version is available, because I don't want to support it yet.
// hardcoded string, since also temporarily until it gets supported one day.
// hardcoded string, since also temporarily until it gets supported one day.
if ( OS . IsMac )
if ( OS . IsMac )
{
{
MessageBox . Show ( "Your current version is outdated! The newest version is " + onlineVersion + ". " +
MessageBox . Show ( "Your current version is outdated! The newest version is " + onlineVersion + ". " +
"Please recompile AM2RLauncher again or disable auto-updating" ) ;
"Please recompile AM2RLauncher again or disable auto-updating" ) ;
return ;
return ;
}
}
log . Info ( "Current version (" + VERSION + ") is outdated! Initiating update for version " + onlineVersion + "." ) ;
log . Info ( "Current version (" + VERSION + ") is outdated! Initiating update for version " + onlineVersion + "." ) ;
string tmpUpdatePath = CrossPlatformOperations . CurrentPath + "/tmpupdate/" ;
string tmpUpdatePath = CrossPlatformOperations . CurrentPath + "/tmpupdate/" ;
string zipPath = CrossPlatformOperations . CurrentPath + "/launcher.zip" ;
string zipPath = CrossPlatformOperations . CurrentPath + "/launcher.zip" ;
// Clean tmpupdate
// Clean tmpupdate
if ( Directory . Exists ( tmpUpdatePath ) )
if ( Directory . Exists ( tmpUpdatePath ) )
Directory . Delete ( tmpUpdatePath , true ) ;
Directory . Delete ( tmpUpdatePath , true ) ;
if ( ! Directory . Exists ( tmpUpdatePath ) )
if ( ! Directory . Exists ( tmpUpdatePath ) )
Directory . CreateDirectory ( tmpUpdatePath ) ;
Directory . CreateDirectory ( tmpUpdatePath ) ;
try
try
{
{
using WebClient client = new WebClient ( ) ;
using WebClient client = new WebClient ( ) ;
string platformSuffix = "" ;
string platformSuffix = "" ;
if ( OS . IsWindows ) platformSuffix = "_win" ;
if ( OS . IsWindows ) platformSuffix = "_win" ;
else if ( OS . IsLinux ) platformSuffix = "_lin" ;
else if ( OS . IsLinux ) platformSuffix = "_lin" ;
log . Info ( "Downloading https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip to " + zipPath + "." ) ;
log . Info ( "Downloading https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip to " + zipPath + "." ) ;
client . DownloadFile ( "https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip" , zipPath ) ;
client . DownloadFile ( "https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip" , zipPath ) ;
log . Info ( "File successfully downloaded." ) ;
log . Info ( "File successfully downloaded." ) ;
}
}
catch ( UnauthorizedAccessException )
catch ( UnauthorizedAccessException )
{
{
log . Error ( "UnauthorizedAccessException caught! Displaying MessageBox." ) ;
log . Error ( "UnauthorizedAccessException caught! Displaying MessageBox." ) ;
MessageBox . Show ( Language . Text . UnauthorizedAccessMessage ) ;
MessageBox . Show ( Language . Text . UnauthorizedAccessMessage ) ;
return ;
return ;
}
}
ZipFile . ExtractToDirectory ( zipPath , tmpUpdatePath ) ;
log . Info ( "Updates successfully extracted to " + tmpUpdatePath ) ;
ZipFile . ExtractToDirectory ( zipPath , tmpUpdatePath ) ;
File . Delete ( zipPath ) ;
log . Info ( "Updates successfully extracted to " + tmpUpdatePath ) ;
File . Move ( updatePath + "/" + CrossPlatformOperations . LauncherName , CrossPlatformOperations . CurrentPath + "/AM2RLauncher.bak" ) ;
if ( OS . IsWindows ) File . Move ( CrossPlatformOperations . LauncherName + ".config" , CrossPlatformOperations . LauncherName + ".oldCfg" ) ;
File . Delete ( zipPath ) ;
foreach ( FileInfo file in new DirectoryInfo ( tmpUpdatePath ) . GetFiles ( ) )
File . Move ( updatePath + "/" + CrossPlatformOperations . LauncherName , CrossPlatformOperations . CurrentPath + "/AM2RLauncher.bak" ) ;
{
if ( OS . IsWindows ) File . Move ( CrossPlatformOperations . LauncherName + ".config" , CrossPlatformOperations . LauncherName + ".oldCfg" ) ;
log . Info ( "Moving " + file . FullName + " to " + CrossPlatformOperations . CurrentPath + "/" + file . Name ) ;
File . Copy ( file . FullName , updatePath + "/" + file . Name , true ) ;
}
// For windows, the actual application is in "AM2RLauncher.dll". Which means, we need to update the lib folder as well.
if ( OS . IsWindows & & Directory . Exists ( CrossPlatformOperations . CurrentPath + "/lib" ) )
{
// So, because Windows behavior is dumb...
foreach ( FileInfo file in new DirectoryInfo ( tmpUpdatePath ) . GetFiles ( ) )
// Rename all files in lib to *.bak
foreach ( FileInfo file in new DirectoryInfo ( CrossPlatformOperations . CurrentPath + "/lib" ) . GetFiles ( ) )
{
{
log . Info ( "Moving " + file . FullName + " to " + CrossPlatformOperations . CurrentPath + "/" + file . Name ) ;
file . CopyTo ( file . Directory + "/" + file . Name + ".bak" ) ;
File . Copy ( file . FullName , updatePath + "/" + file . Name , true ) ;
}
}
// For windows, the actual application is in "AM2RLauncher.dll". Which means, we need to update the lib folder as well.
if ( OS . IsWindows & & Directory . Exists ( CrossPlatformOperations . CurrentPath + "/lib" ) )
{
// So, because Windows behavior is dumb...
// Rename all files in lib to *.bak
// Do the same for each sub directory
foreach ( FileInfo file in new DirectoryInfo ( CrossPlatformOperations . CurrentPath + "/lib" ) . GetFiles ( ) )
foreach ( DirectoryInfo dir in new DirectoryInfo ( CrossPlatformOperations . CurrentPath + "/lib" ) . GetDirectories ( ) )
{
{
file . CopyTo ( file . Directory + "/" + file . Name + ".bak" ) ;
foreach ( FileInfo file in dir . GetFiles ( ) )
}
// Do the same for each sub directory
foreach ( DirectoryInfo dir in new DirectoryInfo ( CrossPlatformOperations . CurrentPath + "/lib" ) . GetDirectories ( ) )
{
{
foreach ( FileInfo file in dir . GetFiles ( ) )
file . CopyTo ( file . Directory + "/" + file . Name + ".bak" ) ;
{
file . CopyTo ( file . Directory + "/" + file . Name + ".bak" ) ;
}
}
}
// Yes, the above calls could be recursive. No, I can't be bothered to make them as such.
if ( Directory . Exists ( tmpUpdatePath + "lib" ) )
HelperMethods . DirectoryCopy ( tmpUpdatePath + "lib" , CrossPlatformOperations . CurrentPath + "/lib" ) ;
}
}
Directory . Delete ( tmpUpdatePath , true ) ;
// Yes, the above calls could be recursive. No, I can't be bothered to make them as such.
if ( Directory . Exists ( tmpUpdatePath + "lib" ) )
HelperMethods . DirectoryCopy ( tmpUpdatePath + "lib" , CrossPlatformOperations . CurrentPath + "/lib" ) ;
}
MainForm . CopyOldConfigToNewConfig ( ) ;
Directory . Delete ( tmpUpdatePath , true ) ;
log . Info ( "Files extracted. Preparing to restart executable..." ) ;
MainForm . CopyOldConfigToNewConfig ( ) ;
if ( OS . IsLinux ) Process . Start ( "chmod" , "+x " + updatePath + "./AM2RLauncher.Gtk" ) ;
Process . Start ( updatePath + "/" + CrossPlatformOperations . LauncherName ) ;
log . Info ( "Files extracted. Preparing to restart executable..." ) ;
Environment . Exit ( 0 ) ;
if ( OS . IsLinux ) Process . Start ( "chmod" , "+x " + updatePath + "./AM2RLauncher.Gtk" ) ;
}
else
Process . Start ( updatePath + "/" + CrossPlatformOperations . LauncherName ) ;
{
Environment . Exit ( 0 ) ;
log . Info ( "AutoUpdate Launcher set to false. Exiting update check." ) ;
}
}
}
}
}