-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
228 lines (177 loc) · 8.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import socket
import argparse
import sys
import os
import xml.etree.ElementTree as ET
import vdf
from copy import deepcopy
from tabulate import tabulate
def saveNewServerInfoToSteamFile(serverName, ipAddress, queryPort, steamServerListPath):
completeAddress = ipAddress + ":" + str(queryPort)
# load and parse steam vdf server list
d = vdf.load(open(steamServerListPath))
d1 = deepcopy(d)
maxIndex = -1
for key, _ in d['Filters']['favorites'].items():
if int(key) > maxIndex:
maxIndex = int(key)
# create new server in vdf dict
d1['Filters']['favorites'][str(maxIndex + 1)] = {'name': serverName, 'address': completeAddress, 'LastPlayed': '0',
'appid': '0', 'accountid': '0'}
vdf.dump(d1, open(steamServerListPath, 'w'), pretty=True)
def updateServer(updateServerList, steamServerListPath):
print('Start updating servers')
d = vdf.load(open(steamServerListPath))
d1 = deepcopy(d)
for key, value in d['Filters']['favorites'].items():
for server in updateServerList:
if value['name'] == server[0]:
d1['Filters']['favorites'][key] = {'name': server[0],
'address': socket.gethostbyname(server[1]) + ':' + str(server[2]),
'LastPlayed': '0', 'appid': '0', 'accountid': '0'}
# save updated servers to steam server list file
vdf.dump(d1, open(steamServerListPath, 'w'), pretty=True)
def deleteServerFromSteamFile(serverName, steamServerListPath):
print('Start deleting servers')
d = vdf.load(open(steamServerListPath))
serverFound = False
for key, value in d['Filters']['favorites'].items():
if value['name'] == serverName:
deleteKey = key
serverFound = True
break
if serverFound:
# only delete item if server found else continue fixing up server indexing
d['Filters']['favorites'].pop(key)
d1 = deepcopy(d)
# fix up server indexing if necessary
keyIndex = 0
# clear up server list
d1['Filters']['favorites'].clear()
for key in d['Filters']['favorites'].keys():
keyIndex += 1
d1['Filters']['favorites'][keyIndex] = d['Filters']['favorites'][key]
# save updated servers to steam server list file
vdf.dump(d1, open(steamServerListPath, 'w'), pretty=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--update", help="only update servers and shutdown after", action="store_true")
parser.add_argument("-r", "--reset", help="resets/deletes config file and create a new blank", action="store_true")
parser.add_argument("configPath", help="path of the config file")
args = parser.parse_args()
print("Welcome to SteamNonStaticIPUpdater")
if args.reset:
# reset config file
os.remove(args.configPath)
if os.path.exists(args.configPath):
# load
print("load config file")
configFile = open(args.configPath, "r")
# load cfg xml
tree = ET.parse(configFile)
root = tree.getroot()
for elem in root:
if elem.tag == 'steamPaths':
# find server list path
steamServerFilePath = elem.find('ServerListPath').text
configFile.close()
else:
# create cfg xml
configFile = open(args.configPath, "w")
print("wait for steam server file path")
steamServerFilePath = input()
steamServerFilePath += "\\serverbrowser_hist.vdf"
config = ET.Element('config')
steamPaths = ET.SubElement(config, 'steamPaths')
ServerFilePath = ET.SubElement(steamPaths, 'ServerListPath')
ServerFilePath.text = steamServerFilePath
a = ET.tostring(config, encoding="unicode")
configFile.write(a)
print("new config file created with %s as server file path" % steamServerFilePath)
configFile.close()
#print(steamServerFilePath)
configFile = open(args.configPath, "r")
# parse cfg xml file
tree = ET.parse(args.configPath)
root = tree.getroot()
if args.update:
# only update
# update all server from cfg file in steam server list
print('Read config file and find all server')
updateServerList = []
for elem in root:
if elem.tag == 'Server':
updateServerList.append((elem.get('name'), elem.get('hostname'), int(elem.get('queryPort'))))
print('Found %s servers to update' % len(updateServerList))
updateServer(updateServerList, steamServerFilePath)
print('Finished updating Servers')
sys.exit(0)
while True:
print("Input Command")
newUserInput = input()
if newUserInput=='create':
# add new server to update list
print('Server Name')
serverName = input()
print('Hostname')
hostname = input()
print('QueryPort')
queryPort = int(input())
# add new server to cfg xml
element = ET.SubElement(root, 'Server', {'name':serverName, 'hostname':hostname, 'queryPort':str(queryPort)})
# save new server to cfg xml
tree.write(args.configPath)
# save new server to steam server list
saveNewServerInfoToSteamFile(serverName, socket.gethostbyname(hostname), queryPort, steamServerFilePath)
elif newUserInput == 'delete':
# list all servers
serverCount = 0
serverList = []
for elem in root:
if elem.tag == 'Server':
serverCount += 1
serverList.append([serverCount, elem.get('name'), elem.get('hostname'), elem.get('queryPort')])
print("\n" + tabulate(serverList, headers=['Server Nr', 'Server name', 'hostname', 'QueryPort'],
tablefmt='orgtbl') + "\n")
print('Server Name')
serverName = input()
print('Do you really want to delete Server %s [y/n]' % serverName)
answer = input()
if answer != 'y':
continue
print('Start deleting server from cfg xml file')
# delete server from cfg xml
for elem in root:
if elem.tag == 'Server' and elem.get('name') == serverName:
root.remove(elem)
break
# save new server to cfg xml
tree.write(args.configPath)
print('Start deleting server from steam server list file')
# delete server from steam server list
deleteServerFromSteamFile(serverName, steamServerFilePath)
print('Server %s removed' % serverName)
elif newUserInput == 'list':
serverCount = 0
serverList = []
for elem in root:
if elem.tag == 'Server':
serverCount += 1
serverList.append([serverCount, elem.get('name'), elem.get('hostname'), elem.get('queryPort')])
print("\n"+tabulate(serverList, headers=['Server Nr', 'Server name', 'hostname', 'QueryPort'],
tablefmt='orgtbl')+"\n")
elif newUserInput == 'update':
# update all server from cfg file in steam server list
print('Read config file and find all server')
updateServerList = []
for elem in root:
if elem.tag == 'Server':
updateServerList.append((elem.get('name'), elem.get('hostname'), int(elem.get('queryPort'))))
print('Found %s servers to update' % len(updateServerList))
updateServer(updateServerList, steamServerFilePath)
print('Finished updating Servers')
elif newUserInput == 'exit':
# terminate updater
break
configFile.close()
print('Bye Bye from SteamNonStaticIPUpdater')