138 lines
2.7 KiB
Python
138 lines
2.7 KiB
Python
import unrealsdk
|
|
|
|
from ..misc import is_in_game
|
|
|
|
|
|
SETTINGS = (
|
|
'AmbientOcclusion',
|
|
'AudioFocus',
|
|
'bNVIDIA3d',
|
|
'DepthOfField',
|
|
'FramerateLocking',
|
|
'FoliageDistance',
|
|
'FXAA',
|
|
'GameDetail',
|
|
'LensFlares',
|
|
'Fullscreen',
|
|
'MaxAnisotropy',
|
|
'NumberOfDecals',
|
|
'PhysXLevel',
|
|
'TextureFade',
|
|
'TextureQuality',
|
|
'ViewDistance',
|
|
'VSync'
|
|
)
|
|
|
|
|
|
class SetItem:
|
|
'Represents a game setting.'
|
|
|
|
def __init__(self, name, value, *options):
|
|
self.name = name
|
|
self.value = value
|
|
self.options = options
|
|
|
|
|
|
def __repr__(self):
|
|
return f'SetItem("{self.name}", "{self.value_string}")'
|
|
|
|
|
|
def __str__(self):
|
|
return self.value_string
|
|
|
|
|
|
@classmethod
|
|
def new_from_option(cls, option):
|
|
return cls(
|
|
option.Name,
|
|
option.CurrValue,
|
|
*tuple(v for v in option.ValueStrings)
|
|
)
|
|
|
|
|
|
@property
|
|
def value_string(self):
|
|
return self.options[self.value]
|
|
|
|
|
|
class Settings:
|
|
'''
|
|
Class to manage game settings. Values can be accessed via dict items. Check the output of
|
|
:meth:`Settings.keys()` for valid setting names.
|
|
'''
|
|
|
|
def __init__(self):
|
|
self._set = unrealsdk.FindObject('WillowSystemSettings', 'WillowGame.Default__WillowSystemSettings')
|
|
|
|
if is_in_game():
|
|
self._set.LoadSystemSettings(False)
|
|
|
|
|
|
def __getitem__(self, key):
|
|
return self.get(key).value
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
self.set(key, value)
|
|
|
|
|
|
def __iter__(self):
|
|
for key in self.keys():
|
|
yield key
|
|
|
|
|
|
def get(self, key) -> SetItem:
|
|
'''
|
|
Get a setting with the name of the specified key
|
|
|
|
:param str key: Name of the setting to get
|
|
:raises KeyError: When the specified key does not exist
|
|
'''
|
|
for value in self._set.SystemOptions:
|
|
if key == value.Name:
|
|
return SetItem.new_from_option(value)
|
|
|
|
raise KeyError(key)
|
|
|
|
|
|
def items(self) -> 'typing.Iterator[tuple[str,int]]':
|
|
'Get all the keys and their associated values'
|
|
for key in self.keys():
|
|
yield key, self.get(key).value
|
|
|
|
|
|
def keys(self) -> 'tuple[str]':
|
|
'Valid names of all the options.'
|
|
return SETTINGS
|
|
|
|
|
|
def set(self, key, new_value):
|
|
'''
|
|
Set a value for the specified key
|
|
|
|
:param str key: Name of the setting to set
|
|
:param typing.Union[str,int] new_value: Value to set for the specified key
|
|
'''
|
|
if isinstance(new_value, str):
|
|
new_value = self.get(key).options.index(new_value)
|
|
|
|
self._set.UpdateSystemOption(key, new_value)
|
|
|
|
|
|
def to_dict(self, strings=False) -> 'dict[str,typing.Union[int,str]]':
|
|
'''
|
|
Get all the keys and their associated values as a dict
|
|
|
|
:params bool strings: If ``True``, return the value name instead of the value index
|
|
'''
|
|
if not strings:
|
|
return {k: v for k, v in self.items()}
|
|
|
|
return {k: str(self.get(k)) for k in self.keys()}
|
|
|
|
|
|
def values(self) -> 'typing.Iterator[int]':
|
|
'Get all the values of each key'
|
|
for key in self.keys():
|
|
yield self.get(key).value
|