Skip to content

Commit f5c48ee

Browse files
committed
Proposal to fix #597
1 parent 0cdf74f commit f5c48ee

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
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

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ protected void loadFrom(String id) {
200200
if (tree == null) initialize(false);
201201
Tree in = ConfigManager.active().get(id);
202202
if (in == null) return;
203-
tree.overwrite(in, false);
203+
tree.overwrite(in, false, true);
204204
}
205205

206206
protected void loadFrom(Path p) {
@@ -212,19 +212,9 @@ protected void loadFrom(Path p) {
212212
return;
213213
}
214214
if (in == null) return;
215-
tree.overwrite(in, false);
215+
tree.overwrite(in, false, true);
216216
}
217217

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-
228218
protected Property<?> getProperty(String option) {
229219
if (tree == null) initialize(false);
230220
return getProperty(tree, option);

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;ZZ)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;ZZ)V
7475
public static fun recast (Lorg/polyfrost/oneconfig/api/config/v1/Property;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
7576
public fun revaluateDisplay ()V
7677
public final fun set (Ljava/lang/Object;)V
@@ -112,8 +113,8 @@ public class org/polyfrost/oneconfig/api/config/v1/Tree : org/polyfrost/oneconfi
112113
public fun getProp ([Ljava/lang/String;)Lorg/polyfrost/oneconfig/api/config/v1/Property;
113114
public fun onAll (Ljava/util/function/BiConsumer;)V
114115
public fun onAllProps (Ljava/util/function/BiConsumer;)V
115-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;Z)V
116-
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;Z)V
116+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Node;ZZ)V
117+
public fun overwrite (Lorg/polyfrost/oneconfig/api/config/v1/Tree;Ljava/util/function/Function;ZZ)V
117118
public fun put (Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
118119
public fun put ([Lorg/polyfrost/oneconfig/api/config/v1/Node;)Lorg/polyfrost/oneconfig/api/config/v1/Tree;
119120
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: 5 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);
192+
}
193+
190194
/**
191195
* Overwrite all data in this node with the data from another node.
192196
* <br><ul>
@@ -198,7 +202,7 @@ public final boolean hasMetadata() {
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.
200204
*/
201-
public abstract void overwrite(Node with, boolean preserveMissingOptions);
205+
public abstract void overwrite(Node with, boolean preserveMissingOptions, boolean markOverwritten);
202206

203207
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
204208
@VisibleForTesting

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,19 @@ public final Property<T> addCallback(@NotNull Collection<Predicate<T>> callbacks
181181

182182
@Override
183183
@SuppressWarnings({"unchecked", "rawtypes"})
184-
public void overwrite(Node with, boolean preserveMissingOptions) {
184+
public void overwrite(Node with, boolean preserveMissingOptions, boolean markOverwritten) {
185185
if (!(with instanceof Property)) throw new IllegalArgumentException("Cannot overwrite a property with a non-property");
186186
if (!Objects.equals(this.getID(), with.getID())) throw new IllegalArgumentException("ID should be the same for overwrite");
187+
if (markOverwritten && Objects.equals(getMetadata("overwritten"), true)) return;
187188
Property<?> that = (Property<?>) with;
188189
this.addMetadata(that.getMetadata());
189190
Object in = that.get();
190191
if (in != null) this.setAsReferential(in);
191192
if (that.conditions != null) this.addDisplayCondition(that.conditions);
192193
if (that.callbacks != null) addCallback((Collection) that.callbacks);
194+
if (markOverwritten) {
195+
addMetadata("overwritten", true);
196+
}
193197
}
194198

195199
/**

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

Lines changed: 12 additions & 10 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,7 @@ 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 markOverwritten) {
7273
for (Map.Entry<String, Node> from : in.theMap.entrySet()) {
7374
String key = keyMapper == null ? from.getKey() : keyMapper.apply(from.getKey());
7475
Node _this = self.get(key);
@@ -82,15 +83,16 @@ private static void _overwrite(Tree self, Tree in, Function<String, String> keyM
8283
continue;
8384
}
8485
if (_this instanceof Tree) {
85-
if (that instanceof Tree) {
86+
if (that instanceof Tree && Objects.equals(_this.getMetadata("overwritten"), true)) {
8687
// if both are trees, recursively overwrite
87-
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions);
88+
_overwrite((Tree) _this, (Tree) that, keyMapper, preserveMissingOptions, markOverwritten);
8889
_this.addMetadata(that.getMetadata());
90+
_this.addMetadata("overwritten", true);
8991
}
9092
// nop. do not attempt to overwrite a tree with a property
9193
else continue;
9294
}
93-
_this.overwrite(that, preserveMissingOptions);
95+
_this.overwrite(that, preserveMissingOptions, markOverwritten);
9496
}
9597
}
9698

@@ -152,7 +154,7 @@ public Tree put(Node n) {
152154
if (old == n) return this; // yeah, ok.
153155
if (old != null) {
154156
// LOGGER.warn("Replacing existing node with id {}: {} -> {}", n.getID(), old, n);
155-
n.overwrite(old, false);
157+
n.overwrite(old, false, false);
156158
}
157159
theMap.put(n.getID(), n);
158160
return this;
@@ -273,18 +275,18 @@ public boolean deepEquals(@Nullable Object obj) {
273275
* @param with the tree to overwrite with
274276
* @param keyMapper the key mapper function to use
275277
*/
276-
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions) {
277-
_overwrite(this, with, keyMapper, preserveMissingOptions);
278+
public void overwrite(Tree with, @NotNull Function<String, String> keyMapper, boolean preserveMissingOptions, boolean markOverwritten) {
279+
_overwrite(this, with, keyMapper, preserveMissingOptions, markOverwritten);
278280
}
279281

280282
@Override
281-
public void overwrite(@NotNull Node with, boolean preserveMissingOptions) {
283+
public void overwrite(@NotNull Node with, boolean preserveMissingOptions, boolean markOverwritten) {
282284
if (!(with instanceof Tree)) throw new IllegalArgumentException("Cannot overwrite a tree with a non-tree node!");
283285
Map<String, String> migrationMap = getMetadata("migrationMap");
284286
if (migrationMap == null) {
285-
_overwrite(this, (Tree) with, null, preserveMissingOptions);
287+
_overwrite(this, (Tree) with, null, preserveMissingOptions, markOverwritten);
286288
} else {
287-
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions);
289+
_overwrite(this, (Tree) with, migrationMap::get, preserveMissingOptions, markOverwritten);
288290
}
289291
}
290292

0 commit comments

Comments
 (0)