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

update: LazyAPI Support for generating default configuration files #574

Merged
merged 2 commits into from
Nov 21, 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
23 changes: 20 additions & 3 deletions src/LazyAPI/ConfigFiles/CultureContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,35 @@ public class CultureContractResolver : DefaultContractResolver
{
private readonly CultureInfo _culture;

private readonly Func<Type, object[]?, JsonConverter> CreateJsonConverterInstance;

public CultureContractResolver(CultureInfo cultureInfo)
{
this._culture = cultureInfo;
this.CreateJsonConverterInstance = (Func<Type, object[]?, JsonConverter>) Delegate.CreateDelegate(typeof(Func<Type, object[]?, JsonConverter>), typeof(JsonConvert).Assembly.GetType("Newtonsoft.Json.Serialization.JsonTypeReflector")?.GetMethod("CreateJsonConverterInstance", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)!);
}

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
var languages = member.GetCustomAttributes<LocalizedPropertyNameAttribute>();
property.PropertyName = languages.FirstOrDefault(x => x.Type == this._culture.Name)?.Text
?? languages.FirstOrDefault(x => x.Type == CultureType.Chinese)?.Text
?? property.PropertyName;
var currentLanguage = languages.FirstOrDefault(x => x.Type == this._culture.Name) ?? languages.FirstOrDefault(x => x.Type == CultureType.Chinese);
if (currentLanguage != null)
{
property.PropertyName = currentLanguage.Text;
property.Order = currentLanguage._order;
property.Required = currentLanguage.Required;
property.DefaultValueHandling = currentLanguage._defaultValueHandling;
property.NullValueHandling = currentLanguage._nullValueHandling;
property.ReferenceLoopHandling = currentLanguage._referenceLoopHandling;
property.ObjectCreationHandling = currentLanguage._objectCreationHandling;
property.TypeNameHandling = currentLanguage._typeNameHandling;
property.IsReference = currentLanguage._isReference;
property.ItemIsReference = currentLanguage._itemIsReference;
property.ItemConverter = currentLanguage.ItemConverterType != null ? this.CreateJsonConverterInstance(currentLanguage.ItemConverterType, currentLanguage.ItemConverterParameters) : null;
property.ItemReferenceLoopHandling = currentLanguage._itemReferenceLoopHandling;
property.ItemTypeNameHandling = currentLanguage._itemTypeNameHandling;
}
return property;
}
}
8 changes: 8 additions & 0 deletions src/LazyAPI/ConfigFiles/JsonConfigBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace LazyAPI.ConfigFiles;

protected virtual string Filename => typeof(T).Namespace ?? typeof(T).Name;

protected virtual void SetDefault()
{
}

private string FullFilename => Path.Combine(TShock.SavePath, $"{this.Filename}.{cultureInfo.Name}.json");

protected JsonConfigBase()
Expand Down Expand Up @@ -44,6 +48,10 @@ private static T GetConfig()
{
return JsonConvert.DeserializeObject<T>(File.ReadAllText(file), _settings) ?? new();
}
else
{
t.SetDefault();
}
File.WriteAllText(file, JsonConvert.SerializeObject(t, _settings));
return t;
}
Expand Down
190 changes: 187 additions & 3 deletions src/LazyAPI/ConfigFiles/LocalizedPropertyNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,197 @@

/**
* Copyright (c) 2007 James Newton-King
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using Newtonsoft.Json;

namespace LazyAPI.ConfigFiles;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class LocalizedPropertyNameAttribute : Attribute
{
public string Text { get;}
public string Text { get; }

public string Type { get;}
public string Type { get; }


internal NullValueHandling? _nullValueHandling;
internal DefaultValueHandling? _defaultValueHandling;
internal ReferenceLoopHandling? _referenceLoopHandling;
internal ObjectCreationHandling? _objectCreationHandling;
internal TypeNameHandling? _typeNameHandling;
internal bool? _isReference;
internal int? _order;
internal Required? _required;
internal bool? _itemIsReference;
internal ReferenceLoopHandling? _itemReferenceLoopHandling;
internal TypeNameHandling? _itemTypeNameHandling;

/// <summary>
/// Gets or sets the <see cref="JsonConverter"/> type used when serializing the property's collection items.
/// </summary>
/// <value>The collection's items <see cref="JsonConverter"/> type.</value>
public Type? ItemConverterType { get; set; }

/// <summary>
/// The parameter list to use when constructing the <see cref="JsonConverter"/> described by <see cref="ItemConverterType"/>.
/// If <c>null</c>, the default constructor is used.
/// When non-<c>null</c>, there must be a constructor defined in the <see cref="JsonConverter"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
/// <code>
/// [JsonProperty(ItemConverterType = typeof(MyContainerConverter), ItemConverterParameters = new object[] { 123, "Four" })]
/// </code>
/// </example>
public object[]? ItemConverterParameters { get; set; }

/// <summary>
/// Gets or sets the <see cref="Type"/> of the <see cref="NamingStrategy"/>.
/// </summary>
/// <value>The <see cref="Type"/> of the <see cref="NamingStrategy"/>.</value>
public Type? NamingStrategyType { get; set; }

/// <summary>
/// The parameter list to use when constructing the <see cref="NamingStrategy"/> described by <see cref="JsonPropertyAttribute.NamingStrategyType"/>.
/// If <c>null</c>, the default constructor is used.
/// When non-<c>null</c>, there must be a constructor defined in the <see cref="NamingStrategy"/> that exactly matches the number,
/// order, and type of these parameters.
/// </summary>
/// <example>
/// <code>
/// [JsonProperty(NamingStrategyType = typeof(MyNamingStrategy), NamingStrategyParameters = new object[] { 123, "Four" })]
/// </code>
/// </example>
public object[]? NamingStrategyParameters { get; set; }

/// <summary>
/// Gets or sets the null value handling used when serializing this property.
/// </summary>
/// <value>The null value handling.</value>
public NullValueHandling NullValueHandling
{
get => this._nullValueHandling ?? default;
set => this._nullValueHandling = value;
}

/// <summary>
/// Gets or sets the default value handling used when serializing this property.
/// </summary>
/// <value>The default value handling.</value>
public DefaultValueHandling DefaultValueHandling
{
get => this._defaultValueHandling ?? default;
set => this._defaultValueHandling = value;
}

/// <summary>
/// Gets or sets the reference loop handling used when serializing this property.
/// </summary>
/// <value>The reference loop handling.</value>
public ReferenceLoopHandling ReferenceLoopHandling
{
get => this._referenceLoopHandling ?? default;
set => this._referenceLoopHandling = value;
}

/// <summary>
/// Gets or sets the object creation handling used when deserializing this property.
/// </summary>
/// <value>The object creation handling.</value>
public ObjectCreationHandling ObjectCreationHandling
{
get => this._objectCreationHandling ?? default;
set => this._objectCreationHandling = value;
}

/// <summary>
/// Gets or sets the type name handling used when serializing this property.
/// </summary>
/// <value>The type name handling.</value>
public TypeNameHandling TypeNameHandling
{
get => this._typeNameHandling ?? default;
set => this._typeNameHandling = value;
}

/// <summary>
/// Gets or sets whether this property's value is serialized as a reference.
/// </summary>
/// <value>Whether this property's value is serialized as a reference.</value>
public bool IsReference
{
get => this._isReference ?? default;
set => this._isReference = value;
}

/// <summary>
/// Gets or sets the order of serialization of a member.
/// </summary>
/// <value>The numeric order of serialization.</value>
public int Order
{
get => this._order ?? default;
set => this._order = value;
}

/// <summary>
/// Gets or sets a value indicating whether this property is required.
/// </summary>
/// <value>
/// A value indicating whether this property is required.
/// </value>
public Required Required
{
get => this._required ?? Required.Default;
set => this._required = value;
}

/// <summary>
/// Gets or sets the reference loop handling used when serializing the property's collection items.
/// </summary>
/// <value>The collection's items reference loop handling.</value>
public ReferenceLoopHandling ItemReferenceLoopHandling
{
get => this._itemReferenceLoopHandling ?? default;
set => this._itemReferenceLoopHandling = value;
}

/// <summary>
/// Gets or sets the type name handling used when serializing the property's collection items.
/// </summary>
/// <value>The collection's items type name handling.</value>
public TypeNameHandling ItemTypeNameHandling
{
get => this._itemTypeNameHandling ?? default;
set => this._itemTypeNameHandling = value;
}

/// <summary>
/// Gets or sets whether this property's collection items are serialized as a reference.
/// </summary>
/// <value>Whether this property's collection items are serialized as a reference.</value>
public bool ItemIsReference
{
get => this._itemIsReference ?? default;
set => this._itemIsReference = value;
}

public LocalizedPropertyNameAttribute(string type, string text)
{
Expand Down
1 change: 0 additions & 1 deletion src/LazyAPI/Database/RecordBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LinqToDB;
using LinqToDB.Data;
using System.Configuration.Provider;
using TShockAPI;
using TShockAPI.DB;
using SqlType = TShockAPI.DB.SqlType;
Expand Down
4 changes: 2 additions & 2 deletions src/LazyAPI/LazyPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public override void Initialize()
{
}

private readonly List<Type> restToLoad = new ();
private readonly List<Type> restToLoad = new();

internal void AutoLoad()
{
foreach (var type in this.GetType().Assembly.GetTypes())
Expand Down
2 changes: 1 addition & 1 deletion src/LazyAPI/PluginContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LazyAPI;
public class PluginContainer : LazyPlugin
{
public override string Author => "cc004 & members of UnrealMultiple";
public override Version Version => new (1, 0, 0, 1);
public override Version Version => new(1, 0, 0, 2);

public PluginContainer(Main game) : base(game) { }
public override void Initialize()
Expand Down
3 changes: 3 additions & 0 deletions src/LazyAPI/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
## 更新日志

```
V1.0.0.2
支持生成默认配置,多语言更多操作的支持!

v1.0.0.1
初步支持多语言配置文件

Expand Down
Loading