2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @author GeyserMC
|
|
|
|
* @link https://github.com/GeyserMC/Geyser
|
|
|
|
*/
|
|
|
|
|
2019-08-02 04:16:17 +00:00
|
|
|
package org.geysermc.connector.utils;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
|
|
|
import org.geysermc.connector.GeyserConnector;
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-08-06 01:59:54 +00:00
|
|
|
import java.util.function.Function;
|
2019-08-02 04:16:17 +00:00
|
|
|
|
|
|
|
public class FileUtils {
|
2019-12-21 22:18:34 +00:00
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Load the given YAML file into the given class
|
|
|
|
*
|
|
|
|
* @param src File to load
|
|
|
|
* @param valueType Class to load file into
|
2020-08-08 22:56:15 +00:00
|
|
|
* @param <T> the type
|
2020-04-21 05:28:44 +00:00
|
|
|
* @return The data as the given class
|
2020-04-22 06:03:46 +00:00
|
|
|
* @throws IOException if the config could not be loaded
|
2020-04-21 05:28:44 +00:00
|
|
|
*/
|
2019-08-02 04:16:17 +00:00
|
|
|
public static <T> T loadConfig(File src, Class<T> valueType) throws IOException {
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
|
|
|
|
return objectMapper.readValue(src, valueType);
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Open the specified file or copy if from resources
|
|
|
|
*
|
|
|
|
* @param name File and resource name
|
|
|
|
* @param fallback Formatting callback
|
|
|
|
* @return File handle of the specified file
|
2020-04-22 06:03:46 +00:00
|
|
|
* @throws IOException if the file failed to copy from resource
|
2020-04-21 05:28:44 +00:00
|
|
|
*/
|
|
|
|
public static File fileOrCopiedFromResource(String name, Function<String, String> fallback) throws IOException {
|
|
|
|
return fileOrCopiedFromResource(new File(name), name, fallback);
|
2019-12-21 22:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Open the specified file or copy if from resources
|
|
|
|
*
|
|
|
|
* @param file File to open
|
|
|
|
* @param name Name of the resource get if needed
|
|
|
|
* @param format Formatting callback
|
|
|
|
* @return File handle of the specified file
|
2020-04-22 06:03:46 +00:00
|
|
|
* @throws IOException if the file failed to copy from resource
|
2020-04-21 05:28:44 +00:00
|
|
|
*/
|
|
|
|
public static File fileOrCopiedFromResource(File file, String name, Function<String, String> format) throws IOException {
|
2019-08-02 04:16:17 +00:00
|
|
|
if (!file.exists()) {
|
2020-02-08 19:43:25 +00:00
|
|
|
file.createNewFile();
|
2019-08-02 04:16:17 +00:00
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
2019-08-06 01:59:54 +00:00
|
|
|
InputStream input = GeyserConnector.class.getResourceAsStream("/" + name); // resources need leading "/" prefix
|
2019-08-02 04:16:17 +00:00
|
|
|
|
2019-08-06 01:59:54 +00:00
|
|
|
byte[] bytes = new byte[input.available()];
|
|
|
|
|
|
|
|
input.read(bytes);
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
for(char c : format.apply(new String(bytes)).toCharArray()) {
|
2019-08-06 01:59:54 +00:00
|
|
|
fos.write(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
fos.flush();
|
|
|
|
input.close();
|
2019-08-02 04:16:17 +00:00
|
|
|
fos.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
2020-04-08 20:11:56 +00:00
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Writes the given data to the specified file on disk
|
|
|
|
*
|
|
|
|
* @param file File to write to
|
|
|
|
* @param data Data to write to the file
|
2020-04-22 06:03:46 +00:00
|
|
|
* @throws IOException if the file failed to write
|
2020-04-21 05:28:44 +00:00
|
|
|
*/
|
2020-04-08 20:11:56 +00:00
|
|
|
public static void writeFile(File file, char[] data) throws IOException {
|
|
|
|
if (!file.exists()) {
|
|
|
|
file.createNewFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(file);
|
|
|
|
|
|
|
|
for (char c : data) {
|
|
|
|
fos.write(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
fos.flush();
|
|
|
|
fos.close();
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Writes the given data to the specified file on disk
|
|
|
|
*
|
|
|
|
* @param name File path to write to
|
|
|
|
* @param data Data to write to the file
|
2020-04-22 06:03:46 +00:00
|
|
|
* @throws IOException if the file failed to write
|
2020-04-21 05:28:44 +00:00
|
|
|
*/
|
2020-04-08 20:11:56 +00:00
|
|
|
public static void writeFile(String name, char[] data) throws IOException {
|
|
|
|
writeFile(new File(name), data);
|
|
|
|
}
|
2020-05-25 01:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an InputStream for the given resource path, throws AssertionError if resource is not found
|
|
|
|
*
|
|
|
|
* @param resource Resource to get
|
|
|
|
* @return InputStream of the given resource
|
|
|
|
*/
|
|
|
|
public static InputStream getResource(String resource) {
|
|
|
|
InputStream stream = FileUtils.class.getClassLoader().getResourceAsStream(resource);
|
|
|
|
if (stream == null) {
|
2020-07-05 23:35:51 +00:00
|
|
|
throw new AssertionError(LanguageUtils.getLocaleStringLog("geyser.toolbox.fail.resource", resource));
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
2019-08-02 04:16:17 +00:00
|
|
|
}
|