9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-04 15:41:38 +00:00

refactor(core): 修改资源包的一些东西

This commit is contained in:
jhqwqmc
2025-05-06 15:37:22 +08:00
parent 3c9651a1b3
commit afa3d033dc
11 changed files with 76 additions and 18 deletions

View File

@@ -44,6 +44,7 @@ import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static net.momirealms.craftengine.core.util.MiscUtils.castToMap;
@@ -73,6 +74,7 @@ public abstract class AbstractPackManager implements PackManager {
private final TreeMap<ConfigSectionParser, List<CachedConfig>> cachedConfigs = new TreeMap<>();
protected BiConsumer<Path, Path> zipGenerator;
protected ResourcePackHost resourcePackHost;
private boolean generateResourcePack = false;
public AbstractPackManager(CraftEngine plugin, BiConsumer<Path, Path> eventDispatcher) {
this.plugin = plugin;
@@ -141,6 +143,7 @@ public abstract class AbstractPackManager implements PackManager {
@Override
public void load() {
initFileSystemProvider();
List<Map<?, ?>> list = Config.instance().settings().getMapList("resource-pack.delivery.hosting");
if (list == null || list.isEmpty()) {
this.resourcePackHost = NoneHost.INSTANCE;
@@ -484,8 +487,42 @@ public abstract class AbstractPackManager implements PackManager {
}
}
private static void initFileSystemProvider() {
String osName = System.getProperty("os.name").toLowerCase();
String providerClass = null;
if (osName.contains("win")) {
providerClass = "sun.nio.fs.WindowsFileSystemProvider";
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
providerClass = "sun.nio.fs.LinuxFileSystemProvider";
} else if (osName.contains("mac")) {
providerClass = "sun.nio.fs.MacOSXFileSystemProvider";
}
if (providerClass != null) {
try {
System.setProperty("java.nio.file.spi.DefaultFileSystemProvider", providerClass);
} catch (Exception ignored) {}
}
}
private static void deleteDirectory(Path folder) throws IOException {
if (!Files.exists(folder)) return;
try (Stream<Path> walk = Files.walk(folder)) {
walk.sorted(Comparator.reverseOrder())
.parallel()
.forEach(path -> {
try {
Files.delete(path);
} catch (IOException ignored) {}
});
}
}
@Override
public void generateResourcePack() {
if (this.generateResourcePack) {
throw new LocalizedException("warning.resource_pack.generation_in_progress");
}
this.generateResourcePack = true;
this.plugin.logger().info("Generating resource pack...");
long start = System.currentTimeMillis();
// get the target location
@@ -494,7 +531,7 @@ public abstract class AbstractPackManager implements PackManager {
.resolve("resource_pack");
try {
org.apache.commons.io.FileUtils.deleteDirectory(generatedPackPath.toFile());
deleteDirectory(generatedPackPath);
} catch (IOException e) {
this.plugin.logger().severe("Error deleting previous resource pack", e);
}
@@ -547,6 +584,7 @@ public abstract class AbstractPackManager implements PackManager {
long end = System.currentTimeMillis();
this.plugin.logger().info("Finished generating resource pack in " + (end - start) + "ms");
this.eventDispatcher.accept(generatedPackPath, zipFile);
this.generateResourcePack = false;
}
private void generateParticle(Path generatedPackPath) {

View File

@@ -66,6 +66,13 @@ public class SelfHost implements ResourcePackHost {
if (port <= 0 || port > 65535) {
throw new LocalizedException("warning.config.host.self.invalid_port", String.valueOf(port));
}
String url = arguments.getOrDefault("url", "").toString();
if (!url.isEmpty()) {
if (!url.startsWith("http://") && !url.startsWith("https://")) {
throw new LocalizedException("warning.config.host.self.invalid_url", url);
}
if (!url.endsWith("/")) url += "/";
}
boolean oneTimeToken = (boolean) arguments.getOrDefault("one-time-token", true);
String protocol = arguments.getOrDefault("protocol", "http").toString();
boolean denyNonMinecraftRequest = (boolean) arguments.getOrDefault("deny-non-minecraft-request", true);
@@ -76,7 +83,7 @@ public class SelfHost implements ResourcePackHost {
maxRequests = ResourceConfigUtils.getAsInt(rateMap.getOrDefault("max-requests", 5), "max-requests");
resetInterval = ResourceConfigUtils.getAsInt(rateMap.getOrDefault("reset-interval", 20), "reset-interval") * 1000;
}
selfHostHttpServer.updateProperties(ip, port, denyNonMinecraftRequest, protocol, maxRequests, resetInterval, oneTimeToken);
selfHostHttpServer.updateProperties(ip, port, url, denyNonMinecraftRequest, protocol, maxRequests, resetInterval, oneTimeToken);
return INSTANCE;
}
}

View File

@@ -48,6 +48,7 @@ public class SelfHostHttpServer {
private String ip = "localhost";
private int port = -1;
private String protocol = "http";
private String url;
private boolean denyNonMinecraft = true;
private boolean useToken;
@@ -57,12 +58,14 @@ public class SelfHostHttpServer {
public void updateProperties(String ip,
int port,
String url,
boolean denyNonMinecraft,
String protocol,
int maxRequests,
int resetInternal,
boolean token) {
this.ip = ip;
this.url = url;
this.denyNonMinecraft = denyNonMinecraft;
this.protocol = protocol;
this.rateLimit = maxRequests;
@@ -112,6 +115,9 @@ public class SelfHostHttpServer {
}
public String url() {
if (this.url != null && !this.url.isEmpty()) {
return this.url;
}
return this.protocol + "://" + this.ip + ":" + this.port + "/";
}

View File

@@ -32,6 +32,10 @@ public enum ProtocolVersion {
return name;
}
public boolean isVersionNewerThan(ProtocolVersion targetVersion) {
return this.getId() >= targetVersion.getId();
}
public static ProtocolVersion getByName(String name) {
for (ProtocolVersion version : values()) {
if (version.getName().equals(name)) {

View File

@@ -1,10 +0,0 @@
package net.momirealms.craftengine.core.util;
import net.momirealms.craftengine.core.plugin.network.ProtocolVersion;
public class ProtocolVersionUtils {
public static boolean isVersionNewerThan(ProtocolVersion version, ProtocolVersion targetVersion) {
return version.getId() >= targetVersion.getId();
}
}