quantum-space-buddies/QSB/Utility/CommandInterpreter.cs

105 lines
2.4 KiB
C#
Raw Normal View History

2023-09-17 21:03:20 +00:00
using QSB.HUD;
using QSB.Messaging;
using QSB.ShipSync.Messages;
using QSB.WorldSync;
2023-09-17 21:03:20 +00:00
using Steamworks;
using System.Linq;
using UnityEngine;
namespace QSB.Utility;
public class CommandInterpreter : MonoBehaviour, IAddComponentOnStart
{
public static bool InterpretCommand(string message)
{
if (message[0] != '/')
{
return false;
}
var commandParts = message.ToLower().Substring(1).Split(' ');
var command = commandParts[0];
switch (command)
{
case "ship":
ShipCommand(commandParts.Skip(1).ToArray());
break;
2023-09-08 11:47:31 +00:00
case "copy-id":
2023-09-18 20:04:52 +00:00
CopySteamID();
2023-09-08 11:47:31 +00:00
break;
default:
2023-09-08 11:47:31 +00:00
WriteToChat($"Unknown command \"{command}\".", Color.red);
break;
}
return true;
}
2023-09-08 11:47:31 +00:00
private static void WriteToChat(string message, Color color)
{
// TODO : make italics work in chat so we can use them here
MultiplayerHUDManager.Instance.WriteMessage(message, color);
}
2023-09-18 20:04:52 +00:00
public static void CopySteamID()
2023-09-08 11:47:31 +00:00
{
if (QSBCore.UseKcpTransport)
{
2023-09-18 20:05:13 +00:00
WriteToChat($"Cannot get Steam ID for KCP-hosted server.", Color.red);
2023-09-08 11:47:31 +00:00
return;
}
2023-09-18 20:04:52 +00:00
var steamID = QSBCore.IsHost
2023-09-17 21:03:20 +00:00
? SteamUser.GetSteamID().ToString()
2023-09-08 11:47:31 +00:00
: QSBNetworkManager.singleton.networkAddress;
2023-09-18 20:04:52 +00:00
GUIUtility.systemCopyBuffer = steamID;
WriteToChat($"Copied {steamID} to the clipboard.", Color.green);
2023-09-08 11:47:31 +00:00
}
public static void ShipCommand(string[] arguments)
{
2023-09-20 21:15:38 +00:00
if (!QSBCore.DebugSettings.DebugMode)
{
return;
}
var command = arguments[0];
switch (command)
{
2023-09-20 21:15:10 +00:00
/* case "explode":
2023-09-08 11:47:31 +00:00
WriteToChat($"Blowing up the ship.", Color.green);
var shipDamageController = Locator.GetShipTransform().GetComponentInChildren<ShipDamageController>();
shipDamageController.Explode();
2023-09-20 21:15:10 +00:00
break;*/
case "repair":
case "damage":
var damage = command == "damage";
switch (arguments[1])
{
case "headlight":
var headlight = QSBWorldSync.GetUnityObject<ShipHeadlightComponent>();
headlight.SetDamaged(damage);
break;
default:
break;
}
2023-09-08 11:47:31 +00:00
WriteToChat($"{(damage ? "Damaging" : "Repairing")} the {arguments[1]}.", Color.green);
break;
case "open-hatch":
QSBWorldSync.GetUnityObject<HatchController>().OpenHatch();
new HatchMessage(true).Send();
break;
case "close-hatch":
QSBWorldSync.GetUnityObject<HatchController>().CloseHatch();
new HatchMessage(false).Send();
break;
default:
2023-09-08 11:47:31 +00:00
WriteToChat($"Unknown ship command \"{command}\".", Color.red);
break;
}
}
}