forked from teamnwah/openmw-tes3coop
handle master/plugin properly
This commit is contained in:
parent
092de45924
commit
6d875dfd54
3 changed files with 69 additions and 75 deletions
|
@ -5,6 +5,7 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
MwIniImporter::MwIniImporter() {
|
||||
const char *map[][2] =
|
||||
|
@ -26,17 +27,12 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
|
|||
std::cout << "load ini file: " << filename << std::endl;
|
||||
|
||||
std::string section("");
|
||||
std::multimap<std::string, std::string> map;
|
||||
std::map<std::string, std::string> map;
|
||||
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
|
||||
// ignore sections for now
|
||||
if(line.empty() || line[0] == ';') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(line[0] == '[') {
|
||||
if(line.length() > 2) {
|
||||
section = line.substr(1, line.length()-3);
|
||||
|
@ -44,6 +40,15 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
|
|||
continue;
|
||||
}
|
||||
|
||||
int comment_pos = line.find(";");
|
||||
if(comment_pos > 0) {
|
||||
line = line.substr(0,comment_pos);
|
||||
}
|
||||
|
||||
if(line.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int pos = line.find("=");
|
||||
if(pos < 1) {
|
||||
continue;
|
||||
|
@ -58,16 +63,12 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
|
|||
strmap MwIniImporter::loadCfgFile(std::string filename) {
|
||||
std::cout << "load cfg file: " << filename << std::endl;
|
||||
|
||||
std::multimap<std::string, std::string> map;
|
||||
std::map<std::string, std::string> map;
|
||||
boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
|
||||
if(line[0] == '[') { // section
|
||||
continue; // ignore for now
|
||||
}
|
||||
|
||||
// we cant say comment by only looking at first char anymore
|
||||
int comment_pos = line.find("#");
|
||||
if(comment_pos > 0) {
|
||||
|
@ -102,67 +103,50 @@ void MwIniImporter::merge(strmap &cfg, strmap &ini) {
|
|||
}
|
||||
}
|
||||
|
||||
void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini) {
|
||||
std::vector<std::string> esmFiles;
|
||||
std::string baseEsm("Game Files:GameFile");
|
||||
std::string esmFile("");
|
||||
|
||||
strmap::iterator it = ini.begin();
|
||||
for(int i=0; it != ini.end(); i++) {
|
||||
esmFile = baseEsm;
|
||||
esmFile.append(1,i+'0');
|
||||
|
||||
it = ini.find(esmFile);
|
||||
if(it == ini.end()) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << "found EMS file: " << it->second << std::endl;
|
||||
esmFiles.push_back(it->second);
|
||||
esmFile = "";
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::string> bsaFiles;
|
||||
std::string baseBsa("Archives:Archive ");
|
||||
std::string bsaFile("");
|
||||
|
||||
it = ini.begin();
|
||||
for(int i=0; it != ini.end(); i++) {
|
||||
bsaFile = baseBsa;
|
||||
bsaFile.append(1,i+'0');
|
||||
|
||||
it = ini.find(bsaFile);
|
||||
if(it == ini.end()) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << "found BSA file: " << it->second << std::endl;
|
||||
bsaFiles.push_back(it->second);
|
||||
bsaFile = "";
|
||||
}
|
||||
|
||||
if(!esmFiles.empty()) {
|
||||
cfg.erase("master");
|
||||
for(std::vector<std::string>::iterator it = esmFiles.begin(); it != esmFiles.end(); it++) {
|
||||
cfg.insert(std::make_pair<std::string, std::string>("master", *it));
|
||||
}
|
||||
}
|
||||
|
||||
if(!bsaFile.empty()) {
|
||||
cfg.erase("plugin");
|
||||
for(std::vector<std::string>::iterator it = bsaFiles.begin(); it != bsaFiles.end(); it++) {
|
||||
cfg.insert(std::make_pair<std::string, std::string>("plugin", *it));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool MwIniImporter::specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void MwIniImporter::writeToFile(std::string file, strmap &cfg) {
|
||||
boost::iostreams::stream<boost::iostreams::file_sink> out(file);
|
||||
void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles) {
|
||||
std::string baseGameFile("Game Files:GameFile");
|
||||
std::string gameFile("");
|
||||
|
||||
strmap::iterator it = ini.begin();
|
||||
for(int i=0; it != ini.end(); i++) {
|
||||
gameFile = baseGameFile;
|
||||
gameFile.append(1,i+'0');
|
||||
|
||||
it = ini.find(gameFile);
|
||||
if(it == ini.end()) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::string filetype(it->second.substr(it->second.length()-4, 3));
|
||||
std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower);
|
||||
|
||||
if(filetype.compare("esm") == 0) {
|
||||
esmFiles.push_back(it->second);
|
||||
}
|
||||
else if(filetype.compare("esp") == 0) {
|
||||
espFiles.push_back(it->second);
|
||||
}
|
||||
|
||||
gameFile = "";
|
||||
}
|
||||
}
|
||||
|
||||
void MwIniImporter::writeGameFiles(boost::iostreams::stream<boost::iostreams::file_sink> &out, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles) {
|
||||
for(std::vector<std::string>::iterator it=esmFiles.begin(); it != esmFiles.end(); it++) {
|
||||
out << "master=" << *it << std::endl;
|
||||
}
|
||||
for(std::vector<std::string>::iterator it=espFiles.begin(); it != espFiles.end(); it++) {
|
||||
out << "plugin=" << *it << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void MwIniImporter::writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, strmap &cfg) {
|
||||
cfg.erase("master");
|
||||
cfg.erase("plugin");
|
||||
|
||||
for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) {
|
||||
out << (it->first) << "=" << (it->second) << std::endl;
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
#ifndef MWINIIMPORTER_IMPORTER
|
||||
#define MWINIIMPORTER_IMPORTER 1
|
||||
|
||||
#include <boost/iostreams/device/file.hpp>
|
||||
#include <boost/iostreams/stream.hpp>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
|
||||
|
||||
typedef std::multimap<std::string, std::string> strmap;
|
||||
typedef std::map<std::string, std::string> strmap;
|
||||
|
||||
class MwIniImporter {
|
||||
|
||||
|
@ -16,8 +19,9 @@ class MwIniImporter {
|
|||
strmap loadIniFile(std::string filename);
|
||||
strmap loadCfgFile(std::string filename);
|
||||
void merge(strmap &cfg, strmap &ini);
|
||||
void importGameFiles(strmap &cfg, strmap &ini);
|
||||
void writeToFile(std::string file, strmap &cfg);
|
||||
void importGameFiles(strmap &cfg, strmap &ini, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles);
|
||||
void writeGameFiles(boost::iostreams::stream<boost::iostreams::file_sink> &out, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles);
|
||||
void writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, strmap &cfg);
|
||||
|
||||
private:
|
||||
bool specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini);
|
||||
|
|
|
@ -58,21 +58,27 @@ int main(int argc, char *argv[]) {
|
|||
std::cerr << "cfg file does not exist" << std::endl;
|
||||
return -4;
|
||||
}
|
||||
|
||||
|
||||
MwIniImporter importer;
|
||||
importer.setVerbose(vm.count("verbose"));
|
||||
boost::iostreams::stream<boost::iostreams::file_sink> file(outputFile);
|
||||
|
||||
std::multimap<std::string, std::string>ini = importer.loadIniFile(iniFile);
|
||||
std::multimap<std::string, std::string>cfg = importer.loadCfgFile(cfgFile);
|
||||
std::map<std::string, std::string>ini = importer.loadIniFile(iniFile);
|
||||
std::map<std::string, std::string>cfg = importer.loadCfgFile(cfgFile);
|
||||
|
||||
importer.merge(cfg, ini);
|
||||
|
||||
if(vm.count("game-files")) {
|
||||
importer.importGameFiles(cfg, ini);
|
||||
std::vector<std::string> esmFiles;
|
||||
std::vector<std::string> espFiles;
|
||||
|
||||
importer.importGameFiles(cfg, ini, esmFiles, espFiles);
|
||||
importer.writeGameFiles(file, esmFiles, espFiles);
|
||||
}
|
||||
|
||||
std::cout << "write to: " << outputFile << std::endl;
|
||||
importer.writeToFile(outputFile, cfg);
|
||||
importer.writeToFile(file, cfg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue