Skip to content

Commit

Permalink
修添加SimpleModelResource
Browse files Browse the repository at this point in the history
  • Loading branch information
CSneko committed Nov 10, 2024
1 parent 49a3f9a commit d6c2502
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 57 deletions.
4 changes: 4 additions & 0 deletions common/src/main/java/org/ayamemc/ayame/Ayame.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package org.ayamemc.ayame;

import org.ayamemc.ayame.model.DefaultModels;
import org.ayamemc.ayame.model.resource.ModelResourceRegistry;
import org.ayamemc.ayame.util.ConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,5 +32,7 @@ public final class Ayame {

public static void init() {
ConfigUtil.init();
ModelResourceRegistry.init();
DefaultModels.init();
}
}
18 changes: 18 additions & 0 deletions common/src/main/java/org/ayamemc/ayame/model/DefaultModels.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

package org.ayamemc.ayame.model;

import org.ayamemc.ayame.model.resource.IModelResource;
import org.ayamemc.ayame.model.resource.ModelContent;
import org.ayamemc.ayame.model.resource.ModelResourceRegistry;
import org.ayamemc.ayame.util.FileUtil;

import java.nio.file.Path;

import static org.ayamemc.ayame.util.ResourceLocationHelper.withAyameNamespace;

public class DefaultModels {
Expand All @@ -30,4 +37,15 @@ public class DefaultModels {
withAyameNamespace("textures/ayame/default"),
withAyameNamespace("metadata/ayame/default")
);

public static final IModelResource AQUARTER_NEKO_RESOURCE = create("aquarter_neko");

// 静态初始化
public static void init(){}

private static IModelResource create(String name) {
Path targetPath = Path.of(MODEL_PATH + name + ".zip");
FileUtil.copyResource("assets/ayame/models/"+name+".zip", targetPath);
return ModelResourceRegistry.create(ModelContent.create().createZipPack(targetPath));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Custom player model mod. Powered by GeckoLib.
* Copyright (C) 2024 CrystalNeko, HappyRespawnanchor, pertaz(Icon Designer)
*
* This file is part of Ayame.
*
* Ayame is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ayame is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ayame. If not, see <https://www.gnu.org/licenses/>.
*/

package org.ayamemc.ayame.model.resource;

import net.minecraft.server.packs.repository.PackDetector;
import net.minecraft.world.level.validation.DirectoryValidator;
import org.ayamemc.ayame.Ayame;
import org.ayamemc.ayame.util.TODO;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;

public class ModelContent extends PackDetector<ModelResourceRegistry.ModelFile> {

public ModelContent(DirectoryValidator validator) {
super(validator);
}

@Nullable
@Override
public ModelResourceRegistry.ModelFile createZipPack(Path path) {
try (ZipFile zipFile = new ZipFile(path.toFile())) {
ZipEntry entry = zipFile.getEntry("index.json");
if (entry != null) {
return new ModelResourceRegistry.ModelFile(zipFile);
}
} catch (IOException e) {
Ayame.LOGGER.error("Failed to read ZIP pack at path: {}", path, e);
}
return null;
}

@Override
protected ModelResourceRegistry.ModelFile createDirectoryPack(Path path) {
throw new TODO("createDirectoryPack");
// Path indexFilePath = path.resolve("index.json");
// if (Files.exists(indexFilePath)) {
// return new ModelResourceRegistry.ModelFile(null);
// }
// return null;
}

public static ModelContent create() {
return new ModelContent(new DirectoryValidator(path1 -> true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

package org.ayamemc.ayame.model.resource;

import org.ayamemc.ayame.util.FileUtil;
import org.ayamemc.ayame.util.JsonInterpreter;
import org.ayamemc.ayame.util.TODO;
import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipFile;

/**
* 模型格式的注册表
Expand Down Expand Up @@ -79,16 +82,24 @@ public static boolean contains(ModelFile modelFile){
return contains(modelFile.getFormat());
}

@ApiStatus.Internal
public static void init() {
// 注册默认的模型格式
register("SIMPLE", SimpleModelResource::new);
}

@FunctionalInterface
public interface ResourceFactory {
// TODO: 此处传入一个已经加载后的文件的实例,和之前的ZipManager类似
IModelResource create(ModelFile modelFile);
}

public static class ModelFile{
// TODO 这里起暂时的占位符作用,后面请修改它
private final ZipFile zipFile;
public ModelFile(ZipFile zipFile){
this.zipFile = zipFile;
}
public JsonInterpreter getIndexJson(){
throw new TODO("Model File");
return JsonInterpreter.of(FileUtil.getInputStreamFromZip(zipFile, "index.json"));
}

public String getFormat(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Custom player model mod. Powered by GeckoLib.
* Copyright (C) 2024 CrystalNeko, HappyRespawnanchor, pertaz(Icon Designer)
*
* This file is part of Ayame.
*
* Ayame is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ayame is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ayame. If not, see <https://www.gnu.org/licenses/>.
*/

package org.ayamemc.ayame.model.resource;

import org.ayamemc.ayame.model.IndexData;

import java.util.List;

public class SimpleModelResource implements IModelResource{
private final ModelResourceRegistry.ModelFile modelFile;
private final IndexData.ModelMetaData metaData;
public SimpleModelResource(ModelResourceRegistry.ModelFile modelFile){
this.modelFile = modelFile;
this.metaData = createMetaData();
}
private IndexData.ModelMetaData createMetaData(){
return IndexData.ModelMetaData.Builder.create().parseJson(modelFile.getIndexJson()).build();
}

@Override
public IndexData.ModelMetaData getMetaData() {
return this.metaData;
}

@Override
public List<IndexData.ModelData> getModels() {
return List.of(IndexData.ModelData.Builder.create()
.model("model.json")
.name(getMetaData().name())
.animation("animation.json")
.texture("texture.png")
.build()
);
}
}
32 changes: 32 additions & 0 deletions common/src/main/java/org/ayamemc/ayame/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static org.ayamemc.ayame.Ayame.LOGGER;

Expand Down Expand Up @@ -169,4 +171,34 @@ public static void copyResource(String resourcePath, Path targetPath) {
}
}

/**
* 从 ZipFile 中获取指定文件的 InputStream。
*
* @param zipFile ZipFile 对象
* @param entryName 要读取的文件名
* @return 指定文件的 InputStream,如果文件不存在则返回 null
*/
public static InputStream getInputStreamFromZip(ZipFile zipFile, String entryName) {
if (zipFile == null || entryName == null || entryName.isEmpty()) {
return null;
}

// 获取指定文件的 ZipEntry 对象
ZipEntry zipEntry = zipFile.getEntry(entryName);

if (zipEntry == null) {
// 文件不存在
return null;
}

try {
// 返回文件的 InputStream
return zipFile.getInputStream(zipEntry);
} catch (Exception e) {
// 处理异常,例如文件读取失败
LOGGER.error("Error reading file from ZipFile: ", e);
return null;
}
}

}
49 changes: 0 additions & 49 deletions common/src/main/java/org/ayamemc/ayame/util/ModelContent.java

This file was deleted.

Binary file not shown.
Binary file added examples/AQuarter_neko/AQuarter_neko.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"format": "SIMPLE",
"name": "AQuarter_neko",
"authors": [
"CrystalNeko"
Expand All @@ -10,8 +11,5 @@
],
"tags": [
"vanilla-like"
],
"animations": [
],
"version": "0.0.1"
]
}
2 changes: 1 addition & 1 deletion examples/test/index.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"format_version": "0.0.1",
"format": "AYAME",
"metadata": {
"name": "AQuarter_neko",
"version": "0.0.1",
Expand Down

0 comments on commit d6c2502

Please sign in to comment.