TechbloxModdingAPI/GamecraftModdingAPI/Blocks/BlockCloneEngine.cs

78 lines
2.8 KiB
C#
Raw Normal View History

using System;
using System.Reflection;
using GamecraftModdingAPI.Engines;
using HarmonyLib;
using RobocraftX.Blocks;
using RobocraftX.Character;
using RobocraftX.Common;
using RobocraftX.Common.Players;
using Svelto.DataStructures;
using Svelto.ECS;
namespace GamecraftModdingAPI.Blocks
{
public class BlockCloneEngine : IApiEngine
{
private static Type copyEngineType =
AccessTools.TypeByName("Gamecraft.GUI.Tweaks.Engines.CopyTweaksOnPickEngine");
private MethodBase copyFromBlock = AccessTools.Method(copyEngineType, "CopyTweaksFromBlock");
private MethodBase copyToBlock = AccessTools.Method(copyEngineType, "ApplyTweaksToPlacedBlock");
public void Ready()
{
}
public EntitiesDB entitiesDB { get; set; }
public void Dispose()
{
}
public void CopyBlockStats(EGID sourceID, EGID targetID)
{
var allCharacters = (LocalFasterReadOnlyList<ExclusiveGroupStruct>) CharacterExclusiveGroups.AllCharacters;
foreach (var ((pickedBlockColl, count), _) in entitiesDB.QueryEntities<PickedBlockExtraDataStruct>(allCharacters))
{
for (int i = 0; i < count; ++i)
{
ref PickedBlockExtraDataStruct pickedBlock = ref pickedBlockColl[i];
var oldStruct = pickedBlock;
pickedBlock.pickedBlockEntityID = sourceID;
pickedBlock.placedBlockEntityID = targetID;
pickedBlock.placedBlockTweaksCopied = false;
pickedBlock.placedBlockTweaksMustCopy = true;
if (entitiesDB.Exists<DBEntityStruct>(pickedBlock.pickedBlockEntityID)
&& entitiesDB.Exists<DBEntityStruct>(pickedBlock.placedBlockEntityID))
{
copyFromBlock.Invoke(Patch.instance, new object[] {pickedBlock.ID, pickedBlock});
copyToBlock.Invoke(Patch.instance, new object[] {pickedBlock.ID, pickedBlock});
pickedBlock.placedBlockTweaksMustCopy = false;
pickedBlock.placedBlockTweaksCopied = false;
}
pickedBlock = oldStruct; //Make sure to not interfere with the game
}
}
}
[HarmonyPatch]
private static class Patch
{
public static object instance;
public static void Postfix(object __instance)
{
instance = __instance;
}
public static MethodBase TargetMethod()
{
return AccessTools.GetDeclaredConstructors(copyEngineType)[0];
}
}
public string Name { get; } = "GamecraftModdingAPIBlockCloneGameEngine";
public bool isRemovable { get; } = false;
}
}