2020-11-13 20:35:53 +00:00
|
|
|
|
using System;
|
2020-11-13 22:59:37 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-11-13 20:35:53 +00:00
|
|
|
|
using System.Reflection;
|
2020-11-13 22:59:37 +00:00
|
|
|
|
using Gamecraft.Wires;
|
2020-11-13 20:35:53 +00:00
|
|
|
|
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");
|
2020-11-13 22:59:37 +00:00
|
|
|
|
private static Type copyWireEngineType =
|
|
|
|
|
AccessTools.TypeByName("Gamecraft.Wires.WireConnectionCopyOnPickEngine");
|
|
|
|
|
private static Type createWireEngineType =
|
|
|
|
|
AccessTools.TypeByName("RobocraftX.GUI.Wires.WireConnectionCreateOnPlaceEngine");
|
2020-11-13 20:35:53 +00:00
|
|
|
|
|
|
|
|
|
private MethodBase copyFromBlock = AccessTools.Method(copyEngineType, "CopyTweaksFromBlock");
|
|
|
|
|
private MethodBase copyToBlock = AccessTools.Method(copyEngineType, "ApplyTweaksToPlacedBlock");
|
2020-11-13 22:59:37 +00:00
|
|
|
|
private MethodBase copyWireFromBlock = AccessTools.Method(copyWireEngineType, "CopyWireInputsAndOutputs");
|
|
|
|
|
private MethodBase copyWireToBlock = AccessTools.Method(createWireEngineType, "PlaceWiresOnPlaceNewCube");
|
2020-11-13 20:35:53 +00:00
|
|
|
|
|
|
|
|
|
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.placedBlockTweaksMustCopy = true;
|
|
|
|
|
if (entitiesDB.Exists<DBEntityStruct>(pickedBlock.pickedBlockEntityID)
|
|
|
|
|
&& entitiesDB.Exists<DBEntityStruct>(pickedBlock.placedBlockEntityID))
|
|
|
|
|
{
|
2020-11-13 22:59:37 +00:00
|
|
|
|
copyFromBlock.Invoke(Patch.copyEngine, new object[] {pickedBlock.ID, pickedBlock});
|
|
|
|
|
|
|
|
|
|
uint playerID = Player.LocalPlayer.Id;
|
|
|
|
|
var parameters = new object[] {playerID, pickedBlock};
|
|
|
|
|
copyWireFromBlock.Invoke(Patch.copyWireEngine, parameters);
|
|
|
|
|
pickedBlock = (PickedBlockExtraDataStruct) parameters[1]; //ref arg
|
|
|
|
|
|
|
|
|
|
copyToBlock.Invoke(Patch.copyEngine, new object[] {pickedBlock.ID, pickedBlock});
|
|
|
|
|
|
|
|
|
|
ExclusiveGroupStruct group = WiresExclusiveGroups.WIRES_COPY_GROUP + playerID;
|
|
|
|
|
copyWireToBlock.Invoke(Patch.createWireEngine, new object[] {group, pickedBlock.ID});
|
|
|
|
|
|
2020-11-13 20:35:53 +00:00
|
|
|
|
pickedBlock.placedBlockTweaksMustCopy = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 22:59:37 +00:00
|
|
|
|
pickedBlock = oldStruct; //Make sure to not interfere with the game - Although that might not be the case with the wire copying
|
2020-11-13 20:35:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HarmonyPatch]
|
|
|
|
|
private static class Patch
|
|
|
|
|
{
|
2020-11-13 22:59:37 +00:00
|
|
|
|
public static object copyEngine;
|
|
|
|
|
public static object copyWireEngine;
|
|
|
|
|
public static object createWireEngine;
|
2020-11-13 20:35:53 +00:00
|
|
|
|
|
|
|
|
|
public static void Postfix(object __instance)
|
|
|
|
|
{
|
2020-11-13 22:59:37 +00:00
|
|
|
|
if (__instance.GetType() == copyEngineType)
|
|
|
|
|
copyEngine = __instance;
|
|
|
|
|
else if (__instance.GetType() == copyWireEngineType)
|
|
|
|
|
copyWireEngine = __instance;
|
|
|
|
|
else if (__instance.GetType() == createWireEngineType)
|
|
|
|
|
createWireEngine = __instance;
|
2020-11-13 20:35:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 22:59:37 +00:00
|
|
|
|
public static IEnumerable<MethodBase> TargetMethods()
|
2020-11-13 20:35:53 +00:00
|
|
|
|
{
|
2020-11-13 22:59:37 +00:00
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
AccessTools.GetDeclaredConstructors(copyEngineType)[0],
|
|
|
|
|
AccessTools.GetDeclaredConstructors(copyWireEngineType)[0],
|
|
|
|
|
AccessTools.GetDeclaredConstructors(createWireEngineType)[0]
|
|
|
|
|
};
|
2020-11-13 20:35:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; } = "GamecraftModdingAPIBlockCloneGameEngine";
|
|
|
|
|
public bool isRemovable { get; } = false;
|
|
|
|
|
}
|
|
|
|
|
}
|