| 1 |
#include "FixupMap.h" |
| 2 |
|
| 3 |
#include <fstream> |
| 4 |
#include <iostream> |
| 5 |
#include <vector> |
| 6 |
#include "i18n.h" |
| 7 |
#include "iscenegraph.h" |
| 8 |
#include "iundo.h" |
| 9 |
#include "ui/imainframe.h" |
| 10 |
#include "ieclass.h" |
| 11 |
|
| 12 |
#include "wxutil/dialog/MessageBox.h" |
| 13 |
#include "os/file.h" |
| 14 |
#include "string/string.h" |
| 15 |
#include "parser/Tokeniser.h" |
| 16 |
|
| 17 |
#include "ShaderReplacer.h" |
| 18 |
#include "SpawnargReplacer.h" |
| 19 |
#include "DeprecatedEclassCollector.h" |
| 20 |
|
| 21 |
#include <regex> |
| 22 |
#include "string/predicate.h" |
| 23 |
|
| 24 |
FixupMap::FixupMap(const std::string& filename) : |
| 25 |
_filename(filename), |
| 26 |
_progress(_("Fixup in progress")) |
| 27 |
{} |
| 28 |
|
| 29 |
FixupMap::Result FixupMap::perform() |
| 30 |
{ |
| 31 |
UndoableCommand cmd("performFixup"); |
| 32 |
|
| 33 |
// Load contents |
| 34 |
loadFixupFile(); |
| 35 |
|
| 36 |
// Load deprecated entities |
| 37 |
loadDeprecatedEntities(); |
| 38 |
|
| 39 |
// Instantiate a line tokeniser |
| 40 |
parser::BasicStringTokeniser tokeniser(_contents, "\n\r"); |
| 41 |
|
| 42 |
_curLineNumber = 0; |
| 43 |
std::size_t parsedSize = 0; |
| 44 |
|
| 45 |
while (tokeniser.hasMoreTokens()) |
| 46 |
{ |
| 47 |
_curLineNumber++; |
| 48 |
|
| 49 |
std::string line = tokeniser.nextToken(); |
| 50 |
|
| 51 |
performFixup(line); |
| 52 |
|
| 53 |
// Approximate the progress |
| 54 |
parsedSize += line.size(); |
| 55 |
|
| 56 |
try |
| 57 |
{ |
| 58 |
double fraction = static_cast<double>(parsedSize) / _contents.size(); |
| 59 |
_progress.setTextAndFraction( |
| 60 |
fmt::format(_("Processing line {0}..."), _curLineNumber), |
| 61 |
fraction); |
| 62 |
} |
| 63 |
catch (wxutil::ModalProgressDialog::OperationAbortedException& ex) |
| 64 |
{ |
| 65 |
wxutil::Messagebox box(_("Fixup cancelled"), ex.what(), ui::IDialog::MESSAGE_ERROR); |
| 66 |
box.run(); |
| 67 |
return _result; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
_progress.setTextAndFraction(_("Completed"), 1.0); |
| 72 |
|
| 73 |
return _result; |
| 74 |
} |
| 75 |
|
| 76 |
void FixupMap::performFixup(const std::string& line) |
| 77 |
{ |
| 78 |
// Skip empty lines and comments |
| 79 |
if (line.empty() || |
| 80 |
string::starts_with(line, "#") || |
| 81 |
string::starts_with(line, "//")) |
| 82 |
{ |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
std::regex expr("^" + MATERIAL_PREFIX + "(.*)\\s=>\\s(.*)$"); |
| 87 |
std::smatch matches; |
| 88 |
|
| 89 |
if (std::regex_match(line, matches, expr)) |
| 90 |
{ |
| 91 |
// Fixup a specific shader |
| 92 |
std::string oldShader = matches[1]; |
| 93 |
std::string newShader = matches[2]; |
| 94 |
|
| 95 |
replaceShader(oldShader, newShader); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
expr = std::regex("^" + ENTITYDEF_PREFIX + "(.*)\\s=>\\s(.*)$"); |
| 100 |
|
| 101 |
if (std::regex_match(line, matches, expr)) |
| 102 |
{ |
| 103 |
// Fixup a specific entitydef |
| 104 |
std::string oldDef = matches[1]; |
| 105 |
std::string newDef = matches[2]; |
| 106 |
|
| 107 |
// Search all spawnargs |
| 108 |
replaceSpawnarg(oldDef, newDef); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
// No specific prefix, this can be everything: spawnarg or texture |
| 113 |
expr = std::regex("^(.*)\\s=>\\s(.*)$"); |
| 114 |
|
| 115 |
if (std::regex_match(line, matches, expr)) |
| 116 |
{ |
| 117 |
std::string oldStr = matches[1]; |
| 118 |
std::string newStr = matches[2]; |
| 119 |
|
| 120 |
// First, try to use these strings as shader replacement |
| 121 |
replaceShader(oldStr, newStr); |
| 122 |
|
| 123 |
// Second, traverse all entities and fix them up |
| 124 |
replaceSpawnarg(oldStr, newStr); |
| 125 |
return; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void FixupMap::replaceSpawnarg(const std::string& oldVal, const std::string& newVal) |
| 130 |
{ |
| 131 |
SpawnargReplacer replacer(oldVal, newVal); |
| 132 |
GlobalSceneGraph().root()->traverseChildren(replacer); |
| 133 |
|
| 134 |
replacer.processEntities(); |
| 135 |
|
| 136 |
_result.replacedModels += replacer.getModelCount(); |
| 137 |
_result.replacedEntities += replacer.getEclassCount(); |
| 138 |
_result.replacedMisc += replacer.getOtherCount(); |
| 139 |
} |
| 140 |
|
| 141 |
void FixupMap::replaceShader(const std::string& oldShader, const std::string& newShader) |
| 142 |
{ |
| 143 |
// Instantiate a walker |
| 144 |
ShaderReplacer replacer(oldShader, newShader); |
| 145 |
GlobalSceneGraph().root()->traverseChildren(replacer); |
| 146 |
|
| 147 |
_result.replacedShaders += replacer.getReplaceCount(); |
| 148 |
} |
| 149 |
|
| 150 |
void FixupMap::loadFixupFile() |
| 151 |
{ |
| 152 |
// Sanity-check the file |
| 153 |
if (!os::fileOrDirExists(_filename) || !os::fileIsReadable(_filename)) |
| 154 |
{ |
| 155 |
wxutil::Messagebox::Show(_("File not readable"), |
| 156 |
_("The specified file doesn't exist."), ui::IDialog::MESSAGE_ERROR); |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// Load the file's contents |
| 161 |
std::ifstream input; |
| 162 |
input.open(_filename.c_str(), std::ios::in|std::ios::ate); |
| 163 |
|
| 164 |
if (!input) |
| 165 |
{ |
| 166 |
wxutil::Messagebox::Show(_("File not readable"), |
| 167 |
_("The specified file can't be opened."), ui::IDialog::MESSAGE_ERROR); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
std::vector<char> buffer; |
| 172 |
buffer.resize(input.tellg()); |
| 173 |
|
| 174 |
input.seekg(0, std::ios::beg); |
| 175 |
input.read(&buffer.front(), buffer.size()); |
| 176 |
input.close(); |
| 177 |
|
| 178 |
_contents = &buffer.front(); |
| 179 |
} |
| 180 |
|
| 181 |
void FixupMap::loadDeprecatedEntities() |
| 182 |
{ |
| 183 |
// Traverse all eclasses and collect replacements |
| 184 |
DeprecatedEclassCollector collector; |
| 185 |
GlobalEntityClassManager().forEachEntityClass(collector); |
| 186 |
|
| 187 |
_contents += "\n"; |
| 188 |
_contents += collector.getFixupCode(); |
| 189 |
} |