MCPcopy Create free account
hub / github.com/TurningWheel/Barony / readItemLocalizationsFromFile

Method readItemLocalizationsFromFile

src/mod_tools.cpp:1490–1642  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1488}
1489
1490void ItemTooltips_t::readItemLocalizationsFromFile(bool forceLoadBaseDirectory)
1491{
1492 if ( !PHYSFS_getRealDir("/lang/item_names.json") )
1493 {
1494 printlog("[JSON]: Error: Could not find file: lang/item_names.json");
1495 return;
1496 }
1497
1498 std::string inputPath = PHYSFS_getRealDir("/lang/item_names.json");
1499 if ( forceLoadBaseDirectory )
1500 {
1501 inputPath = BASE_DATA_DIR;
1502 }
1503 else
1504 {
1505 if ( inputPath != BASE_DATA_DIR )
1506 {
1507 readItemLocalizationsFromFile(true); // force load the base directory first, then modded paths later.
1508 }
1509 else
1510 {
1511 forceLoadBaseDirectory = true;
1512 }
1513 }
1514
1515 inputPath.append("/lang/item_names.json");
1516
1517 File* fp = FileIO::open(inputPath.c_str(), "rb");
1518 if ( !fp )
1519 {
1520 printlog("[JSON]: Error: Could not open json file %s", inputPath.c_str());
1521 return;
1522 }
1523
1524 constexpr uint32_t buffer_size = (1 << 17); // 131kb
1525 if ( fp->size() >= buffer_size )
1526 {
1527 printlog("[JSON]: Error: file size is too large to fit in buffer! %s", inputPath.c_str());
1528 FileIO::close(fp);
1529 return;
1530 }
1531
1532 static char buf[buffer_size];
1533 const int count = fp->read(buf, sizeof(buf[0]), sizeof(buf) - 1);
1534 buf[count] = '\0';
1535 //rapidjson::FileReadStream is(fp, buf, sizeof(buf)); - use this for large chunks.
1536 rapidjson::StringStream is(buf);
1537
1538 rapidjson::Document d;
1539 d.ParseStream(is);
1540 FileIO::close(fp);
1541
1542 if ( !d.IsObject() )
1543 {
1544 printlog("[JSON]: Error: json file does not define a complete object. Is there a syntax error? %s", inputPath.c_str());
1545 return;
1546 }
1547

Callers 3

mainFunction · 0.80
initGameDatafilesFunction · 0.80
consolecommand.cppFile · 0.80

Calls 10

printlogFunction · 0.85
lowercaseStringFunction · 0.85
appendMethod · 0.80
c_strMethod · 0.80
GetIntMethod · 0.80
setIdentifiedNameMethod · 0.80
setUnidentifiedNameMethod · 0.80
sizeMethod · 0.45
readMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected