diff --git a/GCMC/CubePlacerEngine.cs b/GCMC/CubePlacerEngine.cs new file mode 100644 index 0000000..e57190a --- /dev/null +++ b/GCMC/CubePlacerEngine.cs @@ -0,0 +1,104 @@ +using System; +using DataLoader; +using RobocraftX.Blocks; +using RobocraftX.Blocks.Ghost; +using RobocraftX.Blocks.Scaling; +using RobocraftX.Character; +using RobocraftX.CommandLine.Custom; +using RobocraftX.Common; +using RobocraftX.Common.Input; +using RobocraftX.Common.Utilities; +using RobocraftX.CR.MachineEditing; +using RobocraftX.StateSync; +using Svelto.ECS; +using Svelto.ECS.EntityStructs; +using Unity.Jobs; +using Unity.Mathematics; +using uREPL; + +namespace GCMC +{ + public class CubePlacerEngine : IQueryingEntitiesEngine, IDeterministicSim + { + public void Ready() + { + RuntimeCommands.Register("importWorld", ImportWorld, "Imports a Minecraft world."); + } + + public IEntitiesDB entitiesDB { get; set; } + private readonly BlockEntityFactory _blockEntityFactory; //Injected from PlaceBlockEngine - TODO + + private void ImportWorld(string name) + { + PlaceBlock(1, 1, 0); + } + + private void PlaceBlock(ushort block, byte color, uint playerId) + { + BuildBlock(block, color).Init(new BlockPlacementInfoStruct() + { + loadedFromDisk = false, + placedBy = playerId + }); + } + + private EntityStructInitializer BuildBlock(ushort block, byte color) + { + //RobocraftX.CR.MachineEditing.PlaceBlockEngine + ScalingEntityStruct scaling = new ScalingEntityStruct {scale = new float3(1, 1, 1)}; + RotationEntityStruct rotation = new RotationEntityStruct {rotation = quaternion.identity}; + GridRotationStruct gridRotation = new GridRotationStruct + {position = float3.zero, rotation = quaternion.identity}; + CubeCategoryStruct category = new CubeCategoryStruct + {category = CubeCategory.General, type = CubeType.Block}; + uint dbid = block; + DBEntityStruct dbEntity = new DBEntityStruct {DBID = dbid}; + uint num = PrefabsID.DBIDMAP[dbid]; + GFXPrefabEntityStructGO gfx = new GFXPrefabEntityStructGO {prefabID = num}; + BlockPlacementScaleEntityStruct placementScale = new BlockPlacementScaleEntityStruct + { + blockPlacementHeight = 1, blockPlacementWidth = 1, desiredScaleFactor = 1, snapGridScale = 1, + unitSnapOffset = 0, isUsingUnitSize = true + }; + EquippedColourStruct colour = new EquippedColourStruct {indexInPalette = color}; + EGID egid2; + switch (category.category) + { + case CubeCategory.SpawnPoint: + case CubeCategory.BuildingSpawnPoint: + egid2 = MachineEditingGroups.NewSpawnPointBlockID; + break; + default: + egid2 = MachineEditingGroups.NewBlockID; + break; + } + + int cubeId = PrefabsID.GenerateDBID((ushort) category.category, block); + EntityStructInitializer structInitializer = _blockEntityFactory.Build(egid2, (uint) cubeId); //The ghost block index is only used for triggers + if (colour.indexInPalette != byte.MaxValue) + structInitializer.Init(new ColourParameterEntityStruct + { + indexInPalette = colour.indexInPalette + }); + structInitializer.Init(new GFXPrefabEntityStructGPUI(gfx.prefabID)); + structInitializer.Init(new PhysicsPrefabEntityStruct(gfx.prefabID)); + structInitializer.Init(dbEntity); + structInitializer.Init(new PositionEntityStruct {position = 0}); + structInitializer.Init(rotation); + structInitializer.Init(scaling); + structInitializer.Init(gridRotation); + structInitializer.Init(new UniformBlockScaleEntityStruct + { + scaleFactor = placementScale.desiredScaleFactor + }); + return structInitializer; + } + + public JobHandle SimulatePhysicsStep(in float deltaTime, in PhysicsUtility utility, in PlayerInput[] playerInputs) + { + return new JobHandle(); + } + + public string name { get; } + } +} \ No newline at end of file diff --git a/GCMC/GCMC.csproj b/GCMC/GCMC.csproj index 0d6279c..2f13d0f 100644 --- a/GCMC/GCMC.csproj +++ b/GCMC/GCMC.csproj @@ -8,4 +8,58 @@ + + + bin\Debug\netstandard2.0\0Harmony.dll + + + ..\ref\BlockEntityFactory.dll + + + ..\ref\CommandLine.dll + + + ..\ref\DataLoader.dll + + + IllusionPlugin.dll + + + ..\ref\RobocraftX.Blocks.dll + + + ..\ref\RobocraftX.Character.dll + + + ..\ref\RobocraftX.Common.dll + + + ..\ref\RobocraftX.Input.dll + + + ..\ref\RobocraftX.MachineEditor.dll + + + ..\ref\RobocraftX.StateSync.dll + + + ..\ref\Svelto.Common.dll + + + ..\ref\Svelto.ECS.dll + + + ..\ref\Unity.Entities.dll + + + ..\ref\Unity.Mathematics.dll + + + ..\ref\UnityEngine.CoreModule.dll + + + ..\ref\uREPL.dll + + + diff --git a/GCMC/GCMCPlugin.cs b/GCMC/GCMCPlugin.cs new file mode 100644 index 0000000..e748ca2 --- /dev/null +++ b/GCMC/GCMCPlugin.cs @@ -0,0 +1,47 @@ +using System.Reflection; +using Harmony; +using IllusionPlugin; +using UnityEngine; + +namespace GCMC +{ + public class GCMCPlugin : IPlugin + { + public string Name { get; } = "GCMC"; + public string Version { get; } = "v0.0.1"; + public static HarmonyInstance harmony { get; protected set; } + public const string HarmonyID = "io.github.norbipeti.GCMC"; + + public void OnApplicationStart() + { + if (harmony == null) + { + harmony = HarmonyInstance.Create(HarmonyID); + harmony.PatchAll(Assembly.GetExecutingAssembly()); + } + + Debug.Log("GCMC loaded"); + } + + public void OnApplicationQuit() + { + harmony?.UnpatchAll(HarmonyID); + } + + public void OnLevelWasLoaded(int level) + { + } + + public void OnLevelWasInitialized(int level) + { + } + + public void OnUpdate() + { + } + + public void OnFixedUpdate() + { + } + } +} \ No newline at end of file diff --git a/GCMC/IllusionPlugin.dll b/GCMC/IllusionPlugin.dll new file mode 100644 index 0000000..aa7fa2e Binary files /dev/null and b/GCMC/IllusionPlugin.dll differ diff --git a/GCMC/PlaceBlockPatch.cs b/GCMC/PlaceBlockPatch.cs new file mode 100644 index 0000000..a4848ca --- /dev/null +++ b/GCMC/PlaceBlockPatch.cs @@ -0,0 +1,47 @@ +using System.Reflection; +using DataLoader; +using Harmony; +using RobocraftX.Blocks.GUI; +using RobocraftX.Common; +using RobocraftX.CR.MachineEditing; +using RobocraftX.StateSync; +using Svelto.ECS; +using Unity.Entities; +using UnityEngine; + +namespace GCMC +{ + [HarmonyPatch] + public class PlaceBlockPatch + { + static void Postfix(EnginesRoot enginesRoot, ref StateSyncRegistrationHelper stateSyncReg, bool isAuthoritative) + { + if (isAuthoritative) + { + stateSyncReg.AddEngine(new CubePlacerEngine()); + Debug.Log($"Added Minecraft world import engine"); + } + else + Debug.Log("Not authoritative, not adding MC engine"); + } + + static MethodBase TargetMethod(HarmonyInstance instance) + { + return _ComposeMethodInfo(MachineEditingCompositionRoot.StateSyncCompose); + } + + private delegate void ComposeAction(EnginesRoot enginesRoot, + IDataDB dataDB, + RCXMode currentMode, + World physicsWorld, + ref StateSyncRegistrationHelper stateSyncReg, + bool isAuthoritative, + LabelResourceManager labelResourceManager, + LabelResourceManager textBlockLabelResourceManager, + MainGameOptions.Options mainGameOptions); + private static MethodInfo _ComposeMethodInfo(ComposeAction a) + { + return a.Method; + } + } +} \ No newline at end of file