From 3cc6b923edb69907794c3dc6daa61a445b3a56c0 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 8 Apr 2024 01:21:41 +0200 Subject: Added 3ds2mag converter Solidified the converters a bit to prevent selected segvs/page faults. --- MagicEx/CMakeLists.txt | 15 ++++++++++----- MagicEx/include/MagicLoad.h | 4 +--- MagicEx/src/3ds2mag.cpp | 23 +++++++++++++++++++++++ MagicEx/src/convert.h | 22 ++++++++++++++++++++++ MagicEx/src/convert.inl.h | 39 +++++++++++++++++++++++++++++++++++++++ MagicEx/src/mag2obj.cpp | 18 +++++++----------- MagicEx/src/obj2mag.cpp | 18 +++++++----------- 7 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 MagicEx/src/3ds2mag.cpp create mode 100644 MagicEx/src/convert.h create mode 100644 MagicEx/src/convert.inl.h diff --git a/MagicEx/CMakeLists.txt b/MagicEx/CMakeLists.txt index 5c552fc..8d81e81 100644 --- a/MagicEx/CMakeLists.txt +++ b/MagicEx/CMakeLists.txt @@ -9,9 +9,9 @@ target_include_directories(${PROJECT_NAME} PUBLIC include/) target_link_libraries(${PROJECT_NAME} PUBLIC StarsEx PRIVATE l3ds) -add_executable(${PROJECT_NAME}_obj2mag src/obj2mag.cpp) -target_link_libraries(${PROJECT_NAME}_obj2mag PRIVATE ${PROJECT_NAME}) -set_target_properties(${PROJECT_NAME}_obj2mag PROPERTIES OUTPUT_NAME obj2mag) +add_executable(${PROJECT_NAME}_3ds2mag src/3ds2mag.cpp) +target_link_libraries(${PROJECT_NAME}_3ds2mag PRIVATE ${PROJECT_NAME}) +set_target_properties(${PROJECT_NAME}_3ds2mag PROPERTIES OUTPUT_NAME 3ds2mag) add_executable(${PROJECT_NAME}_mag2obj src/mag2obj.cpp) @@ -19,6 +19,11 @@ target_link_libraries(${PROJECT_NAME}_mag2obj PRIVATE ${PROJECT_NAME}) set_target_properties(${PROJECT_NAME}_mag2obj PROPERTIES OUTPUT_NAME mag2obj) +add_executable(${PROJECT_NAME}_obj2mag src/obj2mag.cpp) +target_link_libraries(${PROJECT_NAME}_obj2mag PRIVATE ${PROJECT_NAME}) +set_target_properties(${PROJECT_NAME}_obj2mag PROPERTIES OUTPUT_NAME obj2mag) + + install( - TARGETS ${PROJECT_NAME}_obj2mag ${PROJECT_NAME}_mag2obj RUNTIME - COMPONENT Tools DESTINATION ${CMAKE_INSTALL_PREFIX}) + TARGETS ${PROJECT_NAME}_3ds2mag ${PROJECT_NAME}_mag2obj ${PROJECT_NAME}_obj2mag + RUNTIME COMPONENT Tools DESTINATION ${CMAKE_INSTALL_PREFIX}) diff --git a/MagicEx/include/MagicLoad.h b/MagicEx/include/MagicLoad.h index f447945..c1322bd 100644 --- a/MagicEx/include/MagicLoad.h +++ b/MagicEx/include/MagicLoad.h @@ -8,8 +8,6 @@ #include -#include - #include #include @@ -17,7 +15,7 @@ int LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate=false); int LoadTexture(const char* name, Bitmap*& bmp, int type=0); int LoadAlpha(const char* name, Bitmap& bitmap, int type=0); -template bool ImportInto(const std::filesystem::path& pathname, Solid* target); +template bool ImportInto(const char* pathname, Solid* target); #include "MagicLoad.inl.h" diff --git a/MagicEx/src/3ds2mag.cpp b/MagicEx/src/3ds2mag.cpp new file mode 100644 index 0000000..3bf9460 --- /dev/null +++ b/MagicEx/src/3ds2mag.cpp @@ -0,0 +1,23 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#include + +#include +#include + +#include "convert.h" + + +int +main(int argc, char* argv[]) +{ + if (argc < 2) + return 2; + const std::string input {argv[1]}; + const auto output = input + ".mag"; + return starshatter::convert(input, output); +} diff --git a/MagicEx/src/convert.h b/MagicEx/src/convert.h new file mode 100644 index 0000000..9bba5b8 --- /dev/null +++ b/MagicEx/src/convert.h @@ -0,0 +1,22 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#pragma once + +#include + + +namespace starshatter +{ + + +template int convert(const std::string& input, const std::string& output); + + +} // namespace starshatter + + +#include "convert.inl.h" diff --git a/MagicEx/src/convert.inl.h b/MagicEx/src/convert.inl.h new file mode 100644 index 0000000..7bf29ee --- /dev/null +++ b/MagicEx/src/convert.inl.h @@ -0,0 +1,39 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. +*/ + +#include +#include + +#include +#include + +#include + + +namespace starshatter +{ + + +template +int +convert(const std::string& input, const std::string& output) +{ + DataLoader::Initialize(); + auto solid = std::make_unique(); + if (!ImportInto(input.c_str(), solid.get())) + return 1; + Output exporter {output.c_str()}; + auto* model = solid->GetModel(); + if (model == nullptr) + return 1; + if (!exporter.Save(model)) + return 1; + DataLoader::Close(); + return 0; +} + + +} // namespace starshatter diff --git a/MagicEx/src/mag2obj.cpp b/MagicEx/src/mag2obj.cpp index a8ba386..1439ac7 100644 --- a/MagicEx/src/mag2obj.cpp +++ b/MagicEx/src/mag2obj.cpp @@ -4,24 +4,20 @@ Copyright (c) 1997-2006, Destroyer Studios LLC. */ -#include +#include -#include - -#include #include #include +#include "convert.h" + int main(int argc, char* argv[]) { if (argc < 2) - return 1; - std::string pathname {argv[1]}; - auto solid = std::make_unique(); - if (!ImportInto(pathname.c_str(), solid.get())) - return 1; - ModelFileOBJ exporter {(pathname + ".obj").c_str()}; - exporter.Save(solid->GetModel()); + return 2; + const std::string input {argv[1]}; + const auto output = input + ".obj"; + return starshatter::convert(input, output); } diff --git a/MagicEx/src/obj2mag.cpp b/MagicEx/src/obj2mag.cpp index c9d22e0..5b71bb5 100644 --- a/MagicEx/src/obj2mag.cpp +++ b/MagicEx/src/obj2mag.cpp @@ -4,24 +4,20 @@ Copyright (c) 1997-2006, Destroyer Studios LLC. */ -#include +#include -#include - -#include #include #include +#include "convert.h" + int main(int argc, char* argv[]) { if (argc < 2) - return 1; - std::string pathname {argv[1]}; - auto solid = std::make_unique(); - if (!ImportInto(pathname.c_str(), solid.get())) - return 1; - ModelFileMAG exporter {(pathname + ".mag").c_str()}; - exporter.Save(solid->GetModel()); + return 2; + const std::string input {argv[1]}; + const auto output = input + ".mag"; + return starshatter::convert(input, output); } -- cgit v1.1