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

@ -0,0 +1,57 @@
using System.Reflection;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Closes the window in Valheim 1.0 where MaterialMan hands out a null property block.
///
/// MaterialMan splits its own start-up across two Unity callbacks:
///
/// Awake() -&gt; s_instance = this
/// Start() -&gt; m_propertyBlock = new MaterialPropertyBlock()
///
/// Between the two, MaterialMan.instance answers but its property block is still null. Anything
/// that registers with it in that gap gets a PropertyContainer built around that null - and the
/// container KEEPS it, because it copies the reference into a field of its own rather than
/// reading MaterialMan's each frame. UpdateBlock() opens with m_propertyBlock.Clear(), so that
/// one container then throws a NullReferenceException on every frame for the rest of the scene.
/// A single session logged 463 of them, all identical, all from MaterialMan.UpdateBlock.
///
/// This is vanilla's bug, not ours, but 1.0 is what made us walk into it: VisEquipment.AttachArmor
/// and AttachItem now call RefreshSnowLevel, which registers with MaterialMan, and none of that
/// existed in 0.221. Comparing the two builds, the snow-cover system is the ONLY new caller of
/// MaterialMan - so a mod that dresses the main-menu preview, as this one does for every race,
/// reaches it during the gap where before there was nothing to reach.
///
/// Filling the block in Awake costs one allocation, once per scene, and removes the gap: whoever
/// registers early now gets a real block. Start() still makes its own a moment later and every
/// container built after that point uses that one instead, which is fine - UpdateBlock clears and
/// refills the block before each renderer reads it, so a container holding a different instance
/// behaves identically. Vanilla is otherwise left alone.
///
/// The symptom was only ever visible on the main menu, because loading a world builds a fresh
/// MaterialMan whose Start has long since run. It was never a gameplay bug - just 463 lines of
/// noise standing between us and the log entries that matter.
/// </summary>
[HarmonyPatch(typeof(MaterialMan), "Awake")]
internal static class MaterialManPropertyBlockFix
{
/// <summary>Private in the shipped assembly, so it is reached by reflection like the rest.</summary>
private static readonly FieldInfo PropertyBlock = AccessTools.Field(typeof(MaterialMan), "m_propertyBlock");
/// <summary>If a future build renames or removes the field, patch nothing rather than guess.</summary>
private static bool Prepare() => PropertyBlock != null;
private static void Postfix(MaterialMan __instance)
{
// Awake destroys itself when another instance already claimed s_instance. Writing to a
// component on its way out is harmless, and testing for it would cost more than it saves.
if (PropertyBlock.GetValue(__instance) == null)
{
PropertyBlock.SetValue(__instance, new MaterialPropertyBlock());
}
}
}
}