Check slots, ip and port in config file before starting

From TCAdmin 2.0 Documentation
Jump to: navigation, search

Create the scripts

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)