https://github.com/MinecraftForge/Documentation/blob/5ab4ba6cf9abc0ac4c0abd96ad187461aefd72af/docs/gettingstarted/structuring.md
| 93 | |
| 94 | // https://github.com/MinecraftForge/Documentation/blob/5ab4ba6cf9abc0ac4c0abd96ad187461aefd72af/docs/gettingstarted/structuring.md |
| 95 | std::shared_ptr<ModDetails> ReadMCModTOML(QByteArray contents) |
| 96 | { |
| 97 | std::shared_ptr<ModDetails> details = std::make_shared<ModDetails>(); |
| 98 | |
| 99 | char errbuf[200]; |
| 100 | // top-level table |
| 101 | toml_table_t* tomlData = toml_parse(contents.data(), errbuf, sizeof(errbuf)); |
| 102 | |
| 103 | if(!tomlData) |
| 104 | { |
| 105 | return nullptr; |
| 106 | } |
| 107 | |
| 108 | // array defined by [[mods]] |
| 109 | toml_array_t* tomlModsArr = toml_array_in(tomlData, "mods"); |
| 110 | if(!tomlModsArr) |
| 111 | { |
| 112 | qWarning() << "Corrupted mods.toml? Couldn't find [[mods]] array!"; |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
| 116 | // we only really care about the first element, since multiple mods in one file is not supported by us at the moment |
| 117 | toml_table_t* tomlModsTable0 = toml_table_at(tomlModsArr, 0); |
| 118 | if(!tomlModsTable0) |
| 119 | { |
| 120 | qWarning() << "Corrupted mods.toml? [[mods]] didn't have an element at index 0!"; |
| 121 | return nullptr; |
| 122 | } |
| 123 | |
| 124 | // mandatory properties - always in [[mods]] |
| 125 | toml_datum_t modIdDatum = toml_string_in(tomlModsTable0, "modId"); |
| 126 | if(modIdDatum.ok) |
| 127 | { |
| 128 | details->mod_id = modIdDatum.u.s; |
| 129 | // library says this is required for strings |
| 130 | free(modIdDatum.u.s); |
| 131 | } |
| 132 | toml_datum_t versionDatum = toml_string_in(tomlModsTable0, "version"); |
| 133 | if(versionDatum.ok) |
| 134 | { |
| 135 | details->version = versionDatum.u.s; |
| 136 | free(versionDatum.u.s); |
| 137 | } |
| 138 | toml_datum_t displayNameDatum = toml_string_in(tomlModsTable0, "displayName"); |
| 139 | if(displayNameDatum.ok) |
| 140 | { |
| 141 | details->name = displayNameDatum.u.s; |
| 142 | free(displayNameDatum.u.s); |
| 143 | } |
| 144 | toml_datum_t descriptionDatum = toml_string_in(tomlModsTable0, "description"); |
| 145 | if(descriptionDatum.ok) |
| 146 | { |
| 147 | details->description = descriptionDatum.u.s; |
| 148 | free(descriptionDatum.u.s); |
| 149 | } |
| 150 | |
| 151 | // optional properties - can be in the root table or [[mods]] |
| 152 | toml_datum_t authorsDatum = toml_string_in(tomlData, "authors"); |
no test coverage detected