wip blockModelCombiner

This commit is contained in:
Lukas 2020-11-17 21:31:29 +01:00
parent 7818623c71
commit 43c2c38ec8
10 changed files with 179 additions and 65 deletions

View File

@ -15,5 +15,6 @@ package de.bixilon.minosoft.data;
public enum Mappings { public enum Mappings {
BLOCKS, BLOCKS,
REGISTRIES REGISTRIES,
BLOCK_MODELS
} }

View File

@ -18,6 +18,7 @@ import de.bixilon.minosoft.data.mappings.blocks.Block;
import de.bixilon.minosoft.data.mappings.particle.Particle; import de.bixilon.minosoft.data.mappings.particle.Particle;
import de.bixilon.minosoft.data.mappings.statistics.Statistic; import de.bixilon.minosoft.data.mappings.statistics.Statistic;
import de.bixilon.minosoft.data.mappings.versions.Version; import de.bixilon.minosoft.data.mappings.versions.Version;
import de.bixilon.minosoft.render.blockModels.BlockModelLoader;
import java.util.HashMap; import java.util.HashMap;
@ -38,6 +39,7 @@ public class CustomMapping {
final HashBiMap<Integer, Statistic> statisticIdMap = HashBiMap.create(); final HashBiMap<Integer, Statistic> statisticIdMap = HashBiMap.create();
Version version; Version version;
HashMap<String, HashBiMap<String, Dimension>> dimensionIdentifierMap = new HashMap<>(); HashMap<String, HashBiMap<String, Dimension>> dimensionIdentifierMap = new HashMap<>();
BlockModelLoader blockModelLoader;
public CustomMapping(Version version) { public CustomMapping(Version version) {
this.version = version; this.version = version;
@ -193,6 +195,13 @@ public class CustomMapping {
return version.getMapping().getIdByEnchantment(enchantment); return version.getMapping().getIdByEnchantment(enchantment);
} }
public BlockModelLoader getBlockModelLoader() {
if (blockModelLoader != null) { // TODO: check models
return blockModelLoader;
}
return version.getMapping().getBlockModelLoader();
}
public void unload() { public void unload() {
motiveIdentifierMap.clear(); motiveIdentifierMap.clear();
particleIdentifierMap.clear(); particleIdentifierMap.clear();
@ -208,6 +217,7 @@ public class CustomMapping {
enchantmentMap.clear(); enchantmentMap.clear();
particleIdMap.clear(); particleIdMap.clear();
statisticIdMap.clear(); statisticIdMap.clear();
blockModelLoader.clear();
} }
public void setDimensions(HashMap<String, HashBiMap<String, Dimension>> dimensions) { public void setDimensions(HashMap<String, HashBiMap<String, Dimension>> dimensions) {

View File

@ -22,6 +22,7 @@ import de.bixilon.minosoft.data.mappings.blocks.Blocks;
import de.bixilon.minosoft.data.mappings.particle.Particle; import de.bixilon.minosoft.data.mappings.particle.Particle;
import de.bixilon.minosoft.data.mappings.statistics.Statistic; import de.bixilon.minosoft.data.mappings.statistics.Statistic;
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition; import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import de.bixilon.minosoft.render.blockModels.BlockModelLoader;
import java.util.HashSet; import java.util.HashSet;
@ -41,6 +42,7 @@ public class VersionMapping {
HashBiMap<Integer, Enchantment> enchantmentMap; HashBiMap<Integer, Enchantment> enchantmentMap;
HashBiMap<Integer, Particle> particleIdMap; HashBiMap<Integer, Particle> particleIdMap;
HashBiMap<Integer, Statistic> statisticIdMap; HashBiMap<Integer, Statistic> statisticIdMap;
BlockModelLoader blockModelLoader;
public VersionMapping(Version version) { public VersionMapping(Version version) {
this.version = version; this.version = version;
@ -110,6 +112,10 @@ public class VersionMapping {
return enchantmentMap.inverse().get(enchantment); return enchantmentMap.inverse().get(enchantment);
} }
public BlockModelLoader getBlockModelLoader() {
return blockModelLoader;
}
public void load(Mappings type, JsonObject data) { public void load(Mappings type, JsonObject data) {
switch (type) { switch (type) {
case REGISTRIES -> { case REGISTRIES -> {
@ -191,6 +197,7 @@ public class VersionMapping {
} }
} }
case BLOCKS -> blockMap = Blocks.load("minecraft", data, version.getVersionId() < ProtocolDefinition.FLATTING_VERSION_ID); case BLOCKS -> blockMap = Blocks.load("minecraft", data, version.getVersionId() < ProtocolDefinition.FLATTING_VERSION_ID);
case BLOCK_MODELS -> blockModelLoader = new BlockModelLoader(data);
} }
loaded.add(type); loaded.add(type);
} }
@ -209,6 +216,7 @@ public class VersionMapping {
enchantmentMap.clear(); enchantmentMap.clear();
particleIdMap.clear(); particleIdMap.clear();
statisticIdMap.clear(); statisticIdMap.clear();
blockModelLoader.clear();
} }
public boolean isFullyLoaded() { public boolean isFullyLoaded() {

View File

@ -40,6 +40,7 @@ public class Versions {
static { static {
mappingsHashMap.put("registries", Mappings.REGISTRIES); mappingsHashMap.put("registries", Mappings.REGISTRIES);
mappingsHashMap.put("blocks", Mappings.BLOCKS); mappingsHashMap.put("blocks", Mappings.BLOCKS);
mappingsHashMap.put("blockModels", Mappings.BLOCK_MODELS);
} }
public static Version getVersionById(int versionId) { public static Version getVersionById(int versionId) {

View File

@ -15,7 +15,6 @@ package de.bixilon.minosoft.render;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.network.Connection; import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.render.blockModels.BlockModelLoader;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
@ -35,7 +34,6 @@ public class GameWindow {
new Thread(() -> { new Thread(() -> {
openGLWindow = new OpenGLWindow(); openGLWindow = new OpenGLWindow();
openGLWindow.init(); openGLWindow.init();
BlockModelLoader.blockModelLoader = new BlockModelLoader();
Log.info("Finished loading block models"); Log.info("Finished loading block models");
latch.countDown(); latch.countDown();
mainLoop(); mainLoop();

View File

@ -31,17 +31,16 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
public class BlockModelLoader { public class BlockModelLoader {
TextureLoader textureLoader; private final TextureLoader textureLoader;
public static BlockModelLoader blockModelLoader; private final HashMap<String, HashMap<String, BlockModelInterface>> modelMap;
HashMap<String, HashMap<String, BlockModelInterface>> modelMap;
public BlockModelLoader() { public BlockModelLoader(JsonObject data) {
modelMap = new HashMap<>(); modelMap = new HashMap<>();
HashSet<JsonObject> mods = new HashSet<>(); HashSet<JsonObject> mods = new HashSet<>();
HashMap<String, HashMap<String, float[]>> tints = new HashMap<>(); mods.add(data);
HashMap<String, float[]> tints = null;
try { try {
//TODO: modding tints = readTints(Util.readJsonAsset("mapping/tints.json"));
mods.add(Util.readJsonAsset("mapping/blockModels.json"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -52,7 +51,6 @@ public class BlockModelLoader {
modelMap.put(modName, new HashMap<>()); modelMap.put(modName, new HashMap<>());
} }
blockModels.put(modName, loadModels(mod)); blockModels.put(modName, loadModels(mod));
tints.put(modName, readTints(mod));
} }
textureLoader = new TextureLoader(getTextures(blockModels), tints); textureLoader = new TextureLoader(getTextures(blockModels), tints);
applyTextures(blockModels); applyTextures(blockModels);
@ -61,10 +59,6 @@ public class BlockModelLoader {
} }
} }
public static BlockModelLoader getInstance() {
return blockModelLoader;
}
private void loadBlocks(JsonObject mod, HashMap<String, HashSet<SubBlock>> blockModels) { private void loadBlocks(JsonObject mod, HashMap<String, HashSet<SubBlock>> blockModels) {
for (Map.Entry<String, JsonElement> blockEntry : mod.get("blockStates").getAsJsonObject().entrySet()) { for (Map.Entry<String, JsonElement> blockEntry : mod.get("blockStates").getAsJsonObject().entrySet()) {
JsonObject block = blockEntry.getValue().getAsJsonObject(); JsonObject block = blockEntry.getValue().getAsJsonObject();
@ -151,4 +145,8 @@ public class BlockModelLoader {
public TextureLoader getTextureLoader() { public TextureLoader getTextureLoader() {
return textureLoader; return textureLoader;
} }
public void clear() {
modelMap.clear();
}
} }

View File

@ -40,11 +40,11 @@ public class TextureLoader {
private float step; private float step;
private int totalTextures = 0; private int totalTextures = 0;
public TextureLoader(HashMap<String, HashSet<String>> textures, HashMap<String, HashMap<String, float[]>> tints) { public TextureLoader(HashMap<String, HashSet<String>> textures, HashMap<String, float[]> tints) {
textureCoordinates = new HashMap<>(); textureCoordinates = new HashMap<>();
images = new HashMap<>(); images = new HashMap<>();
for (String mod : textures.keySet()) { for (String mod : textures.keySet()) {
loadTextures(mod, textures.get(mod), tints.get(mod)); loadTextures(mod, textures.get(mod), tints);
} }
combineTextures(); combineTextures();
try { try {

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,17 @@
{
"block/grass_block_top": [
0,
1,
0
],
"block/grass": [
0,
1,
0
],
"block/water_still": [
0,
0,
1
]
}

View File

@ -1,47 +1,85 @@
""" # minosoft
* Minosoft
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
"""
# Minosoft
# Copyright (C) 2020 Moritz Zwerger # Copyright (C) 2020 Moritz Zwerger
# #
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. # This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# #
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
#
# This software is not affiliated with Mojang AB, the original developer of Minecraft.
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details. # This program 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 General Public License for more details.
# #
# You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. # You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
# #
# This software is not affiliated with Mojang AB, the original developer of Minecraft. # This software is not affiliated with Mojang AB, the original developer of Minecraft.
import json import \
import os io
import \
requests
import \
sys
import \
ujson
import \
zipfile
if len(
sys.argv) != 2:
print(
"useage: %s <jar url>".format(
sys.argv[
0]))
sys.exit()
blockStates = {} blockStates = {}
blockModels = {} blockModels = {}
modName = "minecraft" modName = "minecraft"
blockStatesDir = modName + "/blockstates/" print(
blockModelsDir = modName + "/models/block/" "Downloading minecraft jar...")
print("loading blockstates...") request = requests.get(
sys.argv[
1],
allow_redirects=True)
print(
"Unpacking minecraft jar...")
zip = zipfile.ZipFile(
io.BytesIO(
request.content),
"r")
files = zip.namelist()
print(
"Loading blockstates...")
def readRotations(apply, current): def readRotations(
apply,
current):
if "x" in current: if "x" in current:
apply["x"] = current["x"] apply[
"x"] = \
current[
"x"]
if "y" in current: if "y" in current:
apply["y"] = current["y"] apply[
"y"] = \
current[
"y"]
if "z" in current: if "z" in current:
apply["z"] = current["z"] apply[
"z"] = \
current[
"z"]
def readPart(part): def readPart(part):
@ -71,19 +109,38 @@ def readPart(part):
return result return result
for blockStateFile in os.listdir(blockStatesDir): for blockStateFile in [
with open(blockStatesDir + blockStateFile, "r") as file: f
data = json.load(file) for
f
in
files
if
f.startswith(
'assets/minecraft/blockstates/')]:
with zip.open(
blockStateFile) as file:
data = ujson.load(
file)
block = {} block = {}
if "variants" in data: if "variants" in data:
variants = data["variants"] variants = \
data[
"variants"]
states = [] states = []
for variant in variants: for variant in variants:
state = {} state = {}
properties = {} properties = {}
if variant != "": if variant != "":
for part in variant.split(","): for part in variant.split(
properties[part.split("=")[0]] = part.split("=")[1] ","):
properties[
part.split(
"=")[
0]] = \
part.split(
"=")[
1]
state["properties"] = properties state["properties"] = properties
current = variants[variant] current = variants[variant]
if type(current) == type([]): if type(current) == type([]):
@ -95,36 +152,61 @@ for blockStateFile in os.listdir(blockStatesDir):
"states": states "states": states
} }
elif "multipart" in data: elif "multipart" in data:
parts = data["multipart"] parts = \
data[
"multipart"]
conditional = [] conditional = []
for part in parts: for part in parts:
conditional.extend(readPart(part)) conditional.extend(
readPart(
part))
block = { block = {
"conditional": conditional "conditional": conditional
} }
blockStates[blockStateFile.split(".")[0]] = block blockStates[
blockStateFile.split(
".")[
0]] = block
print("loading models...") print(
for blockModelFile in os.listdir(blockModelsDir): "Loading models...")
with open(blockModelsDir + blockModelFile, "r") as file: for blockModelFile in [
data = json.load(file) f
for
f
in
files
if
f.startswith(
'assets/minecraft/models/block/')]:
with zip.open(
blockModelFile) as file:
data = ujson.load(
file)
blockModels[
blockModelFile.split(
".")[
0]] = data
blockModels[blockModelFile.split(".")[0]] = data print(
"Combining files...")
print("combining files...")
finalJson = { finalJson = {
"mod": modName, "mod": modName,
"blockStates": blockStates, "blockStates": blockStates,
"blockModels": blockModels, "blockModels": blockModels,
"tinted_textures": {
"block/grass_block_top": [0, 1, 0],
"block/grass": [0, 1, 0],
"block/water_still": [0, 0, 1]
}
} }
print("saving...") print(
with open("blockModels.json", "w+") as file: "Saving...")
json.dump(finalJson, file) with open(
"../../../AppData/Roaming/Minosoft/assets/assets/blockModels.json",
"w+") as file:
finalJson = ujson.dumps(
finalJson)
file.write(
finalJson.replace(
"minecraft:",
""))
print("finished succesfully") print(
"Finished succesfully")