1.0 compatibility

This commit is contained in:
Ferdinando 2026-09-10 18:05:24 +02:00
commit 6193b3ee2d
33 changed files with 712 additions and 49 deletions

View file

@ -1,10 +1,29 @@
using HarmonyLib;
using UnityEngine;
using Jotunn.Managers;
namespace RacesOfValheim
{
/// <summary>
/// The bat's flight, held on for as long as the worn model exists.
///
/// Debug fly has two halves, and this used to write only one. The field Player.m_debugFly is
/// what the local player's own physics reads; ZDOVars.s_debugFly is what every OTHER client
/// reads, because Character.IsDebugFlying consults the ZDO whenever it is not the owner. With
/// only the field written, a bat flew on its own screen and fell, walked and swam on everyone
/// else's - the fault WarVultureMount.SetFlying already names in its own summary. Both halves
/// go through SetFlying below now, so there is one place that can ever get it wrong again.
///
/// Writing the ZDO every frame is not network spam: ZDO.Set(int, bool) bottoms out in
/// ZDOExtraData.Set, which returns false when the value is unchanged, and only a true return
/// bumps the data revision. Read out of the assembly rather than assumed, because "every frame"
/// deserved the check.
/// </summary>
public class BatFormBehavior : MonoBehaviour
{
private static readonly System.Reflection.FieldInfo DebugFlyField =
AccessTools.Field(typeof(Player), "m_debugFly");
private Animator anim;
private AswidProgression.FormRules formRules;
@ -16,10 +35,7 @@ namespace RacesOfValheim
private void Start()
{
anim = GetComponentInChildren<Animator>();
if (Player.m_localPlayer != null)
{
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(true);
}
SetFlying(true);
}
private void Update()
@ -27,7 +43,7 @@ namespace RacesOfValheim
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
// Ensure flight remains on continuously
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(true);
SetFlying(true);
// Sync Animator
if (anim != null)
@ -35,7 +51,7 @@ namespace RacesOfValheim
Vector3 vel = Player.m_localPlayer.GetVelocity();
vel.y = 0f;
float speed = vel.magnitude;
// Bats don't necessarily have a complex animator like players,
// but if they do have a speed or flying parameter, we set it here.
anim.SetFloat("forward_speed", speed);
@ -45,9 +61,25 @@ namespace RacesOfValheim
private void OnDestroy()
{
if (Player.m_localPlayer != null)
SetFlying(false);
}
/// <summary>
/// Both halves of debug fly, always together. The field write survives a missing or invalid
/// ZNetView on purpose: without it a bat whose networking went away mid-flight would keep
/// flying on foot forever, which is the worse of the two failures.
/// </summary>
private static void SetFlying(bool flying)
{
Player player = Player.m_localPlayer;
if (player == null) return;
if (DebugFlyField != null) DebugFlyField.SetValue(player, flying);
ZNetView nview = player.GetComponent<ZNetView>();
if (nview != null && nview.IsValid() && nview.IsOwner())
{
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false);
nview.GetZDO().Set(ZDOVars.s_debugFly, flying);
}
}
}