Difference between revisions of "Check slots, ip and port in config file before starting"

Line 2: Line 2:
 
Go to the game's settings. Click on the Custom Scripts icon. Add the following script. After creating the script go to the game's settings and click on Update Existing Services. To test the script on a single game server go to the service settings and save. Then start the game server and check the service manager console.log.
 
Go to the game's settings. Click on the Custom Scripts icon. Add the following script. After creating the script go to the game's settings and click on Update Existing Services. To test the script on a single game server go to the service settings and save. Then start the game server and check the service manager console.log.
  
This script will update the config file only if "Read variable values from file" is checked in the config file settings.
+
This script will update the config file only if '''"Read variable values from file"''' is checked in the config file settings.
  
 
<span style="color:red">This script connects to the database. If the database is offline the script will fail. You should have "Ignore execution errors" checked in case the database connection is not available when the script executes.</span>
 
<span style="color:red">This script connects to the database. If the database is offline the script will fail. You should have "Ignore execution errors" checked in case the database connection is not available when the script executes.</span>

Revision as of 10:54, 11 September 2020

Option 1 - Update values if they have been modified (requires database connection)

Go to the game's settings. Click on the Custom Scripts icon. Add the following script. After creating the script go to the game's settings and click on Update Existing Services. To test the script on a single game server go to the service settings and save. Then start the game server and check the service manager console.log.

This script will update the config file only if "Read variable values from file" is checked in the config file settings.

This script connects to the database. If the database is offline the script will fail. You should have "Ignore execution errors" checked in case the database connection is not available when the script executes.

Operating System: Any
Description: Check slots, ip and port in config file before starting
Script Engine: IronPython
Event: Before started
Ignore execution errors Checked
Script:
import clr;
from System.IO import File, Path
from System import Environment
 
#Load objects from database.
clr.AddReference("TCAdmin.GameHosting.SDK")
from TCAdmin.GameHosting.SDK.Objects import Server, Service, Game
ThisService=Service(ThisService_ServiceId)
ThisServer=Server(ThisService.ServerId)
ThisGame=Game(ThisService.GameId)
       
#Update values if changed.
ThisServer.GameHostingUtilitiesService.UpdateConfigFileVariableValues(ThisService.ServiceId)

Option 2 - Prevent service from starting if values have been modified (no database connection required)

Go to the game's settings. Click on the Custom Scripts icon. Add the following script. This can also be configured as a global script. After creating the script go to the game's settings and click on Update Existing Services. To test the script on a single game server go to the service settings and save. Then start the game server. If the IP, port or slots don't have the correct values you will see an error message.

Update the values of slots_regex, ip_regex and port_regex with regular expressions to read the current values. Remember to escape \ to \\. By default these values are configured for San Andreas Multiplayer.

Operating System: Any
Description: Check slots, ip and port in config file before starting
Script Engine: IronPython
Event: Before started
Ignore execution errors Unchecked
Script:
import clr;
from System.IO import File, Path
from System.Text.RegularExpressions import Regex, RegexOptions
from System import Exception, String;

slots_regex = "^\\s*maxplayers\\s+(?<slots>\\d+)"
ip_regex = "^\s*bind\\s+(?<ip>[\\d\\.]+)"
port_regex = "^\\s*port\\s+(?<port>\\d+)"

configpath=Path.Combine(ThisService_RootDirectory, "server.cfg")
if File.Exists(configpath):
  contents=File.ReadAllText(configpath)
  
  #Match slots
  matches = Regex.Matches(contents, slots_regex, RegexOptions.Multiline)
  if matches.Count == 0:
    raise Exception("maxplayers has been removed from server.cfg")
 
  for match in matches:
    realslots=int(match.Groups["slots"].Value)
    allowslots=int(ThisService_Slots)
    if realslots > allowslots:
      raise Exception(String.Format("maxplayers has been increased to {0}. The correct value is {1}.", realslots, allowslots))
   
  #Match ip
  matches = Regex.Matches(contents, ip_regex, RegexOptions.Multiline)
  if matches.Count == 0:
    raise Exception("bind has been removed from server.cfg")
  
  for match in matches:
    realip=match.Groups["ip"].Value
    allowip=ThisService_IpAddress
    if realip != allowip:
      raise Exception(String.Format("bind has been changed to {0}. The correct value is {1}.", realip, allowip))
      
  #Match port
  matches = Regex.Matches(contents, port_regex, RegexOptions.Multiline)
  if matches.Count == 0:
    raise Exception("port has been removed from server.cfg")
  
  for match in matches:
    realport=int(match.Groups["port"].Value)
    allowport= int(ThisService_GamePort)
    if realport != allowport:
      raise Exception(String.Format("port has been changed to {0}. The correct value is {1}.", realport, allowport))

Option 3 - JSON config file

This script updates the ip, port and slots in a config file with json format. This example is for Rage MP
import clr;
import System;
clr.AddReference("Newtonsoft.Json")
from System.IO import File, Path
from Newtonsoft.Json import JsonConvert, Formatting

configpath=Path.Combine(ThisService_RootDirectory, "conf.json")

if File.Exists(configpath):
  contents=File.ReadAllText(configpath)
  jsondata = JsonConvert.DeserializeObject(contents)
  jsondata["bind"]=ThisService_IpAddress
  jsondata["port"]=ThisService_GamePort
  jsondata["maxplayers"]=ThisService_Slots
  File.WriteAllText(configpath, JsonConvert.SerializeObject(jsondata, Formatting.Indented))
Retrieved from "https://help.tcadmin.com/index.php?title=Check_slots,_ip_and_port_in_config_file_before_starting&oldid=2286"