TechbloxModdingAPI/GamecraftModdingAPI/Utility/DebugInterfaceEngine.cs
NorbiPeti 1c5ce37fce Add debug interface API and improve block API
Added API for adding more information on the debug display (not object-oriented yet)
Removed the setter for block type to ensure stability
Made the block API return defaults if the block no longer exists
Added property to check if the block exists
Made a struct for the block's color property
Added missing block IDs
2020-05-17 23:46:31 +02:00

84 lines
No EOL
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.Formatting;
using GamecraftModdingAPI.Blocks;
using GamecraftModdingAPI.Engines;
using GamecraftModdingAPI.Players;
using HarmonyLib;
using RobocraftX.GUI.Debug;
using Svelto.ECS;
using Svelto.ECS.Experimental;
namespace GamecraftModdingAPI.Utility
{
public class DebugInterfaceEngine : IApiEngine
{
private static Dictionary<string, Func<string>> _extraInfo=new Dictionary<string, Func<string>>();
public void Ready()
{
SetInfo("lookedAt", LookedAt);
}
public EntitiesDB entitiesDB { get; set; }
public void Dispose()
{
}
public void SetInfo(string id, Func<string> contentGetter) => _extraInfo[id] = contentGetter;
public bool RemoveInfo(string id) => _extraInfo.Remove(id);
private Player player;
private string LookedAt()
{
if (player == null)
player = new Player(PlayerType.Local);
Block block = player.GetBlockLookedAt();
if (block == null) return "Block: none";
return "Block: " + block.Type + "\nColor: " + block.Color + "\n" + "At: " + block.Position;
}
public string Name { get; } = "GamecraftModdingAPIDebugInterfaceGameEngine";
public bool isRemovable { get; } = true;
[HarmonyPatch]
private class Patch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var list = new List<CodeInstruction>(instructions);
try
{
//Before setting the text from the StringBuffer
int index = list.FindLastIndex(inst => inst.opcode == OpCodes.Ldfld);
var array = new CodeInstruction[]
{
new CodeInstruction(OpCodes.Ldloc_0), //StringBuffer
new CodeInstruction(OpCodes.Call, ((Action<StringBuffer>)AddInfo).Method)
};
list.InsertRange(index, array);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return list;
}
public static void AddInfo(StringBuffer sb)
{
foreach (var info in _extraInfo.Values)
sb.Append(info() + "\n");
}
public static MethodInfo TargetMethod()
{
return AccessTools.Method("RobocraftX.GUI.Debug.DebugDisplayEngine:UpdateDisplay");
}
}
}
}