1111import java .nio .file .Files ;
1212import java .nio .file .Path ;
1313import java .nio .file .StandardCopyOption ;
14+ import java .util .ArrayList ;
1415
1516import javax .imageio .ImageIO ;
1617
2122import de .marhali .json5 .Json5Primitive ;
2223
2324public final class OriginBlacklistConfig {
25+ private static final Json5Object DEFAULT_CONFIG = getDefaultConfig ();
26+
2427 private final Json5 json5 ;
2528 private final File file ;
2629 private final Path filePath ;
@@ -70,16 +73,14 @@ private final void reloadConfigUnsafe() throws IOException {
7073 Json5Element parsed = this .json5 .parse (text );
7174 if (parsed instanceof Json5Object ) {
7275 this .config = (Json5Object ) parsed ;
73- if (merge (this .config , getDefaultConfig ())) {
74- this .saveConfig ();
75- }
76+ merge (this .config , DEFAULT_CONFIG );
7677 } else {
7778 throw new IOException ("Config must be an object!" );
7879 }
7980 } else {
80- this .config = getDefaultConfig ();
81- this .saveConfig ();
81+ this .config = DEFAULT_CONFIG ;
8282 }
83+ this .saveConfig ();
8384 }
8485
8586 private final void reloadIconImage () {
@@ -206,7 +207,6 @@ public final boolean remove(final String key) {
206207 } else {
207208 element = null ;
208209 }
209-
210210 if (element == null ) {
211211 break ;
212212 }
@@ -312,11 +312,7 @@ private static final Json5Object getDefaultConfig() {
312312 addJSONObj (uObj , "check_timer" , Json5Primitive .fromNumber (3600 ), null );
313313 addJSONObj (uObj , "auto_update" , Json5Primitive .fromBoolean (true ), null );
314314 addJSONObj (obj , "update_checker" , uObj , null );
315- final Json5Object hObj = new Json5Object ();
316- addJSONObj (hObj , "enabled" , Json5Primitive .fromBoolean (false ), null );
317- addJSONObj (hObj , "http_port" , Json5Primitive .fromNumber (8080 ), null );
318- addJSONObj (hObj , "listen_addr" , Json5Primitive .fromString ("0.0.0.0" ), null );
319- addJSONObj (obj , "blacklist_http_share" , hObj , null );
315+ addJSONObj (obj , "blacklist_http_api" , Json5Primitive .fromBoolean (false ), null );
320316 addJSONObj (obj , "blacklist_to_whitelist" , Json5Primitive .fromBoolean (false ), null );
321317 addJSONObj (obj , "block_undefined_origin" , Json5Primitive .fromBoolean (false ), null );
322318 addJSONObj (obj , "bStats" , Json5Primitive .fromBoolean (true ), null );
@@ -325,24 +321,157 @@ private static final Json5Object getDefaultConfig() {
325321 return obj ;
326322 }
327323
328- private static final boolean merge (final Json5Object a , final Json5Object b ) {
324+ private static final boolean merge (final Json5Object aObj , final Json5Object bObj ) {
325+ final Json5Object ret = new Json5Object ();
329326 boolean changed = false ;
330-
331- for (String key : b .keySet ()) {
332- Json5Element element = b .get (key );
333- if (!a .has (key )) {
334- a .add (key , element .deepCopy ());
335- changed = true ;
336- } else {
337- final Json5Element _element = a .get (key );
338- if (_element instanceof Json5Object objA && element instanceof Json5Object objB ) {
339- if (merge (objA , objB )) {
327+ for (final String key : bObj .keySet ()) {
328+ final Json5Element dv = bObj .get (key );
329+ if (aObj .has (key )) {
330+ final Json5Element v = aObj .get (key );
331+ if (dv instanceof Json5Object ) {
332+ if (v instanceof Json5Object ) {
333+ final boolean c = merge ((Json5Object ) v , (Json5Object ) dv );
334+ ret .add (key , (Json5Object ) v );
335+ if (c ) {
336+ changed = true ;
337+ }
338+ } else {
339+ ret .add (key , dv .deepCopy ());
340+ changed = true ;
341+ }
342+ } else if (dv instanceof Json5Array ) {
343+ if (v instanceof Json5Array ) {
344+ final Json5Array vArr = (Json5Array ) v ;
345+ final Json5Array dArr = (Json5Array ) dv ;
346+ if (dArr .size () == 0 ) {
347+ ret .add (key , vArr .deepCopy ());
348+ } else {
349+ final Json5Element d0 = dArr .get (0 );
350+ if (d0 instanceof Json5Primitive && ((Json5Primitive ) d0 ).isString ()) {
351+ final Json5Array out = new Json5Array ();
352+ for (final Json5Element e : vArr ) {
353+ if (e instanceof Json5Primitive && ((Json5Primitive ) e ).isString ()) {
354+ out .add (e .deepCopy ());
355+ }
356+ }
357+ if (out .size () > 0 ) {
358+ if (out .size () != vArr .size ()) {
359+ changed = true ;
360+ }
361+ ret .add (key , out );
362+ } else {
363+ ret .add (key , dArr .deepCopy ());
364+ changed = true ;
365+ }
366+ } else {
367+ boolean a = true ;
368+ for (final Json5Element e : vArr ) {
369+ if (e == null ) {
370+ a = false ;
371+ } else if (d0 instanceof Json5Object ) {
372+ if (!(e instanceof Json5Object )) {
373+ a = false ;
374+ }
375+ } else if (d0 instanceof Json5Array ) {
376+ if (!(e instanceof Json5Array )) {
377+ a = false ;
378+ }
379+ } else if (d0 instanceof Json5Primitive && e instanceof Json5Primitive ) {
380+ final Json5Primitive bp = (Json5Primitive ) d0 ;
381+ final Json5Primitive ap = (Json5Primitive ) e ;
382+ if (bp .isBoolean ()) {
383+ if (!ap .isBoolean ()) {
384+ a = false ;
385+ }
386+ } else if (bp .isNumber ()) {
387+ if (!ap .isNumber ()) {
388+ a = false ;
389+ }
390+ } else if (bp .isString ()) {
391+ if (!ap .isString ()) {
392+ a = false ;
393+ }
394+ }
395+ } else {
396+ a = false ;
397+ }
398+ if (!a ) {
399+ break ;
400+ }
401+ }
402+ if (a ) {
403+ ret .add (key , vArr .deepCopy ());
404+ } else {
405+ ret .add (key , dArr .deepCopy ());
406+ changed = true ;
407+ }
408+ }
409+ }
410+ } else {
411+ ret .add (key , dv .deepCopy ());
412+ changed = true ;
413+ }
414+ } else if (dv instanceof Json5Primitive ) {
415+ if (v instanceof Json5Primitive ) {
416+ final Json5Primitive dp = (Json5Primitive ) dv ;
417+ final Json5Primitive vp = (Json5Primitive ) v ;
418+ if (dp .isBoolean ()) {
419+ if (vp .isBoolean ()) {
420+ ret .add (key , vp .deepCopy ());
421+ } else {
422+ ret .add (key , dv .deepCopy ());
423+ changed = true ;
424+ }
425+ } else if (dp .isNumber ()) {
426+ if (vp .isNumber ()) {
427+ ret .add (key , vp .deepCopy ());
428+ } else {
429+ ret .add (key , dv .deepCopy ());
430+ changed = true ;
431+ }
432+ } else if (dp .isString ()) {
433+ if (vp .isString ()) {
434+ ret .add (key , vp .deepCopy ());
435+ } else {
436+ ret .add (key , dv .deepCopy ());
437+ changed = true ;
438+ }
439+ } else {
440+ ret .add (key , dv .deepCopy ());
441+ changed = true ;
442+ }
443+ } else {
444+ ret .add (key , dv .deepCopy ());
340445 changed = true ;
341446 }
447+ } else {
448+ ret .add (key , dv .deepCopy ());
449+ changed = true ;
342450 }
451+ } else {
452+ ret .add (key , dv .deepCopy ());
453+ changed = true ;
343454 }
344455 }
345-
456+ for (final String key : aObj .keySet ()) {
457+ if (!bObj .has (key )) {
458+ ret .add (key , aObj .get (key ).deepCopy ());
459+ }
460+ }
461+ for (final String key : aObj .keySet ()) {
462+ if (!bObj .has (key )) {
463+ ret .add (key , aObj .get (key ).deepCopy ());
464+ }
465+ }
466+ for (final String k : new ArrayList <>(aObj .keySet ())) {
467+ aObj .remove (k );
468+ }
469+ for (final String k : ret .keySet ()) {
470+ aObj .add (k , ret .get (k ));
471+ }
472+ for (final String key : ret .keySet ()) {
473+ aObj .add (key , ret .get (key ));
474+ }
346475 return changed ;
347476 }
348477
0 commit comments