-
Notifications
You must be signed in to change notification settings - Fork 3
/
wc3objectvalidate.cpp
240 lines (191 loc) · 7.86 KB
/
wc3objectvalidate.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/***************************************************************************
* Copyright (C) 2014 by Tamino Dauth *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include "../map.hpp"
#include "../../editor.hpp"
#include "../../exception.hpp"
using namespace wc3lib;
using namespace wc3lib::map;
using namespace wc3lib::editor;
namespace
{
void loadObjectData(ObjectData &objectData, const CustomObjects &customObjects, const QUrl &stringsUrl)
{
objectData.load(nullptr);
objectData.importCustomObjects(customObjects);
objectData.applyMapStrings(stringsUrl);
}
void validateObjectDataTooltipReferences(ObjectData &objectData, const QString &name)
{
const QStringList errors = objectData.validateTooltipReferences();
std::cerr << boost::format("%1% Errors (%2%):") % name.toStdString() % errors.size() << std::endl;
for (const QString &error : errors)
{
std::cerr << error.toStdString() << std::endl;
}
}
}
/**
* Checks if all tooltip references are valid and prints errors for invalid ones.
*/
int main(int argc, char *argv[])
{
string wc3Dir;
string stringsFile;
string input;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("version,V", _("Shows current version of wc3objectvalidate."))
("help,h",_("Shows this text."))
("w", boost::program_options::value<string>(&wc3Dir), _("Warcraft III directory."))
("s", boost::program_options::value<string>(&stringsFile), _("war3map.wts strings file."))
("i", boost::program_options::value<string>(&input), _("Input object collection data file."))
;
boost::program_options::positional_options_description p;
p.add("w", 1);
p.add("s", 1);
p.add("i", 1);
boost::program_options::variables_map vm;
try
{
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
}
catch (const std::exception &exception)
{
std::cerr << boost::format(_("Error while parsing program options: \"%1%\"")) % exception.what() << std::endl;
std::cerr << boost::format(_("Usage: %1% [options] <Warcraft III directory> <map string file> <input object collection data file>")) % argv[0] << std::endl;
return EXIT_FAILURE;
}
boost::program_options::notify(vm);
if (vm.count("version"))
{
static const char *version = "0.1";
std::cout << boost::format(_(
"wc3objectvalidate %1%.\n"
"Copyright © 2010 Tamino Dauth\n"
"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law."
)) % version << std::endl;
return EXIT_SUCCESS;
}
if (vm.count("help"))
{
std::cout << _("Usage: wc3objectvalidate [options] <Warcraft III directory> <map string file> <input object collection data file>") << std::endl << std::endl;
std::cout << desc << std::endl;
std::cout << _("\nReport bugs to [email protected] or on https://wc3lib.org") << std::endl;
return EXIT_SUCCESS;
}
try
{
if (!boost::filesystem::is_directory(wc3Dir))
{
throw Exception(boost::format(_("%1% is not a directory.")) % wc3Dir);
}
if (!boost::filesystem::exists(stringsFile))
{
throw Exception(boost::format(_("Strings file %1% does not exist.")) % stringsFile);
}
if (!boost::filesystem::exists(input))
{
throw Exception(boost::format(_("Input file %1% does not exist.")) % input);
}
std::ifstream in(input);
CustomObjectsCollection customObjectsCollection;
customObjectsCollection.read(in);
MpqPriorityList source;
if (!source.addDefaultSources(QUrl::fromLocalFile(wc3Dir.c_str())))
{
throw Exception(boost::format(_("Could not load the default MPQ archives of Warcraft III from the directory %1%.")) % wc3Dir);
}
const boost::filesystem::path stringsFilePath = boost::filesystem::canonical(stringsFile);
const QUrl stringsUrl = "file://" + QString::fromStdString(stringsFilePath.string());
/*
* Use the shared object data, so references to other object data in tooltips works.
* Load it first that EVERYTHING is loaded when validating it.
*/
if (customObjectsCollection.hasUnits())
{
loadObjectData(*source.sharedData()->sharedObjectData()->unitData(), *customObjectsCollection.units(), stringsUrl);
}
if (customObjectsCollection.hasItems())
{
loadObjectData(*source.sharedData()->sharedObjectData()->itemData(), *customObjectsCollection.items(), stringsUrl);
}
if (customObjectsCollection.hasAbilities())
{
loadObjectData(*source.sharedData()->sharedObjectData()->abilityData(), *customObjectsCollection.abilities(), stringsUrl);
}
if (customObjectsCollection.hasBuffs())
{
loadObjectData(*source.sharedData()->sharedObjectData()->buffData(), *customObjectsCollection.buffs(), stringsUrl);
}
if (customObjectsCollection.hasDestructibles())
{
loadObjectData(*source.sharedData()->sharedObjectData()->destructableData(), *customObjectsCollection.destructibles(), stringsUrl);
}
if (customObjectsCollection.hasDoodads())
{
loadObjectData(*source.sharedData()->sharedObjectData()->doodadData(), *customObjectsCollection.doodads(), stringsUrl);
}
if (customObjectsCollection.hasUpgrades())
{
loadObjectData(*source.sharedData()->sharedObjectData()->upgradeData(), *customObjectsCollection.upgrades(), stringsUrl);
}
// Now validate
if (customObjectsCollection.hasUnits())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->unitData(), _("Unit"));
}
if (customObjectsCollection.hasItems())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->itemData(), _("Item"));
}
if (customObjectsCollection.hasAbilities())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->abilityData(), _("Ability"));
}
if (customObjectsCollection.hasBuffs())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->buffData(), _("Buff"));
}
if (customObjectsCollection.hasDestructibles())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->destructableData(), _("Destructable"));
}
if (customObjectsCollection.hasDoodads())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->doodadData(), _("Doodad"));
}
if (customObjectsCollection.hasUpgrades())
{
validateObjectDataTooltipReferences(*source.sharedData()->sharedObjectData()->upgradeData(), _("Upgrade"));
}
}
catch (const Exception &e)
{
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}