Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/config-impl/api/config-impl.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Config {
protected fun initialize (Z)V
protected fun loadFrom (Ljava/lang/String;)V
protected fun loadFrom (Ljava/nio/file/Path;)V
protected fun loadFromAndDelete (Ljava/nio/file/Path;)V
protected fun makeTree ()Lorg/polyfrost/oneconfig/api/config/v1/Tree;
public fun preload ()V
protected fun restoreDefaults ()V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"chicken": false,
"cow5": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.polyfrost.oneconfig.api.config.v1.annotations.Include;
import org.polyfrost.polyui.data.PolyImage;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -200,7 +199,7 @@ protected void loadFrom(String id) {
if (tree == null) initialize(false);
Tree in = ConfigManager.active().get(id);
if (in == null) return;
tree.overwrite(in, false);
tree.overwrite(in, false, true, tree);
}

protected void loadFrom(Path p) {
Expand All @@ -212,19 +211,9 @@ protected void loadFrom(Path p) {
return;
}
if (in == null) return;
tree.overwrite(in, false);
tree.overwrite(in, false, true, tree);
}

protected void loadFromAndDelete(Path p) {
try {
loadFrom(p);
Files.deleteIfExists(p);
} catch (Exception e) {
ConfigManager.LOGGER.error("An unknown error occurred", e);
}
}


protected Property<?> getProperty(String option) {
if (tree == null) initialize(false);
return getProperty(tree, option);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,28 @@

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.File;

import static org.junit.jupiter.api.Assertions.*;

public class MainTest {
@org.junit.jupiter.api.Test
void test() {
File configFile = new File("config/test_mod.json");
assertTrue(!configFile.exists() || configFile.delete());
TestConfig config = new TestConfig();
config.initialize(true);
Tree t = config.tree;
assertEquals(t, ConfigManager.active().get(t.getID()));
assertNotNull(t.get("chicken").getMetadata("visualizer"));
assertNull(t.get("reserved:overwritten"));
System.err.println(t);
assertTrue(TestConfig.chicken);
assertFalse(TestConfig.cow5);
config.loadFrom(new File(new File(new File(".").getParentFile(), "config-to-migrate-for-test"), "test_migration_mod.json").toPath());
assertNotNull(t.get("reserved:overwritten"));
assertFalse(TestConfig.chicken);
assertTrue(TestConfig.cow5);
System.err.println(t);
}
}
9 changes: 5 additions & 4 deletions modules/config/api/config.api
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Node {
public fun getTitle ()Ljava/lang/String;
public final fun hasMetadata ()Z
public final fun hashCode ()I
public abstract fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
public abstract fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
public final fun removeMetadata (Ljava/lang/String;)Z
public fun setID (Ljava/lang/String;)V
public fun setTitle (Ljava/lang/String;)V
Expand Down Expand Up @@ -70,7 +71,7 @@ public abstract class org/polyfrost/oneconfig/api/config/v1/Property : org/polyf
public final fun isArray ()Z
public final fun isPrimitive ()Z
public fun onDisplayChange (Ljava/util/function/Consumer;)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
public static fun recast (Lorg/polyfrost/oneconfig/api/config/v1/Property;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
public fun revaluateDisplay ()V
public final fun set (Ljava/lang/Object;)V
Expand Down Expand Up @@ -112,8 +113,8 @@ public class org/polyfrost/oneconfig/api/config/v1/Tree : org/polyfrost/oneconfi
public fun getProp ([Ljava/lang/String;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
public fun onAll (Ljava/util/function/BiConsumer;)V
public fun onAllProps (Ljava/util/function/BiConsumer;)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;Z)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;ZZLorg/polyfrost/oneconfig/api/config/v1/Tree;)V
public fun put (Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
public fun put ([Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
public fun set (Ljava/lang/String;Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ public final boolean hasMetadata() {
return metadata != null;
}

public void overwrite(Node with, boolean preserveMissingOptions) {
overwrite(with, preserveMissingOptions, false, null);
}

/**
* Overwrite all data in this node with the data from another node.
* <br><ul>
Expand All @@ -197,8 +201,10 @@ public final boolean hasMetadata() {
*
* @param with the node to overwrite this with
* @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.
* @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.
* @param root The root tree used to mark overwritten nodes for skipOverwritten. May be null if skipOverwritten is false.
*/
public abstract void overwrite(Node with, boolean preserveMissingOptions);
public abstract void overwrite(Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root);

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,29 @@ public final Property<T> addCallback(@NotNull Collection<Predicate<T>> callbacks

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void overwrite(Node with, boolean preserveMissingOptions) {
public void overwrite(Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
if (!(with instanceof Property)) throw new IllegalArgumentException("Cannot overwrite a property with a non-property");
if (!Objects.equals(this.getID(), with.getID())) throw new IllegalArgumentException("ID should be the same for overwrite");
if (skipOverwritten && root == null) {
throw new IllegalArgumentException("Cannot mark overwritten without a root tree!");
}
Node overwritten = null;
if (root != null) {
overwritten = root.get("reserved:overwritten");
}
if (skipOverwritten && overwritten instanceof Tree && !Objects.equals(((Tree) overwritten).get(with.getID()), null)) {
return;
}
Property<?> that = (Property<?>) with;
this.addMetadata(that.getMetadata());
Object in = that.get();
if (in != null) this.setAsReferential(in);
if (that.conditions != null) this.addDisplayCondition(that.conditions);
if (that.callbacks != null) addCallback((Collection) that.callbacks);
if (root != null) {
overwritten = root.getOrPutChild("reserved:overwritten");
((Tree) overwritten).put(new Tree(that.getID(), null, null, null));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -68,7 +69,11 @@ private static void _onAllProp(Tree t, BiConsumer<String, Property<?>> action) {
}
}

private static void _overwrite(Tree self, Tree in, Function<String, String> keyMapper, boolean preserveMissingOptions) {
private static void _overwrite(Tree self, Tree in, Function<String, String> keyMapper, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
Node overwritten = null;
if (root != null) {
overwritten = root.get("reserved:overwritten");
}
for (Map.Entry<String, Node> from : in.theMap.entrySet()) {
String key = keyMapper == null ? from.getKey() : keyMapper.apply(from.getKey());
Node _this = self.get(key);
Expand All @@ -82,15 +87,23 @@ private static void _overwrite(Tree self, Tree in, Function<String, String> keyM
continue;
}
if (_this instanceof Tree) {
if (skipOverwritten && overwritten instanceof Tree && !Objects.equals(((Tree) overwritten).get(that.getID()), null)) {
// this node was previously overwritten, skip
continue;
}
if (that instanceof Tree) {
// if both are trees, recursively overwrite
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions);
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions, skipOverwritten, root);
_this.addMetadata(that.getMetadata());
if (root != null) {
overwritten = root.getOrPutChild("reserved:overwritten");
((Tree) overwritten).put(new Tree(that.getID(), null, null, null));
}
}
// nop. do not attempt to overwrite a tree with a property
else continue;
}
_this.overwrite(that, preserveMissingOptions);
_this.overwrite(that, preserveMissingOptions, skipOverwritten, root);
}
}

Expand Down Expand Up @@ -273,18 +286,21 @@ public boolean deepEquals(@Nullable Object obj) {
* @param with the tree to overwrite with
* @param keyMapper the key mapper function to use
*/
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions) {
_overwrite(this, with, keyMapper, preserveMissingOptions);
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
_overwrite(this, with, keyMapper, preserveMissingOptions, skipOverwritten, root);
}

@Override
public void overwrite(@NotNull Node with, boolean preserveMissingOptions) {
public void overwrite(@NotNull Node with, boolean preserveMissingOptions, boolean skipOverwritten, @Nullable Tree root) {
if (!(with instanceof Tree)) throw new IllegalArgumentException("Cannot overwrite a tree with a non-tree node!");
if (skipOverwritten && root == null) {
throw new IllegalArgumentException("Cannot mark overwritten without a root tree!");
}
Map<String, String> migrationMap = getMetadata("migrationMap");
if (migrationMap == null) {
_overwrite(this, (Tree) with, null, preserveMissingOptions);
_overwrite(this, (Tree) with, null, preserveMissingOptions, skipOverwritten, root);
} else {
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions);
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions, skipOverwritten, root);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
import org.polyfrost.oneconfig.api.config.v1.Node;
import org.polyfrost.oneconfig.api.config.v1.Tree;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* A backend is a storage system for ConfigTrees.
Expand Down Expand Up @@ -114,6 +111,9 @@ public final boolean load(Tree tree) {
return false;
}
if (t == null) return false;
if (t.get("reserved:overwritten") != null) {
tree.put(Objects.requireNonNull(t.get("reserved:overwritten")));
}
tree.overwrite(t, false);

putSafe(tree);
Expand Down