Decompiled source of Koyuki v1.3.0

plugins/4902-koyuki.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Steamworks;
using Steamworks.Data;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Kurosaki;

[BepInPlugin("4902.Koyuki", "Koyuki", "1.0.0")]
public class koyu : BaseUnityPlugin
{
	public static readonly Harmony harmony = new Harmony("4902.Koyuki");

	public static ManualLogSource mls;

	public static ConfigEntry<bool> cfg_saveseeds;

	public static ConfigEntry<int> cfg_millisecond;

	public static ConfigEntry<bool> cfg_synced_percentages;

	public static ConfigEntry<int> percentage;

	private void Awake()
	{
		cfg_saveseeds = ((BaseUnityPlugin)this).Config.Bind<bool>("#", "save/load", true, "[Save/load seeds]\nwhether the fish/koyuki item types should be saved to the save file. this saves the seed that determines the items type, so it will be the same when rejoining.\nloading a save file that has saved seeds without having this enabled or while in lan isn't recommended as the saved seeds can be reset if the number of items is changed.\nsaved item types are synced with other players if the synced percentages config is enabled");
		cfg_millisecond = ((BaseUnityPlugin)this).Config.Bind<int>("#", "timer", 100, "[Client wait_timer]\nwhen joining a lobby as non-host if save/load is enabled then koyuki items will wait to set their item type until the seeds sent by the host have been received or the time spent waiting reached the maximum amount set by this config.\nthere will be a log message specifying if the seeds were received first or the timer ended first. if the timer is often ending before the message from the host is being received then this should be increased. if the host doesn't have this mod then save/load can be disabled since there won't be a received message from the host so the items would always wait the full timer.\nmin is 20, max is 4000. 100 is before the player spawn animation ends, 500 is about 10 seconds, 1000 is about 15 seconds");
		cfg_synced_percentages = ((BaseUnityPlugin)this).Config.Bind<bool>("#", "sync", true, "[Synced percentages]\nautomatically sync config item percentages with the host. only disable if you're aware that disabling this can cause the item types to not be the same as other players if playing with others that have this mod");
		percentage = ((BaseUnityPlugin)this).Config.Bind<int>("#", "koyuki_percent", 50, "percentage that koyuki will replace fish\ninput = percentage, 50 is 50% etc");
		mls = Logger.CreateLogSource("Koyuki");
		mls.LogInfo((object)"yaata");
		harmony.PatchAll(typeof(koyuki));
	}
}
public class koyuki
{
	private static string[] item = new string[2];

	private static Mesh[] fish = (Mesh[])(object)new Mesh[7];

	private static AudioClip[] take = (AudioClip[])(object)new AudioClip[3];

	private static AudioClip[] give = (AudioClip[])(object)new AudioClip[3];

	private static Vector3[] position = (Vector3[])(object)new Vector3[2];

	private static Vector3[] rotation = (Vector3[])(object)new Vector3[2];

	private static bool[] z = new bool[2];

	private static Transform[] sequel = (Transform[])(object)new Transform[6];

	private static ulong lobbyid = 0uL;

	private static bool[] first_item = new bool[3] { true, true, true };

	private static bool sync = false;

	private static string seeds = "nil";

	private static bool[] disconnected;

	private static int synced_percent;

	private static string saved_fish;

	private static List<string> loaded_fish;

	[HarmonyPostfix]
	[HarmonyPatch(typeof(GrabbableObject), "Start")]
	private static void pst1(GrabbableObject __instance)
	{
		pst1async(__instance);
	}

	private static async void pst1async(GrabbableObject __instance)
	{
		if (!(((Object)__instance.itemProperties).name == "FishTestProp"))
		{
			return;
		}
		if (first_item[0])
		{
			Item val = StartOfRound.Instance.allItemsList.itemsList.First((Item _) => ((Object)_).name == "FishTestProp");
			val.restingRotation = new Vector3(0f, 0f, 90f);
			val.verticalOffset = 0.15f;
		}
		if (koyu.cfg_saveseeds.Value && !GameNetworkManager.Instance.disableSteam && seeds == "nil")
		{
			if (!GameNetworkManager.Instance.isHostingGame)
			{
				if (first_item[0])
				{
					koyu.mls.LogInfo((object)"await wait_timer");
					first_item[0] = false;
				}
				await wait_timer((koyu.cfg_millisecond.Value >= 20 && koyu.cfg_millisecond.Value <= 4000) ? koyu.cfg_millisecond.Value : 100);
				if (disconnected[0])
				{
					return;
				}
			}
			if (seeds == "nil")
			{
				seeds = "?";
			}
		}
		if (item[0] == null)
		{
			string text = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "4902-koyuki").Replace("\\", "/");
			koyu.mls.LogMessage((object)("Searching this filepath:" + text));
			AssetBundle assetBundle = AssetBundle.LoadFromFileAsync(text).assetBundle;
			item[0] = "Fish";
			fish[0] = ((Component)__instance).GetComponent<MeshFilter>().mesh;
			take[0] = __instance.itemProperties.grabSFX;
			give[0] = __instance.itemProperties.dropSFX;
			ref Vector3 reference = ref position[0];
			reference = __instance.itemProperties.positionOffset;
			ref Vector3 reference2 = ref rotation[0];
			reference2 = __instance.itemProperties.rotationOffset;
			z[0] = false;
			item[1] = "Koyuki";
			Transform[] componentsInChildren = Object.Instantiate<GameObject>(assetBundle.LoadAsset<GameObject>("koyuki_merged_90x.obj")).GetComponentsInChildren<Transform>();
			for (int i = 1; i <= 6; i++)
			{
				((Component)componentsInChildren[i]).GetComponent<MeshFilter>().mesh = fish1.fish2(((Component)componentsInChildren[i]).GetComponent<MeshFilter>().mesh);
				Object.Instantiate<Transform>(componentsInChildren[i]).SetParent(((Component)__instance).transform);
				componentsInChildren[i].SetParent(__instance.itemProperties.spawnPrefab.transform);
				Transform[] componentsInChildren2 = ((Component)__instance).GetComponentsInChildren<Transform>();
				Transform obj = componentsInChildren2[i + 1];
				Vector3 localPosition = (componentsInChildren[i].localPosition = new Vector3(-7.5f, 0f, 0f));
				obj.localPosition = localPosition;
				Transform obj2 = componentsInChildren2[i + 1];
				Quaternion localRotation = (componentsInChildren[i].localRotation = new Quaternion(0f, 0.7071f, 0f, 0.7071f));
				obj2.localRotation = localRotation;
				Transform obj3 = componentsInChildren2[i + 1];
				localPosition = (componentsInChildren[i].localScale = new Vector3(10f, 10f, 32.65306f));
				obj3.localScale = localPosition;
				fish[i] = Object.Instantiate<Mesh>(((Component)componentsInChildren2[i + 1]).GetComponent<MeshFilter>().mesh);
				sequel[i - 1] = componentsInChildren[i];
			}
			take[1] = assetBundle.LoadAsset<AudioClip>("koyuki_commonskill");
			take[2] = assetBundle.LoadAsset<AudioClip>("koyuki_formation_in_1");
			give[1] = assetBundle.LoadAsset<AudioClip>("koyuki_battle_damage_1");
			give[2] = assetBundle.LoadAsset<AudioClip>("koyuki_formation_select");
			ref Vector3 reference3 = ref position[1];
			reference3 = new Vector3(-0.12f, 0.09f, -0.08f);
			ref Vector3 reference4 = ref rotation[1];
			reference4 = new Vector3(23f, 0f, 0f);
			z[1] = true;
		}
		else if (((Component)__instance).GetComponentsInChildren<Transform>().Length < 6)
		{
			for (int i = 0; i < 6; i++)
			{
				Object.Instantiate<Transform>(sequel[i]).SetParent(((Component)__instance).transform);
				Transform[] componentsInChildren2 = ((Component)__instance).GetComponentsInChildren<Transform>();
				componentsInChildren2[i + 2].localPosition = new Vector3(-7.5f, 0f, 0f);
				componentsInChildren2[i + 2].localRotation = new Quaternion(0f, 0.7071f, 0f, 0.7071f);
				componentsInChildren2[i + 2].localScale = new Vector3(10f, 10f, 32.65306f);
			}
		}
		if (item[0] == null)
		{
			return;
		}
		Shion shion;
		if (!GameNetworkManager.Instance.disableSteam && lobbyid != 0 && (Object)(object)((Component)__instance).GetComponent<NetworkObject>() != (Object)null)
		{
			ulong seed;
			if (koyu.cfg_saveseeds.Value && seeds != "nil" && seeds != "?" && seeds.Contains(((Component)__instance).GetComponent<NetworkObject>().NetworkObjectId + "."))
			{
				if (first_item[2])
				{
					koyu.mls.LogInfo((object)"custom_random = new Shion(saved seed)");
				}
				int num = seeds.IndexOf(((Component)__instance).GetComponent<NetworkObject>().NetworkObjectId + ".");
				int i = ((Component)__instance).GetComponent<NetworkObject>().NetworkObjectId.ToString().Length + 1;
				seed = ulong.Parse(seeds.Substring(num + i, seeds.IndexOf("/", num) - (num + i)));
			}
			else
			{
				if (first_item[2])
				{
					koyu.mls.LogInfo((object)"custom_random = new Shion(lobbyid+networkobjectid)");
				}
				seed = lobbyid + ((Component)__instance).GetComponent<NetworkObject>().NetworkObjectId;
			}
			shion = new Shion(seed);
			if (koyu.cfg_saveseeds.Value)
			{
				((Component)__instance).gameObject.AddComponent<ItemTypeSeed>();
				((Component)__instance).gameObject.GetComponent<ItemTypeSeed>().seed = seed.ToString();
			}
		}
		else
		{
			if (first_item[2])
			{
				koyu.mls.LogInfo((object)"custom_random = new Shion()");
			}
			shion = new Shion();
		}
		if (first_item[2])
		{
			koyu.mls.LogInfo((object)((koyu.cfg_synced_percentages.Value && synced_percent != -1) ? "host config" : "local config"));
		}
		first_item[2] = false;
		int num2 = ((!koyu.cfg_synced_percentages.Value || synced_percent == -1) ? ((shion.next32mm(0, 100) < koyu.percentage.Value) ? 1 : 0) : ((shion.next32mm(0, 100) < synced_percent) ? 1 : 0));
		if (num2 == 1)
		{
			for (int i = 1; i <= 6; i++)
			{
				((Component)((Component)__instance).GetComponentsInChildren<Transform>()[i + 1]).GetComponent<MeshFilter>().mesh = fish[i];
			}
			BoxCollider component = ((Component)__instance).GetComponent<BoxCollider>();
			component.center = new Vector3(8f, 0.3f, 0f);
			component.size = new Vector3(31f, 3.3f, 3.3f);
			((Component)((Component)__instance).GetComponentInChildren<ScanNodeProperties>()).transform.localPosition = new Vector3(8f, 0.3f, 0f);
			((Component)__instance).GetComponent<MeshFilter>().mesh = null;
		}
		else
		{
			for (int i = 1; i <= 6; i++)
			{
				((Component)((Component)__instance).GetComponentsInChildren<Transform>()[i + 1]).GetComponent<MeshFilter>().mesh = null;
			}
			((Component)__instance).GetComponent<MeshFilter>().mesh = fish[0];
		}
		((Component)__instance).GetComponentInChildren<ScanNodeProperties>().headerText = item[num2];
	}

	private static async Task wait_timer(int maxwell)
	{
		int i = 0;
		while (seeds == "nil" && i < maxwell && !disconnected[0])
		{
			i++;
			await Task.Delay(4);
		}
		if (disconnected[0])
		{
			if (!disconnected[1])
			{
				koyu.mls.LogMessage((object)"disconnected before wait_timer ended");
				disconnected[1] = true;
			}
		}
		else if (first_item[1])
		{
			koyu.mls.LogMessage((object)((seeds == "nil") ? ("timer ended before receiving seeds (" + i + "/" + koyu.cfg_millisecond.Value + ")") : ("received seeds before timer ended (" + i + "/" + koyu.cfg_millisecond.Value + ")")));
			first_item[1] = false;
		}
	}

	[HarmonyPatch(typeof(GrabbableObject), "PlayDropSFX")]
	[HarmonyPrefix]
	private static void pre1(ref GrabbableObject __instance)
	{
		if (item[0] != null && (Object)(object)__instance != (Object)null && ((Object)__instance.itemProperties).name == "FishTestProp" && (Object)(object)((Component)__instance).GetComponentInChildren<ScanNodeProperties>() != (Object)null)
		{
			__instance.itemProperties.dropSFX = ((((Component)__instance).GetComponentInChildren<ScanNodeProperties>().headerText == item[1]) ? give[new Shion().next32mm(1, 3)] : give[0]);
		}
	}

	[HarmonyPatch(typeof(PlayerControllerB), "BeginGrabObject")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> trn1(IEnumerable<CodeInstruction> Instrs)
	{
		List<CodeInstruction> i = new List<CodeInstruction>(Instrs);
		for (int j = 0; j < i.Count; j++)
		{
			yield return i[j];
			if (j > 0 && ((object)i[j - 1]).ToString() == "call static float UnityEngine.Mathf::Clamp(float value, float min, float max)" && ((object)i[j]).ToString() == "stfld float GameNetcodeStuff.PlayerControllerB::carryWeight")
			{
				yield return new CodeInstruction(OpCodes.Ldarg_0, (object)null);
				yield return new CodeInstruction(OpCodes.Ldfld, (object)typeof(PlayerControllerB).GetField("currentlyGrabbingObject", BindingFlags.Instance | BindingFlags.NonPublic));
				yield return new CodeInstruction(OpCodes.Call, (object)typeof(koyuki).GetMethod("grab_item"));
			}
		}
	}

	public static void grab_item(GrabbableObject currentlyGrabbingObject)
	{
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		if (item[0] != null && (Object)(object)currentlyGrabbingObject != (Object)null && ((Object)currentlyGrabbingObject.itemProperties).name == "FishTestProp" && (Object)(object)((Component)currentlyGrabbingObject).GetComponentInChildren<ScanNodeProperties>() != (Object)null)
		{
			int num = ((((Component)currentlyGrabbingObject).GetComponentInChildren<ScanNodeProperties>().headerText == item[1]) ? 1 : 0);
			currentlyGrabbingObject.itemProperties.grabSFX = ((num == 1) ? take[new Shion().next32mm(1, 3)] : take[0]);
			currentlyGrabbingObject.itemProperties.canBeInspected = z[num];
			if (GameNetworkManager.Instance.isHostingGame)
			{
				currentlyGrabbingObject.itemProperties.positionOffset = position[num];
				currentlyGrabbingObject.itemProperties.rotationOffset = rotation[num];
			}
			currentlyGrabbingObject.itemProperties.itemName = ((Component)currentlyGrabbingObject).GetComponentInChildren<ScanNodeProperties>().headerText;
		}
	}

	[HarmonyPatch(typeof(GrabbableObject), "GrabItemOnClient")]
	[HarmonyPrefix]
	private static void pre2(GrabbableObject __instance)
	{
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		if (item[0] != null && (Object)(object)__instance != (Object)null && ((Object)__instance.itemProperties).name == "FishTestProp" && (Object)(object)((Component)__instance).GetComponentInChildren<ScanNodeProperties>() != (Object)null)
		{
			int num = ((((Component)__instance).GetComponentInChildren<ScanNodeProperties>().headerText == item[1]) ? 1 : 0);
			__instance.itemProperties.positionOffset = position[num];
			__instance.itemProperties.rotationOffset = rotation[num];
		}
	}

	[HarmonyTranspiler]
	[HarmonyPatch(typeof(PlayerControllerB), "SwitchToItemSlot")]
	private static IEnumerable<CodeInstruction> trn2(IEnumerable<CodeInstruction> Instrs)
	{
		List<CodeInstruction> i = new List<CodeInstruction>(Instrs);
		for (int j = 0; j < i.Count; j++)
		{
			yield return i[j];
			if (j < i.Count - 4 && ((object)i[j + 4]).ToString() == "callvirt virtual void GrabbableObject::EquipItem()")
			{
				yield return new CodeInstruction(OpCodes.Ldarg_0, (object)null);
				yield return new CodeInstruction(OpCodes.Call, (object)typeof(koyuki).GetMethod("hold_item"));
			}
		}
	}

	public static void hold_item(PlayerControllerB player)
	{
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_0166: Unknown result type (might be due to invalid IL or missing references)
		//IL_016b: Unknown result type (might be due to invalid IL or missing references)
		GrabbableObject val = player.ItemSlots[player.currentItemSlot];
		if (item[0] == null || !((Object)(object)val != (Object)null) || !(((Object)val.itemProperties).name == "FishTestProp") || !((Object)(object)((Component)val).GetComponentInChildren<ScanNodeProperties>() != (Object)null))
		{
			return;
		}
		int num = ((((Component)val).GetComponentInChildren<ScanNodeProperties>().headerText == item[1]) ? 1 : 0);
		val.itemProperties.grabSFX = ((num == 1) ? take[new Shion().next32mm(1, 3)] : take[0]);
		PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
		if ((Object)(object)player == (Object)(object)localPlayerController || (Object)(object)localPlayerController.ItemSlots[localPlayerController.currentItemSlot] == (Object)null || ((Object)localPlayerController.ItemSlots[localPlayerController.currentItemSlot].itemProperties).name != "FishTestProp" || localPlayerController.isPlayerDead)
		{
			val.itemProperties.canBeInspected = z[num];
			val.itemProperties.itemName = item[num];
			if (val.isHeld || (Object)(object)player != (Object)(object)localPlayerController)
			{
				val.itemProperties.positionOffset = position[num];
				val.itemProperties.rotationOffset = rotation[num];
			}
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(HUDManager), "DisplayNewScrapFound")]
	private static void pre3(ref List<GrabbableObject> ___itemsToBeDisplayed)
	{
		if (item[0] != null && (Object)(object)___itemsToBeDisplayed[0] != (Object)null && ((Object)___itemsToBeDisplayed[0].itemProperties).name == "FishTestProp" && (Object)(object)((Component)___itemsToBeDisplayed[0]).GetComponentInChildren<ScanNodeProperties>() != (Object)null)
		{
			___itemsToBeDisplayed[0].itemProperties.itemName = (___itemsToBeDisplayed[0].itemProperties.spawnPrefab.GetComponentInChildren<ScanNodeProperties>().headerText = ((Component)___itemsToBeDisplayed[0]).GetComponentInChildren<ScanNodeProperties>().headerText);
		}
	}

	[HarmonyPatch(typeof(HUDManager), "DisplayNewScrapFound")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> trn3(IEnumerable<CodeInstruction> Instrs)
	{
		List<CodeInstruction> i = new List<CodeInstruction>(Instrs);
		for (int j = 0; j < i.Count; j++)
		{
			if (j < i.Count - 1 && ((object)i[j + 1]).ToString() == "callvirt UnityEngine.Renderer[] UnityEngine.GameObject::GetComponentsInChildren()")
			{
				yield return new CodeInstruction(OpCodes.Ldloc_0, (object)null);
				yield return new CodeInstruction(OpCodes.Call, (object)typeof(koyuki).GetMethod("display_items"));
			}
			yield return i[j];
		}
	}

	public static void display_items(GameObject displayingObject)
	{
		if (item[0] == null || !(((Object)displayingObject).name == "FishTestProp(Clone)") || !((Object)(object)displayingObject.GetComponentInChildren<ScanNodeProperties>() != (Object)null))
		{
			return;
		}
		if (displayingObject.GetComponentInChildren<ScanNodeProperties>().headerText == item[1])
		{
			for (int i = 1; i <= 6; i++)
			{
				((Component)displayingObject.GetComponentsInChildren<Transform>()[i + 1]).GetComponent<MeshFilter>().mesh = fish[i];
			}
			displayingObject.GetComponent<MeshFilter>().mesh = null;
		}
		else
		{
			for (int i = 1; i <= 6; i++)
			{
				((Component)displayingObject.GetComponentsInChildren<Transform>()[i + 1]).GetComponent<MeshFilter>().mesh = null;
			}
			displayingObject.GetComponent<MeshFilter>().mesh = fish[0];
		}
	}

	[HarmonyPatch(typeof(StartOfRound), "Awake")]
	[HarmonyPostfix]
	private static void pst2()
	{
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		if (!GameNetworkManager.Instance.disableSteam)
		{
			bool[] array = new bool[2];
			disconnected = array;
			synced_percent = -1;
			if (GameNetworkManager.Instance.currentLobby.HasValue)
			{
				Lobby value = GameNetworkManager.Instance.currentLobby.Value;
				lobbyid = SteamId.op_Implicit(((Lobby)(ref value)).Id) % 1000000000;
				koyu.mls.LogInfo((object)lobbyid);
			}
			else
			{
				koyu.mls.LogError((object)"current lobby id is null");
			}
		}
	}

	[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
	[HarmonyPostfix]
	private static void pst3()
	{
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Expected O, but got Unknown
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		if (!sync)
		{
			if (NetworkManager.Singleton.IsHost)
			{
				NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("4902.Koyuki-Host", new HandleNamedMessageDelegate(host_receive));
			}
			else
			{
				koyu.mls.LogInfo((object)"requesting message from host");
				NetworkManager.Singleton.CustomMessagingManager.RegisterNamedMessageHandler("4902.Koyuki-Client", new HandleNamedMessageDelegate(client_receive));
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(0, (Allocator)2, -1);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("4902.Koyuki-Host", 0uL, val, (NetworkDelivery)3);
				((FastBufferWriter)(ref val)).Dispose();
			}
			sync = true;
		}
	}

	private static void host_receive(ulong id, FastBufferReader r)
	{
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		if (NetworkManager.Singleton.IsHost)
		{
			try
			{
				koyu.mls.LogInfo((object)"received request from client");
				string text = koyu.percentage.Value + "^" + seeds;
				koyu.mls.LogInfo((object)("sending message " + text));
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(FastBufferWriter.GetWriteSize(text, false), (Allocator)2, -1);
				((FastBufferWriter)(ref val)).WriteValueSafe(text, false);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("4902.Koyuki-Client", id, val, (NetworkDelivery)4);
				((FastBufferWriter)(ref val)).Dispose();
			}
			catch (Exception ex)
			{
				koyu.mls.LogError((object)("Error writing strings while syncing: " + ex));
			}
		}
	}

	private static void client_receive(ulong id, FastBufferReader r)
	{
		if (!NetworkManager.Singleton.IsClient)
		{
			return;
		}
		try
		{
			string text = default(string);
			((FastBufferReader)(ref r)).ReadValueSafe(ref text, false);
			koyu.mls.LogInfo((object)("client received message " + text));
			int num = 1;
			if (text.Length - text.Replace("^", "").Length == num)
			{
				string[] array = text.Split(new char[1] { '^' }, num + 1);
				synced_percent = int.Parse(array[0]);
				seeds = array[1];
				return;
			}
			koyu.mls.LogError((object)"received message was not what was expected. wasn't able to sync variables with host. (are the mod versions not the same?)");
			koyu.mls.LogError((object)("found " + (text.Length - text.Replace("^", "").Length) + "/" + num + " ^ in message " + text));
		}
		catch (Exception ex)
		{
			koyu.mls.LogError((object)("Error reading strings while syncing: " + ex));
		}
	}

	[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
	[HarmonyPrefix]
	private static void pre4()
	{
		disconnected[0] = true;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
	private static void pst4()
	{
		sync = false;
		seeds = "nil";
		lobbyid = 0uL;
		first_item = new bool[3] { true, true, true };
		saved_fish = "";
		loaded_fish = new List<string>();
		synced_percent = -1;
		if ((Object)(object)StartOfRound.Instance != (Object)null)
		{
			NetworkManager.Singleton.CustomMessagingManager.UnregisterNamedMessageHandler("4902.Koyuki-Host");
			NetworkManager.Singleton.CustomMessagingManager.UnregisterNamedMessageHandler("4902.Koyuki-Client");
		}
	}

	[HarmonyPatch(typeof(GameNetworkManager), "SaveItemsInShip")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> trn4(IEnumerable<CodeInstruction> Instrs)
	{
		List<CodeInstruction> i = new List<CodeInstruction>(Instrs);
		for (int j = 0; j < i.Count; j++)
		{
			yield return i[j];
			if (((object)i[j]).ToString() == "callvirt virtual void System.Collections.Generic.List<UnityEngine.Vector3>::Add(UnityEngine.Vector3 item)")
			{
				yield return new CodeInstruction(OpCodes.Ldloc_0, (object)null);
				yield return new CodeInstruction(OpCodes.Ldloc, (object)6);
				yield return new CodeInstruction(OpCodes.Call, (object)typeof(koyuki).GetMethod("save_fish"));
			}
		}
	}

	public static void save_fish(GrabbableObject[] _items, int index)
	{
		if (koyu.cfg_saveseeds.Value && GameNetworkManager.Instance.isHostingGame && !GameNetworkManager.Instance.disableSteam && lobbyid != 0 && ((Object)_items[index].itemProperties).name == "FishTestProp")
		{
			if ((Object)(object)((Component)_items[index]).GetComponent<ItemTypeSeed>() != (Object)null)
			{
				ItemTypeSeed component = ((Component)_items[index]).GetComponent<ItemTypeSeed>();
				saved_fish = saved_fish + component.seed + "/";
			}
			else if ((Object)(object)((Component)_items[index]).GetComponent<NetworkObject>() != (Object)null)
			{
				saved_fish = saved_fish + (lobbyid + ((Component)_items[index]).GetComponent<NetworkObject>().NetworkObjectId) + "/";
				koyu.mls.LogInfo((object)"ItemTypeSeed is null! saving seed for this item as lobbyid+networkobjectid");
			}
			else
			{
				saved_fish = saved_fish + new Shion().next32mm(1, 101) + "/";
				koyu.mls.LogInfo((object)"ItemTypeSeed and NetworkObject are null! saving seed for this item as a random number from 1 to 100");
			}
		}
	}

	[HarmonyPatch(typeof(GameNetworkManager), "SaveGame")]
	[HarmonyPostfix]
	private static void pst5()
	{
		if (!koyu.cfg_saveseeds.Value || !GameNetworkManager.Instance.isHostingGame || GameNetworkManager.Instance.disableSteam || !StartOfRound.Instance.inShipPhase || StartOfRound.Instance.isChallengeFile)
		{
			return;
		}
		try
		{
			if (saved_fish != "" && saved_fish.EndsWith("/"))
			{
				saved_fish = saved_fish.Substring(0, saved_fish.Length - 1);
			}
			koyu.mls.LogInfo((object)("saving " + saved_fish));
			ES3.Save<string>("4902.Koyuki-1", saved_fish, GameNetworkManager.Instance.currentSaveFileName);
			saved_fish = "";
		}
		catch (Exception ex)
		{
			koyu.mls.LogError((object)("Error saving item types: " + ex));
		}
	}

	[HarmonyPatch(typeof(StartOfRound), "LoadShipGrabbableItems")]
	[HarmonyTranspiler]
	private static IEnumerable<CodeInstruction> trn5(IEnumerable<CodeInstruction> Instrs)
	{
		List<CodeInstruction> i = new List<CodeInstruction>(Instrs);
		for (int j = 0; j < i.Count; j++)
		{
			yield return i[j];
			if (((object)i[j]).ToString() == "callvirt void Unity.Netcode.NetworkObject::Spawn(bool destroyWithScene)")
			{
				yield return new CodeInstruction(OpCodes.Ldloc_0, (object)null);
				yield return new CodeInstruction(OpCodes.Call, (object)typeof(koyuki).GetMethod("load_fish"));
			}
		}
	}

	public static void load_fish(GrabbableObject _item)
	{
		if (koyu.cfg_saveseeds.Value && GameNetworkManager.Instance.isHostingGame && !GameNetworkManager.Instance.disableSteam && lobbyid != 0 && ((Object)_item.itemProperties).name == "FishTestProp")
		{
			if ((Object)(object)((Component)_item).GetComponent<NetworkObject>() != (Object)null)
			{
				loaded_fish.Add(((Component)_item).GetComponent<NetworkObject>().NetworkObjectId.ToString());
				return;
			}
			loaded_fish.Add("nil");
			koyu.mls.LogInfo((object)"NetworkObject is null! the seed can't be loaded for this item");
		}
	}

	[HarmonyPatch(typeof(StartOfRound), "Start")]
	[HarmonyPostfix]
	private static void pst6()
	{
		if (!koyu.cfg_saveseeds.Value || !GameNetworkManager.Instance.isHostingGame || GameNetworkManager.Instance.disableSteam)
		{
			return;
		}
		try
		{
			string text = ES3.Load<string>("4902.Koyuki-1", GameNetworkManager.Instance.currentSaveFileName, "nil");
			koyu.mls.LogInfo((object)("loaded " + text));
			string[] array = text.Split(new char[1] { '/' });
			if (array[0] != "nil" && array[0] != "" && array.Length == loaded_fish.Count)
			{
				seeds = "";
				for (int i = 0; i < loaded_fish.Count; i++)
				{
					seeds = seeds + loaded_fish[i] + "." + array[i] + "/";
				}
				koyu.mls.LogInfo((object)("current networkobjectids + saved seeds " + seeds));
			}
			loaded_fish = new List<string>();
		}
		catch (Exception ex)
		{
			koyu.mls.LogError((object)("Error loading item types: " + ex));
		}
	}

	static koyuki()
	{
		bool[] array = new bool[2];
		disconnected = array;
		synced_percent = -1;
		saved_fish = "";
		loaded_fish = new List<string>();
	}
}
public class fish1
{
	public static Mesh fish2(Mesh nonReadableMesh)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Expected O, but got Unknown
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		Mesh val = new Mesh();
		val.indexFormat = nonReadableMesh.indexFormat;
		GraphicsBuffer vertexBuffer = nonReadableMesh.GetVertexBuffer(0);
		int num = vertexBuffer.stride * vertexBuffer.count;
		byte[] array = new byte[num];
		vertexBuffer.GetData((Array)array);
		val.SetVertexBufferParams(nonReadableMesh.vertexCount, nonReadableMesh.GetVertexAttributes());
		val.SetVertexBufferData<byte>(array, 0, 0, num, 0, (MeshUpdateFlags)0);
		vertexBuffer.Release();
		val.subMeshCount = nonReadableMesh.subMeshCount;
		GraphicsBuffer indexBuffer = nonReadableMesh.GetIndexBuffer();
		int num2 = indexBuffer.stride * indexBuffer.count;
		byte[] array2 = new byte[num2];
		indexBuffer.GetData((Array)array2);
		val.SetIndexBufferParams(indexBuffer.count, nonReadableMesh.indexFormat);
		val.SetIndexBufferData<byte>(array2, 0, 0, num2, (MeshUpdateFlags)0);
		indexBuffer.Release();
		uint num3 = 0u;
		for (int i = 0; i < val.subMeshCount; i++)
		{
			uint indexCount = nonReadableMesh.GetIndexCount(i);
			val.SetSubMesh(i, new SubMeshDescriptor((int)num3, (int)indexCount, (MeshTopology)0), (MeshUpdateFlags)0);
			num3 += indexCount;
		}
		val.RecalculateNormals();
		val.RecalculateBounds();
		return val;
	}
}
public class ItemTypeSeed : MonoBehaviour
{
	public string guid = koyu.harmony.Id;

	public string seed;
}
public class Shion
{
	private ulong[] state;

	public Shion()
	{
		RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();
		byte[] array = new byte[8];
		randomNumberGenerator.GetBytes(array, 0, 8);
		ulong seed = BitConverter.ToUInt64(array, 0);
		xorshift256_init(seed);
	}

	public Shion(ulong seed)
	{
		xorshift256_init(seed);
	}

	public int next32mm(int min, int max)
	{
		uint num = next32();
		double num2 = (double)(max - min) / 4294967295.0;
		return (int)((double)min + (double)num * num2);
	}

	public byte[] next8()
	{
		ulong value = xoshiro256ss();
		return BitConverter.GetBytes(value);
	}

	public uint next32()
	{
		byte[] value = next8();
		return BitConverter.ToUInt32(value, 0);
	}

	public ulong next64()
	{
		return xoshiro256ss();
	}

	public double next01()
	{
		ulong num = xoshiro256ss();
		return (double)num / 1.8446744073709552E+19;
	}

	private ulong splitmix64(ulong partialstate)
	{
		partialstate += 11400714819323198485uL;
		partialstate = (partialstate ^ (partialstate >> 30)) * 13787848793156543929uL;
		partialstate = (partialstate ^ (partialstate >> 27)) * 10723151780598845931uL;
		return partialstate ^ (partialstate >> 31);
	}

	private void xorshift256_init(ulong seed)
	{
		ulong[] array = new ulong[4];
		array[0] = splitmix64(seed);
		array[1] = splitmix64(array[0]);
		array[2] = splitmix64(array[1]);
		array[3] = splitmix64(array[2]);
		state = array;
	}

	private ulong rotl64(ulong x, int k)
	{
		return (x << k) | (x >> 64 - k);
	}

	private ulong xoshiro256ss()
	{
		ulong result = rotl64(state[1] * 5, 7) * 9;
		ulong num = state[1] << 17;
		state[2] ^= state[0];
		state[3] ^= state[1];
		state[1] ^= state[2];
		state[0] ^= state[3];
		state[2] ^= num;
		state[3] = rotl64(state[3], 45);
		return result;
	}
}