Mods.Common Commands namespace

Added Commands Namespace.
This commit is contained in:
steelphase
2014-10-07 02:12:24 -04:00
parent c5bea39bf8
commit 0cb0ae57f3
6 changed files with 9 additions and 8 deletions

View File

@@ -0,0 +1,57 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Commands
{
[Desc("Enables commands triggered by typing them into the chatbox. Attach this to the world actor.")]
public class ChatCommandsInfo : TraitInfo<ChatCommands> { }
public class ChatCommands : INotifyChat
{
public Dictionary<string, IChatCommand> Commands { get; private set; }
public ChatCommands()
{
Commands = new Dictionary<string, IChatCommand>();
}
public bool OnChat(string playername, string message)
{
if (message.StartsWith("/"))
{
var name = message.Substring(1).Split(' ')[0].ToLowerInvariant();
var command = Commands.FirstOrDefault(x => x.Key == name);
if (command.Value != null)
command.Value.InvokeCommand(name.ToLowerInvariant(), message.Substring(1 + name.Length));
else
Game.Debug("{0} is not a valid command.", name);
return false;
}
return true;
}
public void RegisterCommand(string name, IChatCommand command)
{
Commands.Add(name.ToLowerInvariant(), command);
}
}
public interface IChatCommand
{
void InvokeCommand(string command, string arg);
}
}

View File

@@ -0,0 +1,99 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Globalization;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Commands
{
[Desc("Enables developer cheats via the chatbox. Attach this to the world actor.")]
public class DevCommandsInfo : TraitInfo<DevCommands> { }
public class DevCommands : IChatCommand, IWorldLoaded
{
World world;
public void WorldLoaded(World w, WorldRenderer wr)
{
world = w;
var console = world.WorldActor.Trait<ChatCommands>();
var help = world.WorldActor.Trait<HelpCommand>();
Action<string, string> register = (name, helpText) =>
{
console.RegisterCommand(name, this);
help.RegisterHelp(name, helpText);
};
register("disableshroud", "toggles shroud.");
register("givecash", "gives the default or specified amount of money.");
register("instantbuild", "toggles instant building.");
register("buildanywhere", "toggles you the ability to build anywhere.");
register("unlimitedpower", "toggles infinite power.");
register("enabletech", "toggles the ability to build everything.");
register("instantcharge", "toggles instant support power charging.");
register("all", "toggles all cheats and gives you some cash for your trouble.");
register("crash", "crashes the game");
}
public void InvokeCommand(string name, string arg)
{
if (!world.AllowDevCommands)
{
Game.Debug("Cheats are disabled.");
return;
}
switch (name)
{
case "givecash":
var order = new Order("DevGiveCash", world.LocalPlayer.PlayerActor, false);
int cash;
if (int.TryParse(arg, out cash))
order.ExtraData = (uint)cash;
Game.Debug("Giving {0} credits to player {1}.", cash == 0 ? "cheat default" : cash.ToString(CultureInfo.InvariantCulture), world.LocalPlayer.PlayerName);
world.IssueOrder(order);
break;
case "disableshroud": IssueDevCommand(world, "DevShroudDisable"); break;
case "instantbuild": IssueDevCommand(world, "DevFastBuild"); break;
case "buildanywhere": IssueDevCommand(world, "DevBuildAnywhere"); break;
case "unlimitedpower": IssueDevCommand(world, "DevUnlimitedPower"); break;
case "enabletech": IssueDevCommand(world, "DevEnableTech"); break;
case "instantcharge": IssueDevCommand(world, "DevFastCharge"); break;
case "all":
IssueDevCommand(world, "DevShroudDisable");
IssueDevCommand(world, "DevFastBuild");
IssueDevCommand(world, "DevBuildAnywhere");
IssueDevCommand(world, "DevUnlimitedPower");
IssueDevCommand(world, "DevEnableTech");
IssueDevCommand(world, "DevFastCharge");
IssueDevCommand(world, "DevGiveCash");
break;
case "crash":
throw new DevException();
}
}
static void IssueDevCommand(World world, string command)
{
world.IssueOrder(new Order(command, world.LocalPlayer.PlayerActor, false));
}
class DevException : Exception { }
}
}

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Commands
{
[Desc("Shows a list of available commands in the chatbox. Attach this to the world actor.")]
public class HelpCommandInfo : TraitInfo<HelpCommand> { }
public class HelpCommand : IChatCommand, IWorldLoaded
{
readonly Dictionary<string, string> helpDescriptions;
World world;
ChatCommands console;
public HelpCommand()
{
helpDescriptions = new Dictionary<string, string>();
}
public void WorldLoaded(World w, WorldRenderer wr)
{
world = w;
console = world.WorldActor.Trait<ChatCommands>();
console.RegisterCommand("help", this);
RegisterHelp("help", "provides useful info about various commands");
}
public void InvokeCommand(string name, string arg)
{
Game.Debug("Here are the available commands:");
foreach (var key in console.Commands.Keys)
{
string description;
if (!helpDescriptions.TryGetValue(key, out description))
description = "no description available.";
Game.Debug("{0}: {1}", key, description);
}
}
public void RegisterHelp(string name, string description)
{
helpDescriptions.Add(name, description);
}
}
}

View File

@@ -0,0 +1,49 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Commands
{
[Desc("Allows the player to pause or surrender the game via the chatbox. Attach this to the world actor.")]
public class PlayerCommandsInfo : TraitInfo<PlayerCommands> { }
public class PlayerCommands : IChatCommand, IWorldLoaded
{
World world;
public void WorldLoaded(World w, WorldRenderer wr)
{
world = w;
var console = world.WorldActor.Trait<ChatCommands>();
var help = world.WorldActor.Trait<HelpCommand>();
console.RegisterCommand("pause", this);
help.RegisterHelp("pause", "pause or unpause the game");
console.RegisterCommand("surrender", this);
help.RegisterHelp("surrender", "self-destruct everything and lose the game");
}
public void InvokeCommand(string name, string arg)
{
switch (name)
{
case "pause":
world.IssueOrder(new Order("PauseGame", null, false)
{ TargetString = world.Paused ? "UnPause" : "Pause" });
break;
case "surrender":
world.IssueOrder(new Order("Surrender", world.LocalPlayer.PlayerActor, false));
break;
}
}
}
}