Skip to content

Commit

Permalink
Create WinchConsole program, move relevant shared files to WinchCommo…
Browse files Browse the repository at this point in the history
…n project
  • Loading branch information
xen-42 committed Dec 26, 2023
1 parent bbba7e8 commit bd71508
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 42 deletions.
12 changes: 12 additions & 0 deletions Winch.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DisasterButton", "Winch.Exa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleItems", "Winch.Examples\ExampleItems\ExampleItems.csproj", "{C112288E-E993-44F3-B7B0-FB4BD37F18EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinchConsole", "WinchConsole\WinchConsole.csproj", "{D5CFABA9-FA58-474F-A394-E197BDF38FDD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinchCommon", "WinchCommon\WinchCommon.csproj", "{EF5F69E6-FD4D-4314-B187-A89D00BF9355}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -33,6 +37,14 @@ Global
{C112288E-E993-44F3-B7B0-FB4BD37F18EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C112288E-E993-44F3-B7B0-FB4BD37F18EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C112288E-E993-44F3-B7B0-FB4BD37F18EA}.Release|Any CPU.Build.0 = Release|Any CPU
{D5CFABA9-FA58-474F-A394-E197BDF38FDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5CFABA9-FA58-474F-A394-E197BDF38FDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5CFABA9-FA58-474F-A394-E197BDF38FDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5CFABA9-FA58-474F-A394-E197BDF38FDD}.Release|Any CPU.Build.0 = Release|Any CPU
{EF5F69E6-FD4D-4314-B187-A89D00BF9355}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF5F69E6-FD4D-4314-B187-A89D00BF9355}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF5F69E6-FD4D-4314-B187-A89D00BF9355}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF5F69E6-FD4D-4314-B187-A89D00BF9355}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
23 changes: 19 additions & 4 deletions Winch/Core/WinchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEngine.Profiling.Memory.Experimental;
using Winch.Config;
using Winch.Logging;
using Winch.Util;

namespace Winch.Core
{
public class WinchCore
public class WinchCore
{
public static Logger Log = new Logger();

Expand All @@ -22,6 +20,18 @@ public class WinchCore

public static void Main()
{
Log.Info("Starting console");
try
{
Process.Start(Path.Combine(WinchInstallLocation, "WinchConsole.exe"));
}
catch (Exception e)
{
Log.Error($"Could not start console : {e}");
}

AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);

try
{
string metaPath = Path.Combine(WinchInstallLocation, "mod_meta.json");
Expand Down Expand Up @@ -67,5 +77,10 @@ public static void Main()

Log.Debug("Harmony Patching complete.");
}

public static void OnProcessExit(object sender, EventArgs e)
{
Log.Debug("DREDGE application exited");
}
}
}
6 changes: 3 additions & 3 deletions Winch/Logging/LogSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class LogSocket
private Socket _socket;
private readonly int _port;
private readonly Logger _logger;
private const string IP = "127.0.0.1";

public LogSocket(Logger logger, int port)
{
Expand All @@ -30,14 +29,15 @@ public void WriteToSocket(LogMessage logMessage)

if (!_socket.Connected)
{
var endPoint = new IPEndPoint(IPAddress.Parse(IP), _port); try
var endPoint = new IPEndPoint(IPAddress.Parse(Constants.IP), _port);
try
{
_socket?.Connect(endPoint);
}
catch (Exception ex)
{
_socket = null;

Check warning on line 39 in Winch/Logging/LogSocket.cs

View workflow job for this annotation

GitHub Actions / Build debug / Create artifacts

Cannot convert null literal to non-nullable reference type.
_logger.Error($"Could not connect to console at {IP}:{_port} - {ex}");
_logger.Error($"Could not connect to console at {Constants.IP}:{_port} - {ex}");
}
}

Expand Down
9 changes: 2 additions & 7 deletions Winch/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@

namespace Winch.Logging
{
enum LogLevel
{
UNITY = 0, DEBUG = 1, INFO = 2, WARN = 3, ERROR = 4
}

public class Logger
public class Logger
{
private LogFile? _log;
private LogFile? _latestLog;
Expand Down Expand Up @@ -73,7 +68,7 @@ private void Log(LogLevel level, string message, string source)
{
_logSocket?.WriteToSocket(new LogMessage()
{
Level = level.ToString(),
Level = level,
Message = message,
Source = source
});
Expand Down
20 changes: 5 additions & 15 deletions Winch/Winch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<None Include="mod_meta.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -49,4 +34,9 @@
<Copy SourceFiles="@(DLLs)" DestinationFolder="$(OutputPath)" />
</Target>

<ItemGroup>
<ProjectReference Include="..\WinchCommon\WinchCommon.csproj">
</ProjectReference>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Winch/mod_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"Name": "Winch",
"Author": "Hacktix",
"ModGUID": "hacktix.winch",
"Version": "0.3.0"
"Version": "0.4.0"
}
File renamed without changes.
File renamed without changes.
9 changes: 3 additions & 6 deletions Winch/Config/ModConfig.cs → WinchCommon/Config/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Winch.Core;
using System.Reflection;

namespace Winch.Config
{
public class ModConfig : JSONConfig
public class ModConfig : JSONConfig
{
private static Dictionary<string, string> DefaultConfigs = new Dictionary<string, string>();
private static Dictionary<string, ModConfig> Instances = new Dictionary<string, ModConfig>();
Expand All @@ -18,7 +15,7 @@ private static string GetDefaultConfig(string modName)
{
if(!DefaultConfigs.ContainsKey(modName))
{
WinchCore.Log.Error($"No 'DefaultConfig' attribute found in mod_meta.json for {modName}!");
//WinchCore.Log.Error($"No 'DefaultConfig' attribute found in mod_meta.json for {modName}!");
throw new KeyNotFoundException($"No 'DefaultConfig' attribute found in mod_meta.json for {modName}!");
}
return DefaultConfigs[modName];
Expand Down
16 changes: 14 additions & 2 deletions Winch/Config/WinchConfig.cs → WinchCommon/Config/WinchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class WinchConfig : JSONConfig
{
private static readonly string WinchConfigPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "WinchConfig.json");

private WinchConfig() : base(WinchConfigPath, Properties.Resources.DefaultConfig) { }
private WinchConfig() : base(WinchConfigPath, WinchCommon.Properties.Resources.DefaultConfig) { }

private static WinchConfig? _instance;
public static WinchConfig Instance
Expand All @@ -22,7 +22,19 @@ public static WinchConfig Instance

public static new T? GetProperty<T>(string key, T defaultValue)
{
return ((JSONConfig)Instance).GetProperty(key, defaultValue);
try
{
return ((JSONConfig)Instance).GetProperty(key, defaultValue);
}
catch
{
return defaultValue;
}
}

public static new void SetProperty<T>(string key, T value)
{
((JSONConfig)Instance).SetProperty(key, value);
}
}
}
12 changes: 12 additions & 0 deletions WinchCommon/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Winch;

public static class Constants
{
public const string IP = "127.0.0.1";
}
6 changes: 6 additions & 0 deletions WinchCommon/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Winch;

public enum LogLevel
{
UNITY = 0, DEBUG = 1, INFO = 2, WARN = 3, ERROR = 4
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using WinchCommon;

namespace Winch.Logging
{
Expand All @@ -8,7 +9,7 @@ public class LogMessage
public string Source { get; set; }

Check warning on line 9 in WinchCommon/Logging/LogMessage.cs

View workflow job for this annotation

GitHub Actions / Build debug / Create artifacts

Non-nullable property 'Source' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[JsonProperty("level")]
public string Level { get; set; }
public LogLevel Level { get; set; }

[JsonProperty("message")]
public string Message { get; set; }

Check warning on line 15 in WinchCommon/Logging/LogMessage.cs

View workflow job for this annotation

GitHub Actions / Build debug / Create artifacts

Non-nullable property 'Message' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
33 changes: 33 additions & 0 deletions WinchCommon/WinchCommon.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
</ItemGroup>

</Project>
Loading

0 comments on commit bd71508

Please sign in to comment.