| 1642 | } |
| 1643 | |
| 1644 | void ItemTooltips_t::readBookLocalizationsFromFile(bool forceLoadBaseDirectory) |
| 1645 | { |
| 1646 | if ( !PHYSFS_getRealDir("/lang/book_names.json") ) |
| 1647 | { |
| 1648 | printlog("[JSON]: Error: Could not find file: lang/book_names.json"); |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | std::string inputPath = PHYSFS_getRealDir("/lang/book_names.json"); |
| 1653 | if ( forceLoadBaseDirectory ) |
| 1654 | { |
| 1655 | inputPath = BASE_DATA_DIR; |
| 1656 | } |
| 1657 | else |
| 1658 | { |
| 1659 | if ( inputPath != BASE_DATA_DIR ) |
| 1660 | { |
| 1661 | readBookLocalizationsFromFile(true); // force load the base directory first, then modded paths later. |
| 1662 | } |
| 1663 | else |
| 1664 | { |
| 1665 | forceLoadBaseDirectory = true; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | inputPath.append("/lang/book_names.json"); |
| 1670 | |
| 1671 | File* fp = FileIO::open(inputPath.c_str(), "rb"); |
| 1672 | if ( !fp ) |
| 1673 | { |
| 1674 | printlog("[JSON]: Error: Could not open json file %s", inputPath.c_str()); |
| 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | constexpr uint32_t buffer_size = (1 << 13); |
| 1679 | if ( fp->size() >= buffer_size ) |
| 1680 | { |
| 1681 | printlog("[JSON]: Error: file size is too large to fit in buffer! %s", inputPath.c_str()); |
| 1682 | FileIO::close(fp); |
| 1683 | return; |
| 1684 | } |
| 1685 | |
| 1686 | static char buf[buffer_size]; |
| 1687 | const int count = fp->read(buf, sizeof(buf[0]), sizeof(buf) - 1); |
| 1688 | buf[count] = '\0'; |
| 1689 | //rapidjson::FileReadStream is(fp, buf, sizeof(buf)); - use this for large chunks. |
| 1690 | rapidjson::StringStream is(buf); |
| 1691 | |
| 1692 | rapidjson::Document d; |
| 1693 | d.ParseStream(is); |
| 1694 | FileIO::close(fp); |
| 1695 | |
| 1696 | if ( !d.IsObject() ) |
| 1697 | { |
| 1698 | printlog("[JSON]: Error: json file does not define a complete object. Is there a syntax error? %s", inputPath.c_str()); |
| 1699 | return; |
| 1700 | } |
| 1701 | |