Skip to content

Commit

Permalink
fix: dynamically lookup PieceCategory.Max
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmoecker committed Apr 23, 2024
1 parent 00906b2 commit b9bd3ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion JotunnLib/Configs/PieceTableConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Jotunn.Utils;
using UnityEngine;

namespace Jotunn.Configs
Expand Down Expand Up @@ -42,7 +43,7 @@ public string[] GetCategories()

if (UseCategories)
{
for (int i = 0; i < (int)Piece.PieceCategory.Max; i++)
for (int i = 0; i < PieceUtils.MaxPieceCategory; i++)
{
categories.Add(Enum.GetName(typeof(Piece.PieceCategory), i));
}
Expand Down
2 changes: 1 addition & 1 deletion JotunnLib/Entities/CustomPieceTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CustomPieceTable(GameObject pieceTablePrefab) : base(Assembly.GetCallingA
if (PieceTable != null && PieceTable.m_useCategories)
{
List<string> categories = new List<string>();
for (int i = 0; i < (int)Piece.PieceCategory.Max; i++)
for (int i = 0; i < PieceUtils.MaxPieceCategory; i++)
{
categories.Add(Enum.GetName(typeof(Piece.PieceCategory), i));
}
Expand Down
2 changes: 1 addition & 1 deletion JotunnLib/Managers/PieceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ private void ReloadKnownRecipes(Player self)

private static IEnumerable<CodeInstruction> TranspileMaxCategory(IEnumerable<CodeInstruction> instructions, int maxOffset)
{
int number = (int)Piece.PieceCategory.Max + maxOffset;
int number = PieceUtils.MaxPieceCategory + maxOffset;

foreach (CodeInstruction instruction in instructions)
{
Expand Down
24 changes: 24 additions & 0 deletions JotunnLib/Utils/PieceUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;

namespace Jotunn.Utils
{
internal static class PieceUtils
{
public static int MaxPieceCategory { get; } = GetMaxPieceCategory();

private static int GetMaxPieceCategory()
{
var indexOfMax = Array.IndexOf(Enum.GetNames(typeof(Piece.PieceCategory)), nameof(Piece.PieceCategory.Max));
var values = Enum.GetValues(typeof(Piece.PieceCategory)).Cast<int>().ToArray();

if (indexOfMax >= 0)
{
return values[indexOfMax];
}

Logger.LogWarning("Could not find Piece.PieceCategory.Max, using fallback value 4");
return 4;
}
}
}

0 comments on commit b9bd3ee

Please sign in to comment.