Skip to content

Commit

Permalink
Allow config objects for complex types
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Aug 12, 2024
1 parent 55e8545 commit 54a7824
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 112 deletions.
24 changes: 24 additions & 0 deletions Winch/Components/WinchTerminal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommandTerminal;
using UnityEngine;
using Winch.Config;

namespace Winch.Components
{
[RequireComponent(typeof(Terminal))]
internal class WinchTerminal : MonoBehaviour
{
public bool Enabled => WinchConfig.GetProperty("EnableDeveloperConsole", false);

private Terminal terminal;

public void Start()
{
terminal = GetComponent<Terminal>();
}

public void Update()
{
terminal.enabled = Enabled;
}
}
}
38 changes: 23 additions & 15 deletions Winch/Core/WinchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ public static class WinchCore

public static string WinchInstallLocation => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

internal static string Name => WinchCore.WinchModConfig["Name"].ToString();

internal static string Author => WinchCore.WinchModConfig["Author"].ToString();

internal static string GUID => WinchCore.WinchModConfig["ModGUID"].ToString();

internal static string Version => WinchCore.WinchModConfig["Version"].ToString();

public static void Main()
{
try
{
string metaPath = Path.Combine(WinchInstallLocation, "mod_meta.json");
if (!File.Exists(metaPath))
{
throw new FileNotFoundException($"Missing mod_meta.json file for Winch at {metaPath}. Reinstall the mod.");
}
try
{
string metaPath = Path.Combine(WinchInstallLocation, "mod_meta.json");
if (!File.Exists(metaPath))
{
throw new FileNotFoundException($"Missing mod_meta.json file for Winch at {metaPath}. Reinstall the mod.");
}

string metaText = File.ReadAllText(metaPath);
WinchModConfig = JsonConvert.DeserializeObject<Dictionary<string, object>>(metaText)
?? throw new InvalidOperationException($"Unable to parse mod_meta.json file at {metaPath}. Reinstall the mod.");
}
catch (Exception e)
{
Log.Error(e);
}
string metaText = File.ReadAllText(metaPath);
WinchModConfig = JsonConvert.DeserializeObject<Dictionary<string, object>>(metaText)
?? throw new InvalidOperationException($"Unable to parse mod_meta.json file at {metaPath}. Reinstall the mod.");
}
catch (Exception e)
{
Log.Error(e);
}

string version = VersionUtil.GetVersion();
Log.Info($"Winch {version} booting up...");
Expand Down
121 changes: 62 additions & 59 deletions Winch/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,62 @@

namespace Winch.Logging
{
public class Logger
public class Logger
{
private LogFile? _log;
private LogFile? _latestLog;

private bool _writeLogsToFile;
private bool _writeLogsToConsole;
private LogLevel? _minLogLevel;
private bool _writeLogsToFile = true;
private bool _writeLogsToConsole = false;

private LogSocket? _logSocket;
private LogSocket? _logSocket;

public string LogConsoleExe => Path.Combine(WinchCore.WinchInstallLocation, "WinchConsole.exe");
public LogLevel minLogLevel => EnumUtil.Parse<LogLevel>(WinchConfig.GetProperty("LogLevel", "DEBUG"), true, LogLevel.DEBUG);
public string LogConsoleExe => Path.Combine(WinchCore.WinchInstallLocation, "WinchConsole.exe");

public Logger()
{
_writeLogsToFile = WinchConfig.GetProperty("WriteLogsToFile", true);
if (_writeLogsToFile)
{
_minLogLevel = EnumUtil.Parse<LogLevel>(WinchConfig.GetProperty("LogLevel", "DEBUG"), true, LogLevel.DEBUG);
_log = new LogFile();
_latestLog = new LogFile("latest.log");
CleanupLogs();
}

_writeLogsToConsole = WinchConfig.GetProperty("WriteLogsToConsole", true);

if (_writeLogsToConsole)
{
// Find an avialable port for the logs
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();

// Console exe will get the port from the WinchConfig file
WinchConfig.SetProperty("LogPort", $"{port}");

Info($"Writing logs to port {port}");

try
{
Info($"Starting console at path {LogConsoleExe}");
Process.Start(LogConsoleExe);

_logSocket = new LogSocket(this, port);
}
catch (Exception e)
{
Error($"Could not start console : {e}");
}
}

Info($"Writing logs to file: {_writeLogsToFile}. Writing logs to console: {_writeLogsToConsole}.");
{
_log = new LogFile();
_latestLog = new LogFile("latest.log");
CleanupLogs();
}

_writeLogsToConsole = WinchConfig.GetProperty("WriteLogsToConsole", true);

if (_writeLogsToConsole)
{
// Find an avialable port for the logs
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();

// Console exe will get the port from the WinchConfig file
WinchConfig.SetProperty("LogPort", $"{port}");

Info($"Writing logs to port {port}");

try
{
Info($"Starting console at path {LogConsoleExe}");
Process.Start(LogConsoleExe);

_logSocket = new LogSocket(this, port);
}
catch (Exception e)
{
Error($"Could not start console : {e}");
}
}

Info($"Writing logs to file: {_writeLogsToFile}. Writing logs to console: {_writeLogsToConsole}.");
}

private static void SetMinLogLevel(LogLevel level)
{
}

private static void CleanupLogs()
Expand Down Expand Up @@ -96,25 +99,25 @@ private void Log(LogLevel level, string message)

private void Log(LogLevel level, string message, string source)
{
if (level < _minLogLevel)
return;

if (_writeLogsToConsole)
{
_logSocket?.WriteToSocket(new LogMessage()
{
Level = level,
Message = message,
Source = source
});
}
if (level < minLogLevel)
return;

if (_writeLogsToConsole)
{
_logSocket?.WriteToSocket(new LogMessage()
{
Level = level,
Message = message,
Source = source
});
}

if (_writeLogsToFile)
{
string logMessage = $"[{GetLogTimestamp()}] [{source}] [{level}] : {message}";
_log?.Write(logMessage);
_latestLog?.Write(logMessage);
}
{
string logMessage = $"[{GetLogTimestamp()}] [{source}] [{level}] : {message}";
_log?.Write(logMessage);
_latestLog?.Write(logMessage);
}
}

private string GetLogTimestamp()
Expand Down
4 changes: 3 additions & 1 deletion Winch/Patches/SKUPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using CommandTerminal;
using UnityEngine;
using Winch.Config;
using Winch.Components;

namespace Winch.Patches
{
Expand All @@ -17,13 +18,14 @@ public static void Prefix(this SKUSpecificDisabler __instance)
__instance.destroyIfUnavailable = false; // Disable destroying

// Enable terminal
if (__instance.TryGetComponent<Terminal>(out Terminal terminal) && WinchConfig.GetProperty("EnableDeveloperConsole", false))
if (__instance.TryGetComponent<Terminal>(out Terminal terminal))
{
terminal.ConsoleFont = Font.CreateDynamicFontFromOSFont("Courier New", 16);
__instance.supportedBuilds = BuildEnvironment.ALL;
__instance.supportedPlatforms = Platform.ALL;
__instance.unsupportedOnSteamDeck = false;
__instance.allowInConventionBuilds = true;
terminal.gameObject.AddComponent<WinchTerminal>();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Winch/Serialization/DredgeTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UnityEngine;
using UnityEngine.Localization;
using Winch.Core;
using Winch.Util;

// ReSharper disable HeapView.BoxingAllocation
// ReSharper disable HeapView.PossibleBoxingAllocation
Expand Down Expand Up @@ -113,7 +114,7 @@ protected static LocalizedString CreateLocalizedString(string key, string value)
{
return cached;
}
var localizedString = new LocalizedString(key, value);
var localizedString = LocalizationUtil.CreateReference(key, value);
StringDefinitionCache.Add(keyValueTuple, localizedString);
return localizedString;
}
Expand Down
8 changes: 4 additions & 4 deletions Winch/Util/LocalizationUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public static class LocalizationUtil

private static Dictionary<string, Dictionary<string, string>> StringDatabase = new Dictionary<string, Dictionary<string, string>>();
private static Dictionary<string, Dictionary<string, StringTable>> StringTableDict = new Dictionary<string, Dictionary<string, StringTable>>();
public static LocalizedString Empty => new LocalizedString(string.Empty, string.Empty);
public static LocalizedString Unknown => new LocalizedString("Strings", "label.unknown");
public static LocalizedString CreateReference(string table, string entry) => new LocalizedString(table, entry);

internal static List<Locale> AddedLocales = new List<Locale>();

public static LocalizedString CreateReference(string table, string entry) => new LocalizedString(table, entry);
public static LocalizedString Empty => LocalizationUtil.CreateReference(string.Empty, string.Empty);
public static LocalizedString Unknown => LocalizationUtil.CreateReference("Strings", "label.unknown");

public static void InstallLocale(SystemLanguage language) => InstallLocale(Locale.CreateLocale(language));

public static void InstallLocale(Locale locale)
Expand Down
2 changes: 1 addition & 1 deletion Winch/Util/VersionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class VersionUtil

internal static string GetVersion()
{
return WinchCore.WinchModConfig["Version"].ToString();
return WinchCore.Version;
}

internal static bool ValidateVersion(string version)
Expand Down
Loading

0 comments on commit 54a7824

Please sign in to comment.