using ADOFAIRenderer.Core;
using HarmonyLib;
using UnityEngine;
namespace ADOFAIRenderer.Patch.Game;
///
/// Patches decoration rendering to hide decorations that failed to load (or are empty) and fell back to the default
/// placeholder sprite (tree inside a circle) during a render when AdvancedConfig.HideFailedDecorations is enabled.
///
public static class DecorationPatch
{
///
/// True while a render is loading or active and HideFailedDecorations is enabled in the configuration.
///
public static bool IsEnabled =>
RenderManager.Instance &&
RenderManager.Instance.stage >= RenderStage.Load &&
RenderManager.Instance.Configuration?.AdvancedConfig?.HideFailedDecorations == true;
///
/// Checks whether the given sprite is the default placeholder or not-found sprite of the decoration manager.
///
public static bool IsPlaceholderSprite(Sprite? sprite, scrDecorationManager? manager = null)
{
if (!sprite) return false;
manager ??= scrDecorationManager.instance;
if (!manager) return false;
return sprite == manager.defaultSprite || (manager.notFoundSprite && sprite == manager.notFoundSprite);
}
///
/// Sweeps all visual decorations in scrDecorationManager.instance.allDecorations and clears any placeholder sprites.
/// Called from when scnGame.Play runs.
///
public static void ProcessDecorations()
{
if (!IsEnabled) return;
var manager = scrDecorationManager.instance;
if (!manager || manager.allDecorations == null) return;
foreach (var dec in manager.allDecorations)
{
if (dec is scrVisualDecoration visualDec && visualDec.spriteRenderer)
{
if (IsPlaceholderSprite(visualDec.spriteRenderer.sprite, manager))
{
visualDec.SetSprite((Sprite)null!);
}
}
}
}
///
/// Prefix of scrVisualDecoration.SetSprite(Sprite): replaces placeholder default/not-found sprites with null
/// when is true.
///
[HarmonyPatch(typeof(scrVisualDecoration), nameof(scrVisualDecoration.SetSprite), typeof(Sprite))]
// ReSharper disable once InconsistentNaming
public static class VisualDecorationSetSpritePatch
{
[HarmonyPrefix]
public static void Prefix(scrVisualDecoration __instance, ref Sprite? sprite)
{
if (!IsEnabled || !sprite) return;
if (IsPlaceholderSprite(sprite, __instance.manager))
{
sprite = null;
}
}
}
///
/// Prefix of scrDecorationManager.ShowEmptyDecorations(bool): suppresses showing placeholder default sprites
/// during render when is true.
///
[HarmonyPatch(typeof(scrDecorationManager), nameof(scrDecorationManager.ShowEmptyDecorations))]
public static class ShowEmptyDecorationsPatch
{
[HarmonyPrefix]
public static bool Prefix(bool show)
{
if (show && IsEnabled)
{
return false;
}
return true;
}
}
}