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; yield break;
for (var index = 0; index < blocks.Length; index++) 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]; var block = blocks[index];
if (!block.Exists) continue; if (!block.Exists) continue;
foreach (var property in block.GetType().GetProperties()) 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) internal object GetBlockInfo(Block block, Type type, string name)
{ {
/*var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type }) var opt = AccessTools.Method(typeof(NativeApiExtensions), "QueryEntityOptional",
.Invoke(this, new object[] { block }); - TODO: Cannot call method with by-ref return value new[] { typeof(EntitiesDB), typeof(EcsObjectBase), typeof(ExclusiveGroupStruct) }, new[] { type })
var str = AccessTools.Method(opt.GetType(), "Get").Invoke(opt, Array.Empty<object>()); .Invoke(null, new object[] { entitiesDB, block, null });
return AccessTools.Field(str.GetType(), name).GetValue(str);*/ var str = AccessTools.Property(opt.GetType(), "Value").GetValue(opt);
return AccessTools.Field(type, name).GetValue(Activator.CreateInstance(type)); return AccessTools.Field(str.GetType(), name).GetValue(str);
} }
internal void SetBlockInfo(Block block, Type type, string name, object value) 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 }); .Invoke(this, new object[] { block });
var str = AccessTools.Method(opt.GetType(), "Get").Invoke(opt, Array.Empty<object>()); var prop = AccessTools.Property(opt.GetType(), "Value");
AccessTools.Field(str.GetType(), name).SetValue(str, value);*/ var str = prop.GetValue(opt);
AccessTools.Field(str.GetType(), name).SetValue(str, value);
prop.SetValue(opt, str);
} }
public void UpdateDisplayedBlock(EGID id) public void UpdateDisplayedBlock(EGID id)

View file

@ -64,6 +64,15 @@ namespace TechbloxModdingAPI.Utility
return ref managedArray[index]; 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 bool Exists => state != State.Empty;
public T? Nullable() => this ? Get() : default; public T? Nullable() => this ? Get() : default;