Cleanup CrossPlatformOperations

pull/32/head
Miepee 4 years ago
parent c2fa4c27a9
commit 972c59e056

@ -27,7 +27,7 @@ internal static class MainClass
[STAThread] [STAThread]
public static void Main() public static void Main()
{ {
string launcherDataPath = CrossPlatformOperations.CURRENTPATH; string launcherDataPath = CrossPlatformOperations.CurrentPath;
// Make sure first, ~/.local/share/AM2RLauncher exists // Make sure first, ~/.local/share/AM2RLauncher exists
if (!Directory.Exists(launcherDataPath)) if (!Directory.Exists(launcherDataPath))

@ -25,7 +25,7 @@ internal static class MainClass
[STAThread] [STAThread]
public static void Main() public static void Main()
{ {
string launcherDataPath = CrossPlatformOperations.CURRENTPATH; string launcherDataPath = CrossPlatformOperations.CurrentPath;
// Make sure first, ~/.local/share/AM2RLauncher exists // Make sure first, ~/.local/share/AM2RLauncher exists
if (!Directory.Exists(launcherDataPath)) if (!Directory.Exists(launcherDataPath))

@ -24,7 +24,7 @@ internal static class MainClass
[STAThread] [STAThread]
public static void Main() public static void Main()
{ {
string launcherDataPath = CrossPlatformOperations.CURRENTPATH; string launcherDataPath = CrossPlatformOperations.CurrentPath;
// Make sure first, that the path exists // Make sure first, that the path exists
if (!Directory.Exists(launcherDataPath)) if (!Directory.Exists(launcherDataPath))

@ -22,10 +22,10 @@ namespace AM2RLauncher
public const string VERSION = Core.Core.Version; public const string VERSION = Core.Core.Version;
/// <summary>The Path of the oldConfig. Only gets used Windows-only</summary> /// <summary>The Path of the oldConfig. Only gets used Windows-only</summary>
private static readonly string oldConfigPath = CrossPlatformOperations.CURRENTPATH + "/" + CrossPlatformOperations.LAUNCHERNAME + ".oldCfg"; private static readonly string oldConfigPath = CrossPlatformOperations.CurrentPath + "/" + CrossPlatformOperations.LauncherName + ".oldCfg";
/// <summary>The actual Path where the executable is stored, only used for updating.</summary> /// <summary>The actual Path where the executable is stored, only used for updating.</summary>
private static readonly string updatePath = OS.IsWindows ? CrossPlatformOperations.CURRENTPATH private static readonly string updatePath = OS.IsWindows ? CrossPlatformOperations.CurrentPath
: (OS.IsLinux ? Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) : Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "../../../")); : (OS.IsLinux ? Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) : Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "../../../"));
/// <summary> /// <summary>
@ -43,33 +43,33 @@ namespace AM2RLauncher
// Update section // Update section
// Clean old files that have been left // Clean old files that have been left
if (File.Exists(CrossPlatformOperations.CURRENTPATH + "/AM2RLauncher.bak")) if (File.Exists(CrossPlatformOperations.CurrentPath + "/AM2RLauncher.bak"))
{ {
log.Info("AM2RLauncher.bak detected. Removing file."); log.Info("AM2RLauncher.bak detected. Removing file.");
File.Delete(CrossPlatformOperations.CURRENTPATH + "/AM2RLauncher.bak"); File.Delete(CrossPlatformOperations.CurrentPath + "/AM2RLauncher.bak");
} }
if (OS.IsWindows && File.Exists(oldConfigPath)) if (OS.IsWindows && File.Exists(oldConfigPath))
{ {
log.Info(CrossPlatformOperations.LAUNCHERNAME + ".oldCfg detected. Removing file."); log.Info(CrossPlatformOperations.LauncherName + ".oldCfg detected. Removing file.");
File.Delete(oldConfigPath); File.Delete(oldConfigPath);
} }
if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CURRENTPATH + "/oldLib")) if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CurrentPath + "/oldLib"))
{ {
log.Info("Old lib folder detected, removing folder."); log.Info("Old lib folder detected, removing folder.");
Directory.Delete(CrossPlatformOperations.CURRENTPATH + "/oldLib", true); Directory.Delete(CrossPlatformOperations.CurrentPath + "/oldLib", true);
} }
// Clean up old update libs // Clean up old update libs
if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CURRENTPATH + "/lib")) if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CurrentPath + "/lib"))
{ {
foreach (FileInfo file in new DirectoryInfo(CrossPlatformOperations.CURRENTPATH + "/lib").GetFiles()) foreach (FileInfo file in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetFiles())
{ {
if (file.Name.EndsWith(".bak")) if (file.Name.EndsWith(".bak"))
file.Delete(); file.Delete();
} }
// Do the same for each subdirectory // Do the same for each subdirectory
foreach (DirectoryInfo dir in new DirectoryInfo(CrossPlatformOperations.CURRENTPATH + "/lib").GetDirectories()) foreach (DirectoryInfo dir in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetDirectories())
{ {
foreach (FileInfo file in dir.GetFiles()) foreach (FileInfo file in dir.GetFiles())
{ {
@ -144,8 +144,8 @@ namespace AM2RLauncher
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))
@ -177,27 +177,27 @@ namespace AM2RLauncher
log.Info("Updates successfully extracted to " + tmpUpdatePath); log.Info("Updates successfully extracted to " + tmpUpdatePath);
File.Delete(zipPath); File.Delete(zipPath);
File.Move(updatePath + "/" + CrossPlatformOperations.LAUNCHERNAME, CrossPlatformOperations.CURRENTPATH + "/AM2RLauncher.bak"); File.Move(updatePath + "/" + CrossPlatformOperations.LauncherName, CrossPlatformOperations.CurrentPath + "/AM2RLauncher.bak");
if (OS.IsWindows) File.Move(CrossPlatformOperations.LAUNCHERNAME + ".config", CrossPlatformOperations.LAUNCHERNAME + ".oldCfg"); if (OS.IsWindows) File.Move(CrossPlatformOperations.LauncherName + ".config", CrossPlatformOperations.LauncherName + ".oldCfg");
foreach (var file in new DirectoryInfo(tmpUpdatePath).GetFiles()) foreach (var file in new DirectoryInfo(tmpUpdatePath).GetFiles())
{ {
log.Info("Moving " + file.FullName + " to " + CrossPlatformOperations.CURRENTPATH + "/" + file.Name); log.Info("Moving " + file.FullName + " to " + CrossPlatformOperations.CurrentPath + "/" + file.Name);
File.Copy(file.FullName, updatePath + "/" + file.Name, true); 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. // 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")) if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CurrentPath + "/lib"))
{ {
// So, because Windows behavior is dumb... // So, because Windows behavior is dumb...
// Rename all files in lib to *.bak // Rename all files in lib to *.bak
foreach (FileInfo file in new DirectoryInfo(CrossPlatformOperations.CURRENTPATH + "/lib").GetFiles()) foreach (FileInfo file in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetFiles())
{ {
file.CopyTo(file.Directory + "/" + file.Name + ".bak"); file.CopyTo(file.Directory + "/" + file.Name + ".bak");
} }
// Do the same for each sub directory // Do the same for each sub directory
foreach (DirectoryInfo dir in new DirectoryInfo(CrossPlatformOperations.CURRENTPATH + "/lib").GetDirectories()) foreach (DirectoryInfo dir in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetDirectories())
{ {
foreach (FileInfo file in dir.GetFiles()) foreach (FileInfo file in dir.GetFiles())
{ {
@ -207,7 +207,7 @@ namespace AM2RLauncher
// Yes, the above calls could be recursive. No, I can't be bothered to make them as such. // Yes, the above calls could be recursive. No, I can't be bothered to make them as such.
if (Directory.Exists(tmpUpdatePath + "lib")) if (Directory.Exists(tmpUpdatePath + "lib"))
HelperMethods.DirectoryCopy(tmpUpdatePath + "lib", CrossPlatformOperations.CURRENTPATH + "/lib"); HelperMethods.DirectoryCopy(tmpUpdatePath + "lib", CrossPlatformOperations.CurrentPath + "/lib");
} }
Directory.Delete(tmpUpdatePath, true); Directory.Delete(tmpUpdatePath, true);
@ -217,7 +217,7 @@ namespace AM2RLauncher
log.Info("Files extracted. Preparing to restart executable..."); log.Info("Files extracted. Preparing to restart executable...");
if (OS.IsLinux) System.Diagnostics.Process.Start("chmod", "+x " + updatePath + "./AM2RLauncher.Gtk"); if (OS.IsLinux) System.Diagnostics.Process.Start("chmod", "+x " + updatePath + "./AM2RLauncher.Gtk");
System.Diagnostics.Process.Start(updatePath + "/" + CrossPlatformOperations.LAUNCHERNAME); System.Diagnostics.Process.Start(updatePath + "/" + CrossPlatformOperations.LauncherName);
Environment.Exit(0); Environment.Exit(0);
} }
else else

@ -287,7 +287,7 @@ namespace AM2RLauncher
log.Error(ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace); log.Error(ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace);
MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error); MessageBox.Show(this, ex.Message + "\n*****Stack Trace*****\n\n" + ex.StackTrace, Text.ErrorWindowTitle, MessageBoxType.Error);
if (Directory.Exists(CrossPlatformOperations.CURRENTPATH + " / PatchData")) if (Directory.Exists(CrossPlatformOperations.CurrentPath + " / PatchData"))
HelperMethods.DeleteDirectory(Core.Core.PatchDataPath); HelperMethods.DeleteDirectory(Core.Core.PatchDataPath);
successful = false; successful = false;
} }

@ -82,7 +82,7 @@ namespace AM2RLauncher
{ {
OpenFileDialog fileDialog = new OpenFileDialog OpenFileDialog fileDialog = new OpenFileDialog
{ {
Directory = new Uri(CrossPlatformOperations.CURRENTPATH), Directory = new Uri(CrossPlatformOperations.CurrentPath),
MultiSelect = false, MultiSelect = false,
Title = title Title = title
}; };
@ -133,8 +133,8 @@ namespace AM2RLauncher
} }
if (OS.IsUnix) if (OS.IsUnix)
{ {
string launcherConfigPath = CrossPlatformOperations.NIXLAUNCHERCONFIGPATH; string launcherConfigFilePath = CrossPlatformOperations.NixLauncherConfigFilePath;
string launcherConfigFilePath = launcherConfigPath + "/config.xml"; string launcherConfigPath = Path.GetDirectoryName(launcherConfigFilePath);
XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML(); XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML();
// If folder doesn't exist, create it and the config file // If folder doesn't exist, create it and the config file
@ -178,8 +178,8 @@ namespace AM2RLauncher
} }
else if (OS.IsUnix) else if (OS.IsUnix)
{ {
string launcherConfigPath = CrossPlatformOperations.NIXLAUNCHERCONFIGPATH; string launcherConfigFilePath = CrossPlatformOperations.NixLauncherConfigFilePath;
string launcherConfigFilePath = CrossPlatformOperations.NIXLAUNCHERCONFIGFILEPATH; string launcherConfigPath = Path.GetDirectoryName(launcherConfigFilePath);
XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML(); XML.LauncherConfigXML launcherConfig = new XML.LauncherConfigXML();
// If folder doesn't exist, create it and the config file // If folder doesn't exist, create it and the config file
@ -208,8 +208,8 @@ namespace AM2RLauncher
{ {
if (OS.IsWindows) if (OS.IsWindows)
{ {
string oldConfigPath = CrossPlatformOperations.LAUNCHERNAME + ".oldCfg"; string oldConfigPath = CrossPlatformOperations.LauncherName + ".oldCfg";
string newConfigPath = CrossPlatformOperations.LAUNCHERNAME + ".config"; string newConfigPath = CrossPlatformOperations.LauncherName + ".config";
string oldConfigText = File.ReadAllText(oldConfigPath); string oldConfigText = File.ReadAllText(oldConfigPath);
string newConfigText = File.ReadAllText(newConfigPath); string newConfigText = File.ReadAllText(newConfigPath);
@ -226,7 +226,7 @@ namespace AM2RLauncher
} }
else if (OS.IsUnix) else if (OS.IsUnix)
{ {
string launcherConfigFilePath = CrossPlatformOperations.NIXLAUNCHERCONFIGFILEPATH; string launcherConfigFilePath = CrossPlatformOperations.NixLauncherConfigFilePath;
// For some reason deserializing and saving back again works, not exactly sure why, but I'll take it // For some reason deserializing and saving back again works, not exactly sure why, but I'll take it
XML.LauncherConfigXML launcherConfig = Serializer.Deserialize<XML.LauncherConfigXML>(File.ReadAllText(launcherConfigFilePath)); XML.LauncherConfigXML launcherConfig = Serializer.Deserialize<XML.LauncherConfigXML>(File.ReadAllText(launcherConfigFilePath));

@ -111,7 +111,7 @@ namespace AM2RLauncher
log.Info("Current OS is: " + OS.Name); log.Info("Current OS is: " + OS.Name);
// Set the Current Directory to the path the Launcher is located. Fixes some relative path issues. // Set the Current Directory to the path the Launcher is located. Fixes some relative path issues.
Environment.CurrentDirectory = CrossPlatformOperations.CURRENTPATH; Environment.CurrentDirectory = CrossPlatformOperations.CurrentPath;
log.Info("Set Launcher CWD to " + Environment.CurrentDirectory); log.Info("Set Launcher CWD to " + Environment.CurrentDirectory);
// But log actual folder location nonetheless // But log actual folder location nonetheless

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion> <LangVersion>default</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

@ -27,22 +27,22 @@ public static class Core
/// <summary> /// <summary>
/// Path where the Launcher's PatchData folder is located. /// Path where the Launcher's PatchData folder is located.
/// </summary> /// </summary>
public static readonly string PatchDataPath = CrossPlatformOperations.CURRENTPATH + "/PatchData"; public static readonly string PatchDataPath = CrossPlatformOperations.CurrentPath + "/PatchData";
/// <summary> /// <summary>
/// Path where the AM2R_11.zip is located. /// Path where the AM2R_11.zip is located.
/// </summary> /// </summary>
public static readonly string AM2R11File = CrossPlatformOperations.CURRENTPATH + "/AM2R_11.zip"; public static readonly string AM2R11File = CrossPlatformOperations.CurrentPath + "/AM2R_11.zip";
/// <summary> /// <summary>
/// Path where the Launcher's Profiles folder is located. /// Path where the Launcher's Profiles folder is located.
/// </summary> /// </summary>
public static readonly string ProfilesPath = CrossPlatformOperations.CURRENTPATH + "/Profiles"; public static readonly string ProfilesPath = CrossPlatformOperations.CurrentPath + "/Profiles";
/// <summary> /// <summary>
/// Path where the Launcher's Mods folder is located. /// Path where the Launcher's Mods folder is located.
/// </summary> /// </summary>
public static readonly string ModsPath = CrossPlatformOperations.CURRENTPATH + "/Mods"; public static readonly string ModsPath = CrossPlatformOperations.CurrentPath + "/Mods";
/// <summary> /// <summary>
/// Checks if this is run via WINE. /// Checks if this is run via WINE.

@ -1,7 +1,6 @@
using log4net; using log4net;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
@ -21,36 +20,49 @@ public static class CrossPlatformOperations
/// <summary> /// <summary>
/// Name of the Launcher executable. /// Name of the Launcher executable.
/// </summary> /// </summary>
public static readonly string LAUNCHERNAME = AppDomain.CurrentDomain.FriendlyName; public static readonly string LauncherName = AppDomain.CurrentDomain.FriendlyName;
/// <summary> /// <summary>
/// Path to the Home Folder on *Nix-based systems. /// Path to the Home Folder.
/// </summary> /// </summary>
public static readonly string NIXHOME = Environment.GetEnvironmentVariable("HOME"); public static readonly string Home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
/// <summary> /// <summary>
/// Path to the Config folder on Linux-based systems. /// Config file path for *nix based systems. <br/>
/// </summary> /// </summary>
private static readonly string LINUXXDGCONFIG = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME"); /// <remarks>
/// Linux: Will point to XDG_CONFIG_HOME/AM2RLauncher/config.xml <br/>
/// Mac: Will point to ~/Library/Preferences/AM2RLauncher/config.xml. <br/>
/// Anything else: <see langword="null"/>
/// </remarks>
public static string NixLauncherConfigFilePath
{
get
{
switch (OS.Name)
{
case "Linux": return $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}/AM2RLauncher/config.xml";
case "Mac": return $"{Home}/Library/Preferences/AM2RLauncher/config.xml";
default: return null;
}
}
}
/// <summary> private static string _currentPath;
/// Path to the Config file folder on *nix based systems. <br/>
/// Linux: Will point to XDG_CONFIG_HOME/AM2RLauncher <br/>
/// Mac: Will point to ~/Library/Preferences/AM2RLauncher
/// </summary>
public static readonly string NIXLAUNCHERCONFIGPATH = OS.IsLinux ? (String.IsNullOrWhiteSpace(LINUXXDGCONFIG) ? NIXHOME + "/.config"
: LINUXXDGCONFIG) + "/AM2RLauncher"
: NIXHOME + "/Library/Preferences/AM2RLauncher";
/// <summary> /// <summary>
/// Config file path for *nix based systems. Will be <see cref="NIXLAUNCHERCONFIGPATH"/> + "/config.xml". /// Current Path where the Launcher Data is located.
/// </summary> /// </summary>
public static readonly string NIXLAUNCHERCONFIGFILEPATH = NIXLAUNCHERCONFIGPATH + "/config.xml"; public static string CurrentPath
{
get
{
if (_currentPath is null)
_currentPath = GenerateCurrentPath();
/// <summary> return _currentPath;
/// Current Path where the Launcher is located. For more info, check <see cref="GenerateCurrentPath"/>. }
/// </summary> }
public static readonly string CURRENTPATH = GenerateCurrentPath();
/// <summary> /// <summary>
/// Generates the mirror list, depending on the current Platform. /// Generates the mirror list, depending on the current Platform.
@ -116,7 +128,7 @@ public static class CrossPlatformOperations
// 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 = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\") string realPath = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\")
: path.Replace("~", NIXHOME); : path.Replace("~", Home);
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");
@ -146,7 +158,7 @@ public static class CrossPlatformOperations
// 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 = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\") string realPath = OS.IsWindows ? Environment.ExpandEnvironmentVariables(path).Replace("/", "\\")
: path.Replace("~", NIXHOME); : path.Replace("~", Home);
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!");
@ -257,16 +269,16 @@ public static class CrossPlatformOperations
if (original == output) if (original == 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 (OS.IsWindows) if (OS.IsWindows)
{ {
// We want some fancy parameters for Windows because the terminal scares end users :( // We want some fancy parameters for Windows because the terminal scares end users :(
ProcessStartInfo parameters = new ProcessStartInfo ProcessStartInfo parameters = new ProcessStartInfo
{ {
FileName = CURRENTPATH + "/PatchData/utilities/xdelta/xdelta3.exe", FileName = CurrentPath + "/PatchData/utilities/xdelta/xdelta3.exe",
WorkingDirectory = CURRENTPATH + "", WorkingDirectory = CurrentPath + "",
UseShellExecute = false, UseShellExecute = false,
CreateNoWindow = true, CreateNoWindow = true,
Arguments = arguments Arguments = arguments
@ -282,7 +294,7 @@ public static class CrossPlatformOperations
{ {
FileName = "xdelta3", FileName = "xdelta3",
Arguments = arguments, Arguments = arguments,
WorkingDirectory = CURRENTPATH WorkingDirectory = CurrentPath
}; };
using Process proc = Process.Start(parameters); using Process proc = Process.Start(parameters);
@ -333,19 +345,20 @@ public static class CrossPlatformOperations
} }
/// <summary> /// <summary>
/// Figures out what the AM2RLauncher's <see cref="CURRENTPATH"/> should be.<br/> /// Figures out what the AM2RLauncher's <see cref="CurrentPath"/> should be.<br/>
/// </summary>
/// <remarks>
/// Determination is as follows: /// Determination is as follows:
/// <list type="number"> /// <list type="number">
/// <item><b>$AM2RLAUNCHERDATA</b> environment variable is read and folders are recursively generated.</item> /// <item><b>$AM2RLAUNCHERDATA</b> environment variable is read and folders are recursively generated.</item>
/// <item>The current OS is checked. For Windows, the path where the executable is located will be returned.<br/> /// <item>The current OS is checked. For Windows, the path where the executable is located will be returned.<br/>
/// For Linux, <b>$XDG_DATA_HOME/AM2RLauncher</b> will be returned. /// For Linux, <b>$XDG_DATA_HOME/AM2RLauncher</b> will be returned.
/// Should <b>$XDG_DATA_HOME</b> be empty, it will default to <b>$HOME/.local/share</b>.<br/> /// Should <b>$XDG_DATA_HOME</b> be empty, it will default to <b>$HOME/.local/share</b>.<br/>
/// For Mac, <b>HOME/Library/AM2RLauncher"</b> will be returned.</item> /// For Mac, <b>HOME/Library/Application Support/AM2RLauncher"</b> will be returned.</item>
/// <item>The path where the executable is located will be returned.</item> /// <item>The path where the executable is located will be returned.</item>
/// </list> /// </list>
/// Should any errors occur, it falls down to the next step. /// Should any errors occur, it falls down to the next step.</remarks>
/// </summary> /// <returns>The path where the AM2RLauncher can store its data.</returns>
/// <returns></returns>
private static string GenerateCurrentPath() private static string GenerateCurrentPath()
{ {
// First, we check if the user has a custom AM2RLAUNCHERDATA env var // First, we check if the user has a custom AM2RLAUNCHERDATA env var
@ -375,35 +388,26 @@ public static class CrossPlatformOperations
} }
else if (OS.IsLinux) else if (OS.IsLinux)
{ {
// 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))
{
log.Info("Using default Linux CurrentPath.");
xdgDataHome = NIXHOME + "/.local/share";
}
// Add AM2RLauncher to the end of the dataPath // Linux has the Path at XDG_DATA_HOME/AM2RLauncher
xdgDataHome += "/AM2RLauncher"; string linuxPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}/AM2RLauncher";
try try
{ {
// This will create the directories recursively if they don't exist Directory.CreateDirectory(linuxPath);
Directory.CreateDirectory(xdgDataHome); log.Info($"CurrentPath is set to {linuxPath}");
return linuxPath;
// Our env var is now set and directories exist
log.Info("CurrentPath is set to " + xdgDataHome);
return xdgDataHome;
} }
catch (Exception ex) catch (Exception ex)
{ {
log.Error($"There was an error with '{xdgDataHome}'!\n{ex.Message} {ex.StackTrace}. Falling back to defaults."); log.Error($"There was an error with '{linuxPath}'!\n{ex.Message} {ex.StackTrace}. Falling back to defaults.");
} }
} }
else if (OS.IsMac) else if (OS.IsMac)
{ {
//Mac has the Path at HOME/Library/AM2RLauncher // Cannot use SpecialFolders here, as the current .NET version returns them wrongly.
string macPath = NIXHOME + "/Library/AM2RLauncher"; // Mac has the Path at HOME/Application Support/Library/AM2RLauncher
string macPath = Home + "/Library/Application Support/AM2RLauncher";
try try
{ {
Directory.CreateDirectory(macPath); Directory.CreateDirectory(macPath);
@ -421,6 +425,4 @@ public static class CrossPlatformOperations
log.Info("Something went wrong, falling back to the default CurrentPath."); log.Info("Something went wrong, falling back to the default CurrentPath.");
return Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); return Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
} }
} }

@ -296,15 +296,15 @@ public static class Profile
log.Info("Attempting to delete profile " + profile.Name + "..."); log.Info("Attempting to delete profile " + profile.Name + "...");
// Delete folder in Mods // Delete folder in Mods
if (Directory.Exists(CrossPlatformOperations.CURRENTPATH + profile.DataPath)) if (Directory.Exists(CrossPlatformOperations.CurrentPath + profile.DataPath))
HelperMethods.DeleteDirectory(CrossPlatformOperations.CURRENTPATH + profile.DataPath); HelperMethods.DeleteDirectory(CrossPlatformOperations.CurrentPath + profile.DataPath);
// Delete the zip file in Mods // Delete the zip file in Mods
if (File.Exists(CrossPlatformOperations.CURRENTPATH + profile.DataPath + ".zip")) if (File.Exists(CrossPlatformOperations.CurrentPath + profile.DataPath + ".zip"))
{ {
// For some reason, it was set at read only, so we undo that here // For some reason, it was set at read only, so we undo that here
File.SetAttributes(CrossPlatformOperations.CURRENTPATH + profile.DataPath + ".zip", FileAttributes.Normal); File.SetAttributes(CrossPlatformOperations.CurrentPath + profile.DataPath + ".zip", FileAttributes.Normal);
File.Delete(CrossPlatformOperations.CURRENTPATH + profile.DataPath + ".zip"); File.Delete(CrossPlatformOperations.CurrentPath + profile.DataPath + ".zip");
} }
// Delete folder in Profiles // Delete folder in Profiles
@ -368,7 +368,7 @@ public static class Profile
log.Info("Profile folder created and AM2R_11.zip extracted."); log.Info("Profile folder created and AM2R_11.zip extracted.");
// Set local dataPath for installation files // Set local dataPath for installation files
var dataPath = CrossPlatformOperations.CURRENTPATH + profile.DataPath; var dataPath = CrossPlatformOperations.CurrentPath + profile.DataPath;
string datawin = null, exe = null; string datawin = null, exe = null;
@ -555,8 +555,8 @@ public static class Profile
// Create working dir after some cleanup // Create working dir after some cleanup
string apktoolPath = Core.PatchDataPath + "/utilities/android/apktool.jar", string apktoolPath = Core.PatchDataPath + "/utilities/android/apktool.jar",
uberPath = Core.PatchDataPath + "/utilities/android/uber-apk-signer.jar", uberPath = Core.PatchDataPath + "/utilities/android/uber-apk-signer.jar",
tempDir = new DirectoryInfo(CrossPlatformOperations.CURRENTPATH + "/temp").FullName, tempDir = new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/temp").FullName,
dataPath = CrossPlatformOperations.CURRENTPATH + profile.DataPath; dataPath = CrossPlatformOperations.CurrentPath + profile.DataPath;
if (Directory.Exists(tempDir)) if (Directory.Exists(tempDir))
Directory.Delete(tempDir, true); Directory.Delete(tempDir, true);
Directory.CreateDirectory(tempDir); Directory.CreateDirectory(tempDir);
@ -615,14 +615,14 @@ public static class Profile
CrossPlatformOperations.RunJavaJar("\"" + uberPath + "\" -a \"" + profile.Name + ".apk\"", tempDir); CrossPlatformOperations.RunJavaJar("\"" + uberPath + "\" -a \"" + profile.Name + ".apk\"", tempDir);
// Extra file cleanup // Extra file cleanup
File.Copy(tempDir + "/" + profile.Name + "-aligned-debugSigned.apk", CrossPlatformOperations.CURRENTPATH + "/" + profile.Name + ".apk", true); File.Copy(tempDir + "/" + profile.Name + "-aligned-debugSigned.apk", CrossPlatformOperations.CurrentPath + "/" + profile.Name + ".apk", true);
log.Info(profile.Name + ".apk signed and moved to " + CrossPlatformOperations.CURRENTPATH + "/" + profile.Name + ".apk."); log.Info(profile.Name + ".apk signed and moved to " + CrossPlatformOperations.CurrentPath + "/" + profile.Name + ".apk.");
HelperMethods.DeleteDirectory(tempDir); HelperMethods.DeleteDirectory(tempDir);
// Done // Done
progress.Report(100); progress.Report(100);
log.Info("Successfully created Android APK for profile " + profile.Name + "."); log.Info("Successfully created Android APK for profile " + profile.Name + ".");
CrossPlatformOperations.OpenFolderAndSelectFile(CrossPlatformOperations.CURRENTPATH + "/" + profile.Name + ".apk"); CrossPlatformOperations.OpenFolderAndSelectFile(CrossPlatformOperations.CurrentPath + "/" + profile.Name + ".apk");
} }
/// <summary> /// <summary>
@ -632,7 +632,7 @@ public static class Profile
{ {
// These are used on both windows and linux for game logging // These are used on both windows and linux for game logging
string savePath = OS.IsWindows ? profile.SaveLocation.Replace("%localappdata%", Environment.GetEnvironmentVariable("LOCALAPPDATA")) string savePath = OS.IsWindows ? profile.SaveLocation.Replace("%localappdata%", Environment.GetEnvironmentVariable("LOCALAPPDATA"))
: profile.SaveLocation.Replace("~", CrossPlatformOperations.NIXHOME); : profile.SaveLocation.Replace("~", CrossPlatformOperations.Home);
DirectoryInfo logDir = new DirectoryInfo(savePath + "/logs"); DirectoryInfo logDir = new DirectoryInfo(savePath + "/logs");
string date = String.Join("-", DateTime.Now.ToString().Split(Path.GetInvalidFileNameChars(), StringSplitOptions.RemoveEmptyEntries)); string date = String.Join("-", DateTime.Now.ToString().Split(Path.GetInvalidFileNameChars(), StringSplitOptions.RemoveEmptyEntries));

Loading…
Cancel
Save