#include "dc_language.h" #include #include #include #include namespace dc::language { const std::vector &choices() { static const std::vector list = { {"en", "English", 1}, {"fr", "Fran\xC3\xA7" "ais", 3}, {"de", "Deutsch", 4}, {"it", "Italiano", 5}}; return list; } const Choice *find(const std::string &key) { std::string k = key; std::transform(k.begin(), k.end(), k.begin(), [](unsigned char c) { return char(std::tolower(c)); }); for (const Choice &c : choices()) if (k == c.key) return &c; return nullptr; } std::string readIni(const std::filesystem::path &file) { std::ifstream in(file); std::string line, value; auto trim = [](std::string s) { const size_t first = s.find_first_not_of(" \t\r"); return first == std::string::npos ? std::string() : s.substr(first, s.find_last_not_of(" \t\r") - first + 1); }; while (std::getline(in, line)) { line = trim(line.substr(0, line.find_first_of("#;"))); const size_t equal = line.find('='); if (line.empty() || line.front() == '[' || equal == std::string::npos) continue; if (trim(line.substr(0, equal)) == "language") value = trim(line.substr(equal + 1)); } return value; } bool writeIni(const std::filesystem::path &file, const Choice &choice) { const std::filesystem::path tmp = file.string() + ".tmp"; { std::ofstream out(tmp, std::ios::binary | std::ios::trunc); out << "# Dark Cloud's own settings (the launcher and RecompLauncher write this file; the game reads it when it starts).\n" "# language: en (English), fr (French), de (German) or it (Italian), from the disc's own text and menus.\n" "\n[game]\nlanguage = " << choice.key << "\n"; if (!out) return false; } std::error_code ec; std::filesystem::rename(tmp, file, ec); return !ec; } const Choice *current(const uint8_t *ram) { uint32_t code = 0; std::memcpy(&code, ram + kLanguageCode, sizeof(code)); for (const Choice &c : choices()) if (c.code == code) return &c; return nullptr; } void apply(uint8_t *ram, const Choice &choice) { std::memcpy(ram + kLanguageCode, &choice.code, sizeof(choice.code)); } } // namespace dc::language