Skip to content

Commit

Permalink
Added SerializableDifficultyDef (#353)
Browse files Browse the repository at this point in the history
* Added SerializableDifficultyDef

* Fixed dumb thing from content additions

* MethodImpl
  • Loading branch information
Nebby1999 authored Mar 7, 2022
1 parent 82ed204 commit 200bb69
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
6 changes: 3 additions & 3 deletions R2API/ContentManagement/ContentAddition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public static bool AddItemDef(ItemDef itemDef) {
if (CatalogBlockers.GetAvailability<ItemDef>()) {
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<ItemDisplayRule>()));
ItemAPI.AddInternal(new CustomItem(itemDef, Array.Empty<ItemDisplayRule>()), asm);
return true;
}
RejectContent(itemDef, asm, "ItemDef", "but the ItemCatalog has already initialized!");
Expand Down Expand Up @@ -289,7 +289,7 @@ public static bool AddEquipmentDef(EquipmentDef equipmentDef) {
if (CatalogBlockers.GetAvailability<EquipmentDef>()) {
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<ItemDisplayRule>()));
ItemAPI.AddInternal(new CustomEquipment(equipmentDef, Array.Empty<ItemDisplayRule>()), asm);
return true;
}
RejectContent(equipmentDef, asm, "EquipmentDef", "but the EquipmnetCatalog has already initialized!");
Expand Down Expand Up @@ -329,7 +329,7 @@ public static bool AddEliteDef(EliteDef eliteDef) {
if (CatalogBlockers.GetAvailability<EliteDef>()) {
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<CombatDirector.EliteTierDef> { EliteAPI.VanillaEliteTiers[1], EliteAPI.VanillaEliteTiers[2] }));
EliteAPI.AddInternal(new CustomElite(eliteDef, new List<CombatDirector.EliteTierDef> { EliteAPI.VanillaEliteTiers[1], EliteAPI.VanillaEliteTiers[2] }), asm);
return true;
}
RejectContent(eliteDef, asm, "EliteDef", "but the EliteCatalog has already initialized!");
Expand Down
13 changes: 13 additions & 0 deletions R2API/DifficultyAPI.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using R2API.ScriptableObjects;
using R2API.Utils;
using RoR2;
using System;
Expand Down Expand Up @@ -93,6 +94,18 @@ public static DifficultyIndex AddDifficulty(DifficultyDef? difficulty, bool pref
return pendingIndex;
}

/// <summary>
/// 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
/// </summary>
/// <param name="serializableDifficultyDef">The SerializableDifficultyDef from which to create the DifficultyDef</param>
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);
Expand Down
8 changes: 7 additions & 1 deletion R2API/EliteAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;

// ReSharper disable MemberCanBePrivate.Global
Expand Down Expand Up @@ -115,7 +116,12 @@ private static void RetrieveVanillaEliteTierCount(ILContext il) {
/// </summary>
/// <param name="elite">The elite to add.</param>
/// <returns>true if added, false otherwise</returns>
[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)})]");
}
Expand All @@ -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;
}
Expand Down
17 changes: 15 additions & 2 deletions R2API/ItemAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,7 +64,13 @@ internal static void UnsetHooks() {
/// </summary>
/// <param name="item">The item to add.</param>
/// <returns>true if added, false otherwise</returns>
[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)})]");
}
Expand Down Expand Up @@ -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;

}

/// <summary>
Expand All @@ -115,7 +123,12 @@ public static bool Add(CustomItem? item) {
/// </summary>
/// <param name="item">The equipment item to add.</param>
/// <returns>true if added, false otherwise</returns>
[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)})]");
}
Expand Down Expand Up @@ -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;
}

Expand Down
29 changes: 29 additions & 0 deletions R2API/ScriptableObjects/SerializableDifficultyDef.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 200bb69

Please sign in to comment.