| 1724 | |
| 1725 | #ifndef EDITOR |
| 1726 | void ItemTooltips_t::readTooltipsFromFile(bool forceLoadBaseDirectory) |
| 1727 | { |
| 1728 | if ( !PHYSFS_getRealDir("/items/item_tooltips.json") ) |
| 1729 | { |
| 1730 | printlog("[JSON]: Error: Could not find file: items/item_tooltips.json"); |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | std::string inputPath = PHYSFS_getRealDir("/items/item_tooltips.json"); |
| 1735 | if ( forceLoadBaseDirectory ) |
| 1736 | { |
| 1737 | inputPath = BASE_DATA_DIR; |
| 1738 | } |
| 1739 | else |
| 1740 | { |
| 1741 | if ( inputPath != BASE_DATA_DIR ) |
| 1742 | { |
| 1743 | readTooltipsFromFile(true); // force load the base directory first, then modded paths later. |
| 1744 | } |
| 1745 | else |
| 1746 | { |
| 1747 | forceLoadBaseDirectory = true; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | inputPath.append("/items/item_tooltips.json"); |
| 1752 | |
| 1753 | File* fp = FileIO::open(inputPath.c_str(), "rb"); |
| 1754 | if ( !fp ) |
| 1755 | { |
| 1756 | printlog("[JSON]: Error: Could not open json file %s", inputPath.c_str()); |
| 1757 | return; |
| 1758 | } |
| 1759 | |
| 1760 | constexpr uint32_t buffer_size = (1 << 20); // 1mb |
| 1761 | if ( fp->size() >= buffer_size ) |
| 1762 | { |
| 1763 | printlog("[JSON]: Error: file size is too large to fit in buffer! %s", inputPath.c_str()); |
| 1764 | FileIO::close(fp); |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | static char buf[buffer_size]; |
| 1769 | const int count = fp->read(buf, sizeof(buf[0]), sizeof(buf) - 1); |
| 1770 | buf[count] = '\0'; |
| 1771 | //rapidjson::FileReadStream is(fp, buf, sizeof(buf)); - use this for large chunks. |
| 1772 | rapidjson::StringStream is(buf); |
| 1773 | |
| 1774 | rapidjson::Document d; |
| 1775 | d.ParseStream(is); |
| 1776 | FileIO::close(fp); |
| 1777 | |
| 1778 | if ( !d.IsObject() ) |
| 1779 | { |
| 1780 | printlog("[JSON]: Error: json file does not define a complete object. Is there a syntax error? %s", inputPath.c_str()); |
| 1781 | return; |
| 1782 | } |
| 1783 |
no test coverage detected