Fix support for accessing properties using reflection

The test still crashes the game
This commit is contained in:
Norbi Peti 2022-03-27 03:49:45 +02:00
parent c4a9125ed3
commit c0ef8f1fae
3 changed files with 20 additions and 9 deletions

View file

@ -82,7 +82,7 @@ namespace TechbloxModdingAPI.Blocks
yield break;
for (var index = 0; index < blocks.Length; index++)
{
if (index % 50 == 0) yield return new WaitForSecondsEnumerator(0.2f).Continue(); //The material or flipped status can only be changed 130 times per submission
if (index % 50 == 0) yield return new WaitForSecondsEnumerator(1f).Continue(); //The material or flipped status can only be changed 130 times per submission
var block = blocks[index];
if (!block.Exists) continue;
foreach (var property in block.GetType().GetProperties())

View file

@ -98,19 +98,21 @@ namespace TechbloxModdingAPI.Blocks.Engines
internal object GetBlockInfo(Block block, Type type, string name)
{
/*var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
.Invoke(this, new object[] { block }); - TODO: Cannot call method with by-ref return value
var str = AccessTools.Method(opt.GetType(), "Get").Invoke(opt, Array.Empty<object>());
return AccessTools.Field(str.GetType(), name).GetValue(str);*/
return AccessTools.Field(type, name).GetValue(Activator.CreateInstance(type));
var opt = AccessTools.Method(typeof(NativeApiExtensions), "QueryEntityOptional",
new[] { typeof(EntitiesDB), typeof(EcsObjectBase), typeof(ExclusiveGroupStruct) }, new[] { type })
.Invoke(null, new object[] { entitiesDB, block, null });
var str = AccessTools.Property(opt.GetType(), "Value").GetValue(opt);
return AccessTools.Field(str.GetType(), name).GetValue(str);
}
internal void SetBlockInfo(Block block, Type type, string name, object value)
{
/*var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
.Invoke(this, new object[] { block });
var str = AccessTools.Method(opt.GetType(), "Get").Invoke(opt, Array.Empty<object>());
AccessTools.Field(str.GetType(), name).SetValue(str, value);*/
var prop = AccessTools.Property(opt.GetType(), "Value");
var str = prop.GetValue(opt);
AccessTools.Field(str.GetType(), name).SetValue(str, value);
prop.SetValue(opt, str);
}
public void UpdateDisplayedBlock(EGID id)

View file

@ -64,6 +64,15 @@ namespace TechbloxModdingAPI.Utility
return ref managedArray[index];
}
/// <summary>
/// A non-by-ref view that allows getting and setting the value.
/// </summary>
public T Value
{
get => Get();
set => Get() = value;
}
public bool Exists => state != State.Empty;
public T? Nullable() => this ? Get() : default;