Check slots, ip and port in config file before starting

Revision as of 13:44, 31 January 2018 by TCAWiki (talk | contribs) (Created page with "=== 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 creatin...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.

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

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)
Retrieved from "https://help.tcadmin.com/index.php?title=Check_slots,_ip_and_port_in_config_file_before_starting&oldid=1442"