File: FixupMap.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (189 lines) | stat: -rw-r--r-- 4,385 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "FixupMap.h"

#include <fstream>
#include <iostream>
#include <vector>
#include "i18n.h"
#include "iscenegraph.h"
#include "iundo.h"
#include "ui/imainframe.h"
#include "ieclass.h"

#include "wxutil/dialog/MessageBox.h"
#include "os/file.h"
#include "string/string.h"
#include "parser/Tokeniser.h"

#include "ShaderReplacer.h"
#include "SpawnargReplacer.h"
#include "DeprecatedEclassCollector.h"

#include <regex>
#include "string/predicate.h"

FixupMap::FixupMap(const std::string& filename) :
	_filename(filename),
	_progress(_("Fixup in progress"))
{}

FixupMap::Result FixupMap::perform()
{
	UndoableCommand cmd("performFixup");

	// Load contents
	loadFixupFile();

	// Load deprecated entities
	loadDeprecatedEntities();

	// Instantiate a line tokeniser
	parser::BasicStringTokeniser tokeniser(_contents, "\n\r");

	_curLineNumber = 0;
	std::size_t parsedSize = 0;

	while (tokeniser.hasMoreTokens())
	{
		_curLineNumber++;

		std::string line = tokeniser.nextToken();

		performFixup(line);

		// Approximate the progress
		parsedSize += line.size();

		try
		{
			double fraction = static_cast<double>(parsedSize) / _contents.size();
			_progress.setTextAndFraction(
				fmt::format(_("Processing line {0}..."), _curLineNumber),
				fraction);
		}
		catch (wxutil::ModalProgressDialog::OperationAbortedException& ex)
		{
			wxutil::Messagebox box(_("Fixup cancelled"), ex.what(), ui::IDialog::MESSAGE_ERROR);
			box.run();
			return _result;
		}
	}

	_progress.setTextAndFraction(_("Completed"), 1.0);

	return _result;
}

void FixupMap::performFixup(const std::string& line)
{
	// Skip empty lines and comments
	if (line.empty() ||
		string::starts_with(line, "#") ||
		string::starts_with(line, "//"))
	{
		return;
	}

	std::regex expr("^" + MATERIAL_PREFIX + "(.*)\\s=>\\s(.*)$");
	std::smatch matches;

	if (std::regex_match(line, matches, expr))
	{
		// Fixup a specific shader
		std::string oldShader = matches[1];
		std::string newShader = matches[2];

		replaceShader(oldShader, newShader);
		return;
	}

	expr = std::regex("^" + ENTITYDEF_PREFIX + "(.*)\\s=>\\s(.*)$");

	if (std::regex_match(line, matches, expr))
	{
		// Fixup a specific entitydef
		std::string oldDef = matches[1];
		std::string newDef = matches[2];

		// Search all spawnargs
		replaceSpawnarg(oldDef, newDef);
		return;
	}

	// No specific prefix, this can be everything: spawnarg or texture
	expr = std::regex("^(.*)\\s=>\\s(.*)$");

	if (std::regex_match(line, matches, expr))
	{
		std::string oldStr = matches[1];
		std::string newStr = matches[2];

		// First, try to use these strings as shader replacement
		replaceShader(oldStr, newStr);

		// Second, traverse all entities and fix them up
		replaceSpawnarg(oldStr, newStr);
		return;
	}
}

void FixupMap::replaceSpawnarg(const std::string& oldVal, const std::string& newVal)
{
	SpawnargReplacer replacer(oldVal, newVal);
	GlobalSceneGraph().root()->traverseChildren(replacer);

	replacer.processEntities();

	_result.replacedModels += replacer.getModelCount();
	_result.replacedEntities += replacer.getEclassCount();
	_result.replacedMisc += replacer.getOtherCount();
}

void FixupMap::replaceShader(const std::string& oldShader, const std::string& newShader)
{
	// Instantiate a walker
	ShaderReplacer replacer(oldShader, newShader);
	GlobalSceneGraph().root()->traverseChildren(replacer);

	_result.replacedShaders += replacer.getReplaceCount();
}

void FixupMap::loadFixupFile()
{
	// Sanity-check the file
	if (!os::fileOrDirExists(_filename) || !os::fileIsReadable(_filename))
	{
		wxutil::Messagebox::Show(_("File not readable"),
			_("The specified file doesn't exist."), ui::IDialog::MESSAGE_ERROR);
		return;
	}

	// Load the file's contents
	std::ifstream input;
	input.open(_filename.c_str(), std::ios::in|std::ios::ate);

	if (!input)
	{
		wxutil::Messagebox::Show(_("File not readable"),
			_("The specified file can't be opened."), ui::IDialog::MESSAGE_ERROR);
		return;
	}

	std::vector<char> buffer;
	buffer.resize(input.tellg());

	input.seekg(0, std::ios::beg);
	input.read(&buffer.front(), buffer.size());
	input.close();

	_contents = &buffer.front();
}

void FixupMap::loadDeprecatedEntities()
{
	// Traverse all eclasses and collect replacements
	DeprecatedEclassCollector collector;
	GlobalEntityClassManager().forEachEntityClass(collector);

	_contents += "\n";
	_contents += collector.getFixupCode();
}