Move db wrapper into mod maker.

This commit is contained in:
lzwdgc 2024-04-02 14:08:22 +03:00
parent 350601666e
commit e9fbb6e760
3 changed files with 29 additions and 19 deletions

View file

@ -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));

View file

@ -179,16 +179,7 @@ struct db2 {
using db2_memory_value = std::variant<std::string, int, float>;
using db2_memory = std::map<std::string, std::map<std::string, std::map<std::string, db2_memory_value>>>;
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)];

View file

@ -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;
}