In SinglePlayer, you should be able to use commands without /client. (E.g. /cuboid will act the same as /client cuboid)

This commit is contained in:
UnknownShadow200 2016-09-09 09:07:27 +10:00
parent db9698c331
commit a8f7e2b0a7
8 changed files with 35 additions and 85 deletions

View File

@ -246,7 +246,7 @@
<Compile Include="Network\Utils\FixedBufferStream.cs" />
<Compile Include="Network\Utils\GZipHeaderReader.cs" />
<Compile Include="Network\NetworkProcessor.WoM.cs" />
<Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\CommandList.cs" />
<Compile Include="Commands\CommandReader.cs" />
<Compile Include="Network\Utils\NetWriter.cs" />
<Compile Include="Math\AABB.cs" />

View File

@ -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 ) {

View File

@ -8,7 +8,6 @@ namespace ClassicalSharp.Commands {
public class CommandReader {
string rawInput;
int firstArgOffset;
int curOffset;
/// <summary> Returns the next argument, or null if there are no more arguments left. </summary>
@ -33,29 +32,6 @@ namespace ClassicalSharp.Commands {
return arg;
}
/// <summary> Attempts to parse the next argument as a 32-bit integer. </summary>
public bool NextInt( out int value ) {
return Int32.TryParse( Next(), out value );
}
/// <summary> Attempts to parse the next argument as a 32-bit floating point number. </summary>
public bool NextFloat( out float value ) {
return Single.TryParse( Next(), out value );
}
/// <summary> Attempts to parse the next argument as a 6 digit hex number. </summary>
/// <remarks> #xxxxxx or xxxxxx are accepted. </remarks>
public bool NextHexColour( out FastColour value ) {
return FastColour.TryParse( Next(), out value );
}
/// <summary> Attempts to parse the next argument using the specified parsing function. </summary>
public bool NextOf<T>( out T value, TryParseFunc<T> 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;
}
/// <summary> Total number of arguments yet to be processed. </summary>
public int RemainingArgs {
get { return CountArgsFrom( curOffset ); }
}
/// <summary> Total number of arguments provided by the user. </summary>
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;
}
/// <summary> Rewinds the internal state back to the first argument. </summary>
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
}
}
}

View File

@ -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++ )

View File

@ -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 );

View File

@ -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() );

View File

@ -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;

View File

@ -20,7 +20,6 @@ namespace ClassicalSharp {
public delegate TResult Func<T1, TResult>( T1 arg1 );
public delegate TResult Func<T1, T2, TResult>( T1 arg1, T2 arg2 );
public delegate TResult Func<T1, T2, T3, TResult>( T1 arg1, T2 arg2, T3 arg3 );
public delegate bool TryParseFunc<T>( string s, out T value );
// ################################################################
public static partial class Utils {