Check slots, ip and port in config file before starting

From TCAdmin 2.0 Documentation
Jump to: navigation, search

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. 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 and check the service manager console.log. If the lines are missing from the config file they will be added. If the values have been changed they will be updated to the correct values.

Update the values of SLOTS_LINE, IP_LINE and GAMEPORT_LINE as needed. By default these values are configured for Minecraft.

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

SLOTS_LINE="max-players="
IP_LINE="server-ip="
GAMEPORT_LINE="server-port="

#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)

#Add missing lines.
for config in ThisGame.ConfigFiles:
  configpath=Path.Combine(ThisService.RootDirectory, config.RelativePath)
  if File.Exists(configpath):
    contents=File.ReadAllText(configpath)
    #Add slots
    if contents.IndexOf(SLOTS_LINE) == -1 and config.Template.IndexOf(SLOTS_LINE) != -1 :
      File.AppendAllText(configpath, Environment.NewLine + SLOTS_LINE + ThisService.Slots.ToString())
      Script.WriteToConsole("Added slots line to " + configpath)
    #Add IP
    if contents.IndexOf(IP_LINE) == -1 and config.Template.IndexOf(IP_LINE) != -1 :
      File.AppendAllText(configpath, Environment.NewLine + IP_LINE + ThisService.IpAddress.ToString())
      Script.WriteToConsole("Added IP line to " + configpath)
    #Add Game Port
    if contents.IndexOf(GAMEPORT_LINE) == -1 and config.Template.IndexOf(GAMEPORT_LINE) != -1 :
      File.AppendAllText(configpath, Environment.NewLine + GAMEPORT_LINE + ThisService.GamePort.ToString())
      Script.WriteToConsole("Added game port line to " + configpath)      
      
#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))