Reduce currentPath redudancy

pull/32/head
Miepee 4 years ago
parent 5b2147f7f8
commit e4e1d3303b

@ -5,6 +5,7 @@ using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using AM2RLauncher.Core;
using log4net.Repository.Hierarchy;
using Application = Eto.Forms.Application;
using FileInfo = System.IO.FileInfo;
@ -26,7 +27,7 @@ internal static class MainClass
[STAThread]
public static void Main()
{
string launcherDataPath = GenerateCurrentPath();
string launcherDataPath = CrossPlatformOperations.CURRENTPATH;
// Make sure first, ~/.local/share/AM2RLauncher exists
if (!Directory.Exists(launcherDataPath))
@ -84,44 +85,4 @@ internal static class MainClass
MessageBox.Show(Language.Text.UnhandledException + "\n*****Stack Trace*****\n\n" + e.ExceptionObject, "GTK", MessageBoxType.Error);
});
}
// This is a duplicate of CrossPlatformOperations.GenerateCurrentPath, because trying to invoke that would cause a crash due to currentPlatform not being initialized.
private static string GenerateCurrentPath()
{
string nixHome = Environment.GetEnvironmentVariable("HOME");
// First, we check if the user has a custom AM2RLAUNCHERDATA env var
string am2rLauncherDataEnvVar = Environment.GetEnvironmentVariable("AM2RLAUNCHERDATA");
if (!String.IsNullOrWhiteSpace(am2rLauncherDataEnvVar))
{
try
{
// This will create the directories recursively if they don't exist
Directory.CreateDirectory(am2rLauncherDataEnvVar);
// Our env var is now set and directories exist
return am2rLauncherDataEnvVar;
}
catch { }
}
// First check if XDG_DATA_HOME is set, if not we'll use ~/.local/share
string xdgDataHome = Environment.GetEnvironmentVariable("XDG_DATA_HOME");
if (String.IsNullOrWhiteSpace(xdgDataHome))
xdgDataHome = nixHome + "/.local/share";
// Add AM2RLauncher to the end of the dataPath
xdgDataHome += "/AM2RLauncher";
try
{
// This will create the directories recursively if they don't exist
Directory.CreateDirectory(xdgDataHome);
// Our env var is now set and directories exist
return xdgDataHome;
}
catch { }
return Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
}
}

@ -4,6 +4,7 @@ using log4net.Config;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using AM2RLauncher.Core;
using log4net.Repository.Hierarchy;
namespace AM2RLauncher.Mac;
@ -24,7 +25,7 @@ internal static class MainClass
[STAThread]
public static void Main()
{
string launcherDataPath = GenerateCurrentPath();
string launcherDataPath = CrossPlatformOperations.CURRENTPATH;
// Make sure first, ~/.local/share/AM2RLauncher exists
if (!Directory.Exists(launcherDataPath))
@ -68,35 +69,4 @@ internal static class MainClass
MessageBox.Show(Language.Text.UnhandledException + "\n*****Stack Trace*****\n\n" + e.ExceptionObject, "Mac", MessageBoxType.Error);
});
}
// This is a duplicate of CrossPlatformOperations.GenerateCurrentPath, because trying to invoke that would cause a crash due to currentPlatform not being initialized.
[return: NotNull]
private static string GenerateCurrentPath()
{
string am2rLauncherDataEnvVar = Environment.GetEnvironmentVariable("AM2RLAUNCHERDATA");
if (!String.IsNullOrWhiteSpace(am2rLauncherDataEnvVar))
{
try
{
// This will create the directories recursively if they don't exist
Directory.CreateDirectory(am2rLauncherDataEnvVar);
// Our env var is now set and directories exist
return am2rLauncherDataEnvVar;
}
catch { }
}
string nixHome = Environment.GetEnvironmentVariable("HOME");
//Mac has the Path at HOME/Library/AM2RLauncher
string macPath = nixHome + "/Library/AM2RLauncher";
try
{
Directory.CreateDirectory(macPath);
return macPath;
}
catch { }
return Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
}
}

@ -4,6 +4,7 @@ using log4net.Config;
using System;
using System.IO;
using System.Reflection;
using AM2RLauncher.Core;
using log4net.Repository.Hierarchy;
namespace AM2RLauncher.Wpf;
@ -23,7 +24,7 @@ internal static class MainClass
[STAThread]
public static void Main()
{
string launcherDataPath = GenerateCurrentPath();
string launcherDataPath = CrossPlatformOperations.CURRENTPATH;
// Make sure first, that the path exists
if (!Directory.Exists(launcherDataPath))
@ -69,28 +70,4 @@ internal static class MainClass
log.Error("An unhandled exception has occurred: \n*****Stack Trace*****\n\n" + e.ExceptionObject);
MessageBox.Show(Language.Text.UnhandledException + "\n*****Stack Trace*****\n\n" + e.ExceptionObject, "Microsoft .NET Framework", MessageBoxType.Error);
}
// This is a duplicate of CrossPlatformOperations.GenerateCurrentPath, because trying to invoke that would cause a crash due to currentPlatform not being initialized.
private static string GenerateCurrentPath()
{
// First, we check if the user has a custom AM2RLAUNCHERDATA env var
string am2rLauncherDataEnvVar = Environment.GetEnvironmentVariable("AM2RLAUNCHERDATA");
// Windows has the path where the exe is located as default
string defaultPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
if (String.IsNullOrWhiteSpace(am2rLauncherDataEnvVar))
return defaultPath;
try
{
// This will create the directories recursively if they don't exist
Directory.CreateDirectory(am2rLauncherDataEnvVar);
// Our env var is now set and directories exist
return am2rLauncherDataEnvVar;
}
catch
{
return defaultPath;
}
}
}
Loading…
Cancel
Save