using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using Burst2Flame;
using HarmonyLib;
using TMPro;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("BetterStats")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Mostra os stats no formato \"base (combinado)\" na tela de personagem e na de level up.")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("BetterStats")]
[assembly: AssemblyTitle("BetterStats")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace BetterStats;
[BepInPlugin("com.gumatos.betterstats", "Better Stats", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Log { get; private set; }
private void Awake()
{
Log = ((BaseUnityPlugin)this).Logger;
((BaseUnityPlugin)this).Logger.LogInfo((object)"Better Stats carregado.");
AplicarPatches();
}
private static void AplicarPatches()
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Expected O, but got Unknown
Harmony val = new Harmony("com.gumatos.betterstats");
List<string> list = new List<string>();
int num = 0;
int num2 = 0;
int num3 = 0;
Type[] types;
try
{
types = typeof(Plugin).Assembly.GetTypes();
}
catch (Exception ex)
{
Log.LogError((object)("Better Stats: nao deu para listar os tipos do mod — nenhum gancho aplicado: " + ex));
return;
}
foreach (Type type in types)
{
if (EhClasseDeGancho(type))
{
num++;
try
{
int num4 = val.CreateClassProcessor(type).Patch()?.Count ?? 0;
num3 += num4;
num2++;
Log.LogInfo((object)("Better Stats: gancho aplicado — " + type.Name + " (" + num4 + " metodo(s) do jogo)."));
}
catch (Exception ex2)
{
list.Add(type.Name);
Log.LogError((object)("Better Stats: FALHA ao aplicar o gancho " + type.Name + " — " + ex2.Message));
}
}
}
string text = "Better Stats: patches Harmony aplicados (" + num2 + "/" + num + " ganchos, " + num3 + " metodos do jogo).";
if (list.Count == 0)
{
Log.LogInfo((object)text);
}
else
{
Log.LogError((object)(text + " GANCHOS QUE FALHARAM: " + string.Join(", ", list.ToArray()) + ". O mod continua de pe, mas a tela que dependia deles nao existe nesta sessao."));
}
}
private static bool EhClasseDeGancho(Type tipo)
{
try
{
if (tipo == null || !tipo.IsClass)
{
return false;
}
return tipo.GetCustomAttributes(typeof(HarmonyPatch), inherit: false).Length != 0;
}
catch (Exception)
{
return false;
}
}
}
[HarmonyPatch(typeof(InventoryManager), "UpdateStats")]
public static class InventoryStatPatch
{
private static bool _loggedBox;
private static bool _shiftedValues;
private static void Postfix(InventoryManager __instance, Character character)
{
try
{
if (character == null || (Object)(object)__instance == (Object)null || (Object)(object)__instance.AttributesValuesMainHolder == (Object)null)
{
return;
}
CharacterAttribute[] levelableCharacterAttributes = Game.Instance.LevelableCharacterAttributes;
CharacterAttribute[] levelableCharacterAttributesFinal = Game.Instance.LevelableCharacterAttributesFinal;
if (levelableCharacterAttributes == null || levelableCharacterAttributesFinal == null)
{
return;
}
ShiftValuesColumn(__instance);
int num = Math.Min(levelableCharacterAttributes.Length, levelableCharacterAttributesFinal.Length);
for (int i = 0; i < num; i++)
{
if (i < __instance.AttributesValuesMainHolder.childCount && !((Object)(object)levelableCharacterAttributes[i] == (Object)null) && !((Object)(object)levelableCharacterAttributesFinal[i] == (Object)null))
{
Transform child = __instance.AttributesValuesMainHolder.GetChild(i);
TextMeshProUGUI val = (((Object)(object)child != (Object)null) ? ((Component)child).GetComponent<TextMeshProUGUI>() : null);
if (!((Object)(object)val == (Object)null))
{
float baseValue = GetBaseValue(character, levelableCharacterAttributes[i]);
float combinedVal = Mathf.Ceil(character[((Object)levelableCharacterAttributesFinal[i]).name]);
((TMP_Text)val).enableWordWrapping = false;
((TMP_Text)val).text = BuildInventoryValue(val, baseValue, combinedVal);
LogBoxOnce(val, baseValue, combinedVal);
}
}
}
}
catch (Exception ex)
{
Plugin.Log.LogWarning((object)("Better Stats (personagem) erro: " + ex.Message));
}
}
private static void ShiftValuesColumn(InventoryManager mgr)
{
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
//IL_002d: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Unknown result type (might be due to invalid IL or missing references)
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
//IL_0053: Unknown result type (might be due to invalid IL or missing references)
//IL_005e: Unknown result type (might be due to invalid IL or missing references)
//IL_0070: Unknown result type (might be due to invalid IL or missing references)
if (_shiftedValues)
{
return;
}
_shiftedValues = true;
try
{
Transform attributesValuesMainHolder = mgr.AttributesValuesMainHolder;
RectTransform val = (RectTransform)(object)((attributesValuesMainHolder is RectTransform) ? attributesValuesMainHolder : null);
if (!((Object)(object)val == (Object)null))
{
Vector2 anchoredPosition = val.anchoredPosition;
val.anchoredPosition = new Vector2(anchoredPosition.x - 40f, anchoredPosition.y);
Plugin.Log.LogInfo((object)$"Better Stats: coluna de valores deslocada x {anchoredPosition.x:0.#} -> {anchoredPosition.x - 40f:0.#} (holderRect={val.rect})");
}
}
catch (Exception ex)
{
Plugin.Log.LogWarning((object)("Better Stats shift erro: " + ex.Message));
}
}
private static void LogBoxOnce(TextMeshProUGUI comp, float baseVal, float combinedVal)
{
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
//IL_006a: Unknown result type (might be due to invalid IL or missing references)
if (_loggedBox)
{
return;
}
_loggedBox = true;
try
{
float x = ((TMP_Text)comp).GetPreferredValues(baseVal.ToString("F0")).x;
float x2 = ((TMP_Text)comp).GetPreferredValues("(" + combinedVal.ToString("F0") + ")").x;
ManualLogSource log = Plugin.Log;
object[] array = new object[4];
Rect rect = ((TMP_Text)comp).rectTransform.rect;
array[0] = ((Rect)(ref rect)).width;
array[1] = ((TMP_Text)comp).fontSize;
array[2] = x;
array[3] = x2;
log.LogInfo((object)string.Format("Better Stats box: largura={0:0.#} fontSize={1:0.#} baseW={2:0.#} parenW={3:0.#}", array));
}
catch
{
}
}
internal static float GetBaseValue(Character character, CharacterAttribute baseAttr)
{
if (character.SavedMap != null && character.SavedMap.ContainsKey(baseAttr.Guid))
{
return Mathf.Ceil(character.SavedMap[baseAttr.Guid]);
}
return Mathf.Ceil(character[((Object)baseAttr).name]);
}
internal static string FormatBaseCombined(float baseVal, float combinedVal, int smallSize)
{
if (combinedVal == baseVal)
{
return baseVal.ToString("F0");
}
string text = "(" + combinedVal.ToString("F0") + ")";
if (smallSize > 0)
{
return baseVal.ToString("F0") + " <size=" + smallSize + ">" + text + "</size>";
}
return baseVal.ToString("F0") + " " + text;
}
internal static string BuildInventoryValue(TextMeshProUGUI comp, float baseVal, float combinedVal)
{
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: 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_0082: Unknown result type (might be due to invalid IL or missing references)
string text = baseVal.ToString("F0");
if (combinedVal == baseVal)
{
return text;
}
string text2 = "(" + combinedVal.ToString("F0") + ")";
float fontSize = ((TMP_Text)comp).fontSize;
if (fontSize <= 0f)
{
return text + " " + text2;
}
Rect rect = ((TMP_Text)comp).rectTransform.rect;
float width = ((Rect)(ref rect)).width;
float x = ((TMP_Text)comp).GetPreferredValues(text).x;
float x2 = ((TMP_Text)comp).GetPreferredValues(" ").x;
float x3 = ((TMP_Text)comp).GetPreferredValues(text2).x;
if (width <= 1f || x3 <= 0.01f)
{
return FormatBaseCombined(baseVal, combinedVal, Mathf.Max(6, Mathf.RoundToInt(fontSize * 0.7f)));
}
float num = Mathf.Clamp((width - x - x2) / x3, 0.35f, 1f);
int num2 = Mathf.Max(6, Mathf.RoundToInt(fontSize * num));
return text + " <size=" + num2 + ">" + text2 + "</size>";
}
}
[HarmonyPatch(typeof(RoguelikeManager), "UpdateAttributes")]
public static class RoguelikeStatPatch
{
private static void Postfix(RoguelikeManager __instance)
{
try
{
if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.StatValues == (Object)null)
{
return;
}
Character currentRoguelikeSkillSelectingCharacter = __instance.CurrentRoguelikeSkillSelectingCharacter;
if (currentRoguelikeSkillSelectingCharacter == null)
{
return;
}
CharacterAttribute[] levelableCharacterAttributes = Game.Instance.LevelableCharacterAttributes;
CharacterAttribute[] levelableCharacterAttributesFinal = Game.Instance.LevelableCharacterAttributesFinal;
if (levelableCharacterAttributes == null || levelableCharacterAttributesFinal == null)
{
return;
}
int num = Math.Min(levelableCharacterAttributes.Length, levelableCharacterAttributesFinal.Length);
int smallSize = ((((TMP_Text)__instance.StatValues).fontSize > 0f) ? Mathf.Max(6, Mathf.RoundToInt(((TMP_Text)__instance.StatValues).fontSize * 0.7f)) : 0);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < num; i++)
{
if (!((Object)(object)levelableCharacterAttributes[i] == (Object)null) && !((Object)(object)levelableCharacterAttributesFinal[i] == (Object)null))
{
float baseValue = InventoryStatPatch.GetBaseValue(currentRoguelikeSkillSelectingCharacter, levelableCharacterAttributes[i]);
float combinedVal = Mathf.Ceil(currentRoguelikeSkillSelectingCharacter[((Object)levelableCharacterAttributesFinal[i]).name]);
stringBuilder.Append(InventoryStatPatch.FormatBaseCombined(baseValue, combinedVal, smallSize)).Append('\n');
}
}
((TMP_Text)__instance.StatValues).text = stringBuilder.ToString();
}
catch (Exception ex)
{
Plugin.Log.LogWarning((object)("Better Stats (level up) erro: " + ex.Message));
}
}
}