2020-01-09 03:05:42 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-01-09 03:05:42 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
2019-08-02 04:16:17 +00:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
|
|
|
import org.geysermc.connector.GeyserConnector;
|
|
|
|
|
2020-11-22 10:40:53 +00:00
|
|
|
import java.io.*;
|
2021-07-17 18:36:04 +00:00
|
|
|
import java.lang.annotation.Annotation;
|
2021-01-03 17:53:26 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-09-17 00:08:26 +00:00
|
|
|
import java.nio.file.Files;
|
2021-08-25 10:31:12 +00:00
|
|
|
import java.nio.file.Path;
|
2020-09-17 00:08:26 +00:00
|
|
|
import java.security.MessageDigest;
|
2021-07-17 18:36:04 +00:00
|
|
|
import java.util.Set;
|
2019-08-06 01:59:54 +00:00
|
|
|
import java.util.function.Function;
|
2021-07-17 18:36:04 +00:00
|
|
|
import java.util.stream.Collectors;
|
2021-08-25 10:31:12 +00:00
|
|
|
import java.util.stream.Stream;
|
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-09-17 00:08:26 +00:00
|
|
|
public static <T> T loadYaml(InputStream src, Class<T> valueType) throws IOException {
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()).enable(JsonParser.Feature.IGNORE_UNDEFINED).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
|
|
return objectMapper.readValue(src, valueType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> T loadJson(InputStream src, Class<T> valueType) throws IOException {
|
2021-01-03 17:53:26 +00:00
|
|
|
// Read specifically with UTF-8 to allow any non-UTF-encoded JSON to read
|
|
|
|
return GeyserConnector.JSON_MAPPER.readValue(new InputStreamReader(src, StandardCharsets.UTF_8), valueType);
|
2020-09-17 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
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()) {
|
2021-02-16 21:25:46 +00:00
|
|
|
//noinspection ResultOfMethodCallIgnored
|
2020-02-08 19:43:25 +00:00
|
|
|
file.createNewFile();
|
2021-02-16 21:25:46 +00:00
|
|
|
try (FileOutputStream fos = new FileOutputStream(file)) {
|
|
|
|
try (InputStream input = GeyserConnector.class.getResourceAsStream("/" + name)) { // resources need leading "/" prefix
|
|
|
|
byte[] bytes = new byte[input.available()];
|
2019-08-02 04:16:17 +00:00
|
|
|
|
2021-02-16 21:25:46 +00:00
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
|
input.read(bytes);
|
2019-08-06 01:59:54 +00:00
|
|
|
|
2021-02-16 21:25:46 +00:00
|
|
|
for(char c : format.apply(new String(bytes)).toCharArray()) {
|
|
|
|
fos.write(c);
|
|
|
|
}
|
2019-08-06 01:59:54 +00:00
|
|
|
|
2021-02-16 21:25:46 +00:00
|
|
|
fos.flush();
|
|
|
|
}
|
2019-08-06 01:59:54 +00:00
|
|
|
}
|
2019-08-02 04:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:25:46 +00:00
|
|
|
try (FileOutputStream fos = new FileOutputStream(file)) {
|
|
|
|
for (char c : data) {
|
|
|
|
fos.write(c);
|
|
|
|
}
|
2020-04-08 20:11:56 +00:00
|
|
|
|
2021-02-16 21:25:46 +00:00
|
|
|
fos.flush();
|
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 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) {
|
2021-06-08 03:09:42 +00:00
|
|
|
throw new AssertionError("Unable to find resource: " + resource);
|
2020-05-25 01:07:05 +00:00
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
2020-08-28 18:36:24 +00:00
|
|
|
|
2020-09-17 00:08:26 +00:00
|
|
|
/**
|
2020-11-16 23:57:57 +00:00
|
|
|
* Calculate the SHA256 hash of a file
|
|
|
|
*
|
2020-09-17 00:08:26 +00:00
|
|
|
* @param file File to calculate the hash for
|
|
|
|
* @return A byte[] representation of the hash
|
|
|
|
*/
|
|
|
|
public static byte[] calculateSHA256(File file) {
|
|
|
|
byte[] sha256;
|
|
|
|
|
|
|
|
try {
|
2020-11-22 10:40:53 +00:00
|
|
|
sha256 = MessageDigest.getInstance("SHA-256").digest(readAllBytes(file));
|
2020-09-17 00:08:26 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException("Could not calculate pack hash", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sha256;
|
|
|
|
}
|
|
|
|
|
2020-11-16 23:57:57 +00:00
|
|
|
/**
|
|
|
|
* Calculate the SHA1 hash of a file
|
|
|
|
*
|
|
|
|
* @param file File to calculate the hash for
|
|
|
|
* @return A byte[] representation of the hash
|
|
|
|
*/
|
|
|
|
public static byte[] calculateSHA1(File file) {
|
|
|
|
byte[] sha1;
|
|
|
|
|
|
|
|
try {
|
2020-11-22 10:40:53 +00:00
|
|
|
sha1 = MessageDigest.getInstance("SHA-1").digest(readAllBytes(file));
|
2020-11-16 23:57:57 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException("Could not calculate pack hash", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sha1;
|
|
|
|
}
|
|
|
|
|
2020-11-22 10:40:53 +00:00
|
|
|
/**
|
|
|
|
* An android compatible version of {@link Files#readAllBytes}
|
|
|
|
*
|
|
|
|
* @param file File to read bytes of
|
|
|
|
* @return The byte array of the file
|
|
|
|
*/
|
|
|
|
public static byte[] readAllBytes(File file) {
|
2021-01-12 20:06:48 +00:00
|
|
|
try (InputStream inputStream = new FileInputStream(file)) {
|
|
|
|
return readAllBytes(inputStream);
|
2020-12-04 21:55:24 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Cannot read " + file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param stream the InputStream to read off of
|
|
|
|
* @return the byte array of an InputStream
|
|
|
|
*/
|
|
|
|
public static byte[] readAllBytes(InputStream stream) {
|
|
|
|
try {
|
|
|
|
int size = stream.available();
|
|
|
|
byte[] bytes = new byte[size];
|
2021-02-16 21:25:46 +00:00
|
|
|
try (BufferedInputStream buf = new BufferedInputStream(stream)) {
|
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
|
buf.read(bytes, 0, bytes.length);
|
|
|
|
}
|
2020-12-04 21:55:24 +00:00
|
|
|
return bytes;
|
|
|
|
} catch (IOException e) {
|
2021-08-25 10:31:12 +00:00
|
|
|
throw new RuntimeException("Error while trying to read input stream!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read the lines of a file and return it as a stream
|
|
|
|
*
|
|
|
|
* @param path File path to read
|
|
|
|
* @return The lines as a stream
|
|
|
|
*/
|
|
|
|
public static Stream<String> readAllLines(Path path) {
|
|
|
|
try {
|
|
|
|
return new BufferedReader(new InputStreamReader(java.nio.file.Files.newInputStream(path))).lines();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Error while trying to read file!", e);
|
2020-12-04 21:55:24 +00:00
|
|
|
}
|
2020-11-22 10:40:53 +00:00
|
|
|
}
|
2021-07-17 18:36:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a set of all the classes that are annotated by a given annotation.
|
|
|
|
* Keep in mind that these are from a set of generated annotations generated
|
|
|
|
* at compile time by the annotation processor, meaning that arbitrary annotations
|
|
|
|
* cannot be passed into this method and expected to have a set of classes
|
|
|
|
* returned back.
|
|
|
|
*
|
|
|
|
* @param annotationClass the annotation class
|
|
|
|
* @return a set of all the classes annotated by the given annotation
|
|
|
|
*/
|
|
|
|
public static Set<Class<?>> getGeneratedClassesForAnnotation(Class<? extends Annotation> annotationClass) {
|
|
|
|
return getGeneratedClassesForAnnotation(annotationClass.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a set of all the classes that are annotated by a given annotation.
|
|
|
|
* Keep in mind that these are from a set of generated annotations generated
|
|
|
|
* at compile time by the annotation processor, meaning that arbitrary annotations
|
|
|
|
* cannot be passed into this method and expected to have a set of classes
|
|
|
|
* returned back.
|
|
|
|
*
|
|
|
|
* @param input the fully qualified name of the annotation
|
|
|
|
* @return a set of all the classes annotated by the given annotation
|
|
|
|
*/
|
|
|
|
public static Set<Class<?>> getGeneratedClassesForAnnotation(String input) {
|
|
|
|
InputStream annotatedClass = FileUtils.getResource(input);
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(annotatedClass));
|
|
|
|
return reader.lines().map(className -> {
|
|
|
|
try {
|
|
|
|
return Class.forName(className);
|
|
|
|
} catch (ClassNotFoundException ex) {
|
|
|
|
GeyserConnector.getInstance().getLogger().error("Failed to find class " + className, ex);
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toSet());
|
|
|
|
}
|
2019-08-02 04:16:17 +00:00
|
|
|
}
|