Add Default auto-generated member to scripting structures and improve deserialization

This commit is contained in:
Wojtek Figat
2022-08-01 18:49:54 +02:00
parent 4915e9fea0
commit fe1cab6a7f
11 changed files with 57 additions and 65 deletions

View File

@@ -6,11 +6,6 @@ namespace FlaxEngine
{
partial struct D6JointDrive
{
/// <summary>
/// The default <see cref="D6JointDrive"/> structure.
/// </summary>
public static readonly D6JointDrive Default = new D6JointDrive(0.0f, 0.0f, float.MaxValue, false);
/// <summary>
/// Initializes a new instance of the <see cref="D6JointDrive"/> struct.
/// </summary>

View File

@@ -4,11 +4,6 @@ namespace FlaxEngine
{
partial struct HingeJointDrive
{
/// <summary>
/// The default <see cref="HingeJointDrive"/> structure.
/// </summary>
public static readonly HingeJointDrive Default = new HingeJointDrive(0.0f, float.MaxValue, 1.0f, false);
/// <summary>
/// Initializes a new instance of the <see cref="HingeJointDrive"/> struct.
/// </summary>

View File

@@ -4,11 +4,6 @@ namespace FlaxEngine
{
partial struct SpringParameters
{
/// <summary>
/// The default <see cref="SpringParameters"/> structure.
/// </summary>
public static readonly SpringParameters Default = new SpringParameters(0.0f, 0.0f);
/// <summary>
/// Constructs a spring.
/// </summary>
@@ -23,11 +18,6 @@ namespace FlaxEngine
partial struct LimitLinearRange
{
/// <summary>
/// The default <see cref="LimitLinearRange"/> structure with empty limit.
/// </summary>
public static readonly LimitLinearRange Default = new LimitLinearRange(0.0f, 0.0f);
/// <summary>
/// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
/// </summary>
@@ -62,11 +52,6 @@ namespace FlaxEngine
partial struct LimitLinear
{
/// <summary>
/// The default <see cref="LimitLinear"/> structure with empty limit.
/// </summary>
public static readonly LimitLinear Default = new LimitLinear(0.0f);
/// <summary>
/// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
/// </summary>
@@ -97,11 +82,6 @@ namespace FlaxEngine
partial struct LimitAngularRange
{
/// <summary>
/// The default <see cref="LimitAngularRange"/> structure with empty limit.
/// </summary>
public static readonly LimitAngularRange Default = new LimitAngularRange(0.0f, 0.0f);
/// <summary>
/// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
/// </summary>
@@ -136,11 +116,6 @@ namespace FlaxEngine
partial struct LimitConeRange
{
/// <summary>
/// The default <see cref="LimitConeRange"/> structure with a 45 degree cone limit.
/// </summary>
public static readonly LimitConeRange Default = new LimitConeRange(90.0f, 90.0f);
/// <summary>
/// Constructs a hard limit. Once the limit is reached the movement of the attached bodies will come to a stop.
/// </summary>

View File

@@ -29,7 +29,7 @@ API_ENUM() enum class WindowStartPosition
/// <summary>
/// Settings for new window.
/// </summary>
API_STRUCT() struct CreateWindowSettings
API_STRUCT(NoDefault) struct CreateWindowSettings
{
DECLARE_SCRIPTING_TYPE_MINIMAL(CreateWindowSettings);

View File

@@ -16,33 +16,8 @@ namespace FlaxEditor.Content.Settings
/// </summary>
public GraphicsSettings()
{
// Initialize PostFx settings with default options (C# structs doesn't support it)
// Initialize PostFx settings with default options (C# structs don't support it)
PostProcessSettings = FlaxEngine.PostProcessSettings.Default;
}
}
}
namespace FlaxEngine
{
partial struct PostProcessSettings
{
private static PostProcessSettings _default;
/// <summary>
/// The default <see cref="PostProcessSettings"/>.
/// </summary>
public static PostProcessSettings Default
{
get
{
if (!_default.AmbientOcclusion.Enabled)
{
object obj = _default;
Utils.InitStructure(obj, typeof(PostProcessSettings));
_default = (PostProcessSettings)obj;
}
return _default;
}
}
}
}

View File

@@ -49,7 +49,7 @@ API_ENUM() enum class TextWrapping
/// <summary>
/// Structure which describes text layout properties.
/// </summary>
API_STRUCT() struct TextLayoutOptions
API_STRUCT(NoDefault) struct TextLayoutOptions
{
DECLARE_SCRIPTING_TYPE_MINIMAL(TextLayoutOptions);

View File

@@ -928,7 +928,12 @@ namespace FlaxEngine
stream.Write(0);
}
internal static void InitStructure(object obj, Type type)
/// <summary>
/// Initializes the structure (value type) by inflating it with values from <see cref="System.ComponentModel.DefaultValueAttribute"/> (recursive).
/// </summary>
/// <param name="obj">The object to initialize.</param>
/// <param name="type">The structure type.</param>
public static void InitStructure(object obj, Type type)
{
var fields = type.GetFields(BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public);
for (var i = 0; i < fields.Length; i++)

View File

@@ -1148,6 +1148,7 @@ namespace Flax.Build.Bindings
indent += " ";
// Fields
var hasDefaultMember = false;
foreach (var fieldInfo in structureInfo.Fields)
{
contents.AppendLine();
@@ -1164,6 +1165,7 @@ namespace Flax.Build.Bindings
contents.Append("const ");
else if (fieldInfo.IsStatic)
contents.Append("static ");
hasDefaultMember |= string.Equals(fieldInfo.Name, "Default", StringComparison.Ordinal);
string type;
if (fieldInfo.Type.IsArray && (fieldInfo.NoArray || structureInfo.IsPod))
@@ -1234,6 +1236,45 @@ namespace Flax.Build.Bindings
}
}
// Default value
if (!structureInfo.NoDefault && !hasDefaultMember)
{
contents.AppendLine();
contents.Append(indent).AppendLine("private static bool _defaultValid;");
contents.Append(indent).AppendLine($"private static {structureInfo.Name} _default;");
contents.AppendLine();
contents.Append(indent).AppendLine("/// <summary>");
contents.Append(indent).AppendLine($"/// The default <see cref=\"{structureInfo.Name}\"/>.");
contents.Append(indent).AppendLine("/// </summary>");
contents.Append(indent).AppendLine($"public static {structureInfo.Name} Default");
contents.Append(indent).AppendLine("{");
indent += " ";
contents.Append(indent).AppendLine("get");
contents.Append(indent).AppendLine("{");
indent += " ";
contents.Append(indent).AppendLine("if (!_defaultValid)");
contents.Append(indent).AppendLine("{");
indent += " ";
contents.Append(indent).AppendLine("_defaultValid = true;");
contents.Append(indent).AppendLine("object obj = _default;");
contents.Append(indent).AppendLine($"FlaxEngine.Utils.InitStructure(obj, typeof({structureInfo.Name}));");
contents.Append(indent).AppendLine($"_default = ({structureInfo.Name})obj;");
indent = indent.Substring(0, indent.Length - 4);
contents.Append(indent).AppendLine("}");
contents.Append(indent).AppendLine("return _default;");
indent = indent.Substring(0, indent.Length - 4);
contents.Append(indent).AppendLine("}");
indent = indent.Substring(0, indent.Length - 4);
contents.Append(indent).AppendLine("}");
contents.AppendLine();
contents.Append(indent).AppendLine("[System.Runtime.Serialization.OnDeserializing]");
contents.Append(indent).AppendLine("internal void OnDeserializing(System.Runtime.Serialization.StreamingContext context)");
contents.Append(indent).AppendLine("{");
contents.Append(indent).AppendLine(" // Initialize structure with default values to replicate C++ deserialization behavior");
contents.Append(indent).AppendLine(" this = Default;");
contents.Append(indent).AppendLine("}");
}
// Nested types
foreach (var apiTypeInfo in structureInfo.Children)
{

View File

@@ -19,7 +19,7 @@ namespace Flax.Build.Bindings
partial class BindingsGenerator
{
private static readonly Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();
private const int CacheVersion = 15;
private const int CacheVersion = 16;
internal static void Write(BinaryWriter writer, string e)
{

View File

@@ -1113,6 +1113,9 @@ namespace Flax.Build.Bindings
case "nopod":
desc.ForceNoPod = true;
break;
case "nodefault":
desc.NoDefault = true;
break;
case "attributes":
desc.Attributes = tag.Value;
break;

View File

@@ -14,6 +14,7 @@ namespace Flax.Build.Bindings
public List<FieldInfo> Fields = new List<FieldInfo>();
public bool IsAutoSerialization;
public bool ForceNoPod;
public bool NoDefault;
public override bool IsStruct => true;
public override bool IsValueType => true;
@@ -72,6 +73,7 @@ namespace Flax.Build.Bindings
BindingsGenerator.Write(writer, Functions);
writer.Write(IsAutoSerialization);
writer.Write(ForceNoPod);
writer.Write(NoDefault);
base.Write(writer);
}
@@ -82,6 +84,7 @@ namespace Flax.Build.Bindings
Functions = BindingsGenerator.Read(reader, Functions);
IsAutoSerialization = reader.ReadBoolean();
ForceNoPod = reader.ReadBoolean();
NoDefault = reader.ReadBoolean();
base.Read(reader);
}