From 200bb6981f07499effc3108502dcfbaf4d190766 Mon Sep 17 00:00:00 2001 From: Nebby <78170922+Nebby1999@users.noreply.github.com> Date: Mon, 7 Mar 2022 13:56:19 -0300 Subject: [PATCH] Added SerializableDifficultyDef (#353) * Added SerializableDifficultyDef * Fixed dumb thing from content additions * MethodImpl --- R2API/ContentManagement/ContentAddition.cs | 6 ++-- R2API/DifficultyAPI.cs | 13 +++++++++ R2API/EliteAPI.cs | 8 ++++- R2API/ItemAPI.cs | 17 +++++++++-- .../SerializableDifficultyDef.cs | 29 +++++++++++++++++++ 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 R2API/ScriptableObjects/SerializableDifficultyDef.cs diff --git a/R2API/ContentManagement/ContentAddition.cs b/R2API/ContentManagement/ContentAddition.cs index ab08a412..9a988351 100644 --- a/R2API/ContentManagement/ContentAddition.cs +++ b/R2API/ContentManagement/ContentAddition.cs @@ -222,7 +222,7 @@ public static bool AddItemDef(ItemDef itemDef) { if (CatalogBlockers.GetAvailability()) { R2API.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {itemDef} via {nameof(ContentAddition)}.{nameof(AddItemDef)}()" + $"The assembly should ideally add them via {nameof(ItemAPI)} so that they can use ItemAPI's IDRS systems, adding anyways."); - ItemAPI.Add(new CustomItem(itemDef, Array.Empty())); + ItemAPI.AddInternal(new CustomItem(itemDef, Array.Empty()), asm); return true; } RejectContent(itemDef, asm, "ItemDef", "but the ItemCatalog has already initialized!"); @@ -289,7 +289,7 @@ public static bool AddEquipmentDef(EquipmentDef equipmentDef) { if (CatalogBlockers.GetAvailability()) { R2API.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {equipmentDef} via {nameof(ContentAddition)}.{nameof(AddEquipmentDef)}()" + $"The assembly should ideally add them via {nameof(ItemAPI)} so that they can use ItemAPI's IDRS systems, adding anyways."); - ItemAPI.Add(new CustomEquipment(equipmentDef, Array.Empty())); + ItemAPI.AddInternal(new CustomEquipment(equipmentDef, Array.Empty()), asm); return true; } RejectContent(equipmentDef, asm, "EquipmentDef", "but the EquipmnetCatalog has already initialized!"); @@ -329,7 +329,7 @@ public static bool AddEliteDef(EliteDef eliteDef) { if (CatalogBlockers.GetAvailability()) { R2API.Logger.LogInfo($"Assembly {asm.GetName().Name} is adding an {eliteDef} via {nameof(ContentAddition)}.{nameof(AddEliteDef)}()" + $"The assembly should ideally add them via {nameof(EliteAPI)} so that they can use EliteAPI's elite tier systems, adding the elite anyways as Tier1 elite."); - EliteAPI.Add(new CustomElite(eliteDef, new List { EliteAPI.VanillaEliteTiers[1], EliteAPI.VanillaEliteTiers[2] })); + EliteAPI.AddInternal(new CustomElite(eliteDef, new List { EliteAPI.VanillaEliteTiers[1], EliteAPI.VanillaEliteTiers[2] }), asm); return true; } RejectContent(eliteDef, asm, "EliteDef", "but the EliteCatalog has already initialized!"); diff --git a/R2API/DifficultyAPI.cs b/R2API/DifficultyAPI.cs index 86ad9817..7242b6b5 100644 --- a/R2API/DifficultyAPI.cs +++ b/R2API/DifficultyAPI.cs @@ -1,3 +1,4 @@ +using R2API.ScriptableObjects; using R2API.Utils; using RoR2; using System; @@ -93,6 +94,18 @@ public static DifficultyIndex AddDifficulty(DifficultyDef? difficulty, bool pref return pendingIndex; } + /// + /// Add a DifficultyDef to the list of available difficulties using a SerializableDifficultyDef + /// This must be called before the DifficultyCatalog inits, so before plugin.Start() + /// You can get the DifficultyIndex from the SerializableDifficultyDef's DifficultyIndex property + /// If this is called after the DifficultyCatalog inits, then the DifficultyIndex will return -1/DifficultyIndex.Invalid and ignore the difficulty + /// + /// The SerializableDifficultyDef from which to create the DifficultyDef + public static void AddDifficulty(SerializableDifficultyDef serializableDifficultyDef) { + serializableDifficultyDef.CreateDifficultyDef(); + serializableDifficultyDef.DifficultyIndex = AddDifficulty(serializableDifficultyDef.DifficultyDef, serializableDifficultyDef.preferPositiveIndex); + } + [R2APISubmoduleInit(Stage = InitStage.SetHooks)] internal static void SetHooks() { DifficultyCatalogReady?.Invoke(null, null); diff --git a/R2API/EliteAPI.cs b/R2API/EliteAPI.cs index d6dfea9c..6675a2d7 100644 --- a/R2API/EliteAPI.cs +++ b/R2API/EliteAPI.cs @@ -7,6 +7,7 @@ using System.Collections.ObjectModel; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using UnityEngine; // ReSharper disable MemberCanBePrivate.Global @@ -115,7 +116,12 @@ private static void RetrieveVanillaEliteTierCount(ILContext il) { /// /// The elite to add. /// true if added, false otherwise + [MethodImpl(MethodImplOptions.NoInlining)] public static bool Add(CustomElite? elite) { + return AddInternal(elite, Assembly.GetCallingAssembly()); + } + + internal static bool AddInternal(CustomElite elite, Assembly addingAssembly) { if (!Loaded) { throw new InvalidOperationException($"{nameof(EliteAPI)} is not loaded. Please use [{nameof(R2APISubmoduleDependency)}(nameof({nameof(EliteAPI)})]"); } @@ -130,7 +136,7 @@ public static bool Add(CustomElite? elite) { } - R2APIContentManager.HandleContentAddition(Assembly.GetCallingAssembly(), elite.EliteDef); + R2APIContentManager.HandleContentAddition(addingAssembly, elite.EliteDef); EliteDefinitions.Add(elite); return true; } diff --git a/R2API/ItemAPI.cs b/R2API/ItemAPI.cs index 9d12b245..2674feeb 100644 --- a/R2API/ItemAPI.cs +++ b/R2API/ItemAPI.cs @@ -8,6 +8,7 @@ using System.Collections.ObjectModel; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Xml.Linq; using UnityEngine; using Object = UnityEngine.Object; @@ -63,7 +64,13 @@ internal static void UnsetHooks() { /// /// The item to add. /// true if added, false otherwise + [MethodImpl(MethodImplOptions.NoInlining)] public static bool Add(CustomItem? item) { + return AddInternal(item, Assembly.GetCallingAssembly()); + } + + internal static bool AddInternal(CustomItem item, Assembly addingAssembly) { + if (!Loaded) { throw new InvalidOperationException($"{nameof(ItemAPI)} is not loaded. Please use [{nameof(R2APISubmoduleDependency)}(nameof({nameof(ItemAPI)})]"); } @@ -100,11 +107,12 @@ public static bool Add(CustomItem? item) { R2API.Logger.LogError($"Custom item '{item.ItemDef.name}' is not XMLsafe. Item not added."); } if (xmlSafe) { - R2APIContentManager.HandleContentAddition(Assembly.GetCallingAssembly(), item.ItemDef); + R2APIContentManager.HandleContentAddition(addingAssembly, item.ItemDef); return true; } return false; + } /// @@ -115,7 +123,12 @@ public static bool Add(CustomItem? item) { /// /// The equipment item to add. /// true if added, false otherwise + [MethodImpl(MethodImplOptions.NoInlining)] public static bool Add(CustomEquipment? item) { + return AddInternal(item, Assembly.GetCallingAssembly()); + } + + internal static bool AddInternal(CustomEquipment item, Assembly addingAssembly) { if (!Loaded) { throw new InvalidOperationException($"{nameof(ItemAPI)} is not loaded. Please use [{nameof(R2APISubmoduleDependency)}(nameof({nameof(ItemAPI)})]"); } @@ -152,7 +165,7 @@ public static bool Add(CustomEquipment? item) { R2API.Logger.LogError($"Custom equipment '{item.EquipmentDef.name}' is not XMLsafe. Item not added."); } if (xmlSafe) { - R2APIContentManager.HandleContentAddition(Assembly.GetCallingAssembly(), item.EquipmentDef); + R2APIContentManager.HandleContentAddition(addingAssembly, item.EquipmentDef); return true; } diff --git a/R2API/ScriptableObjects/SerializableDifficultyDef.cs b/R2API/ScriptableObjects/SerializableDifficultyDef.cs new file mode 100644 index 00000000..d67b4c13 --- /dev/null +++ b/R2API/ScriptableObjects/SerializableDifficultyDef.cs @@ -0,0 +1,29 @@ +using RoR2; +using UnityEngine; + +namespace R2API.ScriptableObjects { + [CreateAssetMenu(fileName = "new SerializableDifficultyDef", menuName = "R2API/SerializableDifficultyDef")] + public class SerializableDifficultyDef : ScriptableObject { + [Tooltip("Scaling value of the difficulty, Drizzle is 1, Rainstorm is 2, Monsoon is 3")] + public float scalingValue; + public string descriptionToken; + public string nameToken; + [Tooltip("Unique identifier for this Difficulty")] + public string serverTag; + [Tooltip("If true, beating the game on this difficulty will unlock the survivor's Mastery skin")] + public bool countsAsHardMode; + [Tooltip("If set to true, the difficulty index will be a possitive number, this causes the difficulty to have all the Eclipse modifiers (From 1 to 8)")] + public bool preferPositiveIndex = false; + public Color difficultyColor; + public Sprite iconSprite; + + public DifficultyDef DifficultyDef { get; private set; } + public DifficultyIndex DifficultyIndex { get; internal set; } + + internal void CreateDifficultyDef() { + DifficultyDef = new DifficultyDef(scalingValue, nameToken, string.Empty, descriptionToken, difficultyColor, serverTag, countsAsHardMode); + DifficultyDef.foundIconSprite = true; + DifficultyDef.iconSprite = iconSprite; + } + } +}