scripthookvdotnet icon indicating copy to clipboard operation
scripthookvdotnet copied to clipboard

Can mods add console commands?

Open justalemon opened this issue 5 years ago • 15 comments

I was trying to create a Console Command on both v2 and v3 but I ended up finding out that the Console Attribute is part of the ASI and not the DLLs.

Considering that is not possible to access the SHVDN namespace: Is possible to create console commands from our scripts? If so, how?

justalemon avatar Nov 19 '19 05:11 justalemon

Yes and no. You don't have to manually add console commands: All script classes are accessible from the console, so any static method can be called already. It is not currently possible to add those to the list printed via Help() though.

E.g. the following:

public class MyScript : Script
{
    public static void MyCommand()
    {
         ...
    }
};

Can be called by typing MyScript.MyCommand() into the console. The static method doesn't have to be a member of a Script class. It can be anything in the script assembly. As long as it is public static.

crosire avatar Nov 19 '19 19:11 crosire

I think in the initial version of the console (when it was still in C# userspace) it was possible to add commands by simply adding the ConsoleCommand to a method of a class which inherits Script. I'm not sure if we can easily go back to such functionality with the class being in core. IMO it would be nice, since it would declare some kind of public API for users of a script. Else a user would have to know everything about public static script methods beforehand.

JohnnyCrazy avatar Nov 19 '19 23:11 JohnnyCrazy

It's still technically possible. There was an implementation that did this, but I removed it before the release in https://github.com/crosire/scripthookvdotnet/commit/7995f3fdd3cb8fcd0583d8a1d23b0d22e691d8a6. Can't really remember why. Probably for simplification. But I guess that could be added back.

crosire avatar Nov 19 '19 23:11 crosire

Scripts could then add their own Help() command to list there public static methods.

Tanjitsu avatar Nov 20 '19 13:11 Tanjitsu

Can be called by typing MyScript.MyCommand() into the console. The static method doesn't have to be a member of a Script class. It can be anything in the script assembly. As long as it is public static.

If the script uses SHVDN 2, is not possible to execute the function because it does not checks what version the assembly needs (v2 vs v3).

image

justalemon avatar Nov 23 '19 07:11 justalemon

It is not currently possible to add those to the list printed via Help() though.

This would be pretty useful to avoid polluting control presses and the input cheat field. Think of the following:

  • User writes Help() on the console
  • Sees ConfigureScriptName() with a description of Opens the configuration options for ScriptName
  • Enters ConfigureScriptName() on the console and a NativeUI menu opens up with the options

justalemon avatar Nov 23 '19 07:11 justalemon

Enters ConfigureScriptName() on the console and a NativeUI menu opens up with the options

This would make it dependent upon NativeUI which is not maintainable :(

Tanjitsu avatar Nov 23 '19 10:11 Tanjitsu

This would make it dependent upon NativeUI which is not maintainable :(

That is just an example showing what I'm trying to do with BulletLines. Calling ConfigureScriptName() would just do UIMenu.Visible = !UIMenu.Visible;.

The point here is that showing the user what he can do with the script in Help() will make it easier if he "forgets" to read the configuration instructions.

justalemon avatar Nov 23 '19 19:11 justalemon

Maybe something like a ScriptAttribute[HasHelp] to tell the Console that the Script as a default Script.Help() Method which can then be listed in the Console.Help() options.

Tanjitsu avatar Nov 24 '19 10:11 Tanjitsu

Can we write in this console?

jokeruarwentto avatar Feb 08 '20 19:02 jokeruarwentto

Can we write in this console?

I believe that's already possible, just rename ScriptHookVDotNet.asi to *.dll and add reference to it.

Then:

var console = AppDomain.CurrentDomain.GetData("Console") as SHVDN.Console;
console.PrintInfo("Hello");

Sardelka9515 avatar Oct 09 '22 08:10 Sardelka9515

I believe that's already possible, just rename ScriptHookVDotNet.asi to *.dll and add reference to it.

No need to rename it, you can reference the ASI directly.

justalemon avatar Oct 09 '22 21:10 justalemon

Yeah, but only if you edit the project file directly, visual studio won't let you do so in GUI

Sardelka9515 avatar Oct 10 '22 10:10 Sardelka9515

Weird, both VS and RIder allow me to add the file directly without needing to rename it.

justalemon avatar Oct 12 '22 05:10 justalemon

When we add the ability to console commands, I'd like to use the classic syntax [cmd name] [arguments], just like how shells like bash and FiveM console parse commands and arguments. We can keep the ability to invoke public static methods with a dedicated command as long as all of scripts run in a single AppDomain (may be too difficult to achieve if we managed to provide a dedicated AppDomain per script Assembly so we can reload individual script assembly though). We would manage arguments with the classic strings. Here's part of code how FiveM parses commands in the console: https://github.com/citizenfx/fivem/blob/5979ce4596ddc1f8cdf0a51fcb6c33453f980fc9/code/client/citicore/console/Console.Commands.cpp#L51 https://github.com/citizenfx/fivem/blob/5979ce4596ddc1f8cdf0a51fcb6c33453f980fc9/code/client/citicore/console/Console.cpp#L406

kagikn avatar May 25 '23 11:05 kagikn