Commit 37f30ece519bd7ad264f896ab97031f5e1cb0910

Authored by Mumfrey
1 parent c2d5a299

Add support class-wide setting prefix in CloudConfig

src/client/java/com/mumfrey/liteloader/modconfig/CloudConfig.java
... ... @@ -20,6 +20,7 @@ import com.mumfrey.liteloader.Tickable;
20 20 import com.mumfrey.liteloader.core.LiteLoader;
21 21 import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
22 22 import com.mumfrey.webprefs.WebPreferencesManager;
  23 +import com.mumfrey.webprefs.exceptions.InvalidKeyException;
23 24 import com.mumfrey.webprefs.interfaces.IWebPreferences;
24 25  
25 26 import net.minecraft.client.Minecraft;
... ... @@ -66,9 +67,14 @@ public abstract class CloudConfig
66 67 private static final int POLL_RESET_INTERVAL = 60 * CloudConfig.TICKS_PER_SECOND;
67 68  
68 69 /**
  70 + * Pattern for validating keys
  71 + */
  72 + protected static final Pattern keyPattern = Pattern.compile("^[a-z0-9_\\-\\.]{1,32}$");
  73 +
  74 + /**
69 75 * Same as normal key pattern except that $ gets translated to . for key
70 76 */
71   - private static final Pattern keyPattern = Pattern.compile("(?i)^[a-z0-9_\\-\\$]{1,32}$");
  77 + protected static final Pattern fieldKeyPattern = Pattern.compile("(?i)^[a-z0-9_\\-\\$]{1,32}$");
72 78  
73 79 /**
74 80 * A field value tracker
... ... @@ -86,7 +92,24 @@ public abstract class CloudConfig
86 92 TrackedField(Field handle)
87 93 {
88 94 this.handle = handle;
89   - this.name = handle.getName().replace("$", ".").toLowerCase();
  95 + this.name = this.getName(handle);
  96 + }
  97 +
  98 + private String getName(Field handle)
  99 + {
  100 + String name = handle.getName().replace("$", ".").toLowerCase();
  101 + String prefix = CloudConfig.this.getPrefix();
  102 + if (prefix == null)
  103 + {
  104 + return name;
  105 + }
  106 +
  107 + String key = prefix + name;
  108 + if (!CloudConfig.keyPattern.matcher(key).matches())
  109 + {
  110 + throw new InvalidKeyException("[" + key + "] is not a valid key PREFIX=" + prefix + " NAME=" + name);
  111 + }
  112 + return key;
90 113 }
91 114  
92 115 @SuppressWarnings("unchecked")
... ... @@ -492,7 +515,7 @@ public abstract class CloudConfig
492 515 continue;
493 516 }
494 517  
495   - if (!CloudConfig.keyPattern.matcher(field.getName()).matches())
  518 + if (!CloudConfig.fieldKeyPattern.matcher(field.getName()).matches())
496 519 {
497 520 LiteLoaderLogger.warning("Skipping field with invalid name %s in %s", field.getName(), this.getClass().getName());
498 521 continue;
... ... @@ -587,4 +610,15 @@ public abstract class CloudConfig
587 610 protected void onUpdated()
588 611 {
589 612 }
  613 +
  614 + /**
  615 + * Stub for subclasses, used to provide a prefix for all field names in this
  616 + * class.
  617 + *
  618 + * @return
  619 + */
  620 + protected String getPrefix()
  621 + {
  622 + return null;
  623 + }
590 624 }
... ...