Files
got-mod-strategicview-plus/Class1.cs
Jose Luis Montañes Ojados 7e598d765a add offsets dictionary
2026-01-06 15:29:51 +01:00

357 lines
13 KiB
C#

using BepInEx;
using HarmonyLib;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace StrategicMapPlus
{
[BepInPlugin("com.mod.strategicmapplus", "Strategic Map Plus", "28.0.0")]
public class StrategicMapMod : BaseUnityPlugin
{
private float timer = 0f;
private float interval = 0.5f;
private bool texturesLoaded = false;
public static Dictionary<string, Sprite> SpriteCache = new Dictionary<string, Sprite>();
private HashSet<string> reportedMissingKeys = new HashSet<string>();
void Awake()
{
Logger.LogInfo("Strategic Map Plus v28: Posición 0,0,0.");
}
void Update()
{
if (!texturesLoaded && Time.time > 3f) LoadTextures();
timer += Time.deltaTime;
if (timer < interval) return;
timer = 0f;
if (texturesLoaded) ScanStrategicMap();
}
void LoadTextures()
{
Texture2D[] allTextures = Resources.FindObjectsOfTypeAll<Texture2D>();
Dictionary<string, Texture2D> textureLookup = new Dictionary<string, Texture2D>();
foreach (var tex in allTextures)
{
if (tex != null && !string.IsNullOrEmpty(tex.name))
{
if (!textureLookup.ContainsKey(tex.name)) textureLookup.Add(tex.name, tex);
}
}
int loadedCount = 0;
foreach (var kvp in Textures.TextureMap)
{
string logicKey = kvp.Key;
string texName = kvp.Value;
if (textureLookup.ContainsKey(texName))
{
Texture2D tex = textureLookup[texName];
Textures.IconOffset offset = null;
if (Textures.IconsOffsets.ContainsKey(logicKey)) offset = Textures.IconsOffsets[logicKey];
Texture2D roundTex = MakeTextureTransparent(tex, offset);
Sprite s = Sprite.Create(roundTex, new Rect(0, 0, roundTex.width, roundTex.height), new Vector2(0.5f, 0.5f));
SpriteCache[logicKey] = s;
loadedCount++;
}
}
if (loadedCount > 0) texturesLoaded = true;
}
void ScanStrategicMap()
{
GameObject regionsRoot = GameObject.Find("UI/2DRoot/Layout/Limit/FrontDialogs/StrategicView/Root/Regions");
if (regionsRoot == null || !regionsRoot.activeInHierarchy) return;
foreach (Transform region in regionsRoot.transform)
{
Transform army = region.Find("Army");
if (army == null) continue;
Component defenseRenderer = army.GetComponent("iron.match.renderers.strategicview.DefenseCountRenderer");
Transform visualRoot = army.Find("Root");
Transform targetParent = (visualRoot != null) ? visualRoot : army;
if (defenseRenderer != null)
ProcessRegion((MonoBehaviour)defenseRenderer, targetParent);
}
}
void ProcessRegion(MonoBehaviour renderer, Transform parentTransform)
{
var visualizer = parentTransform.GetComponent<StrategicDisplay>();
if (visualizer == null)
{
visualizer = parentTransform.gameObject.AddComponent<StrategicDisplay>();
visualizer.Initialize();
}
var traverse = Traverse.Create(renderer);
var model = traverse.Property("model").GetValue();
if (model == null) { visualizer.Hide(); return; }
var children = Traverse.Create(model).Field("Children").GetValue() as IEnumerable;
if (children == null) children = Traverse.Create(model).Field("children").GetValue() as IEnumerable;
string orderType = "";
bool isSpecial = false;
int strength = 0;
bool found = false;
if (children != null)
{
foreach (var child in children)
{
var childTrav = Traverse.Create(child);
var attributesObj = childTrav.Field("attributes").GetValue();
if (attributesObj != null)
{
var valuesDict = Traverse.Create(attributesObj).Field("data").GetValue() as IDictionary;
if (valuesDict != null && valuesDict.Contains(400003))
{
object typeCont = valuesDict[400003];
object typeVal = Traverse.Create(typeCont).Property("Value").GetValue();
if (typeVal?.ToString() == "Order")
{
if (valuesDict.Contains(400038)) // OrderType
{
object oCont = valuesDict[400038];
object oVal = Traverse.Create(oCont).Property("Value").GetValue();
var list = oVal as IList;
if (list != null && list.Count > 0) orderType = list[0].ToString();
}
if (valuesDict.Contains(400039)) // HasStar
{
object sCont = valuesDict[400039];
object sVal = Traverse.Create(sCont).Property("Value").GetValue();
if (sVal != null && sVal.ToString().ToLower() == "true") isSpecial = true;
}
if (valuesDict.Contains(400004)) // Strength
{
object strCont = valuesDict[400004];
object strVal = Traverse.Create(strCont).Property("Value").GetValue();
int.TryParse(strVal?.ToString(), out strength);
}
found = true;
break;
}
}
}
}
}
if (!found)
{
visualizer.Hide();
return;
}
// Generar clave lógica
string generatedKey = $"{orderType}_{strength}";
if (isSpecial) generatedKey += "_Star";
if (SpriteCache.ContainsKey(generatedKey))
{
visualizer.ShowIcon(SpriteCache[generatedKey]);
}
else
{
if (!reportedMissingKeys.Contains(generatedKey))
{
Logger.LogWarning($"[FALTA MAPEO] El juego pide: '{generatedKey}'");
reportedMissingKeys.Add(generatedKey);
}
visualizer.ShowText(generatedKey);
}
}
// Función auxiliar que podrías llamar desde tu parche de Harmony
public static Texture2D MakeTextureTransparent(Texture originalTexture, Textures.IconOffset offset = null)
{
if (originalTexture == null) return null;
// 1. Crear una RenderTexture temporal para poder leer la textura original
RenderTexture tmp = RenderTexture.GetTemporary(
originalTexture.width,
originalTexture.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
// 2. Copiar la textura original a la RenderTexture (esto "desbloquea" los píxeles)
Graphics.Blit(originalTexture, tmp);
// 3. Activar la RenderTexture para leerla
RenderTexture previous = RenderTexture.active;
RenderTexture.active = tmp;
// 4. Crear una nueva Texture2D leíble
Texture2D newTexture = new Texture2D(originalTexture.width, originalTexture.height);
newTexture.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
// 5. MODIFICAR LOS PÍXELES (Aquí ocurre la magia)
Color[] pixels = newTexture.GetPixels();
float width = newTexture.width;
float height = newTexture.height;
Vector2 center = new Vector2(width / 2f, height / 2f);
float radius = (Mathf.Min(width, height) / 2f); // Radio máximo
if (offset != null)
{
center += new Vector2(offset.x, offset.y);
if (offset.size > 0) radius = offset.size / 2f;
}
for (int i = 0; i < pixels.Length; i++)
{
// --- Opción A: Máscara Redonda (Matemática) ---
int x = i % newTexture.width;
int y = i / newTexture.width;
float dist = Vector2.Distance(new Vector2(x, y), center);
if (dist > radius)
{
pixels[i] = Color.clear; // Hacemos transparente lo que esté fuera del círculo
}
// --- Opción B: Quitar el negro (Si prefieres por color) ---
// if (pixels[i].r < 0.1f && pixels[i].g < 0.1f && pixels[i].b < 0.1f)
// {
// pixels[i] = Color.clear;
// }
}
newTexture.SetPixels(pixels);
newTexture.Apply();
// 6. Limpieza
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(tmp);
return newTexture;
}
}
// --- VISUALIZADOR ---
public class StrategicDisplay : MonoBehaviour
{
private GameObject displayObj;
private Image iconImage;
private TextMeshProUGUI debugText;
private bool isInitialized = false;
private bool hasContent = false;
void Update()
{
if (!isInitialized || displayObj == null) return;
bool shouldShow = hasContent && Input.GetKey(KeyCode.V);
if (displayObj.activeSelf != shouldShow)
{
displayObj.SetActive(shouldShow);
}
}
public void Initialize()
{
if (isInitialized && displayObj != null) return;
if (displayObj != null) Destroy(displayObj);
try
{
displayObj = new GameObject("OrderIcon_Mod");
displayObj.transform.SetParent(this.transform, false);
// POSICIÓN EXACTA 0,0,0
displayObj.transform.localPosition = Vector3.zero;
// Aseguramos capa correcta
displayObj.layer = this.gameObject.layer;
RectTransform rt = displayObj.AddComponent<RectTransform>();
rt.sizeDelta = new Vector2(45, 45); // Tamaño del icono
iconImage = displayObj.AddComponent<Image>();
iconImage.raycastTarget = false;
iconImage.color = Color.white;
// Backup Text
GameObject textObj = new GameObject("BackupText");
textObj.transform.SetParent(displayObj.transform, false);
textObj.layer = this.gameObject.layer;
RectTransform textRt = textObj.AddComponent<RectTransform>();
textRt.anchoredPosition = Vector2.zero;
debugText = textObj.AddComponent<TextMeshProUGUI>();
debugText.alignment = TextAlignmentOptions.Center;
debugText.fontSize = 14;
debugText.fontStyle = FontStyles.Bold;
debugText.color = Color.magenta;
// CRUCIAL: Ponemos el icono al final de la lista de hijos para que se pinte ENCIMA
displayObj.transform.SetAsLastSibling();
// Inicialmente oculto
displayObj.SetActive(false);
isInitialized = true;
}
catch (System.Exception e)
{
Debug.LogWarning("Error UI: " + e.Message);
if (displayObj != null) Destroy(displayObj);
isInitialized = false;
}
}
public void Hide()
{
hasContent = false;
if (displayObj != null && displayObj.activeSelf) displayObj.SetActive(false);
}
public void ShowIcon(Sprite s)
{
if (!isInitialized) Initialize();
if (!isInitialized) return;
// Aseguramos orden de dibujado (Encima de todo)
displayObj.transform.SetAsLastSibling();
if (s != null)
{
iconImage.sprite = s;
iconImage.enabled = true;
if (debugText != null) debugText.text = "";
hasContent = true;
}
}
public void ShowText(string t)
{
if (!isInitialized) Initialize();
if (!isInitialized) return;
displayObj.transform.SetAsLastSibling();
if (iconImage != null) iconImage.enabled = false;
if (debugText != null) debugText.text = t;
hasContent = true;
}
}
}