Difference between revisions of "Check slots, ip and port in config file before starting"
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=== Option 1 - Update values if they have been modified (requires database connection) === | === 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. | + | 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. |
− | 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 | ||
− | |||
− | |||
<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> | ||
Line 17: | Line 14: | ||
from System.IO import File, Path | from System.IO import File, Path | ||
from System import Environment | from System import Environment | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
#Load objects from database. | #Load objects from database. | ||
clr.AddReference("TCAdmin.GameHosting.SDK") | clr.AddReference("TCAdmin.GameHosting.SDK") | ||
Line 28: | Line 21: | ||
ThisServer=Server(ThisService.ServerId) | ThisServer=Server(ThisService.ServerId) | ||
ThisGame=Game(ThisService.GameId) | ThisGame=Game(ThisService.GameId) | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
#Update values if changed. | #Update values if changed. | ||
ThisServer.GameHostingUtilitiesService.UpdateConfigFileVariableValues(ThisService.ServiceId) | ThisServer.GameHostingUtilitiesService.UpdateConfigFileVariableValues(ThisService.ServiceId) | ||
Line 52: | Line 27: | ||
=== Option 2 - Prevent service from starting if values have been modified (no database connection required) === | === 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. | 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. | + | '''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. | 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. | ||
Line 108: | Line 83: | ||
if realport != allowport: | if realport != allowport: | ||
raise Exception(String.Format("port has been changed to {0}. The correct value is {1}.", 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)) |
Latest revision as of 09:23, 23 July 2021
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.
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))