180 lines
7.5 KiB
C#
180 lines
7.5 KiB
C#
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The hooks that keep an Alfr looking like one. Only the look so far; the shield, the death
|
|
/// timer and the powers add their own patches as they are written.
|
|
/// </summary>
|
|
public static class AlfarPatches
|
|
{
|
|
/// <summary>
|
|
/// The only thing that actually colours a player.
|
|
///
|
|
/// SetSkinColor followed by UpdateColors does nothing, because UpdateColors is what REBUILDS
|
|
/// the body material from Valheim's own values and throws any earlier write away
|
|
/// (DvergarPatches documents the dead end). Writing _SkinColor here, after it has run, is the
|
|
/// only order that survives.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(VisEquipment), "UpdateColors")]
|
|
public static class Alfar_UpdateColors_Patch
|
|
{
|
|
public static void Postfix(VisEquipment __instance)
|
|
{
|
|
if (__instance == null || __instance.m_bodyModel == null) return;
|
|
|
|
Player player = __instance.GetComponent<Player>();
|
|
if (player == null || !AlfarVisuals.IsAlfarPlayer(player)) return;
|
|
|
|
AlfarVisuals.ApplyGreyTexture(__instance, true);
|
|
|
|
Material material = __instance.m_bodyModel.material;
|
|
if (material != null && material.HasProperty("_SkinColor"))
|
|
{
|
|
material.SetColor("_SkinColor", AlfarVisuals.SkinFor(player));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hair and beard never attach to an Alfr. Every equipment change makes the game write the
|
|
/// profile's hair back (Humanoid.SetupVisEquipment), and ApplyHairless only clears it on its
|
|
/// 0.5s pass, so players saw the hair flash in for a moment. Zeroing the hash here, where
|
|
/// VisEquipment spawns the model, means it never appears. Covers remote players too, since
|
|
/// their visuals go through the same call.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(VisEquipment), "SetHairEquipped")]
|
|
public static class Alfar_SetHairEquipped_Patch
|
|
{
|
|
public static void Prefix(VisEquipment __instance, ref int hash)
|
|
{
|
|
if (hash != 0 && IsAlfarVis(__instance)) hash = 0;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(VisEquipment), "SetBeardEquipped")]
|
|
public static class Alfar_SetBeardEquipped_Patch
|
|
{
|
|
public static void Prefix(VisEquipment __instance, ref int hash)
|
|
{
|
|
if (hash != 0 && IsAlfarVis(__instance)) hash = 0;
|
|
}
|
|
}
|
|
|
|
private static bool IsAlfarVis(VisEquipment vis)
|
|
{
|
|
Player player = vis.GetComponent<Player>();
|
|
return player != null && AlfarVisuals.IsAlfarPlayer(player);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The character preview on the main menu is a Player too, with the saved race in its custom data
|
|
/// and no ZNetView. Its Update may never run, so a small driver on it keeps the look: skin, hair,
|
|
/// scale and eyes, twice a second, for as long as the preview exists. The existing menu hook only
|
|
/// pushes ONE colour refresh and knows nothing of eyes or texture.
|
|
/// </summary>
|
|
public class AlfarPreviewDriver : MonoBehaviour
|
|
{
|
|
private Player player;
|
|
private float next;
|
|
|
|
private void Awake() { player = GetComponent<Player>(); }
|
|
|
|
private void Update()
|
|
{
|
|
if (player == null || Time.time < next) return;
|
|
next = Time.time + 0.5f;
|
|
|
|
bool isAlfar = AlfarVisuals.IsAlfarPlayer(player);
|
|
AlfarVisuals.ApplyScale(player, isAlfar);
|
|
AlfarVisuals.ApplyEyes(player);
|
|
if (isAlfar) AlfarVisuals.ApplyHairless(player);
|
|
AlfarVisuals.SyncSkin(player, isAlfar);
|
|
}
|
|
}
|
|
|
|
public static void AttachPreviewDriver(FejdStartup instance)
|
|
{
|
|
Player preview = instance.GetType()
|
|
.GetMethod("GetPreviewPlayer",
|
|
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic |
|
|
System.Reflection.BindingFlags.Public)
|
|
?.Invoke(instance, null) as Player;
|
|
if (preview != null && preview.GetComponent<AlfarPreviewDriver>() == null)
|
|
{
|
|
preview.gameObject.AddComponent<AlfarPreviewDriver>();
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(FejdStartup), "SetupCharacterPreview")]
|
|
public static class Alfar_Preview_Setup_Patch
|
|
{
|
|
[HarmonyPriority(Priority.Last)]
|
|
public static void Postfix(FejdStartup __instance) => AttachPreviewDriver(__instance);
|
|
}
|
|
|
|
[HarmonyPatch(typeof(FejdStartup), "SetSelectedProfile")]
|
|
public static class Alfar_Preview_Profile_Patch
|
|
{
|
|
[HarmonyPriority(Priority.Last)]
|
|
public static void Postfix(FejdStartup __instance) => AttachPreviewDriver(__instance);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scale every frame, the cosmetics on a throttle.
|
|
///
|
|
/// On Player.Update because that is the one place body scale survives: the Trol's postfix
|
|
/// forces every other player's visual back to 1 on the same frame, so a scale written
|
|
/// anywhere else is simply overwritten. RaceScale keeps the two from undoing each other.
|
|
/// This runs for every player in range, not just the local one, so other clients see an
|
|
/// Alfr as one.
|
|
/// </summary>
|
|
[HarmonyPatch(typeof(Player), "Update")]
|
|
public static class Alfar_PlayerUpdate_Patch
|
|
{
|
|
private static readonly Dictionary<Player, float> nextCosmeticPass = new Dictionary<Player, float>();
|
|
private const float CosmeticInterval = 0.5f;
|
|
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
if (__instance == null) return;
|
|
|
|
bool isAlfar = AlfarVisuals.IsAlfarPlayer(__instance);
|
|
|
|
AlfarVisuals.ApplyScale(__instance, isAlfar);
|
|
|
|
// The passive carrier belongs to the local body only; status effects are re-added
|
|
// because they do not survive a reload or a death.
|
|
if (__instance == Player.m_localPlayer)
|
|
{
|
|
if (isAlfar && !__instance.IsDead())
|
|
{
|
|
AlfarStatusEffects.Ensure(__instance);
|
|
AlfarPowers.Tick(__instance);
|
|
}
|
|
else
|
|
{
|
|
AlfarStatusEffects.Remove(__instance);
|
|
if (AlfarArchon.Active) AlfarArchon.ForceClear(__instance);
|
|
}
|
|
}
|
|
|
|
if (!nextCosmeticPass.TryGetValue(__instance, out float due) || Time.time >= due)
|
|
{
|
|
nextCosmeticPass[__instance] = Time.time + CosmeticInterval;
|
|
|
|
// Unconditional, like the other races': the eyes have to be able to remove
|
|
// themselves from whoever stops being an Alfr, and they cannot do that from
|
|
// behind an `if (isAlfar)`.
|
|
AlfarVisuals.ApplyEyes(__instance);
|
|
AlfarVisuals.ApplyArchonGlow(__instance);
|
|
|
|
if (isAlfar) AlfarVisuals.ApplyHairless(__instance);
|
|
AlfarVisuals.SyncSkin(__instance, isAlfar);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|