Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console System Improvements #46

Merged

Conversation

MuffinTastic
Copy link
Contributor

@MuffinTastic MuffinTastic commented Jan 31, 2023

Changes:

  • Adds bindings to access several console system features from managed
  • Adds callbacks to CVars
  • Adds commands
    • Created on native side via the CCmd class
    • Created on managed side via ConCmd.X attributes (only a couple exist right now)
    • Managed dispatch system that converts arguments to the callback's parameter types (see examples)
  • Improves functionality of command parsing
    • Sections of the command can be surrounded with quotes ", enabling the passing of string arguments containing spaces
    • Commands can be split up into multiple statements to be run one after the other, via semicolons ; and newlines \n
    • Comment out the remainder of a line with //
    • If desired, you can pass in a cursor position and the parsing API will give you back which statement and which argument the cursor is in. Useful for autocomplete
    • I hate dealing with strings in C++
  • list command has been made a CCmd instance, and now lists the flags of each command and variable
Usage Examples

Native CVar with a callback

static FloatCVar cvartest_float( "cvartest.float", 0.0f, CVarFlags::None, "Yeah",
	[]( float oldValue, float newValue )
	{
		spdlog::trace( "cvartest.float changed! old {}, new {}", oldValue, newValue );
	}
);

Native CCmd

static CCmd cvartest_command( "cvartest.command", CVarFlags::None, "A test command",
	[]( std::vector<std::string> arguments )
	{
		spdlog::trace( "cvartest.command has been invoked! Hooray" );
		
		for ( int i = 0; i < arguments.size(); i++ )
		{
		    spdlog::trace( "\t{} - '{}'", i, arguments.at( i ) );
		}
	}
);

Several managed ConCmds

[ConCmd.Test( "cvartest.managed.anything", "This is a description" )]
public static void One( List<string> arguments )
{
	Log.Info( "This is a command, woohoo" );

	foreach ( var arg in arguments )
	{
		Log.Info( $" - {arg}" );
	}
}

[ConCmd.Cheat( "cvartest.managed.string" )]
public static void String( string hello )
{
	Log.Info( hello + " [modified]");
}

[ConCmd.Test( "cvartest.managed.float" )]
public static void Float( float hello )
{
	Log.Info( hello + 20.0f );
}

[ConCmd.Test( "cvartest.managed.double" )]
public static void Double( double hello )
{
	Log.Info( hello + 34.0 );
}

[ConCmd.Test( "cvartest.managed.int" )]
public static void Int( int hello )
{
	Log.Info( hello - 123 );
}

[ConCmd.Test( "cvartest.managed.uint" )]
public static void UInt( uint hello )
{
	Log.Info( hello + 456 );
}

[ConCmd.Test( "cvartest.managed.long" )]
public static void Long( long hello )
{
	Log.Info( hello - 1234 );
}

[ConCmd.Test( "cvartest.managed.ulong" )]
public static void ULong( ulong hello )
{
	Log.Info( hello - 5678 );
}

[ConCmd.Test( "cvartest.managed.bool" )]
public static void Bool( bool hello )
{
	Log.Info( hello );
}

[ConCmd.Test( "cvartest.managed.two" )]
public static void Two( int one, float two )
{
	Log.Info( $"{one}, {two}" );
}

[ConCmd.Test( "cvartest.managed.twodefault" )]
public static void TwoDefault( int one, float two = 10.0f )
{
	Log.Info( $"{one}, {two}" );
}

[ConCmd.Test( "cvartest.managed.threedefault" )]
public static void ThreeDefault( int one, bool two, float three = 10.0f )
{
	Log.Info( $"{one}, {two}, {three}" );
}

[ConCmd.Test( "cvartest.managed.threedefault2" )]
public static void ThreeDefault2( int one, bool two = true, float three = 10.0f )
{
	Log.Info( $"{one}, {two}, {three}" );
}

image

Notes:

  • While the bindings for registering managed ConVars are there you currently can't make them at the moment, because that really should be done through static properties, but source generation is not my forté. Happy to implement suggestions, unless you want to merge and do it yourself

  • A few CVarSystem methods (GetString(), ToString()) don't have bindings right now due to memory leak concerns
    Possible interop memory leak when passing temporary strings from native to managed #43

@xezno
Copy link
Member

xezno commented Jan 31, 2023

LGTM, thanks a ton for this! Should make both engine & game development much easier, and I do feel your concerns about string operations in c++ - I think they suck too - but I think your solution works fine. Awesome work!

@xezno xezno merged commit b0e67a0 into mocha-engine:master Jan 31, 2023
@MuffinTastic MuffinTastic deleted the MuffinTastic/cvar-system-improvements branch January 31, 2023 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants