quantum-space-buddies/SteamRerouter/ModSide/Interop.cs

84 lines
2.2 KiB
C#
Raw Normal View History

2023-06-10 03:44:28 +00:00
using HarmonyLib;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using UnityEngine;
using Debug = UnityEngine.Debug;
2023-06-10 03:50:16 +00:00
namespace SteamRerouter.ModSide;
2023-06-10 03:44:28 +00:00
2023-06-10 05:32:56 +00:00
/// <summary>
/// top level file on the mod
/// </summary>
2023-06-10 06:54:55 +00:00
public static class Interop
2023-06-10 03:44:28 +00:00
{
2023-06-10 06:54:55 +00:00
public static EntitlementsManager.AsyncOwnershipStatus OwnershipStatus = EntitlementsManager.AsyncOwnershipStatus.NotReady;
2023-06-10 03:44:28 +00:00
2023-06-10 06:54:55 +00:00
public static void Init()
2023-06-10 03:44:28 +00:00
{
2023-06-10 06:54:55 +00:00
Log("init");
2023-06-10 05:47:31 +00:00
Harmony.CreateAndPatchAll(typeof(Patches));
2023-06-10 03:44:28 +00:00
2023-06-10 06:54:55 +00:00
// cache dlc ownership since the patched function gets called often
OwnershipStatus = IsDlcOwned() ? EntitlementsManager.AsyncOwnershipStatus.Owned : EntitlementsManager.AsyncOwnershipStatus.NotOwned;
}
public static void Log(object msg) => Debug.Log($"[SteamRerouter] {msg}");
public static void LogError(object msg) => Debug.LogError($"[SteamRerouter] {msg}");
private static bool IsDlcOwned()
{
2023-06-10 07:16:17 +00:00
var ownershipStatus = DoCommand(true, 0) != 0;
2023-06-10 06:54:55 +00:00
Log($"dlc owned: {ownershipStatus}");
return ownershipStatus;
}
public static void EarnAchivement(Achievements.Type type)
{
2023-06-10 07:16:17 +00:00
Log($"earn achievement {type}");
DoCommand(false, 1, (int)type);
2023-06-10 06:54:55 +00:00
}
2023-06-10 07:16:17 +00:00
private static int DoCommand(bool wait, int type, int arg = default)
2023-06-10 06:54:55 +00:00
{
2023-06-10 03:44:28 +00:00
var processPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
2023-06-10 03:57:38 +00:00
"SteamRerouter.exe"
2023-06-10 03:44:28 +00:00
);
Log($"process path = {processPath}");
2023-06-10 05:32:56 +00:00
var gamePath = Application.dataPath;
2023-06-10 03:44:28 +00:00
Log($"game path = {gamePath}");
var workingDirectory = Path.Combine(gamePath, "Plugins", "x86_64");
Log($"working dir = {workingDirectory}");
var args = new[]
{
2023-06-10 06:54:55 +00:00
Path.Combine(gamePath, "Managed"),
type.ToString(),
arg.ToString()
2023-06-10 03:44:28 +00:00
};
Log($"args = {args.Join()}");
2023-06-10 06:54:55 +00:00
var process = Process.Start(new ProcessStartInfo
2023-06-10 03:44:28 +00:00
{
FileName = processPath,
WorkingDirectory = workingDirectory,
2023-06-10 05:32:56 +00:00
Arguments = args.Join(x => $"\"{x}\"", " "),
2023-06-10 03:44:28 +00:00
UseShellExecute = false,
2023-06-10 06:54:55 +00:00
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
2023-06-10 03:44:28 +00:00
});
2023-06-10 07:16:17 +00:00
if (wait)
{
process!.WaitForExit();
2023-06-10 03:44:28 +00:00
2023-06-10 07:16:17 +00:00
Log($"StandardOutput:\n{process.StandardOutput.ReadToEnd()}");
LogError($"StandardError:\n{process.StandardError.ReadToEnd()}");
Log($"ExitCode: {process.ExitCode}");
2023-06-10 05:32:56 +00:00
2023-06-10 07:16:17 +00:00
return process.ExitCode;
}
return -1;
2023-06-10 03:44:28 +00:00
}
2023-06-10 05:32:56 +00:00
}