//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.Persistence.SourceGenerator;
using System.Reflection;
using MUnique.OpenMU.DataModel;
///
/// Helper class containing shared functionality for model generators.
///
internal static class ModelGeneratorHelper
{
private static IList _customTypes;
///
/// Gets a header template for a generated file.
///
public static string FileHeaderTemplate => @"//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
//------------------------------------------------------------------------------
//
// This source code was auto-generated by a roslyn code generator.
//
//------------------------------------------------------------------------------
// ReSharper disable All";
///
/// Gets the namespace of the configuration classes.
///
public static string ConfigurationNamespace => "MUnique.OpenMU.DataModel.Configuration";
///
/// Gets the types which need to be customized for persistence.
///
public static IEnumerable CustomTypes => _customTypes ??= GetCustomTypes();
///
/// Determines whether the given type is a configuration type.
///
/// The type.
/// true if the given type is a configuration type; otherwise, false.
public static bool IsConfigurationType(Type type)
{
if (type.Namespace != null
&& type.Namespace.StartsWith(ConfigurationNamespace, StringComparison.InvariantCulture))
{
return true;
}
if (type.BaseType is { Namespace: { } }
&& type.BaseType.Namespace.StartsWith(ConfigurationNamespace, StringComparison.InvariantCulture))
{
return true;
}
if (type.Name.Contains("Definition", StringComparison.InvariantCulture))
{
return true;
}
if (type.Name is "AttributeRelationship" or "PlugInConfiguration" or "ConstValueAttribute")
{
return true;
}
return false;
}
///
/// Gets the parameter definitions for a method or constructor.
///
/// The parameters.
/// The string of the parameter definitions.
public static string GetParameterDefinitions(ICollection parameters)
{
var result = new StringBuilder();
foreach (var p in parameters)
{
result.Append(p.ParameterType.GetCSharpFullName())
.Append(" ")
.Append(p.Name);
if (parameters.Count > p.Position + 1)
{
result.Append(", ");
}
}
return result.ToString();
}
///
/// Gets the parameters used to call a method.
///
/// The parameter infos.
/// The parameters used to call a method.
public static string GetParameters(ICollection parameters)
{
var result = new StringBuilder();
foreach (var p in parameters)
{
result.Append(p.Name);
if (parameters.Count > p.Position + 1)
{
result.Append(", ");
}
}
return result.ToString();
}
///
/// Overrides the implementation, so that the correct class instance
/// is created and the id is assigned.
///
/// The type.
/// Name of the class.
/// The implementation for .
public static string OverrideClonable(Type type, string className)
{
return $$"""
///
public override {{type.Namespace}}.{{className}} Clone(MUnique.OpenMU.DataModel.Configuration.GameConfiguration gameConfiguration)
{
var clone = new {{className}}();
clone.AssignValuesOf(this, gameConfiguration);
return clone;
}
///
public override void AssignValuesOf({{type.Namespace}}.{{className}} other, MUnique.OpenMU.DataModel.Configuration.GameConfiguration gameConfiguration)
{
base.AssignValuesOf(other, gameConfiguration);
this.Id = other.GetId();
}
""";
}
///
/// Determines the types which require customization.
///
/// The types that require customization.
private static List GetCustomTypes()
{
var result = new List();
var loadedTypes = typeof(DataModel.Attributes.PowerUpDefinition).Assembly.GetTypes()
.Where(type => type.IsClass && type.IsPublic)
.Where(type => !type.IsSealed && !type.IsAbstract && type.GetConstructor([]) != null).ToList();
result.AddRange(loadedTypes);
result.Add(typeof(MUnique.OpenMU.AttributeSystem.AttributeDefinition));
result.Add(typeof(MUnique.OpenMU.AttributeSystem.StatAttribute));
result.Add(typeof(MUnique.OpenMU.AttributeSystem.ConstValueAttribute));
result.Add(typeof(MUnique.OpenMU.AttributeSystem.AttributeRelationship));
result.Add(typeof(MUnique.OpenMU.Interfaces.LetterHeader));
result.Add(typeof(MUnique.OpenMU.Interfaces.Friend));
result.Add(typeof(MUnique.OpenMU.PlugIns.PlugInConfiguration));
return result;
}
}