Decompiled source of UltraAchievements Library v1.0.0

plugins/UltraAchievements-Library/UltraAchievements_Lib.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using GameConsole;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("UltraAchievements_Lib")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+4e844d77d613a2dbf415678293a7d158518fa46c")]
[assembly: AssemblyProduct("UltraAchievements_Lib")]
[assembly: AssemblyTitle("UltraAchievements_Lib")]
[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 UltraAchievements_Lib
{
	public class AchievementBehaviour : MonoBehaviour
	{
		private float _endTimer = 0f;

		private float _startTimer = 0f;

		private float _animTimer;

		private bool _endTimerStarted = false;

		private bool _startTimerStarted = false;

		private RectTransform _rect;

		private void Start()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			_rect = ((Component)this).GetComponent<RectTransform>();
			_rect.anchoredPosition = new Vector2(1170f, -414f);
			_startTimerStarted = true;
		}

		private void Update()
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			if (_startTimerStarted)
			{
				_startTimer += Time.deltaTime;
				_animTimer += Time.deltaTime * 1.35f;
				if (_animTimer >= 1f)
				{
					_animTimer = 1f;
				}
				_rect.anchoredPosition = new Vector2(Mathf.Lerp(1170f, 760f, _animTimer), -414f);
				if (_startTimer >= 5f)
				{
					_animTimer = 0f;
					_endTimerStarted = true;
					_startTimerStarted = false;
				}
			}
			if (_endTimerStarted)
			{
				_endTimer += Time.deltaTime;
				_animTimer += Time.deltaTime * 1.35f;
				if (_animTimer >= 1f)
				{
					_animTimer = 1f;
				}
				_rect.anchoredPosition = new Vector2(Mathf.Lerp(780f, 1170f, _animTimer), -414f);
				if (_endTimer >= 5f)
				{
					_endTimerStarted = false;
					Object.Destroy((Object)(object)((Component)this).transform.parent);
				}
			}
		}
	}
	public class AchievementHolder : MonoBehaviour
	{
		public Text title;

		public Image icon;

		public Text description;
	}
	public class AchievementInfo : ScriptableObject
	{
		public string id;

		public string achName;

		public Sprite icon;

		[TextArea]
		public string description;

		public GameObject holderTemplate;

		public bool isHidden;

		public bool isProgressive;

		public int maxProgress;

		[HideInInspector]
		public bool isCompleted;

		[HideInInspector]
		public int progress;
	}
	public static class AchievementManager
	{
		private static readonly Dictionary<Type, AchievementInfo> TypeToAchInfo = new Dictionary<Type, AchievementInfo>();

		public static readonly Dictionary<string, AchievementInfo> IdToAchInfo = new Dictionary<string, AchievementInfo>();

		public static AchievementInfo CurrentInfo;

		private static string[] AllSaveLines
		{
			get
			{
				if (File.Exists(Plugin.SavePath))
				{
					return File.ReadAllLines(Plugin.SavePath);
				}
				File.Create(Plugin.SavePath);
				return Array.Empty<string>();
			}
		}

		private static string[] AllProgLines
		{
			get
			{
				if (File.Exists(Plugin.ProgSavePath))
				{
					return File.ReadAllLines(Plugin.ProgSavePath);
				}
				File.Create(Plugin.ProgSavePath);
				return Array.Empty<string>();
			}
		}

		public static void MarkAchievementComplete(AchievementInfo achInfo)
		{
			if ((Object)(object)achInfo == (Object)null)
			{
				MonoSingleton<Console>.Instance.ProcessInput("Achievement does not exist, please check that the id matches");
			}
			else if (!achInfo.isCompleted)
			{
				achInfo.isCompleted = true;
				if (IdToAchInfo.TryGetValue(achInfo.id, out var value))
				{
					value.isCompleted = true;
				}
				if ((Object)(object)achInfo.holderTemplate != (Object)null)
				{
					GameObject val = Object.Instantiate<GameObject>(achInfo.holderTemplate, CreateOverlay().transform, true);
					AchievementHolder component = val.GetComponent<AchievementHolder>();
					((Component)component.description).GetComponent<Text>().text = achInfo.description;
					((Component)component.title).GetComponent<Text>().text = achInfo.achName;
					component.icon.sprite = achInfo.icon;
					val.AddComponent<AchievementBehaviour>();
				}
				SaveToFile(achInfo);
			}
		}

		private static void SaveToFile(AchievementInfo achInfo)
		{
			if (!File.Exists(Plugin.SavePath))
			{
				File.Create(Plugin.SavePath);
				using StreamWriter streamWriter = new StreamWriter(Plugin.SavePath);
				streamWriter.WriteLine(achInfo.id ?? "");
				streamWriter.Close();
				return;
			}
			if (File.Exists(Plugin.SavePath))
			{
				using (StreamWriter streamWriter2 = new StreamWriter(Plugin.SavePath, append: true))
				{
					streamWriter2.WriteLine(achInfo.id ?? "");
					streamWriter2.Close();
				}
			}
		}

		private static void SaveProgAchievement(AchievementInfo achInfo)
		{
			if (!File.Exists(Plugin.ProgSavePath))
			{
				File.Create(Plugin.ProgSavePath);
				using StreamWriter streamWriter = new StreamWriter(Plugin.ProgSavePath);
				streamWriter.WriteLine($"{achInfo.id} - {achInfo.progress}");
				streamWriter.Close();
				return;
			}
			if (!File.Exists(Plugin.ProgSavePath))
			{
				return;
			}
			List<string> list = new List<string>(File.ReadAllLines(Plugin.ProgSavePath));
			for (int i = 0; i < list.Count; i++)
			{
				string text = list[i];
				if (text.Contains(achInfo.id))
				{
					list[i] = $"{achInfo.id} - {achInfo.progress}";
					File.WriteAllLines(Plugin.ProgSavePath, list);
					return;
				}
			}
			using StreamWriter streamWriter2 = new StreamWriter(Plugin.ProgSavePath, append: true);
			streamWriter2.WriteLine($"{achInfo.id} - {achInfo.progress}");
			streamWriter2.Close();
		}

		public static void SaveAllProgAchievements()
		{
			foreach (AchievementInfo value in TypeToAchInfo.Values)
			{
				if (value.isProgressive)
				{
					SaveProgAchievement(value);
				}
			}
		}

		public static void RegisterAchievement(Type ach)
		{
			string iD = ach.GetCustomAttribute<RegisterAchievementAttribute>().ID;
			if (IdToAchInfo.TryGetValue(iD, out var value))
			{
				TypeToAchInfo.Add(ach, value);
			}
		}

		public static void RegisterAllAchievements(Assembly asm)
		{
			IEnumerable<Type> enumerable = asm.GetTypes().Where(IsPossibleAchievement);
			foreach (Type item in enumerable)
			{
				RegisterAchievement(item);
			}
		}

		public static void RegisterAchievementInfos(IEnumerable<AchievementInfo> infos)
		{
			foreach (AchievementInfo info in infos)
			{
				string[] allSaveLines = AllSaveLines;
				foreach (string text in allSaveLines)
				{
					if (info.id == text)
					{
						info.isCompleted = true;
					}
				}
				string[] allProgLines = AllProgLines;
				foreach (string text2 in allProgLines)
				{
					if (text2.Contains(info.id) && info.isProgressive)
					{
						info.progress = Convert.ToInt32(text2.Split(new char[1] { '-' })[1]);
					}
				}
				IdToAchInfo.Add(info.id, info);
			}
		}

		private static bool IsPossibleAchievement(Type type)
		{
			if (type.IsInterface)
			{
				return false;
			}
			if (type.IsAbstract)
			{
				return false;
			}
			return type.GetCustomAttribute<RegisterAchievementAttribute>() != null;
		}

		public static AchievementInfo GetAchievementInfo(Type type)
		{
			TypeToAchInfo.TryGetValue(type, out var value);
			return value;
		}

		public static AchievementInfo GetAchievementInfo(string id)
		{
			IdToAchInfo.TryGetValue(id, out var value);
			return value;
		}

		private static GameObject CreateOverlay()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected O, but got Unknown
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject();
			((Object)val).name = "Achievement Overlay";
			val.AddComponent<Canvas>();
			val.GetComponent<Canvas>().renderMode = (RenderMode)0;
			val.GetComponent<Canvas>().sortingOrder = 1000;
			val.AddComponent<CanvasScaler>();
			val.AddComponent<GraphicRaycaster>();
			val.GetComponent<CanvasScaler>().uiScaleMode = (ScaleMode)1;
			val.GetComponent<CanvasScaler>().screenMatchMode = (ScreenMatchMode)0;
			val.GetComponent<CanvasScaler>().matchWidthOrHeight = 0f;
			val.GetComponent<CanvasScaler>().referenceResolution = new Vector2(1920f, 1080f);
			Object.DontDestroyOnLoad((Object)(object)val);
			return val;
		}
	}
	[BepInPlugin("protract.ultrakill.ultra_achievements_lib", "UltraAchievementsLib", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static readonly string SavePath = Path.Combine(Application.persistentDataPath, "achList.txt");

		public static readonly string ProgSavePath = Path.Combine(Application.persistentDataPath, "achProgress.txt");

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("Protract.UltraAchievements_Lib");
			val.PatchAll();
			if (!File.Exists(SavePath))
			{
				File.Create(SavePath);
			}
			if (!File.Exists(ProgSavePath))
			{
				File.Create(ProgSavePath);
			}
		}

		private void OnDestroy()
		{
			AchievementManager.SaveAllProgAchievements();
		}
	}
	[AttributeUsage(AttributeTargets.Class, Inherited = false)]
	public sealed class RegisterAchievementAttribute : Attribute
	{
		public string ID { get; }

		public RegisterAchievementAttribute(string id)
		{
			ID = id;
		}
	}
}
namespace UltraAchievements_Lib.Commands
{
	[RegisterCommand]
	public class GiveAchievement : ICommand
	{
		public string Name => "Add item";

		public string Description => "Gives you an achievement based on the specified ID";

		public string Command => "ual_addach";

		public void Execute(Console con, string[] args)
		{
			if (args.Length != 0 && AchievementManager.IdToAchInfo.TryGetValue(args[0], out var value))
			{
				AchievementManager.MarkAchievementComplete(value);
			}
		}
	}
	[AttributeUsage(AttributeTargets.Class)]
	[HarmonyPatch]
	public class RegisterCommandAttribute : Attribute
	{
		[HarmonyPatch(typeof(Console), "Awake")]
		[HarmonyPostfix]
		private static void RegisterAll(Console __instance)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			foreach (TypeInfo definedType in Assembly.GetExecutingAssembly().DefinedTypes)
			{
				if (definedType.GetCustomAttribute<RegisterCommandAttribute>() != null)
				{
					__instance.RegisterCommand((ICommand)Activator.CreateInstance(definedType));
				}
			}
		}
	}
}