Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing CustomModules #2753

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions Exiled.CustomModules/API/Features/CustomModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ namespace Exiled.CustomModules.API.Features
using Exiled.API.Features.Attributes;
using Exiled.API.Features.Core;
using Exiled.API.Features.DynamicEvents;
using Exiled.API.Features.Serialization;
using Exiled.API.Features.Serialization.CustomConverters;
using Exiled.API.Interfaces;
using Exiled.CustomModules.API.Enums;
using Exiled.CustomModules.API.Features.Attributes;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NodeDeserializers;

/// <summary>
/// Represents a marker class for custom modules.
Expand Down Expand Up @@ -209,6 +212,39 @@ internal string PointerPath
}
}

/// <summary>
/// Gets or sets the serializer for custom modules.
/// </summary>
private static ISerializer ModuleSerializer { get; set; } = new SerializerBuilder()
.WithTypeConverter(new VectorsConverter())
.WithTypeConverter(new ColorConverter())
.WithTypeConverter(new AttachmentIdentifiersConverter())
.WithTypeConverter(new EnumClassConverter())
.WithTypeConverter(new PrivateConstructorConverter())
.WithEventEmitter(eventEmitter => new TypeAssigningEventEmitter(eventEmitter))
.WithTypeInspector(inner => new CommentGatheringTypeInspector(inner))
.WithEmissionPhaseObjectGraphVisitor(args => new CommentsObjectGraphVisitor(args.InnerVisitor))
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.IgnoreFields()
.DisableAliases()
.Build();

/// <summary>
/// Gets or sets the deserializer for custom modules.
/// </summary>
private static IDeserializer ModuleDeserializer { get; set; } = new DeserializerBuilder()
.WithTypeConverter(new VectorsConverter())
.WithTypeConverter(new ColorConverter())
.WithTypeConverter(new AttachmentIdentifiersConverter())
.WithTypeConverter(new EnumClassConverter())
.WithTypeConverter(new PrivateConstructorConverter())
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithNodeDeserializer(inner => new CustomModuleDeserializer(), deserializer => deserializer.InsteadOf<ObjectNodeDeserializer>())
.WithDuplicateKeyChecking()
.IgnoreFields()
.IgnoreUnmatchedProperties()
.Build();

/// <summary>
/// Compares two operands: <see cref="CustomModule"/> and <see cref="object"/>.
/// </summary>
Expand Down Expand Up @@ -424,13 +460,13 @@ public void SerializeModule()

if (File.Exists(FilePath) && File.Exists(PointerPath))
{
File.WriteAllText(FilePath, EConfig.Serializer.Serialize(this));
File.WriteAllText(PointerPath, EConfig.Serializer.Serialize(Config));
File.WriteAllText(FilePath, ModuleSerializer.Serialize(this));
File.WriteAllText(PointerPath, ModuleSerializer.Serialize(Config));
return;
}

File.WriteAllText(FilePath ?? throw new ArgumentNullException(nameof(FilePath)), EConfig.Serializer.Serialize(this));
File.WriteAllText(PointerPath ?? throw new ArgumentNullException(nameof(PointerPath)), EConfig.Serializer.Serialize(Config));
File.WriteAllText(FilePath ?? throw new ArgumentNullException(nameof(FilePath)), ModuleSerializer.Serialize(this));
File.WriteAllText(PointerPath ?? throw new ArgumentNullException(nameof(PointerPath)), ModuleSerializer.Serialize(Config));
}

/// <summary>
Expand All @@ -454,7 +490,7 @@ public void DeserializeModule()
{
try
{
Config = EConfig.Deserializer.Deserialize<ModulePointer>(File.ReadAllText(PointerPath));
Config = ModuleDeserializer.Deserialize<ModulePointer>(File.ReadAllText(PointerPath));
}
catch
{
Expand All @@ -470,7 +506,7 @@ public void DeserializeModule()
return;
}

CustomModule deserializedModule = EConfig.Deserializer.Deserialize(File.ReadAllText(FilePath), GetType()) as CustomModule;
CustomModule deserializedModule = ModuleDeserializer.Deserialize(File.ReadAllText(FilePath), GetType()) as CustomModule;
CopyProperties(deserializedModule);

foreach (string file in Directory.GetFiles(ChildPath))
Expand All @@ -480,7 +516,7 @@ public void DeserializeModule()

try
{
Config = EConfig.Deserializer.Deserialize<ModulePointer>(File.ReadAllText(file));
Config = ModuleDeserializer.Deserialize<ModulePointer>(File.ReadAllText(file));
}
catch
{
Expand Down
37 changes: 37 additions & 0 deletions Exiled.CustomModules/API/Features/CustomModuleDeserializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// -----------------------------------------------------------------------
// <copyright file="CustomModuleDeserializer.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.CustomModules.API.Features
{
using System;

using Exiled.CustomModules.API.Features.CustomRoles;
using Exiled.CustomModules.API.Features.Deserializers;
using YamlDotNet.Core;
using YamlDotNet.Serialization;

/// <inheritdoc />
public class CustomModuleDeserializer : INodeDeserializer
{
/// <inheritdoc />
public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
{
if (CustomRoleDeserializer.IsCustomRoleType(expectedType))
{
return CustomRoleDeserializer.Deserialize(parser, expectedType, nestedObjectDeserializer, out value);
}

if (expectedType == typeof(RoleSettings))
{
return RoleSettingsDeserializer.Deserialize(parser, expectedType, nestedObjectDeserializer, out value);
}

value = null;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// -----------------------------------------------------------------------
// <copyright file="CustomRoleDeserializer.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.CustomModules.API.Features.Deserializers
{
using System;
using System.Reflection;

using Exiled.CustomModules.API.Features.CustomRoles;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

/// <summary>
/// The deserializer for Custom Roles.
/// </summary>
public static class CustomRoleDeserializer
{
/// <summary>
/// The actual deserializer.
/// </summary>
/// <param name="parser">The Yaml Parser.</param>
/// <param name="expectedType">The type.</param>
/// <param name="nestedObjectDeserializer">The base deserializer as backup.</param>
/// <param name="value">If valid, returns this.</param>
/// <returns>A bool stating if it was successful or not.</returns>
public static bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
{
value = Activator.CreateInstance(expectedType);
parser.Consume<MappingStart>();

while (parser.TryConsume<Scalar>(out Scalar scalar))
{
PropertyInfo property = expectedType.GetProperty(scalar.Value, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (property != null)
{
object propertyValue = nestedObjectDeserializer(parser, property.PropertyType);
property.SetValue(value, propertyValue);
}
else if (scalar.Value.Equals("settings", StringComparison.OrdinalIgnoreCase))
{
PropertyInfo settingsProperty = expectedType.GetProperty("Settings", BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (settingsProperty != null)
{
object settingsValue = nestedObjectDeserializer(parser, settingsProperty.PropertyType);
settingsProperty.SetValue(value, settingsValue);
}
}
else
{
// Skip unknown properties
parser.SkipThisAndNestedEvents();
}
}

parser.Consume<MappingEnd>();
return true;
}

/// <summary>
/// A function that returns whether a type is a custom role.
/// </summary>
/// <param name="type">The Type.</param>
/// <returns>A bool that says if the type is a custom role.</returns>
public static bool IsCustomRoleType(Type type)
{
while (type != null)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(CustomRole<>))
{
return true;
}

type = type.BaseType;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// -----------------------------------------------------------------------
// <copyright file="RoleSettingsDeserializer.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.CustomModules.API.Features.Deserializers
{
using System;
using System.Reflection;

using Exiled.CustomModules.API.Features.CustomRoles;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;

/// <summary>
/// The deserializer for Role Settings.
/// </summary>
public static class RoleSettingsDeserializer
{
/// <summary>
/// The actual deserializer.
/// </summary>
/// <param name="parser">The Yaml Parser.</param>
/// <param name="expectedType">The type.</param>
/// <param name="nestedObjectDeserializer">The base deserializer as backup.</param>
/// <param name="value">If valid, returns this.</param>
/// <returns>A bool stating if it was successful or not.</returns>
public static bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
{
RoleSettings roleSettings = new RoleSettings();
parser.Consume<MappingStart>();

while (parser.TryConsume<Scalar>(out Scalar scalar))
{
PropertyInfo property = typeof(RoleSettings).GetProperty(scalar.Value, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (property != null)
{
object propertyValue = nestedObjectDeserializer(parser, property.PropertyType);
property.SetValue(roleSettings, propertyValue);
}
else
{
// If the property does not exist, skip the scalar value
parser.SkipThisAndNestedEvents();
}
}

parser.Consume<MappingEnd>();
value = roleSettings;
return true;
}
}
}
Loading