dont rewrite all files

This commit is contained in:
zimzaza4
2024-04-21 19:30:53 +08:00
parent a768a4cc64
commit 4cb2825b46
3 changed files with 53 additions and 13 deletions

View File

@@ -39,7 +39,6 @@ public class ExtensionMain implements Extension {
GeneratorMain.startGenerate(source, generatedPack); GeneratorMain.startGenerate(source, generatedPack);
Path generatedPackZip = dataFolder().resolve("generated_pack.zip"); Path generatedPackZip = dataFolder().resolve("generated_pack.zip");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(generatedPackZip))) { try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(generatedPackZip))) {

View File

@@ -2,10 +2,12 @@ package re.imc.geysermodelenginepackgenerator;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import re.imc.geysermodelenginepackgenerator.generator.*; import re.imc.geysermodelenginepackgenerator.generator.*;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
@@ -13,6 +15,7 @@ import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
public class GeneratorMain { public class GeneratorMain {
public static final Map<String, Entity> entityMap = new HashMap<>(); public static final Map<String, Entity> entityMap = new HashMap<>();
@@ -42,12 +45,20 @@ public class GeneratorMain {
} }
String modelId = file1.getName().toLowerCase(); String modelId = file1.getName().toLowerCase();
entityMap.put(modelId, new Entity(modelId)); Entity entity = new Entity(modelId);
entityMap.put(modelId, entity);
for (File e : file1.listFiles()) { for (File e : file1.listFiles()) {
if (e.getName().endsWith(".png")) { if (e.getName().endsWith(".png")) {
textureMap.put(modelId, new Texture(modelId, e.toPath())); textureMap.put(modelId, new Texture(modelId, e.toPath()));
} }
if (e.getName().equals("config.properties")) {
try {
entity.getProperties().load(new FileReader(e));
} catch (IOException ex) {
ex.printStackTrace();
}
}
if (e.getName().endsWith(".json")) { if (e.getName().endsWith(".json")) {
try { try {
String json = Files.readString(e.toPath()); String json = Files.readString(e.toPath());
@@ -83,18 +94,28 @@ public class GeneratorMain {
generateManifest = true; generateManifest = true;
} }
File[] files = entityFolder.listFiles(); File[] files = entityFolder.listFiles();
if (files == null || files.length < entityMap.size()) { if (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 = new File(output, "manifest.json").toPath();
try { if (path.toFile().exists()) {
Files.writeString(path, try {
PackManifest.generate(), StandardCharsets.UTF_8); JsonObject manifest = new JsonParser().parse(Files.readString(path)).getAsJsonObject();
} catch (IOException e) { manifest.get("header").getAsJsonObject().addProperty("uuid", UUID.randomUUID().toString());
e.printStackTrace(); Files.writeString(path, GSON.toJson(manifest));
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
Files.writeString(path,
PackManifest.generate(), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
} }
} }
@@ -110,6 +131,9 @@ public class GeneratorMain {
stringAnimationEntry.getValue().addHeadBind(geo); stringAnimationEntry.getValue().addHeadBind(geo);
} }
Path path = animationsFolder.toPath().resolve(stringAnimationEntry.getKey() + ".animation.json"); Path path = animationsFolder.toPath().resolve(stringAnimationEntry.getKey() + ".animation.json");
if (path.toFile().exists()) {
continue;
}
try { try {
Files.writeString(path, GSON.toJson(stringAnimationEntry.getValue().getJson()), StandardCharsets.UTF_8); Files.writeString(path, GSON.toJson(stringAnimationEntry.getValue().getJson()), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
@@ -120,6 +144,9 @@ public class GeneratorMain {
for (Map.Entry<String, Geometry> stringGeometryEntry : geometryMap.entrySet()) { for (Map.Entry<String, Geometry> stringGeometryEntry : geometryMap.entrySet()) {
stringGeometryEntry.getValue().modify(); stringGeometryEntry.getValue().modify();
Path path = modelsFolder.toPath().resolve(stringGeometryEntry.getKey() + ".geo.json"); Path path = modelsFolder.toPath().resolve(stringGeometryEntry.getKey() + ".geo.json");
if (path.toFile().exists()) {
continue;
}
try { try {
Files.writeString(path, GSON.toJson(stringGeometryEntry.getValue().getJson()), StandardCharsets.UTF_8); Files.writeString(path, GSON.toJson(stringGeometryEntry.getValue().getJson()), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
@@ -129,6 +156,9 @@ public class GeneratorMain {
for (Map.Entry<String, Texture> stringTextureEntry : textureMap.entrySet()) { for (Map.Entry<String, Texture> stringTextureEntry : textureMap.entrySet()) {
Path path = texturesFolder.toPath().resolve(stringTextureEntry.getKey() + ".png"); Path path = texturesFolder.toPath().resolve(stringTextureEntry.getKey() + ".png");
if (path.toFile().exists()) {
continue;
}
try { try {
Files.copy(stringTextureEntry.getValue().getPath(), path, StandardCopyOption.REPLACE_EXISTING); Files.copy(stringTextureEntry.getValue().getPath(), path, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
@@ -139,7 +169,9 @@ public class GeneratorMain {
for (Map.Entry<String, Entity> stringEntityEntry : entityMap.entrySet()) { for (Map.Entry<String, Entity> stringEntityEntry : entityMap.entrySet()) {
stringEntityEntry.getValue().modify(); stringEntityEntry.getValue().modify();
Path path = entityFolder.toPath().resolve(stringEntityEntry.getKey() + ".entity.json"); Path path = entityFolder.toPath().resolve(stringEntityEntry.getKey() + ".entity.json");
if (path.toFile().exists()) {
continue;
}
try { try {
Files.writeString(path, stringEntityEntry.getValue().getJson(), StandardCharsets.UTF_8); Files.writeString(path, stringEntityEntry.getValue().getJson(), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {

View File

@@ -5,6 +5,8 @@ import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import java.util.Properties;
@Getter @Getter
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
@@ -17,7 +19,7 @@ public class Entity {
"description": { "description": {
"identifier": "modelengine:%entity_id%", "identifier": "modelengine:%entity_id%",
"materials": { "materials": {
"default": "entity_alphatest" "default": "%material%"
}, },
"textures": { "textures": {
"default": "%texture%" "default": "%texture%"
@@ -37,7 +39,7 @@ public class Entity {
] ]
}, },
"render_controllers": [ "render_controllers": [
"controller.render.default" "%render_controller%"
] ]
} }
} }
@@ -48,6 +50,9 @@ public class Entity {
String modelId; String modelId;
String json; String json;
boolean hasHeadAnimation = false; boolean hasHeadAnimation = false;
Properties properties = new Properties();
public Entity(String modelId) { public Entity(String modelId) {
this.modelId = modelId; this.modelId = modelId;
} }
@@ -56,7 +61,11 @@ public class Entity {
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/" + modelId)
.replace("%look_at_target%", hasHeadAnimation ? "animation." + modelId + ".look_at_target" : "animation.common.look_at_target") .replace("%look_at_target%", "animation." + modelId + ".look_at_target")
.replace("%material%", properties.getProperty("material", "entity_alphatest"))
.replace("%render_controller%", properties.getProperty("render_controller", "controller.render.default"))
; ;
} }