Fix saving of default config
This commit is contained in:
parent
2b78de3d76
commit
43d01ac420
2 changed files with 33 additions and 5 deletions
|
@ -3,6 +3,7 @@ package ru.ruscalworld.fabricexporter.config;
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import ru.ruscalworld.fabricexporter.FabricExporter;
|
import ru.ruscalworld.fabricexporter.FabricExporter;
|
||||||
|
import ru.ruscalworld.fabricexporter.util.FileUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -10,6 +11,7 @@ import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public abstract class Config {
|
public abstract class Config {
|
||||||
|
@ -22,13 +24,11 @@ public abstract class Config {
|
||||||
public void load() throws IOException {
|
public void load() throws IOException {
|
||||||
Path path = FabricLoader.getInstance().getConfigDir().resolve(this.getFileName());
|
Path path = FabricLoader.getInstance().getConfigDir().resolve(this.getFileName());
|
||||||
if (!path.toFile().exists()) try {
|
if (!path.toFile().exists()) try {
|
||||||
URL url = this.getClass().getClassLoader().getResource("config/" + this.getFileName());
|
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("config/" + this.getFileName());
|
||||||
assert url != null;
|
List<String> lines = FileUtil.getLinesFromStream(stream);
|
||||||
File file = new File(url.toURI());
|
|
||||||
byte[] bytes = Files.readAllBytes(file.toPath());
|
|
||||||
|
|
||||||
Files.createFile(path);
|
Files.createFile(path);
|
||||||
Files.write(path, bytes);
|
Files.write(path, lines);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
FabricExporter.getLogger().fatal("Unable to save default config");
|
FabricExporter.getLogger().fatal("Unable to save default config");
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package ru.ruscalworld.fabricexporter.util;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FileUtil {
|
||||||
|
/**
|
||||||
|
* Reads stream into a list of lines
|
||||||
|
* @param stream Input stream
|
||||||
|
* @return List of lines
|
||||||
|
*/
|
||||||
|
public static List<String> getLinesFromStream(InputStream stream) throws IOException {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
InputStreamReader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8);
|
||||||
|
BufferedReader reader = new BufferedReader(streamReader);
|
||||||
|
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) result.add(line);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue