1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

Compare commits

..

No commits in common. "92353e40e62c92280934bd37ac2b6299813aaef8" and "810406fb940b40d2f0999dee8a1a3e5467b86dbd" have entirely different histories.

5 changed files with 23 additions and 13 deletions

View file

@ -39,7 +39,6 @@ Fixes
- Fixed Combination.IsModifierDown querying one of its modifiers instead of all of them
Removals
- Removed DataContract attribute from GenericDataHolder
- Marked EnumHelper as obsolete due to its reimplementation in [DynamicEnums](https://www.nuget.org/packages/DynamicEnums)
### MLEM.Ui
@ -80,8 +79,6 @@ Improvements
- Multi-target net452, making MLEM compatible with MonoGame for consoles
- Added trimming and AOT annotations and made MLEM.Data trimmable
- Store a RuntimeTexturePacker packed texture region's source region
- Use JSON.NET attributes in favor of DataContract and DataMember
- Allow adding JsonTypeSafeWrapper instances to JsonTypeSafeGenericDataHolder directly
Fixes
- Fixed data texture atlases not allowing most characters in their region names

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using MLEM.Misc;
using Newtonsoft.Json;
@ -6,8 +7,8 @@ namespace MLEM.Data.Json {
/// <summary>
/// An <see cref="IGenericDataHolder"/> represents an object that can hold generic key-value based data.
/// This class uses <see cref="JsonTypeSafeWrapper"/> for each object stored to ensure that objects with a custom <see cref="JsonConverter"/> get deserialized as an instance of their original type if <see cref="JsonSerializer.TypeNameHandling"/> is not set to <see cref="TypeNameHandling.None"/>.
/// Note that, using <see cref="SetData"/>, adding <see cref="JsonTypeSafeWrapper{T}"/> instances directly is also possible.
/// </summary>
[DataContract]
#if NET7_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for instantiation of JsonTypeSafeWrapper instances might not be available at runtime.")]
#endif
@ -15,7 +16,7 @@ namespace MLEM.Data.Json {
private static readonly string[] EmptyStrings = new string[0];
[JsonProperty]
[DataMember(EmitDefaultValue = false)]
private Dictionary<string, JsonTypeSafeWrapper> data;
/// <inheritdoc />
@ -26,7 +27,7 @@ namespace MLEM.Data.Json {
} else {
if (this.data == null)
this.data = new Dictionary<string, JsonTypeSafeWrapper>();
this.data[key] = data as JsonTypeSafeWrapper ?? JsonTypeSafeWrapper.Of(data);
this.data[key] = JsonTypeSafeWrapper.Of(data);
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json;
namespace MLEM.Data.Json {
@ -14,7 +15,6 @@ namespace MLEM.Data.Json {
/// <summary>
/// Returns this json type-safe wrapper's value as an <see cref="object"/>.
/// </summary>
[JsonIgnore]
public abstract object Value { get; }
/// <summary>
@ -45,12 +45,12 @@ namespace MLEM.Data.Json {
}
/// <inheritdoc />
[DataContract]
public class JsonTypeSafeWrapper<T> : JsonTypeSafeWrapper {
/// <inheritdoc />
public override object Value => this.value;
[JsonProperty]
[DataMember]
private readonly T value;
/// <summary>

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace MLEM.Misc {
/// <summary>
@ -6,9 +7,12 @@ namespace MLEM.Misc {
/// A lot of MLEM components extend this class to allow for users to add additional data to them easily.
/// This <see cref="IGenericDataHolder"/> implemention uses an underlying <see cref="Dictionary{String,Object}"/> that only keeps track of non-default values.
/// </summary>
[DataContract]
public class GenericDataHolder : IGenericDataHolder {
private static readonly string[] EmptyStrings = new string[0];
[DataMember(EmitDefaultValue = false)]
private Dictionary<string, object> data;
/// <inheritdoc />

View file

@ -37,16 +37,24 @@ public class DataTests {
[Test]
public void TestJsonTypeSafety() {
var serializer = new JsonSerializer {TypeNameHandling = TypeNameHandling.Auto};
// normal generic data holder should crush the time span down to a string due to its custom serializer
var data = new GenericDataHolder();
data.SetData("Time", TimeSpan.FromMinutes(5));
var read = DataTests.SerializeAndDeserialize(serializer, data);
Assert.IsNotInstanceOf<TimeSpan>(read.GetData<object>("Time"));
Assert.Throws<InvalidCastException>(() => read.GetData<TimeSpan>("Time"));
// json type safe generic data holder should wrap the time span to ensure that it stays a time span
var safeData = new JsonTypeSafeGenericDataHolder();
// data holder should wrap the time span to ensure that it stays a time span
safeData.SetData("Time", TimeSpan.FromMinutes(5));
var safeRead = DataTests.SerializeAndDeserialize(safeData);
var safeRead = DataTests.SerializeAndDeserialize(serializer, safeData);
Assert.IsInstanceOf<TimeSpan>(safeRead.GetData<object>("Time"));
Assert.DoesNotThrow(() => safeRead.GetData<TimeSpan>("Time"));
}
private static T SerializeAndDeserialize<T>(T t) {
var serializer = new JsonSerializer {TypeNameHandling = TypeNameHandling.Auto};
private static T SerializeAndDeserialize<T>(JsonSerializer serializer, T t) {
var writer = new StringWriter();
serializer.Serialize(writer, t);
return serializer.Deserialize<T>(new JsonTextReader(new StringReader(writer.ToString())));