Archive eaglerxserver (plugin, 1.5.2, 1.8.8, 1.12.2, clean)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
|
||||
plugins {
|
||||
id "java-library"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly(libs.velocity.nightconfig)
|
||||
compileOnly(libs.snakeyaml)
|
||||
compileOnly(libs.gson)
|
||||
compileOnly(libs.guava)
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.gson.GSONConfigLoader;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.nightconfig.NightConfigLoader;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.snakeyaml.YAMLConfigLoader;
|
||||
|
||||
public enum EnumConfigFormat {
|
||||
|
||||
YAML(ImmutableSet.of("yml", "yaml"), "yml", (f) -> {
|
||||
return YAMLConfigLoader.getConfigFile(f);
|
||||
}, "org.yaml.snakeyaml.Yaml"),
|
||||
|
||||
TOML(ImmutableSet.of("toml"), "toml", (f) -> {
|
||||
return NightConfigLoader.getConfigFile(f);
|
||||
}, "com.electronwill.nightconfig.toml.TomlParser"),
|
||||
|
||||
JSON(ImmutableSet.of("json"), "json", (f) -> {
|
||||
return GSONConfigLoader.getConfigFile(f);
|
||||
}, "com.google.gson.Gson");
|
||||
|
||||
private interface IConfigProvider {
|
||||
IEaglerConfig load(File file) throws IOException;
|
||||
}
|
||||
|
||||
private final Set<String> exts;
|
||||
private final String defaultExt;
|
||||
private final IConfigProvider provider;
|
||||
private final String depends;
|
||||
private int supported;
|
||||
|
||||
private EnumConfigFormat(Set<String> exts, String defaultExt, IConfigProvider provider, String depends) {
|
||||
this.exts = exts;
|
||||
this.defaultExt = defaultExt;
|
||||
this.provider = provider;
|
||||
this.depends = depends;
|
||||
}
|
||||
|
||||
public Set<String> getExts() {
|
||||
return exts;
|
||||
}
|
||||
|
||||
public String getDefaultExt() {
|
||||
return defaultExt;
|
||||
}
|
||||
|
||||
public IEaglerConfig getConfigFile(File file) throws IOException {
|
||||
return provider.load(file);
|
||||
}
|
||||
|
||||
public boolean isSupported() {
|
||||
if (supported == 0) {
|
||||
supported = classAvailable(depends) ? 2 : 1;
|
||||
}
|
||||
return supported == 2;
|
||||
}
|
||||
|
||||
public static Set<EnumConfigFormat> getSupported() {
|
||||
Set<EnumConfigFormat> ret = EnumSet.noneOf(EnumConfigFormat.class);
|
||||
for (EnumConfigFormat fmt : values()) {
|
||||
if (fmt.isSupported()) {
|
||||
ret.add(fmt);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static boolean classAvailable(String name) {
|
||||
try {
|
||||
Class.forName(name);
|
||||
return true;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface IEaglerConfList {
|
||||
|
||||
boolean exists();
|
||||
|
||||
boolean initialized();
|
||||
|
||||
void setComment(String comment);
|
||||
|
||||
IEaglerConfSection appendSection();
|
||||
|
||||
IEaglerConfList appendList();
|
||||
|
||||
void appendInteger(int value);
|
||||
|
||||
default void appendIntegers(List<Integer> lst) {
|
||||
lst.forEach((i) -> this.appendInteger(i));
|
||||
}
|
||||
|
||||
void appendString(String string);
|
||||
|
||||
default void appendStrings(List<String> lst) {
|
||||
lst.forEach(this::appendString);
|
||||
}
|
||||
|
||||
int getLength();
|
||||
|
||||
IEaglerConfSection getIfSection(int index);
|
||||
|
||||
IEaglerConfList getIfList(int index);
|
||||
|
||||
boolean isInteger(int index);
|
||||
|
||||
int getIfInteger(int index, int defaultVal);
|
||||
|
||||
default int getIfInteger(int index) {
|
||||
return getIfInteger(index, -1);
|
||||
}
|
||||
|
||||
boolean isString(int index);
|
||||
|
||||
String getIfString(int index, String defaultVal);
|
||||
|
||||
default String getIfString(int index) {
|
||||
return getIfString(index, null);
|
||||
}
|
||||
|
||||
default List<Integer> getAsIntegerList() {
|
||||
int len = getLength();
|
||||
List<Integer> ret = new ArrayList<>(len);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (isInteger(i)) {
|
||||
ret.add(getIfInteger(i));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
default List<Integer> getAsIntegerList(Supplier<List<Integer>> defaultContents) {
|
||||
if (!initialized()) {
|
||||
appendIntegers(defaultContents.get());
|
||||
}
|
||||
return getAsIntegerList();
|
||||
}
|
||||
|
||||
default List<Integer> getAsIntegerList(Supplier<List<Integer>> defaultContents, String comment) {
|
||||
if (!initialized()) {
|
||||
setComment(comment);
|
||||
appendIntegers(defaultContents.get());
|
||||
}
|
||||
return getAsIntegerList();
|
||||
}
|
||||
|
||||
default List<String> getAsStringList() {
|
||||
int len = getLength();
|
||||
List<String> ret = new ArrayList<>(len);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
String str = getIfString(i);
|
||||
if (str != null) {
|
||||
ret.add(str);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
default List<String> getAsStringList(Supplier<List<String>> defaultContents) {
|
||||
if (!initialized()) {
|
||||
appendStrings(defaultContents.get());
|
||||
}
|
||||
return getAsStringList();
|
||||
}
|
||||
|
||||
default List<String> getAsStringList(Supplier<List<String>> defaultContents, String comment) {
|
||||
if (!initialized()) {
|
||||
setComment(comment);
|
||||
appendStrings(defaultContents.get());
|
||||
}
|
||||
return getAsStringList();
|
||||
}
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface IEaglerConfSection {
|
||||
|
||||
boolean exists();
|
||||
|
||||
boolean initialized();
|
||||
|
||||
void setComment(String comment);
|
||||
|
||||
IEaglerConfSection getIfSection(String name);
|
||||
|
||||
IEaglerConfSection getSection(String name);
|
||||
|
||||
IEaglerConfList getIfList(String name);
|
||||
|
||||
IEaglerConfList getList(String name);
|
||||
|
||||
List<String> getKeys();
|
||||
|
||||
boolean isBoolean(String name);
|
||||
|
||||
boolean getBoolean(String name);
|
||||
|
||||
boolean getBoolean(String name, boolean defaultValue, String comment);
|
||||
|
||||
default boolean getBoolean(String name, boolean defaultValue) {
|
||||
return getBoolean(name, defaultValue, null);
|
||||
}
|
||||
|
||||
boolean getBoolean(String name, Supplier<Boolean> defaultValue, String comment);
|
||||
|
||||
default boolean getBoolean(String name, Supplier<Boolean> defaultValue) {
|
||||
return getBoolean(name, defaultValue, null);
|
||||
}
|
||||
|
||||
boolean isInteger(String name);
|
||||
|
||||
int getInteger(String name, int defaultValue, String comment);
|
||||
|
||||
default int getInteger(String name, int defaultValue) {
|
||||
return getInteger(name, defaultValue, null);
|
||||
}
|
||||
|
||||
int getInteger(String name, Supplier<Integer> defaultValue, String comment);
|
||||
|
||||
default int getInteger(String name, Supplier<Integer> defaultValue) {
|
||||
return getInteger(name, defaultValue, null);
|
||||
}
|
||||
|
||||
boolean isString(String name);
|
||||
|
||||
String getIfString(String name);
|
||||
|
||||
String getString(String name, String defaultValue, String comment);
|
||||
|
||||
default String getString(String name, String defaultValue) {
|
||||
return getString(name, defaultValue, null);
|
||||
}
|
||||
|
||||
String getString(String name, Supplier<String> defaultValue, String comment);
|
||||
|
||||
default String getString(String name, Supplier<String> defaultValue) {
|
||||
return getString(name, defaultValue, null);
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface IEaglerConfig {
|
||||
|
||||
EnumConfigFormat getFormat();
|
||||
|
||||
IEaglerConfSection getRoot();
|
||||
|
||||
boolean isModified();
|
||||
|
||||
boolean saveIfModified() throws IOException;
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2026 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public interface IRandomSupplier<T> extends Supplier<T> {
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class ReflectUtil {
|
||||
|
||||
public static RuntimeException propagateReflectThrowable(Exception ex) {
|
||||
if (ex instanceof InvocationTargetException exx) {
|
||||
Throwable cause = exx.getCause();
|
||||
if (cause != null) {
|
||||
if (cause instanceof RuntimeException cause2) {
|
||||
return cause2;
|
||||
}
|
||||
return new RuntimeException("Encountered an InvocationTargetException while performing reflection",
|
||||
cause);
|
||||
}
|
||||
} else if (ex instanceof RuntimeException exx) {
|
||||
return exx;
|
||||
}
|
||||
return new RuntimeException("Could not perform reflection!", ex);
|
||||
}
|
||||
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class WrapUtil {
|
||||
|
||||
// From an old version of apache commons lang3
|
||||
public static String wrap(final String str, int wrapLength, String newLineStr, final boolean wrapLongWords,
|
||||
String wrapOn) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
if (newLineStr == null) {
|
||||
newLineStr = System.lineSeparator();
|
||||
}
|
||||
if (wrapLength < 1) {
|
||||
wrapLength = 1;
|
||||
}
|
||||
final Pattern patternToWrapOn = Pattern.compile(wrapOn);
|
||||
final int inputLineLength = str.length();
|
||||
int offset = 0;
|
||||
final StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);
|
||||
|
||||
while (offset < inputLineLength) {
|
||||
int spaceToWrapAt = -1;
|
||||
Matcher matcher = patternToWrapOn.matcher(str.substring(offset,
|
||||
Math.min((int) Math.min(Integer.MAX_VALUE, offset + wrapLength + 1L), inputLineLength)));
|
||||
if (matcher.find()) {
|
||||
if (matcher.start() == 0) {
|
||||
offset += matcher.end();
|
||||
continue;
|
||||
}
|
||||
spaceToWrapAt = matcher.start() + offset;
|
||||
}
|
||||
|
||||
// only last line without leading spaces is left
|
||||
if (inputLineLength - offset <= wrapLength) {
|
||||
break;
|
||||
}
|
||||
|
||||
while (matcher.find()) {
|
||||
spaceToWrapAt = matcher.start() + offset;
|
||||
}
|
||||
|
||||
if (spaceToWrapAt >= offset) {
|
||||
// normal case
|
||||
wrappedLine.append(str, offset, spaceToWrapAt);
|
||||
wrappedLine.append(newLineStr);
|
||||
offset = spaceToWrapAt + 1;
|
||||
|
||||
} else // really long word or URL
|
||||
if (wrapLongWords) {
|
||||
// wrap really long word one line at a time
|
||||
wrappedLine.append(str, offset, wrapLength + offset);
|
||||
wrappedLine.append(newLineStr);
|
||||
offset += wrapLength;
|
||||
} else {
|
||||
// do not wrap really long word, just extend beyond limit
|
||||
matcher = patternToWrapOn.matcher(str.substring(offset + wrapLength));
|
||||
if (matcher.find()) {
|
||||
spaceToWrapAt = matcher.start() + offset + wrapLength;
|
||||
}
|
||||
|
||||
if (spaceToWrapAt >= 0) {
|
||||
wrappedLine.append(str, offset, spaceToWrapAt);
|
||||
wrappedLine.append(newLineStr);
|
||||
offset = spaceToWrapAt + 1;
|
||||
} else {
|
||||
wrappedLine.append(str, offset, str.length());
|
||||
offset = inputLineLength;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Whatever is left in line is short enough to just pass through
|
||||
wrappedLine.append(str, offset, str.length());
|
||||
|
||||
return wrappedLine.toString();
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (c) 2026 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.docsutil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class DocsDirectory {
|
||||
|
||||
final Map<String, DocsSection> map = new HashMap<>();
|
||||
|
||||
DocsDirectory() {
|
||||
}
|
||||
|
||||
public <T> T addFile(String name, IDocConfLoader<T> provider) throws IOException {
|
||||
DocsSection section = map.get(name);
|
||||
if (section == null) {
|
||||
map.put(name, section = new DocsSection());
|
||||
}
|
||||
return provider.call(section);
|
||||
}
|
||||
|
||||
public interface IDocConfLoader<T> {
|
||||
T call(IEaglerConfSection section) throws IOException;
|
||||
}
|
||||
|
||||
}
|
||||
+360
@@ -0,0 +1,360 @@
|
||||
/*
|
||||
* Copyright (c) 2026 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.docsutil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DocsGenerator {
|
||||
|
||||
private final String title;
|
||||
private final String desc;
|
||||
private final Map<String, DocsDirectory> map = new HashMap<>();
|
||||
private String primaryFile;
|
||||
|
||||
public DocsGenerator(String title, String desc) {
|
||||
this.title = title;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public void addPlatform(String platform, IDocDirLoader provider) throws IOException {
|
||||
DocsDirectory directory = map.get(platform);
|
||||
if (directory == null) {
|
||||
map.put(platform, directory = new DocsDirectory());
|
||||
}
|
||||
provider.call(directory);
|
||||
}
|
||||
|
||||
public interface IDocDirLoader {
|
||||
void call(DocsDirectory dir) throws IOException;
|
||||
}
|
||||
|
||||
private static void flatten(Map<String, Header> headers, Map<String, Content> content, String platf, String pfx,
|
||||
String title, DocsSection section) {
|
||||
for (Map.Entry<String, Object> etr : section.entries.entrySet()) {
|
||||
if (etr.getValue() instanceof DocsValue v) {
|
||||
flatten(content, platf, pfx + etr.getKey(), title + "`" + etr.getKey() + "`", v);
|
||||
} else if (etr.getValue() instanceof DocsSection v) {
|
||||
Header headerObj = new Header(title + "`" + etr.getKey() + "`");
|
||||
headerObj.platforms.add(platf);
|
||||
if (v.comment != null && !v.comment.isEmpty()) {
|
||||
Summary summaryObj = new Summary(v.comment);
|
||||
summaryObj.platforms.add(platf);
|
||||
headerObj.summary.add(summaryObj);
|
||||
}
|
||||
merge(headers, pfx + etr.getKey(), headerObj);
|
||||
flatten(headers, content, platf, pfx + etr.getKey() + ":", title + "`" + etr.getKey() + "` : ", v);
|
||||
} else if (etr.getValue() instanceof DocsList v) {
|
||||
flatten(headers, content, platf, pfx + etr.getKey(), title + "`" + etr.getKey() + "`", v);
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void flatten(Map<String, Header> headers, Map<String, Content> content, String platf, String pfx,
|
||||
String title, DocsList section) {
|
||||
boolean hasHeader = false;
|
||||
List<String> values = new ArrayList<>();
|
||||
for (Object etr : section.entries) {
|
||||
if (etr instanceof DocsValue v) {
|
||||
values.add(v.value == null ? "null" :
|
||||
v.type != DocsValue.Type.STR ? v.value :
|
||||
("\"" + v.value.replace("\\", "\\\\").replace("\"", "\\\"") + "\""));
|
||||
} else if (etr instanceof DocsSection v) {
|
||||
int idx = title.lastIndexOf(": ");
|
||||
if (idx == -1) {
|
||||
idx = title.lastIndexOf("> ") + 3;
|
||||
}
|
||||
String t2 = title.substring(0, idx + 2) + "[" + title.substring(idx + 2) + "]";
|
||||
flatten(headers, content, platf, pfx + ":{}:", t2 + " : ", v);
|
||||
hasHeader = true;
|
||||
} else if (etr instanceof DocsList v) {
|
||||
int idx = title.lastIndexOf(": ");
|
||||
if (idx == -1) {
|
||||
idx = title.lastIndexOf("> ") + 3;
|
||||
}
|
||||
String t2 = title.substring(0, idx + 2) + "[" + title.substring(idx + 2) + "]";
|
||||
flatten(headers, content, platf, pfx + ":[]:", t2, v);
|
||||
hasHeader = true;
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
if (hasHeader) {
|
||||
int idx = title.lastIndexOf(": ");
|
||||
if (idx == -1) {
|
||||
idx = title.lastIndexOf("> ") + 3;
|
||||
}
|
||||
String t2 = title.substring(0, idx + 2) + "[" + title.substring(idx + 2) + "]";
|
||||
Header headerObj = new Header(t2);
|
||||
headerObj.platforms.add(platf);
|
||||
if (section.comment != null && !section.comment.isEmpty()) {
|
||||
Summary summaryObj = new Summary(section.comment);
|
||||
summaryObj.platforms.add(platf);
|
||||
headerObj.summary.add(summaryObj);
|
||||
}
|
||||
merge(headers, pfx, headerObj);
|
||||
} else {
|
||||
Content contentObj = new Content(title);
|
||||
contentObj.platforms.add(platf);
|
||||
if (section.comment != null && !section.comment.isEmpty()) {
|
||||
Summary summaryObj = new Summary(section.comment);
|
||||
summaryObj.platforms.add(platf);
|
||||
contentObj.summary.add(summaryObj);
|
||||
}
|
||||
Defaults defaultsObj = new Defaults("[ " + String.join(", ", values) + " ]");
|
||||
defaultsObj.platforms.add(platf);
|
||||
contentObj.defaults.add(defaultsObj);
|
||||
merge(content, pfx, contentObj);
|
||||
}
|
||||
}
|
||||
|
||||
private static void flatten(Map<String, Content> content, String platf, String pfx, String title,
|
||||
DocsValue value) {
|
||||
Content contentObj = new Content(title);
|
||||
contentObj.platforms.add(platf);
|
||||
if (value.comment != null && !value.comment.isEmpty()) {
|
||||
Summary summaryObj = new Summary(value.comment);
|
||||
summaryObj.platforms.add(platf);
|
||||
contentObj.summary.add(summaryObj);
|
||||
}
|
||||
if (!value.randomized) {
|
||||
Defaults defaultsObj = new Defaults(
|
||||
value.value == null ? "null" :
|
||||
value.type != DocsValue.Type.STR ? value.value :
|
||||
("\"" + value.value.replace("\\", "\\\\").replace("\"", "\\\"") + "\""));
|
||||
defaultsObj.platforms.add(platf);
|
||||
contentObj.defaults.add(defaultsObj);
|
||||
}
|
||||
merge(content, pfx, contentObj);
|
||||
}
|
||||
|
||||
private static void merge(Map<String, Header> headers, String pfx, Header item) {
|
||||
Header existing = headers.get(pfx);
|
||||
if (existing == null) {
|
||||
headers.put(pfx, item);
|
||||
return;
|
||||
}
|
||||
for (String str : item.platforms) {
|
||||
if (!existing.platforms.contains(str)) {
|
||||
existing.platforms.add(str);
|
||||
}
|
||||
}
|
||||
eag: for (Summary str1 : item.summary) {
|
||||
for (Summary str2 : existing.summary) {
|
||||
if (str1.summary.equals(str2.summary)) {
|
||||
for (String str : str1.platforms) {
|
||||
if (!str2.platforms.contains(str)) {
|
||||
str2.platforms.add(str);
|
||||
}
|
||||
}
|
||||
continue eag;
|
||||
}
|
||||
}
|
||||
existing.summary.add(str1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void merge(Map<String, Content> content, String id, Content item) {
|
||||
Content existing = content.get(id);
|
||||
if (existing == null) {
|
||||
content.put(id, item);
|
||||
return;
|
||||
}
|
||||
for (String str : item.platforms) {
|
||||
if (!existing.platforms.contains(str)) {
|
||||
existing.platforms.add(str);
|
||||
}
|
||||
}
|
||||
eag: for (Summary str1 : item.summary) {
|
||||
for (Summary str2 : existing.summary) {
|
||||
if (str1.summary.equals(str2.summary)) {
|
||||
for (String str : str1.platforms) {
|
||||
if (!str2.platforms.contains(str)) {
|
||||
str2.platforms.add(str);
|
||||
}
|
||||
}
|
||||
continue eag;
|
||||
}
|
||||
}
|
||||
existing.summary.add(str1);
|
||||
}
|
||||
eag: for (Defaults str1 : item.defaults) {
|
||||
for (Defaults str2 : existing.defaults) {
|
||||
if (str1.defaults.equals(str2.defaults)) {
|
||||
for (String str : str1.platforms) {
|
||||
if (!str2.platforms.contains(str)) {
|
||||
str2.platforms.add(str);
|
||||
}
|
||||
}
|
||||
continue eag;
|
||||
}
|
||||
}
|
||||
existing.defaults.add(str1);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeDocs(PrintWriter writer) {
|
||||
List<String> sortedPlatforms = new ArrayList<>(map.keySet());
|
||||
Collections.sort(sortedPlatforms);
|
||||
Map<String, Header> headers = new HashMap<>();
|
||||
Map<String, Content> sections = new HashMap<>();
|
||||
|
||||
for (String platfName : sortedPlatforms) {
|
||||
DocsDirectory platf = map.get(platfName);
|
||||
for (Map.Entry<String, DocsSection> dir : platf.map.entrySet()) {
|
||||
flatten(headers, sections, platfName, dir.getKey() + ">",
|
||||
"`/" + dir.getKey() + ".cfg` > ", dir.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
List<Map.Entry<String, Content>> sortedSections = new ArrayList<>(sections.entrySet());
|
||||
Collections.sort(sortedSections, Map.Entry.comparingByKey());
|
||||
|
||||
if (primaryFile != null) {
|
||||
String pfx = primaryFile + ">";
|
||||
List<Map.Entry<String, Content>> tmp = new ArrayList<>(sortedSections.size());
|
||||
for (Map.Entry<String, Content> etr : sortedSections) {
|
||||
if (etr.getKey().startsWith(pfx)) {
|
||||
tmp.add(etr);
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, Content> etr : sortedSections) {
|
||||
if (!etr.getKey().startsWith(pfx)) {
|
||||
tmp.add(etr);
|
||||
}
|
||||
}
|
||||
sortedSections = tmp;
|
||||
}
|
||||
|
||||
writer.println("## " + title);
|
||||
for (String str : desc.split("(\\r\\n|\\r|\\n)")) {
|
||||
writer.println(str);
|
||||
}
|
||||
|
||||
writer.println();
|
||||
writer.println("*(Placeholder extension \".cfg\" replaced with \".yaml\", \".toml\", or \".gson\")*");
|
||||
|
||||
String vigg = null;
|
||||
for (Map.Entry<String, Content> etr : sortedSections) {
|
||||
String key = etr.getKey();
|
||||
int idx = key.lastIndexOf(':');
|
||||
if (idx != -1 && (vigg == null || vigg.length() != idx || !vigg.regionMatches(0, key, 0, idx))) {
|
||||
vigg = key.substring(0, idx);
|
||||
idx = -1;
|
||||
while ((idx = key.indexOf(':', idx + 1)) != -1) {
|
||||
Header head = headers.remove(key.substring(0, idx));
|
||||
if (head != null) {
|
||||
writer.println();
|
||||
writer.println("## " + head.title + " <sub>(" + String.join(", ", head.platforms) + ")</sub>");
|
||||
if (!head.summary.isEmpty()) {
|
||||
writer.println("**Summary:**");
|
||||
for (Summary sum : head.summary) {
|
||||
writer.println("- " + sum.summary + " <sub>(" + String.join(", ", sum.platforms) + ")</sub>");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Content sec = etr.getValue();
|
||||
writer.println();
|
||||
writer.println("## <small>" + sec.title + " <sub>(" + String.join(", ", sec.platforms) + ")</sub></small>");
|
||||
if (!sec.summary.isEmpty()) {
|
||||
writer.println("**Summary:**");
|
||||
for (Summary sum : sec.summary) {
|
||||
writer.println("- " + sum.summary + " <sub>(" + String.join(", ", sum.platforms) + ")</sub>");
|
||||
}
|
||||
}
|
||||
if (!sec.defaults.isEmpty()) {
|
||||
if (!sec.summary.isEmpty()) {
|
||||
writer.println();
|
||||
}
|
||||
writer.println("**Defaults:**");
|
||||
for (Defaults def : sec.defaults) {
|
||||
writer.println("- `" + def.defaults + "` <sub>(" + String.join(", ", def.platforms) + ")</sub>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public void setPrimaryFile(String primaryFile) {
|
||||
this.primaryFile = primaryFile;
|
||||
}
|
||||
|
||||
private static class Header {
|
||||
|
||||
private final String title;
|
||||
private final List<String> platforms;
|
||||
private final List<Summary> summary;
|
||||
|
||||
private Header(String title) {
|
||||
this.title = title;
|
||||
this.platforms = new ArrayList<>();
|
||||
this.summary = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Content {
|
||||
|
||||
private final String title;
|
||||
private final List<String> platforms;
|
||||
private final List<Summary> summary;
|
||||
private final List<Defaults> defaults;
|
||||
|
||||
private Content(String title) {
|
||||
this.title = title;
|
||||
this.platforms = new ArrayList<>();
|
||||
this.summary = new ArrayList<>();
|
||||
this.defaults = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Summary {
|
||||
|
||||
private final String summary;
|
||||
private final List<String> platforms;
|
||||
|
||||
private Summary(String summary) {
|
||||
this.summary = summary;
|
||||
this.platforms = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class Defaults {
|
||||
|
||||
private final String defaults;
|
||||
private final List<String> platforms;
|
||||
|
||||
private Defaults(String defaults) {
|
||||
this.defaults = defaults;
|
||||
this.platforms = new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) 2026 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.docsutil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
class DocsList implements IEaglerConfList {
|
||||
|
||||
final List<Object> entries = new ArrayList<>();
|
||||
private boolean initialized;
|
||||
String comment;
|
||||
|
||||
DocsList() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection appendSection() {
|
||||
DocsSection object = new DocsSection();
|
||||
entries.add(object);
|
||||
initialized = true;
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList appendList() {
|
||||
DocsList object = new DocsList();
|
||||
entries.add(object);
|
||||
initialized = true;
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInteger(int value) {
|
||||
DocsValue object = new DocsValue(DocsValue.Type.INT, Integer.toString(value), null, false);
|
||||
entries.add(object);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendString(String string) {
|
||||
DocsValue object = new DocsValue(DocsValue.Type.STR, string, null, false);
|
||||
entries.add(object);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return entries.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(int index) {
|
||||
if (index < 0 || index >= entries.size())
|
||||
return null;
|
||||
if (entries.get(index) instanceof DocsSection s) {
|
||||
return s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(int index) {
|
||||
if (index < 0 || index >= entries.size())
|
||||
return null;
|
||||
if (entries.get(index) instanceof DocsList l) {
|
||||
return l;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(int index) {
|
||||
if (index < 0 || index >= entries.size())
|
||||
return false;
|
||||
if (entries.get(index) instanceof DocsValue v) {
|
||||
return v.type == DocsValue.Type.INT;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIfInteger(int index, int defaultVal) {
|
||||
if (index < 0 || index >= entries.size())
|
||||
return defaultVal;
|
||||
if (entries.get(index) instanceof DocsValue v && v.type == DocsValue.Type.INT) {
|
||||
return Integer.parseInt(v.value);
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(int index) {
|
||||
if (index < 0 || index >= entries.size())
|
||||
return false;
|
||||
if (entries.get(index) instanceof DocsValue v) {
|
||||
return v.type == DocsValue.Type.STR;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(int index, String defaultVal) {
|
||||
if (index < 0 || index >= entries.size())
|
||||
return defaultVal;
|
||||
if (entries.get(index) instanceof DocsValue v && v.type == DocsValue.Type.STR) {
|
||||
return v.value;
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
/*
|
||||
* Copyright (c) 2026 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.docsutil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IRandomSupplier;
|
||||
|
||||
class DocsSection implements IEaglerConfSection {
|
||||
|
||||
final Map<String, Object> entries = new HashMap<>();
|
||||
private boolean initialized;
|
||||
String comment;
|
||||
|
||||
DocsSection() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(String name) {
|
||||
Object obj = entries.get(name);
|
||||
if (obj instanceof DocsSection s) {
|
||||
return s;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getSection(String name) {
|
||||
if (entries.get(name) instanceof DocsSection s) {
|
||||
return s;
|
||||
} else {
|
||||
DocsSection sec = new DocsSection();
|
||||
entries.put(name, sec);
|
||||
initialized = true;
|
||||
return sec;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(String name) {
|
||||
Object obj = entries.get(name);
|
||||
if (obj instanceof DocsList l) {
|
||||
return l;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getList(String name) {
|
||||
if (entries.get(name) instanceof DocsList l) {
|
||||
return l;
|
||||
} else {
|
||||
DocsList lst = new DocsList();
|
||||
entries.put(name, lst);
|
||||
initialized = true;
|
||||
return lst;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getKeys() {
|
||||
return ImmutableList.copyOf(entries.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBoolean(String name) {
|
||||
if (entries.get(name) instanceof DocsValue v) {
|
||||
return v.type == DocsValue.Type.BOOL;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.BOOL) {
|
||||
return Boolean.parseBoolean(v.value);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, boolean defaultValue, String comment) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.BOOL) {
|
||||
return Boolean.parseBoolean(v.value);
|
||||
} else {
|
||||
DocsValue val = new DocsValue(DocsValue.Type.BOOL, Boolean.toString(defaultValue), comment, false);
|
||||
entries.put(name, val);
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, Supplier<Boolean> defaultValue, String comment) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.BOOL) {
|
||||
return Boolean.parseBoolean(v.value);
|
||||
} else {
|
||||
boolean def = defaultValue.get();
|
||||
DocsValue val = new DocsValue(DocsValue.Type.BOOL, Boolean.toString(def), comment,
|
||||
defaultValue instanceof IRandomSupplier);
|
||||
entries.put(name, val);
|
||||
initialized = true;
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(String name) {
|
||||
if (entries.get(name) instanceof DocsValue v) {
|
||||
return v.type == DocsValue.Type.INT;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, int defaultValue, String comment) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.INT) {
|
||||
return Integer.parseInt(v.value);
|
||||
} else {
|
||||
DocsValue val = new DocsValue(DocsValue.Type.INT, Integer.toString(defaultValue), comment, false);
|
||||
entries.put(name, val);
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, Supplier<Integer> defaultValue, String comment) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.INT) {
|
||||
return Integer.parseInt(v.value);
|
||||
} else {
|
||||
int def = defaultValue.get();
|
||||
DocsValue val = new DocsValue(DocsValue.Type.INT, Integer.toString(def), comment,
|
||||
defaultValue instanceof IRandomSupplier);
|
||||
entries.put(name, val);
|
||||
initialized = true;
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(String name) {
|
||||
if (entries.get(name) instanceof DocsValue v) {
|
||||
return v.type == DocsValue.Type.STR;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(String name) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.STR) {
|
||||
return v.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, String defaultValue, String comment) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.STR) {
|
||||
return v.value;
|
||||
} else {
|
||||
DocsValue val = new DocsValue(DocsValue.Type.STR, defaultValue, comment, false);
|
||||
entries.put(name, val);
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, Supplier<String> defaultValue, String comment) {
|
||||
if (entries.get(name) instanceof DocsValue v && v.type == DocsValue.Type.STR) {
|
||||
return v.value;
|
||||
} else {
|
||||
String def = defaultValue.get();
|
||||
DocsValue val = new DocsValue(DocsValue.Type.STR, def, comment,
|
||||
defaultValue instanceof IRandomSupplier);
|
||||
entries.put(name, val);
|
||||
initialized = true;
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2026 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.docsutil;
|
||||
|
||||
class DocsValue {
|
||||
|
||||
static enum Type {
|
||||
BOOL, INT, STR
|
||||
}
|
||||
|
||||
final Type type;
|
||||
final String value;
|
||||
final String comment;
|
||||
final boolean randomized;
|
||||
|
||||
DocsValue(Type type, String value, String comment, boolean randomized) {
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
this.comment = comment;
|
||||
this.randomized = randomized;
|
||||
}
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.gson;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.EnumConfigFormat;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
|
||||
public class GSONConfigBase implements IEaglerConfig {
|
||||
|
||||
final File file;
|
||||
GSONConfigSection root;
|
||||
boolean modified;
|
||||
|
||||
GSONConfigBase(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumConfigFormat getFormat() {
|
||||
return EnumConfigFormat.JSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveIfModified() throws IOException {
|
||||
if (modified) {
|
||||
GSONConfigLoader.writeConfigFile(root.json, file);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.gson;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class GSONConfigList implements IEaglerConfList {
|
||||
|
||||
private final GSONConfigBase owner;
|
||||
final JsonArray json;
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
public GSONConfigList(GSONConfigBase owner, JsonArray json, boolean exists) {
|
||||
this.owner = owner;
|
||||
this.json = json;
|
||||
this.exists = this.initialized = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection appendSection() {
|
||||
JsonObject object = new JsonObject();
|
||||
json.add(object);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new GSONConfigSection(owner, object, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList appendList() {
|
||||
JsonArray array = new JsonArray();
|
||||
json.add(array);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new GSONConfigList(owner, array, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInteger(int value) {
|
||||
json.add(value);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendString(String string) {
|
||||
json.add(string);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return json.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(int index) {
|
||||
if (index < 0 || index >= json.size())
|
||||
return null;
|
||||
JsonElement el = json.get(index);
|
||||
if (el != null && el.isJsonObject()) {
|
||||
return new GSONConfigSection(owner, el.getAsJsonObject(), true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(int index) {
|
||||
if (index < 0 || index >= json.size())
|
||||
return null;
|
||||
JsonElement el = json.get(index);
|
||||
if (el != null && el.isJsonArray()) {
|
||||
return new GSONConfigList(owner, el.getAsJsonArray(), true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(int index) {
|
||||
if (index < 0 || index >= json.size())
|
||||
return false;
|
||||
JsonElement el = json.get(index);
|
||||
return el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIfInteger(int index, int defaultVal) {
|
||||
if (index < 0 || index >= json.size())
|
||||
return defaultVal;
|
||||
JsonElement el = json.get(index);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber()) {
|
||||
return el.getAsInt();
|
||||
} else {
|
||||
return defaultVal;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(int index) {
|
||||
if (index < 0 || index >= json.size())
|
||||
return false;
|
||||
JsonElement el = json.get(index);
|
||||
return el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(int index, String defaultVal) {
|
||||
if (index < 0 || index >= json.size())
|
||||
return defaultVal;
|
||||
JsonElement el = json.get(index);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber()) {
|
||||
return el.getAsString();
|
||||
} else {
|
||||
return defaultVal;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.gson;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
import net.lax1dude.eaglercraft.backend.server.util.GsonLenient;
|
||||
|
||||
public class GSONConfigLoader {
|
||||
|
||||
private static final Gson GSON = GsonLenient.setLenient(new GsonBuilder()).setPrettyPrinting().serializeNulls()
|
||||
.create();
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file) throws IOException {
|
||||
JsonObject obj;
|
||||
try (Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
|
||||
obj = GSON.fromJson(reader, JsonObject.class);
|
||||
} catch (FileNotFoundException ex) {
|
||||
obj = new JsonObject();
|
||||
} catch (JsonParseException ex) {
|
||||
throw new IOException("JSON config file has a syntax error: " + file.getAbsolutePath(), ex);
|
||||
}
|
||||
return getConfigFile(file, obj);
|
||||
}
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file, JsonObject jsonObject) throws IOException {
|
||||
GSONConfigBase base = new GSONConfigBase(file);
|
||||
base.root = new GSONConfigSection(base, jsonObject, jsonObject.size() > 0);
|
||||
return base;
|
||||
}
|
||||
|
||||
public static void writeConfigFile(JsonObject configIn, File file) throws IOException {
|
||||
File p = file.getAbsoluteFile().getParentFile();
|
||||
if (p != null && !p.isDirectory() && !p.mkdirs()) {
|
||||
throw new IOException("Could not create directory: " + p.getAbsolutePath());
|
||||
}
|
||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
|
||||
GSON.toJson(configIn, writer);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+228
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.gson;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class GSONConfigSection implements IEaglerConfSection {
|
||||
|
||||
private final GSONConfigBase owner;
|
||||
final JsonObject json;
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
protected GSONConfigSection(GSONConfigBase owner, JsonObject json, boolean exists) {
|
||||
this.owner = owner;
|
||||
this.json = json;
|
||||
this.exists = this.initialized = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonObject()) {
|
||||
return new GSONConfigSection(owner, el.getAsJsonObject(), true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getSection(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonObject()) {
|
||||
return new GSONConfigSection(owner, el.getAsJsonObject(), true);
|
||||
} else {
|
||||
JsonObject obj = new JsonObject();
|
||||
json.add(name, obj);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new GSONConfigSection(owner, obj, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonArray()) {
|
||||
return new GSONConfigList(owner, el.getAsJsonArray(), true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getList(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonArray()) {
|
||||
return new GSONConfigList(owner, el.getAsJsonArray(), true);
|
||||
} else {
|
||||
JsonArray obj = new JsonArray();
|
||||
json.add(name, obj);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new GSONConfigList(owner, obj, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getKeys() {
|
||||
return ImmutableList.copyOf(
|
||||
json.entrySet().stream().map(Map.Entry<String, JsonElement>::getKey).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBoolean(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
return el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
return el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isBoolean() && el.getAsBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, boolean defaultValue, String comment) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isBoolean()) {
|
||||
return el.getAsBoolean();
|
||||
} else {
|
||||
json.addProperty(name, defaultValue);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, Supplier<Boolean> defaultValue, String comment) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isBoolean()) {
|
||||
return el.getAsBoolean();
|
||||
} else {
|
||||
boolean b = defaultValue.get();
|
||||
json.addProperty(name, b);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
return el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, int defaultValue, String comment) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber()) {
|
||||
return el.getAsJsonPrimitive().getAsInt();
|
||||
} else {
|
||||
json.addProperty(name, defaultValue);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, Supplier<Integer> defaultValue, String comment) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isNumber()) {
|
||||
return el.getAsJsonPrimitive().getAsInt();
|
||||
} else {
|
||||
Integer i = defaultValue.get();
|
||||
json.addProperty(name, i);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
return el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(String name) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
|
||||
return el.getAsString();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, String defaultValue, String comment) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
|
||||
return el.getAsString();
|
||||
} else {
|
||||
json.addProperty(name, defaultValue);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, Supplier<String> defaultValue, String comment) {
|
||||
JsonElement el = json.get(name);
|
||||
if (el != null && el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) {
|
||||
return el.getAsString();
|
||||
} else {
|
||||
String d = defaultValue.get();
|
||||
json.addProperty(name, d);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.nightconfig;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.EnumConfigFormat;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
|
||||
public class NightConfigBase implements IEaglerConfig {
|
||||
|
||||
NightConfigSection root;
|
||||
boolean modified;
|
||||
|
||||
@Override
|
||||
public EnumConfigFormat getFormat() {
|
||||
return EnumConfigFormat.TOML;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveIfModified() throws IOException {
|
||||
if (modified) {
|
||||
NightConfigLoader.writeConfigFile((CommentedFileConfig) root.config);
|
||||
modified = false;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.nightconfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.electronwill.nightconfig.core.CommentedConfig;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class NightConfigList implements IEaglerConfList {
|
||||
|
||||
private final NightConfigBase owner;
|
||||
private final List<Object> data;
|
||||
private final IContext commentSetter;
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
public interface IContext {
|
||||
void setComment(String comment);
|
||||
|
||||
CommentedConfig genSection();
|
||||
}
|
||||
|
||||
public NightConfigList(NightConfigBase owner, List<Object> list, IContext commentSetter, boolean exists) {
|
||||
this.owner = owner;
|
||||
this.data = list;
|
||||
this.commentSetter = commentSetter;
|
||||
this.exists = this.initialized = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
commentSetter.setComment(NightConfigLoader.createComment(comment));
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection appendSection() {
|
||||
CommentedConfig conf = commentSetter.genSection();
|
||||
data.add(conf);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new NightConfigSection(owner, conf, null, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList appendList() {
|
||||
List<Object> list = new ArrayList<>();
|
||||
data.add(list);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new NightConfigList(owner, list, new IContext() {
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommentedConfig genSection() {
|
||||
return commentSetter.genSection();
|
||||
}
|
||||
}, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInteger(int value) {
|
||||
data.add(value);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendString(String string) {
|
||||
data.add(string);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return data.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(int index) {
|
||||
if (index < 0 || index >= data.size())
|
||||
return null;
|
||||
Object val = data.get(index);
|
||||
return (val instanceof CommentedConfig vall) ? new NightConfigSection(owner, vall, null, true) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(int index) {
|
||||
if (index < 0 || index >= data.size())
|
||||
return null;
|
||||
Object val = data.get(index);
|
||||
return (val instanceof List) ? new NightConfigList(owner, (List<Object>) val, new IContext() {
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommentedConfig genSection() {
|
||||
return commentSetter.genSection();
|
||||
}
|
||||
}, true) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(int index) {
|
||||
if (index < 0 || index >= data.size())
|
||||
return false;
|
||||
return (data.get(index) instanceof Number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIfInteger(int index, int defaultVal) {
|
||||
if (index < 0 || index >= data.size())
|
||||
return defaultVal;
|
||||
Object val = data.get(index);
|
||||
return (val instanceof Number num) ? num.intValue() : defaultVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(int index) {
|
||||
if (index < 0 || index >= data.size())
|
||||
return false;
|
||||
return (data.get(index) instanceof String);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(int index, String defaultVal) {
|
||||
if (index < 0 || index >= data.size())
|
||||
return defaultVal;
|
||||
Object val = data.get(index);
|
||||
return (val instanceof String str) ? str : defaultVal;
|
||||
}
|
||||
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.nightconfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.electronwill.nightconfig.core.CommentedConfig;
|
||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.WrapUtil;
|
||||
|
||||
public class NightConfigLoader {
|
||||
|
||||
public static final int TOML_COMMENT_WRAP = 80;
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file) throws IOException {
|
||||
CommentedFileConfig config = null;
|
||||
try {
|
||||
config = CommentedFileConfig.builder(file).sync().preserveInsertionOrder()
|
||||
.onFileNotFound(FileNotFoundAction.READ_NOTHING).build();
|
||||
config.load();
|
||||
} catch (Exception ex) {
|
||||
try {
|
||||
if (config != null) {
|
||||
config.close();
|
||||
}
|
||||
} catch (Exception exx) {
|
||||
}
|
||||
throw new IOException("Failed to load config file: " + file.getAbsolutePath());
|
||||
}
|
||||
return getConfigFile(config);
|
||||
}
|
||||
|
||||
public static IEaglerConfig getConfigFile(CommentedConfig config) {
|
||||
NightConfigBase ret = new NightConfigBase();
|
||||
ret.root = new NightConfigSection(ret, config, null, config.size() > 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void writeConfigFile(CommentedFileConfig configIn) throws IOException {
|
||||
File p = configIn.getFile().getAbsoluteFile().getParentFile();
|
||||
if (p != null && !p.isDirectory() && !p.mkdirs()) {
|
||||
throw new IOException("Could not create directory: " + p.getAbsolutePath());
|
||||
}
|
||||
try {
|
||||
try {
|
||||
configIn.save();
|
||||
} finally {
|
||||
configIn.close();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new IOException("Failed to save config file: " + configIn.getFile().getAbsolutePath(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static String createComment(String stringIn) {
|
||||
if (stringIn != null) {
|
||||
return " " + WrapUtil.wrap(stringIn, TOML_COMMENT_WRAP, System.lineSeparator() + " ", false, " ");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.nightconfig;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.electronwill.nightconfig.core.CommentedConfig;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class NightConfigSection implements IEaglerConfSection {
|
||||
|
||||
private final NightConfigBase owner;
|
||||
final CommentedConfig config;
|
||||
private final Consumer<String> commentSetter;
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
public NightConfigSection(NightConfigBase owner, CommentedConfig config, Consumer<String> commentSetter,
|
||||
boolean exists) {
|
||||
this.owner = owner;
|
||||
this.config = config;
|
||||
this.commentSetter = commentSetter;
|
||||
this.exists = this.initialized = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
if (commentSetter != null) {
|
||||
commentSetter.accept(NightConfigLoader.createComment(comment));
|
||||
owner.modified = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof CommentedConfig o2)
|
||||
? new NightConfigSection(owner, o2, (str) -> config.setComment(k, str), true)
|
||||
: null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getSection(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof CommentedConfig o2) {
|
||||
return new NightConfigSection(owner, o2, (str) -> config.setComment(k, str), true);
|
||||
} else {
|
||||
CommentedConfig sub = config.createSubConfig();
|
||||
config.set(k, sub);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new NightConfigSection(owner, sub, (str) -> config.setComment(k, str), false);
|
||||
}
|
||||
}
|
||||
|
||||
private NightConfigList.IContext bindListContext(List<String> key) {
|
||||
return new NightConfigList.IContext() {
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
config.setComment(key, comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommentedConfig genSection() {
|
||||
return config.createSubConfig();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof List) ? new NightConfigList(owner, (List<Object>) o, bindListContext(k), true) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getList(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof List) {
|
||||
return new NightConfigList(owner, (List<Object>) o, bindListContext(k), true);
|
||||
} else {
|
||||
List<Object> sub = new ArrayList<>();
|
||||
config.set(k, sub);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new NightConfigList(owner, sub, bindListContext(k), false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getKeys() {
|
||||
return ImmutableList.copyOf(config.valueMap().keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBoolean(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof Boolean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof Boolean o2) && o2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, boolean defaultValue, String comment) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof Boolean o2) {
|
||||
return o2;
|
||||
} else {
|
||||
config.set(k, defaultValue);
|
||||
if (comment != null) {
|
||||
config.setComment(k, NightConfigLoader.createComment(comment));
|
||||
}
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, Supplier<Boolean> defaultValue, String comment) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof Boolean o2) {
|
||||
return o2;
|
||||
} else {
|
||||
Boolean d = defaultValue.get();
|
||||
config.set(k, d);
|
||||
if (comment != null) {
|
||||
config.setComment(k, NightConfigLoader.createComment(comment));
|
||||
}
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof Number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, int defaultValue, String comment) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof Number o2) {
|
||||
return o2.intValue();
|
||||
} else {
|
||||
config.set(k, defaultValue);
|
||||
if (comment != null) {
|
||||
config.setComment(k, NightConfigLoader.createComment(comment));
|
||||
}
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, Supplier<Integer> defaultValue, String comment) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof Number o2) {
|
||||
return o2.intValue();
|
||||
} else {
|
||||
Integer d = defaultValue.get();
|
||||
config.set(k, d);
|
||||
if (comment != null) {
|
||||
config.setComment(k, NightConfigLoader.createComment(comment));
|
||||
}
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof String);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(String name) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
return (o instanceof String str) ? str : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, String defaultValue, String comment) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof String str) {
|
||||
return str;
|
||||
} else {
|
||||
config.set(k, defaultValue);
|
||||
if (comment != null) {
|
||||
config.setComment(k, NightConfigLoader.createComment(comment));
|
||||
}
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, Supplier<String> defaultValue, String comment) {
|
||||
List<String> k = Collections.singletonList(name);
|
||||
Object o = config.get(k);
|
||||
if (o instanceof String str) {
|
||||
return str;
|
||||
} else {
|
||||
String d = defaultValue.get();
|
||||
config.set(k, d);
|
||||
if (comment != null) {
|
||||
config.setComment(k, NightConfigLoader.createComment(comment));
|
||||
}
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
|
||||
public class YAMLConfigLoader {
|
||||
|
||||
private static final boolean MODERN;
|
||||
|
||||
static {
|
||||
boolean b = false;
|
||||
try {
|
||||
Class.forName("org.yaml.snakeyaml.LoaderOptions").getMethod("setProcessComments", boolean.class);
|
||||
b = true;
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
}
|
||||
MODERN = b;
|
||||
}
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file) throws IOException {
|
||||
if (MODERN) {
|
||||
return net.lax1dude.eaglercraft.backend.server.config.snakeyaml.modern.YAMLConfigLoader.getConfigFile(file);
|
||||
} else {
|
||||
return net.lax1dude.eaglercraft.backend.server.config.snakeyaml.legacy.YAMLConfigLoader.getConfigFile(file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.legacy;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.yaml.snakeyaml.error.Mark;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.ReflectUtil;
|
||||
|
||||
class LegacyHelper {
|
||||
|
||||
private static final boolean HAS_FLOWSTYLE;
|
||||
private static final boolean HAS_SCALARSTYLE;
|
||||
|
||||
private static final Constructor<MappingNode> ctor_MappingNode;
|
||||
private static final Constructor<SequenceNode> ctor_SequenceNode;
|
||||
private static final Object flowStyle;
|
||||
private static final Constructor<ScalarNode> ctor_ScalarNode;
|
||||
private static final Method meth_createStyle;
|
||||
|
||||
static {
|
||||
Class<?> flowStyleClz = null;
|
||||
Class<?> scalarStyleClz = null;
|
||||
try {
|
||||
flowStyleClz = Class.forName("org.yaml.snakeyaml.DumperOptions$FlowStyle");
|
||||
} catch (ClassNotFoundException ex) {
|
||||
}
|
||||
try {
|
||||
scalarStyleClz = Class.forName("org.yaml.snakeyaml.DumperOptions$ScalarStyle");
|
||||
} catch (ClassNotFoundException ex) {
|
||||
}
|
||||
try {
|
||||
Constructor<MappingNode> _ctor_MappingNode = null;
|
||||
Constructor<SequenceNode> _ctor_SequenceNode = null;
|
||||
Object _flowStyle = null;
|
||||
if (flowStyleClz != null) {
|
||||
try {
|
||||
_ctor_MappingNode = MappingNode.class.getConstructor(Tag.class, List.class, flowStyleClz);
|
||||
_ctor_SequenceNode = SequenceNode.class.getConstructor(Tag.class, List.class, flowStyleClz);
|
||||
_flowStyle = Enum.valueOf((Class) flowStyleClz, "BLOCK");
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
flowStyleClz = null;
|
||||
}
|
||||
}
|
||||
if (flowStyleClz == null) {
|
||||
_ctor_MappingNode = MappingNode.class.getConstructor(Tag.class, List.class, Boolean.class);
|
||||
_ctor_SequenceNode = SequenceNode.class.getConstructor(Tag.class, List.class, Boolean.class);
|
||||
_flowStyle = null;
|
||||
}
|
||||
Constructor<ScalarNode> _ctor_ScalarNode = null;
|
||||
Method _meth_createStyle = null;
|
||||
if (scalarStyleClz != null) {
|
||||
try {
|
||||
_ctor_ScalarNode = ScalarNode.class.getConstructor(Tag.class, String.class, Mark.class, Mark.class,
|
||||
scalarStyleClz);
|
||||
_meth_createStyle = scalarStyleClz.getMethod("createStyle", Character.class);
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
scalarStyleClz = null;
|
||||
}
|
||||
}
|
||||
if (scalarStyleClz == null) {
|
||||
_ctor_ScalarNode = ScalarNode.class.getConstructor(Tag.class, String.class, Mark.class, Mark.class,
|
||||
Character.class);
|
||||
_meth_createStyle = null;
|
||||
}
|
||||
ctor_MappingNode = _ctor_MappingNode;
|
||||
ctor_SequenceNode = _ctor_SequenceNode;
|
||||
flowStyle = _flowStyle;
|
||||
ctor_ScalarNode = _ctor_ScalarNode;
|
||||
meth_createStyle = _meth_createStyle;
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw new ExceptionInInitializerError(ex);
|
||||
}
|
||||
HAS_FLOWSTYLE = flowStyleClz != null;
|
||||
HAS_SCALARSTYLE = scalarStyleClz != null;
|
||||
}
|
||||
|
||||
public static MappingNode mappingNode(Tag tag, List<NodeTuple> lst) {
|
||||
try {
|
||||
if (HAS_FLOWSTYLE) {
|
||||
return ctor_MappingNode.newInstance(tag, lst, flowStyle);
|
||||
} else {
|
||||
return ctor_MappingNode.newInstance(tag, lst, Boolean.FALSE);
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw ReflectUtil.propagateReflectThrowable(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static SequenceNode sequenceNode(Tag tag, List<Node> lst) {
|
||||
try {
|
||||
if (HAS_FLOWSTYLE) {
|
||||
return ctor_SequenceNode.newInstance(tag, lst, flowStyle);
|
||||
} else {
|
||||
return ctor_SequenceNode.newInstance(tag, lst, Boolean.FALSE);
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw ReflectUtil.propagateReflectThrowable(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static ScalarNode scalarNode(Tag tag, String value, Character style) {
|
||||
try {
|
||||
if (HAS_SCALARSTYLE) {
|
||||
return ctor_ScalarNode.newInstance(tag, value, null, null, meth_createStyle.invoke(null, style));
|
||||
} else {
|
||||
return ctor_ScalarNode.newInstance(tag, value, null, null, style);
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
throw ReflectUtil.propagateReflectThrowable(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void fixScalars(Node data, Field scalarStyle) {
|
||||
if (data instanceof ScalarNode s) {
|
||||
try {
|
||||
Character c = (Character) scalarStyle.get(s);
|
||||
if (c != null && (char) c == 0) {
|
||||
scalarStyle.set(s, null);
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
}
|
||||
} else if (data instanceof SequenceNode s) {
|
||||
for (Node n : s.getValue()) {
|
||||
fixScalars(n, scalarStyle);
|
||||
}
|
||||
} else if (data instanceof MappingNode s) {
|
||||
for (NodeTuple n : s.getValue()) {
|
||||
fixScalars(n.getKeyNode(), scalarStyle);
|
||||
fixScalars(n.getValueNode(), scalarStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.legacy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.EnumConfigFormat;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
|
||||
public class YAMLConfigBase implements IEaglerConfig {
|
||||
|
||||
final File file;
|
||||
YAMLConfigSection root;
|
||||
boolean modified;
|
||||
|
||||
YAMLConfigBase(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumConfigFormat getFormat() {
|
||||
return EnumConfigFormat.YAML;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveIfModified() throws IOException {
|
||||
if (modified) {
|
||||
YAMLConfigLoader.writeConfigFile(root.yaml, file);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.legacy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class YAMLConfigList implements IEaglerConfList {
|
||||
|
||||
private final YAMLConfigBase owner;
|
||||
final SequenceNode yaml;
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
public YAMLConfigList(YAMLConfigBase owner, SequenceNode yaml, boolean exists) {
|
||||
this.owner = owner;
|
||||
this.yaml = yaml;
|
||||
this.exists = this.initialized = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection appendSection() {
|
||||
MappingNode obj = LegacyHelper.mappingNode(Tag.MAP, new ArrayList<>());
|
||||
yaml.getValue().add(obj);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigSection(owner, obj, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList appendList() {
|
||||
SequenceNode obj = LegacyHelper.sequenceNode(Tag.SEQ, new ArrayList<>());
|
||||
yaml.getValue().add(obj);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigList(owner, obj, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInteger(int value) {
|
||||
yaml.getValue().add(LegacyHelper.scalarNode(Tag.INT, Integer.toString(value), null));
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendString(String string) {
|
||||
yaml.getValue().add(LegacyHelper.scalarNode(Tag.STR, string, '\''));
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return yaml.getValue().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return null;
|
||||
Node t = lst.get(index);
|
||||
if (t instanceof MappingNode tt) {
|
||||
return new YAMLConfigSection(owner, tt, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return null;
|
||||
Node t = lst.get(index);
|
||||
if (t instanceof SequenceNode tt) {
|
||||
return new YAMLConfigList(owner, tt, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return false;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode tt)) {
|
||||
try {
|
||||
Double.parseDouble(tt.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIfInteger(int index, int defaultVal) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return defaultVal;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode tt)) {
|
||||
try {
|
||||
return (int) Double.parseDouble(tt.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return false;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(int index, String defaultVal) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return defaultVal;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode tt)) {
|
||||
return tt.getValue();
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.legacy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
||||
import org.yaml.snakeyaml.error.YAMLException;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
import org.yaml.snakeyaml.representer.BaseRepresenter;
|
||||
import org.yaml.snakeyaml.representer.Represent;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
|
||||
public class YAMLConfigLoader {
|
||||
|
||||
private static final Yaml YAML;
|
||||
|
||||
static {
|
||||
DumperOptions dumpOpts = new DumperOptions();
|
||||
dumpOpts.setPrettyFlow(true);
|
||||
dumpOpts.setDefaultFlowStyle(FlowStyle.FLOW);
|
||||
Representer representer;
|
||||
try {
|
||||
representer = Representer.class.getConstructor(DumperOptions.class).newInstance(dumpOpts);
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
try {
|
||||
representer = Representer.class.getConstructor().newInstance();
|
||||
} catch (ReflectiveOperationException exx) {
|
||||
throw new ExceptionInInitializerError(exx);
|
||||
}
|
||||
}
|
||||
Field scalarStyleField = null;
|
||||
try {
|
||||
scalarStyleField = ScalarNode.class.getDeclaredField("style");
|
||||
if (scalarStyleField.getType() == Character.class) {
|
||||
scalarStyleField.setAccessible(true);
|
||||
} else {
|
||||
scalarStyleField = null;
|
||||
}
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
}
|
||||
final Field scalarStyleFieldF = scalarStyleField;
|
||||
try {
|
||||
Field f = BaseRepresenter.class.getDeclaredField("multiRepresenters");
|
||||
f.setAccessible(true);
|
||||
((Map<Class<?>, Represent>) f.get(representer)).put(Node.class, new Represent() {
|
||||
@Override
|
||||
public Node representData(Object data) {
|
||||
if (scalarStyleFieldF != null) {
|
||||
LegacyHelper.fixScalars((Node) data, scalarStyleFieldF);
|
||||
}
|
||||
return (Node) data;
|
||||
}
|
||||
});
|
||||
} catch (ReflectiveOperationException exx) {
|
||||
throw new ExceptionInInitializerError(exx);
|
||||
}
|
||||
YAML = new Yaml(representer, dumpOpts);
|
||||
}
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file) throws IOException {
|
||||
Node obj;
|
||||
try (Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
|
||||
synchronized (YAML) {
|
||||
obj = YAML.compose(reader);
|
||||
}
|
||||
} catch (FileNotFoundException ex) {
|
||||
obj = null;
|
||||
} catch (YAMLException ex) {
|
||||
throw new IOException("YAML config file has a syntax error: " + file.getAbsolutePath(), ex);
|
||||
}
|
||||
if (obj == null) {
|
||||
obj = LegacyHelper.mappingNode(Tag.MAP, new ArrayList<>());
|
||||
}
|
||||
if (!(obj instanceof MappingNode obj2)) {
|
||||
throw new IOException("Root node " + obj.getClass().getSimpleName() + " is not a map!");
|
||||
}
|
||||
return getConfigFile(file, obj2);
|
||||
}
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file, MappingNode node) throws IOException {
|
||||
YAMLConfigBase base = new YAMLConfigBase(file);
|
||||
base.root = new YAMLConfigSection(base, node, node.getValue().size() > 0);
|
||||
return base;
|
||||
}
|
||||
|
||||
public static void writeConfigFile(Node configIn, File file) throws IOException {
|
||||
File p = file.getAbsoluteFile().getParentFile();
|
||||
if (p != null && !p.isDirectory() && !p.mkdirs()) {
|
||||
throw new IOException("Could not create directory: " + p.getAbsolutePath());
|
||||
}
|
||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
|
||||
synchronized (YAML) {
|
||||
YAML.dump(configIn, writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+305
@@ -0,0 +1,305 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.legacy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class YAMLConfigSection implements IEaglerConfSection {
|
||||
|
||||
private final YAMLConfigBase owner;
|
||||
final MappingNode yaml;
|
||||
final Map<String, NodeTuple> accelerator = new HashMap<>();
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
protected YAMLConfigSection(YAMLConfigBase owner, MappingNode yaml, boolean exists) {
|
||||
this.owner = owner;
|
||||
this.yaml = yaml;
|
||||
this.exists = this.initialized = exists;
|
||||
rehash();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public void rehash() {
|
||||
accelerator.clear();
|
||||
for (NodeTuple t : yaml.getValue()) {
|
||||
Node key = t.getKeyNode();
|
||||
if (key instanceof ScalarNode key2) {
|
||||
accelerator.put(key2.getValue(), t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof MappingNode value2)) {
|
||||
return new YAMLConfigSection(owner, value2, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getSection(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof MappingNode value2)) {
|
||||
return new YAMLConfigSection(owner, value2, true);
|
||||
} else {
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
MappingNode obj = LegacyHelper.mappingNode(Tag.MAP, new ArrayList<>());
|
||||
NodeTuple tt = new NodeTuple(key, obj);
|
||||
accelerator.put(name, tt);
|
||||
yaml.getValue().add(tt);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigSection(owner, obj, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof SequenceNode value2)) {
|
||||
return new YAMLConfigList(owner, value2, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getList(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof SequenceNode value2)) {
|
||||
return new YAMLConfigList(owner, value2, true);
|
||||
} else {
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
SequenceNode obj = LegacyHelper.sequenceNode(Tag.SEQ, new ArrayList<>());
|
||||
NodeTuple tt = new NodeTuple(key, obj);
|
||||
accelerator.put(name, tt);
|
||||
yaml.getValue().add(tt);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigList(owner, obj, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getKeys() {
|
||||
ImmutableList.Builder<String> builder = ImmutableList.builder();
|
||||
for (NodeTuple t : yaml.getValue()) {
|
||||
Node key = t.getKeyNode();
|
||||
if (key instanceof ScalarNode key2) {
|
||||
builder.add(key2.getValue());
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBoolean(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
String str = value2.getValue().toLowerCase();
|
||||
return "false".equals(str) || "true".equals(str);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
return t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)
|
||||
&& Boolean.valueOf(value2.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, boolean defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
String str = value2.getValue().toLowerCase();
|
||||
boolean b = false;
|
||||
if ("false".equals(str) || (b = "true".equals(str))) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
ScalarNode node = LegacyHelper.scalarNode(Tag.BOOL, Boolean.toString(defaultValue), null);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, Supplier<Boolean> defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
String str = value2.getValue().toLowerCase();
|
||||
boolean b = false;
|
||||
if ("false".equals(str) || (b = "true".equals(str))) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
boolean b = defaultValue.get();
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
ScalarNode node = LegacyHelper.scalarNode(Tag.BOOL, Boolean.toString(b), null);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
try {
|
||||
Double.parseDouble(value2.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, int defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
try {
|
||||
return (int) Double.parseDouble(value2.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
ScalarNode node = LegacyHelper.scalarNode(Tag.INT, Integer.toString(defaultValue), null);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, Supplier<Integer> defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
try {
|
||||
return (int) Double.parseDouble(value2.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
int i = defaultValue.get();
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
ScalarNode node = LegacyHelper.scalarNode(Tag.INT, Integer.toString(i), null);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
return value2.getValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, String defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
return value2.getValue();
|
||||
}
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
ScalarNode node = LegacyHelper.scalarNode(Tag.STR, defaultValue, '\'');
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, Supplier<String> defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
return value2.getValue();
|
||||
}
|
||||
String str = defaultValue.get();
|
||||
ScalarNode key = LegacyHelper.scalarNode(Tag.STR, name, null);
|
||||
ScalarNode node = LegacyHelper.scalarNode(Tag.STR, str, '\'');
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.modern;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.EnumConfigFormat;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
|
||||
public class YAMLConfigBase implements IEaglerConfig {
|
||||
|
||||
final File file;
|
||||
YAMLConfigSection root;
|
||||
boolean modified;
|
||||
|
||||
YAMLConfigBase(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumConfigFormat getFormat() {
|
||||
return EnumConfigFormat.YAML;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveIfModified() throws IOException {
|
||||
if (modified) {
|
||||
YAMLConfigLoader.writeConfigFile(root.yaml, file);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.modern;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
||||
import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class YAMLConfigList implements IEaglerConfList {
|
||||
|
||||
private final YAMLConfigBase owner;
|
||||
final SequenceNode yaml;
|
||||
private final Consumer<String> commentSetter;
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
public YAMLConfigList(YAMLConfigBase owner, SequenceNode yaml, Consumer<String> commentSetter, boolean exists) {
|
||||
this.owner = owner;
|
||||
this.yaml = yaml;
|
||||
this.commentSetter = commentSetter;
|
||||
this.exists = this.initialized = exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
if (commentSetter != null) {
|
||||
commentSetter.accept(comment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection appendSection() {
|
||||
MappingNode obj = new MappingNode(Tag.MAP, new ArrayList<>(), FlowStyle.BLOCK);
|
||||
yaml.getValue().add(obj);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigSection(owner, obj, null, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList appendList() {
|
||||
SequenceNode obj = new SequenceNode(Tag.SEQ, new ArrayList<>(), FlowStyle.BLOCK);
|
||||
yaml.getValue().add(obj);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigList(owner, obj, null, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendInteger(int value) {
|
||||
yaml.getValue().add(new ScalarNode(Tag.INT, Integer.toString(value), null, null, ScalarStyle.PLAIN));
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendString(String string) {
|
||||
yaml.getValue().add(new ScalarNode(Tag.STR, string, null, null, ScalarStyle.SINGLE_QUOTED));
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return yaml.getValue().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return null;
|
||||
Node t = lst.get(index);
|
||||
if (t instanceof MappingNode tt) {
|
||||
return new YAMLConfigSection(owner, tt, null, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return null;
|
||||
Node t = lst.get(index);
|
||||
if (t instanceof SequenceNode tt) {
|
||||
return new YAMLConfigList(owner, tt, null, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return false;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode tt)) {
|
||||
try {
|
||||
Double.parseDouble(tt.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIfInteger(int index, int defaultVal) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return defaultVal;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode tt)) {
|
||||
try {
|
||||
return (int) Double.parseDouble(tt.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(int index) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return false;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(int index, String defaultVal) {
|
||||
List<Node> lst = yaml.getValue();
|
||||
if (index < 0 || index >= lst.size())
|
||||
return defaultVal;
|
||||
Node t = lst.get(index);
|
||||
if (t != null && (t instanceof ScalarNode tt)) {
|
||||
return tt.getValue();
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.modern;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.comments.CommentLine;
|
||||
import org.yaml.snakeyaml.comments.CommentType;
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
import org.yaml.snakeyaml.error.YAMLException;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfig;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.WrapUtil;
|
||||
|
||||
public class YAMLConfigLoader {
|
||||
|
||||
private static final Yaml YAML;
|
||||
|
||||
static {
|
||||
LoaderOptions loadOpts = new LoaderOptions();
|
||||
try {
|
||||
LoaderOptions.class.getMethod("setProcessComments", boolean.class).invoke(loadOpts, true);
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
}
|
||||
DumperOptions dumpOpts = new DumperOptions();
|
||||
dumpOpts.setPrettyFlow(true);
|
||||
dumpOpts.setDefaultFlowStyle(FlowStyle.FLOW);
|
||||
try {
|
||||
DumperOptions.class.getMethod("setProcessComments", boolean.class).invoke(dumpOpts, true);
|
||||
} catch (ReflectiveOperationException ex) {
|
||||
}
|
||||
YAML = new Yaml(new Constructor(loadOpts), new Representer(dumpOpts), dumpOpts);
|
||||
}
|
||||
|
||||
public static final int YAML_COMMENT_WRAP = 80;
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file) throws IOException {
|
||||
Node obj;
|
||||
try (Reader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
|
||||
synchronized (YAML) {
|
||||
obj = YAML.compose(reader);
|
||||
}
|
||||
} catch (FileNotFoundException ex) {
|
||||
obj = null;
|
||||
} catch (YAMLException ex) {
|
||||
throw new IOException("YAML config file has a syntax error: " + file.getAbsolutePath(), ex);
|
||||
}
|
||||
if (obj == null) {
|
||||
obj = new MappingNode(Tag.MAP, new ArrayList<>(), FlowStyle.BLOCK);
|
||||
}
|
||||
if (!(obj instanceof MappingNode obj2)) {
|
||||
throw new IOException("Root node " + obj.getClass().getSimpleName() + " is not a map!");
|
||||
}
|
||||
return getConfigFile(file, obj2);
|
||||
}
|
||||
|
||||
public static IEaglerConfig getConfigFile(File file, MappingNode node) throws IOException {
|
||||
YAMLConfigBase base = new YAMLConfigBase(file);
|
||||
base.root = new YAMLConfigSection(base, node, null, node.getValue().size() > 0);
|
||||
return base;
|
||||
}
|
||||
|
||||
public static void writeConfigFile(Node configIn, File file) throws IOException {
|
||||
File p = file.getAbsoluteFile().getParentFile();
|
||||
if (p != null && !p.isDirectory() && !p.mkdirs()) {
|
||||
throw new IOException("Could not create directory: " + p.getAbsolutePath());
|
||||
}
|
||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
|
||||
synchronized (YAML) {
|
||||
YAML.serialize(configIn, writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void createComment(String text, List<CommentLine> ret) {
|
||||
if (text != null) {
|
||||
String[] lines = WrapUtil.wrap(text, YAML_COMMENT_WRAP, "\n", false, " ").split("\n");
|
||||
for (int i = 0; i < lines.length; ++i) {
|
||||
ret.add(new CommentLine(null, null, " " + lines[i], CommentType.BLOCK));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void createCommentHelper(String text, ScalarNode ret) {
|
||||
List<CommentLine> lst = ret.getBlockComments();
|
||||
if (lst == null) {
|
||||
lst = new ArrayList<>();
|
||||
ret.setBlockComments(lst);
|
||||
} else {
|
||||
lst.clear();
|
||||
}
|
||||
createComment(text, lst);
|
||||
}
|
||||
|
||||
}
|
||||
+364
@@ -0,0 +1,364 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.config.snakeyaml.modern;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
|
||||
import org.yaml.snakeyaml.DumperOptions.ScalarStyle;
|
||||
import org.yaml.snakeyaml.comments.CommentLine;
|
||||
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||
import org.yaml.snakeyaml.nodes.Node;
|
||||
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfList;
|
||||
import net.lax1dude.eaglercraft.backend.server.config.IEaglerConfSection;
|
||||
|
||||
public class YAMLConfigSection implements IEaglerConfSection {
|
||||
|
||||
private final YAMLConfigBase owner;
|
||||
final MappingNode yaml;
|
||||
private final Consumer<String> commentSetter;
|
||||
final Map<String, NodeTuple> accelerator = new HashMap<>();
|
||||
private final boolean exists;
|
||||
private boolean initialized;
|
||||
|
||||
protected YAMLConfigSection(YAMLConfigBase owner, MappingNode yaml, Consumer<String> commentSetter,
|
||||
boolean exists) {
|
||||
this.owner = owner;
|
||||
this.yaml = yaml;
|
||||
this.commentSetter = commentSetter;
|
||||
this.exists = this.initialized = exists;
|
||||
rehash();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists() {
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
public void rehash() {
|
||||
accelerator.clear();
|
||||
for (NodeTuple t : yaml.getValue()) {
|
||||
Node key = t.getKeyNode();
|
||||
if (key instanceof ScalarNode key2) {
|
||||
accelerator.put(key2.getValue(), t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComment(String comment) {
|
||||
if (commentSetter != null) {
|
||||
commentSetter.accept(comment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getIfSection(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof MappingNode value2)) {
|
||||
return new YAMLConfigSection(owner, value2, (comment) -> {
|
||||
YAMLConfigLoader.createCommentHelper(comment, (ScalarNode) t.getKeyNode());
|
||||
owner.modified = true;
|
||||
}, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfSection getSection(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof MappingNode value2)) {
|
||||
return new YAMLConfigSection(owner, value2, (comment) -> {
|
||||
YAMLConfigLoader.createCommentHelper(comment, (ScalarNode) t.getKeyNode());
|
||||
owner.modified = true;
|
||||
}, true);
|
||||
} else {
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
MappingNode obj = new MappingNode(Tag.MAP, new ArrayList<>(), FlowStyle.BLOCK);
|
||||
NodeTuple tt = new NodeTuple(key, obj);
|
||||
accelerator.put(name, tt);
|
||||
yaml.getValue().add(tt);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigSection(owner, obj, (comment) -> {
|
||||
YAMLConfigLoader.createCommentHelper(comment, key);
|
||||
owner.modified = true;
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getIfList(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof SequenceNode value2)) {
|
||||
return new YAMLConfigList(owner, value2, (comment) -> {
|
||||
YAMLConfigLoader.createCommentHelper(comment, (ScalarNode) t.getKeyNode());
|
||||
owner.modified = true;
|
||||
}, true);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEaglerConfList getList(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof SequenceNode value2)) {
|
||||
return new YAMLConfigList(owner, value2, (comment) -> {
|
||||
YAMLConfigLoader.createCommentHelper(comment, (ScalarNode) t.getKeyNode());
|
||||
owner.modified = true;
|
||||
}, true);
|
||||
} else {
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
SequenceNode obj = new SequenceNode(Tag.SEQ, new ArrayList<>(), FlowStyle.BLOCK);
|
||||
NodeTuple tt = new NodeTuple(key, obj);
|
||||
accelerator.put(name, tt);
|
||||
yaml.getValue().add(tt);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return new YAMLConfigList(owner, obj, (comment) -> {
|
||||
YAMLConfigLoader.createCommentHelper(comment, key);
|
||||
owner.modified = true;
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getKeys() {
|
||||
ImmutableList.Builder<String> builder = ImmutableList.builder();
|
||||
for (NodeTuple t : yaml.getValue()) {
|
||||
Node key = t.getKeyNode();
|
||||
if (key instanceof ScalarNode key2) {
|
||||
builder.add(key2.getValue());
|
||||
}
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBoolean(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
String str = value2.getValue().toLowerCase();
|
||||
return "false".equals(str) || "true".equals(str);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
return t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)
|
||||
&& Boolean.valueOf(value2.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, boolean defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
String str = value2.getValue().toLowerCase();
|
||||
boolean b = false;
|
||||
if ("false".equals(str) || (b = "true".equals(str))) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
if (comment != null) {
|
||||
List<CommentLine> lst = new ArrayList<>();
|
||||
YAMLConfigLoader.createComment(comment, lst);
|
||||
key.setBlockComments(lst);
|
||||
}
|
||||
ScalarNode node = new ScalarNode(Tag.BOOL, Boolean.toString(defaultValue), null, null, ScalarStyle.PLAIN);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String name, Supplier<Boolean> defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
String str = value2.getValue().toLowerCase();
|
||||
boolean b = false;
|
||||
if ("false".equals(str) || (b = "true".equals(str))) {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
boolean b = defaultValue.get();
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
if (comment != null) {
|
||||
List<CommentLine> lst = new ArrayList<>();
|
||||
YAMLConfigLoader.createComment(comment, lst);
|
||||
key.setBlockComments(lst);
|
||||
}
|
||||
ScalarNode node = new ScalarNode(Tag.BOOL, Boolean.toString(b), null, null, ScalarStyle.PLAIN);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInteger(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
try {
|
||||
Double.parseDouble(value2.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, int defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
try {
|
||||
return (int) Double.parseDouble(value2.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
if (comment != null) {
|
||||
List<CommentLine> lst = new ArrayList<>();
|
||||
YAMLConfigLoader.createComment(comment, lst);
|
||||
key.setBlockComments(lst);
|
||||
}
|
||||
ScalarNode node = new ScalarNode(Tag.INT, Integer.toString(defaultValue), null, null, ScalarStyle.PLAIN);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInteger(String name, Supplier<Integer> defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
try {
|
||||
return (int) Double.parseDouble(value2.getValue());
|
||||
} catch (NumberFormatException ex) {
|
||||
}
|
||||
}
|
||||
int i = defaultValue.get();
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
if (comment != null) {
|
||||
List<CommentLine> lst = new ArrayList<>();
|
||||
YAMLConfigLoader.createComment(comment, lst);
|
||||
key.setBlockComments(lst);
|
||||
}
|
||||
ScalarNode node = new ScalarNode(Tag.INT, Integer.toString(i), null, null, ScalarStyle.PLAIN);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isString(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIfString(String name) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
return value2.getValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, String defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
return value2.getValue();
|
||||
}
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
if (comment != null) {
|
||||
List<CommentLine> lst = new ArrayList<>();
|
||||
YAMLConfigLoader.createComment(comment, lst);
|
||||
key.setBlockComments(lst);
|
||||
}
|
||||
ScalarNode node = new ScalarNode(Tag.STR, defaultValue, null, null, ScalarStyle.SINGLE_QUOTED);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String name, Supplier<String> defaultValue, String comment) {
|
||||
NodeTuple t = accelerator.get(name);
|
||||
if (t != null && t.getValueNode() != null && (t.getValueNode() instanceof ScalarNode value2)) {
|
||||
return value2.getValue();
|
||||
}
|
||||
String str = defaultValue.get();
|
||||
ScalarNode key = new ScalarNode(Tag.STR, name, null, null, ScalarStyle.PLAIN);
|
||||
if (comment != null) {
|
||||
List<CommentLine> lst = new ArrayList<>();
|
||||
YAMLConfigLoader.createComment(comment, lst);
|
||||
key.setBlockComments(lst);
|
||||
}
|
||||
ScalarNode node = new ScalarNode(Tag.STR, str, null, null, ScalarStyle.SINGLE_QUOTED);
|
||||
t = new NodeTuple(key, node);
|
||||
accelerator.put(name, t);
|
||||
yaml.getValue().add(t);
|
||||
owner.modified = true;
|
||||
initialized = true;
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2025 lax1dude. All Rights Reserved.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
package net.lax1dude.eaglercraft.backend.server.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import net.lax1dude.eaglercraft.backend.server.config.ReflectUtil;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public class GsonLenient {
|
||||
|
||||
private static final Method meth_setStrictness;
|
||||
private static final Object enum_LENIENT;
|
||||
private static final Method meth_setLenient;
|
||||
|
||||
static {
|
||||
Method _meth_setStrictness;
|
||||
Object _enum_LENIENT;
|
||||
Method _meth_setLenient;
|
||||
try {
|
||||
Class<?> class_Strictness = Class.forName("com.google.gson.Strictness");
|
||||
_enum_LENIENT = Enum.valueOf((Class) class_Strictness, "LENIENT");
|
||||
_meth_setStrictness = GsonBuilder.class.getMethod("setStrictness", class_Strictness);
|
||||
_meth_setLenient = null;
|
||||
} catch (IllegalArgumentException | ReflectiveOperationException e) {
|
||||
_meth_setStrictness = null;
|
||||
_enum_LENIENT = null;
|
||||
try {
|
||||
_meth_setLenient = GsonBuilder.class.getMethod("setLenient");
|
||||
} catch (ReflectiveOperationException ee) {
|
||||
_meth_setLenient = null;
|
||||
}
|
||||
}
|
||||
meth_setStrictness = _meth_setStrictness;
|
||||
enum_LENIENT = _enum_LENIENT;
|
||||
meth_setLenient = _meth_setLenient;
|
||||
}
|
||||
|
||||
public static GsonBuilder setLenient(GsonBuilder builder) {
|
||||
try {
|
||||
if (meth_setStrictness != null) {
|
||||
meth_setStrictness.invoke(builder, enum_LENIENT);
|
||||
} else if (meth_setLenient != null) {
|
||||
meth_setLenient.invoke(builder);
|
||||
}
|
||||
return builder;
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw ReflectUtil.propagateReflectThrowable(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user