@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
22
22
#include < json/json.h>
23
23
#include < algorithm>
24
24
#include " content/mods.h"
25
+ #include " database/database.h"
25
26
#include " filesys.h"
26
27
#include " log.h"
27
28
#include " content/subgames.h"
@@ -422,83 +423,29 @@ ClientModConfiguration::ClientModConfiguration(const std::string &path) :
422
423
}
423
424
#endif
424
425
425
- ModMetadata::ModMetadata (const std::string &mod_name) : m_mod_name(mod_name)
426
+ ModMetadata::ModMetadata (const std::string &mod_name, ModMetadataDatabase *database):
427
+ m_mod_name(mod_name), m_database(database)
426
428
{
429
+ m_database->getModEntries (m_mod_name, &m_stringvars);
427
430
}
428
431
429
432
void ModMetadata::clear ()
430
433
{
434
+ for (const auto &pair : m_stringvars) {
435
+ m_database->removeModEntry (m_mod_name, pair.first );
436
+ }
431
437
Metadata::clear ();
432
- m_modified = true ;
433
438
}
434
439
435
- bool ModMetadata::save (const std::string &root_path )
440
+ bool ModMetadata::setString (const std::string &name, const std::string &var )
436
441
{
437
- Json::Value json;
438
- for (StringMap::const_iterator it = m_stringvars.begin ();
439
- it != m_stringvars.end (); ++it) {
440
- json[it->first ] = it->second ;
441
- }
442
-
443
- if (!fs::PathExists (root_path)) {
444
- if (!fs::CreateAllDirs (root_path)) {
445
- errorstream << " ModMetadata[" << m_mod_name
446
- << " ]: Unable to save. '" << root_path
447
- << " ' tree cannot be created." << std::endl;
448
- return false ;
442
+ if (Metadata::setString (name, var)) {
443
+ if (var.empty ()) {
444
+ m_database->removeModEntry (m_mod_name, name);
445
+ } else {
446
+ m_database->setModEntry (m_mod_name, name, var);
449
447
}
450
- } else if (!fs::IsDir (root_path)) {
451
- errorstream << " ModMetadata[" << m_mod_name << " ]: Unable to save. '"
452
- << root_path << " ' is not a directory." << std::endl;
453
- return false ;
454
- }
455
-
456
- bool w_ok = fs::safeWriteToFile (
457
- root_path + DIR_DELIM + m_mod_name, fastWriteJson (json));
458
-
459
- if (w_ok) {
460
- m_modified = false ;
461
- } else {
462
- errorstream << " ModMetadata[" << m_mod_name << " ]: failed write file."
463
- << std::endl;
464
- }
465
- return w_ok;
466
- }
467
-
468
- bool ModMetadata::load (const std::string &root_path)
469
- {
470
- m_stringvars.clear ();
471
-
472
- std::ifstream is ((root_path + DIR_DELIM + m_mod_name).c_str (),
473
- std::ios_base::binary);
474
- if (!is.good ()) {
475
- return false ;
476
- }
477
-
478
- Json::Value root;
479
- Json::CharReaderBuilder builder;
480
- builder.settings_ [" collectComments" ] = false ;
481
- std::string errs;
482
-
483
- if (!Json::parseFromStream (builder, is, &root, &errs)) {
484
- errorstream << " ModMetadata[" << m_mod_name
485
- << " ]: failed read data "
486
- " (Json decoding failure). Message: "
487
- << errs << std::endl;
488
- return false ;
489
- }
490
-
491
- const Json::Value::Members attr_list = root.getMemberNames ();
492
- for (const auto &it : attr_list) {
493
- Json::Value attr_value = root[it];
494
- m_stringvars[it] = attr_value.asString ();
448
+ return true ;
495
449
}
496
-
497
- return true ;
498
- }
499
-
500
- bool ModMetadata::setString (const std::string &name, const std::string &var)
501
- {
502
- m_modified = Metadata::setString (name, var);
503
- return m_modified;
450
+ return false ;
504
451
}
0 commit comments