-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconfig.py
More file actions
134 lines (105 loc) · 4.04 KB
/
config.py
File metadata and controls
134 lines (105 loc) · 4.04 KB
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
import os
import json
from typing import List
from helpers import format_filename
import utils.config_utils as CU
from modules.combat import Loadout, CustomWeapon
CONFIG_FILENAME = format_filename("config.json")
STREAMER_LAYOUT_DEFAULT = {'layout': [
[
['{}%', 'PERCENTAGE_RETURN', 'font-size: 20pt;']
],
[
['Total Loots: {}', 'TOTAL_LOOTS'],
['Total Spend: {} PED', 'TOTAL_SPEND'],
['Total Return: {} PED', 'TOTAL_RETURN']
]], 'style': 'font-size: 12pt;'}
class Config(object):
# Version
version = CU.ConfigValue(3)
# Core Configuration
location = CU.ConfigSecret("")
name = CU.ConfigValue("")
theme = CU.ConfigValue("dark")
# Screenshot Configuration
screenshot_directory = CU.ConfigValue("~/Documents/Globals/")
screenshot_delay = CU.ConfigValue(500)
screenshot_threshold = CU.ConfigValue(0)
screenshot_enabled = CU.ConfigValue(True)
# Combat Configuration
loadouts: List[Loadout] = CU.ConfigValue([], type=Loadout)
selected_loadout: Loadout = CU.ConfigValue(None, type=Loadout)
custom_weapons: List[CustomWeapon] = CU.ConfigValue(None)
# Streaming and Twitch
streamer_layout = CU.JsonConfigValue(STREAMER_LAYOUT_DEFAULT)
twitch_prefix = CU.ConfigValue("!")
twitch_token = CU.ConfigValue("oauth:")
twitch_username = CU.ConfigValue("NannyBot")
twitch_channel = CU.ConfigValue("")
twitch_commands_enabled = CU.ConfigValue(None)
def __init__(self):
# Initialize mutable options
self.initialized = False
self.loadouts = []
self.custom_weapons = []
self.twitch_commands_enabled = ["commands", "allreturns", "toploots", "info"]
self.load_config()
self.print()
self.initialized = True
def load_config(self):
if not os.path.exists(CONFIG_FILENAME):
return
try:
with open(CONFIG_FILENAME, 'r') as f:
CONFIG = json.loads(f.read())
except:
config_contents = ""
print("Emtpy Config")
return
if CONFIG.get("version", 1) < self.version.value:
fn_name = "version_{}_to_{}".format(CONFIG.get("version", 1), self.version.value)
CONFIG = getattr(CU, fn_name)(CONFIG)
for item, value in CONFIG.items():
if item == "loadouts":
loadouts = []
for data in value:
if isinstance(data, list):
loadouts.append(Loadout(**dict(zip(Loadout.FIELDS, data))))
else:
loadouts.append(Loadout(**data))
value = loadouts
elif item == "selected_loadout":
if isinstance(data, list):
value = Loadout(**dict(zip(Loadout.FIELDS, data)))
else:
value = Loadout(**value)
setattr(self, item, value)
def dump(self) -> dict:
p = {}
for attr_name in dir(self):
attr = getattr(self, attr_name)
if attr_name == "loadouts":
p[attr_name] = [loadout.dump() for loadout in attr.value]
elif attr_name == "selected_loadout":
p[attr_name] = attr.value.dump() if attr.value else {}
elif isinstance(attr, CU.ConfigValue):
p[attr_name] = attr.value
return p
def print(self):
print(json.dumps(self.dump(), sort_keys=True, indent=4))
def save(self):
if not self.initialized:
return
try:
to_save = json.dumps(self.dump(), indent=2, sort_keys=True)
with open(CONFIG_FILENAME, 'w') as f:
f.write(to_save)
except:
print("Error saving config!")
def __setattr__(self, item, value):
print("Setting", item, value)
if not isinstance(getattr(self, item, None), CU.ConfigValue):
return super().__setattr__(item, value)
config_item: CU.ConfigValue = getattr(self, item)
config_item._value = value
self.save()