1- use std:: fmt:: Debug ;
2- use serde:: { Serialize , Deserialize } ;
1+ use crate :: log:: print_flush;
2+ use crate :: log:: { print_log, LogType } ;
3+ use once_cell:: sync:: Lazy ;
4+ use serde:: { Deserialize , Serialize } ;
35use serde_json:: Value ;
6+ use std:: fmt:: Debug ;
47use std:: fs:: File ;
5- use std:: { fs } ;
6- use std:: io:: { self , ErrorKind , Read , Write } ;
8+ use std:: io:: { self , ErrorKind , Read , Write } ;
79use std:: sync:: Mutex ;
8- use once_cell:: sync:: Lazy ;
9- use crate :: log:: { print_log, LogType } ;
10- use crate :: log:: print_flush;
10+ use std:: { fs, vec} ;
1111
1212#[ derive( Serialize , Deserialize , Debug , Clone ) ]
1313pub struct Config {
@@ -21,7 +21,6 @@ pub struct Config {
2121 pub send_all_value_every_time : bool ,
2222 pub check_rate_ms : u64 ,
2323 pub restrict_send_rate : bool ,
24- pub update_handle_addresses : Vec < String > ,
2524 pub config_status : String ,
2625}
2726
@@ -52,13 +51,12 @@ impl Default for Config {
5251 send_all_value_every_time : false ,
5352 check_rate_ms : 1 ,
5453 restrict_send_rate : true ,
55- update_handle_addresses : vec ! [ "/avatar/parameters/osc_clock@ForceSync" . to_string( ) ] ,
5654 config_status : format ! ( "{:?}" , ConfigStatus :: Fallback ) ,
5755 }
5856 }
5957}
6058
61- pub static CONFIG : Lazy < Mutex < Config > > = Lazy :: new ( || { Mutex :: new ( load_config ( ) ) } ) ;
59+ pub static CONFIG : Lazy < Mutex < Config > > = Lazy :: new ( || Mutex :: new ( load_config ( ) ) ) ;
6260
6361pub fn init_config ( ) {
6462 Lazy :: force ( & CONFIG ) ;
@@ -129,7 +127,7 @@ pub fn read_config_json(json_path: &str, complement: bool) -> Result<Config, io:
129127fn check_itgr (
130128 partial : & serde_json:: Value ,
131129 default : & serde_json:: Value ,
132- exceptions : & [ & str ]
130+ exceptions : & [ & str ] ,
133131) -> bool {
134132 let mut count = 0 ;
135133 if let ( Value :: Object ( partial_map) , Value :: Object ( default_map) ) = ( partial, default) {
@@ -138,7 +136,10 @@ fn check_itgr(
138136 continue ;
139137 }
140138 if !partial_map. contains_key ( k) {
141- print_flush ( print_log ( format ! ( "Lacked property found: {}" , k) , LogType :: WARN ) ) ;
139+ print_flush ( print_log (
140+ format ! ( "Lacked property found: {}" , k) ,
141+ LogType :: WARN ,
142+ ) ) ;
142143 count += 1 ;
143144 }
144145 }
@@ -167,22 +168,37 @@ pub fn repair_config_json(force: bool) -> Result<bool, io::Error> {
167168 print_flush ( print_log ( format ! ( "Config file found" ) , LogType :: INFO ) ) ;
168169 }
169170 Err ( _error) => {
170- print_flush ( print_log ( format ! ( "Failed to load config file" ) , LogType :: WARN ) ) ;
171+ print_flush ( print_log (
172+ format ! ( "Failed to load config file" ) ,
173+ LogType :: WARN ,
174+ ) ) ;
171175 }
172176 }
173177 fs:: remove_file ( path) ?;
174178 }
175179 file = File :: create ( "./config.json" ) ?;
176- let json = serde_json:: to_string_pretty ( & config) ?;
180+ let json =
181+ serde_json:: to_string_pretty ( & validate ( config, vec ! [ "config_status" . to_string( ) ] ) ) ?;
177182
178- file. write_all ( json. as_bytes ( ) ) . expect (
179- & print_log ( "Failed to write to file" . to_string ( ) , LogType :: ERROR )
180- ) ;
183+ file. write_all ( json. as_bytes ( ) ) . expect ( & print_log (
184+ "Failed to write to file" . to_string ( ) ,
185+ LogType :: ERROR ,
186+ ) ) ;
181187 }
182188
183189 Ok ( true )
184190}
185191
192+ fn validate ( config : Config , exclusions : Vec < String > ) -> serde_json:: Value {
193+ let mut value = serde_json:: to_value ( config) . unwrap ( ) ;
194+ if let serde_json:: Value :: Object ( ref mut map) = value {
195+ for key in exclusions {
196+ map. remove ( & key) ;
197+ }
198+ }
199+ value
200+ }
201+
186202fn load_config ( ) -> Config {
187203 let config;
188204 match read_config_json ( "./config.json" , true ) {
@@ -216,34 +232,58 @@ fn load_config() -> Config {
216232 print_flush ( print_log ( "Config file created" . to_string ( ) , LogType :: INFO ) ) ;
217233 } else {
218234 config = get_fallback_config ( ) ;
219- print_flush ( print_log ( "Using fallback config" . to_string ( ) , LogType :: WARN ) ) ;
235+ print_flush ( print_log (
236+ "Using fallback config" . to_string ( ) ,
237+ LogType :: WARN ,
238+ ) ) ;
220239 }
221240 }
222241 _ => {
223- print_flush ( print_log ( t ! ( "failed_to_load_config" ) . to_string ( ) , LogType :: WARN ) ) ;
224- print_flush ( print_log ( t ! ( "how_to_repair_config" ) . to_string ( ) , LogType :: INFO ) ) ;
242+ print_flush ( print_log (
243+ t ! ( "failed_to_load_config" ) . to_string ( ) ,
244+ LogType :: WARN ,
245+ ) ) ;
246+ print_flush ( print_log (
247+ t ! ( "how_to_repair_config" ) . to_string ( ) ,
248+ LogType :: INFO ,
249+ ) ) ;
225250 config = get_fallback_config ( ) ;
226251 }
227252 }
228253 }
229254 }
230255
231256 if config. send_all_value_every_time {
232- print_flush ( print_log ( t ! ( "warning_send_all_value" ) . to_string ( ) , LogType :: WARN ) ) ;
257+ print_flush ( print_log (
258+ t ! ( "warning_send_all_value" ) . to_string ( ) ,
259+ LogType :: WARN ,
260+ ) ) ;
233261 }
234262
235263 if !config. restrict_send_rate {
236- print_flush ( print_log ( t ! ( "warning_restrict_send_rate" ) . to_string ( ) , LogType :: WARN ) ) ;
264+ print_flush ( print_log (
265+ t ! ( "warning_restrict_send_rate" ) . to_string ( ) ,
266+ LogType :: WARN ,
267+ ) ) ;
237268 }
238269
239270 if config. check_rate_ms == 0 {
240- print_flush ( print_log ( t ! ( "warning_check_rate_ms_zero" ) . to_string ( ) , LogType :: WARN ) ) ;
271+ print_flush ( print_log (
272+ t ! ( "warning_check_rate_ms_zero" ) . to_string ( ) ,
273+ LogType :: WARN ,
274+ ) ) ;
241275 } else if config. check_rate_ms > 100 {
242- print_flush ( print_log ( t ! ( "warning_check_rate_ms_too_much" ) . to_string ( ) , LogType :: WARN ) ) ;
276+ print_flush ( print_log (
277+ t ! ( "warning_check_rate_ms_too_much" ) . to_string ( ) ,
278+ LogType :: WARN ,
279+ ) ) ;
243280 }
244281
245282 if config. use_osc_query {
246- print_flush ( print_log ( t ! ( "warning_osc_query_enabled" ) . to_string ( ) , LogType :: INFO ) ) ;
283+ print_flush ( print_log (
284+ t ! ( "warning_osc_query_enabled" ) . to_string ( ) ,
285+ LogType :: INFO ,
286+ ) ) ;
247287 }
248288
249289 return config;
0 commit comments