From f90035309021e1449e0ddc6649ee68edd1005ab8 Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Thu, 2 Aug 2018 00:36:18 +0300 Subject: [PATCH] Overhaul with modern command lines. --- cppan.yml | 6 +- src/common/mat.h | 128 ++++++++++++------------ src/db_add_language/db_add_language.cpp | 19 ++-- src/db_extractor/db_extractor.cpp | 19 ++-- src/mmm_extractor/mmm_extractor.cpp | 11 +- src/mmp_extractor/mmp.cpp | 33 +++--- src/mmp_extractor/mmp.h | 18 ++-- src/mmp_extractor/mmp_extractor.cpp | 19 ++-- src/mod_reader/mod_reader.cpp | 47 ++------- src/mpj_loader/mpj.cpp | 2 +- src/mpj_loader/mpj.h | 14 +-- src/mpj_loader/mpj_loader.cpp | 12 +-- src/save_loader/save_loader.cpp | 10 +- src/script2txt/script2txt.cpp | 10 +- src/tm_converter/tm_converter.cpp | 11 +- 15 files changed, 167 insertions(+), 192 deletions(-) diff --git a/cppan.yml b/cppan.yml index cac07bc..1eca10a 100644 --- a/cppan.yml +++ b/cppan.yml @@ -71,9 +71,6 @@ projects: dependencies: - common - unpaker: - root_dir: src/unpaker - mmm_extractor: root_dir: src/mmm_extractor dependencies: @@ -161,6 +158,9 @@ projects: post_target: | cppan_flex_bison_pair(LALR1_CPP_VARIANT_PARSER script2txt) + unpaker: + root_dir: src/unpaker + diff --git a/src/common/mat.h b/src/common/mat.h index 26b333d..4cc7cc5 100644 --- a/src/common/mat.h +++ b/src/common/mat.h @@ -18,13 +18,15 @@ #pragma once +#include +#include + +#include + #include #include #include -#include -#include - template class mat { @@ -69,13 +71,13 @@ public: int getBytesLength() const { return size() * sizeof(T); } int getPos(const T *const elem) const { return elem - &data[0]; } const std::vector &getData() const { return data; } - std::vector &getData() { return data; } - - // left/right - mat mirror() + std::vector &getData() { return data; } + + // left/right + mat mirror() { int cols = width; - int rows = height; + int rows = height; mat m(width, height); for (int row = 0; row < rows; row++) { @@ -87,13 +89,13 @@ public: } } return m; - } - - // up/down - mat flip() + } + + // up/down + mat flip() { int cols = width; - int rows = height; + int rows = height; mat m(width, height); for (int row = 0; row < rows; row++) { @@ -111,57 +113,57 @@ private: std::vector data; int width; int height; -}; - -inline void write_mat_bmp(const std::string &filename, int width, int height, int bits, const uint8_t *b, size_t s) +}; + +inline void write_mat_bmp(const path &filename, int width, int height, int bits, const uint8_t *b, size_t s) { - FILE *f = fopen(filename.c_str(), "wb"); - if (f == nullptr) - return; - bmp_header h = { 0 }; - h.bfType = 0x4D42; - h.bfSize = sizeof(bmp_header) + sizeof(bmp_info_header) + s; - h.bfOffBits = sizeof(bmp_header) + sizeof(bmp_info_header); - bmp_info_header i = { 0 }; - i.biSize = sizeof(i); - i.biWidth = width; - i.biHeight = height; - i.biPlanes = 1; - i.biBitCount = bits; - i.biCompression = 0; - i.biSizeImage = 0; - i.biXPelsPerMeter = 0; - i.biYPelsPerMeter = 0; - i.biClrUsed = 0; - i.biClrImportant = 0; - fwrite(&h, sizeof(bmp_header), 1, f); - fwrite(&i, sizeof(bmp_info_header), 1, f); - fwrite(b, s, 1, f); - fclose(f); -} - -template -inline void write_mat_bmp(const std::string &filename, const mat &m) + auto f = primitives::filesystem::fopen(filename, "wb"); + if (f == nullptr) + return; + bmp_header h = { 0 }; + h.bfType = 0x4D42; + h.bfSize = sizeof(bmp_header) + sizeof(bmp_info_header) + s; + h.bfOffBits = sizeof(bmp_header) + sizeof(bmp_info_header); + bmp_info_header i = { 0 }; + i.biSize = sizeof(i); + i.biWidth = width; + i.biHeight = height; + i.biPlanes = 1; + i.biBitCount = bits; + i.biCompression = 0; + i.biSizeImage = 0; + i.biXPelsPerMeter = 0; + i.biYPelsPerMeter = 0; + i.biClrUsed = 0; + i.biClrImportant = 0; + fwrite(&h, sizeof(bmp_header), 1, f); + fwrite(&i, sizeof(bmp_info_header), 1, f); + fwrite(b, s, 1, f); + fclose(f); +} + +template +void write_mat_bmp(const path &filename, const mat &m) +{ + write_mat_bmp(filename, m.getWidth(), m.getHeight(), sizeof(T) * CHAR_BIT, (const uint8_t *)&m(0, 0), m.size() * sizeof(T)); +} + +template +void write_mat_tga(const path &filename, const mat &m) { - write_mat_bmp(filename, m.getWidth(), m.getHeight(), sizeof(T) * CHAR_BIT, (const uint8_t *)&m(0, 0), m.size() * sizeof(T)); -} + auto f = primitives::filesystem::fopen(filename, "wb"); + if (f == nullptr) + return; -template -inline void write_mat_tga(const std::string &filename, const mat &m) -{ - FILE *f = fopen(filename.c_str(), "wb"); - if (f == nullptr) - return; - - tga t; - t.width = m.getWidth(); - t.height = m.getHeight(); - - // header - fwrite(&t, sizeof(tga), 1, f); + tga t; + t.width = m.getWidth(); + t.height = m.getHeight(); + + // header + fwrite(&t, sizeof(tga), 1, f); fwrite(t.label(), t.idlength, 1, f); - - // data - fwrite(&m(0, 0), m.size() * sizeof(T), 1, f); - fclose(f); -} + + // data + fwrite(&m(0, 0), m.size() * sizeof(T), 1, f); + fclose(f); +} diff --git a/src/db_add_language/db_add_language.cpp b/src/db_add_language/db_add_language.cpp index 0e4a2e9..a778cf5 100644 --- a/src/db_add_language/db_add_language.cpp +++ b/src/db_add_language/db_add_language.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -285,21 +286,19 @@ void process_lang(polygon4::Storage &s, const path &p, polygon4::String polygon4 int main(int argc, char *argv[]) { - if (argc != 3) - { - std::cout << "Usage: prog db.sqlite dir_to_lang_dbs" << "\n"; - return 1; - } - path d = argv[2]; + cl::opt db_fn(cl::Positional, cl::desc(""), cl::Required); + cl::opt dir_to_lang_dbs(cl::Positional, cl::desc(""), cl::Required); - auto storage = polygon4::initStorage(argv[1]); + cl::ParseCommandLineOptions(argc, argv); + + auto storage = polygon4::initStorage(db_fn); storage->load(); - kv_resolved = get_kv_resolved(d, *storage.get()); + kv_resolved = get_kv_resolved(dir_to_lang_dbs, *storage.get()); // to check correctness - process_lang(*storage.get(), d / "ru", &polygon4::LocalizedString::ru); + process_lang(*storage.get(), dir_to_lang_dbs / "ru", &polygon4::LocalizedString::ru); - for (auto &f : fs::directory_iterator(d)) + for (auto &f : fs::directory_iterator(dir_to_lang_dbs)) { if (!fs::is_directory(f)) continue; diff --git a/src/db_extractor/db_extractor.cpp b/src/db_extractor/db_extractor.cpp index a872874..07e2809 100644 --- a/src/db_extractor/db_extractor.cpp +++ b/src/db_extractor/db_extractor.cpp @@ -22,12 +22,13 @@ #include #include +#include #include -void create_sql(std::string path, const db &db) +void create_sql(path p, const db &db) { - std::ofstream ofile(path + ".sql"); + std::ofstream ofile(p += ".sql"); if (!ofile) return; @@ -125,14 +126,12 @@ void create_sql(std::string path, const db &db) int main(int argc, char *argv[]) { - if (argc != 2) - { - std::cout << "Usage:\n" << argv[0] << " path/to/aim_game/data/db" << "\n" << argv[0] << " path/to/aim_game/data/quest" << "\n"; - return 1; - } - path p = argv[1]; + cl::opt db_fn(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); + db db; - db.open(p); - create_sql(p.string(), db); + db.open(db_fn); + create_sql(db_fn, db); return 0; } diff --git a/src/mmm_extractor/mmm_extractor.cpp b/src/mmm_extractor/mmm_extractor.cpp index 65ec776..d7064db 100644 --- a/src/mmm_extractor/mmm_extractor.cpp +++ b/src/mmm_extractor/mmm_extractor.cpp @@ -26,6 +26,7 @@ #include #include +#include using namespace std; @@ -66,12 +67,10 @@ void process_mmm(const path &fn) int main(int argc, char *argv[]) { - if (argc != 2) - { - cout << "Usage:\n" << argv[0] << " {file.mmm,dir}" << "\n"; - return 1; - } - path p = argv[1]; + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); + if (fs::is_regular_file(p)) process_mmm(p); else if (fs::is_directory(p)) diff --git a/src/mmp_extractor/mmp.cpp b/src/mmp_extractor/mmp.cpp index 3ecba91..cd09ef4 100644 --- a/src/mmp_extractor/mmp.cpp +++ b/src/mmp_extractor/mmp.cpp @@ -117,14 +117,14 @@ void mmp::load(const buffer &b) } } -void mmp::load(const std::string &fn) +void mmp::load(const path &fn) { filename = fn; buffer b(read_file(filename)); load(b); } -void mmp::loadTextureNames(const std::string &fn) +void mmp::loadTextureNames(const path &fn) { std::ifstream ifile(fn); while (ifile) @@ -254,7 +254,8 @@ void mmp::process() void mmp::writeFileInfo() { - std::ofstream ofile(filename + ".info.txt"); + auto fn = filename; + std::ofstream ofile(fn += ".info.txt"); if (!ofile) return; ofile << "width: " << h.width << "\n"; @@ -270,7 +271,8 @@ void mmp::writeFileInfo() void mmp::writeTexturesList() { - std::ofstream ofile(filename + ".textures.txt"); + auto fn = filename; + std::ofstream ofile(fn += ".textures.txt"); if (!ofile) return; for (auto &t : textures) @@ -291,8 +293,9 @@ void mmp::writeTexturesList() void mmp::writeHeightMap() { - auto fn = filename + ".heightmap16.r16"; - FILE *f = fopen(fn.c_str(), "wb"); + auto fn = filename; + fn += ".heightmap16.r16"; + auto f = primitives::filesystem::fopen(fn, "wb"); if (f == nullptr) return; fwrite(&heightmap(0, 0), heightmap.size() * sizeof(decltype(heightmap)::type), 1, f); @@ -311,7 +314,8 @@ void mmp::writeHeightMapSegmented() void mmp::writeTextureMap() { - auto fn = filename + ".texmap.bmp"; + auto fn = filename; + fn += ".texmap.bmp"; write_mat_bmp(fn, texmap); } @@ -328,31 +332,36 @@ void mmp::writeTextureAlphaMaps() break; } } - auto fn = filename + ".texmap." + std::to_string(t.first) + "." + std::to_string(tex_id) + ".bmp"; + auto fn = filename; + fn += ".texmap." + std::to_string(t.first) + "." + std::to_string(tex_id) + ".bmp"; write_mat_bmp(fn, t.second); } } void mmp::writeTextureMapColored() { - auto fn = filename + ".texmap.colored.bmp"; + auto fn = filename; + fn += ".texmap.colored.bmp"; write_mat_bmp(fn, texmap_colored); } void mmp::writeColorMap() { - auto fn = filename + ".colormap.bmp"; + auto fn = filename; + fn += ".colormap.bmp"; write_mat_bmp(fn, colormap); } void mmp::writeShadowMap() { - auto fn = filename + ".shadowmap.bmp"; + auto fn = filename; + fn += ".shadowmap.bmp"; write_mat_bmp(fn, shadowmap); } void mmp::writeNormalMap() { - auto fn = filename + ".normalmap.bmp"; + auto fn = filename; + fn += ".normalmap.bmp"; write_mat_bmp(fn, normalmap); } diff --git a/src/mmp_extractor/mmp.h b/src/mmp_extractor/mmp.h index fdf1713..f685093 100644 --- a/src/mmp_extractor/mmp.h +++ b/src/mmp_extractor/mmp.h @@ -16,17 +16,19 @@ * along with this program. If not, see . */ +#include +#include +#include +#include + +#include + #include #include #include #include #include -#include -#include -#include -#include - using Height = float; enum class HeaderSegmentType : uint32_t @@ -165,7 +167,7 @@ struct mmp std::vector segments; // - std::string filename; + path filename; int xsegs; int ysegs; std::map textures; @@ -186,8 +188,8 @@ struct mmp mat normalmap; void load(const buffer &b); - void load(const std::string &filename); - void loadTextureNames(const std::string &filename); + void load(const path &filename); + void loadTextureNames(const path &filename); void process(); diff --git a/src/mmp_extractor/mmp_extractor.cpp b/src/mmp_extractor/mmp_extractor.cpp index 4ba266b..ee344fb 100644 --- a/src/mmp_extractor/mmp_extractor.cpp +++ b/src/mmp_extractor/mmp_extractor.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -31,18 +32,17 @@ using namespace std; int main(int argc, char *argv[]) { - if (argc < 2 || argc > 3) - { - cout << "Usage:\n" << argv[0] << " {file.mmp,mmp_dir} [texture_ids.txt]" << "\n"; - return 1; - } + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + cl::opt texture_ids(cl::Positional, cl::desc("")); - auto func = [&argc, &argv](auto &p) + cl::ParseCommandLineOptions(argc, argv); + + auto func = [&texture_ids](auto &p) { mmp m; - if (argc > 2) - m.loadTextureNames(argv[2]); - m.load(p.string()); + if (!texture_ids.empty()) + m.loadTextureNames(texture_ids); + m.load(p); m.process(); m.writeFileInfo(); m.writeTexturesList(); @@ -56,7 +56,6 @@ int main(int argc, char *argv[]) m.writeNormalMap(); }; - path p = argv[1]; if (fs::is_regular_file(p)) func(p); else if (fs::is_directory(p)) diff --git a/src/mod_reader/mod_reader.cpp b/src/mod_reader/mod_reader.cpp index 5036231..19fa78c 100644 --- a/src/mod_reader/mod_reader.cpp +++ b/src/mod_reader/mod_reader.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -33,12 +34,9 @@ using namespace std; // options -bool all_formats = false; -bool silent = false; -bool printMaxPolygonBlock = false; -path p; - -bool parse_cmd(int argc, char *argv[]); +cl::opt all_formats("a", cl::desc("all formats")); +cl::opt silent("s", cl::desc("silent")); +cl::opt printMaxPolygonBlock("m", cl::desc("print max polygon block")); void convert_model(const path &fn) { @@ -56,11 +54,10 @@ void convert_model(const path &fn) int main(int argc, char *argv[]) { - if (argc < 2 || !parse_cmd(argc, argv)) - { - printf("Usage: %s [OPTIONS] {model_file,model_dir}\n", argv[0]); - return 1; - } + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); + if (fs::is_regular_file(p)) convert_model(p); else if (fs::is_directory(p)) @@ -78,31 +75,3 @@ int main(int argc, char *argv[]) throw std::runtime_error("Bad fs object"); return 0; } - -bool parse_cmd(int argc, char *argv[]) -{ - for (int i = 1; i < argc; i++) - { - auto arg = argv[i]; - if (*arg != '-') - { - if (i != argc - 1) - return false; - p = arg; - continue; - } - switch (arg[1]) - { - case 'a': - all_formats = true; - break; - case 's': - silent = true; - break; - case 'm': - printMaxPolygonBlock = true; - break; - } - } - return true; -} diff --git a/src/mpj_loader/mpj.cpp b/src/mpj_loader/mpj.cpp index bfa6070..aef5c0f 100644 --- a/src/mpj_loader/mpj.cpp +++ b/src/mpj_loader/mpj.cpp @@ -219,7 +219,7 @@ void mpj::load(const buffer &b) h.load(b); } -void mpj::load(const std::string &fn) +void mpj::load(const path &fn) { filename = fn; buffer b(read_file(filename)); diff --git a/src/mpj_loader/mpj.h b/src/mpj_loader/mpj.h index 7256876..a3a5a4f 100644 --- a/src/mpj_loader/mpj.h +++ b/src/mpj_loader/mpj.h @@ -16,16 +16,18 @@ * along with this program. If not, see . */ +#include +#include +#include + +#include + #include #include #include #include #include -#include -#include -#include - enum class SegmentType : uint32_t { none = 0, @@ -204,8 +206,8 @@ struct mpj header h; // - std::string filename; + path filename; void load(const buffer &b); - void load(const std::string &filename); + void load(const path &filename); }; diff --git a/src/mpj_loader/mpj_loader.cpp b/src/mpj_loader/mpj_loader.cpp index bfa72f7..f028671 100644 --- a/src/mpj_loader/mpj_loader.cpp +++ b/src/mpj_loader/mpj_loader.cpp @@ -19,6 +19,7 @@ #include "mpj.h" #include +#include #include #include @@ -30,12 +31,11 @@ using namespace std; int main(int argc, char *argv[]) { - if (argc != 2) - { - cout << "Usage:\n" << argv[0] << " file.mpj" << "\n"; - return 1; - } + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); + mpj m; - m.load(argv[1]); + m.load(p); return 0; } diff --git a/src/save_loader/save_loader.cpp b/src/save_loader/save_loader.cpp index 80da001..dfcb64b 100644 --- a/src/save_loader/save_loader.cpp +++ b/src/save_loader/save_loader.cpp @@ -20,17 +20,16 @@ #include #include +#include #include #include int main(int argc, char *argv[]) { - if (argc != 2) - { - printf("Usage: %s {file.sav,saves_dir}\n", argv[0]); - return 1; - } + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); //save_changes.mech_org = "ORG_PLAYER"; save_changes.money = 999999999.0f; @@ -47,7 +46,6 @@ int main(int argc, char *argv[]) writeFile(p.string(), save_changes.out.buf()); }; - path p = argv[1]; if (fs::is_regular_file(p)) func(p); else if (fs::is_directory(p)) diff --git a/src/script2txt/script2txt.cpp b/src/script2txt/script2txt.cpp index 82092e9..8f48fa1 100644 --- a/src/script2txt/script2txt.cpp +++ b/src/script2txt/script2txt.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -32,11 +33,9 @@ using std::string; int main(int argc, char *argv[]) { - if (argc != 2) - { - cout << "Usage:\n" << argv[0] << " {script.scr,scr_dir}"; - return 1; - } + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); auto func = [](auto filename) { @@ -76,7 +75,6 @@ int main(int argc, char *argv[]) } }; - path p = argv[1]; if (fs::is_regular_file(p)) func(p.string()); else if (fs::is_directory(p)) diff --git a/src/tm_converter/tm_converter.cpp b/src/tm_converter/tm_converter.cpp index ab1d179..94b0ee8 100644 --- a/src/tm_converter/tm_converter.cpp +++ b/src/tm_converter/tm_converter.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -82,12 +83,10 @@ void convert(const path &fn) int main(int argc, char *argv[]) { - if (argc != 2) - { - printf("Usage: %s file.tm\n", argv[0]); - return 1; - } - path p = argv[1]; + cl::opt p(cl::Positional, cl::desc(""), cl::Required); + + cl::ParseCommandLineOptions(argc, argv); + if (fs::is_regular_file(p)) convert(p); else if (fs::is_directory(p))