using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using LethalLib.Modules;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BuyableKnives")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BuyableKnives")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("26314bee-88d6-47b5-ae2a-70caeb019537")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace buyableknives;
[BepInPlugin("IyeQ.BuyableKnives", "Buyable Knives", "1.0.0")]
[BepInDependency("evaisa.lethallib", "0.13.2")]
public class BuyableKnives : BaseUnityPlugin
{
public class ClonedItem : Item
{
public Item original;
}
internal class Unflagger : MonoBehaviour
{
public void Awake()
{
((Object)((Component)this).gameObject).hideFlags = (HideFlags)0;
}
}
[HarmonyPatch]
internal static class Patches
{
[HarmonyPostfix]
[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
public static void ServerConnect()
{
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Expected O, but got Unknown
//IL_008b: Unknown result type (might be due to invalid IL or missing references)
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Expected O, but got Unknown
if (IsHost)
{
LoggerInstance.LogInfo((object)"Started hosting, using local settings");
NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("BuyableKnives_OnRequestConfigSync", new HandleNamedMessageDelegate(OnRequestSync));
UpdateShopItemPrice();
}
else
{
LoggerInstance.LogInfo((object)"Connected to server, requesting settings");
NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("BuyableKnives_OnReceiveConfigSync", new HandleNamedMessageDelegate(OnReceiveSync));
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BuyableKnives_OnRequestConfigSync", 0uL, new FastBufferWriter(0, (Allocator)2, -1), (NetworkDelivery)2);
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameNetworkManager), "Start")]
public static void Start()
{
LoggerInstance.LogWarning((object)"Game network manager start");
CloneKnife();
}
[HarmonyPostfix]
[HarmonyPatch(typeof(GameNetworkManager), "StartDisconnect")]
public static void ServerDisconnect()
{
LoggerInstance.LogInfo((object)"Server disconnect");
KnifePriceRemote = -1;
}
}
private const string modGUID = "IyeQ.BuyableKnives";
private const string modName = "Buyable Knives";
private const string modVersion = "1.0.0";
private readonly Harmony harmony = new Harmony("IyeQ.BuyableKnives");
private static BuyableKnives Instance;
private static ConfigEntry<int> KnifePriceConfig;
internal static int KnifePriceRemote = -1;
private static Dictionary<string, TerminalNode> infoNodes = new Dictionary<string, TerminalNode>();
public static byte CurrentVersionByte = 1;
private static ManualLogSource LoggerInstance => ((BaseUnityPlugin)Instance).Logger;
public static List<Item> AllItems => Resources.FindObjectsOfTypeAll<Item>().Reverse().ToList();
public static Item Knife => ((IEnumerable<Item>)AllItems).FirstOrDefault((Func<Item, bool>)((Item item) => ((Object)item).name.Equals("Knife") && (Object)(object)item.spawnPrefab != (Object)null));
public static ClonedItem KnifeClone { get; private set; }
public static int KnifePriceLocal => KnifePriceConfig.Value;
public static int KnifePrice => (KnifePriceRemote > -1) ? KnifePriceRemote : KnifePriceLocal;
private static bool IsHost => NetworkManager.Singleton.IsHost;
private static ulong LocalClientId => NetworkManager.Singleton.LocalClientId;
private void Awake()
{
if ((Object)(object)Instance == (Object)null)
{
Object.DontDestroyOnLoad((Object)(object)this);
Instance = this;
}
harmony.PatchAll();
KnifePriceConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Prices", "KnifePrice", 5, "Credits needed to buy Knife");
((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin Buyable Knives is loaded with version 1.0.0!");
}
private static ClonedItem CloneNonScrap(Item original, int price)
{
ClonedItem clonedItem = ScriptableObject.CreateInstance<ClonedItem>();
Object.DontDestroyOnLoad((Object)(object)clonedItem);
clonedItem.original = original;
GameObject val = NetworkPrefabs.CloneNetworkPrefab(original.spawnPrefab, "Buyable" + ((Object)original).name);
val.AddComponent<Unflagger>();
Object.DontDestroyOnLoad((Object)(object)val);
CopyFields(original, (Item)(object)clonedItem);
val.GetComponent<GrabbableObject>().itemProperties = (Item)(object)clonedItem;
((Item)clonedItem).spawnPrefab = val;
((Object)clonedItem).name = "Buyable" + ((Object)original).name;
((Item)clonedItem).creditsWorth = price;
((Item)clonedItem).isScrap = false;
return clonedItem;
}
public static void CopyFields(Item source, Item destination)
{
FieldInfo[] fields = typeof(Item).GetFields();
FieldInfo[] array = fields;
foreach (FieldInfo fieldInfo in array)
{
fieldInfo.SetValue(destination, fieldInfo.GetValue(source));
}
}
private static TerminalNode CreateInfoNode(string name, string description)
{
if (infoNodes.ContainsKey(name))
{
return infoNodes[name];
}
TerminalNode val = ScriptableObject.CreateInstance<TerminalNode>();
Object.DontDestroyOnLoad((Object)(object)val);
val.clearPreviousText = true;
((Object)val).name = name + "InfoNode";
val.displayText = description + "\n\n";
infoNodes.Add(name, val);
return val;
}
private static void CloneKnife()
{
if (!((Object)(object)Knife == (Object)null) && !((Object)(object)KnifeClone != (Object)null))
{
KnifeClone = CloneNonScrap(Knife, KnifePrice);
AddToShop();
}
}
private static void AddToShop()
{
ClonedItem knifeClone = KnifeClone;
int knifePrice = KnifePrice;
Items.RegisterShopItem((Item)(object)knifeClone, (TerminalNode)null, (TerminalNode)null, CreateInfoNode("Knife", "Butler's knife. Only use it for safety purposes. Do not attempt to attack your colleagues."), knifePrice);
LoggerInstance.LogInfo((object)$"Knife added to Shop for {KnifePrice} credits");
}
private static void UpdateShopItemPrice()
{
((Item)KnifeClone).creditsWorth = KnifePrice;
Items.UpdateShopItemPrice((Item)(object)KnifeClone, KnifePrice);
LoggerInstance.LogInfo((object)$"Knife price updated to {KnifePrice} credits");
}
public static void WriteData(FastBufferWriter writer)
{
((FastBufferWriter)(ref writer)).WriteByte(CurrentVersionByte);
((FastBufferWriter)(ref writer)).WriteBytes(BitConverter.GetBytes(KnifePriceLocal), -1, 0);
}
public static void ReadData(FastBufferReader reader)
{
byte b = default(byte);
((FastBufferReader)(ref reader)).ReadByte(ref b);
if (b == CurrentVersionByte)
{
byte[] value = new byte[4];
((FastBufferReader)(ref reader)).ReadBytes(ref value, 4, 0);
KnifePriceRemote = BitConverter.ToInt32(value, 0);
UpdateShopItemPrice();
LoggerInstance.LogInfo((object)"Host config set successfully");
return;
}
throw new Exception("Invalid version byte");
}
public static void OnRequestSync(ulong clientID, FastBufferReader reader)
{
//IL_0032: Unknown result type (might be due to invalid IL or missing references)
//IL_0049: Unknown result type (might be due to invalid IL or missing references)
if (!IsHost)
{
return;
}
LoggerInstance.LogInfo((object)("Sending config to client " + clientID));
FastBufferWriter val = default(FastBufferWriter);
((FastBufferWriter)(ref val))..ctor(5, (Allocator)2, 5);
try
{
WriteData(val);
NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("BuyableKnives_OnReceiveConfigSync", clientID, val, (NetworkDelivery)2);
}
catch (Exception arg)
{
LoggerInstance.LogError((object)$"Failed to send config: {arg}");
}
finally
{
((FastBufferWriter)(ref val)).Dispose();
}
}
public static void OnReceiveSync(ulong clientID, FastBufferReader reader)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
LoggerInstance.LogInfo((object)"Received config from host");
try
{
ReadData(reader);
}
catch (Exception arg)
{
LoggerInstance.LogError((object)$"Failed to receive config: {arg}");
KnifePriceRemote = -1;
}
}
}