using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using uTank2; using static uTank2.PluginCore; namespace MosswartMassacre { /// /// Helper class for working with the VTank plugin /// public static unsafe class vTank { internal static IList ChatQueue = null; internal static Type ChatType; /// /// The TrustedRelay interface for VTank control /// public static cExternalInterfaceTrustedRelay Instance { get; internal set; } /// /// Current VTank action locks. Key is lock type, Value is when the lock is set to expire. /// public static Dictionary locks = new Dictionary(); /// /// Enables VTank helper functionality /// public static void Enable() { foreach (uTank2.ActionLockType ty in Enum.GetValues(typeof(uTank2.ActionLockType))) locks[ty] = DateTime.MinValue; try { ConstructorInfo ctor = typeof(cExternalInterfaceTrustedRelay) .GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)[0]; Instance = (cExternalInterfaceTrustedRelay)ctor.Invoke(new object[] { eExternalsPermissionLevel.None }); FieldInfo fieldInfo = Instance.GetType() .GetField("a", BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo.SetValue(Instance, 15); Type vTankChatHandler = typeof(uTank2.PluginCore).Assembly.GetType("a7"); FieldInfo vTankChatList = vTankChatHandler .GetField("a", BindingFlags.NonPublic | BindingFlags.Static); ChatType = vTankChatHandler.GetNestedType("a"); ChatQueue = (IList)(vTankChatList.GetValue(null)); } catch { Disable(); } } /// /// Disables VTank helper functionality /// public static void Disable() { ChatType = null; ChatQueue = null; Instance = null; } /// /// Lock VTank from performing actions. Use Decision_UnLock to cancel. /// /// the type of action to put a lock on /// time to lock vtank for public static void Decision_Lock(uTank2.ActionLockType actionLockType, TimeSpan timeSpan) { Instance?.Decision_Lock(actionLockType, timeSpan); DateTime newExp = DateTime.UtcNow + timeSpan; if (locks[actionLockType] < newExp) locks[actionLockType] = newExp; } /// /// Cancel a VTank lock /// /// the type of action to unlock public static void Decision_UnLock(uTank2.ActionLockType actionLockType) { Instance?.Decision_UnLock(actionLockType); locks[actionLockType] = DateTime.MinValue; } #region Tell(string message, int color = 0, int target = 0) /// /// Sends a chat message to VTank so that it will be capturable by metas. /// /// message to send /// color of the chat text /// chat window target public static void Tell(string message, int color = 0, int target = 0) { if (ChatQueue != null) { object newA = Activator.CreateInstance(ChatType); ChatType.GetField("a").SetValue(newA, message); // message ChatType.GetField("b").SetValue(newA, color); // color ChatType.GetField("c").SetValue(newA, target); // target try { ChatQueue.Add(newA); } catch { } } } #endregion } }