Controlling the Crysis Dedicated Server
From CryWiki
| ||||||||||||||
In this tutorial, we are going to learn to how control the Crysis Dedicated Server(DS) using C#. You will learn how to remotely execute server commands using the HTTP server built into the DS. Once you learn how to control the DS, you can code your own tool to ban, kick, say... ; anything that's possible in RCon is possible here. Check out the Crysis Server Toolkit API for a complete reference of functions.
Contents |
Prerequisites
- Visual Studio (2005/2008)
- We are going to use the C# component of Visual Studio, which is an integrated development environment(IDE) that allows us to code efficiently. A free version of Visual Studio called Visual C# Express can be downloaded here.
- Basic Understanding of Programming
- The Crysis Dedicated Server Toolkit for C#
- You can download it here. We'll be using this to control the server.
- A Properly Configured Crysis Dedicated Server Installation
- Learn how to configure the dedicated server here.
Configuring the Dedicated Server
First, we have to configure the dedicated server(DS) so it accepts incoming connections. Open up the autoexec.cfg file in your DS installation and add the following lines(skip this step if you already have them)
http_password (http password)
http_startserver port:(http port)
Replace the (rcon port), (rcon password), (http password), and (http port) with your desired values. Make sure you write down the http port and the http password as we're going to be using them later.
Setting up Visual Studio
In Visual Studio, click on File→New→Project... and select Visual C# / Console Application project. Give it a descriptive name and click OK.
|
Now you should see a tab page titled Program.cs. Here we are going to write all of our code.
Finally, add the CDSToolkit.cs file from the Crysis Dedicated Server Toolkit to your project. Right click on the project, select Add→Existing Item... and add the file.
Creating a CDSSession
In the main function, type the following and replace (ip) with the IP address of your server (if the server is on your computer, use 127.0.0.1), (port) with the DS's http port, and (password) with the DS's http password.
CrysisServer serverSession = new CrysisServer((ip), (port), (password));
This will create a new CDSession object. The entire toolkit revolves around this object.
|
Executing Arbitrary Commands
To execute a command, we are going to call CDSSession's ExecuteCommand method. Add the following and replace (command) with whatever you want to execute on the server.
serverSession.ExecuteCommand("(command)");
|
Testing it Out
To test this code out, we're going to make the server change the map to shore. First start your Dedicated Server. Then add the following to your code:
serverSession.ExecuteCommand("map shore");
Now click the run icon (the green arrow) in the toolbar. If it executed successfully, your server's map should change.
Common Problems
Make sure your ip, port, and password are right.
|
Using the API
The toolkit also contains a number of functions for commonly used commands. You can check it out at the Crysis Server Toolkit API. For example, to get the status of the server, simply call "Status()":
string status = serverSession.Status();
Executing Commands Asynchronously
You can also execute commands using the Asynchronous (async) model. This model doesn't block, and will let you update your user interface while the command is being processed. Most of the functions in the toolkit has an async alternative. For example, the status function we used we used earlier has an async version called StatusAsync. The following is an example of an async call to Status.
Result r = serverSession.KickAsync(player); //kicks the player using async
while (r.Status == AsyncCallStatus.Running) //wait until the kick has finished
{
Thread.Sleep(1);
Application.DoEvents(); //update the user interface
}
if (r.Status == AsyncCallStatus.Error) //if there's an error, throw an exception
{
throw r.Value as Exception; //in case of an error, the result's value is the exception
}
string status = r.Value as string; //Gets the value of the result casted to a string



