-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
158 lines (125 loc) · 4.72 KB
/
config.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
import pytoml as toml
import os
import path_finder
current_server = ""
current_server_full_name = ""
remote_data_file = ""
local_versions_file = ""
local_checksums_file = ""
nwn_server_address = ""
config_path = ""
main_conf_values = {}
confs = {}
gui_confs = {}
player_name = ""
launcher_version_address = "http://5.9.81.74:2020/version"
def load_config(path):
global confs, gui_confs
confs, gui_confs = _load_configs(path)
print(gui_confs)
global config_path
config_path = path
global player_name
player_name = _load_player_name()
global main_conf_values
with open(os.path.join(path, "main_config.toml")) as conffile:
config = toml.load(conffile)
main_conf_values = config
if config["default_server"] in confs:
setup_config_for(config["default_server"])
else:
serialize_to_main_conf(["default_server"], [next(iter(confs.values()))["name_short"]])
setup_config_for(next(iter(confs.values()))["name_short"])
def setup_config_for(server_name):
print("setting up server for: " + server_name)
server_config = confs[server_name]
global current_server
current_server = server_config["name_short"]
global remote_data_file
remote_data_file = server_config["remote_data_file"]
global local_versions_file
local_versions_file = current_server + "_file_versions.toml"
global local_checksums_file
local_checksums_file = "file_checksums.toml"
global nwn_server_address
nwn_server_address = server_config["nwn_server_address"]
global current_server_full_name
current_server_full_name = server_config["name_full"]
def get_gui_conf(key, alt_key = None, default_value = None) -> object:
if key in gui_confs[current_server]:
return gui_confs[current_server][key]
else:
if alt_key in gui_confs[current_server]:
return gui_confs[current_server][alt_key]
return default_value
def get_server_conf(key, default_value = None) -> object:
if key in confs[current_server]:
return confs[current_server][key]
return default_value
def serialize_to_main_conf(keys : list, values : list):
config = None
print("Setting keys: ", keys, " to values: ", values)
with open(os.path.join(config_path, "main_config.toml")) as conffile:
config = toml.load(conffile)
for i in range(0, len(keys)):
config[keys[i]] = values[i]
global main_conf_values
main_conf_values = config
if config is not None:
f = open(os.path.join(config_path, "main_config.toml"),'w')
f.write(toml.dumps(config))
f.flush()
f.close()
def serialize_to_current_server_conf(keys: list, values: list):
server_config = None
print("Setting keys: ", keys, " to values: ", values)
with open(os.path.join(config_path, current_server, "config.toml")) as conffile:
server_config = toml.load(conffile)
for i in range(0, len(keys)):
server_config[keys[i]] = values[i]
if server_config is not None:
f = open(os.path.join(config_path, current_server, "config.toml"),'w')
f.write(toml.dumps(server_config))
f.flush()
f.close()
def _load_player_name() -> str:
try:
with open(path_finder.get_nwnplayer_path()) as nwnplayer_conf:
for line in nwnplayer_conf:
if "Player Name" in line or "player name" in line:
return line[line.index('=') + 1:-1]
except:
print("Couldn't open NWN config file.")
return " "
def set_player_name(name: str):
f = open(path_finder.get_nwnplayer_path(), 'r')
lines = f.readlines()
for i in range(0, len(lines)):
if "Player Name" in lines[i] or "player name" in lines[i]:
lines[i] = lines[i][0:lines[i].index('=')+1] + name + '\n'
f.close()
f = open(path_finder.get_nwnplayer_path(), 'w')
f.writelines(lines)
# do the remaining operations on the file
f.close()
def _load_configs(path) -> dict:
confs = {}
gui_confs = {}
for item in os.listdir(path):
if os.path.isdir(os.path.join(path, item)):
name_short = ""
try:
with open(os.path.join(path, item, "config.toml")) as conffile:
config = toml.load(conffile)
name_short = config["name_short"]
confs[name_short] = config
except IOError:
continue
try:
with open(os.path.join(path, item, "gui_config.toml")) as conffile:
config = toml.load(conffile)
gui_confs[name_short] = config
except IOError:
print("Failed to open GUI conf file.")
continue
return confs, gui_confs