Decompiled source of Lethal Questing v1.1.1

LethalQuesting.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using LethalNetworkAPI;
using Microsoft.CodeAnalysis;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LethalQuesting")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LethalQuesting")]
[assembly: AssemblyTitle("LethalQuesting")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace LethalQuesting
{
	public enum QuestType
	{
		None,
		Scrap,
		Kill,
		GroupSurvival,
		Betray,
		MovementProhibition
	}
	public struct QuestData : INetworkSerializable
	{
		public QuestType Type;

		public string Title;

		public string TargetName;

		public int TargetCount;

		public int CurrentCount;

		public int Reward;

		public bool IsRewardGiven;

		public unsafe void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			((BufferSerializer<QuestType>*)(&serializer))->SerializeValue<QuestType>(ref Type, default(ForEnums));
			serializer.SerializeValue(ref Title, false);
			serializer.SerializeValue(ref TargetName, false);
			((BufferSerializer<int>*)(&serializer))->SerializeValue<int>(ref TargetCount, default(ForPrimitives));
			((BufferSerializer<int>*)(&serializer))->SerializeValue<int>(ref CurrentCount, default(ForPrimitives));
			((BufferSerializer<int>*)(&serializer))->SerializeValue<int>(ref Reward, default(ForPrimitives));
			((BufferSerializer<bool>*)(&serializer))->SerializeValue<bool>(ref IsRewardGiven, default(ForPrimitives));
		}
	}
	public class ScrapData
	{
		public string Name;

		public int Value;

		public ulong NetworkId;

		public bool IsCollected;

		public ScrapData(GrabbableObject obj)
		{
			Name = obj.itemProperties.itemName;
			Value = obj.scrapValue;
			NetworkId = ((NetworkBehaviour)obj).NetworkObjectId;
			IsCollected = false;
		}
	}
	[HarmonyPatch(typeof(RoundManager))]
	public static class QuestManager
	{
		[HarmonyPatch("SyncScrapValuesClientRpc")]
		[HarmonyPostfix]
		public static void GenerateDailyQuest(NetworkObjectReference[] spawnedScrap, int[] allScrapValue)
		{
			if (NetworkManager.Singleton.IsServer)
			{
				switch (QuestType.Scrap)
				{
				case QuestType.Scrap:
					ScrapManager.ScanMapForScraps();
					GenerateScrapQuest();
					break;
				case QuestType.Kill:
					LethalQuesting.mls.LogInfo((object)"Kill Quest!");
					break;
				}
			}
		}

		private static void GenerateScrapQuest()
		{
			List<ScrapData> todaysScraps = ScrapManager.TodaysScraps;
			if (todaysScraps != null && todaysScraps.Count != 0)
			{
				int mode = Random.Range(0, 3);
				string[] array = ScrapManager.ChooseScrapForQuest(todaysScraps, mode).Split('#');
				if (!(array[0] == "None"))
				{
					QuestData questData = default(QuestData);
					questData.Type = QuestType.Scrap;
					questData.TargetName = array[0];
					questData.TargetCount = int.Parse(array[1]);
					questData.CurrentCount = 0;
					questData.Reward = CalculateReward(int.Parse(array[1]), int.Parse(array[2]));
					questData.IsRewardGiven = false;
					QuestData value = questData;
					LethalQuesting.ScrapQuestData.Value = value;
					UpdateQuests.UpdateTheQuests(LethalQuesting.ScrapQuestData.Value);
					LethalQuesting.mls.LogInfo((object)("New Scrap Quest Generated: " + value.TargetName));
				}
			}
		}

		private static int CalculateReward(int count, int totalValue)
		{
			return (int)((10.0 + (double)totalValue / 2.0) * (0.8 + (double)count / 5.0));
		}
	}
	public static class ScrapManager
	{
		[HarmonyPatch(typeof(GrabbableObject), "DiscardItem")]
		public static class ScrapCollectPatch
		{
			[HarmonyPostfix]
			private static void Postfix(GrabbableObject __instance)
			{
				if (__instance.itemProperties.isScrap && __instance.isInShipRoom)
				{
					LethalQuesting.ItemDroppedMessage.SendServer(((NetworkBehaviour)__instance).NetworkObjectId);
				}
			}
		}

		public static List<ScrapData> TodaysScraps = new List<ScrapData>();

		private static void Postfix()
		{
		}

		public static void ScanMapForScraps()
		{
			TodaysScraps.Clear();
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			GrabbableObject[] array2 = array;
			foreach (GrabbableObject val in array2)
			{
				if (!((Object)(object)val == (Object)null) && val.itemProperties.isScrap)
				{
					NetworkObject component = ((Component)val).GetComponent<NetworkObject>();
					if (!((Object)(object)component == (Object)null) && component.IsSpawned && !val.isInShipRoom && !val.isHeld)
					{
						TodaysScraps.Add(new ScrapData(val));
						LethalQuesting.mls.LogInfo((object)("Added " + val.itemProperties.itemName));
					}
				}
			}
			LethalQuesting.mls.LogInfo((object)$"Map scanned, {TodaysScraps.Count} scraps found.");
		}

		public static string ChooseScrapForQuest(List<ScrapData> TodaysScraps, int mode)
		{
			if (TodaysScraps == null || TodaysScraps.Count == 0)
			{
				return "None#0";
			}
			string finalName = "";
			int num = 0;
			int num2 = 0;
			switch (mode)
			{
			case 0:
			{
				var anon2 = (from s in TodaysScraps
					group s by s.Name into g
					orderby g.Count() descending
					select new
					{
						Name = g.Key,
						Count = g.Count()
					}).FirstOrDefault();
				if (anon2 != null)
				{
					finalName = anon2.Name;
					num = anon2.Count;
				}
				break;
			}
			case 1:
			{
				ScrapData scrapData = TodaysScraps.OrderByDescending((ScrapData s) => s.Value).FirstOrDefault();
				if (scrapData != null)
				{
					finalName = scrapData.Name;
					num = TodaysScraps.Count((ScrapData s) => s.Name == finalName);
				}
				break;
			}
			default:
			{
				var anon = (from s in TodaysScraps
					group s by s.Name into g
					orderby Random.value
					select new
					{
						Name = g.Key,
						Count = g.Count()
					}).FirstOrDefault();
				if (anon != null)
				{
					finalName = anon.Name;
					num = anon.Count;
				}
				break;
			}
			}
			if (!string.IsNullOrEmpty(finalName))
			{
				num2 = TodaysScraps.Where((ScrapData s) => s.Name == finalName).Sum((ScrapData s) => s.Value);
			}
			return (!string.IsNullOrEmpty(finalName)) ? $"{finalName}#{num}#{num2}" : "None#0#0";
		}

		public static void CheckScrapCollection(ulong netId)
		{
			if (((NetworkBehaviour)RoundManager.Instance).IsServer)
			{
				ScrapData scrapData = TodaysScraps.FirstOrDefault((ScrapData s) => s.NetworkId == netId);
				if (scrapData != null && !scrapData.IsCollected && scrapData.Name == LethalQuesting.ScrapQuestData.Value.TargetName)
				{
					scrapData.IsCollected = true;
					QuestData value = LethalQuesting.ScrapQuestData.Value;
					value.CurrentCount++;
					LethalQuesting.ScrapQuestData.Value = value;
				}
			}
		}
	}
	[BepInPlugin("com.fighter.lethalquesting", "Lethal Questing", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class LethalQuesting : BaseUnityPlugin
	{
		public static LNetworkVariable<QuestData> ScrapQuestData = LNetworkVariable<QuestData>.Create("ScrapQuestData", default(QuestData), (LNetworkVariableWritePerms)0, (Action<QuestData, QuestData>)null);

		public static LNetworkMessage<ulong> ItemDroppedMessage = LNetworkMessage<ulong>.Create("ItemDropped", (Action<ulong, ulong>)null, (Action<ulong>)null, (Action<ulong, ulong>)null);

		public static TextMeshProUGUI myCustomText;

		public static ManualLogSource mls;

		private void Awake()
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			mls = Logger.CreateLogSource("LQuesting");
			ScrapQuestData.OnValueChanged += delegate(QuestData oldVal, QuestData newVal)
			{
				UpdateQuests.UpdateTheQuests(newVal);
			};
			ItemDroppedMessage.OnServerReceived += delegate(ulong objId, ulong senderId)
			{
				if (NetworkManager.Singleton.IsServer)
				{
					ScrapManager.CheckScrapCollection(objId);
				}
			};
			Harmony val = new Harmony("com.fighter.lethalquesting");
			val.PatchAll();
			mls.LogInfo((object)"Lethal Questing loaded!");
		}

		public static void CreateTextOnHUD()
		{
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)myCustomText != (Object)null)
			{
				return;
			}
			GameObject val = GameObject.Find("Systems/UI/Canvas");
			if ((Object)(object)val == (Object)null)
			{
				Canvas val2 = Object.FindObjectOfType<Canvas>();
				if ((Object)(object)val2 != (Object)null)
				{
					val = ((Component)val2).gameObject;
				}
			}
			if (!((Object)(object)val == (Object)null))
			{
				GameObject val3 = new GameObject("quest_text");
				val3.transform.SetParent(val.transform, false);
				val3.layer = 5;
				myCustomText = val3.AddComponent<TextMeshProUGUI>();
				if ((Object)(object)HUDManager.Instance != (Object)null && HUDManager.Instance.controlTipLines.Length != 0)
				{
					((TMP_Text)myCustomText).font = ((TMP_Text)HUDManager.Instance.controlTipLines[0]).font;
					((TMP_Text)myCustomText).fontSharedMaterial = ((TMP_Text)HUDManager.Instance.controlTipLines[0]).fontSharedMaterial;
				}
				((TMP_Text)myCustomText).text = "Welcome, land on a moon to see the quests, enjoy the ride!";
				((TMP_Text)myCustomText).fontSize = 16f;
				((Graphic)myCustomText).color = new Color(1f, 1f, 1f);
				((TMP_Text)myCustomText).alignment = (TextAlignmentOptions)516;
				RectTransform component = val3.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(1f, 1f);
				component.anchorMax = new Vector2(1f, 1f);
				component.pivot = new Vector2(1f, 1f);
				component.anchoredPosition = new Vector2(-10f, -60f);
				component.sizeDelta = new Vector2(260f, 360f);
				val3.transform.SetAsLastSibling();
			}
		}

		public static void UpdateQuestLog(string Quests)
		{
			if ((Object)(object)myCustomText == (Object)null)
			{
				CreateTextOnHUD();
			}
			((Component)myCustomText).gameObject.SetActive(true);
			((TMP_Text)myCustomText).transform.SetAsLastSibling();
			((TMP_Text)myCustomText).text = Quests ?? "";
			mls.LogInfo((object)"Quest log updated!");
		}

		public static string IncrementStringNumber(string input)
		{
			if (int.TryParse(input, out var result))
			{
				return (result + 1).ToString();
			}
			return input;
		}
	}
	[HarmonyPatch(typeof(HUDManager), "Start")]
	public class HUDManagerPatch
	{
		[HarmonyPostfix]
		private static void ShowHUD()
		{
			LethalQuesting.CreateTextOnHUD();
		}
	}
	public class UpdateQuests
	{
		public static void UpdateTheQuests(QuestData data)
		{
			if (data.Type != 0)
			{
				string text = (data.IsRewardGiven ? "green" : "orange");
				string text2 = "";
				text2 = data.Type switch
				{
					QuestType.Scrap => $"Collect {data.TargetName}: {data.CurrentCount}/{data.TargetCount}", 
					QuestType.Kill => $"Exterminate {data.TargetName}: {data.CurrentCount}/{data.TargetCount}", 
					_ => $"{data.Title}: {data.CurrentCount}/{data.TargetCount}", 
				};
				string quests = "<color=" + text + ">" + text2 + "</color>\n" + string.Format("<color={0}>Reward: ■{1} {2}", text, data.Reward, data.IsRewardGiven ? "(Paid)" : "");
				LethalQuesting.UpdateQuestLog(quests);
				if (NetworkManager.Singleton.IsServer && data.CurrentCount >= data.TargetCount && !data.IsRewardGiven)
				{
					GiveReward(data);
				}
			}
		}

		private static void GiveReward(QuestData data)
		{
			Terminal val = Object.FindObjectOfType<Terminal>();
			if ((Object)(object)val != (Object)null)
			{
				val.groupCredits += data.Reward;
				val.SyncGroupCreditsClientRpc(val.groupCredits, val.numberOfItemsInDropship);
				data.IsRewardGiven = true;
				LethalQuesting.ScrapQuestData.Value = data;
				LethalQuesting.mls.LogInfo((object)$"[REWARD] {data.Reward} credits added to terminal.");
			}
		}
	}
}