Browse Source

Fix support for accessing properties using reflection

The test still crashes the game
tags/v2.3.0
NorbiPeti 2 years ago
parent
commit
c0ef8f1fae
3 changed files with 20 additions and 9 deletions
  1. +1
    -1
      TechbloxModdingAPI/Blocks/BlockTests.cs
  2. +10
    -8
      TechbloxModdingAPI/Blocks/Engines/BlockEngine.cs
  3. +9
    -0
      TechbloxModdingAPI/Utility/OptionalRef.cs

+ 1
- 1
TechbloxModdingAPI/Blocks/BlockTests.cs 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())


+ 10
- 8
TechbloxModdingAPI/Blocks/Engines/BlockEngine.cs 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)


+ 9
- 0
TechbloxModdingAPI/Utility/OptionalRef.cs 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;



Loading…
Cancel
Save