handle master/plugin properly

pull/21/head
Sebastian Wick 13 years ago
parent 092de45924
commit 6d875dfd54

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include <algorithm>
MwIniImporter::MwIniImporter() { MwIniImporter::MwIniImporter() {
const char *map[][2] = const char *map[][2] =
@ -26,17 +27,12 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
std::cout << "load ini file: " << filename << std::endl; std::cout << "load ini file: " << filename << std::endl;
std::string section(""); 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()); boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
std::string line; std::string line;
while (std::getline(file, line)) { while (std::getline(file, line)) {
// ignore sections for now
if(line.empty() || line[0] == ';') {
continue;
}
if(line[0] == '[') { if(line[0] == '[') {
if(line.length() > 2) { if(line.length() > 2) {
section = line.substr(1, line.length()-3); section = line.substr(1, line.length()-3);
@ -44,6 +40,15 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
continue; continue;
} }
int comment_pos = line.find(";");
if(comment_pos > 0) {
line = line.substr(0,comment_pos);
}
if(line.empty()) {
continue;
}
int pos = line.find("="); int pos = line.find("=");
if(pos < 1) { if(pos < 1) {
continue; continue;
@ -58,16 +63,12 @@ strmap MwIniImporter::loadIniFile(std::string filename) {
strmap MwIniImporter::loadCfgFile(std::string filename) { strmap MwIniImporter::loadCfgFile(std::string filename) {
std::cout << "load cfg file: " << filename << std::endl; 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()); boost::iostreams::stream<boost::iostreams::file_source>file(filename.c_str());
std::string line; std::string line;
while (std::getline(file, 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 // we cant say comment by only looking at first char anymore
int comment_pos = line.find("#"); int comment_pos = line.find("#");
if(comment_pos > 0) { if(comment_pos > 0) {
@ -102,67 +103,50 @@ void MwIniImporter::merge(strmap &cfg, strmap &ini) {
} }
} }
void MwIniImporter::importGameFiles(strmap &cfg, strmap &ini) { bool MwIniImporter::specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini) {
std::vector<std::string> esmFiles; return false;
std::string baseEsm("Game Files:GameFile"); }
std::string esmFile("");
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(); strmap::iterator it = ini.begin();
for(int i=0; it != ini.end(); i++) { for(int i=0; it != ini.end(); i++) {
esmFile = baseEsm; gameFile = baseGameFile;
esmFile.append(1,i+'0'); gameFile.append(1,i+'0');
it = ini.find(esmFile); it = ini.find(gameFile);
if(it == ini.end()) { if(it == ini.end()) {
break; break;
} }
std::cout << "found EMS file: " << it->second << std::endl; std::string filetype(it->second.substr(it->second.length()-4, 3));
esmFiles.push_back(it->second); std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower);
esmFile = "";
}
if(filetype.compare("esm") == 0) {
std::vector<std::string> bsaFiles; esmFiles.push_back(it->second);
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;
} }
else if(filetype.compare("esp") == 0) {
std::cout << "found BSA file: " << it->second << std::endl; espFiles.push_back(it->second);
bsaFiles.push_back(it->second);
bsaFile = "";
} }
if(!esmFiles.empty()) { gameFile = "";
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()) { void MwIniImporter::writeGameFiles(boost::iostreams::stream<boost::iostreams::file_sink> &out, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles) {
cfg.erase("plugin"); for(std::vector<std::string>::iterator it=esmFiles.begin(); it != esmFiles.end(); it++) {
for(std::vector<std::string>::iterator it = bsaFiles.begin(); it != bsaFiles.end(); it++) { out << "master=" << *it << std::endl;
cfg.insert(std::make_pair<std::string, std::string>("plugin", *it));
} }
for(std::vector<std::string>::iterator it=espFiles.begin(); it != espFiles.end(); it++) {
out << "plugin=" << *it << std::endl;
} }
} }
bool MwIniImporter::specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini) { void MwIniImporter::writeToFile(boost::iostreams::stream<boost::iostreams::file_sink> &out, strmap &cfg) {
return false; cfg.erase("master");
} cfg.erase("plugin");
void MwIniImporter::writeToFile(std::string file, strmap &cfg) {
boost::iostreams::stream<boost::iostreams::file_sink> out(file);
for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) { for(strmap::iterator it=cfg.begin(); it != cfg.end(); it++) {
out << (it->first) << "=" << (it->second) << std::endl; out << (it->first) << "=" << (it->second) << std::endl;

@ -1,12 +1,15 @@
#ifndef MWINIIMPORTER_IMPORTER #ifndef MWINIIMPORTER_IMPORTER
#define MWINIIMPORTER_IMPORTER 1 #define MWINIIMPORTER_IMPORTER 1
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <string> #include <string>
#include <map> #include <map>
#include <vector>
#include <exception> #include <exception>
typedef std::multimap<std::string, std::string> strmap; typedef std::map<std::string, std::string> strmap;
class MwIniImporter { class MwIniImporter {
@ -16,8 +19,9 @@ class MwIniImporter {
strmap loadIniFile(std::string filename); strmap loadIniFile(std::string filename);
strmap loadCfgFile(std::string filename); strmap loadCfgFile(std::string filename);
void merge(strmap &cfg, strmap &ini); void merge(strmap &cfg, strmap &ini);
void importGameFiles(strmap &cfg, strmap &ini); void importGameFiles(strmap &cfg, strmap &ini, std::vector<std::string> &esmFiles, std::vector<std::string> &espFiles);
void writeToFile(std::string file, strmap &cfg); 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: private:
bool specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini); bool specialMerge(std::string cfgKey, std::string iniKey, strmap &cfg, strmap &ini);

@ -59,20 +59,26 @@ int main(int argc, char *argv[]) {
return -4; return -4;
} }
MwIniImporter importer; MwIniImporter importer;
importer.setVerbose(vm.count("verbose")); 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::map<std::string, std::string>ini = importer.loadIniFile(iniFile);
std::multimap<std::string, std::string>cfg = importer.loadCfgFile(cfgFile); std::map<std::string, std::string>cfg = importer.loadCfgFile(cfgFile);
importer.merge(cfg, ini); importer.merge(cfg, ini);
if(vm.count("game-files")) { 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; std::cout << "write to: " << outputFile << std::endl;
importer.writeToFile(outputFile, cfg); importer.writeToFile(file, cfg);
return 0; return 0;
} }

Loading…
Cancel
Save