Commit 54b5ecdd8de9ec971f24165b0dfffc48723e8897
1 parent
e38f7f24
LiteLoader 1.6.4_02 - experimental - changes to Exposable implementation, regist…
…er exposables by reference, renamed annotations, added aggressive option
Showing
4 changed files
with
85 additions
and
65 deletions
java/com/mumfrey/liteloader/core/LiteLoader.java
@@ -788,29 +788,18 @@ public final class LiteLoader | @@ -788,29 +788,18 @@ public final class LiteLoader | ||
788 | } | 788 | } |
789 | 789 | ||
790 | /** | 790 | /** |
791 | - * @param modClass | ||
792 | - */ | ||
793 | - public void writeConfig(LiteMod mod) | ||
794 | - { | ||
795 | - if (mod != null) | ||
796 | - { | ||
797 | - this.writeConfig(mod.getClass()); | ||
798 | - } | ||
799 | - } | ||
800 | - | ||
801 | - /** | ||
802 | - * @param exposableClass | 791 | + * @param exposable |
803 | */ | 792 | */ |
804 | - public void writeConfig(Class<? extends Exposable> exposableClass) | 793 | + public void writeConfig(Exposable exposable) |
805 | { | 794 | { |
806 | - this.configManager.invalidateConfig(exposableClass); | 795 | + this.configManager.invalidateConfig(exposable); |
807 | } | 796 | } |
808 | 797 | ||
809 | /** | 798 | /** |
810 | * Register an arbitrary Exposable | 799 | * Register an arbitrary Exposable |
811 | * | 800 | * |
812 | * @param exposable Exposable object to register | 801 | * @param exposable Exposable object to register |
813 | - * @param fileName | 802 | + * @param fileName Override config file name to use (leave null to use value from ExposableConfig specified value) |
814 | */ | 803 | */ |
815 | public void registerExposable(Exposable exposable, String fileName) | 804 | public void registerExposable(Exposable exposable, String fileName) |
816 | { | 805 | { |
@@ -1091,7 +1080,7 @@ public final class LiteLoader | @@ -1091,7 +1080,7 @@ public final class LiteLoader | ||
1091 | } | 1080 | } |
1092 | else if (this.minecraft.currentScreen instanceof GuiMainMenu && Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && Keyboard.isKeyDown(Keyboard.KEY_TAB)) | 1081 | else if (this.minecraft.currentScreen instanceof GuiMainMenu && Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) && Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && Keyboard.isKeyDown(Keyboard.KEY_TAB)) |
1093 | { | 1082 | { |
1094 | - this.minecraft.displayGuiScreen(new GuiScreenModInfo(this.minecraft, (GuiMainMenu)this.minecraft.currentScreen, this, this.enabledModsList, this.configManager)); | 1083 | + this.displayModInfoScreen((GuiMainMenu)this.minecraft.currentScreen); |
1095 | } | 1084 | } |
1096 | } | 1085 | } |
1097 | 1086 | ||
@@ -1224,7 +1213,17 @@ public final class LiteLoader | @@ -1224,7 +1213,17 @@ public final class LiteLoader | ||
1224 | { | 1213 | { |
1225 | return this.displayModInfoScreenTab; | 1214 | return this.displayModInfoScreenTab; |
1226 | } | 1215 | } |
1227 | - | 1216 | + |
1217 | + /** | ||
1218 | + * Display the "mod info" overlay over the specified main menu GUI | ||
1219 | + * | ||
1220 | + * @param parentScreen | ||
1221 | + */ | ||
1222 | + public void displayModInfoScreen(GuiMainMenu parentScreen) | ||
1223 | + { | ||
1224 | + this.minecraft.displayGuiScreen(new GuiScreenModInfo(this.minecraft, parentScreen, this, this.enabledModsList, this.configManager)); | ||
1225 | + } | ||
1226 | + | ||
1228 | private static void logInfo(String string, Object... args) | 1227 | private static void logInfo(String string, Object... args) |
1229 | { | 1228 | { |
1230 | LiteLoader.logger.info(String.format(string, args)); | 1229 | LiteLoader.logger.info(String.format(string, args)); |
java/com/mumfrey/liteloader/modconfig/ConfigManager.java
@@ -11,10 +11,9 @@ import com.google.common.base.Strings; | @@ -11,10 +11,9 @@ import com.google.common.base.Strings; | ||
11 | import com.google.common.io.Files; | 11 | import com.google.common.io.Files; |
12 | import com.mumfrey.liteloader.Configurable; | 12 | import com.mumfrey.liteloader.Configurable; |
13 | import com.mumfrey.liteloader.LiteMod; | 13 | import com.mumfrey.liteloader.LiteMod; |
14 | -import com.mumfrey.liteloader.ExposeConfig; | ||
15 | 14 | ||
16 | /** | 15 | /** |
17 | - * Registry where we keep the mod config panel classes | 16 | + * Registry where we keep the mod config panel classes and config file writers |
18 | * | 17 | * |
19 | * @author Adam Mummery-Smith | 18 | * @author Adam Mummery-Smith |
20 | */ | 19 | */ |
@@ -28,7 +27,7 @@ public class ConfigManager | @@ -28,7 +27,7 @@ public class ConfigManager | ||
28 | /** | 27 | /** |
29 | * Mod config writers | 28 | * Mod config writers |
30 | */ | 29 | */ |
31 | - private Map<Class<? extends Exposable>, ExposableConfigWriter> configWriters = new HashMap<Class<? extends Exposable>, ExposableConfigWriter>(); | 30 | + private Map<Exposable, ExposableConfigWriter> configWriters = new HashMap<Exposable, ExposableConfigWriter>(); |
32 | 31 | ||
33 | /** | 32 | /** |
34 | * List of config writers, for faster iteration in onTick | 33 | * List of config writers, for faster iteration in onTick |
@@ -51,18 +50,20 @@ public class ConfigManager | @@ -51,18 +50,20 @@ public class ConfigManager | ||
51 | 50 | ||
52 | /** | 51 | /** |
53 | * @param exposable | 52 | * @param exposable |
53 | + * @param fallbackFileName | ||
54 | + * @param ignoreMissingConfigAnnotation | ||
54 | */ | 55 | */ |
55 | - public void registerExposable(Exposable exposable, String fileName, boolean force) | 56 | + public void registerExposable(Exposable exposable, String fallbackFileName, boolean ignoreMissingConfigAnnotation) |
56 | { | 57 | { |
57 | - ExposeConfig exposeConfig = exposable.getClass().<ExposeConfig>getAnnotation(ExposeConfig.class); | ||
58 | - if (exposeConfig != null) | 58 | + ExposableOptions options = exposable.getClass().<ExposableOptions>getAnnotation(ExposableOptions.class); |
59 | + if (options != null) | ||
59 | { | 60 | { |
60 | - if (fileName == null) exposeConfig.filename(); | ||
61 | - this.initConfigWriter(exposable, fileName, exposeConfig.strategy()); | 61 | + if (fallbackFileName == null) fallbackFileName = options.filename(); |
62 | + this.initConfigWriter(exposable, fallbackFileName, options.strategy(), options.aggressive()); | ||
62 | } | 63 | } |
63 | - else if (force) | 64 | + else if (ignoreMissingConfigAnnotation) |
64 | { | 65 | { |
65 | - this.initConfigWriter(exposable, fileName, ConfigStrategy.Versioned); | 66 | + this.initConfigWriter(exposable, fallbackFileName, ConfigStrategy.Versioned, false); |
66 | } | 67 | } |
67 | } | 68 | } |
68 | 69 | ||
@@ -73,11 +74,11 @@ public class ConfigManager | @@ -73,11 +74,11 @@ public class ConfigManager | ||
73 | * @param fileName | 74 | * @param fileName |
74 | * @param strategy | 75 | * @param strategy |
75 | */ | 76 | */ |
76 | - private void initConfigWriter(Exposable exposable, String fileName, ConfigStrategy strategy) | 77 | + private void initConfigWriter(Exposable exposable, String fileName, ConfigStrategy strategy, boolean aggressive) |
77 | { | 78 | { |
78 | - if (this.configWriters.containsKey(exposable.getClass())) | 79 | + if (this.configWriters.containsKey(exposable)) |
79 | { | 80 | { |
80 | - throw new IllegalArgumentException("Cannot register multiple Exposable instances of the same class or the Exposable already registered"); | 81 | + return; |
81 | } | 82 | } |
82 | 83 | ||
83 | if (Strings.isNullOrEmpty(fileName)) | 84 | if (Strings.isNullOrEmpty(fileName)) |
@@ -88,27 +89,13 @@ public class ConfigManager | @@ -88,27 +89,13 @@ public class ConfigManager | ||
88 | fileName = fileName.substring(7); | 89 | fileName = fileName.substring(7); |
89 | } | 90 | } |
90 | 91 | ||
91 | - ExposableConfigWriter configWriter = ExposableConfigWriter.create(exposable, strategy, fileName); | 92 | + ExposableConfigWriter configWriter = ExposableConfigWriter.create(exposable, strategy, fileName, aggressive); |
92 | if (configWriter != null) | 93 | if (configWriter != null) |
93 | { | 94 | { |
94 | - this.configWriters.put(exposable.getClass(), configWriter); | 95 | + this.configWriters.put(exposable, configWriter); |
95 | this.configWriterList.add(configWriter); | 96 | this.configWriterList.add(configWriter); |
96 | } | 97 | } |
97 | } | 98 | } |
98 | - | ||
99 | - /** | ||
100 | - * Initialise the config writer for the specified mod | ||
101 | - * | ||
102 | - * @param exposable | ||
103 | - */ | ||
104 | - public void initConfig(Exposable exposable) | ||
105 | - { | ||
106 | - Class<? extends Exposable> exposableClass = exposable.getClass(); | ||
107 | - if (exposableClass != null && this.configWriters.containsKey(exposableClass)) | ||
108 | - { | ||
109 | - this.configWriters.get(exposableClass).init(); | ||
110 | - } | ||
111 | - } | ||
112 | 99 | ||
113 | /** | 100 | /** |
114 | * If the specified mod has a versioned config strategy, attempt to copy the config | 101 | * If the specified mod has a versioned config strategy, attempt to copy the config |
@@ -179,16 +166,29 @@ public class ConfigManager | @@ -179,16 +166,29 @@ public class ConfigManager | ||
179 | } | 166 | } |
180 | 167 | ||
181 | /** | 168 | /** |
169 | + * Initialise the config writer for the specified mod | ||
170 | + * | ||
171 | + * @param exposable | ||
172 | + */ | ||
173 | + public void initConfig(Exposable exposable) | ||
174 | + { | ||
175 | + if (this.configWriters.containsKey(exposable)) | ||
176 | + { | ||
177 | + this.configWriters.get(exposable).init(); | ||
178 | + } | ||
179 | + } | ||
180 | + | ||
181 | + /** | ||
182 | * Invalidate the specified mod config, cause it to be written to disk or scheduled for writing | 182 | * Invalidate the specified mod config, cause it to be written to disk or scheduled for writing |
183 | * if it has been written recent | 183 | * if it has been written recent |
184 | * | 184 | * |
185 | - * @param exposableClass | 185 | + * @param exposable |
186 | */ | 186 | */ |
187 | - public void invalidateConfig(Class<? extends Exposable> exposableClass) | 187 | + public void invalidateConfig(Exposable exposable) |
188 | { | 188 | { |
189 | - if (exposableClass != null && this.configWriters.containsKey(exposableClass)) | 189 | + if (this.configWriters.containsKey(exposable)) |
190 | { | 190 | { |
191 | - this.configWriters.get(exposableClass).invalidate(); | 191 | + this.configWriters.get(exposable).invalidate(); |
192 | } | 192 | } |
193 | } | 193 | } |
194 | 194 |
java/com/mumfrey/liteloader/modconfig/ExposableConfigWriter.java
@@ -38,6 +38,11 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -38,6 +38,11 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
38 | private final boolean versioned; | 38 | private final boolean versioned; |
39 | 39 | ||
40 | /** | 40 | /** |
41 | + * Disable anti-hammer and always save when requested | ||
42 | + */ | ||
43 | + private final boolean aggressive; | ||
44 | + | ||
45 | + /** | ||
41 | * Gson instance | 46 | * Gson instance |
42 | */ | 47 | */ |
43 | private final Gson gson; | 48 | private final Gson gson; |
@@ -56,16 +61,17 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -56,16 +61,17 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
56 | * It's possible that writes may be requested from different threads, lock object to prevent cross-thread derp | 61 | * It's possible that writes may be requested from different threads, lock object to prevent cross-thread derp |
57 | */ | 62 | */ |
58 | private Object readWriteLock = new Object(); | 63 | private Object readWriteLock = new Object(); |
59 | - | 64 | + |
60 | /** | 65 | /** |
61 | * @param exposable | 66 | * @param exposable |
62 | * @param configFile | 67 | * @param configFile |
63 | */ | 68 | */ |
64 | - private ExposableConfigWriter(Exposable exposable, File configFile, boolean versioned) | 69 | + private ExposableConfigWriter(Exposable exposable, File configFile, boolean versioned, boolean aggressive) |
65 | { | 70 | { |
66 | this.exposable = exposable; | 71 | this.exposable = exposable; |
67 | this.configFile = configFile; | 72 | this.configFile = configFile; |
68 | this.versioned = versioned; | 73 | this.versioned = versioned; |
74 | + this.aggressive = aggressive; | ||
69 | 75 | ||
70 | GsonBuilder gsonBuilder = new GsonBuilder(); | 76 | GsonBuilder gsonBuilder = new GsonBuilder(); |
71 | gsonBuilder.setPrettyPrinting(); | 77 | gsonBuilder.setPrettyPrinting(); |
@@ -93,6 +99,14 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -93,6 +99,14 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
93 | } | 99 | } |
94 | 100 | ||
95 | /** | 101 | /** |
102 | + * Returns true if anti-hammer is disabled for this writer | ||
103 | + */ | ||
104 | + public boolean isAggressive() | ||
105 | + { | ||
106 | + return this.aggressive; | ||
107 | + } | ||
108 | + | ||
109 | + /** | ||
96 | * Returns true if this writer has been invalidated but not yet been flushed to disk | 110 | * Returns true if this writer has been invalidated but not yet been flushed to disk |
97 | */ | 111 | */ |
98 | boolean isDirty() | 112 | boolean isDirty() |
@@ -205,11 +219,14 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -205,11 +219,14 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
205 | */ | 219 | */ |
206 | void invalidate() | 220 | void invalidate() |
207 | { | 221 | { |
208 | - long sinceLastWrite = System.currentTimeMillis() - this.lastWrite; | ||
209 | - if (sinceLastWrite < ANTI_HAMMER_DELAY) | 222 | + if (!this.aggressive) |
210 | { | 223 | { |
211 | - this.dirty = true; | ||
212 | - return; | 224 | + long sinceLastWrite = System.currentTimeMillis() - this.lastWrite; |
225 | + if (sinceLastWrite < ANTI_HAMMER_DELAY) | ||
226 | + { | ||
227 | + this.dirty = true; | ||
228 | + return; | ||
229 | + } | ||
213 | } | 230 | } |
214 | 231 | ||
215 | this.write(); | 232 | this.write(); |
@@ -220,7 +237,7 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -220,7 +237,7 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
220 | */ | 237 | */ |
221 | void onTick() | 238 | void onTick() |
222 | { | 239 | { |
223 | - if (this.dirty) | 240 | + if (!this.aggressive && this.dirty) |
224 | { | 241 | { |
225 | long sinceLastWrite = System.currentTimeMillis() - this.lastWrite; | 242 | long sinceLastWrite = System.currentTimeMillis() - this.lastWrite; |
226 | if (sinceLastWrite >= ANTI_HAMMER_DELAY) | 243 | if (sinceLastWrite >= ANTI_HAMMER_DELAY) |
@@ -235,7 +252,7 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -235,7 +252,7 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
235 | */ | 252 | */ |
236 | void sync() | 253 | void sync() |
237 | { | 254 | { |
238 | - if (this.dirty) | 255 | + if (this.dirty || this.aggressive) |
239 | { | 256 | { |
240 | this.write(); | 257 | this.write(); |
241 | } | 258 | } |
@@ -249,13 +266,13 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | @@ -249,13 +266,13 @@ public final class ExposableConfigWriter implements InstanceCreator<Exposable> | ||
249 | * @param fileName | 266 | * @param fileName |
250 | * @return | 267 | * @return |
251 | */ | 268 | */ |
252 | - static ExposableConfigWriter create(Exposable exposable, ConfigStrategy strategy, String fileName) | 269 | + static ExposableConfigWriter create(Exposable exposable, ConfigStrategy strategy, String fileName, boolean aggressive) |
253 | { | 270 | { |
254 | if (!fileName.toLowerCase().endsWith(".json")) | 271 | if (!fileName.toLowerCase().endsWith(".json")) |
255 | fileName = fileName + ".json"; | 272 | fileName = fileName + ".json"; |
256 | 273 | ||
257 | File configFile = strategy.getFileForStrategy(fileName); | 274 | File configFile = strategy.getFileForStrategy(fileName); |
258 | - ExposableConfigWriter writer = new ExposableConfigWriter(exposable, configFile, strategy == ConfigStrategy.Versioned); | 275 | + ExposableConfigWriter writer = new ExposableConfigWriter(exposable, configFile, strategy == ConfigStrategy.Versioned, aggressive); |
259 | 276 | ||
260 | return writer; | 277 | return writer; |
261 | } | 278 | } |
java/com/mumfrey/liteloader/ExposeConfig.java renamed to java/com/mumfrey/liteloader/modconfig/ExposableOptions.java
1 | -package com.mumfrey.liteloader; | 1 | +package com.mumfrey.liteloader.modconfig; |
2 | 2 | ||
3 | import java.lang.annotation.ElementType; | 3 | import java.lang.annotation.ElementType; |
4 | import java.lang.annotation.Retention; | 4 | import java.lang.annotation.Retention; |
5 | import java.lang.annotation.RetentionPolicy; | 5 | import java.lang.annotation.RetentionPolicy; |
6 | import java.lang.annotation.Target; | 6 | import java.lang.annotation.Target; |
7 | 7 | ||
8 | -import com.mumfrey.liteloader.modconfig.ConfigStrategy; | ||
9 | - | ||
10 | /** | 8 | /** |
11 | - * Annotation which can be a applied to mod classes to indicate they should be serialised with Gson | 9 | + * Annotation which can be a applied to mod classes to indicate that members decorated with the Gson |
10 | + * Expose annotation should be serialised with Gson | ||
12 | * | 11 | * |
13 | * @author Adam Mummery-Smith | 12 | * @author Adam Mummery-Smith |
14 | */ | 13 | */ |
15 | @Target(ElementType.TYPE) | 14 | @Target(ElementType.TYPE) |
16 | @Retention(RetentionPolicy.RUNTIME) | 15 | @Retention(RetentionPolicy.RUNTIME) |
17 | -public @interface ExposeConfig | 16 | +public @interface ExposableOptions |
18 | { | 17 | { |
19 | /** | 18 | /** |
20 | * Configuration strategy to use | 19 | * Configuration strategy to use |
@@ -25,4 +24,9 @@ public @interface ExposeConfig | @@ -25,4 +24,9 @@ public @interface ExposeConfig | ||
25 | * Config file name, if not specified the mod class name is used | 24 | * Config file name, if not specified the mod class name is used |
26 | */ | 25 | */ |
27 | String filename() default ""; | 26 | String filename() default ""; |
27 | + | ||
28 | + /** | ||
29 | + * Set to true to disable write anti-hammer for config file | ||
30 | + */ | ||
31 | + boolean aggressive() default false; | ||
28 | } | 32 | } |