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 org.apache.logging.log4j.LogManager;
|
||||
import ru.ruscalworld.fabricexporter.FabricExporter;
|
||||
import ru.ruscalworld.fabricexporter.util.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -10,6 +11,7 @@ import java.io.InputStream;
|
|||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public abstract class Config {
|
||||
|
@ -22,13 +24,11 @@ public abstract class Config {
|
|||
public void load() throws IOException {
|
||||
Path path = FabricLoader.getInstance().getConfigDir().resolve(this.getFileName());
|
||||
if (!path.toFile().exists()) try {
|
||||
URL url = this.getClass().getClassLoader().getResource("config/" + this.getFileName());
|
||||
assert url != null;
|
||||
File file = new File(url.toURI());
|
||||
byte[] bytes = Files.readAllBytes(file.toPath());
|
||||
InputStream stream = this.getClass().getClassLoader().getResourceAsStream("config/" + this.getFileName());
|
||||
List<String> lines = FileUtil.getLinesFromStream(stream);
|
||||
|
||||
Files.createFile(path);
|
||||
Files.write(path, bytes);
|
||||
Files.write(path, lines);
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
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