90 lines
2.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>
2023-06-09 23:54:55 -07:00
public static class Interop
2023-06-09 20:44:28 -07:00
{
2023-06-09 23:54:55 -07:00
public static EntitlementsManager.AsyncOwnershipStatus OwnershipStatus = EntitlementsManager.AsyncOwnershipStatus.NotReady;
2023-06-09 20:44:28 -07:00
2023-06-09 23:54:55 -07:00
public static void Init()
2023-06-09 20:44:28 -07:00
{
2023-06-09 23:54:55 -07:00
Log("init");
2023-06-09 22:47:31 -07:00
Harmony.CreateAndPatchAll(typeof(Patches));
2023-06-09 20:44:28 -07:00
2023-09-18 09:48:25 +01:00
// Cache DLC ownership since the patched function gets called often.
// This won't work if the player buys the DLC mid-game, but too bad!
OwnershipStatus = IsDlcOwned()
? EntitlementsManager.AsyncOwnershipStatus.Owned
: EntitlementsManager.AsyncOwnershipStatus.NotOwned;
2023-06-09 23:54:55 -07:00
}
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 00:16:17 -07:00
var ownershipStatus = DoCommand(true, 0) != 0;
2023-06-09 23:54:55 -07:00
Log($"dlc owned: {ownershipStatus}");
return ownershipStatus;
}
public static void EarnAchivement(Achievements.Type type)
{
2023-06-10 00:16:17 -07:00
Log($"earn achievement {type}");
DoCommand(false, 1, (int)type);
2023-06-09 23:54:55 -07:00
}
private static int DoCommand(bool waitForExit, int type, int arg = default)
2023-06-09 23:54:55 -07:00
{
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 23:54:55 -07:00
Path.Combine(gamePath, "Managed"),
type.ToString(),
arg.ToString()
2023-06-09 20:44:28 -07:00
};
2023-09-18 09:48:25 +01:00
2023-06-09 20:44:28 -07:00
Log($"args = {args.Join()}");
2023-06-09 23:54:55 -07:00
var 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 23:54:55 -07:00
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
2023-06-09 20:44:28 -07:00
});
2023-09-18 09:48:25 +01:00
if (waitForExit)
2023-06-10 00:16:17 -07:00
{
process!.WaitForExit();
2023-06-09 20:44:28 -07:00
2023-06-10 00:16:17 -07:00
Log($"StandardOutput:\n{process.StandardOutput.ReadToEnd()}");
Log($"StandardError:\n{process.StandardError.ReadToEnd()}");
2023-06-10 00:16:17 -07:00
Log($"ExitCode: {process.ExitCode}");
2023-06-09 22:32:56 -07:00
2023-06-10 00:16:17 -07:00
return process.ExitCode;
}
2023-09-18 09:48:25 +01:00
2023-06-10 00:16:17 -07:00
return -1;
2023-06-09 20:44:28 -07:00
}
2023-06-09 22:32:56 -07:00
}