From e9fbb6e760869faed7ba2d06b50980f7e4c3180b Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Tue, 2 Apr 2024 14:08:22 +0300 Subject: [PATCH] Move db wrapper into mod maker. --- src/aim1_mod_maker/aim1_mod_maker.h | 30 +++++++++++++++++++++++++++-- src/common/db2.h | 17 +--------------- src/db_extractor2/db_extractor2.cpp | 1 - 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/aim1_mod_maker/aim1_mod_maker.h b/src/aim1_mod_maker/aim1_mod_maker.h index af449ec..b65de46 100644 --- a/src/aim1_mod_maker/aim1_mod_maker.h +++ b/src/aim1_mod_maker/aim1_mod_maker.h @@ -72,6 +72,28 @@ struct aim_exe_v1_06_constants { }; struct mod_maker { + struct db_wrapper { + db2::files::db2_internal m; + path fn; + int codepage{1251}; + bool written{}; + + ~db_wrapper() { + if (!written) { + write(); + } + } + void write() { + m.save(fn); + written = true; + } + auto &operator[](this auto &&d, const std::string &s) { + return d.m[s]; + } + auto &operator[](this auto &&d, const std::u8string &s) { + return d.m[(const char *)s.c_str()]; + } + }; enum class file_type { unknown, @@ -358,7 +380,7 @@ private: } return backup; } - auto open_db(auto &&name, int db_codepage) { + db_wrapper open_db(auto &&name, int db_codepage) { auto d = db2{get_data_dir() / name, db_codepage}; auto files = d.open().get_files(); for (auto &&f : files) { @@ -368,7 +390,11 @@ private: } files_to_distribute.insert(f); } - return d.open().to_map(); + db_wrapper w; + w.m = d.open().to_map(); + w.fn = d.fn; + w.codepage = d.codepage; + return w; } path get_hash_fn(path fn, const byte_array &data) const { return get_mod_dir() / std::format("{:0X}.hash", get_insert_hash(fn, data)); diff --git a/src/common/db2.h b/src/common/db2.h index 74b5a86..6679e17 100644 --- a/src/common/db2.h +++ b/src/common/db2.h @@ -179,16 +179,7 @@ struct db2 { using db2_memory_value = std::variant; using db2_memory = std::map>>; - path fn; - int codepage{1251}; db2_memory m; - bool written{}; - - ~db2_internal() { - if (!written) { - write(); - } - } auto begin(this auto &&d) {return d.m.begin();} auto end(this auto &&d) {return d.m.end();} @@ -215,7 +206,7 @@ struct db2 { } return ja; } - void save(const path &fn) { + void save(const path &fn, int codepage = 1251) { auto s_to_char20 = [&](char20 &dst, const std::string &in, int codepage = 1251) { auto s = utf8_to_dbstr(in); if (s.size() + 1 > sizeof(char20)) { @@ -288,10 +279,6 @@ struct db2 { write_file(path{fn} += ".ind", indv.d); write_file(path{fn} += ".dat", datv.d); } - void write() { - save(fn); - written = true; - } }; // converts string to utf8, trims them @@ -304,8 +291,6 @@ struct db2 { }; db2_internal m; - m.fn = db.fn; - m.codepage = db.codepage; auto tbl = tab_.data->tables(); for (auto &&t : tbl) { auto &jt = m[prepare_string(t.name)]; diff --git a/src/db_extractor2/db_extractor2.cpp b/src/db_extractor2/db_extractor2.cpp index 55bf44e..6a84bf7 100644 --- a/src/db_extractor2/db_extractor2.cpp +++ b/src/db_extractor2/db_extractor2.cpp @@ -40,7 +40,6 @@ int main(int argc, char *argv[]) auto m = f.to_map(); write_file(path{db_fn} += ".json", m.to_json().dump(1)); m.save(path{db_fn} += "new"); - m.written = true; return 0; }