| 1 |
#include "DarkmodTxt.h" |
| 2 |
|
| 3 |
#include "i18n.h" |
| 4 |
#include "iarchive.h" |
| 5 |
#include "itextstream.h" |
| 6 |
#include "ifilesystem.h" |
| 7 |
|
| 8 |
#include <fmt/format.h> |
| 9 |
#include "string/trim.h" |
| 10 |
#include "string/convert.h" |
| 11 |
#include "string/case_conv.h" |
| 12 |
|
| 13 |
namespace map |
| 14 |
{ |
| 15 |
|
| 16 |
std::string DarkmodTxt::getFilename() |
| 17 |
{ |
| 18 |
return NAME(); |
| 19 |
} |
| 20 |
|
| 21 |
const std::string& DarkmodTxt::getTitle() |
| 22 |
{ |
| 23 |
return _title; |
| 24 |
} |
| 25 |
|
| 26 |
void DarkmodTxt::setTitle(const std::string& title) |
| 27 |
{ |
| 28 |
_title = title; |
| 29 |
} |
| 30 |
|
| 31 |
const std::string& DarkmodTxt::getAuthor() |
| 32 |
{ |
| 33 |
return _author; |
| 34 |
} |
| 35 |
|
| 36 |
void DarkmodTxt::setAuthor(const std::string& author) |
| 37 |
{ |
| 38 |
_author = author; |
| 39 |
} |
| 40 |
|
| 41 |
const std::string& DarkmodTxt::getDescription() |
| 42 |
{ |
| 43 |
return _description; |
| 44 |
} |
| 45 |
|
| 46 |
void DarkmodTxt::setDescription(const std::string& desc) |
| 47 |
{ |
| 48 |
_description = desc; |
| 49 |
} |
| 50 |
|
| 51 |
const std::string& DarkmodTxt::getVersion() |
| 52 |
{ |
| 53 |
return _version; |
| 54 |
} |
| 55 |
|
| 56 |
void DarkmodTxt::setVersion(const std::string& version) |
| 57 |
{ |
| 58 |
_version = version; |
| 59 |
} |
| 60 |
|
| 61 |
const std::string& DarkmodTxt::getReqTdmVersion() |
| 62 |
{ |
| 63 |
return _reqTdmVersion; |
| 64 |
} |
| 65 |
|
| 66 |
void DarkmodTxt::setReqTdmVersion(const std::string& reqVersion) |
| 67 |
{ |
| 68 |
_reqTdmVersion = reqVersion; |
| 69 |
} |
| 70 |
|
| 71 |
const DarkmodTxt::TitleList& DarkmodTxt::getMissionTitles() |
| 72 |
{ |
| 73 |
return _missionTitles; |
| 74 |
} |
| 75 |
|
| 76 |
void DarkmodTxt::setMissionTitles(const DarkmodTxt::TitleList& list) |
| 77 |
{ |
| 78 |
_missionTitles = list; |
| 79 |
} |
| 80 |
|
| 81 |
void DarkmodTxt::ParseMissionTitles(std::vector<std::string>& titleList, const std::string& source) |
| 82 |
{ |
| 83 |
std::size_t titleNum = 1; |
| 84 |
std::size_t endIndex = 0; |
| 85 |
|
| 86 |
while (true) |
| 87 |
{ |
| 88 |
std::string start = fmt::format("Mission {0:d} Title:", titleNum); |
| 89 |
std::string end = fmt::format("Mission {0:d} Title:", titleNum + 1); |
| 90 |
|
| 91 |
std::size_t startIndex = source.find(start, endIndex); |
| 92 |
|
| 93 |
if (startIndex == std::string::npos) break; |
| 94 |
|
| 95 |
endIndex = source.find(end, startIndex); |
| 96 |
|
| 97 |
// Extract next title string |
| 98 |
std::string title = source.substr(startIndex, (endIndex != std::string::npos) ? endIndex - startIndex : source.size() - startIndex); |
| 99 |
string::trim_left(title, start); |
| 100 |
string::trim(title); |
| 101 |
|
| 102 |
titleList.push_back(title); |
| 103 |
|
| 104 |
++titleNum; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
DarkmodTxtPtr DarkmodTxt::CreateFromString(const std::string& contents) |
| 109 |
{ |
| 110 |
DarkmodTxtPtr info(new DarkmodTxt); |
| 111 |
|
| 112 |
try |
| 113 |
{ |
| 114 |
// Determine the positions in the file |
| 115 |
std::size_t titlePos = contents.find("Title:"); |
| 116 |
std::size_t missionTitlesPos = contents.find("Mission 1 Title:"); |
| 117 |
std::size_t descPos = contents.find("Description:"); |
| 118 |
std::size_t authorPos = contents.find("Author:"); |
| 119 |
std::size_t versionPos = contents.find("\nVersion:"); |
| 120 |
std::size_t reqVersionPos = contents.find("Required TDM Version:"); |
| 121 |
|
| 122 |
// Validate the order of the markers in the file |
| 123 |
bool positionsValid = titlePos != std::string::npos && titlePos < descPos && // Title is required & before description (or EOF) |
| 124 |
(missionTitlesPos == std::string::npos || missionTitlesPos < descPos) && // Optional Mission Titles & before description (or EOF) |
| 125 |
(descPos == std::string::npos || descPos < authorPos) && // Optional description & before author (or EOF) |
| 126 |
(authorPos == std::string::npos || authorPos < versionPos) && // Author optional & before description (or EOF) |
| 127 |
(versionPos == std::string::npos || versionPos < reqVersionPos); // Version optional & before req version (or EOF) |
| 128 |
|
| 129 |
if (!positionsValid) |
| 130 |
{ |
| 131 |
throw ParseException(_("Order of the elements Title/Description/Author/etc. is incorrect")); |
| 132 |
} |
| 133 |
|
| 134 |
std::size_t len = contents.size(); |
| 135 |
|
| 136 |
if (titlePos != std::string::npos) |
| 137 |
{ |
| 138 |
std::size_t endPos = (missionTitlesPos != std::string::npos) ? missionTitlesPos : descPos; |
| 139 |
|
| 140 |
info->_title = contents.substr(titlePos, (endPos != std::string::npos) ? endPos - titlePos : len - titlePos); |
| 141 |
string::trim_left(info->_title, "Title:"); |
| 142 |
string::trim(info->_title); |
| 143 |
} |
| 144 |
|
| 145 |
info->_missionTitles.clear(); |
| 146 |
info->_missionTitles.push_back(info->_title); // [0] is title by default |
| 147 |
|
| 148 |
if (missionTitlesPos != std::string::npos) |
| 149 |
{ |
| 150 |
std::string missionTitles = contents.substr(missionTitlesPos, (descPos != std::string::npos) ? descPos - missionTitlesPos: len - missionTitlesPos); |
| 151 |
ParseMissionTitles(info->_missionTitles, missionTitles); |
| 152 |
} |
| 153 |
|
| 154 |
if (descPos != std::string::npos) |
| 155 |
{ |
| 156 |
info->_description = contents.substr(descPos, (authorPos != std::string::npos) ? authorPos - descPos : len - descPos); |
| 157 |
string::trim_left(info->_description, "Description:"); |
| 158 |
string::trim(info->_description); |
| 159 |
} |
| 160 |
|
| 161 |
if (authorPos != std::string::npos) |
| 162 |
{ |
| 163 |
std::size_t endPos = (versionPos != std::string::npos) ? versionPos : reqVersionPos; |
| 164 |
|
| 165 |
info->_author = contents.substr(authorPos, (endPos != std::string::npos) ? endPos - authorPos : len - authorPos); |
| 166 |
string::trim_left(info->_author, "Author:"); |
| 167 |
string::trim(info->_author); |
| 168 |
} |
| 169 |
|
| 170 |
if (versionPos != std::string::npos) |
| 171 |
{ |
| 172 |
info->_version = contents.substr(versionPos, (reqVersionPos != std::string::npos) ? reqVersionPos - versionPos : len - versionPos); |
| 173 |
string::trim_left(info->_version, "\nVersion:"); |
| 174 |
string::trim(info->_version); |
| 175 |
} |
| 176 |
|
| 177 |
if (reqVersionPos != std::string::npos) |
| 178 |
{ |
| 179 |
info->_reqTdmVersion = contents.substr(reqVersionPos, len - reqVersionPos); |
| 180 |
|
| 181 |
string::trim_left(info->_reqTdmVersion, "Required TDM Version:"); |
| 182 |
string::trim_left(info->_reqTdmVersion, "v"); |
| 183 |
string::trim(info->_reqTdmVersion); |
| 184 |
} |
| 185 |
} |
| 186 |
catch (const std::exception& ex) |
| 187 |
{ |
| 188 |
// Convert ordinary exceptions in ParseExceptions |
| 189 |
rError() << "Exception parsing darkmod.txt: " << ex.what() << std::endl; |
| 190 |
throw ParseException(ex.what()); |
| 191 |
} |
| 192 |
|
| 193 |
return info; |
| 194 |
} |
| 195 |
|
| 196 |
DarkmodTxtPtr DarkmodTxt::CreateFromStream(std::istream& stream) |
| 197 |
{ |
| 198 |
// Read all the stream contents into a string |
| 199 |
std::string str(std::istreambuf_iterator<char>(stream), {}); |
| 200 |
return CreateFromString(str); |
| 201 |
} |
| 202 |
|
| 203 |
DarkmodTxtPtr DarkmodTxt::LoadForCurrentMod() |
| 204 |
{ |
| 205 |
std::string darkmodTxtPath = GetOutputPathForCurrentMod() + NAME(); |
| 206 |
|
| 207 |
rMessage() << "Trying to open file " << darkmodTxtPath << std::endl; |
| 208 |
|
| 209 |
ArchiveTextFilePtr file = GlobalFileSystem().openTextFileInAbsolutePath(darkmodTxtPath); |
| 210 |
|
| 211 |
if (file) |
| 212 |
{ |
| 213 |
std::istream stream(&(file->getInputStream())); |
| 214 |
return CreateFromStream(stream); |
| 215 |
} |
| 216 |
|
| 217 |
return std::make_shared<DarkmodTxt>(); |
| 218 |
} |
| 219 |
|
| 220 |
std::string DarkmodTxt::toString() |
| 221 |
{ |
| 222 |
std::string output; |
| 223 |
|
| 224 |
if (!_title.empty()) |
| 225 |
{ |
| 226 |
output += fmt::format("Title: {0}", _title); |
| 227 |
} |
| 228 |
|
| 229 |
if (_missionTitles.size() > 1) |
| 230 |
{ |
| 231 |
// Skip the first string, which is the same as the title |
| 232 |
for (std::size_t i = 1; i < _missionTitles.size(); ++i) |
| 233 |
{ |
| 234 |
output += fmt::format("\nMission {1:d} Title: {0}", _missionTitles[i], i); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (!_description.empty()) |
| 239 |
{ |
| 240 |
output += fmt::format("\nDescription: {0}", _description); |
| 241 |
} |
| 242 |
|
| 243 |
if (!_author.empty()) |
| 244 |
{ |
| 245 |
output += fmt::format("\nAuthor: {0}", _author); |
| 246 |
} |
| 247 |
|
| 248 |
if (!_version.empty()) |
| 249 |
{ |
| 250 |
output += fmt::format("\nVersion: {0}", _version); |
| 251 |
} |
| 252 |
|
| 253 |
if (!_reqTdmVersion.empty()) |
| 254 |
{ |
| 255 |
output += fmt::format("\nRequired TDM Version: {0}", _reqTdmVersion); |
| 256 |
} |
| 257 |
|
| 258 |
return output; |
| 259 |
} |
| 260 |
|
| 261 |
} |