|
- using System;
- using System.Linq;
- using System.Reflection;
- using GamecraftModdingAPI.Commands;
- using Harmony;
- using RobocraftX.GUI.CommandLine;
- using RobocraftX.Multiplayer;
- using RobocraftX.StateSync;
- using RobocraftX.Character;
- using Svelto.ECS;
- using Unity.Entities;
- using UnityEngine;
- using uREPL;
- using Svelto.Context;
- using RobocraftX;
- using RobocraftX.Blocks.Ghost;
- using RobocraftX.Blocks.Scaling;
- using RobocraftX.Common;
- using RobocraftX.CR.MachineEditing;
- using Svelto.ECS.EntityStructs;
- using Unity.Mathematics;
-
- namespace ExtraCommands.Basics
- {
- [CustomCommand("SetScale")]
- class SetScaleCommandEngine : ICustomCommandEngine
- {
-
- public void Ready()
- {
- CommandRegistrationHelper.Register<float, float, float>("SetScale", SetScaleCommand, "Set the scale for the next block. Use 0 0 0 to reset. The displayed cube is uniformly scaled until placed.");
- }
-
- public IEntitiesDB entitiesDB { get; set; }
-
- private static float3 _scale;
- //private HarmonyInstance harmony;
-
- private void SetScaleCommand(float x, float y, float z)
- {
- /*if (harmony == null)
- {
- Console.WriteLine("Patching...");
- harmony = HarmonyInstance.Create("org.exmods.extracommands.setscale");
- var type = Type.GetType("RobocraftX.CR.MachineEditing.PlacementCursorEngine");
- var method = type.GetMethod("<UpdateCursor>g__UpdatePlacementCursorScales|10_0");
- harmony.Patch(method, new HarmonyMethod(((Func<bool>) PatchPrefix).Method));
- Console.WriteLine("Patched");
- }*/
-
- _scale = new float3(x, y, z);
- EntityCollection<GhostScalingEntityStruct> scalings =
- entitiesDB.QueryEntities<GhostScalingEntityStruct>(
- (ExclusiveGroupStruct) NamedExclusiveGroup<GHOST_BLOCKS>.Group);
- Console.WriteLine("Found " + scalings.length + " scalings.");
- var egid = new EGID(0, NamedExclusiveGroup<GHOST_BLOCKS>.Group);
- for (int index = 0; index < scalings.length; index++)
- {
- Console.WriteLine("G scaling " + index + ": " + scalings[index].ghostScale);
- ref var scaling = ref entitiesDB.QueryEntity<ScalingEntityStruct>(egid);
- Console.WriteLine("Scaling " + index + ": " + scaling);
- ref var scale = ref entitiesDB.QueryEntity<BlockPlacementScaleEntityStruct>(egid);
- Console.WriteLine("Scale " + index + ": " + scale.snapGridScale);
- UpdateScale(ref scale, ref scaling, ref scalings[index], egid);
- }
- }
-
- private void UpdateScale(ref BlockPlacementScaleEntityStruct scale, ref ScalingEntityStruct scaling,
- ref GhostScalingEntityStruct gscaling, EGID egid)
- {
- Console.WriteLine("Attempting to update scale...");
- if (_scale.x < 4e-5 || _scale.y < 4e-5 || _scale.z < 4e-5) return;
- Console.WriteLine("Scale is set, continuing.");
- scale.snapGridScale = Math.Max((int) _scale.x, 1);
- scale.blockPlacementHeight = Math.Max((int) _scale.y, 1);
- scale.desiredScaleFactor = Math.Max((int) _scale.x, 1);
- entitiesDB.PublishEntityChange<BlockPlacementScaleEntityStruct>(egid);
- Console.WriteLine("Scale published");
- scaling.scale = _scale;
- entitiesDB.PublishEntityChange<ScalingEntityStruct>(egid);
- Console.WriteLine("Scaling published");
- gscaling.ghostScale = _scale;
- gscaling.hasBlockBeenUnformedScaled = true; //Apply scale instead of overwriting it
- entitiesDB.PublishEntityChange<GhostScalingEntityStruct>(egid);
- Console.WriteLine("Scale updated (" + scaling.scale + ")");
- }
-
- public void Dispose()
- {
- CommandRegistrationHelper.Unregister("SetScale");
- }
-
- /*public void Add(ref GhostScalingEntityStruct entityView, EGID egid)
- { //If the cursor is near a block, it recreates the entity - nope
- Console.WriteLine("Entity " + egid + " added: " + entityView.ghostScale);
- ref var scale = ref entitiesDB.QueryEntity<BlockPlacementScaleEntityStruct>(egid);
- ref var scaling = ref entitiesDB.QueryEntity<ScalingEntityStruct>(egid);
- UpdateScale(ref scale, ref scaling, ref entityView);
- entitiesDB.PublishEntityChange<ScalingEntityStruct>(egid);
- entitiesDB.PublishEntityChange<BlockPlacementScaleEntityStruct>(egid);
- entitiesDB.PublishEntityChange<GhostScalingEntityStruct>(egid);
- }
-
- public void Remove(ref GhostScalingEntityStruct entityView, EGID egid)
- {
- }*/
-
- /*private bool PatchPrefix()
- {
- if (math.any(_scale < new float3(4e-5)))
- return true;
- return false; //Prevent updating
- }*/
-
- //ScaleGhostBlockEngine.UpdateScaling
- [HarmonyPatch]
- public class ScalePatch
- {
- static bool Prefix()
- {
- if (math.any(_scale < new float3(4e-5)))
- return true;
- return false; //Prevent updating
- }
-
- static MethodBase TargetMethod(HarmonyInstance instance)
- {
- return typeof(ScaleGhostBlockEngine).GetMethod("UpdateScaling",
- BindingFlags.NonPublic | BindingFlags.Instance);
- }
- }
- //RobocraftX.Blocks.Ghost.UniformScaleGhostBlockEngine.SimulatePhysicsStep - Does not update the ghost block but the outline still gets rounded
- //RobocraftX.Blocks.Ghost.GhostScalingSyncEngine.SimulatePhysicsStep (reflection) - Doesn't do anything immediately visible
- //RobocraftX.CR.MachineEditing.PlacementCursorEngine.<UpdateCursor>g__UpdatePlacementCursorScales|10_0
- //[HarmonyPatch(typeof(UniformScaleGhostBlockEngine))]
- //[HarmonyPatch("SimulatePhysicsStep")]
- //[HarmonyPatch]
- public class UniformScalePatch
- {
- static bool Prefix()
- {
- if (math.any(_scale < new float3(4e-5)))
- return true;
- return false; //Prevent updating
- }
-
- static MethodBase TargetMethod(HarmonyInstance instance)
- {
- return typeof(PlacementCursorEngine)
- .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
- .First(m => m.Name.Contains("UpdatePlacementCursorScales"));
- }
- }
-
- public string Name { get; } = "SetScale";
- public string Description { get; } = "Set the scale for the next block to be placed.";
- }
- }
|