1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

Added a generic version of IGenericDataHolder.SetData

This commit is contained in:
Ell 2022-11-27 12:34:07 +01:00
parent e5cfebef3b
commit d72b094a7a
3 changed files with 28 additions and 5 deletions

View file

@ -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

View file

@ -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<string, JsonTypeSafeWrapper> data;
/// <inheritdoc />
[Obsolete("This method will be removed in a future update in favor of the generic SetData<T>.")]
public void SetData(string key, object data) {
if (data == default) {
this.SetData<object>(key, data);
}
/// <inheritdoc />
public void SetData<T>(string key, T data) {
if (EqualityComparer<T>.Default.Equals(data, default)) {
if (this.data != null)
this.data.Remove(key);
} else {
if (this.data == null)
this.data = new Dictionary<string, JsonTypeSafeWrapper>();
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<T>(data) : JsonTypeSafeWrapper.Of(data);
}
}

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
namespace MLEM.Misc {
@ -12,8 +13,14 @@ namespace MLEM.Misc {
private Dictionary<string, object> data;
/// <inheritdoc />
[Obsolete("This method will be removed in a future update in favor of the generic SetData<T>.")]
public void SetData(string key, object data) {
if (data == default) {
this.SetData<object>(key, data);
}
/// <inheritdoc />
public void SetData<T>(string key, T data) {
if (EqualityComparer<T>.Default.Equals(data, default)) {
if (this.data != null)
this.data.Remove(key);
} else {
@ -50,8 +57,16 @@ namespace MLEM.Misc {
/// </summary>
/// <param name="key">The key to store the data by</param>
/// <param name="data">The data to store in the object</param>
[Obsolete("This method will be removed in a future update in favor of the generic SetData<T>.")]
void SetData(string key, object data);
/// <summary>
/// Store a piece of generic data on this object.
/// </summary>
/// <param name="key">The key to store the data by</param>
/// <param name="data">The data to store in the object</param>
void SetData<T>(string key, T data);
/// <summary>
/// Returns a piece of generic data of the given type on this object.
/// </summary>