TechbloxModdingAPI/GamecraftModdingAPI/Blocks/PlacementEngine.cs

103 lines
4 KiB
C#
Raw Normal View History

2020-01-03 00:14:26 +00:00
using System;
using System.Reflection;
2020-01-04 00:54:35 +00:00
2020-01-03 00:14:26 +00:00
using DataLoader;
using Gamecraft.Wires;
2020-05-03 19:31:09 +00:00
using HarmonyLib;
2020-01-03 00:14:26 +00:00
using RobocraftX.Blocks;
using RobocraftX.Blocks.Scaling;
using RobocraftX.Character;
using RobocraftX.Common;
using RobocraftX.CR.MachineEditing;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using Unity.Mathematics;
using UnityEngine;
2020-01-04 00:54:35 +00:00
using GamecraftModdingAPI.Utility;
2020-05-12 00:28:26 +00:00
using GamecraftModdingAPI.Engines;
using GamecraftModdingAPI.Players;
2020-11-09 21:18:25 +00:00
using RobocraftX.Rendering.GPUI;
2020-01-04 00:54:35 +00:00
namespace GamecraftModdingAPI.Blocks
2020-01-03 00:14:26 +00:00
{
2020-01-04 00:54:35 +00:00
/// <summary>
/// Engine which executes block placement actions
/// </summary>
2020-01-03 00:14:26 +00:00
public class PlacementEngine : IApiEngine
{
public bool IsInGame;
2020-01-03 00:14:26 +00:00
public void Dispose()
{
IsInGame = false;
}
public void Ready()
{
IsInGame = true;
}
public EntitiesDB entitiesDB { get; set; }
private static BlockEntityFactory _blockEntityFactory; //Injected from PlaceSingleBlockEngine
2020-01-03 00:14:26 +00:00
public EntityInitializer PlaceBlock(BlockIDs block, float3 position, Player player, bool autoWire)
2020-01-03 00:14:26 +00:00
{ //It appears that only the non-uniform scale has any visible effect, but if that's not given here it will be set to the uniform one
return BuildBlock((ushort) block, position, autoWire, (player ?? Player.LocalPlayer).Id);
2020-01-03 00:14:26 +00:00
}
private EntityInitializer BuildBlock(ushort block, float3 position, bool autoWire, uint playerId)
2020-01-03 00:14:26 +00:00
{
if (_blockEntityFactory == null)
throw new BlockException("The factory is null.");
uint resourceId = (uint) PrefabsID.GenerateResourceID(0, block);
if (!PrefabsID.PrefabIDByResourceIDMap.ContainsKey(resourceId))
throw new BlockException("Block with ID " + block + " not found!");
//RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine
DBEntityStruct dbEntity = new DBEntityStruct {DBID = block};
2020-01-03 00:14:26 +00:00
EntityInitializer structInitializer = _blockEntityFactory.Build(CommonExclusiveGroups.nextBlockEntityID, block); //The ghost block index is only used for triggers
uint prefabId = PrefabsID.GetOrCreatePrefabID(block, (byte) BlockMaterial.SteelBodywork, 0, false);
structInitializer.Init(new GFXPrefabEntityStructGPUI(prefabId));
structInitializer.Init(new PhysicsPrefabEntityStruct(prefabId));
2020-01-03 00:14:26 +00:00
structInitializer.Init(dbEntity);
structInitializer.Init(new PositionEntityStruct {position = position});
2020-04-12 23:31:06 +00:00
structInitializer.Init(new BlockPlacementInfoStruct()
{
loadedFromDisk = false,
placedBy = playerId,
triggerAutoWiring = autoWire && structInitializer.Has<BlockPortsStruct>()
2020-04-12 23:31:06 +00:00
});
foreach (var group in CharacterExclusiveGroups.AllCharacters)
{
EGID playerEGID = new EGID(playerId, group);
if (!entitiesDB.TryQueryEntitiesAndIndex<PickedBlockExtraDataStruct>(playerEGID, out uint index,
out var array)) continue;
ref PickedBlockExtraDataStruct pickedBlock = ref array[index];
pickedBlock.placedBlockEntityID = structInitializer.EGID;
pickedBlock.placedBlockWasAPickedBlock = false;
}
return structInitializer;
2020-01-03 00:14:26 +00:00
}
public string Name => "GamecraftModdingAPIPlacementGameEngine";
2020-01-03 00:14:26 +00:00
public bool isRemovable => false;
2020-05-12 00:28:26 +00:00
[HarmonyPatch]
2020-01-03 00:14:26 +00:00
public class FactoryObtainerPatch
{
static void Postfix(BlockEntityFactory blockEntityFactory)
{
_blockEntityFactory = blockEntityFactory;
2020-01-04 00:54:35 +00:00
Logging.MetaDebugLog("Block entity factory injected.");
2020-01-03 00:14:26 +00:00
}
2020-05-03 19:31:09 +00:00
static MethodBase TargetMethod(Harmony instance)
2020-01-03 00:14:26 +00:00
{
return AccessTools.TypeByName("RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine").GetConstructors()[0];
2020-01-03 00:14:26 +00:00
}
}
}
}