Skip to content

Commit 03e0b25

Browse files
authored
Merge branch 'v1' into state-v2-and-actually-use-glrenderer
2 parents d72f971 + eb7be18 commit 03e0b25

File tree

9 files changed

+74
-34
lines changed

9 files changed

+74
-34
lines changed

modules/config-impl/api/config-impl.api

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Config {
2525
protected fun initialize (Z)V
2626
protected fun loadFrom (Ljava/lang/String;)V
2727
protected fun loadFrom (Ljava/nio/file/Path;)V
28-
protected fun loadFromAndDelete (Ljava/nio/file/Path;)V
2928
protected fun makeTree ()Lorg/polyfrost/oneconfig/api/config/v1/Tree;
3029
public fun preload ()V
3130
protected fun restoreDefaults ()V
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"chicken": false,
3+
"cow5": true
4+
}

modules/config-impl/src/main/java/org/polyfrost/oneconfig/api/config/v1/Config.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.polyfrost.oneconfig.api.config.v1.annotations.Include;
3434
import org.polyfrost.polyui.data.PolyImage;
3535

36-
import java.nio.file.Files;
3736
import java.nio.file.Path;
3837
import java.util.ArrayList;
3938
import java.util.Arrays;
@@ -200,7 +199,7 @@ protected void loadFrom(String id) {
200199
if (tree == null) initialize(false);
201200
Tree in = ConfigManager.active().get(id);
202201
if (in == null) return;
203-
tree.overwrite(in, false);
202+
tree.overwrite(in, false, true, tree);
204203
}
205204

206205
protected void loadFrom(Path p) {
@@ -212,19 +211,9 @@ protected void loadFrom(Path p) {
212211
return;
213212
}
214213
if (in == null) return;
215-
tree.overwrite(in, false);
214+
tree.overwrite(in, false, true, tree);
216215
}
217216

218-
protected void loadFromAndDelete(Path p) {
219-
try {
220-
loadFrom(p);
221-
Files.deleteIfExists(p);
222-
} catch (Exception e) {
223-
ConfigManager.LOGGER.error("An unknown error occurred", e);
224-
}
225-
}
226-
227-
228217
protected Property<?> getProperty(String option) {
229218
if (tree == null) initialize(false);
230219
return getProperty(tree, option);

modules/config-impl/src/test/java/org/polyfrost/oneconfig/api/config/v1/MainTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,28 @@
2626

2727
package org.polyfrost.oneconfig.api.config.v1;
2828

29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import java.io.File;
30+
31+
import static org.junit.jupiter.api.Assertions.*;
3132

3233
public class MainTest {
3334
@org.junit.jupiter.api.Test
3435
void test() {
36+
File configFile = new File("config/test_mod.json");
37+
assertTrue(!configFile.exists() || configFile.delete());
3538
TestConfig config = new TestConfig();
3639
config.initialize(true);
3740
Tree t = config.tree;
3841
assertEquals(t, ConfigManager.active().get(t.getID()));
3942
assertNotNull(t.get("chicken").getMetadata("visualizer"));
43+
assertNull(t.get("reserved:overwritten"));
44+
System.err.println(t);
45+
assertTrue(TestConfig.chicken);
46+
assertFalse(TestConfig.cow5);
47+
config.loadFrom(new File(new File(new File(".").getParentFile(), "config-to-migrate-for-test"), "test_migration_mod.json").toPath());
48+
assertNotNull(t.get("reserved:overwritten"));
49+
assertFalse(TestConfig.chicken);
50+
assertTrue(TestConfig.cow5);
4051
System.err.println(t);
4152
}
4253
}

modules/config/api/config.api

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Node {
1414
public fun getTitle ()Ljava/lang/String;
1515
public final fun hasMetadata ()Z
1616
public final fun hashCode ()I
17-
public abstract fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
17+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
18+
public abstract fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
1819
public final fun removeMetadata (Ljava/lang/String;)Z
1920
public fun setID (Ljava/lang/String;)V
2021
public fun setTitle (Ljava/lang/String;)V
@@ -70,7 +71,7 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Property : org/polyf
7071
public final fun isArray ()Z
7172
public final fun isPrimitive ()Z
7273
public fun onDisplayChange (Ljava/util/function/Consumer;)V
73-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
74+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
7475
public static fun recast (Lorg/polyfrost/oneconfig/api/config/v1/Property;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
7576
public final fun removeCallback (Ljava/util/function/Predicate;)Z
7677
public fun revaluateDisplay ()V
@@ -113,8 +114,8 @@ public class org/polyfrost/oneconfig/api/config/v1/Tree : org/polyfrost/oneconfi
113114
public fun getProp ([Ljava/lang/String;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
114115
public fun onAll (Ljava/util/function/BiConsumer;)V
115116
public fun onAllProps (Ljava/util/function/BiConsumer;)V
116-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
117-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;Z)V
117+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
118+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
118119
public fun put (Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
119120
public fun put ([Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
120121
public fun set (Ljava/lang/String;Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/Node.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ public final boolean hasMetadata() {
187187
return metadata != null;
188188
}
189189

190+
public void overwrite(Node with, boolean preserveMissingOptions) {
191+
overwrite(with, preserveMissingOptions, false, null);
192+
}
193+
190194
/**
191195
* Overwrite all data in this node with the data from another node.
192196
* <br><ul>
@@ -197,8 +201,10 @@ public final boolean hasMetadata() {
197201
*
198202
* @param with the node to overwrite this with
199203
* @param preserveMissingOptions if true, any properties that are missing in THIS tree that are present in the input tree, will be added to this tree.
204+
* @param skipOverwritten **DEPENDS ON PARAM "ROOT" BEING NOT NULL** - skips being overwritten if a child node has already been marked as overwritten in the root tree.
205+
* @param root The root tree used to mark overwritten nodes for skipOverwritten. May be null if skipOverwritten is false.
200206
*/
201-
public abstract void overwrite(Node with, boolean preserveMissingOptions);
207+
public abstract void overwrite(Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root);
202208

203209
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
204210
@VisibleForTesting

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/Property.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,29 @@ public final boolean removeCallback(@NotNull Predicate<T> callback) {
188188

189189
@Override
190190
@SuppressWarnings({"unchecked", "rawtypes"})
191-
public void overwrite(Node with, boolean preserveMissingOptions) {
191+
public void overwrite(Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
192192
if (!(with instanceof Property)) throw new IllegalArgumentException("Cannot overwrite a property with a non-property");
193193
if (!Objects.equals(this.getID(), with.getID())) throw new IllegalArgumentException("ID should be the same for overwrite");
194+
if (skipOverwritten && root == null) {
195+
throw new IllegalArgumentException("Cannot mark overwritten without a root tree!");
196+
}
197+
Node overwritten = null;
198+
if (root != null) {
199+
overwritten = root.get("reserved:overwritten");
200+
}
201+
if (skipOverwritten && overwritten instanceof Tree && !Objects.equals(((Tree) overwritten).get(with.getID()), null)) {
202+
return;
203+
}
194204
Property<?> that = (Property<?>) with;
195205
this.addMetadata(that.getMetadata());
196206
Object in = that.get();
197207
if (in != null) this.setAsReferential(in);
198208
if (that.conditions != null) this.addDisplayCondition(that.conditions);
199209
if (that.callbacks != null) addCallback((Collection) that.callbacks);
210+
if (root != null) {
211+
overwritten = root.getOrPutChild("reserved:overwritten");
212+
((Tree) overwritten).put(new Tree(that.getID(), null, null, null));
213+
}
200214
}
201215

202216
/**

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/Tree.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Collections;
3333
import java.util.LinkedHashMap;
3434
import java.util.Map;
35+
import java.util.Objects;
3536
import java.util.function.BiConsumer;
3637
import java.util.function.Function;
3738
import java.util.stream.Collectors;
@@ -68,7 +69,11 @@ private static void _onAllProp(Tree t, BiConsumer<String, Property<?>> action) {
6869
}
6970
}
7071

71-
private static void _overwrite(Tree self, Tree in, Function<String, String> keyMapper, boolean preserveMissingOptions) {
72+
private static void _overwrite(Tree self, Tree in, Function<String, String> keyMapper, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
73+
Node overwritten = null;
74+
if (root != null) {
75+
overwritten = root.get("reserved:overwritten");
76+
}
7277
for (Map.Entry<String, Node> from : in.theMap.entrySet()) {
7378
String key = keyMapper == null ? from.getKey() : keyMapper.apply(from.getKey());
7479
Node _this = self.get(key);
@@ -82,15 +87,23 @@ private static void _overwrite(Tree self, Tree in, Function<String, String> keyM
8287
continue;
8388
}
8489
if (_this instanceof Tree) {
90+
if (skipOverwritten && overwritten instanceof Tree && !Objects.equals(((Tree) overwritten).get(that.getID()), null)) {
91+
// this node was previously overwritten, skip
92+
continue;
93+
}
8594
if (that instanceof Tree) {
8695
// if both are trees, recursively overwrite
87-
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions);
96+
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions, skipOverwritten, root);
8897
_this.addMetadata(that.getMetadata());
98+
if (root != null) {
99+
overwritten = root.getOrPutChild("reserved:overwritten");
100+
((Tree) overwritten).put(new Tree(that.getID(), null, null, null));
101+
}
89102
}
90103
// nop. do not attempt to overwrite a tree with a property
91104
else continue;
92105
}
93-
_this.overwrite(that, preserveMissingOptions);
106+
_this.overwrite(that, preserveMissingOptions, skipOverwritten, root);
94107
}
95108
}
96109

@@ -273,18 +286,21 @@ public boolean deepEquals(@Nullable Object obj) {
273286
* @param with the tree to overwrite with
274287
* @param keyMapper the key mapper function to use
275288
*/
276-
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions) {
277-
_overwrite(this, with, keyMapper, preserveMissingOptions);
289+
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
290+
_overwrite(this, with, keyMapper, preserveMissingOptions, skipOverwritten, root);
278291
}
279292

280293
@Override
281-
public void overwrite(@NotNull Node with, boolean preserveMissingOptions) {
294+
public void overwrite(@NotNull Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
282295
if (!(with instanceof Tree)) throw new IllegalArgumentException("Cannot overwrite a tree with a non-tree node!");
296+
if (skipOverwritten && root == null) {
297+
throw new IllegalArgumentException("Cannot mark overwritten without a root tree!");
298+
}
283299
Map<String, String> migrationMap = getMetadata("migrationMap");
284300
if (migrationMap == null) {
285-
_overwrite(this, (Tree) with, null, preserveMissingOptions);
301+
_overwrite(this, (Tree) with, null, preserveMissingOptions, skipOverwritten, root);
286302
} else {
287-
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions);
303+
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions, skipOverwritten, root);
288304
}
289305
}
290306

modules/config/src/main/java/org/polyfrost/oneconfig/api/config/v1/backend/Backend.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
import org.polyfrost.oneconfig.api.config.v1.Node;
3636
import org.polyfrost.oneconfig.api.config.v1.Tree;
3737

38-
import java.util.Collection;
39-
import java.util.Collections;
40-
import java.util.HashMap;
41-
import java.util.Map;
38+
import java.util.*;
4239

4340
/**
4441
* A backend is a storage system for ConfigTrees.
@@ -114,6 +111,9 @@ public final boolean load(Tree tree) {
114111
return false;
115112
}
116113
if (t == null) return false;
114+
if (t.get("reserved:overwritten") != null) {
115+
tree.put(Objects.requireNonNull(t.get("reserved:overwritten")));
116+
}
117117
tree.overwrite(t, false);
118118

119119
putSafe(tree);

0 commit comments

Comments
 (0)