// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using EpicGames.Core;
using EpicGames.UHT.Tokenizer;
using EpicGames.UHT.Types;
using EpicGames.UHT.Utils;
namespace EpicGames.UHT.Tables
{
///
/// Delegate to invoke to sanitize a loctext default value
///
/// Property in question
/// The default value
/// Token for the loctext type being parsed
/// Output sanitized value.
/// True if sanitized, false if not.
public delegate bool UhtLocTextDefaultValueDelegate(UhtTextProperty property, IUhtTokenReader defaultValueReader, ref UhtToken macroToken, StringBuilder innerDefaultValue);
///
/// Attribute defining the loctext sanitizer
///
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class UhtLocTextDefaultValueAttribute : Attribute
{
///
/// Name of the sanitizer (i.e. LOCTEXT, NSLOCTEXT, ...)
///
public string? Name { get; set; }
}
///
/// Loctext sanitizer
///
public struct UhtLocTextDefaultValue
{
///
/// Delegate to invoke
///
public UhtLocTextDefaultValueDelegate Delegate { get; set; }
}
///
/// Table of loctext sanitizers
///
public class UhtLocTextDefaultValueTable
{
private readonly Dictionary _locTextDefaultValues = new();
///
/// Return the loc text default value associated with the given name
///
///
/// Loc text default value handler
///
public bool TryGet(StringView name, out UhtLocTextDefaultValue locTextDefaultValue)
{
return _locTextDefaultValues.TryGetValue(name, out locTextDefaultValue);
}
///
/// Handle a loctext default value attribute
///
/// Method info
/// Defining attribute
/// Thrown if the attribute isn't properly defined
public void OnLocTextDefaultValueAttribute(MethodInfo methodInfo, UhtLocTextDefaultValueAttribute locTextDefaultValueAttribute)
{
if (String.IsNullOrEmpty(locTextDefaultValueAttribute.Name))
{
throw new UhtIceException("A loc text default value attribute must have a name");
}
UhtLocTextDefaultValue locTextDefaultValue = new()
{
Delegate = (UhtLocTextDefaultValueDelegate)Delegate.CreateDelegate(typeof(UhtLocTextDefaultValueDelegate), methodInfo)
};
_locTextDefaultValues.Add(new StringView(locTextDefaultValueAttribute.Name), locTextDefaultValue);
}
}
}