RacesOfValheim/JotunnModStub/PlayerVisualHider.cs
kanym c3a3bd2864 chore(logging): strip the AswidDebug tracing and silence the visual hider
The [AswidDebug] logging shipped in 1.3.8 wrote a line per aura tick per enemy
in radius, four times a second while channelling, plus a line for every devour
attempt and every inedible kill. It filled BepInEx's log for anyone playing an
Aswid. Genuine failure warnings are kept and retagged [Aswid]; the F8
consume-nearest-mob test patch goes with it.

PlayerVisualHider.Diagnostics is off now that transformed bodies are confirmed
hidden. It is a static rather than a const so flipping it back does not leave
the guarded calls unreachable.

The 1.3.9 changelog also records the logging removal, and that remote Skadi
flight now works for observers - the second of the two known issues left open
in 1.3.8, confirmed in multiplayer.
2026-08-02 19:07:59 +02:00

162 lines
6.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Hides and restores a player's "Visual" child without ever deactivating it.
///
/// The player's Animator sits on that same GameObject, and Unity rebinds an Animator whenever it
/// is re-enabled: the controller restarts from its entry state, which for the player is the same
/// get-up clip that plays when you spawn into a world. Deactivating the Visual to hide the human
/// body during a monster form is therefore why reverting looked exactly like respawning.
///
/// Hiding is done with Renderer.forceRenderingOff on top of Renderer.enabled. Nothing in the game
/// touches forceRenderingOff on a character (only EnvZone uses it, on its own props), so the body
/// stays invisible even if some other code re-enables the renderers behind our back.
/// </summary>
public class PlayerVisualHider : MonoBehaviour
{
// Tracing for the hide/show paths. Off by default; flip to true if a transformed body
// ever shows up again and the log should name which path did it.
public static bool Diagnostics = false;
// VisEquipment rebuilds item visuals whenever gear changes and they come back visible, so
// the hidden set has to be re-swept rather than applied once.
private const float RescanInterval = 0.25f;
private const float LogInterval = 2f;
private struct HiddenRenderer
{
public Renderer Renderer;
public bool WasEnabled;
}
private readonly List<HiddenRenderer> m_hidden = new List<HiddenRenderer>();
private float m_nextRescan;
private float m_nextLog;
private string m_context = "";
public static void Hide(Transform playerVisual, string context)
{
if (playerVisual == null) return;
// Undo the two older ways this body could have been hidden, so a form entered right
// after one of those code paths still starts from a sane state.
if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true);
if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
// Belt and braces: if anything else ever does deactivate the Visual, at least keep the
// animator from snapping back to its spawn state.
Animator anim = playerVisual.GetComponent<Animator>();
if (anim != null) anim.keepAnimatorStateOnDisable = true;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
if (hider == null) hider = playerVisual.gameObject.AddComponent<PlayerVisualHider>();
bool alreadyHiding = hider.enabled;
hider.m_context = context;
hider.enabled = true;
int added = hider.HideRenderers();
if (Diagnostics && (!alreadyHiding || added > 0))
{
Jotunn.Logger.LogInfo(
$"[VisualHider] hide '{context}' newlyHidden={added} tracked={hider.m_hidden.Count} " +
$"visualActive={playerVisual.gameObject.activeInHierarchy}");
}
}
public static void Show(Transform playerVisual, string context)
{
if (playerVisual == null) return;
if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true);
if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
if (hider == null || !hider.enabled) return;
int restored = hider.RestoreRenderers();
if (Diagnostics)
{
Jotunn.Logger.LogInfo(
$"[VisualHider] show '{context}' restored={restored} (hidden by '{hider.m_context}')");
}
}
public static bool IsHidden(Transform playerVisual)
{
if (playerVisual == null) return false;
PlayerVisualHider hider = playerVisual.GetComponent<PlayerVisualHider>();
return hider != null && hider.enabled;
}
/// <summary>Hides every renderer not already hidden by us. Returns how many were added.</summary>
private int HideRenderers()
{
int added = 0;
// Equipment is parented to the body's bones, so this covers weapons, armour, hair and
// the cape along with the body mesh itself.
foreach (Renderer r in GetComponentsInChildren<Renderer>(true))
{
if (r == null || r.forceRenderingOff) continue; // already ours
m_hidden.Add(new HiddenRenderer { Renderer = r, WasEnabled = r.enabled });
r.enabled = false;
r.forceRenderingOff = true;
added++;
}
m_nextRescan = Time.time + RescanInterval;
return added;
}
private int RestoreRenderers()
{
int restored = 0;
foreach (HiddenRenderer h in m_hidden)
{
if (h.Renderer == null) continue;
h.Renderer.forceRenderingOff = false;
h.Renderer.enabled = h.WasEnabled;
restored++;
}
m_hidden.Clear();
enabled = false;
return restored;
}
private void LateUpdate()
{
if (Time.time < m_nextRescan) return;
int added = HideRenderers();
if (!Diagnostics || Time.time < m_nextLog) return;
// If anything re-enables a renderer we own, forceRenderingOff still keeps it invisible -
// but we want to know who, so report it rather than silently papering over it.
int reEnabled = 0;
string firstName = "";
foreach (HiddenRenderer h in m_hidden)
{
if (h.Renderer == null || !h.Renderer.enabled) continue;
if (reEnabled == 0) firstName = h.Renderer.name;
reEnabled++;
}
if (added == 0 && reEnabled == 0) return;
m_nextLog = Time.time + LogInterval;
Jotunn.Logger.LogInfo(
$"[VisualHider] sweep '{m_context}' newlyHidden={added} reEnabledBehindUs={reEnabled} " +
$"first='{firstName}' tracked={m_hidden.Count}");
}
}
}