forked from valleyofdoom/AutoGpuAffinity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
132 lines (102 loc) · 3.75 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
import logging
import os
from configparser import ConfigParser
from dataclasses import dataclass
from enum import Enum
LOG_CONFIG = logging.getLogger("CONFIG")
class Api(Enum):
LIBLAVA = 1
D3D9 = 2
@dataclass
class Settings:
cache_duration: int
benchmark_duration: int
custom_cpus: list[int]
api: Api
sync_driver_affinity: bool
skip_confirmation: bool
@dataclass
class MSIAfterburner:
profile: int
location: str
@dataclass
class Xperf:
enabled: bool
location: str
save_etls: bool
@dataclass
class Liblava:
fullscreen: bool
x_resolution: int
y_resolution: int
fps_cap: int
triple_buffering: bool
class Config:
def __init__(self, config_path: str):
if not os.path.exists(config_path):
error_msg = f"config file not found at path: {config_path}"
LOG_CONFIG.error(error_msg)
raise FileNotFoundError(error_msg)
config = ConfigParser(delimiters="=")
config.read(config_path)
apis: dict[int, Api] = {
1: Api.LIBLAVA,
2: Api.D3D9,
}
self.settings = Settings(
cache_duration=config.getint("settings", "cache_duration"),
benchmark_duration=config.getint("settings", "benchmark_duration"),
custom_cpus=Config.str_to_int_array(config.get("settings", "custom_cpus")),
api=apis[config.getint("settings", "api")],
sync_driver_affinity=config.getboolean("settings", "sync_driver_affinity"),
skip_confirmation=config.getboolean("settings", "skip_confirmation"),
)
self.msi_afterburner = MSIAfterburner(
profile=config.getint("MSI Afterburner", "profile"),
location=config.get("MSI Afterburner", "location"),
)
self.xperf = Xperf(
config.getboolean("xperf", "enabled"),
config.get("xperf", "location"),
config.getboolean("xperf", "save_etls"),
)
self.liblava = Liblava(
config.getboolean("liblava", "fullscreen"),
config.getint("liblava", "x_resolution"),
config.getint("liblava", "y_resolution"),
config.getint("liblava", "fps_cap"),
config.getboolean("liblava", "triple_buffering"),
)
def validate_config(self):
errors = 0
if self.settings.cache_duration < 0 or self.settings.benchmark_duration <= 0:
LOG_CONFIG.error("invalid durations specified")
errors += 1
if self.xperf.enabled and not os.path.exists(self.xperf.location):
LOG_CONFIG.error("invalid xperf path specified")
errors += 1
if self.msi_afterburner.profile > 0 and not os.path.exists(
self.msi_afterburner.location,
):
LOG_CONFIG.error("invalid MSI Afterburner path specified")
errors += 1
if self.settings.api not in Api:
LOG_CONFIG.error("invalid api specified")
errors += 1
return 1 if errors else 0
@staticmethod
def str_to_int_array(str_array: str) -> list[int]:
# return if empty
if str_array == "[]":
return []
# convert to list[str]
str_array_without_brackets = str_array[1:-1]
split_array = [x.strip() for x in str_array_without_brackets.split(",")]
parsed_list: list[int] = []
for item in split_array:
if ".." in item:
lower, upper = item.split("..")
parsed_list.extend(range(int(lower), int(upper) + 1))
else:
parsed_list.append(int(item))
return parsed_list