// Copyright Epic Games, Inc. All Rights Reserved.
using System.Diagnostics;
using System.Linq;
using EpicGames.Core;
namespace UnrealBuildTool
{
///
///
///
public enum LocalizationTargetDescriptorLoadingPolicy
{
///
///
///
Never,
///
///
///
Always,
///
///
///
Editor,
///
///
///
Game,
///
///
///
PropertyNames,
///
///
///
ToolTips,
};
///
/// How this localization target should be generated during the localization gather pipeline
///
public enum LocalizationConfigGenerationPolicy
{
///
/// This localization target should never have localization config files associated with it during the localization gather pipeline.
///
Never,
///
/// This localization target should only use user generated localization config files during the localization gather pipeline.
///
User,
///
/// Default auto-generated localization config files will be used to generate the localization target and localization content files during the localization gather pipeline
///
Auto,
}
///
/// Description of a localization target.
///
[DebuggerDisplay("Name={Name}")]
public class LocalizationTargetDescriptor
{
///
/// Name of this target
///
public readonly string Name;
///
/// When should the localization data associated with a target should be loaded?
///
public LocalizationTargetDescriptorLoadingPolicy LoadingPolicy;
///
/// How should this localization target's localization config files be generated during a localization gather.
///
public LocalizationConfigGenerationPolicy ConfigGenerationPolicy;
///
/// Constructor
///
/// Name of the target
/// When should the localization data associated with a target should be loaded?
public LocalizationTargetDescriptor(string InName, LocalizationTargetDescriptorLoadingPolicy InLoadingPolicy)
{
Name = InName;
LoadingPolicy = InLoadingPolicy;
// Older plugins and localization target descriptors won't have the generation policy. We default it to Never.
ConfigGenerationPolicy = LocalizationConfigGenerationPolicy.Never;
}
///
/// Constructor
///
/// The name of the localization target.
/// When the localization data associated with this localization target should be loaded.
/// How the localization config files should be generated during a localization gather to create the localization data files.
public LocalizationTargetDescriptor(string InName, LocalizationTargetDescriptorLoadingPolicy InLoadingPolicy, LocalizationConfigGenerationPolicy InGenerationPolicy)
{
Name = InName;
LoadingPolicy = InLoadingPolicy;
ConfigGenerationPolicy = InGenerationPolicy;
}
///
/// Constructs a LocalizationTargetDescriptor from a Json object
///
///
/// The new localization target descriptor
public static LocalizationTargetDescriptor FromJsonObject(JsonObject InObject)
{
LocalizationTargetDescriptor descriptor = new LocalizationTargetDescriptor(InObject.GetStringField("Name"), InObject.GetEnumField("LoadingPolicy"));
LocalizationConfigGenerationPolicy policy;
if (InObject.TryGetEnumField("ConfigGenerationPolicy", out policy))
{
descriptor.ConfigGenerationPolicy = policy;
}
return descriptor;
}
///
/// Write this target to a JsonWriter
///
/// Writer to output to
void Write(JsonWriter Writer)
{
Writer.WriteObjectStart();
Writer.WriteValue("Name", Name);
Writer.WriteValue("LoadingPolicy", LoadingPolicy.ToString());
Writer.WriteValue("ConfigGenerationPolicy", ConfigGenerationPolicy.ToString());
Writer.WriteObjectEnd();
}
JsonObject ToJsonObject()
{
JsonObject localizationTargetObject = new JsonObject();
localizationTargetObject.AddOrSetFieldValue("Name", Name);
localizationTargetObject.AddOrSetFieldValue("LoadingPolicy", LoadingPolicy.ToString());
localizationTargetObject.AddOrSetFieldValue("ConfigGenerationPolicy", ConfigGenerationPolicy.ToString());
return localizationTargetObject;
}
///
/// Write an array of target descriptors
///
/// The Json writer to output to
/// Name of the array
/// Array of targets
public static void WriteArray(JsonWriter Writer, string Name, LocalizationTargetDescriptor[]? Targets)
{
if (Targets != null && Targets.Length > 0)
{
Writer.WriteArrayStart(Name);
foreach (LocalizationTargetDescriptor Target in Targets)
{
Target.Write(Writer);
}
Writer.WriteArrayEnd();
}
}
///
/// Updates a JsonObject with an array of localization target descriptors.
///
/// The Json object to update.
/// Name of the array
/// Array of targets
public static void UpdateJson(JsonObject InObject, string Name, LocalizationTargetDescriptor[]? Targets)
{
if (Targets != null && Targets.Length > 0)
{
JsonObject[] JsonObjects = Targets.Select(X => X.ToJsonObject()).ToArray();
InObject.AddOrSetFieldValue(Name, JsonObjects);
}
else
{
// The loaded plugin descriptor could have an existing localization target descriptor
// If we remove it in memory, we should try and also remove it from the cached json in this case
InObject.RemoveField(Name);
}
}
}
}