logger + url encoded path fix (for installing packages only rn)

This commit is contained in:
ZacAttack
2024-09-23 05:47:17 -07:00
parent 61ec8ad166
commit 8e50064e44
6 changed files with 86 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using WinDurango.UI.Utils;
namespace WinDurango.UI
{
@@ -10,6 +11,7 @@ namespace WinDurango.UI
{
private static readonly FileVersionInfo Fvi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
public static readonly string DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WinDurango");
public static readonly Logger logger = new Logger();
public static readonly uint Major = (uint)Fvi.ProductMajorPart;
public static readonly uint Minor = (uint)Fvi.ProductMinorPart;
public static readonly uint Patch = (uint)Fvi.ProductBuildPart;
@@ -34,6 +36,7 @@ namespace WinDurango.UI
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
logger.WriteDebug("Showing MainWindow");
MainWindow.Activate();
}
}

View File

@@ -30,7 +30,7 @@ namespace WinDurango.UI.Settings
if (!File.Exists(filePath))
{
Debug.WriteLine("Could not get the list of installed packages!");
App.logger.WriteDebug("Could not get the list of installed packages!");
using StreamWriter writer = File.CreateText(filePath);
writer.WriteLine("{}");
}
@@ -44,7 +44,7 @@ namespace WinDurango.UI.Settings
}
else
{
Debug.WriteLine($"Couldn't uninstall {pkg.Id.FamilyName} as it was not found in the package list.");
App.logger.WriteDebug($"Couldn't uninstall {pkg.Id.FamilyName} as it was not found in the package list.");
return;
}
@@ -64,7 +64,7 @@ namespace WinDurango.UI.Settings
if (!File.Exists(filePath))
{
Debug.WriteLine("Could not get the list of installed packages!");
App.logger.WriteDebug("Could not get the list of installed packages!");
using StreamWriter writer = File.CreateText(filePath);
writer.WriteLine("{}");
}
@@ -140,7 +140,7 @@ namespace WinDurango.UI.Settings
if (!File.Exists(filePath))
{
Debug.WriteLine("Could not get the list of installed packages!");
App.logger.WriteDebug("Could not get the list of installed packages!");
using StreamWriter writer = File.CreateText(filePath);
writer.WriteLine("{}");
}

View File

@@ -24,7 +24,7 @@ public class WdSettingsData
public class WdSettings
{
private readonly string _settingsFile = Path.Combine(App.DataDir, "Settings.json");
private readonly string _settingsFile = Path.Combine(App.DataDir, "settings.json");
public WdSettingsData Settings { get; private set; }
public WdSettings()
@@ -46,7 +46,7 @@ public class WdSettings
{
BackupSettings();
GenerateSettings();
Debug.WriteLine($"Settings were reset due to the settings file version being too new. ({loadedSettings.SaveVersion})");
App.logger.WriteInformation($"Settings were reset due to the settings file version being too new. ({loadedSettings.SaveVersion})");
}
loadedSettings = JsonSerializer.Deserialize<WdSettingsData>(json);
Settings = loadedSettings;
@@ -54,23 +54,27 @@ public class WdSettings
}
catch (Exception ex)
{
Debug.WriteLine("Error loading settings: " + ex.Message);
App.logger.WriteError("Error loading settings: " + ex.Message);
GenerateSettings();
}
}
else
{
App.logger.WriteWarning($"Settings file doesn't exist");
GenerateSettings();
}
}
private void BackupSettings()
{
File.Move(_settingsFile, _settingsFile + ".old_" + ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds().ToString());
string settingsBackup = _settingsFile + ".old_" + ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds().ToString();
App.logger.WriteInformation($"Backing up settings.json to {settingsBackup}");
File.Move(_settingsFile, settingsBackup);
}
private async void GenerateSettings()
{
App.logger.WriteInformation($"Generating settings file...");
Settings = GetDefaults();
await SaveSettings();
}
@@ -86,6 +90,7 @@ public class WdSettings
{
try
{
App.logger.WriteInformation($"Saving settings...");
Settings.SaveVersion = App.VerPacked;
JsonSerializerOptions options = new() { WriteIndented = true };
string json = JsonSerializer.Serialize(Settings, options);
@@ -94,7 +99,7 @@ public class WdSettings
}
catch (Exception ex)
{
Debug.WriteLine("Error saving settings: " + ex.Message);
App.logger.WriteError("Error saving settings: " + ex.Message);
}
}
@@ -111,12 +116,12 @@ public class WdSettings
}
catch (Exception ex)
{
Debug.WriteLine($"Error setting {setting}: " + ex.Message);
App.logger.WriteWarning($"Error setting {setting}: " + ex.Message);
}
}
else
{
Debug.WriteLine($"Property {setting} does not exist or is read only.");
App.logger.WriteError($"Setting {setting} does not exist... this shouldn't happen.");
}
}
}

50
Utils/Logger.cs Normal file
View File

@@ -0,0 +1,50 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace WinDurango.UI.Utils
{
public class Logger
{
private static readonly string logDir = Path.Combine(App.DataDir, "logs");
private static readonly string logFile = Path.Combine(logDir, $"WinDurangoUI_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.log");
private static readonly object @lock = new object();
private static readonly Logger instance = new Logger();
static Logger()
{
if (!Directory.Exists(logDir))
Directory.CreateDirectory(logDir);
}
public static Logger Instance => instance;
public void WriteDebug(string str) => WriteLog("DEBUG", str);
public void WriteError(string str) => WriteLog("ERROR", str);
public void WriteWarning(string str) => WriteLog("WARNING", str);
public void WriteInformation(string str) => WriteLog("INFORMATION", str);
public void WriteException(Exception e, bool throwException = false)
{
WriteLog("EXCEPTION", e.ToString());
if (throwException)
throw e;
}
private void WriteLog(string level, string message)
{
string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{level}] {message}";
Debug.WriteLine(logEntry);
lock (@lock)
{
using (StreamWriter writer = new(logFile, true))
{
writer.WriteLine(logEntry);
writer.Flush();
}
}
}
}
}

View File

@@ -33,7 +33,7 @@ namespace WinDurango.UI.Utils
public static async Task InstallXPackageAsync(string dir, XvdMode mode = XvdMode.CreateSymlinks)
{
Debug.WriteLine(dir);
App.logger.WriteDebug(dir);
string mountDir = Path.Combine(dir, "Mount");
string exvdDir = Path.Combine(dir, "EmbeddedXvd");
@@ -113,22 +113,26 @@ namespace WinDurango.UI.Utils
public static async Task<string?> InstallPackageAsync(Uri appxManifestUri)
{
string manifestPath = Uri.UnescapeDataString(appxManifestUri.AbsolutePath);
// TODO: strip UI
if (!File.Exists(appxManifestUri.AbsolutePath))
if (!File.Exists(manifestPath))
{
await new NoticeDialog($"{appxManifestUri.AbsolutePath} was not found.", "Error").Show();
await new NoticeDialog($"{manifestPath} was not found.", "Error").Show();
return null;
}
App.logger.WriteInformation($"Installing package \"{manifestPath}\"...");
var status = new ProgressDialog($"Installing package...", "Installing", false);
_ = App.MainWindow.DispatcherQueue.TryEnqueue(async () => await status.ShowAsync());
PackageManager pm = new();
try
{
App.logger.WriteInformation($"Reading manifest...");
status.Text = "Reading manifest...";
string manifest;
await using (var stream = File.OpenRead(appxManifestUri.AbsolutePath))
await using (var stream = File.OpenRead(manifestPath))
{
var reader = new StreamReader(stream);
manifest = await reader.ReadToEndAsync();
@@ -156,6 +160,7 @@ namespace WinDurango.UI.Utils
status.Progress = 40.0;
status.Text = $"Installing {pkgName}...";
App.logger.WriteInformation($"Registering...");
var deployment = await pm.RegisterPackageAsync(appxManifestUri, null, DeploymentOptions.DevelopmentMode);
status.Progress = 60.0;
@@ -171,6 +176,7 @@ namespace WinDurango.UI.Utils
status.Progress = 100.0;
status.Hide();
App.logger.WriteInformation($"{recentPkg.Id.Name} was installed.");
await new NoticeDialog($"{recentPkg.Id.Name} was installed! :)").Show();
return recentPkg.Id.FamilyName;
}
@@ -178,6 +184,8 @@ namespace WinDurango.UI.Utils
{
// we're fucked :(
status.Hide();
App.logger.WriteError($"{appxManifestUri} failed to install");
App.logger.WriteException(e);
await new NoticeDialog($"{appxManifestUri} failed to install: {e.Message}", "Error").Show();
return null;
}
@@ -185,6 +193,7 @@ namespace WinDurango.UI.Utils
public static async Task RemovePackage(Package package)
{
App.logger.WriteError($"Uninstalling {package.DisplayName}...");
var status = new ProgressDialog($"Uninstalling {package.DisplayName}...", "Uninstalling", false);
_ = App.MainWindow.DispatcherQueue.TryEnqueue(async () => await status.ShowAsync());
PackageManager pm = new();
@@ -196,12 +205,15 @@ namespace WinDurango.UI.Utils
InstalledPackages.RemoveInstalledPackage(package);
status.Progress = 100.0;
status.Hide();
App.logger.WriteError($"{package.DisplayName} was uninstalled.");
await new NoticeDialog($"{package.DisplayName} was uninstalled.").Show();
App.MainWindow.ReloadAppList();
}
catch (Exception ex)
{
status.Hide();
App.logger.WriteError($"{package.DisplayName} failed to uninstall");
App.logger.WriteException(ex);
await new NoticeDialog($"{package.DisplayName} failed to uninstall: {ex.Message}", "Error!").Show();
}
return;

View File

@@ -14,7 +14,7 @@
<DisableXbfLineInfo>False</DisableXbfLineInfo>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
<Version>0.0.1</Version>
<Version>0.0.2</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
</PropertyGroup>