@@ -84,10 +84,7 @@ def __init__(self, settings_dict: dict[str, Any], strict: bool = False) -> None:
84
84
del settings_dict ['compress_rom' ]
85
85
if strict :
86
86
validate_settings (settings_dict )
87
- self .settings_dict .update (settings_dict )
88
- for info in self .setting_infos .values ():
89
- if info .name not in self .settings_dict :
90
- self .settings_dict [info .name ] = info .default
87
+ self .update (settings_dict , initialize = True )
91
88
92
89
if self .world_count < 1 :
93
90
self .world_count = 1
@@ -105,6 +102,14 @@ def copy(self) -> Settings:
105
102
settings .settings_dict = copy .deepcopy (settings .settings_dict )
106
103
return settings
107
104
105
+ def update (self , settings_dict : dict [str , Any ], * , initialize : bool = False ) -> None :
106
+ for info in self .setting_infos .values ():
107
+ if info .type is type (None ):
108
+ continue
109
+ if not initialize and info .name not in settings_dict :
110
+ continue
111
+ setattr (self , info .name , settings_dict [info .name ] if info .name in settings_dict else info .default )
112
+
108
113
def get_settings_display (self ) -> str :
109
114
padding = 0
110
115
for setting in filter (lambda s : s .shared , self .setting_infos .values ()):
@@ -114,16 +119,16 @@ def get_settings_display(self) -> str:
114
119
for setting in filter (lambda s : s .shared , self .setting_infos .values ()):
115
120
name = setting .name + ': ' + ' ' * (padding - len (setting .name ))
116
121
if setting .type == list :
117
- val = ('\n ' + (' ' * (padding + 2 ))).join (self . settings_dict [ setting .name ] )
122
+ val = ('\n ' + (' ' * (padding + 2 ))).join (getattr ( self , setting .name ) )
118
123
else :
119
- val = str (self . settings_dict [ setting .name ] )
124
+ val = str (getattr ( self , setting .name ) )
120
125
output += name + val + '\n '
121
126
return output
122
127
123
128
def get_settings_string (self ) -> str :
124
129
bits = []
125
130
for setting in filter (lambda s : s .shared and s .bitwidth > 0 , self .setting_infos .values ()):
126
- value = self . settings_dict [ setting .name ]
131
+ value = getattr ( self , setting .name )
127
132
i_bits = []
128
133
if setting .name in LEGACY_STARTING_ITEM_SETTINGS :
129
134
items = LEGACY_STARTING_ITEM_SETTINGS [setting .name ]
@@ -223,9 +228,9 @@ def update_with_settings_string(self, text: str) -> None:
223
228
else :
224
229
raise TypeError (f'Cannot decode type { setting .type } from settings string' )
225
230
226
- self . settings_dict [ setting .name ] = value
231
+ setattr ( self , setting .name , value )
227
232
228
- self . settings_dict [ 'starting_items' ] = {} # Settings string contains the GUI format, so clear the current value of the dict format.
233
+ setattr ( self , 'starting_items' , {}) # Settings string contains the GUI format, so clear the current value of the dict format.
229
234
self .distribution .reset () # convert starting_items
230
235
self .settings_string = self .get_settings_string ()
231
236
self .numeric_seed = self .get_numeric_seed ()
@@ -249,10 +254,6 @@ def update_seed(self, seed: str) -> None:
249
254
self .sanitize_seed ()
250
255
self .numeric_seed = self .get_numeric_seed ()
251
256
252
- def update (self ) -> None :
253
- self .settings_string = self .get_settings_string ()
254
- self .numeric_seed = self .get_numeric_seed ()
255
-
256
257
def load_distribution (self ) -> None :
257
258
if self .enable_distribution_file :
258
259
if self .distribution_file :
@@ -286,7 +287,7 @@ def check_dependency(self, setting_name: str, check_random: bool = True) -> bool
286
287
def get_dependency (self , setting_name : str , check_random : bool = True ) -> Any :
287
288
info = SettingInfos .setting_infos [setting_name ]
288
289
not_in_dist = '_settings' not in self .distribution .src_dict or info .name not in self .distribution .src_dict ['_settings' ].keys ()
289
- if check_random and 'randomize_key' in info .gui_params and self . settings_dict [ info .gui_params ['randomize_key' ]] and not_in_dist :
290
+ if check_random and 'randomize_key' in info .gui_params and getattr ( self , info .gui_params ['randomize_key' ]) and not_in_dist :
290
291
return info .disabled_default
291
292
elif info .dependency is not None :
292
293
return info .disabled_default if info .dependency (self ) and not_in_dist else None
@@ -298,7 +299,7 @@ def remove_disabled(self) -> None:
298
299
if info .dependency is not None :
299
300
new_value = self .get_dependency (info .name )
300
301
if new_value is not None :
301
- self . settings_dict [ info .name ] = new_value
302
+ setattr ( self , info .name , new_value )
302
303
self ._disabled .add (info .name )
303
304
304
305
self .settings_string = self .get_settings_string ()
@@ -327,46 +328,47 @@ def resolve_random_settings(self, cosmetic: bool, randomize_key: Optional[str] =
327
328
# Make sure the setting is meant to be randomized and not specified in distribution
328
329
# We that check it's not specified in the distribution so that plando can override randomized settings
329
330
not_in_dist = '_settings' not in self .distribution .src_dict or info .name not in self .distribution .src_dict ['_settings' ].keys ()
330
- if self . settings_dict [ info .gui_params ['randomize_key' ]] and not_in_dist :
331
+ if getattr ( self , info .gui_params ['randomize_key' ]) and not_in_dist :
331
332
randomize_keys_enabled .add (info .gui_params ['randomize_key' ])
332
333
choices , weights = zip (* info .gui_params ['distribution' ])
333
- self . settings_dict [ info .name ] = random .choices (choices , weights = weights )[0 ]
334
+ setattr ( self , info .name , random .choices (choices , weights = weights )[0 ])
334
335
335
336
# Second pass to make sure disabled settings are set properly.
336
337
# Stupid hack: disable randomize keys, then re-enable.
337
338
for randomize_keys in randomize_keys_enabled :
338
- self . settings_dict [ randomize_keys ] = False
339
+ setattr ( self , randomize_keys , False )
339
340
for info in sorted_infos :
340
341
if cosmetic == info .shared :
341
342
continue
342
343
dependency = self .get_dependency (info .name , check_random = False )
343
344
if dependency is None :
344
345
continue
345
- self . settings_dict [ info .name ] = dependency
346
+ setattr ( self , info .name , dependency )
346
347
for randomize_keys in randomize_keys_enabled :
347
- self . settings_dict [ randomize_keys ] = True
348
+ setattr ( self , randomize_keys , True )
348
349
349
350
def to_json (self , * , legacy_starting_items : bool = False ) -> dict [str , Any ]:
350
351
if legacy_starting_items :
351
352
settings = self .copy ()
352
353
for setting_name , items in LEGACY_STARTING_ITEM_SETTINGS .items ():
353
- settings .settings_dict [setting_name ] = []
354
+ starting_items = []
355
+ setattr (settings , setting_name , starting_items )
354
356
for entry in items .values ():
355
357
if entry .item_name in self .starting_items :
356
358
count = self .starting_items [entry .item_name ]
357
359
if not isinstance (count , int ):
358
360
count = count .count
359
361
if count > entry .i :
360
- settings . settings_dict [ setting_name ] .append (entry .setting_name )
362
+ starting_items .append (entry .setting_name )
361
363
else :
362
364
settings = self
363
365
return { # TODO: This should be done in a way that is less insane than a double-digit line dictionary comprehension.
364
366
setting .name : (
365
367
{name : (
366
368
{name : record .to_json () for name , record in record .items ()} if isinstance (record , dict ) else record .to_json ()
367
- ) for name , record in settings . settings_dict [ setting .name ] .items ()}
369
+ ) for name , record in getattr ( settings , setting .name ) .items ()}
368
370
if setting .name == 'starting_items' and not legacy_starting_items else
369
- settings . settings_dict [ setting .name ]
371
+ getattr ( settings , setting .name )
370
372
)
371
373
for setting in self .setting_infos .values ()
372
374
if setting .shared and (
@@ -380,7 +382,7 @@ def to_json(self, *, legacy_starting_items: bool = False) -> dict[str, Any]:
380
382
}
381
383
382
384
def to_json_cosmetics (self ) -> dict [str , Any ]:
383
- return {setting .name : self . settings_dict [ setting .name ] for setting in self .setting_infos .values () if setting .cosmetic }
385
+ return {setting .name : getattr ( self , setting .name ) for setting in self .setting_infos .values () if setting .cosmetic }
384
386
385
387
386
388
# gets the randomizer settings, whether to open the gui, and the logger level from command line arguments
0 commit comments