Default settings support (#428)

This commit is contained in:
Druss99
2024-12-18 07:47:05 -05:00
committed by GitHub
parent 8f384b35dc
commit 266e69a6ce
4 changed files with 54 additions and 16 deletions

View File

@@ -1339,27 +1339,31 @@ class Settings:
self.plugins[self.plugin_settings_key(identifier)] = value
def __setstate__(self, state: dict[str, Any]) -> None:
# restore Enum & timedelta types
for key, value in state.items():
if isinstance(self.__dict__.get(key), timedelta) and isinstance(value, int):
state[key] = timedelta(minutes=value)
elif isinstance(self.__dict__.get(key), Enum) and isinstance(value, str):
state[key] = eval(value)
elif isinstance(value, dict):
state[key] = self.obj_hook(value)
# __setstate__ is called with the dict of the object being unpickled. We
# can provide save compatibility for new settings options (which
# normally would not be present in the unpickled object) by creating a
# new settings object, updating it with the unpickled state, and
# updating our dict with that.
new_state = Settings().__dict__
new_state.update(state)
new_state.update(self.deserialize_state_dict(state))
self.__dict__.update(new_state)
from game.plugins import LuaPluginManager
LuaPluginManager().load_settings(self)
@staticmethod
def deserialize_state_dict(state: dict[str, Any]) -> dict[str, Any]:
# restore Enum & timedelta types
s = Settings()
for key, value in state.items():
if isinstance(s.__dict__.get(key), timedelta) and isinstance(value, int):
state[key] = timedelta(minutes=value)
elif isinstance(s.__dict__.get(key), Enum) and isinstance(value, str):
state[key] = eval(value)
elif isinstance(value, dict):
state[key] = s.obj_hook(value)
return state
@classmethod
def _field_description(cls, settings_field: Field[Any]) -> OptionDescription:
return settings_field.metadata[SETTING_DESCRIPTION_KEY]