quantum-space-buddies/SteamRerouter/ExeSide/Program.cs

92 lines
2.4 KiB
C#
Raw Normal View History

2023-06-10 07:08:33 +00:00
using HarmonyLib;
using Steamworks;
2023-06-10 05:32:56 +00:00
using System;
2023-06-10 03:44:28 +00:00
using System.IO;
using System.Reflection;
2023-06-10 03:50:16 +00:00
namespace SteamRerouter.ExeSide;
2023-06-10 03:44:28 +00:00
2023-06-10 05:32:56 +00:00
/// <summary>
/// top level file on the exe
/// </summary>
2023-06-10 03:44:28 +00:00
public static class Program
{
2023-06-10 06:54:55 +00:00
private static int Main(string[] args)
2023-06-10 03:44:28 +00:00
{
2023-06-10 06:54:55 +00:00
var managedDir = args[0];
2023-06-10 03:44:28 +00:00
Log($"managed dir = {managedDir}");
AppDomain.CurrentDomain.AssemblyResolve += (_, e) =>
{
var name = new AssemblyName(e.Name).Name + ".dll";
var path = Path.Combine(managedDir, name);
return File.Exists(path) ? Assembly.LoadFile(path) : null;
};
2023-06-10 06:54:55 +00:00
var type = int.Parse(args[1]);
Log($"command type = {type}");
var arg = int.Parse(args[2]);
Log($"command arg = {arg}");
return DoCommand(type, arg);
2023-06-10 03:44:28 +00:00
}
2023-06-10 06:54:55 +00:00
public static void Log(object msg) => Console.Out.WriteLine(msg);
public static void LogError(object msg) => Console.Error.WriteLine(msg);
2023-06-10 05:47:31 +00:00
2023-06-10 06:54:55 +00:00
private static int DoCommand(int type, int arg = default)
{
2023-06-10 05:32:56 +00:00
// copied from QSBCore
if (!Packsize.Test())
2023-06-10 03:44:28 +00:00
{
2023-06-10 06:54:55 +00:00
LogError("[Steamworks.NET] Packsize Test returned false, the wrong version of Steamworks.NET is being run in this platform.");
2023-06-10 03:44:28 +00:00
}
2023-06-10 05:32:56 +00:00
if (!DllCheck.Test())
2023-06-10 03:44:28 +00:00
{
2023-06-10 06:54:55 +00:00
LogError("[Steamworks.NET] DllCheck Test returned false, One or more of the Steamworks binaries seems to be the wrong version.");
2023-06-10 05:32:56 +00:00
}
2023-06-10 03:44:28 +00:00
2023-06-10 05:32:56 +00:00
// from facepunch.steamworks SteamClient.cs
2023-06-10 06:17:41 +00:00
Environment.SetEnvironmentVariable("SteamAppId", "753640");
Environment.SetEnvironmentVariable("SteamGameId", "753640");
2023-06-10 05:32:56 +00:00
if (!SteamAPI.Init())
{
2023-11-04 23:01:00 +00:00
LogError($"FATAL - SteamAPI.Init() failed. Do you have Steam open, and are you logged in?");
2023-06-10 06:54:55 +00:00
return -1;
2023-06-10 03:44:28 +00:00
}
2023-06-10 05:32:56 +00:00
2023-06-10 06:54:55 +00:00
var exitCode = -1;
switch (type)
{
// dlc status
case 0:
var owned = SteamApps.BIsDlcInstalled((AppId_t)1622100U);
Log($"dlc owned: {owned}");
exitCode = owned ? 1 : 0;
break;
2023-06-10 05:47:31 +00:00
2023-06-10 06:54:55 +00:00
// earn achievement
case 1:
2023-06-10 07:08:33 +00:00
var achievementType = (Achievements.Type)arg;
Log("Earn " + achievementType);
// for some reason even with unsafe code turned on it throws a FieldAccessException
var s_names = (string[])AccessTools.Field(typeof(Achievements), "s_names").GetValue(null);
if (!SteamUserStats.SetAchievement(s_names[(int)achievementType]))
{
LogError("Unable to grant achievement \"" + s_names[(int)achievementType] + "\"");
}
else
2023-06-10 06:54:55 +00:00
{
2023-06-10 07:08:33 +00:00
exitCode = 0;
2023-06-10 06:54:55 +00:00
}
SteamUserStats.StoreStats();
break;
}
2023-06-10 05:32:56 +00:00
SteamAPI.Shutdown();
2023-06-10 06:54:55 +00:00
return exitCode;
2023-06-10 03:44:28 +00:00
}
2023-06-10 05:32:56 +00:00
}