2020-01-03 01:14:26 +01:00
using System ;
using System.Reflection ;
2020-01-03 19:54:35 -05:00
2020-01-03 01:14:26 +01:00
using DataLoader ;
2021-04-19 03:13:00 +02:00
using Gamecraft.Wires ;
2020-05-03 15:31:09 -04:00
using HarmonyLib ;
2020-01-03 01:14:26 +01: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-03 19:54:35 -05:00
using GamecraftModdingAPI.Utility ;
2020-05-11 20:28:26 -04:00
using GamecraftModdingAPI.Engines ;
2020-05-13 14:02:36 +02:00
using GamecraftModdingAPI.Players ;
2020-11-09 16:18:25 -05:00
using RobocraftX.Rendering.GPUI ;
2020-01-03 19:54:35 -05:00
namespace GamecraftModdingAPI.Blocks
2020-01-03 01:14:26 +01:00
{
2020-01-03 19:54:35 -05:00
/// <summary>
/// Engine which executes block placement actions
/// </summary>
2020-01-03 01:14:26 +01:00
public class PlacementEngine : IApiEngine
{
2020-05-27 17:20:53 +02:00
public bool IsInGame ;
2020-01-03 01:14:26 +01:00
public void Dispose ( )
{
IsInGame = false ;
}
public void Ready ( )
{
IsInGame = true ;
}
2020-03-12 23:36:23 +01:00
public EntitiesDB entitiesDB { get ; set ; }
2021-04-10 02:02:47 +02:00
private static BlockEntityFactory _blockEntityFactory ; //Injected from PlaceSingleBlockEngine
2020-01-03 01:14:26 +01:00
2021-04-19 19:32:14 +02:00
public EntityInitializer PlaceBlock ( BlockIDs block , float3 position , Player player , bool autoWire )
2020-01-03 01:14:26 +01: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
2021-04-19 19:32:14 +02:00
return BuildBlock ( ( ushort ) block , position , autoWire , ( player ? ? Player . LocalPlayer ) . Id ) ;
2020-01-03 01:14:26 +01:00
}
2021-04-19 19:32:14 +02:00
private EntityInitializer BuildBlock ( ushort block , float3 position , bool autoWire , uint playerId )
2020-01-03 01:14:26 +01:00
{
if ( _blockEntityFactory = = null )
2020-11-12 02:39:58 +01:00
throw new BlockException ( "The factory is null." ) ;
2021-04-10 02:02:47 +02:00
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 01:14:26 +01:00
2021-04-10 02:02:47 +02:00
EntityInitializer structInitializer = _blockEntityFactory . Build ( CommonExclusiveGroups . nextBlockEntityID , block ) ; //The ghost block index is only used for triggers
2021-04-19 19:32:14 +02:00
uint prefabId = PrefabsID . GetOrCreatePrefabID ( block , ( byte ) BlockMaterial . SteelBodywork , 0 , false ) ;
2020-04-09 01:14:21 +02:00
structInitializer . Init ( new GFXPrefabEntityStructGPUI ( prefabId ) ) ;
structInitializer . Init ( new PhysicsPrefabEntityStruct ( prefabId ) ) ;
2020-01-03 01:14:26 +01:00
structInitializer . Init ( dbEntity ) ;
structInitializer . Init ( new PositionEntityStruct { position = position } ) ;
2020-04-13 01:31:06 +02:00
structInitializer . Init ( new BlockPlacementInfoStruct ( )
{
loadedFromDisk = false ,
2021-04-10 02:02:47 +02:00
placedBy = playerId ,
2021-04-19 03:13:00 +02:00
triggerAutoWiring = autoWire & & structInitializer . Has < BlockPortsStruct > ( )
2020-04-13 01:31:06 +02:00
} ) ;
2021-04-10 02:02:47 +02:00
2021-04-19 19:32:14 +02:00
foreach ( var group in CharacterExclusiveGroups . AllCharacters )
2020-11-13 23:59:37 +01:00
{
2021-04-19 19:32:14 +02:00
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 ;
}
2020-07-15 21:58:24 +02:00
return structInitializer ;
2020-01-03 01:14:26 +01:00
}
2021-04-19 19:32:14 +02:00
public string Name = > "GamecraftModdingAPIPlacementGameEngine" ;
2020-01-03 01:14:26 +01:00
2021-04-10 02:02:47 +02:00
public bool isRemovable = > false ;
2020-05-11 20:28:26 -04:00
2020-05-27 17:20:53 +02:00
[HarmonyPatch]
2020-01-03 01:14:26 +01:00
public class FactoryObtainerPatch
{
static void Postfix ( BlockEntityFactory blockEntityFactory )
{
_blockEntityFactory = blockEntityFactory ;
2020-01-03 19:54:35 -05:00
Logging . MetaDebugLog ( "Block entity factory injected." ) ;
2020-01-03 01:14:26 +01:00
}
2020-05-03 15:31:09 -04:00
static MethodBase TargetMethod ( Harmony instance )
2020-01-03 01:14:26 +01:00
{
2021-04-10 02:02:47 +02:00
return AccessTools . TypeByName ( "RobocraftX.CR.MachineEditing.PlaceSingleBlockEngine" ) . GetConstructors ( ) [ 0 ] ;
2020-01-03 01:14:26 +01:00
}
}
}
}