Skip to content

Commit

Permalink
added MDL importer/exporter for Kuro1
Browse files Browse the repository at this point in the history
  • Loading branch information
TwnKey committed Feb 1, 2023
1 parent 1dea83f commit e3d31cc
Show file tree
Hide file tree
Showing 12 changed files with 1,917 additions and 137 deletions.
13 changes: 5 additions & 8 deletions mdl/AssetConfig.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#pragma once
#include <string>
#include <json.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#include <unordered_map>
#include <iostream>
#include "CLEDecrypt.h"
using json = nlohmann::json;
using ordered_json = nlohmann::ordered_json;

struct AssetConfig
{

std::unordered_map<std::string, std::unordered_map< unsigned int, std::string>> map_ids; //Key: Shader name Value: <Key: Switch map name, Value:tex slot value>
std::unordered_map < std::string, std::unordered_map < std::string, unsigned int >> reverse_map_ids;
int bone_limit_per_mesh = -1;

AssetConfig(std::string json_filepath) {
decrypt(json_filepath);
std::ifstream f(json_filepath);
//Be careful, the original asset_config file has comments in it, which are not supported by this lib (rightfully I think)
json data = json::parse(f);
ordered_json data = ordered_json::parse(f);

if (data.count("BoneLimitPerMesh") > 0) {

Expand All @@ -27,15 +28,11 @@ struct AssetConfig
for (auto shader_data : data["ShaderInfos"]) {
for (auto map_ : shader_data["switches"]) {
this->map_ids[shader_data["name"]][map_["tex_slot"]] = map_["name"];
this->reverse_map_ids[shader_data["name"]][map_["name"]] = map_["tex_slot"];
}

}

f.close();




}

};
Expand Down
2 changes: 1 addition & 1 deletion mdl/CLEDecrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <crypto++/filters.h>
#include <crypto++/blowfish.h>
#include <crypto++/secblock.h>
#include <zstd.h>
#include <zstd/zstd.h>
#include "Utilities.h"
#include <unordered_set>

Expand Down
54 changes: 34 additions & 20 deletions mdl/ED9AssetExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ int main(int argc, char** argv)
{
std::vector<model> ani_mdls = {};
model base_model;
for (const auto& file : fs::directory_iterator(filepath)) {
filepath = file.path().string();
std::string base_filename = filepath.substr(filepath.find_last_of("/\\") + 1);
std::string::size_type const p(base_filename.find_last_of('.'));
std::string scene_name = base_filename.substr(0, p);
for (const auto& file : fs::directory_iterator(filepath )) {
if (file.path().extension() == ".mdl"){
std::string file_str = file.path().string();
std::string base_filename = file_str.substr(file_str.find_last_of("/\\") + 1);
std::string::size_type const p(base_filename.find_last_of('.'));
std::string scene_name = base_filename.substr(0, p);

decrypt(filepath);
std::ifstream input(filepath, std::ios::binary);
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
MDLFile mdl(scene_name, buffer);
model m = mdl.extract_model(conf);
if (m.meshes.size() != 0)
base_model = m;
else
ani_mdls.push_back(m);
decrypt(file_str);
std::ifstream input(file_str, std::ios::binary);
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
MDLFile mdl(scene_name, buffer);
model m = mdl.extract_model(conf);
if (m.meshes.size() != 0)
base_model = m;
else
ani_mdls.push_back(m);
}
}
for (size_t i = 0; i < ani_mdls.size(); i++) {
base_model.to_merge(ani_mdls[i]);
Expand All @@ -49,12 +51,24 @@ int main(int argc, char** argv)
std::string::size_type const p(base_filename.find_last_of('.'));
std::string scene_name = base_filename.substr(0, p);

decrypt(filepath);
std::ifstream input(filepath, std::ios::binary);
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
MDLFile mdl(scene_name, buffer);
model m = mdl.extract_model(conf);
m.to_fbx(conf);

std::string extension = base_filename.substr(p + 1, base_filename.length());

if (extension.compare("mdl") == 0) {
decrypt(filepath);
std::ifstream input(filepath, std::ios::binary);
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
MDLFile mdl(scene_name, buffer);
model m = mdl.extract_model(conf);
m.to_fbx(conf);
}
else if (extension.compare("fbx") == 0) {

model m(filepath);
m.to_mdl(conf);

}

}
catch (std::exception e) {
std::string msg = e.what();
Expand Down
49 changes: 49 additions & 0 deletions mdl/GenerateParametersLayout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// GenerateParametersLayout.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <MDLFile.h>
#include <model.h>
#include <filesystem>
#include "AssetConfig.h"
#include <cstring>
#include <JsonGenerator.h>

namespace fs = std::filesystem;
int main(int argc, char** argv)
{

if (argc > 1) {
std::error_code ec;
std::string filepath = std::string(argv[1]);
AssetConfig conf("asset_config.json");
if (fs::is_directory(filepath, ec))
{
std::vector<model> ani_mdls = {};
model base_model;
for (const auto& file : fs::directory_iterator(filepath)) {
filepath = file.path().string();
JsonGenerator gen(filepath, conf);

}

}
else {

std::string base_filename = filepath.substr(filepath.find_last_of("/\\") + 1);
try {
JsonGenerator gen(filepath, conf);


}
catch (std::exception e) {
std::string msg = e.what();
std::transform(msg.begin(), msg.end(), msg.begin(), ::toupper);
std::cout << msg << " " << base_filename << std::endl;

}
}
}
}
20 changes: 20 additions & 0 deletions mdl/JsonGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "JsonGenerator.h"
#include <CLEDecrypt.h>
#include <MDLFile.h>
bool JsonGenerator::MDLToJson(std::string filepath, AssetConfig conf) {
std::string base_filename = filepath.substr(filepath.find_last_of("/\\") + 1);
std::string::size_type const p(base_filename.find_last_of('.'));
std::string scene_name = base_filename.substr(0, p);
decrypt(filepath);
std::ifstream input(filepath, std::ios::binary);
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
MDLFile mdl(scene_name, buffer);
mdl.write_to_json(conf);


return true; }
bool JsonGenerator::FBXToJson(std::string filepath) {

model m = model(filepath);
m.to_json();
return true; }
30 changes: 30 additions & 0 deletions mdl/JsonGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <string>
#include <iostream>
#include "AssetConfig.h"
class JsonGenerator
{
public:
JsonGenerator(std::string filepath, AssetConfig conf) {
std::string base_filename = filepath.substr(filepath.find_last_of("/\\") + 1);
std::string::size_type const p(base_filename.find_last_of('.'));
std::string scene_name = base_filename.substr(0, p);
std::string extension = base_filename.substr(p+1, base_filename.length());

if (extension.compare("mdl") == 0)
MDLToJson(filepath, conf);
else if (extension.compare("fbx") == 0)
FBXToJson(filepath);
else
std::cout << "no idea" << std::endl;
/*decrypt(filepath);
std::ifstream input(filepath, std::ios::binary);
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(input), {});
MDLFile mdl(scene_name, buffer);*/
}

bool MDLToJson(std::string filepath, AssetConfig conf);
bool FBXToJson(std::string filepath);

};

Loading

0 comments on commit e3d31cc

Please sign in to comment.