User Tools

Site Tools


client_side_scripting:client_scripting_interface-basic_howto

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
client_side_scripting:client_scripting_interface-basic_howto [2020/07/30 16:08]
boingman Added some specific information about newlines and additional watch arguments with expected results; amended version numbers with client
client_side_scripting:client_scripting_interface-basic_howto [2021/06/19 08:12]
boingman [Using Scripts in the Client] Add further information about JXClient script management.
Line 1: Line 1:
 ====== Client Scripting Interface ====== ====== Client Scripting Interface ======
- 
 The **client scripting interface** can be used to control a Crossfire client using an external program (//​script//​). This interface can be used to extend the client or automate repetitive tasks (see [[client_side_scripting:​scripts|sample scripts]]). The **client scripting interface** can be used to control a Crossfire client using an external program (//​script//​). This interface can be used to extend the client or automate repetitive tasks (see [[client_side_scripting:​scripts|sample scripts]]).
  
-The script ​is started from the client ​and communicates ​by reading information from standard input and writing commands to standard output. ​A full list of commands ​can be found below. +The script ​communicates with the client by reading information from standard input and writing commands to standard output. ​Scripts ​can be written in any programming languageThe same client scripting interface is used by the GTKv2 client and the JXClient. ​Any discrepancy should be reported ​as a bug.
- +
- +
-===== Before we start ===== +
- +
-Several clients are available for this game. This page documents ​the scripting capabilities of version 1.71.0 GTK2 client and the JXClient ​of 27-Jan-2020. +
- +
-CrossFire scripting does not use carriage return (hexadecimal 0d) characters as part of newlines regardless of operating system. Only the linefeed (hexadecimal 0a) character is acceptable ​as a line terminator. +
- +
-===== Getting Started =====+
  
-The ''​script''​ command starts a script at the given path. The script must be executable (''​chmod +x''​ with an appropriate shebang). For some languages, it may be necessary ​to write a wrapper script to execute the actual program.+===== Using Scripts in the Client ==== 
 +Several client commands are available ​to start, stop, list, and communicate with scripts:
  
-Additionally,​ on Windows systems where the shebang is not supported, the player must provide a means of running such a script. For example ​''​script ​python ​<path-to-script>'' ​if you are using python.+  * ''​script ​<​path>​''​: start a script ​located at <path>. 
 +  * ''​scripts'':​ list currently running scripts, along with their numerical IDs 
 +  * ''​scripttell <id> <​string>'':​ send text to currently running ​script ​<id> 
 +  * ''​scriptkill <​id>'':​ stop currently running script <id>
  
-The ''​scripts'' ​command lists running scripts ​with a numeric identifier. ​Scripts ​can be stopped using the ''​scriptkill''​ commandThe ''​scripttell'' ​command can be used to send arbitrary commands ​to a script.+  * JXClient only: 
 +    * ''​scriptkillall''​: stop all currently ​running scripts 
 +    * Unique alphanumeric substrings of <​path>​ may be used as <id> so that script names may be used in lieu of numbers 
 +===== Writing ​Scripts ​===== 
 +The script must be executable (e.g. ''​chmod +x'' ​with an appropriate shebang). For some languages, it may be necessary ​to write a wrapper script ​to start the program. Additionally,​ on Windows systems where the shebang is not supported, the player must provide a means of running such a script. For example ''​script python <​path-to-script>''​ if you are using python.
  
 ==== Hello World ==== ==== Hello World ====
Line 76: Line 74:
   fflush(stdout)   fflush(stdout)
  
-The stdout has something called a buffer. When you write to output device, it's not immediatly ​sent to it. For performance reasons, successive print to stdout are grouped. Most of the time, \n is enough to force sending of data, but we ensure all data are sent to client by flushing the stdout (force empty buffer). In the future, when you think client didn't get a command but the script did send it, ensure you flushed stdout.\\ ​+The stdout has something called a buffer. When you write to output device, it's not immediately ​sent to it. For performance reasons, successive print to stdout are grouped. Most of the time, \n is enough to force sending of data, but we ensure all data are sent to client by flushing the stdout (force empty buffer). In the future, when you think client didn't get a command but the script did send it, ensure you flushed stdout.\\ ​
 \\  \\ 
-Then comes a loop. This loop will read from stdin (where client puts informations ​for the script) and copy them to stderr (our only access to console since stdout is a connection to client). Because I don't want to use scanf I used the binary read and write commands. Stdin is the file handle 0 and stderr is file handle 2. We first read up to 200 char from stdin and if we read something we write it to stderr. If we didn't read anything, that means we have lost the client (shouldn'​t happen) and we simply exit.+Then comes a loop. This loop will read from stdin (where client puts information ​for the script) and copy them to stderr (our only access to console since stdout is a connection to client). Because I don't want to use scanf I used the binary read and write commands. Stdin is the file handle 0 and stderr is file handle 2. We first read up to 200 char from stdin and if we read something we write it to stderr. If we didn't read anything, that means we have lost the client (shouldn'​t happen) and we simply exit.
  
 Since we asked to monitor all commands from client to server, we get them. These commands are our move commands and they use the same format as issue. If you run our first script while this second script is still running, you will still say hello world, but you'll get the following in your console: Since we asked to monitor all commands from client to server, we get them. These commands are our move commands and they use the same format as issue. If you run our first script while this second script is still running, you will still say hello world, but you'll get the following in your console:
Line 91: Line 89:
  
   scriptkill <​pathtoscript>​   scriptkill <​pathtoscript>​
 +
 +(The JXClient additionally recognizes partial script names as well as numbers and has an additional command scriptkillall to stop all currently running scripts in one go.)
  
 or or
-  scriptkill <​number>​ 
  
-the JXClient additionally supports +  scriptkill <​number>​
-  scriptkillall+
  
 Then type __scripts__ to ensure it's stopped (or to figure out the number of the script). Edit it, comment the line printf("​monitor\n"​),​ compile and run the script again. Move and look at console. You see nothing. The script didn't ask anything so the client didn't tell it anything. Your script will only get that for which it asks. Then type __scripts__ to ensure it's stopped (or to figure out the number of the script). Edit it, comment the line printf("​monitor\n"​),​ compile and run the script again. Move and look at console. You see nothing. The script didn't ask anything so the client didn't tell it anything. Your script will only get that for which it asks.
Line 134: Line 132:
 ===== Reference ===== ===== Reference =====
  
-There are two things you can still do with Script. The first is to request a bit of informations. The client then tells the script what it wants to know. The second thing is triggering an action of the script from client interface. The command scripttell allows player to say something to a script. The script will get the exact command typed by player. See below for command list.+There are two things you can still do with scripts. The first is to request a bit of information. The client then tells the script what it wants to know. The second thing is triggering an action of the script from client interface. The command scripttell allows player to say something to a script. The script will get the exact command typed by player. See below for command list.
  
  
Line 141: Line 139:
  
   * //watch <command type>// - watch the given command from server-client protocol. <command type> specifies the commands we want to watch. Set to empty to get all commands.   * //watch <command type>// - watch the given command from server-client protocol. <command type> specifies the commands we want to watch. Set to empty to get all commands.
-    * Besides what is already documented, watch also supports "​tick",​ "drawinfo" (GTK2 client) and "drawextinfo" (JXClient). +    * Besides what is already documented, watch also supports "​tick",​ "drawextinfo" (for most servers)and "drawinfo" (for very old servers
-    * The "​tick"​ argument will provide the script with "watch tick <​integer>",​ as a tick of the client's clock. +    * The "​tick"​ argument will provide the script with "watch tick <​integer>",​ as a tick of the server's clock. 
-    * The "draw" ​commands are functionally equivalent in the GTK2 client and the JXClient with some minor differences. Both will provide the script with text printed in the Messages text area. The GTK2 provides the script with "​watch ​drawinfo ​<​integer>​ <​message>", ​while the JXClient will provide ​"​watch ​drawextinfo <​integer> ​<​integer>​ <​message>"​.+    * "drawextinfo" provide the script with text printed in the Messages text area. The format is "​watch ​drawextinfo <​integer> ​<​integer>​ <​message>​". For some very old servers (none of them which are online at the time of writing), "​drawextinfo"​ is not supported and you must fall back to "​drawinfo", ​which has the arguments ​"​watch ​drawinfo ​<​integer>​ <​message>"​.
   * //unwatch <command type>// - unwatch the given command from server-client protocol. <command type> specifies the commands we want to watch. Set to empty to get all commands.   * //unwatch <command type>// - unwatch the given command from server-client protocol. <command type> specifies the commands we want to watch. Set to empty to get all commands.
-  * //request <data type>// - Request a piece of informations ​from client memory. Following is a table of <data type> allowed:+  * //request <data type>// - Request a piece of information ​from client memory. Following is a table of <data type> allowed:
  
 <box round | Data Type Table> <box round | Data Type Table>
Line 174: Line 172:
  
   * //issue <​repeat>​ <​must_send>​ <​command>//​ - send <​command>​ to server on behalf of client. <​repeat>​ is the number of times to execute command <​must_send>​ tells whether or not the command must sent at all cost (1 or 0). <​repeat>​ and <​must_send>​ are optional parameters. See [[#The Issue Command]] for more details.   * //issue <​repeat>​ <​must_send>​ <​command>//​ - send <​command>​ to server on behalf of client. <​repeat>​ is the number of times to execute command <​must_send>​ tells whether or not the command must sent at all cost (1 or 0). <​repeat>​ and <​must_send>​ are optional parameters. See [[#The Issue Command]] for more details.
-  * //issue mark <​tag>//​ - special case of issue command. only gets the command '​mark'​ and a object tag 
-  * //issue lock <new state> <​tag>//​ - special case of issue command. Only gets the command '​lock'​ with 2 parameters 
   * //draw <​color>​ <​text>//​ - draw the following text on client interface with given color. Usefull for debugging and may help you to forget about using the stderr.   * //draw <​color>​ <​text>//​ - draw the following text on client interface with given color. Usefull for debugging and may help you to forget about using the stderr.
   * //monitor// - start monitoring commands send from client to server. Doesn'​t take any parameter. This goes to the script'​s stdin automatically.   * //monitor// - start monitoring commands send from client to server. Doesn'​t take any parameter. This goes to the script'​s stdin automatically.
   * //​unmonitor//​ - stop monitoring commands send from client to server. Doesn'​t take any parameter.   * //​unmonitor//​ - stop monitoring commands send from client to server. Doesn'​t take any parameter.
 +  * Special case ISSUE commands (these are issued without repeat and mustsend arguments):
 +    * //issue apply <​tag>//​ - Applies the object corresponding to <​tag>​.
 +    * //issue take <tag> [<​count>​]//​ - Retrieves the object corresponding to <​tag>​. If <​count>​ is given, it represents how many items to take. By default all items are retrieved.
 +    * //issue drop <tag> [<​count>​]//​ - Discards the object corresponding to <​tag>​. If <​count>​ is given, it represents how many items to drop. By default all items are discarded.
 +    * //issue mark <​tag>//​ - Marks the object corresponding to <​tag>​.
 +    * //issue lock <​newstate>​ <​tag>//​ - Locks or unlocks the object corresponding to <tag> in your inventory depending on the value of <​newstate>​. Valid values are 1 for lock and 0 for unlock.
  
 ==== Information from client ==== ==== Information from client ====
Line 194: Line 196:
  
   * FIXME ** NOTE more information strings to be added here, incomplete list **   * FIXME ** NOTE more information strings to be added here, incomplete list **
- 
  
  
 ==== The issue command ==== ==== The issue command ====
-This command has it'​s ​particularities. I'll try to detail all I've encountered so far.+This command has it'​s ​peculiarities.
  
 Usage: Usage:
   issue [<​repeat>​ [<​must_send>​]] <​command>​   issue [<​repeat>​ [<​must_send>​]] <​command>​
  
-  * //<​repeat>//​ - Times the command should be issued. It's an optional numeric parameter. (Doesn'​t always work as expected) +  * //<​repeat>//​ - Times the command should be issued ​(not repeated after it's initial execution). It's an optional numeric parameter. (Doesn'​t always work as expected.
-  * //<​must_send>//​ - May be 1 or 0. If set to 1 the command will always be sent to the server. Otherwise it //may// be discarded under certain circumstances. ​+  * //<​must_send>//​ - May be 1 or 0. If set to 1 the command will always be sent to the server. Otherwise it //may// be discarded under certain circumstances.
  
 The next examples should clarify the use of numeric parameters. The next examples should clarify the use of numeric parameters.
Line 238: Line 239:
   issue 0 0 lookat 2 3   issue 0 0 lookat 2 3
  
-FIXME add a complete set of commands that work with <repeatand another of those that work without it.+  * FIXME Move "​issue ​<cmd<​tag>"​ documentation here, better formatted.
  
  
 ===== Platform-Specific Notes ===== ===== Platform-Specific Notes =====
  
-Scripting works thanks to a patch from archaios. ​Known issues are:+Known issues are:
  
     * If you want to run a Perl script for instance, you need to issue 'perl <​script_name.pl>,​ even if .pl is correctly seen as perl script.     * If you want to run a Perl script for instance, you need to issue 'perl <​script_name.pl>,​ even if .pl is correctly seen as perl script.
     * If script doesn'​t output anything, try turning off buffering (in perl $| = 1) or flush your pipe, or add a sleep at end of program. It seems Windows closes pipes at script termination before client gets time to grab output. ​     * If script doesn'​t output anything, try turning off buffering (in perl $| = 1) or flush your pipe, or add a sleep at end of program. It seems Windows closes pipes at script termination before client gets time to grab output. ​
  
 +===== A Few Thanks =====
  
 +Scripting was rendered initially functional with a patch from archaios and is being kept current by GTKv2 client maintainers Partmedia and crowbert, and JXClient maintainer Ragnor.
  
client_side_scripting/client_scripting_interface-basic_howto.txt · Last modified: 2021/06/19 08:12 by boingman