diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ebe883f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,54 @@ +cmake_minimum_required(VERSION 3.14) + +project("srmtools") + +# build type +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the build type" FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo" "MinSizeRel") +endif() +# hide other incomplete configurations from Visual Studio +set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE INTERNAL "Active configuration" FORCE) +mark_as_advanced(FORCE CMAKE_CONFIGURATION_TYPES) + +# installation +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/INSTALL" CACHE PATH "Installation prefix" FORCE) +endif() +set(CMAKE_INSTALL_MESSAGE LAZY) + +set(CMAKE_CXX_STANDARD 17) + +# Visual Studio settings +if(MSVC) + # build MT configuration by default + string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") + string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") + string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}") + + # enable multithreaded build + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") + + # disable annoying warnings + add_definitions(-D_CRT_SECURE_NO_WARNINGS) +endif() +set_property(GLOBAL PROPERTY USE_FOLDERS ON) + +# packaging +set(CPACK_GENERATOR "ZIP") +set(CPACK_PACKAGE_FILE_NAME "srm-tools-0.9") +include(CPack) + +macro(SetTargetCategory target category) + file(GLOB_RECURSE files LIST_DIRECTORIES false RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *) + foreach(i IN LISTS files) + get_filename_component(source_path "${i}" PATH) + string(REPLACE "/" "\\" source_path "${source_path}") + source_group("${source_path}" FILES ${i}) + endforeach() + + set_property(TARGET ${target} PROPERTY FOLDER ${category}) +endmacro() + +add_subdirectory("src") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..2832ea9 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable(srm2sav srm2sav.cxx) +add_executable(sav2srm sav2srm.cxx) + +install(TARGETS srm2sav sav2srm DESTINATION "bin") + +SetTargetCategory(srm2sav "") +SetTargetCategory(sav2srm "") diff --git a/src/sav2srm.cxx b/src/sav2srm.cxx new file mode 100644 index 0000000..38ecbb1 --- /dev/null +++ b/src/sav2srm.cxx @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + + +void sav2srm(const filesystem::path &in_fn, const filesystem::path &out_fn) +{ + ifstream ifs(in_fn, ifstream::binary); + if(!ifs.is_open()) + throw runtime_error("unable to open input file"); + + auto data_size(filesystem::file_size(in_fn)); + + std::vector data(data_size); + ifs.read((char *)data.data(), data_size); + + ofstream ofs(out_fn, ofstream::binary); + if(!ofs.is_open()) + throw runtime_error("unable to create output file"); + + for(auto const &e : data) + { + uint16_t d = e << 8 | 0xff; + ofs.write((char *)&d, sizeof(d)); + } +} + + + +int main(int argc, char *argv[]) +{ + int exit_code(0); + + try + { + if(argc == 2) + { + filesystem::path in_fn(argv[1]); + filesystem::path out_fn(argv[1]); + out_fn.replace_extension(".srm"); + + if(in_fn == out_fn) + throw runtime_error("overwriting input file"); + + sav2srm(in_fn, out_fn); + } + else + { + cout << "usage: sav2srm " << endl; + exit_code = 1; + } + } + catch(const exception &e) + { + cerr << "error: " << e.what() << endl; + exit_code = 2; + } + catch(...) + { + cerr << "error: unhandled exception" << endl; + exit_code = 3; + } + + return exit_code; +} diff --git a/src/srm2sav.cxx b/src/srm2sav.cxx new file mode 100644 index 0000000..bc73fcf --- /dev/null +++ b/src/srm2sav.cxx @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + + +void srm2sav(const filesystem::path &in_fn, const filesystem::path &out_fn) +{ + ifstream ifs(in_fn, ifstream::binary); + if(!ifs.is_open()) + throw runtime_error("unable to open input file"); + + auto data_size(filesystem::file_size(in_fn)); + if(data_size & 1) + throw runtime_error("file is not SRM file"); + + std::vector data(data_size / sizeof(uint16_t)); + ifs.read((char *)data.data(), data_size); + + ofstream ofs(out_fn, ofstream::binary); + if(!ofs.is_open()) + throw runtime_error("unable to create output file"); + + for(auto const &e : data) + { + uint8_t d = e >> 8 & 0xff; + ofs.write((char *)&d, sizeof(d)); + } +} + + + +int main(int argc, char *argv[]) +{ + int exit_code(0); + + try + { + if(argc == 2) + { + filesystem::path in_fn(argv[1]); + filesystem::path out_fn(argv[1]); + out_fn.replace_extension(".sav"); + + if(in_fn == out_fn) + throw runtime_error("overwriting input file"); + + srm2sav(in_fn, out_fn); + } + else + { + cout << "usage: srm2sav " << endl; + exit_code = 1; + } + } + catch(const exception &e) + { + cerr << "error: " << e.what() << endl; + exit_code = 2; + } + catch(...) + { + cerr << "error: unhandled exception" << endl; + exit_code = 3; + } + + return exit_code; +}