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