-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig_class.py
150 lines (120 loc) · 4.88 KB
/
config_class.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
from cached_property import threaded_cached_property
from collections import OrderedDict
import json
from nestris_ocr.utils.sub_image import spawn_subimage
# fmt: off
CONFIG_DEFAULTS = {
"player.name": "",
"player.twitch_url": "",
"calibrator.ui": "SIMPLE",
"performance.support_hex_score": True,
"performance.support_hex_level": True,
"performance.scan_rate": 30,
"stats.enabled": False,
"stats.capture_method": "FIELD",
# related to getting the base image for ocr
"capture.method": "WINDOW",
"capture.source_id": "OBS",
"capture.source_extra_data" : "",
"capture.deinterlace_method": "DISCARD_BOTTOM",
"capture.deinterlace_res": "FULL",
"calibration.palette": "DEFAULT",
"calibration.flash_method": "NONE",
# rgb limit of flash. Value above threshold is considered flash. Only used in BACKGROUND method
"calibration.flash_threshold": 150,
"calibration.game_coords": [0, 0, 1500, 1500],
"calibration.pct.score": [0.751, 0.252, 0.185, 0.030],
"calibration.pct.lines": [0.595, 0.074, 0.088, 0.030],
"calibration.pct.level": [0.814, 0.717, 0.057, 0.030],
"calibration.pct.stats": [0.187, 0.392, 0.091, 0.459],
"calibration.pct.flash": [0.749, 0.370, 0.07, 0.012],
# if capture_field, 2 primary colors will be needed
"calibration.capture_field": True,
"calibration.pct.field": [0.376, 0.175, 0.311, 0.72],
"calibration.dynamic_colors": True,
"calibration.pct.color1": [0.151, 0.48, 0.018, 0.018],
"calibration.pct.color2": [0.151, 0.554, 0.018, 0.018],
"calibration.dynamic_black_n_white": True,
"calibration.color_interpolation": True,
"calibration.pct.black_n_white": [0.827, 0.431, 0.028, 0.02],
"calibration.capture_preview": True,
"calibration.pct.preview": [0.753, 0.5, 0.12, 0.064],
"calibration.capture_das": False,
"calibration.pct.das.current_piece": [0.060, 0.191, 0.09, 0.059],
"calibration.pct.das.current_piece_das": [0.216, 0.211, 0.061, 0.034],
"calibration.pct.das.instant_das": [0.154, 0.134, 0.061, 0.034],
"network.host": "127.0.0.1",
"network.port": 3338,
"network.protocol": "LEGACY",
"debug.print_packet": True,
"debug.print_benchmark": False
}
# fmt: on
CONFIG_CHOICES = {
"calibrator.ui": {"SIMPLE", "ADVANCED"},
"stats.capture_method": {"FIELD", "TEXT"},
"capture.method": {"WINDOW", "OPENCV", "FILE"},
"calibration.flash_method": {"FIELD", "BACKGROUND", "NONE"},
"capture.deinterlace_method": {
"NONE",
"DISCARD_TOP",
"DISCARD_BOTTOM",
"TOP_FIRST",
"BOTTOM_FIRST",
},
"capture.deinterlace_res": {"FULL", "HALF"},
"network.protocol": {"LEGACY", "FILE", "AUTOBAHN", "AUTOBAHN_V2"},
}
class Config:
def __init__(self, path, auto_save=True, default_config=None):
self.path = path
self.auto_save = auto_save
self.default_config = default_config
try:
with open(path, "r") as file:
self.data = json.load(file, object_pairs_hook=OrderedDict)
except Exception:
self.data = self.load_defaultconfig()
self.save()
def load_defaultconfig(self):
result = None
if self.default_config is not None:
try:
with open(self.default_config, "r") as file:
result = json.load(file, object_pairs_hook=OrderedDict)
except Exception:
result = None
if result is None:
result = OrderedDict(CONFIG_DEFAULTS)
return result
def __getitem__(self, key):
if key not in CONFIG_DEFAULTS:
raise KeyError("Invalid key %s" % (key,))
return self.data.get(key, CONFIG_DEFAULTS[key])
def __setitem__(self, key, value):
if key not in CONFIG_DEFAULTS:
raise KeyError("Invalid key")
if key in CONFIG_CHOICES:
if value not in CONFIG_CHOICES[key]:
raise ValueError("Invalid value. Not allowed by CONFIG_CHOICES")
# fmt: off
default = CONFIG_DEFAULTS[key]
if (
isinstance(default, int) and not isinstance(value, int)
or isinstance(default, float) and not isinstance(value, float)
or isinstance(default, str) and not isinstance(value, str)
or isinstance(default, list) and not (isinstance(value, list) or isinstance(value, tuple))
):
raise TypeError("Invalid type. Check CONFIG_DEFAULTS for the type to use")
# fmt: on
self.data[key] = value
if key == "calibration.pct.field":
self.__dict__.pop("stats2_percentages", None)
if self.auto_save:
self.save()
def save(self):
with open(self.path, "w") as file:
json.dump(self.data, file, indent=2)
@threaded_cached_property
def stats2_percentages(self):
return spawn_subimage(self["calibration.pct.field"])