Skip to content

Commit

Permalink
Merge branch 'dev' into tai/SettingsChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
TAImatem committed Jul 25, 2020
2 parents 6180bf2 + 84f9513 commit 867dd84
Show file tree
Hide file tree
Showing 24 changed files with 304 additions and 201 deletions.
2 changes: 1 addition & 1 deletion OWML.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
public class Constants
{
public const string QuitKeyPhrase = "{RageAgainstTheDyingOfTheLight}";
public const string ConsolePortArgument = "consolePort";
public const string OwmlConfigFileName = "OWML.Config.json";
public const string OwmlDefaultConfigFileName = "OWML.DefaultConfig.json";
public const string OwmlManifestFileName = "OWML.Manifest.json";
public const string LocalAddress = "127.0.0.1";
public const string ModConfigFileName = "config.json";
public const string ModDefaultConfigFileName = "default-config.json";
public const string ModManifestFileName = "manifest.json";
Expand Down
11 changes: 8 additions & 3 deletions OWML.Common/IModConsole.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
namespace OWML.Common
using System;

namespace OWML.Common
{
public interface IModConsole
{
void WriteLine(string s);
void WriteLine(params object[] s);
[Obsolete("Use WriteLine(string) or WriteLine(string, MessageType) instead.")]
void WriteLine(params object[] objects);

void WriteLine(string line);
void WriteLine(string line, MessageType type);
}
}
2 changes: 1 addition & 1 deletion OWML.Common/IOwmlConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public interface IOwmlConfig
string DataPath { get; }
string OWMLPath { get; set; }
string ModsPath { get; }
string OutputFilePath { get; }
bool Verbose { get; set; }
bool BlockInput { get; set; }
int SocketPort { get; set; }
}
}
12 changes: 12 additions & 0 deletions OWML.Common/MessageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace OWML.Common
{
public enum MessageType
{
Message = 0,
Error = 1,
Warning = 2,
Info = 3,
Success = 4,
Quit = 5
}
}
1 change: 1 addition & 0 deletions OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Menus\IModNumberInput.cs" />
<Compile Include="Menus\IModTextInput.cs" />
<Compile Include="Menus\IModToggleInput.cs" />
<Compile Include="MessageType.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
39 changes: 11 additions & 28 deletions OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@ public class App
private readonly IModManifest _owmlManifest;
private readonly IModConsole _writer;
private readonly IModFinder _modFinder;
private readonly OutputListener _listener;
private readonly PathFinder _pathFinder;
private readonly OWPatcher _owPatcher;
private readonly VRPatcher _vrPatcher;

public App(IOwmlConfig owmlConfig, IModManifest owmlManifest, IModConsole writer, IModFinder modFinder,
OutputListener listener, PathFinder pathFinder, OWPatcher owPatcher, VRPatcher vrPatcher)
PathFinder pathFinder, OWPatcher owPatcher, VRPatcher vrPatcher)
{
_owmlConfig = owmlConfig;
_owmlManifest = owmlManifest;
_writer = writer;
_modFinder = modFinder;
_listener = listener;
_pathFinder = pathFinder;
_owPatcher = owPatcher;
_vrPatcher = vrPatcher;
Expand All @@ -44,12 +42,6 @@ public void Run(string[] args)

CreateLogsDirectory();

var hasPortArgument = CommandLineArguments.HasArgument(Constants.ConsolePortArgument);
if (!hasPortArgument)
{
ListenForOutput();
}

var mods = _modFinder.GetMods();

ShowModList(mods);
Expand All @@ -58,6 +50,7 @@ public void Run(string[] args)

StartGame(args);

var hasPortArgument = CommandLineArguments.HasArgument(Constants.ConsolePortArgument);
if (hasPortArgument)
{
ExitConsole();
Expand Down Expand Up @@ -116,25 +109,6 @@ private bool IsMadeForSameOwmlMajorVersion(IModManifest manifest)
owmlVersionSplit[1] == modVersionSplit[1];
}

private void ListenForOutput()
{
_listener.OnOutput += OnOutput;
_listener.Start();
}

private void OnOutput(string s)
{
var lines = s.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
_writer.WriteLine(line);
if (line.EndsWith(Constants.QuitKeyPhrase))
{
ExitConsole();
}
}
}

private bool HasVrMod(List<IModData> mods)
{
var vrMod = mods.FirstOrDefault(x => x.RequireVR && x.Config.Enabled);
Expand All @@ -161,6 +135,15 @@ private void PatchGame(List<IModData> mods)
private void StartGame(string[] args)
{
_writer.WriteLine("Starting game...");

if (args.Contains("-consolePort"))
{
var index = Array.IndexOf(args, "-consolePort");
var list = new List<string>(args);
list.RemoveRange(index, 2);
args = list.ToArray();
}

try
{
Process.Start($"{_owmlConfig.GamePath}/OuterWilds.exe", string.Join(" ", args));
Expand Down
3 changes: 2 additions & 1 deletion OWML.Launcher/OWML.DefaultConfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"gamePath": "C:/Program Files/Epic Games/OuterWilds",
"verbose": true,
"combinationsBlockInput": false
"combinationsBlockInput": false,
"socketPort": 0
}
2 changes: 1 addition & 1 deletion OWML.Launcher/OWML.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="App.cs" />
<Compile Include="OutputListener.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SocketListener.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
62 changes: 0 additions & 62 deletions OWML.Launcher/OutputListener.cs

This file was deleted.

25 changes: 22 additions & 3 deletions OWML.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,38 @@ public class Program
static void Main(string[] args)
{
var owmlConfig = GetOwmlConfig() ?? CreateOwmlConfig();
SaveConsolePort(owmlConfig);
SaveOwmlPath(owmlConfig);
var owmlManifest = GetOwmlManifest();
var writer = OutputFactory.CreateOutput(owmlConfig, null, owmlManifest);
var writer = OutputFactory.CreateOutput(owmlConfig, null, owmlManifest,
CommandLineArguments.HasArgument(Constants.ConsolePortArgument));
var modFinder = new ModFinder(owmlConfig, writer);
var outputListener = new OutputListener(owmlConfig);
var pathFinder = new PathFinder(owmlConfig, writer);
var owPatcher = new OWPatcher(owmlConfig, writer);
var vrPatcher = new VRPatcher(owmlConfig, writer);
var app = new App(owmlConfig, owmlManifest, writer, modFinder,
outputListener, pathFinder, owPatcher, vrPatcher);
pathFinder, owPatcher, vrPatcher);
app.Run(args);
}

private static void SaveConsolePort(IOwmlConfig owmlConfig)
{
if (CommandLineArguments.HasArgument(Constants.ConsolePortArgument))
{
var argument = CommandLineArguments.GetArgument(Constants.ConsolePortArgument);
if (!int.TryParse(argument, out var port))
{
ConsoleUtils.WriteByType(MessageType.Error, "Error - Bad port.");
return;
}
owmlConfig.SocketPort = port;
}
else
{
new SocketListener(owmlConfig).Init();
}
}

private static IOwmlConfig GetOwmlConfig()
{
return JsonHelper.LoadJsonObject<OwmlConfig>(Constants.OwmlConfigFileName);
Expand Down
95 changes: 95 additions & 0 deletions OWML.Launcher/SocketListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Newtonsoft.Json;
using OWML.Common;
using OWML.ModHelper;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace OWML.Launcher
{
public class SocketListener
{
private const int BufferSize = 1024;
private static int _port;
private static TcpListener _server;
private static IOwmlConfig _config;

public SocketListener(IOwmlConfig config)
{
_config = config;
}

public void Init()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
_port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
_config.SocketPort = _port;
JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, _config);

new Task(SetupSocketListener).Start();
}

private void SetupSocketListener()
{
try
{
ListenToSocket();
}
catch (SocketException ex)
{
ConsoleUtils.WriteByType(MessageType.Error, $"Error in socket listener: {ex}");
}
finally
{
_server?.Stop();
}
}

private void ListenToSocket()
{
var localAddress = IPAddress.Parse(Constants.LocalAddress);

_server = new TcpListener(localAddress, _port);
_server.Start();

var bytes = new byte[BufferSize];

while (true)
{
var client = _server.AcceptTcpClient();

ConsoleUtils.WriteByType(MessageType.Success, "Console connected to socket!");

var stream = client.GetStream();

int i;

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
ProcessMessage(bytes, i);
}

client.Close();
}
}

private void ProcessMessage(byte[] bytes, int count)
{
var json = Encoding.UTF8.GetString(bytes, 0, count);

var data = JsonConvert.DeserializeObject<SocketMessage>(json);

if (data.Type == MessageType.Quit)
{
Environment.Exit(0);
}

ConsoleUtils.WriteByType(data.Type,
$"[{data.SenderName}.{data.SenderType}] : {data.Message}");
}
}
}
32 changes: 32 additions & 0 deletions OWML.ModHelper/Logging/ConsoleUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using OWML.Common;
using System;

namespace OWML.ModHelper
{
public static class ConsoleUtils
{
public static void WriteByType(MessageType type, string line)
{
switch (type)
{
case MessageType.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
case MessageType.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case MessageType.Success:
Console.ForegroundColor = ConsoleColor.Green;
break;
case MessageType.Message:
Console.ForegroundColor = ConsoleColor.Gray;
break;
case MessageType.Info:
Console.ForegroundColor = ConsoleColor.Cyan;
break;
}
Console.WriteLine(line);
Console.ForegroundColor = ConsoleColor.Gray;
}
}
}
Loading

0 comments on commit 867dd84

Please sign in to comment.