using System;
using System.Reflection;
using UnityEngine;
namespace Unity.Pipeline.HotReload
{
///
/// Helper utilities for integrating hot reload calls into methods marked with [HotReloadWithOverrides].
/// Provides convenience methods for checking and invoking hot reload overrides.
///
public static class HotReloadHelper
{
///
/// Execute method with hot reload override check.
/// If hot reload override exists, invokes it; otherwise invokes original method.
///
/// Type of the instance
/// Instance to invoke method on
/// Name of the method to invoke
/// Original method implementation
/// Method parameters
/// Result of method invocation
public static object ExecuteWithHotReload(T instance, string methodName, Func originalMethod, params object[] parameters)
{
var methodId = GetMethodId(methodName);
// Try hot reload first
if (HotReloadRegistry.TryInvokeHotReload(methodId, instance, parameters))
{
return null; // Hot reload methods currently don't return values (can be enhanced later)
}
// Fall back to original method
return originalMethod?.Invoke();
}
///
/// Execute void method with hot reload override check.
/// Convenience overload for void methods.
///
/// Type of the instance
/// Instance to invoke method on
/// Name of the method to invoke
/// Original method implementation
/// Method parameters
public static void ExecuteWithHotReload(T instance, string methodName, Action originalMethod, params object[] parameters)
{
var methodId = GetMethodId(methodName);
// Try hot reload first
if (HotReloadRegistry.TryInvokeHotReload(methodId, instance, parameters))
{
return; // Hot reload override was invoked
}
// Fall back to original method
originalMethod?.Invoke();
}
///
/// Execute method with custom method ID and hot reload override check.
/// Use this when the method has a custom ID specified in [HotReloadWithOverrides(Id = "...")].
///
/// Type of the instance
/// Instance to invoke method on
/// Custom method ID from HotReloadWithOverrides attribute
/// Original method implementation
/// Method parameters
/// Result of method invocation
public static object ExecuteWithHotReloadCustomId(T instance, string customMethodId, Func originalMethod, params object[] parameters)
{
// Try hot reload first
if (HotReloadRegistry.TryInvokeHotReload(customMethodId, instance, parameters))
{
return null; // Hot reload methods currently don't return values
}
// Fall back to original method
return originalMethod?.Invoke();
}
///
/// Execute void method with custom method ID and hot reload override check.
/// Use this when the method has a custom ID specified in [HotReloadWithOverrides(Id = "...")].
///
/// Type of the instance
/// Instance to invoke method on
/// Custom method ID from HotReloadWithOverrides attribute
/// Original method implementation
/// Method parameters
public static void ExecuteWithHotReloadCustomId(T instance, string customMethodId, Action originalMethod, params object[] parameters)
{
// Try hot reload first
if (HotReloadRegistry.TryInvokeHotReload(customMethodId, instance, parameters))
{
return; // Hot reload override was invoked
}
// Fall back to original method
originalMethod?.Invoke();
}
///
/// Check if a hot reload override is active for a specific method.
/// Useful for debugging or conditional logic based on hot reload state.
///
/// Type of the instance
/// Name of the method to check
/// True if hot reload override is active
public static bool IsHotReloadActive(string methodName)
{
var methodId = GetMethodId(methodName);
var stats = HotReloadRegistry.GetStats();
return stats.ActiveOverrideIds.Contains(methodId);
}
///
/// Check if a hot reload override is active for a specific custom method ID.
///
/// Custom method ID to check
/// True if hot reload override is active
public static bool IsHotReloadActive(string customMethodId)
{
var stats = HotReloadRegistry.GetStats();
return stats.ActiveOverrideIds.Contains(customMethodId);
}
///
/// Generate standard method ID from type and method name.
/// Format: TypeName.MethodName
///
/// Type containing the method
/// Name of the method
/// Standard method ID
private static string GetMethodId(string methodName)
{
return $"{typeof(T).Name}.{methodName}";
}
}
}