Allow mutating the player house item (door, etc)

This commit is contained in:
Kurt 2020-05-01 19:28:24 -07:00
parent 5f7585ca0f
commit 7c7a4fe86f
2 changed files with 31 additions and 1 deletions

View File

@ -1,8 +1,10 @@
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace NHSE.Core
{
[StructLayout(LayoutKind.Explicit, Size = SIZE, Pack = 1)]
[TypeConverter(typeof(ValueTypeTypeConverter))]
public struct GSaveItemName
{
public const int SIZE = 0x08;
@ -17,5 +19,7 @@ public struct GSaveItemName
public bool Equals(GSaveItemName obj) => obj.UniqueID == UniqueID && obj.SystemParam == SystemParam && obj.AdditionalParam == AdditionalParam;
public static bool operator ==(GSaveItemName left, GSaveItemName right) => left.Equals(right);
public static bool operator !=(GSaveItemName left, GSaveItemName right) => !(left == right);
public override string ToString() => UniqueID.ToString();
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections;
using System.ComponentModel;
namespace NHSE.Core
{
/// <summary>
/// Used for allowing a struct to be mutated in a PropertyGrid.
/// </summary>
public class ValueTypeTypeConverter : ExpandableObjectConverter
{
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) => true;
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
object boxed = context.PropertyDescriptor.GetValue(context.Instance);
foreach (DictionaryEntry entry in propertyValues)
{
var pi = context.PropertyDescriptor.PropertyType.GetProperty(entry.Key.ToString());
if (pi?.CanWrite == true)
pi.SetValue(boxed, Convert.ChangeType(entry.Value, pi.PropertyType), null);
}
return boxed;
}
}
}