63 lines
1.3 KiB
C#
Raw Normal View History

2023-06-09 20:44:28 -07:00
using HarmonyLib;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using UnityEngine;
using Debug = UnityEngine.Debug;
2023-06-09 20:50:16 -07:00
namespace SteamRerouter.ModSide;
2023-06-09 20:44:28 -07:00
2023-06-09 22:32:56 -07:00
/// <summary>
/// top level file on the mod
/// </summary>
public class Interop : MonoBehaviour
2023-06-09 20:44:28 -07:00
{
2023-06-09 22:32:56 -07:00
private static Process _process;
2023-06-09 20:44:28 -07:00
2023-06-09 22:32:56 -07:00
private void Awake()
2023-06-09 20:44:28 -07:00
{
2023-06-09 22:32:56 -07:00
Log("awake");
2023-06-09 20:44:28 -07:00
2023-06-09 22:47:31 -07:00
Harmony.CreateAndPatchAll(typeof(Patches));
2023-06-09 22:50:09 -07:00
var port = IpcServer.Listen();
2023-06-09 20:44:28 -07:00
var processPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
2023-06-09 20:57:38 -07:00
"SteamRerouter.exe"
2023-06-09 20:44:28 -07:00
);
Log($"process path = {processPath}");
2023-06-09 22:32:56 -07:00
var gamePath = Application.dataPath;
2023-06-09 20:44:28 -07:00
Log($"game path = {gamePath}");
var workingDirectory = Path.Combine(gamePath, "Plugins", "x86_64");
Log($"working dir = {workingDirectory}");
var args = new[]
{
2023-06-09 22:32:56 -07:00
port.ToString(),
2023-06-09 20:44:28 -07:00
Path.Combine(gamePath, "Managed")
};
Log($"args = {args.Join()}");
2023-06-09 22:32:56 -07:00
_process = Process.Start(new ProcessStartInfo
2023-06-09 20:44:28 -07:00
{
FileName = processPath,
WorkingDirectory = workingDirectory,
2023-06-09 22:32:56 -07:00
Arguments = args.Join(x => $"\"{x}\"", " "),
2023-06-09 20:44:28 -07:00
UseShellExecute = false,
2023-06-09 22:32:56 -07:00
CreateNoWindow = false //true
2023-06-09 20:44:28 -07:00
});
2023-06-09 22:50:09 -07:00
IpcServer.Accept();
2023-06-09 22:32:56 -07:00
}
private void OnDestroy()
{
Log("destroy");
2023-06-09 22:50:09 -07:00
IpcServer.Quit();
2023-06-09 22:47:31 -07:00
_process.WaitForExit();
_process.Dispose();
2023-06-09 20:44:28 -07:00
}
2023-06-09 20:57:38 -07:00
public static void Log(object msg) => Debug.Log($"[SteamRerouter] {msg}");
2023-06-09 22:32:56 -07:00
}