diff --git a/CHANGELOG.md b/CHANGELOG.md index 07d0d9f..bb6c257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ Improvements - Allow specifying percentage-based padding for a NinePatch - Improved the way InputHandler down time calculation works - Allow explicitly specifying each region for extended auto tiles +- Added a generic version of IGenericDataHolder.SetData - **Drastically improved StaticSpriteBatch batching performance** - **Made GenericFont and TokenizedString support UTF-32 characters like emoji** @@ -87,7 +88,6 @@ Improvements - 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 diff --git a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs index 2146ced..496e44c 100644 --- a/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs +++ b/MLEM.Data/Json/JsonTypeSafeGenericDataHolder.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using MLEM.Misc; using Newtonsoft.Json; @@ -19,14 +20,21 @@ namespace MLEM.Data.Json { private Dictionary data; /// + [Obsolete("This method will be removed in a future update in favor of the generic SetData.")] public void SetData(string key, object data) { - if (data == default) { + this.SetData(key, data); + } + + /// + public void SetData(string key, T data) { + if (EqualityComparer.Default.Equals(data, default)) { if (this.data != null) this.data.Remove(key); } else { if (this.data == null) this.data = new Dictionary(); - this.data[key] = data as JsonTypeSafeWrapper ?? JsonTypeSafeWrapper.Of(data); + // if types already match exactly, we don't need to use Of (which requires dynamic code) + this.data[key] = data.GetType() == typeof(T) ? new JsonTypeSafeWrapper(data) : JsonTypeSafeWrapper.Of(data); } } diff --git a/MLEM/Misc/GenericDataHolder.cs b/MLEM/Misc/GenericDataHolder.cs index 4014ca9..fb425e4 100644 --- a/MLEM/Misc/GenericDataHolder.cs +++ b/MLEM/Misc/GenericDataHolder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace MLEM.Misc { @@ -12,8 +13,14 @@ namespace MLEM.Misc { private Dictionary data; /// + [Obsolete("This method will be removed in a future update in favor of the generic SetData.")] public void SetData(string key, object data) { - if (data == default) { + this.SetData(key, data); + } + + /// + public void SetData(string key, T data) { + if (EqualityComparer.Default.Equals(data, default)) { if (this.data != null) this.data.Remove(key); } else { @@ -50,8 +57,16 @@ namespace MLEM.Misc { /// /// The key to store the data by /// The data to store in the object + [Obsolete("This method will be removed in a future update in favor of the generic SetData.")] void SetData(string key, object data); + /// + /// Store a piece of generic data on this object. + /// + /// The key to store the data by + /// The data to store in the object + void SetData(string key, T data); + /// /// Returns a piece of generic data of the given type on this object. ///