sub folder support

This commit is contained in:
zimzaza4
2024-05-02 00:39:05 +08:00
parent 4cb2825b46
commit 9dc8c9c386
6 changed files with 112 additions and 75 deletions

View File

@@ -5,41 +5,33 @@ import org.geysermc.event.subscribe.Subscribe;
import org.geysermc.geyser.api.event.lifecycle.GeyserLoadResourcePacksEvent; import org.geysermc.geyser.api.event.lifecycle.GeyserLoadResourcePacksEvent;
import org.geysermc.geyser.api.event.lifecycle.GeyserPreInitializeEvent; import org.geysermc.geyser.api.event.lifecycle.GeyserPreInitializeEvent;
import org.geysermc.geyser.api.extension.Extension; import org.geysermc.geyser.api.extension.Extension;
import re.imc.geysermodelenginepackgenerator.generator.Entity;
import re.imc.geysermodelenginepackgenerator.util.ZipUtil; import re.imc.geysermodelenginepackgenerator.util.ZipUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
public class ExtensionMain implements Extension { public class ExtensionMain implements Extension {
private File source; private File source;
Path generatedPackZip;
@Subscribe @Subscribe
public void onLoad(GeyserPreInitializeEvent event) { public void onLoad(GeyserPreInitializeEvent event) {
source = dataFolder().resolve("input").toFile(); source = dataFolder().resolve("input").toFile();
source.mkdirs(); source.mkdirs();
File[] files = source.listFiles();
if (files != null) {
for (File file : files) {
String id = "modelengine:" + file.getName().toLowerCase();
GeyserUtils.addCustomEntity(id);
}
}
}
@Subscribe
public void onPackLoad(GeyserLoadResourcePacksEvent event) {
File generatedPack = dataFolder().resolve("generated_pack").toFile(); File generatedPack = dataFolder().resolve("generated_pack").toFile();
GeneratorMain.startGenerate(source, generatedPack); GeneratorMain.startGenerate(source, generatedPack);
Path generatedPackZip = dataFolder().resolve("generated_pack.zip"); generatedPackZip = dataFolder().resolve("generated_pack.zip");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(generatedPackZip))) { try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(generatedPackZip))) {
// 压缩文件夹 // 压缩文件夹
@@ -48,6 +40,19 @@ public class ExtensionMain implements Extension {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
for (String entity : GeneratorMain.entityMap.keySet()) {
String id = "modelengine:" + entity;
GeyserUtils.addCustomEntity(id);
}
}
@Subscribe
public void onPackLoad(GeyserLoadResourcePacksEvent event) {
event.resourcePacks().add(generatedPackZip); event.resourcePacks().add(generatedPackZip);
} }
} }

View File

@@ -23,7 +23,6 @@ public class GeneratorMain {
public static final Map<String, Geometry> geometryMap = new HashMap<>(); public static final Map<String, Geometry> geometryMap = new HashMap<>();
public static final Map<String, Texture> textureMap = new HashMap<>(); public static final Map<String, Texture> textureMap = new HashMap<>();
public static final Gson GSON = new GsonBuilder() public static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.create(); .create();
@@ -35,22 +34,21 @@ public class GeneratorMain {
startGenerate(source, output); startGenerate(source, output);
} }
public static void startGenerate(File source, File output) { public static void generateFromFolder(String currentPath, File folder) {
if (folder.listFiles() == null) {
return;
for (File file1 : source.listFiles()) {
if (file1.isDirectory()) {
if (file1.listFiles() == null) {
continue;
} }
String modelId = file1.getName().toLowerCase(); String modelId = folder.getName().toLowerCase();
Entity entity = new Entity(modelId); Entity entity = new Entity(modelId);
entityMap.put(modelId, entity); boolean canAdd = false;
for (File e : folder.listFiles()) {
for (File e : file1.listFiles()) { if (e.isDirectory()) {
generateFromFolder(currentPath + folder.getName() + "/", e);
}
if (e.getName().endsWith(".png")) { if (e.getName().endsWith(".png")) {
textureMap.put(modelId, new Texture(modelId, e.toPath())); canAdd = true;
textureMap.put(modelId, new Texture(modelId, currentPath, e.toPath()));
} }
if (e.getName().equals("config.properties")) { if (e.getName().equals("config.properties")) {
try { try {
@@ -64,6 +62,7 @@ public class GeneratorMain {
String json = Files.readString(e.toPath()); String json = Files.readString(e.toPath());
if (isAnimationFile(json)) { if (isAnimationFile(json)) {
Animation animation = new Animation(); Animation animation = new Animation();
animation.setPath(currentPath);
animation.load(json); animation.load(json);
animation.setModelId(modelId); animation.setModelId(modelId);
animationMap.put(modelId, animation); animationMap.put(modelId, animation);
@@ -72,6 +71,7 @@ public class GeneratorMain {
if (isGeometryFile(json)) { if (isGeometryFile(json)) {
Geometry geometry = new Geometry(); Geometry geometry = new Geometry();
geometry.load(json); geometry.load(json);
geometry.setPath(currentPath);
geometry.setModelId(modelId); geometry.setModelId(modelId);
geometryMap.put(modelId, geometry); geometryMap.put(modelId, geometry);
} }
@@ -80,6 +80,20 @@ public class GeneratorMain {
} }
} }
} }
if (canAdd) {
entity.setPath(currentPath);
entityMap.put(modelId, entity);
}
}
public static void startGenerate(File source, File output) {
for (File file1 : source.listFiles()) {
if (file1.isDirectory()) {
if (file1.listFiles() == null) {
continue;
}
generateFromFolder("", file1);
} }
} }
@@ -89,18 +103,19 @@ public class GeneratorMain {
File texturesFolder = new File(output, "textures/entity"); File texturesFolder = new File(output, "textures/entity");
File manifestFile = new File(output, "manifest.json");
boolean generateManifest = false; boolean generateManifest = false;
if (!entityFolder.exists()) { if (!entityFolder.exists()) {
generateManifest = true; generateManifest = true;
} }
File[] files = entityFolder.listFiles(); File[] files = entityFolder.listFiles();
if (files == null || files.length != entityMap.size()) { if (!manifestFile.exists() || files == null || files.length != entityMap.size()) {
generateManifest = true; generateManifest = true;
} }
if (generateManifest) { if (generateManifest) {
output.mkdirs(); output.mkdirs();
Path path = new File(output, "manifest.json").toPath(); Path path = manifestFile.toPath();
if (path.toFile().exists()) { if (path.toFile().exists()) {
try { try {
JsonObject manifest = new JsonParser().parse(Files.readString(path)).getAsJsonObject(); JsonObject manifest = new JsonParser().parse(Files.readString(path)).getAsJsonObject();
@@ -124,56 +139,63 @@ public class GeneratorMain {
modelsFolder.mkdirs(); modelsFolder.mkdirs();
texturesFolder.mkdirs(); texturesFolder.mkdirs();
for (Map.Entry<String, Animation> stringAnimationEntry : animationMap.entrySet()) { for (Map.Entry<String, Animation> entry : animationMap.entrySet()) {
stringAnimationEntry.getValue().modify(); entry.getValue().modify();
Geometry geo = geometryMap.get(stringAnimationEntry.getKey()); Geometry geo = geometryMap.get(entry.getKey());
if (geo != null) { if (geo != null) {
stringAnimationEntry.getValue().addHeadBind(geo); entry.getValue().addHeadBind(geo);
} }
Path path = animationsFolder.toPath().resolve(stringAnimationEntry.getKey() + ".animation.json"); Path path = animationsFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".animation.json");
path.toFile().getParentFile().mkdirs();
if (path.toFile().exists()) { if (path.toFile().exists()) {
continue; continue;
} }
try { try {
Files.writeString(path, GSON.toJson(stringAnimationEntry.getValue().getJson()), StandardCharsets.UTF_8); Files.writeString(path, GSON.toJson(entry.getValue().getJson()), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
for (Map.Entry<String, Geometry> stringGeometryEntry : geometryMap.entrySet()) { for (Map.Entry<String, Geometry> entry : geometryMap.entrySet()) {
stringGeometryEntry.getValue().modify(); entry.getValue().modify();
Path path = modelsFolder.toPath().resolve(stringGeometryEntry.getKey() + ".geo.json"); Path path = modelsFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".geo.json");
path.toFile().getParentFile().mkdirs();
if (path.toFile().exists()) { if (path.toFile().exists()) {
continue; continue;
} }
try { try {
Files.writeString(path, GSON.toJson(stringGeometryEntry.getValue().getJson()), StandardCharsets.UTF_8); Files.writeString(path, GSON.toJson(entry.getValue().getJson()), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
for (Map.Entry<String, Texture> stringTextureEntry : textureMap.entrySet()) { for (Map.Entry<String, Texture> entry : textureMap.entrySet()) {
Path path = texturesFolder.toPath().resolve(stringTextureEntry.getKey() + ".png"); Path path = texturesFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".png");
path.toFile().getParentFile().mkdirs();
if (path.toFile().exists()) { if (path.toFile().exists()) {
continue; continue;
} }
try { try {
Files.copy(stringTextureEntry.getValue().getPath(), path, StandardCopyOption.REPLACE_EXISTING); Files.copy(entry.getValue().getOriginalPath(), path, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
for (Map.Entry<String, Entity> stringEntityEntry : entityMap.entrySet()) { for (Map.Entry<String, Entity> entry : entityMap.entrySet()) {
stringEntityEntry.getValue().modify(); entry.getValue().modify();
Path path = entityFolder.toPath().resolve(stringEntityEntry.getKey() + ".entity.json"); Path path = entityFolder.toPath().resolve(entry.getValue().getPath() + entry.getKey() + ".entity.json");
path.toFile().getParentFile().mkdirs();
if (path.toFile().exists()) { if (path.toFile().exists()) {
continue; continue;
} }
try { try {
Files.writeString(path, stringEntityEntry.getValue().getJson(), StandardCharsets.UTF_8); Files.writeString(path, entry.getValue().getJson(), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -34,6 +34,8 @@ public class Animation {
String modelId; String modelId;
JsonObject json; JsonObject json;
String path;
public void load(String json) { public void load(String json) {
this.json = new JsonParser().parse(json).getAsJsonObject(); this.json = new JsonParser().parse(json).getAsJsonObject();
} }

View File

@@ -28,14 +28,15 @@ public class Entity {
"default": "%geometry%" "default": "%geometry%"
}, },
"animations": { "animations": {
"default": "animation.%entity_id%.idle", "default": "animation.%entity_id%",
"look_at_target": "%look_at_target%" "look_at_target": "%look_at_target%"
}, },
"scripts": { "scripts": {
"animate": [ "animate": [
"default", "default",
"look_at_target" "look_at_target",
"spawn"
] ]
}, },
"render_controllers": [ "render_controllers": [
@@ -51,6 +52,8 @@ public class Entity {
String json; String json;
boolean hasHeadAnimation = false; boolean hasHeadAnimation = false;
String path;
Properties properties = new Properties(); Properties properties = new Properties();
public Entity(String modelId) { public Entity(String modelId) {
@@ -60,7 +63,7 @@ public class Entity {
public void modify() { public void modify() {
json = TEMPLATE.replace("%entity_id%", modelId) json = TEMPLATE.replace("%entity_id%", modelId)
.replace("%geometry%", "geometry.modelengine_" + modelId) .replace("%geometry%", "geometry.modelengine_" + modelId)
.replace("%texture%", "textures/entity/" + modelId) .replace("%texture%", "textures/entity/" + path + modelId)
.replace("%look_at_target%", "animation." + modelId + ".look_at_target") .replace("%look_at_target%", "animation." + modelId + ".look_at_target")
.replace("%material%", properties.getProperty("material", "entity_alphatest")) .replace("%material%", properties.getProperty("material", "entity_alphatest"))
.replace("%render_controller%", properties.getProperty("render_controller", "controller.render.default")) .replace("%render_controller%", properties.getProperty("render_controller", "controller.render.default"))

View File

@@ -19,6 +19,8 @@ public class Geometry {
String modelId; String modelId;
JsonObject json; JsonObject json;
String path;
public void load(String json) { public void load(String json) {
this.json = new JsonParser().parse(json).getAsJsonObject(); this.json = new JsonParser().parse(json).getAsJsonObject();
} }

View File

@@ -12,5 +12,8 @@ import java.nio.file.Path;
public class Texture { public class Texture {
String modelId; String modelId;
Path path;
String path;
Path originalPath;
} }