Skip to content

Commit

Permalink
move Log to new Singletons system
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Aug 23, 2024
1 parent ddba935 commit 6e4c649
Show file tree
Hide file tree
Showing 20 changed files with 97 additions and 65 deletions.
2 changes: 0 additions & 2 deletions JiayiLauncher/Appearance/CssBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace JiayiLauncher.Appearance;



public class CssProperty
{
public string Property { get; set; }
Expand Down
6 changes: 4 additions & 2 deletions JiayiLauncher/Features/Mods/ModConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ModConfigManager
public readonly string ConfigPath;
//private readonly string _configExtension; // maybe in the future

private readonly Log _log = Singletons.Get<Log>();

public ModConfigManager(Mod mod)
{
var modDataFolder = Path.Combine(PackageData.GetGameDataPath(), "RoamingState", mod.DataFolderName);
Expand All @@ -35,7 +37,7 @@ public async Task AddConfig(IBrowserFile file)
await using var stream = File.Create(filePath);
await file.OpenReadStream().CopyToAsync(stream);

Log.Write(this, $"Added config {fileName}");
_log.Write(this, $"Added config {fileName}");
}

public void RemoveConfig(string path)
Expand All @@ -47,7 +49,7 @@ public void RemoveConfig(string path)

File.Delete(configPath);

Log.Write(this, $"Removed config {configName}");
_log.Write(this, $"Removed config {configName}");
}

public List<string> GetConfigs()
Expand Down
8 changes: 5 additions & 3 deletions JiayiLauncher/Features/Profiles/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Profile
{
public string Name { get; set; }
public string Path { get; set; }

private static readonly Log _log = Singletons.Get<Log>();

public Profile(string name, string path)
{
Expand All @@ -25,7 +27,7 @@ public static async Task<Profile> Create(string name, ProfileCollection collecti
await File.WriteAllTextAsync(System.IO.Path.Combine(fullPath, "README.txt"),
"This folder contains the game data for this profile. Don't mess with it unless you know what you're doing.");

Log.Write("Profile.Create", $"Created profile {name} at {fullPath}");
_log.Write("Profile.Create", $"Created profile {name} at {fullPath}");

return new Profile(name, fullPath);
}
Expand Down Expand Up @@ -55,7 +57,7 @@ public ProfileInfo GetInfo()
public async Task Apply()
{
await PackageData.ReplaceGameData(Path);
Log.Write("Profile.Apply", $"Applied profile {Name}");
_log.Write("Profile.Apply", $"Applied profile {Name}");
}

public void Delete()
Expand All @@ -64,7 +66,7 @@ public void Delete()
Directory.Delete(Path, true);

ProfileCollection.Current!.Profiles.Remove(this);
Log.Write("Profile.Delete", $"Deleted profile {Name}");
_log.Write("Profile.Delete", $"Deleted profile {Name}");
}

public bool IsValid()
Expand Down
4 changes: 3 additions & 1 deletion JiayiLauncher/Features/Profiles/ProfileCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static void Load(string basePath)
}

Current = collection;
Log.Write("ProfileCollection", $"Loaded {profiles.Length} profiles from {basePath}");

var log = Singletons.Get<Log>();
log.Write("ProfileCollection", $"Loaded {profiles.Length} profiles from {basePath}");
}

public void Add(Profile profile)
Expand Down
21 changes: 11 additions & 10 deletions JiayiLauncher/Features/Shaders/ShaderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class ShaderManager
public static List<string> AvailableShaders => Shaders.Where(x => x != AppliedShader).ToList();

private static string[] _blockedFolders = ["iOS", "Android"];
private static readonly Log _log = Singletons.Get<Log>();

public static async Task BackupVanillaShaders()
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public static async Task BackupVanillaShaders()
File.Copy(file, Path.Combine(backupPath, fileName), true);
}

Log.Write(nameof(ShaderManager), $"Backed up vanilla shaders for version {await PackageData.GetVersion()}");
_log.Write(nameof(ShaderManager), $"Backed up vanilla shaders for version {await PackageData.GetVersion()}");
}

public static async Task DeleteBackupShaders()
Expand All @@ -57,7 +58,7 @@ public static async Task DeleteBackupShaders()
var path = Path.Combine(JiayiSettings.Instance.ShadersPath, "Vanilla", version);
if (Directory.Exists(path)) Directory.Delete(path, true);

Log.Write(nameof(ShaderManager), $"Deleted backup shaders for version {version}");
_log.Write(nameof(ShaderManager), $"Deleted backup shaders for version {version}");
}

public static async Task RestoreVanillaShaders()
Expand All @@ -79,7 +80,7 @@ public static async Task RestoreVanillaShaders()
File.Copy(file, Path.Combine(path, fileName), true);
}

Log.Write(nameof(ShaderManager), $"Restored vanilla shaders for version {version}");
_log.Write(nameof(ShaderManager), $"Restored vanilla shaders for version {version}");
}

public static void UpdateShaders()
Expand All @@ -105,7 +106,7 @@ public static void UpdateShaders()
var applied = Directory.GetDirectories(Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied"));
AppliedShader = Path.GetFileName(applied.FirstOrDefault() ?? string.Empty);

Log.Write(nameof(ShaderManager), AppliedShader == string.Empty
_log.Write(nameof(ShaderManager), AppliedShader == string.Empty
? $"Updated shaders list. Found {Shaders.Count} shaders. {AppliedShader} is currently applied."
: $"Updated shaders list. Found {Shaders.Count} shaders. No shader is currently applied.");
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public static async Task AddShader(IBrowserFile file)
Directory.Move(materialsFolder, Path.Combine(JiayiSettings.Instance.ShadersPath, Path.GetFileNameWithoutExtension(file.Name)));
Directory.Delete(tempPath, true);

Log.Write(nameof(ShaderManager), $"Added shader {file.Name}");
_log.Write(nameof(ShaderManager), $"Added shader {file.Name}");
UpdateShaders();
}

Expand Down Expand Up @@ -170,7 +171,7 @@ public static void RenameShader(string shader, string newName) {

Directory.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, newName));

Log.Write(nameof(ShaderManager), $"Renamed shader {shader} to {newName}");
_log.Write(nameof(ShaderManager), $"Renamed shader {shader} to {newName}");
UpdateShaders();
}

Expand All @@ -183,7 +184,7 @@ public static void EnableShader(string shader)

Directory.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, "Applied", shader));

Log.Write(nameof(ShaderManager), $"Enabled shader {shader}");
_log.Write(nameof(ShaderManager), $"Enabled shader {shader}");
UpdateShaders();
}

Expand All @@ -194,7 +195,7 @@ public static void DisableShader(string shader)

Directory.Move(path, Path.Combine(JiayiSettings.Instance.ShadersPath, shader));

Log.Write(nameof(ShaderManager), $"Disabled shader {shader}");
_log.Write(nameof(ShaderManager), $"Disabled shader {shader}");
UpdateShaders();
}

Expand All @@ -205,7 +206,7 @@ public static void DeleteShader(string shader)

Directory.Delete(path, true);

Log.Write(nameof(ShaderManager), $"Deleted shader {shader}");
_log.Write(nameof(ShaderManager), $"Deleted shader {shader}");
UpdateShaders();
}

Expand Down Expand Up @@ -243,7 +244,7 @@ public static async Task ApplyShader()
File.Copy(shader, Path.Combine(shaderPath, fileName));
}

Log.Write(nameof(ShaderManager), $"Applied shader {AppliedShader}");
_log.Write(nameof(ShaderManager), $"Applied shader {AppliedShader}");
}

public static List<string> GetMaterialDiff(string shader)
Expand Down
10 changes: 6 additions & 4 deletions JiayiLauncher/Features/Stats/JiayiStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public static void Save()

public static void Load()
{
var log = Singletons.Get<Log>();

if (!File.Exists(_statsPath))
{
Instance = new JiayiStats();
Save();
Log.Write(nameof(JiayiStats), "Created new stats file.");
log.Write(nameof(JiayiStats), "Created new stats file.");
return;
}

Expand All @@ -73,19 +75,19 @@ public static void Load()
{
Instance = new JiayiStats();
Save();
Log.Write(nameof(JiayiStats), "Stats file was corrupted or invalid. Created new stats file.");
log.Write(nameof(JiayiStats), "Stats file was corrupted or invalid. Created new stats file.");
return;
}

Instance = stats;
Log.Write(nameof(JiayiStats), "Loaded stats.");
log.Write(nameof(JiayiStats), "Loaded stats.");
}
catch (Exception e)
{
stream.Close();
Instance = new JiayiStats();
Save();
Log.Write(nameof(JiayiStats), $"Stats file was corrupted or invalid. Created new stats file. Error: {e}");
log.Write(nameof(JiayiStats), $"Stats file was corrupted or invalid. Created new stats file. Error: {e}");
}
}
}
9 changes: 5 additions & 4 deletions JiayiLauncher/Features/Versions/VersionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static class VersionList
private static readonly SortedDictionary<string, MinecraftVersion> _versionDict = new(new VersionComparer());
private static readonly DisplayCatalogHandler _catalog = DisplayCatalogHandler.ProductionConfig();
private static readonly string _versionsPath = Path.Combine(JiayiSettings.Instance!.VersionsPath, "versions.json");
private static readonly Log _log = Singletons.Get<Log>();

private static bool _loaded;

Expand Down Expand Up @@ -67,7 +68,7 @@ public static async Task UpdateVersions(bool clear = false)

if (InternetManager.OfflineMode)
{
Log.Write(nameof(VersionList), "Offline mode enabled, skipping version list update.");
_log.Write(nameof(VersionList), "Offline mode enabled, skipping version list update.");
return;
}

Expand All @@ -89,7 +90,7 @@ public static async Task UpdateVersions(bool clear = false)
var mcVersion = new MinecraftVersion(fileName, updateId, version);
if (_versionDict.TryAdd(version, mcVersion))
{
Log.Write(nameof(VersionList), $"Found new version: {version}");
_log.Write(nameof(VersionList), $"Found new version: {version}");
var jsonOut = JsonConvert.SerializeObject(_versionDict, Formatting.Indented);
await File.WriteAllTextAsync(_versionsPath, jsonOut);
}
Expand Down Expand Up @@ -126,7 +127,7 @@ public static async Task UpdateVersions(bool clear = false)
var mcVersion = new MinecraftVersion(fileName, updateId, version);
if (_versionDict.TryAdd(version, mcVersion))
{
Log.Write(nameof(VersionList), $"Found new version: {version}");
_log.Write(nameof(VersionList), $"Found new version: {version}");
var jsonOut = JsonConvert.SerializeObject(_versionDict, Formatting.Indented);
await File.WriteAllTextAsync(_versionsPath, jsonOut);
}
Expand All @@ -143,7 +144,7 @@ public static async Task UpdateVersions(bool clear = false)
if (_versions.Count != 0) _versions.Clear();
_versions.AddRange(_versionDict.Keys);

Log.Write(nameof(VersionList), $"Updated version list. Found {_versions.Count} versions.");
_log.Write(nameof(VersionList), $"Updated version list. Found {_versions.Count} versions.");
}

public static async Task<List<string>> GetVersionList()
Expand Down
22 changes: 12 additions & 10 deletions JiayiLauncher/Features/Versions/VersionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum SwitchResult

public static int DownloadProgress { get; private set; }
public static event EventHandler? SwitchProgressChanged;

private static readonly Log _log = Singletons.Get<Log>();

public static bool VersionInstalled(string ver)
{
Expand Down Expand Up @@ -152,7 +154,7 @@ await Task.Run(() =>
// my favorite part of this class
public static async Task<SwitchResult> Switch(string version)
{
Log.Write(nameof(VersionManager), $"Switching to version {version}");
_log.Write(nameof(VersionManager), $"Switching to version {version}");

if (ShaderManager.AppliedShader != string.Empty)
{
Expand All @@ -166,7 +168,7 @@ public static async Task<SwitchResult> Switch(string version)

if (!WinRegistry.DeveloperModeEnabled())
{
Log.Write(nameof(VersionManager), "Developer mode is disabled, asking user to enable", Log.LogLevel.Warning);
_log.Write(nameof(VersionManager), "Developer mode is disabled, asking user to enable", Log.LogLevel.Warning);
return SwitchResult.DeveloperModeDisabled;
}

Expand All @@ -175,7 +177,7 @@ public static async Task<SwitchResult> Switch(string version)
{
if (package.InstalledPath.Contains(version))
{
Log.Write(nameof(VersionManager), "Version already installed");
_log.Write(nameof(VersionManager), "Version already installed");
return SwitchResult.Succeeded;
}

Expand All @@ -186,24 +188,24 @@ public static async Task<SwitchResult> Switch(string version)
var backupPath = Path.Combine(JiayiSettings.Instance.VersionsPath, ".backup");
if (Directory.Exists(backupPath))
{
Log.Write(nameof(VersionManager),
_log.Write(nameof(VersionManager),
"Backup found, this might mean the launcher failed to switch versions last time",
Log.LogLevel.Warning);
}
else
{
Log.Write(nameof(VersionManager), "Backing up game data");
_log.Write(nameof(VersionManager), "Backing up game data");
await PackageData.BackupGameData(backupPath);
}

Log.Write(nameof(VersionManager), "Removing old game data");
_log.Write(nameof(VersionManager), "Removing old game data");
Directory.Delete(PackageData.GetGameDataPath(), true);

await PackageData.PackageManager.RemovePackageAsync(package.Id.FullName, 0);
}
}

Log.Write(nameof(VersionManager), "Registering package");
_log.Write(nameof(VersionManager), "Registering package");

var manifest = Path.Combine(folder, "AppxManifest.xml");

Expand All @@ -214,7 +216,7 @@ public static async Task<SwitchResult> Switch(string version)

if (result.IsRegistered)
{
Log.Write(nameof(VersionManager), "Package registered");
_log.Write(nameof(VersionManager), "Package registered");

var path = Path.Combine(JiayiSettings.Instance.VersionsPath, ".backup");
if (Directory.Exists(path))
Expand All @@ -230,11 +232,11 @@ public static async Task<SwitchResult> Switch(string version)
{
if (e.ToString().Contains("sideload"))
{
Log.Write(nameof(VersionManager), "Developer mode is disabled, asking user to enable", Log.LogLevel.Warning);
_log.Write(nameof(VersionManager), "Developer mode is disabled, asking user to enable", Log.LogLevel.Warning);
return SwitchResult.DeveloperModeDisabled;
}

Log.Write(nameof(VersionManager), $"Unknown error: {e}");
_log.Write(nameof(VersionManager), $"Unknown error: {e}");
}

return SwitchResult.UnknownError;
Expand Down
6 changes: 4 additions & 2 deletions JiayiLauncher/Modals/EditProfile.razor
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
{
if (Profile == null || _textBox?.Value == null || _textBox?.Value?.Trim() == string.Empty || _textBox?.Value.Length >= 256) return;

var log = Singletons.Get<Log>();

var newName = _textBox!.Value?.Trim() ?? Profile.Name;

if (Directory.Exists(Profile.Path))
Expand All @@ -42,7 +44,7 @@
if (Directory.Exists(newPath))
{
await Modal.CloseAsync(ModalResult.Cancel());
Log.Write("Profile", $"Directory already exists: {newPath}");
log.Write("Profile", $"Directory already exists: {newPath}");
return;
}

Expand All @@ -57,7 +59,7 @@
}
catch (Exception e)
{
Log.Write("EditProfile.SaveClicked", $"Could not move directory: {e}", Log.LogLevel.Error);
log.Write("EditProfile.SaveClicked", $"Could not move directory: {e}", Log.LogLevel.Error);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion JiayiLauncher/Modals/NewMod.razor
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@
}
catch (Exception e)
{
Log.Write(this, $"GetFinalRedirect failed: {e}", Log.LogLevel.Error);
var log = Singletons.Get<Log>();
log.Write(this, $"GetFinalRedirect failed: {e}", Log.LogLevel.Error);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion JiayiLauncher/Modals/NewTheme.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

if (Directory.Exists(dirPath))
{
Log.Write(nameof(PrepareThemePublish), $"Failed to create theme (already exists): {name}", Log.LogLevel.Warning);
var log = Singletons.Get<Log>();
log.Write(nameof(PrepareThemePublish), $"Failed to create theme (already exists): {name}", Log.LogLevel.Warning);
return;
}

Expand Down
Loading

0 comments on commit 6e4c649

Please sign in to comment.