2012-03-30 18:59:44 +00:00
|
|
|
#include "importer.hpp"
|
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
2014-05-23 12:41:49 +00:00
|
|
|
#include <iostream>
|
2014-05-19 12:56:41 +00:00
|
|
|
|
2012-03-30 20:58:54 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2022-08-06 16:09:50 +00:00
|
|
|
#include <components/files/configurationmanager.hpp>
|
2022-08-19 22:26:23 +00:00
|
|
|
#include <components/files/conversion.hpp>
|
2022-08-06 16:09:50 +00:00
|
|
|
|
2012-03-30 20:58:54 +00:00
|
|
|
namespace bpo = boost::program_options;
|
2022-06-08 21:25:50 +00:00
|
|
|
namespace sfs = std::filesystem;
|
2012-03-30 20:58:54 +00:00
|
|
|
|
2014-01-14 19:30:56 +00:00
|
|
|
#ifndef _WIN32
|
2012-03-30 18:59:44 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-01-14 19:30:56 +00:00
|
|
|
#else
|
2012-03-30 18:59:44 +00:00
|
|
|
|
2014-05-22 11:42:47 +00:00
|
|
|
// Include on Windows only
|
|
|
|
#include <boost/locale.hpp>
|
|
|
|
|
2014-01-14 19:30:56 +00:00
|
|
|
class utf8argv
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
utf8argv(int argc, wchar_t* wargv[])
|
|
|
|
{
|
|
|
|
args.reserve(argc);
|
|
|
|
argv = new const char*[argc];
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
{
|
|
|
|
args.push_back(boost::locale::conv::utf_to_utf<char>(wargv[i]));
|
|
|
|
argv[i] = args.back().c_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~utf8argv() { delete[] argv; }
|
2014-05-20 06:42:21 +00:00
|
|
|
char** get() const { return const_cast<char**>(argv); }
|
2014-01-14 19:30:56 +00:00
|
|
|
|
|
|
|
private:
|
2014-09-26 15:12:48 +00:00
|
|
|
utf8argv(const utf8argv&);
|
|
|
|
utf8argv& operator=(const utf8argv&);
|
2014-01-14 19:30:56 +00:00
|
|
|
|
|
|
|
const char** argv;
|
|
|
|
std::vector<std::string> args;
|
|
|
|
};
|
|
|
|
|
2014-05-22 12:35:57 +00:00
|
|
|
/* The only way to pass Unicode on Winodws with CLI is to use wide
|
|
|
|
characters interface which presents UTF-16 encoding. The rest of
|
|
|
|
OpenMW application stack assumes UTF-8 encoding, therefore this
|
|
|
|
conversion.
|
|
|
|
|
2022-06-11 21:38:09 +00:00
|
|
|
For boost::filesystem::path::imbue see components/files/windowspath.cpp
|
2014-05-22 12:35:57 +00:00
|
|
|
*/
|
2014-01-14 19:30:56 +00:00
|
|
|
int wmain(int argc, wchar_t* wargv[])
|
|
|
|
{
|
|
|
|
utf8argv converter(argc, wargv);
|
|
|
|
char** argv = converter.get();
|
|
|
|
#endif
|
2014-12-18 02:24:10 +00:00
|
|
|
|
2013-06-26 16:29:09 +00:00
|
|
|
try
|
|
|
|
{
|
2015-01-29 23:07:18 +00:00
|
|
|
bpo::options_description desc("Syntax: openmw-iniimporter <options> inifile configfile\nAllowed options");
|
2014-12-18 02:24:10 +00:00
|
|
|
bpo::positional_options_description p_desc;
|
2022-09-12 14:48:15 +00:00
|
|
|
auto addOption = desc.add_options();
|
|
|
|
addOption("help,h", "produce help message");
|
|
|
|
addOption("verbose,v", "verbose output");
|
|
|
|
addOption("ini,i", bpo::value<Files::MaybeQuotedPath>(), "morrowind.ini file");
|
|
|
|
addOption("cfg,c", bpo::value<Files::MaybeQuotedPath>(), "openmw.cfg file");
|
|
|
|
addOption("output,o", bpo::value<Files::MaybeQuotedPath>()->default_value({}), "openmw.cfg file");
|
|
|
|
addOption("game-files,g", "import esm and esp files");
|
|
|
|
addOption("fonts,f", "import bitmap fonts");
|
|
|
|
addOption("no-archives,A", "disable bsa archives import");
|
|
|
|
addOption("encoding,e", bpo::value<std::string>()->default_value("win1252"),
|
2014-12-18 02:24:10 +00:00
|
|
|
"Character encoding used in OpenMW game messages:\n"
|
|
|
|
"\n\twin1250 - Central and Eastern European such as Polish, Czech, Slovak, Hungarian, Slovene, Bosnian, "
|
|
|
|
"Croatian, Serbian (Latin script), Romanian and Albanian languages\n"
|
|
|
|
"\n\twin1251 - Cyrillic alphabet such as Russian, Bulgarian, Serbian Cyrillic and other languages\n"
|
2022-09-12 14:48:15 +00:00
|
|
|
"\n\twin1252 - Western European (Latin) alphabet, used by default");
|
2014-12-18 02:24:10 +00:00
|
|
|
;
|
|
|
|
p_desc.add("ini", 1).add("cfg", 1);
|
|
|
|
|
|
|
|
bpo::variables_map vm;
|
|
|
|
|
2013-06-26 16:29:09 +00:00
|
|
|
bpo::parsed_options parsed = bpo::command_line_parser(argc, argv).options(desc).positional(p_desc).run();
|
|
|
|
bpo::store(parsed, vm);
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
if (vm.count("help") || !vm.count("ini") || !vm.count("cfg"))
|
|
|
|
{
|
|
|
|
std::cout << desc;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-02 18:47:09 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
bpo::notify(vm);
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2022-08-14 20:49:40 +00:00
|
|
|
std::filesystem::path iniFile(
|
|
|
|
vm["ini"].as<Files::MaybeQuotedPath>().u8string()); // This call to u8string is redundant, but required to
|
|
|
|
// build on MSVC 14.26 due to implementation bugs.
|
|
|
|
std::filesystem::path cfgFile(
|
|
|
|
vm["cfg"].as<Files::MaybeQuotedPath>().u8string()); // This call to u8string is redundant, but required to
|
|
|
|
// build on MSVC 14.26 due to implementation bugs.
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
// if no output is given, write back to cfg file
|
2022-08-14 20:49:40 +00:00
|
|
|
std::filesystem::path outputFile = vm["output"]
|
|
|
|
.as<Files::MaybeQuotedPath>()
|
|
|
|
.u8string(); // This call to u8string is redundant, but required to build
|
|
|
|
// on MSVC 14.26 due to implementation bugs.
|
2014-12-18 02:24:10 +00:00
|
|
|
if (vm["output"].defaulted())
|
|
|
|
{
|
2022-08-14 20:49:40 +00:00
|
|
|
outputFile = vm["cfg"]
|
|
|
|
.as<Files::MaybeQuotedPath>()
|
|
|
|
.u8string(); // This call to u8string is redundant, but required to build on MSVC 14.26 due
|
|
|
|
// to implementation bugs.
|
2014-12-18 02:24:10 +00:00
|
|
|
}
|
2012-04-02 15:07:18 +00:00
|
|
|
|
2022-06-08 21:25:50 +00:00
|
|
|
if (!std::filesystem::exists(iniFile))
|
|
|
|
{
|
2014-12-18 02:24:10 +00:00
|
|
|
std::cerr << "ini file does not exist" << std::endl;
|
|
|
|
return -3;
|
|
|
|
}
|
2022-06-08 21:25:50 +00:00
|
|
|
if (!std::filesystem::exists(cfgFile))
|
2014-12-18 02:24:10 +00:00
|
|
|
std::cerr << "cfg file does not exist" << std::endl;
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
MwIniImporter importer;
|
2015-03-06 10:19:57 +00:00
|
|
|
importer.setVerbose(vm.count("verbose") != 0);
|
2013-01-01 20:59:30 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
// Font encoding settings
|
|
|
|
std::string encoding(vm["encoding"].as<std::string>());
|
|
|
|
importer.setInputEncoding(ToUTF8::calculateEncoding(encoding));
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
MwIniImporter::multistrmap ini = importer.loadIniFile(iniFile);
|
|
|
|
MwIniImporter::multistrmap cfg = importer.loadCfgFile(cfgFile);
|
2012-04-02 15:07:18 +00:00
|
|
|
|
2022-08-13 11:22:53 +00:00
|
|
|
if (!vm.count("fonts"))
|
|
|
|
{
|
|
|
|
ini.erase("Fonts:Font 0");
|
|
|
|
ini.erase("Fonts:Font 1");
|
|
|
|
ini.erase("Fonts:Font 2");
|
|
|
|
}
|
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
importer.merge(cfg, ini);
|
|
|
|
importer.mergeFallback(cfg, ini);
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
if (vm.count("game-files"))
|
|
|
|
{
|
2015-02-21 21:02:25 +00:00
|
|
|
importer.importGameFiles(cfg, ini, iniFile);
|
2014-12-18 02:24:10 +00:00
|
|
|
}
|
2013-02-03 16:42:58 +00:00
|
|
|
|
2014-12-18 02:24:10 +00:00
|
|
|
if (!vm.count("no-archives"))
|
|
|
|
{
|
|
|
|
importer.importArchives(cfg, ini);
|
|
|
|
}
|
2012-03-31 09:36:51 +00:00
|
|
|
|
2022-08-19 22:26:23 +00:00
|
|
|
std::cout << "write to: " << Files::pathToUnicodeString(outputFile) << std::endl;
|
2022-08-06 16:31:27 +00:00
|
|
|
std::ofstream file(outputFile);
|
2014-12-18 02:24:10 +00:00
|
|
|
importer.writeToFile(file, cfg);
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR: " << e.what() << std::endl;
|
|
|
|
}
|
2012-03-30 18:59:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|