1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
|
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <unordered_set>
#include <vector>
#include <boost/program_options.hpp>
#include <components/esm/format.hpp>
#include <components/esm3/esmreader.hpp>
#include <components/esm3/esmwriter.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/files/conversion.hpp>
#include <components/files/openfile.hpp>
#include <components/misc/strings/algorithm.hpp>
#include "arguments.hpp"
#include "labels.hpp"
#include "record.hpp"
#include "tes4.hpp"
namespace
{
using namespace EsmTool;
constexpr unsigned majorVersion = 1;
constexpr unsigned minorVersion = 3;
// Create a local alias for brevity
namespace bpo = boost::program_options;
struct ESMData
{
ESM::Header mHeader;
std::deque<std::unique_ptr<EsmTool::RecordBase>> mRecords;
// Value: (Reference, Deleted flag)
std::map<ESM::Cell*, std::deque<std::pair<ESM::CellRef, bool>>> mCellRefs;
std::map<int, int> mRecordStats;
};
bool parseOptions(int argc, char** argv, Arguments& info)
{
bpo::options_description desc(R"(Inspect and extract from Morrowind ES files (ESM, ESP, ESS)
Syntax: esmtool [options] mode infile [outfile]
Allowed modes:
dump Dumps all readable data from the input file.
clone Clones the input file to the output file.
comp Compares the given files.
Allowed options)");
auto addOption = desc.add_options();
addOption("help,h", "print help message.");
addOption("version,v", "print version information and quit.");
addOption("raw,r", bpo::value<std::string>(),
"Show an unformatted list of all records and subrecords of given format:\n"
"\n\tTES3"
"\n\tTES4");
// The intention is that this option would interact better
// with other modes including clone, dump, and raw.
addOption("type,t", bpo::value<std::vector<std::string>>(),
"Show only records of this type (four character record code). May "
"be specified multiple times. Only affects dump mode.");
addOption("name,n", bpo::value<std::string>(), "Show only the record with this name. Only affects dump mode.");
addOption("plain,p",
"Print contents of dialogs, books and scripts. "
"(skipped by default) "
"Only affects dump mode.");
addOption("quiet,q", "Suppress all record information. Useful for speed tests.");
addOption("loadcells,C", "Browse through contents of all cells.");
addOption("encoding,e", bpo::value<std::string>(&(info.encoding))->default_value("win1252"),
"Character encoding used in ESMTool:\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"
"\n\twin1252 - Western European (Latin) alphabet, used by default");
std::string finalText
= "\nIf no option is given, the default action is to parse all records in the archive\nand display "
"diagnostic information.";
// input-file is hidden and used as a positional argument
bpo::options_description hidden("Hidden Options");
auto addHiddenOption = hidden.add_options();
addHiddenOption("mode,m", bpo::value<std::string>(), "esmtool mode");
addHiddenOption("input-file,i", bpo::value<Files::MaybeQuotedPathContainer>(), "input file");
bpo::positional_options_description p;
p.add("mode", 1).add("input-file", 2);
// there might be a better way to do this
bpo::options_description all;
all.add(desc).add(hidden);
bpo::variables_map variables;
try
{
bpo::parsed_options valid_opts = bpo::command_line_parser(argc, argv).options(all).positional(p).run();
bpo::store(valid_opts, variables);
}
catch (std::exception& e)
{
std::cout << "ERROR parsing arguments: " << e.what() << std::endl;
return false;
}
bpo::notify(variables);
if (variables.count("help"))
{
std::cout << desc << finalText << std::endl;
return false;
}
if (variables.count("version"))
{
std::cout << "ESMTool version " << majorVersion << '.' << minorVersion << std::endl;
return false;
}
if (!variables.count("mode"))
{
std::cout << "No mode specified!\n\n" << desc << finalText << std::endl;
return false;
}
if (variables.count("type") > 0)
info.types = variables["type"].as<std::vector<std::string>>();
if (variables.count("name") > 0)
info.name = variables["name"].as<std::string>();
info.mode = variables["mode"].as<std::string>();
if (!(info.mode == "dump" || info.mode == "clone" || info.mode == "comp"))
{
std::cout << "\nERROR: invalid mode \"" << info.mode << "\"\n\n" << desc << finalText << std::endl;
return false;
}
if (!variables.count("input-file"))
{
std::cout << "\nERROR: missing ES file\n\n";
std::cout << desc << finalText << std::endl;
return false;
}
// handling gracefully the user adding multiple files
/* if (variables["input-file"].as< std::vector<std::string> >().size() > 1)
{
std::cout << "\nERROR: more than one ES file specified\n\n";
std::cout << desc << finalText << std::endl;
return false;
}*/
const auto& inputFiles = variables["input-file"].as<Files::MaybeQuotedPathContainer>();
info.filename = inputFiles[0].u8string(); // This call to u8string is redundant, but required to build on
// MSVC 14.26 due to implementation bugs.
if (inputFiles.size() > 1)
info.outname = inputFiles[1].u8string(); // This call to u8string is redundant, but required to build on
// MSVC 14.26 due to implementation bugs.
if (const auto it = variables.find("raw"); it != variables.end())
info.mRawFormat = ESM::parseFormat(it->second.as<std::string>());
info.quiet_given = variables.count("quiet") != 0;
info.loadcells_given = variables.count("loadcells") != 0;
info.plain_given = variables.count("plain") != 0;
// Font encoding settings
info.encoding = variables["encoding"].as<std::string>();
if (info.encoding != "win1250" && info.encoding != "win1251" && info.encoding != "win1252")
{
std::cout << info.encoding << " is not a valid encoding option.\n";
info.encoding = "win1252";
}
std::cout << ToUTF8::encodingUsingMessage(info.encoding) << std::endl;
return true;
}
void loadCell(const Arguments& info, ESM::Cell& cell, ESM::ESMReader& esm, ESMData* data);
int load(const Arguments& info, ESMData* data);
int clone(const Arguments& info);
int comp(const Arguments& info);
}
int main(int argc, char** argv)
{
try
{
Arguments info;
if (!parseOptions(argc, argv, info))
return 1;
if (info.mode == "dump")
return load(info, nullptr);
else if (info.mode == "clone")
return clone(info);
else if (info.mode == "comp")
return comp(info);
else
{
std::cout << "Invalid or no mode specified, dying horribly. Have a nice day." << std::endl;
return 1;
}
}
catch (std::exception& e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
return 0;
}
namespace
{
void loadCell(const Arguments& info, ESM::Cell& cell, ESM::ESMReader& esm, ESMData* data)
{
bool quiet = (info.quiet_given || info.mode == "clone");
bool save = (info.mode == "clone");
// Skip back to the beginning of the reference list
// FIXME: Changes to the references backend required to support multiple plugins have
// almost certainly broken this following line. I'll leave it as is for now, so that
// the compiler does not complain.
cell.restore(esm, 0);
// Loop through all the references
ESM::CellRef ref;
if (!quiet)
std::cout << " References:\n";
bool deleted = false;
ESM::MovedCellRef movedCellRef;
bool moved = false;
while (cell.getNextRef(esm, ref, deleted, movedCellRef, moved))
{
if (data != nullptr && save)
data->mCellRefs[&cell].push_back(std::make_pair(ref, deleted));
if (quiet)
continue;
std::cout << " - Refnum: " << ref.mRefNum.mIndex << '\n';
std::cout << " ID: " << ref.mRefID << '\n';
std::cout << " Position: (" << ref.mPos.pos[0] << ", " << ref.mPos.pos[1] << ", " << ref.mPos.pos[2]
<< ")\n";
if (ref.mScale != 1.f)
std::cout << " Scale: " << ref.mScale << '\n';
if (!ref.mOwner.empty())
std::cout << " Owner: " << ref.mOwner << '\n';
if (!ref.mGlobalVariable.empty())
std::cout << " Global: " << ref.mGlobalVariable << '\n';
if (!ref.mFaction.empty())
std::cout << " Faction: " << ref.mFaction << '\n';
if (!ref.mFaction.empty() || ref.mFactionRank != -2)
std::cout << " Faction rank: " << ref.mFactionRank << '\n';
std::cout << " Enchantment charge: " << ref.mEnchantmentCharge << '\n';
std::cout << " Uses/health: " << ref.mChargeInt << '\n';
std::cout << " Count: " << ref.mCount << '\n';
std::cout << " Blocked: " << static_cast<int>(ref.mReferenceBlocked) << '\n';
std::cout << " Deleted: " << deleted << '\n';
if (!ref.mKey.empty())
std::cout << " Key: " << ref.mKey << '\n';
std::cout << " Lock level: " << ref.mLockLevel << '\n';
if (!ref.mTrap.empty())
std::cout << " Trap: " << ref.mTrap << '\n';
if (!ref.mSoul.empty())
std::cout << " Soul: " << ref.mSoul << '\n';
if (ref.mTeleport)
{
std::cout << " Destination position: (" << ref.mDoorDest.pos[0] << ", " << ref.mDoorDest.pos[1]
<< ", " << ref.mDoorDest.pos[2] << ")\n";
if (!ref.mDestCell.empty())
std::cout << " Destination cell: " << ref.mDestCell << '\n';
}
std::cout << " Moved: " << std::boolalpha << moved << std::noboolalpha << '\n';
if (moved)
{
std::cout << " Moved refnum: " << movedCellRef.mRefNum.mIndex << '\n';
std::cout << " Moved content file: " << movedCellRef.mRefNum.mContentFile << '\n';
std::cout << " Target: " << movedCellRef.mTarget[0] << ", " << movedCellRef.mTarget[1] << '\n';
}
}
}
void printRawTes3(const std::filesystem::path& path)
{
std::cout << "TES3 RAW file listing: " << Files::pathToUnicodeString(path) << '\n';
ESM::ESMReader esm;
esm.openRaw(path);
while (esm.hasMoreRecs())
{
ESM::NAME n = esm.getRecName();
std::cout << "Record: " << n.toStringView() << '\n';
esm.getRecHeader();
while (esm.hasMoreSubs())
{
size_t offs = esm.getFileOffset();
esm.getSubName();
esm.skipHSub();
n = esm.retSubName();
std::ios::fmtflags f(std::cout.flags());
std::cout << " " << n.toStringView() << " - " << esm.getSubSize() << " bytes @ 0x" << std::hex
<< offs << '\n';
std::cout.flags(f);
}
}
}
int loadTes3(const Arguments& info, std::unique_ptr<std::ifstream>&& stream, ESMData* data)
{
std::cout << "Loading TES3 file: " << info.filename << '\n';
ESM::ESMReader esm;
ToUTF8::Utf8Encoder encoder(ToUTF8::calculateEncoding(info.encoding));
esm.setEncoder(&encoder);
std::unordered_set<uint32_t> skipped;
try
{
bool quiet = (info.quiet_given || info.mode == "clone");
bool loadCells = (info.loadcells_given || info.mode == "clone");
bool save = (info.mode == "clone");
esm.open(std::move(stream), info.filename);
if (data != nullptr)
data->mHeader = esm.getHeader();
if (!quiet)
{
std::cout << "Author: " << esm.getAuthor() << '\n'
<< "Description: " << esm.getDesc() << '\n'
<< "File format version: " << esm.esmVersionF() << '\n';
std::vector<ESM::Header::MasterData> masterData = esm.getGameFiles();
if (!masterData.empty())
{
std::cout << "Masters:" << '\n';
for (const auto& master : masterData)
std::cout << " " << master.name << ", " << master.size << " bytes\n";
}
}
// Loop through all records
while (esm.hasMoreRecs())
{
const ESM::NAME n = esm.getRecName();
uint32_t flags;
esm.getRecHeader(flags);
auto record = EsmTool::RecordBase::create(n);
if (record == nullptr)
{
if (!quiet && skipped.count(n.toInt()) == 0)
{
std::cout << "Skipping " << n.toStringView() << " records.\n";
skipped.emplace(n.toInt());
}
esm.skipRecord();
if (quiet)
break;
std::cout << " Skipping\n";
continue;
}
record->setFlags(static_cast<int>(flags));
record->setPrintPlain(info.plain_given);
record->load(esm);
// Is the user interested in this record type?
bool interested = true;
if (!info.types.empty()
&& std::find(info.types.begin(), info.types.end(), n.toStringView()) == info.types.end())
interested = false;
if (!info.name.empty() && !Misc::StringUtils::ciEqual(info.name, record->getId()))
interested = false;
if (!quiet && interested)
{
std::cout << "\nRecord: " << n.toStringView() << " " << record->getId() << "\n"
<< "Record flags: " << recordFlags(record->getFlags()) << '\n';
record->print();
}
if (record->getType().toInt() == ESM::REC_CELL && loadCells && interested)
{
loadCell(info, record->cast<ESM::Cell>()->get(), esm, data);
}
if (data != nullptr)
{
if (save)
data->mRecords.push_back(std::move(record));
++data->mRecordStats[n.toInt()];
}
}
}
catch (const std::exception& e)
{
std::cout << "\nERROR:\n\n " << e.what() << std::endl;
if (data != nullptr)
data->mRecords.clear();
return 1;
}
return 0;
}
int load(const Arguments& info, ESMData* data)
{
if (info.mRawFormat.has_value() && info.mode == "dump")
{
switch (*info.mRawFormat)
{
case ESM::Format::Tes3:
printRawTes3(info.filename);
break;
case ESM::Format::Tes4:
std::cout << "Printing raw TES4 file is not supported: "
<< Files::pathToUnicodeString(info.filename) << "\n";
break;
}
return 0;
}
auto stream = Files::openBinaryInputFileStream(info.filename);
if (!stream->is_open())
{
std::cout << "Failed to open file " << info.filename << ": " << std::generic_category().message(errno)
<< '\n';
return -1;
}
const ESM::Format format = ESM::readFormat(*stream);
stream->seekg(0);
switch (format)
{
case ESM::Format::Tes3:
return loadTes3(info, std::move(stream), data);
case ESM::Format::Tes4:
if (data != nullptr)
{
std::cout << "Collecting data from esm file is not supported for TES4\n";
return -1;
}
return loadTes4(info, std::move(stream));
}
std::cout << "Unsupported ESM format: " << ESM::NAME(format).toStringView() << '\n';
return -1;
}
int clone(const Arguments& info)
{
if (info.outname.empty())
{
std::cout << "You need to specify an output name" << std::endl;
return 1;
}
ESMData data;
if (load(info, &data) != 0)
{
std::cout << "Failed to load, aborting." << std::endl;
return 1;
}
size_t recordCount = data.mRecords.size();
int digitCount = 1; // For a nicer output
if (recordCount > 0)
digitCount = (int)std::log10(recordCount) + 1;
std::cout << "Loaded " << recordCount << " records:\n\n";
int i = 0;
for (std::pair<int, int> stat : data.mRecordStats)
{
ESM::NAME name;
name = stat.first;
int amount = stat.second;
std::cout << std::setw(digitCount) << amount << " " << name.toStringView() << " ";
if (++i % 3 == 0)
std::cout << '\n';
}
if (i % 3 != 0)
std::cout << '\n';
std::cout << "\nSaving records to: " << Files::pathToUnicodeString(info.outname) << "...\n";
ESM::ESMWriter esm;
ToUTF8::Utf8Encoder encoder(ToUTF8::calculateEncoding(info.encoding));
esm.setEncoder(&encoder);
esm.setHeader(data.mHeader);
esm.setVersion(ESM::VER_130);
esm.setRecordCount(recordCount);
std::fstream save(info.outname, std::fstream::out | std::fstream::binary);
esm.save(save);
int saved = 0;
for (auto& record : data.mRecords)
{
if (i <= 0)
break;
const ESM::NAME typeName = record->getType();
esm.startRecord(typeName, record->getFlags());
record->save(esm);
if (typeName.toInt() == ESM::REC_CELL)
{
ESM::Cell* ptr = &record->cast<ESM::Cell>()->get();
if (!data.mCellRefs[ptr].empty())
{
for (std::pair<ESM::CellRef, bool>& ref : data.mCellRefs[ptr])
ref.first.save(esm, ref.second);
}
}
esm.endRecord(typeName);
saved++;
int perc = recordCount == 0 ? 100 : (int)((saved / (float)recordCount) * 100);
if (perc % 10 == 0)
{
std::cerr << "\r" << perc << "%";
}
}
std::cout << "\rDone!" << std::endl;
esm.close();
save.close();
return 0;
}
int comp(const Arguments& info)
{
if (info.filename.empty() || info.outname.empty())
{
std::cout << "You need to specify two input files" << std::endl;
return 1;
}
Arguments fileOne;
Arguments fileTwo;
fileOne.mode = "clone";
fileTwo.mode = "clone";
fileOne.encoding = info.encoding;
fileTwo.encoding = info.encoding;
fileOne.filename = info.filename;
fileTwo.filename = info.outname;
ESMData dataOne;
if (load(fileOne, &dataOne) != 0)
{
std::cout << "Failed to load " << Files::pathToUnicodeString(info.filename) << ", aborting comparison."
<< std::endl;
return 1;
}
ESMData dataTwo;
if (load(fileTwo, &dataTwo) != 0)
{
std::cout << "Failed to load " << Files::pathToUnicodeString(info.outname) << ", aborting comparison."
<< std::endl;
return 1;
}
if (dataOne.mRecords.size() != dataTwo.mRecords.size())
{
std::cout << "Not equal, different amount of records." << std::endl;
return 1;
}
return 0;
}
}
|