@ -1,21 +1,19 @@
using Eto ;
using Eto ;
using Eto.Forms ;
using log4net ;
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Configuration ;
using System.Configuration ;
using System.Diagnostics ;
using System.Diagnostics ;
using System.IO ;
using System.IO ;
using System.Text ;
using System.Reflection ;
using System.Reflection ;
using System.Text.RegularExpressions ;
using System.Text.RegularExpressions ;
using log4net ;
namespace AM2RLauncher
namespace AM2RLauncher
{
{
/// <summary>
/// <summary>
/// Class that does Operations that work cross-platform.
/// Class that does Operations that work cross-platform.
/// </summary>
/// </summary>
class CrossPlatformOperations
public class CrossPlatformOperations
{
{
/// <summary>
/// <summary>
/// The logger for <see cref="MainForm"/>, used to write any caught exceptions.
/// The logger for <see cref="MainForm"/>, used to write any caught exceptions.
@ -25,17 +23,27 @@ namespace AM2RLauncher
/// <summary>
/// <summary>
/// Gets the current platform.
/// Gets the current platform.
/// </summary>
/// </summary>
readonly private static Platform currentPlatform = Platform . Instance ;
private static readonly Platform currentPlatform = Platform . Instance ;
/// <summary>
/// <summary>
/// Current Path where the Launcher is located. For more info, check <see cref="GenerateCurrentPath"/>.
/// Current Path where the Launcher is located. For more info, check <see cref="GenerateCurrentPath"/>.
/// </summary>
/// </summary>
static readonly public string CURRENTPATH = GenerateCurrentPath ( ) ;
public static readonly string CURRENTPATH = GenerateCurrentPath ( ) ;
/// <summary>
/// <summary>
/// Name of the Launcher executable.
/// Name of the Launcher executable.
/// </summary>
/// </summary>
static readonly public string LAUNCHERNAME = AppDomain . CurrentDomain . FriendlyName ;
public static readonly string LAUNCHERNAME = AppDomain . CurrentDomain . FriendlyName ;
/// <summary>
/// Path to the Home Folder on *Nix-based systems.
/// </summary>
public static readonly string NIXHOME = Environment . GetEnvironmentVariable ( "HOME" ) ;
/// <summary>
/// Path to the Config folder on *Nix-based systems.
/// </summary>
public static readonly string NIXXDGCONFIG = Environment . GetEnvironmentVariable ( "XDG_CONFIG_HOME" ) ;
/// <summary>
/// <summary>
/// Generates the mirror list, depending on the current Platform.
/// Generates the mirror list, depending on the current Platform.
@ -82,9 +90,8 @@ namespace AM2RLauncher
if ( currentPlatform . IsGtk )
if ( currentPlatform . IsGtk )
{
{
// Config for nix systems will be saved in XDG_CONFIG_HOME/AM2RLauncher (or if empty, ~/.config)
// Config for nix systems will be saved in XDG_CONFIG_HOME/AM2RLauncher (or if empty, ~/.config)
string homePath = Environment . GetEnvironmentVariable ( "HOME" ) ;
string homePath = NIXHOME ;
string xdgConfigHome = Environment . GetEnvironmentVariable ( "XDG_CONFIG_HOME" ) ;
string launcherConfigPath = ( String . IsNullOrWhiteSpace ( NIXXDGCONFIG ) ? ( homePath + "/.config" ) : NIXXDGCONFIG ) + "/AM2RLauncher" ;
string launcherConfigPath = ( String . IsNullOrWhiteSpace ( xdgConfigHome ) ? ( homePath + "/.config" ) : xdgConfigHome ) + "/AM2RLauncher" ;
string launcherConfigFilePath = launcherConfigPath + "/config.xml" ;
string launcherConfigFilePath = launcherConfigPath + "/config.xml" ;
XML . LauncherConfigXML launcherConfig = new XML . LauncherConfigXML ( ) ;
XML . LauncherConfigXML launcherConfig = new XML . LauncherConfigXML ( ) ;
@ -130,9 +137,8 @@ namespace AM2RLauncher
else if ( currentPlatform . IsGtk )
else if ( currentPlatform . IsGtk )
{
{
// Config for nix systems will be saved in XDG_CONFIG_HOME/AM2RLauncher (or if empty, ~/.config)
// Config for nix systems will be saved in XDG_CONFIG_HOME/AM2RLauncher (or if empty, ~/.config)
string homePath = Environment . GetEnvironmentVariable ( "HOME" ) ;
string homePath = NIXHOME ;
string xdgConfigHome = Environment . GetEnvironmentVariable ( "XDG_CONFIG_HOME" ) ;
string launcherConfigPath = ( String . IsNullOrWhiteSpace ( NIXXDGCONFIG ) ? ( homePath + "/.config" ) : NIXXDGCONFIG ) + "/AM2RLauncher" ;
string launcherConfigPath = ( String . IsNullOrWhiteSpace ( xdgConfigHome ) ? ( homePath + "/.config" ) : xdgConfigHome ) + "/AM2RLauncher" ;
string launcherConfigFilePath = launcherConfigPath + "/config.xml" ;
string launcherConfigFilePath = launcherConfigPath + "/config.xml" ;
XML . LauncherConfigXML launcherConfig = new XML . LauncherConfigXML ( ) ;
XML . LauncherConfigXML launcherConfig = new XML . LauncherConfigXML ( ) ;
@ -170,7 +176,7 @@ namespace AM2RLauncher
MatchCollection oldMatch = settingRegex . Matches ( oldConfigText ) ;
MatchCollection oldMatch = settingRegex . Matches ( oldConfigText ) ;
MatchCollection newMatch = settingRegex . Matches ( newConfigText ) ;
MatchCollection newMatch = settingRegex . Matches ( newConfigText ) ;
for ( int i = 0 ; i < oldMatch . Count ; i + + )
for ( int i = 0 ; i < oldMatch . Count ; i + + )
newConfigText = newConfigText . Replace ( newMatch [ i ] . Value , oldMatch [ i ] . Value ) ;
newConfigText = newConfigText . Replace ( newMatch [ i ] . Value , oldMatch [ i ] . Value ) ;
File . WriteAllText ( newConfigPath , newConfigText ) ;
File . WriteAllText ( newConfigPath , newConfigText ) ;
@ -179,9 +185,8 @@ namespace AM2RLauncher
else if ( currentPlatform . IsGtk )
else if ( currentPlatform . IsGtk )
{
{
// Config for nix systems will be saved in XDG_CONFIG_HOME/AM2RLauncher (or if empty, ~/.config)
// Config for nix systems will be saved in XDG_CONFIG_HOME/AM2RLauncher (or if empty, ~/.config)
string homePath = Environment . GetEnvironmentVariable ( "HOME" ) ;
string homePath = NIXHOME ;
string xdgConfigHome = Environment . GetEnvironmentVariable ( "XDG_CONFIG_HOME" ) ;
string launcherConfigPath = ( String . IsNullOrWhiteSpace ( NIXXDGCONFIG ) ? ( homePath + "/.config" ) : NIXXDGCONFIG ) + "/AM2RLauncher" ;
string launcherConfigPath = ( String . IsNullOrWhiteSpace ( xdgConfigHome ) ? ( homePath + "/.config" ) : xdgConfigHome ) + "/AM2RLauncher" ;
string launcherConfigFilePath = launcherConfigPath + "/config.xml" ;
string launcherConfigFilePath = launcherConfigPath + "/config.xml" ;
XML . LauncherConfigXML launcherConfig = new XML . LauncherConfigXML ( ) ;
XML . LauncherConfigXML launcherConfig = new XML . LauncherConfigXML ( ) ;
@ -212,7 +217,7 @@ namespace AM2RLauncher
// We have to replace forward slashes with backslashes here on windows because explorer.exe is picky...
// We have to replace forward slashes with backslashes here on windows because explorer.exe is picky...
// And on Nix systems, we want to replace ~ with its corresponding env var
// And on Nix systems, we want to replace ~ with its corresponding env var
string realPath = currentPlatform . IsWinForms ? Environment . ExpandEnvironmentVariables ( path ) . Replace ( "/" , "\\" )
string realPath = currentPlatform . IsWinForms ? Environment . ExpandEnvironmentVariables ( path ) . Replace ( "/" , "\\" )
: path . Replace ( "~" , Environment. GetEnvironmentVariable ( "HOME" ) ) ;
: path . Replace ( "~" , NIXHOME ) ;
if ( ! Directory . Exists ( realPath ) )
if ( ! Directory . Exists ( realPath ) )
{
{
log . Info ( realPath + " did not exist and was created" ) ;
log . Info ( realPath + " did not exist and was created" ) ;
@ -238,7 +243,7 @@ namespace AM2RLauncher
// We have to replace forward slashes with backslashes here on windows because explorer.exe is picky...
// We have to replace forward slashes with backslashes here on windows because explorer.exe is picky...
// And on nix systems, we want to replace ~ with its corresponding env var
// And on nix systems, we want to replace ~ with its corresponding env var
string realPath = currentPlatform . IsWinForms ? Environment . ExpandEnvironmentVariables ( path ) . Replace ( "/" , "\\" )
string realPath = currentPlatform . IsWinForms ? Environment . ExpandEnvironmentVariables ( path ) . Replace ( "/" , "\\" )
: path . Replace ( "~" , Environment. GetEnvironmentVariable ( "HOME" ) ) ;
: path . Replace ( "~" , NIXHOME ) ;
if ( ! File . Exists ( realPath ) )
if ( ! File . Exists ( realPath ) )
{
{
log . Error ( realPath + "did not exist, operation to open its folder was cancelled!" ) ;
log . Error ( realPath + "did not exist, operation to open its folder was cancelled!" ) ;
@ -273,16 +278,16 @@ namespace AM2RLauncher
arguments = "-version" ;
arguments = "-version" ;
}
}
ProcessStartInfo javaStart = new ProcessStartInfo ( ) ;
ProcessStartInfo javaStart = new ProcessStartInfo
javaStart . FileName = process ;
{
javaStart . Arguments = arguments ;
FileName = process ,
javaStart . UseShellExecute = false ;
Arguments = arguments ,
javaStart . CreateNoWindow = true ;
UseShellExecute = false ,
CreateNoWindow = true
} ;
Process java = new Process ( ) ;
java. StartInfo = javaStart ;
Process java = new Process { StartInfo = javaStart } ;
// This is primarily for linux, but could be happening on windows as well
// This is primarily for linux, but could be happening on windows as well
try
try
@ -297,7 +302,6 @@ namespace AM2RLauncher
}
}
return java . ExitCode = = 0 ;
return java . ExitCode = = 0 ;
}
}
/// <summary>
/// <summary>
@ -309,16 +313,16 @@ namespace AM2RLauncher
string process = "xdelta3" ;
string process = "xdelta3" ;
string arguments = "-V" ;
string arguments = "-V" ;
ProcessStartInfo xdeltaStart = new ProcessStartInfo ( ) ;
ProcessStartInfo xdeltaStart = new ProcessStartInfo
xdeltaStart . FileName = process ;
{
xdeltaStart . Arguments = arguments ;
FileName = process ,
xdeltaStart . UseShellExecute = false ;
Arguments = arguments ,
xdeltaStart . CreateNoWindow = true ;
UseShellExecute = false ,
CreateNoWindow = true
} ;
Process xdelta = new Process ( ) ;
xdelta. StartInfo = xdeltaStart ;
Process xdelta = new Process { StartInfo = xdeltaStart } ;
try
try
{
{
@ -347,7 +351,7 @@ namespace AM2RLauncher
if ( original = = output )
if ( original = = output )
output = output + = "_" ;
output = output + = "_" ;
string arguments = "-f -d -s \"" + original . Replace ( CURRENTPATH + "/" , "" ) + "\" \"" + patch . Replace ( CURRENTPATH + "/" , "" )
string arguments = "-f -d -s \"" + original . Replace ( CURRENTPATH + "/" , "" ) + "\" \"" + patch . Replace ( CURRENTPATH + "/" , "" )
+ "\" \"" + output . Replace ( CURRENTPATH + "/" , "" ) + "\"" ;
+ "\" \"" + output . Replace ( CURRENTPATH + "/" , "" ) + "\"" ;
if ( currentPlatform . IsWinForms )
if ( currentPlatform . IsWinForms )
@ -438,7 +442,7 @@ namespace AM2RLauncher
if ( string . IsNullOrWhiteSpace ( xdgDataHome ) )
if ( string . IsNullOrWhiteSpace ( xdgDataHome ) )
{
{
log . Info ( "Using default Linux CurrentPath." ) ;
log . Info ( "Using default Linux CurrentPath." ) ;
xdgDataHome = Environment. GetEnvironmentVariable ( "HOME" ) + "/.local/share" ;
xdgDataHome = NIXHOME + "/.local/share" ;
}
}
// Add AM2RLauncher to the end of the dataPath
// Add AM2RLauncher to the end of the dataPath