From a8f7e2b0a742d3ae9c5d5fe2a25310e7a586dcbc Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 9 Sep 2016 09:07:27 +1000 Subject: [PATCH] In SinglePlayer, you should be able to use commands without /client. (E.g. /cuboid will act the same as /client cuboid) --- ClassicalSharp/ClassicalSharp.csproj | 2 +- .../{CommandManager.cs => CommandList.cs} | 45 ++++++++------ ClassicalSharp/Commands/CommandReader.cs | 58 +------------------ ClassicalSharp/Commands/DefaultCommands.cs | 6 +- ClassicalSharp/Game/ChatLog.cs | 4 +- ClassicalSharp/Game/Game.Init.cs | 2 +- ClassicalSharp/Game/Game.Properties.cs | 2 +- ClassicalSharp/Utils/Utils.cs | 1 - 8 files changed, 35 insertions(+), 85 deletions(-) rename ClassicalSharp/Commands/{CommandManager.cs => CommandList.cs} (69%) diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 1bc26f721..e82f04ac5 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -246,7 +246,7 @@ - + diff --git a/ClassicalSharp/Commands/CommandManager.cs b/ClassicalSharp/Commands/CommandList.cs similarity index 69% rename from ClassicalSharp/Commands/CommandManager.cs rename to ClassicalSharp/Commands/CommandList.cs index 2e58588f5..d0be212c6 100644 --- a/ClassicalSharp/Commands/CommandManager.cs +++ b/ClassicalSharp/Commands/CommandList.cs @@ -4,11 +4,15 @@ using System.Collections.Generic; namespace ClassicalSharp.Commands { - public class CommandManager : IGameComponent { + public class CommandList : IGameComponent { - public static bool IsCommandPrefix( string input ) { - return Utils.CaselessStarts( input, "/client " ) || - Utils.CaselessEquals( input, "/client" ); + const string prefix = "/client"; + public bool IsCommandPrefix( string input ) { + if( game.Network.IsSinglePlayer && Utils.CaselessStarts( input, "/" ) ) + return true; + + return Utils.CaselessStarts( input, prefix + " " ) + || Utils.CaselessEquals( input, prefix ); } protected Game game; @@ -21,10 +25,9 @@ namespace ClassicalSharp.Commands { Register( new InfoCommand() ); Register( new RenderTypeCommand() ); - if( game.Network.IsSinglePlayer ) { - Register( new ModelCommand() ); - Register( new CuboidCommand() ); - } + if( !game.Network.IsSinglePlayer ) return; + Register( new ModelCommand() ); + Register( new CuboidCommand() ); } public void Ready( Game game ) { } @@ -42,37 +45,41 @@ namespace ClassicalSharp.Commands { RegisteredCommands.Add( command ); } - public Command GetMatch( string commandName ) { + public Command GetMatch( string cmdName ) { Command match = null; for( int i = 0; i < RegisteredCommands.Count; i++ ) { Command cmd = RegisteredCommands[i]; - if( !Utils.CaselessStarts( cmd.Name, commandName ) ) continue; + if( !Utils.CaselessStarts( cmd.Name, cmdName ) ) continue; if( match != null ) { - game.Chat.Add( "&e/client: Multiple commands found that start with: \"&f" + commandName + "&e\"." ); + game.Chat.Add( "&e/client: Multiple commands found that start with: \"&f" + cmdName + "&e\"." ); return null; } match = cmd; } if( match == null ) - game.Chat.Add( "&e/client: Unrecognised command: \"&f" + commandName + "&e\"." ); + game.Chat.Add( "&e/client: Unrecognised command: \"&f" + cmdName + "&e\"." ); return match; } public void Execute( string text ) { + if( Utils.CaselessStarts( text, prefix ) ) { + text = text.Substring( prefix.Length ).TrimStart( ' ' ); + text = "/" + text; + } + CommandReader reader = new CommandReader( text ); - if( reader.TotalArgs == 0 ) { + string cmdName = reader.Next(); + if( cmdName == null ) { game.Chat.Add( "&eList of client commands:" ); PrintDefinedCommands( game ); game.Chat.Add( "&eTo see a particular command's help, type /client help [cmd name]" ); return; - } - string commandName = reader.Next(); - Command cmd = GetMatch( commandName ); - if( cmd != null ) { - cmd.Execute( reader ); - } + } + + Command cmd = GetMatch( cmdName ); + if( cmd != null ) cmd.Execute( reader ); } public void PrintDefinedCommands( Game game ) { diff --git a/ClassicalSharp/Commands/CommandReader.cs b/ClassicalSharp/Commands/CommandReader.cs index 8d2953c51..f27e07b77 100644 --- a/ClassicalSharp/Commands/CommandReader.cs +++ b/ClassicalSharp/Commands/CommandReader.cs @@ -8,7 +8,6 @@ namespace ClassicalSharp.Commands { public class CommandReader { string rawInput; - int firstArgOffset; int curOffset; /// Returns the next argument, or null if there are no more arguments left. @@ -33,29 +32,6 @@ namespace ClassicalSharp.Commands { return arg; } - /// Attempts to parse the next argument as a 32-bit integer. - public bool NextInt( out int value ) { - return Int32.TryParse( Next(), out value ); - } - - /// Attempts to parse the next argument as a 32-bit floating point number. - public bool NextFloat( out float value ) { - return Single.TryParse( Next(), out value ); - } - - /// Attempts to parse the next argument as a 6 digit hex number. - /// #xxxxxx or xxxxxx are accepted. - public bool NextHexColour( out FastColour value ) { - return FastColour.TryParse( Next(), out value ); - } - - /// Attempts to parse the next argument using the specified parsing function. - public bool NextOf( out T value, TryParseFunc parser ) { - bool success = parser( Next(), out value ); - if( !success ) value = default( T ); - return success; - } - bool MoveNext() { if( curOffset >= rawInput.Length ) return false; int next = rawInput.IndexOf( ' ', curOffset ); @@ -66,41 +42,9 @@ namespace ClassicalSharp.Commands { return true; } - /// Total number of arguments yet to be processed. - public int RemainingArgs { - get { return CountArgsFrom( curOffset ); } - } - - /// Total number of arguments provided by the user. - public int TotalArgs { - get { return CountArgsFrom( firstArgOffset ); } - } - - int CountArgsFrom( int startOffset ) { - int count = 0; - int pos = curOffset; - curOffset = startOffset; - while( MoveNext() ) { - count++; - } - curOffset = pos; - return count; - } - - /// Rewinds the internal state back to the first argument. - public void Reset() { - curOffset = firstArgOffset; - } - public CommandReader( string input ) { rawInput = input.TrimEnd( ' ' ); - // Check that the string has at least one argument - the command name - int firstSpaceIndex = rawInput.IndexOf( ' ' ); - if( firstSpaceIndex < 0 ) { - firstSpaceIndex = rawInput.Length - 1; - } - firstArgOffset = firstSpaceIndex + 1; - curOffset = firstArgOffset; + curOffset = 1; // skip start / for the ocmmand } } } diff --git a/ClassicalSharp/Commands/DefaultCommands.cs b/ClassicalSharp/Commands/DefaultCommands.cs index d8982da97..2db96625e 100644 --- a/ClassicalSharp/Commands/DefaultCommands.cs +++ b/ClassicalSharp/Commands/DefaultCommands.cs @@ -19,7 +19,7 @@ namespace ClassicalSharp.Commands { } public override void Execute( CommandReader reader ) { - game.CommandManager.PrintDefinedCommands( game ); + game.CommandList.PrintDefinedCommands( game ); } } @@ -38,10 +38,10 @@ namespace ClassicalSharp.Commands { string cmdName = reader.Next(); if( cmdName == null ) { game.Chat.Add( "&eList of client commands:" ); - game.CommandManager.PrintDefinedCommands( game ); + game.CommandList.PrintDefinedCommands( game ); game.Chat.Add( "&eTo see a particular command's help, type /client help [cmd name]" ); } else { - Command cmd = game.CommandManager.GetMatch( cmdName ); + Command cmd = game.CommandList.GetMatch( cmdName ); if( cmd == null ) return; string[] help = cmd.Help; for( int i = 0; i < help.Length; i++ ) diff --git a/ClassicalSharp/Game/ChatLog.cs b/ClassicalSharp/Game/ChatLog.cs index 1373c670c..e10501b18 100644 --- a/ClassicalSharp/Game/ChatLog.cs +++ b/ClassicalSharp/Game/ChatLog.cs @@ -34,8 +34,8 @@ namespace ClassicalSharp { text = text.TrimEnd( trimChars ); if( String.IsNullOrEmpty( text ) ) return; - if( CommandManager.IsCommandPrefix( text ) ) { - game.CommandManager.Execute( text ); + if( game.CommandList.IsCommandPrefix( text ) ) { + game.CommandList.Execute( text ); return; } game.Network.SendChat( text, partial ); diff --git a/ClassicalSharp/Game/Game.Init.cs b/ClassicalSharp/Game/Game.Init.cs index 15457ca83..855771e87 100644 --- a/ClassicalSharp/Game/Game.Init.cs +++ b/ClassicalSharp/Game/Game.Init.cs @@ -89,7 +89,7 @@ namespace ClassicalSharp { UpdateProjection(); Gui = AddComponent( new GuiInterface( this ) ); - CommandManager = AddComponent( new CommandManager() ); + CommandList = AddComponent( new CommandList() ); SelectionManager = AddComponent( new SelectionManager() ); WeatherRenderer = AddComponent( new WeatherRenderer() ); HeldBlockRenderer = AddComponent( new HeldBlockRenderer() ); diff --git a/ClassicalSharp/Game/Game.Properties.cs b/ClassicalSharp/Game/Game.Properties.cs index b587b8419..c0e6c1c6a 100644 --- a/ClassicalSharp/Game/Game.Properties.cs +++ b/ClassicalSharp/Game/Game.Properties.cs @@ -101,7 +101,7 @@ namespace ClassicalSharp { public IDrawer2D Drawer2D; public GuiInterface Gui; - public CommandManager CommandManager; + public CommandList CommandList; public SelectionManager SelectionManager; public ParticleManager ParticleManager; public PickedPosRenderer Picking; diff --git a/ClassicalSharp/Utils/Utils.cs b/ClassicalSharp/Utils/Utils.cs index a485e749e..e2c4263f9 100644 --- a/ClassicalSharp/Utils/Utils.cs +++ b/ClassicalSharp/Utils/Utils.cs @@ -20,7 +20,6 @@ namespace ClassicalSharp { public delegate TResult Func( T1 arg1 ); public delegate TResult Func( T1 arg1, T2 arg2 ); public delegate TResult Func( T1 arg1, T2 arg2, T3 arg3 ); - public delegate bool TryParseFunc( string s, out T value ); // ################################################################ public static partial class Utils {