Files
MxValheim/MxValheim/KillFeed/Patch.cs
2026-02-07 03:08:08 -05:00

424 lines
18 KiB
C#

using BepInEx;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.Diagnostics;
using UnityEngine.UI;
using static MxValheimMod;
namespace MxValheim.KillFeed
{
public class KillFeed_Patch
{
public static void SendKillToAll(string attacker, string victim, string victimInternalName, string weaponPrefab, int encodedType)
{
if (ZRoutedRpc.instance != null)
{
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "RPC_MxKillMsg", attacker, victim, victimInternalName, weaponPrefab, encodedType);
}
}
public void OnReceiveKillMsg(long sender, string attacker, string victim, string victimInternalName, string weaponPrefab, int encodedType)
{
ZNet zn = new ZNet();
if (zn.IsDedicated()) return;
if (attacker == "The World") return;
float distance = (encodedType / 1000) / 10.0f;
int remainder = encodedType % 1000;
bool isBoss = (remainder >= 100);
int level = (remainder % 100) / 10;
int type = remainder % 10;
Sprite victimIcon = GetCreatureIcon(victimInternalName);
GameObject prefab = ZNetScene.instance.GetPrefab(victimInternalName);
string localizedVictim = victimInternalName;
if (prefab != null)
{
Character c = prefab.GetComponent<Character>();
if (c != null) localizedVictim = c.m_name;
}
// Message Format Variables
string type1Separator = Localization.instance.Localize("$killfeed_format_type1");
string type2Separator = Localization.instance.Localize("$killfeed_format_type2");
string finalMsg = "";
string attackerFormat = "";
string victimFormat = "";
string distanceFormat = "";
string starFormat = "";
string crossFormat = "";
// Format Message in Divided Section
starFormat = "<color=#fffb00>★</color>";
crossFormat = "<color=#a0a3a1>✝</color>";
attackerFormat = $"<color=#00ff06>{attacker.ToUpper()}</color>";
if (type == 1)
{
victimFormat = $"{crossFormat} <color=#00ff06>{victim.ToUpper()}</color>";
} else if(type == 2)
{
switch (level)
{
case 1:
victimFormat = $"<color=#00ff06>{victim.ToUpper()}</color>";
break;
case 2:
victimFormat = $"{starFormat} <color=#00ff06>{victim.ToUpper()}</color>";
break;
case 3:
victimFormat = $"{starFormat}{starFormat} <color=#00ff06>{victim.ToUpper()}</color>";
break;
case 4:
victimFormat = $"{starFormat}{starFormat}{starFormat} <color=#00ff06>{victim.ToUpper()}</color>";
break;
case 5:
victimFormat = $"{starFormat}{starFormat}{starFormat}{starFormat} <color=#00ff06>{victim.ToUpper()}</color>";
break;
}
}
distanceFormat = $"{Localization.instance.Localize("$killfeed_format_distance_before")}<color=#9402f5>{distance:F1}m</color>{Localization.instance.Localize("$killfeed_format_distance_after")}";
finalMsg =
(type == 1) ? $"{victimFormat}{type1Separator}{attackerFormat}{distanceFormat}": // Player Death
(type == 2) ? $"{attackerFormat}{type2Separator}{victimFormat}{distanceFormat}": // Player Killed Something
$"{attackerFormat}{type2Separator}{victimFormat}{distanceFormat}"; // Failsafe
Sprite weaponIcon = null;
if (ObjectDB.instance != null && !string.IsNullOrEmpty(weaponPrefab))
{
GameObject itemObj = ObjectDB.instance.GetItemPrefab(weaponPrefab);
if (itemObj != null)
{
ItemDrop itemDrop = itemObj.GetComponent<ItemDrop>();
if (itemDrop != null && itemDrop.m_itemData.m_shared.m_icons.Length > 0)
{
weaponIcon = itemDrop.m_itemData.m_shared.m_icons[0];
}
} else
{
}
}
if (type == 1)
{ // Player
borderColor = new Color(0.545f, 0f, 0f);
}
else if (isBoss)
{ // Boss
borderColor = new Color(0.545f, 0.361f, 0f);
}
else if (level >= 2)
{ // 1-Star +
borderColor = new Color(0.525f, 0.545f, 0f);
}
else if (level < 2)
{
borderColor = new Color(0.141f, 0.141f, 0.153f);
}
lock (_msgQueue)
{
_msgQueue.Enqueue(finalMsg);
_borderColQueue.Enqueue(borderColor);
_iconQueue.Enqueue(weaponIcon);
_victimIconQueue.Enqueue(victimIcon);
}
}
public void ShowNextMessage(string msg, Sprite weaponIcon, Sprite victimIcon, Color borderColor)
{
if (_hudRoot == null) CreateCustomHud();
// Re-check for font here just in case it wasn't loaded during Awake
if (_killText.font.name != "AveriaSerifLibre-Bold")
{
foreach (Font f in Resources.FindObjectsOfTypeAll<Font>())
{
if (f.name == "AveriaSerifLibre-Bold")
{
_killText.font = f;
break;
}
}
}
_killText.text = msg;
_border.effectColor = borderColor;
_border.enabled = true;
// Weapon icon (Left)
_weaponIconSlot.sprite = weaponIcon;
_weaponIconSlot.enabled = (weaponIcon != null);
// Victim icon (Right)
if (_victimIconSlot != null)
{
_victimIconSlot.sprite = victimIcon;
_victimIconSlot.enabled = (victimIcon != null);
}
_weaponIconSlot.gameObject.SetActive(weaponIcon != null);
_victimIconSlot.gameObject.SetActive(victimIcon != null);
_panelImage.rectTransform.localScale = (borderColor != Color.white) ? new Vector3(1.1f, 1.1f, 1.1f) : Vector3.one;
_hudRoot.SetActive(true);
_canvasGroup.alpha = 1f;
_displayTimer = 5.0f;
}
private Sprite GetCreatureIcon(string creatureName)
{
if (ObjectDB.instance == null) return null;
// Clean the name (remove "(Clone)" if it somehow slipped through)
string cleanName = creatureName.Replace("(Clone)", "").Trim();
// List of common trophies that don't follow the exact pattern if needed
// But for most (Boar, Greyling, Neck, Deer), this works:
string replace = cleanName.Replace("_","");
string trophyName = "Trophy" + replace;
GameObject trophyObj = ObjectDB.instance.GetItemPrefab(trophyName);
if (trophyObj != null)
{
ItemDrop item = trophyObj.GetComponent<ItemDrop>();
return item?.m_itemData?.m_shared?.m_icons[0];
}
return null;
}
[HarmonyPatch(typeof(Character), nameof(Character.Damage))]
public static class DamageTracker
{
static void Prefix(Character __instance, HitData hit)
{
if (__instance == null || hit == null) return;
Character attacker = hit.GetAttacker();
if (attacker != null)
{
string weaponId = "";
if (attacker is Player p)
{
// IMPORTANT: Use .m_dropPrefab.name to get "AxeStone"
// Do NOT use .m_shared.m_name which returns "$item_axe_stone"
ItemDrop.ItemData currentWeapon = p.GetCurrentWeapon();
if (currentWeapon != null && currentWeapon.m_dropPrefab != null)
{
weaponId = currentWeapon.m_dropPrefab.name;
}
}
_activeTrackers[__instance] = new KillData
{
attackerName = attacker.GetHoverName(),
weaponPrefabName = weaponId
};
}
}
}
[HarmonyPatch(typeof(Character), nameof(Character.ApplyDamage))]
public static class DeathNotifier
{
// The parameter names MUST match the game's: hit, showDamageText, triggerEffects, mod
static void Postfix(Character __instance, HitData hit, bool showDamageText, bool triggerEffects, HitData.DamageModifier mod)
{
// Check if the character is dead or just died
if (__instance.GetHealth() <= 0f)
{
ZNetView nview = __instance.GetComponent<ZNetView>();
// Prevent inter-npc kill
if (!__instance.IsPlayer() && !hit.GetAttacker().IsPlayer()) return;
// This check should now pass because ApplyDamage happens before the object is invalidated
if (nview == null || !nview.IsValid() || !nview.IsOwner()) return;
float distance = 0f;
Character attacker = hit.GetAttacker();
if (attacker != null)
{
Debug.Log($"KillFeed:ApplyDamage Attacker is {attacker} {attacker.transform.position}/{__instance.transform.position}");
distance = Vector3.Distance(attacker.transform.position, __instance.transform.position);
}
// Use Utils.GetPrefabName to get "Boar" instead of "Sanglier" or "Boar(Clone)"
string internalName = __instance.gameObject.name;
int cloneIndex = internalName.IndexOf("(Clone)");
if (cloneIndex != -1)
{
internalName = internalName.Substring(0, cloneIndex);
}
_activeTrackers.TryGetValue(__instance, out KillData data);
string attackerName = data?.attackerName ?? hit.GetAttacker()?.GetHoverName() ?? "The World";
string weaponName = "";
if (data != null && !string.IsNullOrEmpty(data.weaponPrefabName))
{
weaponName = data.weaponPrefabName;
}
else if (hit.GetAttacker() is Player p)
{
// Fallback: If tracker missed it, get current player's weapon
weaponName = p.GetCurrentWeapon()?.m_dropPrefab?.name ?? "";
}
string victimName = __instance.GetHoverName();
int type = 0;
if (__instance.IsPlayer()) type = 1;
else if (attackerName == Player.m_localPlayer?.GetHoverName()) type = 2;
int level = __instance.GetLevel();
bool isBoss = __instance.IsBoss();
int distInt = (int)(Math.Min(distance, 999f) * 10);
int encodedType = type + (level * 10) + (isBoss ? 100 : 0) + (distInt * 1000);
if (ZRoutedRpc.instance != null)
{
ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.Everybody, "RPC_MxKillMsg",
attackerName, victimName, internalName, weaponName, encodedType);
}
_activeTrackers.Remove(__instance);
}
}
}
private void CreateCustomHud()
{
GameObject oldRoot = GameObject.Find("MxKillFeed_Root");
if (oldRoot != null) UnityEngine.Object.Destroy(oldRoot);
_hudRoot = new GameObject("MxKillFeed_Root");
UnityEngine.Object.DontDestroyOnLoad(_hudRoot);
Canvas c = _hudRoot.AddComponent<Canvas>();
c.renderMode = RenderMode.ScreenSpaceOverlay;
c.sortingOrder = 10000;
CanvasScaler scaler = _hudRoot.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(1920, 1080);
_canvasGroup = _hudRoot.AddComponent<CanvasGroup>();
_canvasGroup.alpha = 0f; // Start invisible for the fade-in
// 1. Smaller, sleeker Panel
GameObject panel = new GameObject("Background", typeof(RectTransform), typeof(Image));
panel.transform.SetParent(_hudRoot.transform, false);
_panelImage = panel.GetComponent<Image>();
_panelImage.color = new Color(0, 0, 0, 0.6f); // More transparent/sleek
// 1. Add Layout Group to handle the horizontal row
HorizontalLayoutGroup layout = panel.AddComponent<HorizontalLayoutGroup>();
layout.childAlignment = TextAnchor.MiddleCenter;
layout.spacing = 10f;
layout.padding = new RectOffset(15, 15, 5, 5); // Internal margins
layout.childControlWidth = true; // Let the layout control widths
layout.childControlHeight = true;
layout.childForceExpandWidth = false;
// 2. Add the Content Size Fitter (The "Stretcher")
ContentSizeFitter fitter = panel.AddComponent<ContentSizeFitter>();
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
// Try to find the "sunken" wood panel style if shout_field isn't your vibe
foreach (Sprite s in Resources.FindObjectsOfTypeAll<Sprite>())
{
if (s.name == "item_background")
{ // This gives a nice square framed look
_panelImage.sprite = s;
_panelImage.type = Image.Type.Sliced;
break;
}
}
RectTransform pRect = panel.GetComponent<RectTransform>();
pRect.anchorMin = pRect.anchorMax = pRect.pivot = new Vector2(0.5f, 1f);
pRect.anchoredPosition = new Vector2(0, -30); // Closer to top
pRect.sizeDelta = new Vector2(400, 35); // Shorter and narrower
pRect.localScale = Vector3.one;
// 2. Compact Icon
GameObject iconObj = new GameObject("Icon", typeof(RectTransform), typeof(Image));
iconObj.transform.SetParent(panel.transform, false);
_weaponIconSlot = iconObj.GetComponent<Image>();
_weaponIconSlot.preserveAspect = true;
LayoutElement weaponLayout = iconObj.AddComponent<LayoutElement>();
weaponLayout.preferredWidth = 24;
weaponLayout.preferredHeight = 24;
RectTransform iRect = iconObj.GetComponent<RectTransform>();
iRect.anchorMin = iRect.anchorMax = iRect.pivot = new Vector2(0, 0.5f);
iRect.anchoredPosition = new Vector2(8, 0);
iRect.sizeDelta = new Vector2(25, 25); // Smaller icon
// 3. Elegant Text
GameObject textObj = new GameObject("Text", typeof(RectTransform), typeof(Text));
textObj.transform.SetParent(panel.transform, false);
_killText = textObj.GetComponent<Text>();
// Important: Text needs to have its "Horizontal Overflow" set to Wrap or Overflow
_killText.horizontalOverflow = HorizontalWrapMode.Overflow;
// Attempt to find Valheim's specific font, fallback to Arial if not found
Font valheimFont = null;
foreach (Font f in Resources.FindObjectsOfTypeAll<Font>())
{
if (f.name == "AveriaSerifLibre-Bold")
{
valheimFont = f;
break;
}
}
// Corrected fallback for older Unity versions used in Valheim
if (valheimFont == null)
{
valheimFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
}
_killText.font = valheimFont;
_killText.fontSize = 12;
_killText.alignment = TextAnchor.MiddleLeft;
_killText.supportRichText = true;
_killText.color = Color.white;
// 4. Victim Portrait Slot
GameObject victimObj = new GameObject("VictimIcon", typeof(RectTransform), typeof(Image));
victimObj.transform.SetParent(panel.transform, false);
_victimIconSlot = victimObj.GetComponent<Image>();
_victimIconSlot.preserveAspect = true;
LayoutElement victimLayout = victimObj.AddComponent<LayoutElement>();
victimLayout.preferredWidth = 24;
victimLayout.preferredHeight = 24;
RectTransform vRect = victimObj.GetComponent<RectTransform>();
vRect.anchorMin = vRect.anchorMax = vRect.pivot = new Vector2(1, 0.5f);
vRect.anchoredPosition = new Vector2(-8, 0);
vRect.sizeDelta = new Vector2(30, 30);
RectTransform tRect = textObj.GetComponent<RectTransform>();
tRect.anchorMin = Vector2.zero;
tRect.anchorMax = Vector2.one;
tRect.offsetMin = new Vector2(40, 0);
tRect.offsetMax = new Vector2(-40, 0);
_border = panel.AddComponent<Outline>();
_border.effectDistance = new Vector2(2, 2);
_border.enabled = false; // Off by default
_hudRoot.SetActive(false);
}
}
}