@@ -204,32 +204,46 @@ def __call__(self) -> float:
204204
205205
206206class UserSettingsLoader :
207+ version_name_label_show : bool = False
208+ theme_name : str = "darkly"
209+ ping_before_scan : bool = True
210+ global_font : str = "微软雅黑"
211+ max_thread_number : int = 256
212+ MOTD_use_unifont : bool = True
213+ accumulation_player : bool = True
214+ allow_hor_resize : bool = True
215+ use_proxy : bool = False
216+ proxy_kind : str = "sock5"
217+ proxy_host : str = "127.0.0.1"
218+ proxy_port : int = 1080
219+
207220 def __init__ (self ):
208221 self .config_path = path_join (config_dir , "user_configs.json" )
209- self .configs : Dict = self .defaults ()
222+ self .configs : dict [ str , Any ] = self .find_default_config ()
210223 if not exists (self .config_path ):
211224 return
212225 with open (self .config_path , "r" , encoding = "utf-8" ) as f :
213- json_configs : Dict = json .load (f )
226+ json_configs : dict [ str , Any ] = json .load (f )
214227 if self .configs .keys () != json_configs .keys ():
215228 warnings .warn (f"可能存在潜在的设置信息不兼容问题. 以下是 user_configs.json 中的内容: { json_configs } " )
216229 self .configs = {** self .configs , ** json_configs } # 酷炫就完事了 实际含义是拼接两个字典 并且有重复时遵从后者
230+ for name , value in json_configs .items ():
231+ if name not in self .configs :
232+ print (f"未知的设置项: { name } -> { value } " )
233+ continue
234+ setattr (self , name , value )
235+
236+ def set_value (self , key : str , value : Any ):
237+ setattr (self , key , value )
238+ self .configs [key ] = value
239+
240+ def find_default_config (self ):
241+ config_dict : dict [str , Any ] = {}
242+ for name in self .__annotations__ .keys ():
243+ value = getattr (self , name )
244+ config_dict [name ] = value
245+ return config_dict
217246
218- @staticmethod
219- def defaults () -> Dict :
220- return {'if_version_name_shown_as_label' : False ,
221- 'theme_name' : "darkly" ,
222- 'ping_before_scan' : True ,
223- "global_font" : "微软雅黑" ,
224- "max_thread_number" : 256 ,
225- "MOTD_use_unicode_font" : True ,
226- "players_all" : False ,
227- "allow_hor_resize" : False ,
228- "use_proxy" : False ,
229- "proxy_kind" : "sock5" ,
230- "proxy_host" : "127.0.0.1" ,
231- "proxy_port" : 11451
232- }
233247
234248
235249class UserSettingsSaver :
@@ -243,6 +257,6 @@ def save_user_configs(loader: UserSettingsLoader):
243257UserAddressOperator .read_config_file_list ()
244258
245259scale_rater = ScaleRater ()
246- user_settings_loader = UserSettingsLoader ()
260+ config : UserSettingsLoader = UserSettingsLoader ()
247261
248262Sty = StyOP (None )
0 commit comments