using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SecretLabs.Patches;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SecretLabs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("SecretLabs")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SecretLabs")]
[assembly: AssemblyTitle("SecretLabs")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace SecretLabs
{
[Serializable]
public class Config : SyncedInstance<Config>
{
[CompilerGenerated]
private static class <>O
{
public static HandleNamedMessageDelegate <0>__OnRequestSync;
public static HandleNamedMessageDelegate <1>__OnReceiveSync;
}
public bool customPriceEnabled;
public int moonPrice;
public Config(ConfigFile cfg)
{
InitInstance(this);
customPriceEnabled = cfg.Bind<bool>("General", "CustomPriceEnabled", false, "Enable this if you wish to configure the moons route price below.").Value;
moonPrice = cfg.Bind<int>("General", "MoonPrice", 700, "The route price of the Secret Labs moon.").Value;
}
public static void RequestSync()
{
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
if (!SyncedInstance<Config>.IsClient)
{
return;
}
FastBufferWriter val = default(FastBufferWriter);
((FastBufferWriter)(ref val))..ctor(SyncedInstance<Config>.IntSize, (Allocator)2, -1);
try
{
SyncedInstance<Config>.MessageManager.SendNamedMessage("SecretLabs_OnRequestConfigSync", 0uL, val, (NetworkDelivery)3);
}
finally
{
((IDisposable)(FastBufferWriter)(ref val)).Dispose();
}
}
public static void OnRequestSync(ulong clientId, FastBufferReader _)
{
//IL_002d: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
if (!SyncedInstance<Config>.IsHost)
{
return;
}
byte[] array = SyncedInstance<Config>.SerializeToBytes(SyncedInstance<Config>.Instance);
int num = array.Length;
FastBufferWriter val = default(FastBufferWriter);
((FastBufferWriter)(ref val))..ctor(num + SyncedInstance<Config>.IntSize, (Allocator)2, -1);
try
{
((FastBufferWriter)(ref val)).WriteValueSafe<int>(ref num, default(ForPrimitives));
((FastBufferWriter)(ref val)).WriteBytesSafe(array, -1, 0);
SyncedInstance<Config>.MessageManager.SendNamedMessage("SecretLabs_OnReceiveConfigSync", clientId, val, (NetworkDelivery)3);
}
catch (Exception arg)
{
Plugin.Logger.LogDebug((object)$"Error occurred syncing config with client: {clientId}\n{arg}");
}
finally
{
((IDisposable)(FastBufferWriter)(ref val)).Dispose();
}
}
public static void OnReceiveSync(ulong _, FastBufferReader reader)
{
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
if (!((FastBufferReader)(ref reader)).TryBeginRead(SyncedInstance<Config>.IntSize))
{
Plugin.Logger.LogError((object)"Config sync error: Could not begin reading buffer.");
return;
}
int num = default(int);
((FastBufferReader)(ref reader)).ReadValueSafe<int>(ref num, default(ForPrimitives));
if (!((FastBufferReader)(ref reader)).TryBeginRead(num))
{
Plugin.Logger.LogError((object)"Config sync error: Host could not sync.");
return;
}
byte[] data = new byte[num];
((FastBufferReader)(ref reader)).ReadBytesSafe(ref data, num, 0);
SyncedInstance<Config>.SyncInstance(data);
Plugin.Logger.LogDebug((object)"Successfully synced config with host.");
}
[HarmonyPostfix]
[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
public static void InitializeLocalPlayer()
{
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_0063: Expected O, but got Unknown
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_002c: Expected O, but got Unknown
if (SyncedInstance<Config>.IsHost)
{
CustomMessagingManager messageManager = SyncedInstance<Config>.MessageManager;
object obj = <>O.<0>__OnRequestSync;
if (obj == null)
{
HandleNamedMessageDelegate val = OnRequestSync;
<>O.<0>__OnRequestSync = val;
obj = (object)val;
}
messageManager.RegisterNamedMessageHandler("SecretLabs_OnRequestConfigSync", (HandleNamedMessageDelegate)obj);
SyncedInstance<Config>.Synced = true;
return;
}
SyncedInstance<Config>.Synced = false;
CustomMessagingManager messageManager2 = SyncedInstance<Config>.MessageManager;
object obj2 = <>O.<1>__OnReceiveSync;
if (obj2 == null)
{
HandleNamedMessageDelegate val2 = OnReceiveSync;
<>O.<1>__OnReceiveSync = val2;
obj2 = (object)val2;
}
messageManager2.RegisterNamedMessageHandler("SecretLabs_OnReceiveConfigSync", (HandleNamedMessageDelegate)obj2);
RequestSync();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
public static void PlayerLeave()
{
SyncedInstance<Config>.RevertSync();
}
}
[Serializable]
public class SyncedInstance<T>
{
[NonSerialized]
protected static int IntSize = 4;
internal static CustomMessagingManager MessageManager => NetworkManager.Singleton.CustomMessagingManager;
internal static bool IsClient => NetworkManager.Singleton.IsClient;
internal static bool IsHost => NetworkManager.Singleton.IsHost;
public static T Default { get; set; }
public static T Instance { get; set; }
public static bool Synced { get; internal set; }
protected void InitInstance(T instance)
{
Default = instance;
Instance = instance;
}
internal static void SyncInstance(byte[] data)
{
Instance = DeserializeFromBytes(data);
Synced = true;
}
internal static void RevertSync()
{
Instance = Default;
Synced = false;
}
public static byte[] SerializeToBytes(T val)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using MemoryStream memoryStream = new MemoryStream();
try
{
binaryFormatter.Serialize(memoryStream, val);
return memoryStream.ToArray();
}
catch (Exception arg)
{
Plugin.Logger.LogError((object)$"Error serializing instance: {arg}");
return null;
}
}
public static T DeserializeFromBytes(byte[] data)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
using MemoryStream serializationStream = new MemoryStream(data);
try
{
return (T)binaryFormatter.Deserialize(serializationStream);
}
catch (Exception arg)
{
Plugin.Logger.LogError((object)$"Error deserializing instance: {arg}");
return default(T);
}
}
}
[BepInPlugin("SecretLabs", "SecretLabs", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
private Harmony _harmony;
public static ManualLogSource Logger;
public static Config Config { get; internal set; }
private void Awake()
{
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Expected O, but got Unknown
Logger = ((BaseUnityPlugin)this).Logger;
_harmony = new Harmony("SecretLabs");
Config = new Config(((BaseUnityPlugin)this).Config);
_harmony.PatchAll(typeof(RoutePatches));
_harmony.PatchAll(typeof(Config));
Logger.LogDebug((object)"Plugin SecretLabs is loaded!");
}
}
public static class PluginInfo
{
public const string PLUGIN_GUID = "SecretLabs";
public const string PLUGIN_NAME = "SecretLabs";
public const string PLUGIN_VERSION = "1.0.0";
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "SecretLabs";
public const string PLUGIN_NAME = "SecretLabs";
public const string PLUGIN_VERSION = "1.0.0";
}
}
namespace SecretLabs.Patches
{
[HarmonyPatch(typeof(Terminal))]
public class RoutePatches
{
[HarmonyPatch("LoadNewNode")]
[HarmonyPrefix]
private static void LoadNewNodePatchBefore(ref TerminalNode node)
{
if (!SyncedInstance<Config>.Instance.customPriceEnabled)
{
return;
}
Terminal obj = Object.FindObjectOfType<Terminal>();
Plugin.Logger.LogDebug((object)((Object)node).name);
CompatibleNoun[] compatibleNouns = obj.terminalNodes.allKeywords.First((TerminalKeyword terminalKeyword) => terminalKeyword.word == "route").compatibleNouns;
foreach (CompatibleNoun val in compatibleNouns)
{
if (!((Object)(object)val.result == (Object)null) && ((Object)val.result).name == "secret labsRoute")
{
val.result.itemCost = SyncedInstance<Config>.Instance.moonPrice;
}
}
}
[HarmonyPatch("LoadNewNodeIfAffordable")]
[HarmonyPrefix]
private static void LoadNewNodeIfAffordablePatch(ref TerminalNode node)
{
if (SyncedInstance<Config>.Instance.customPriceEnabled)
{
Plugin.Logger.LogDebug((object)((Object)node).name);
if (!((Object)(object)node == (Object)null) && !(((Object)node).name != "secret labsRouteConfirm"))
{
node.itemCost = Math.Abs(SyncedInstance<Config>.Instance.moonPrice);
}
}
}
}
}