Support config loading from input stream as fallback if URL cannot be acquired (#3647)

This commit is contained in:
vadage 2024-01-14 10:04:15 +01:00 committed by GitHub
parent 3b9d399afd
commit 5b29ac0d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 3 deletions

View File

@ -66,14 +66,26 @@ public abstract class Config {
return getClass().getClassLoader().getResource("assets/viaversion/config.yml"); return getClass().getClassLoader().getResource("assets/viaversion/config.yml");
} }
public InputStream getDefaultConfigInputStream() {
return getClass().getClassLoader().getResourceAsStream("assets/viaversion/config.yml");
}
public Map<String, Object> loadConfig(File location) { public Map<String, Object> loadConfig(File location) {
return loadConfig(location, getDefaultConfigURL()); final URL defaultConfigUrl = getDefaultConfigURL();
if (defaultConfigUrl != null) {
return loadConfig(location, defaultConfigUrl);
}
return loadConfig(location, this::getDefaultConfigInputStream);
} }
public synchronized Map<String, Object> loadConfig(File location, URL jarConfigFile) { public synchronized Map<String, Object> loadConfig(File location, URL jarConfigFile) {
return loadConfig(location, jarConfigFile::openStream);
}
private synchronized Map<String, Object> loadConfig(File location, InputStreamSupplier configSupplier) {
List<String> unsupported = getUnsupportedOptions(); List<String> unsupported = getUnsupportedOptions();
try { try {
commentStore.storeComments(jarConfigFile.openStream()); commentStore.storeComments(configSupplier.get());
for (String option : unsupported) { for (String option : unsupported) {
List<String> comments = commentStore.header(option); List<String> comments = commentStore.header(option);
if (comments != null) { if (comments != null) {
@ -96,7 +108,7 @@ public abstract class Config {
} }
Map<String, Object> defaults = config; Map<String, Object> defaults = config;
try (InputStream stream = jarConfigFile.openStream()) { try (InputStream stream = configSupplier.get()) {
defaults = (Map<String, Object>) YAML.get().load(stream); defaults = (Map<String, Object>) YAML.get().load(stream);
for (String option : unsupported) { for (String option : unsupported) {
defaults.remove(option); defaults.remove(option);

View File

@ -0,0 +1,27 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.util;
import java.io.IOException;
import java.io.InputStream;
@FunctionalInterface
public interface InputStreamSupplier {
InputStream get() throws IOException;
}