Debian Codegraph @

code/datastructures/FlagFileData.cpp

raw
1 /*
2 Copyright (C) 2009-2012 wxLauncher Team
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <wx/wx.h>
20
21 #include "datastructures/FlagFileData.h"
22
23 #include "global/MemoryDebugging.h"
24
25 int Flag::flagIndexCounter = 0;
26
27 Flag::Flag()
28 : flagIndex(flagIndexCounter++) {
29 }
30
31 #include <wx/listimpl.cpp> // Magic Incantation
32 WX_DEFINE_LIST(FlagList);
33
34 #include <wx/listimpl.cpp> // Magic Incantation
35 WX_DEFINE_LIST(FlagCategoryList);
36
37 FlagSet::FlagSet(wxString name)
38 : name(name) {
39 }
40
41 #include <wx/listimpl.cpp> // Magic Incantation
42 WX_DEFINE_LIST(FlagSetsList);
43
44 ProxyFlagDataItem::ProxyFlagDataItem(const wxString& flagString, int flagIndex)
45 : flagString(flagString), flagIndex(flagIndex) {
46 }
47
48 #include <wx/listimpl.cpp> // Magic Incantation
49 WX_DEFINE_LIST(ProxyFlagData);
50
51 FlagListBoxDataItem::FlagListBoxDataItem(const wxString& fsoCategory)
52 : fsoCategory(fsoCategory),
53 shortDescription(wxEmptyString),
54 flagString(wxEmptyString),
55 isRecommendedFlag(false) {
56 wxASSERT(!fsoCategory.IsEmpty());
57 }
58
59 FlagListBoxDataItem::FlagListBoxDataItem(const wxString& shortDescription,
60 const wxString& flagString, bool isRecommendedFlag)
61 : fsoCategory(wxEmptyString),
62 shortDescription(shortDescription),
63 flagString(flagString),
64 isRecommendedFlag(isRecommendedFlag) {
65 // shortDescription can be empty
66 wxASSERT(!flagString.IsEmpty());
67 }
68
69 #include <wx/listimpl.cpp> // Magic Incantation
70 WX_DEFINE_LIST(FlagListBoxData);
71
72 FlagFileData::FlagFileData()
73 : isProxyDataGenerated(false),
74 isFlagListBoxDataGenerated(false) {
75 }
76
77 FlagFileData::~FlagFileData() {
78 for ( FlagCategoryList::iterator catIter = this->begin(); catIter != this->end(); catIter++ ) {
79 FlagCategory* category = *catIter;
80 for ( FlagList::iterator iter = category->flags.begin(); iter != category->flags.end(); iter++ ) {
81 Flag *flag = *iter;
82 delete flag;
83 }
84 category->flags.Clear();
85
86 delete category;
87 }
88 this->allSupportedFlagsByCategory.Clear();
89
90 FlagSetsList::iterator flagSetIter = this->flagSets.begin();
91 while ( flagSetIter != this->flagSets.end() ) {
92 delete *flagSetIter;
93 flagSetIter++;
94 }
95 this->flagSets.clear();
96 }
97
98 void FlagFileData::AddEasyFlag(const wxString& easyFlag) {
99 wxASSERT(!easyFlag.IsEmpty());
100 wxASSERT_MSG(this->easyFlags.Index(easyFlag.c_str()) == wxNOT_FOUND,
101 wxString::Format(_T("attempted to add easy flag '%s' a second time"), easyFlag.c_str()));
102
103 this->easyFlags.Add(easyFlag);
104 }
105
106 void FlagFileData::AddFlag(Flag* flag) {
107 wxASSERT(flag != NULL);
108 wxASSERT(!flag->fsoCatagory.IsEmpty());
109
110 FlagCategoryList::iterator iter;
111 for (iter = this->begin(); iter != this->end(); iter++ ) {
112 if ( flag->fsoCatagory == (*iter)->categoryName ) {
113 break;
114 }
115 }
116 if ( iter == this->end() ) {
117 // did not find the category, so add it
118 FlagCategory* flagCat = new FlagCategory();
119 flagCat->categoryName = flag->fsoCatagory;
120
121 Flag* headFlag = new Flag();
122 headFlag->fsoCatagory = flag->fsoCatagory;
123 headFlag->isRecomendedFlag = false;
124 flagCat->flags.Append(headFlag);
125 flagCat->flags.Append(flag);
126 this->allSupportedFlagsByCategory.Append(flagCat);
127 } else {
128 (*iter)->flags.Append(flag);
129 }
130 }
131
132 void FlagFileData::GenerateFlagSets() {
133 wxASSERT_MSG(!this->easyFlags.IsEmpty(),
134 _T("GenerateFlagSets() called when there are no easy flag categories."));
135 // GenerateFlagSets should be run exactly once, at least until the new mod.ini support is working
136 wxASSERT_MSG(this->flagSets.IsEmpty(),
137 _T("GenerateFlagSets() called when there already are flag sets."));
138
139 // \todo include the flag sets of the mod.inis as well
140
141 // custom
142 this->flagSets.Append(new FlagSet(_("Custom")));
143
144 // the easy flags.
145 wxUint32 counter = 0;
146 wxArrayString::const_iterator easyIter = this->easyFlags.begin();
147 while ( easyIter != this->easyFlags.end() ) {
148 wxString easyFlag = *easyIter;
149
150 if ( easyFlag.StartsWith(_T("Custom")) ) {
151 // do nothing, we already have a custom
152 } else {
153 FlagSet* flagSet = new FlagSet(easyFlag);
154 for (FlagCategoryList::const_iterator catIter = this->begin(); catIter != this->end(); catIter++) {
155 for (FlagList::const_iterator flagIter = (*catIter)->flags.begin();
156 flagIter != (*catIter)->flags.end(); flagIter++) {
157
158 Flag* flag = *flagIter;
159
160 if ( !flag->flagString.IsEmpty()
161 && (flag->easyEnable & counter) > 0 ) {
162 flagSet->flagsToEnable.Add(flag->flagString);
163 }
164 if ( !flag->flagString.IsEmpty()
165 && (flag->easyDisable & counter) > 0 ) {
166 flagSet->flagsToDisable.Add(flag->flagString);
167 }
168 }
169 }
170 this->flagSets.Append(flagSet);
171 }
172
173 if (counter < 1) {
174 counter = 2; // prime the counter so we can bitshift for the rest
175 } else {
176 counter = counter << 1;
177 }
178 if ( counter > (wxUint32)(1 << 31) ) {
179 // we have checked 31 bits of counter, this is too many easy flag sets
180 easyIter = this->easyFlags.end();
181 wxLogError(_T("FS2 Open executable has more than 31 easy flag categories"));
182 } else {
183 easyIter++;
184 }
185 }
186 }
187
188 ProxyFlagData* FlagFileData::GenerateProxyFlagData() const {
189 wxASSERT(!this->allSupportedFlagsByCategory.IsEmpty());
190 wxASSERT_MSG(!this->isProxyDataGenerated,
191 _T("Attempted to generate proxy data twice.")); // should never need to generate proxy data twice
192
193 ProxyFlagData* proxyData = new ProxyFlagData();
194
195 for (FlagCategoryList::const_iterator catIter = this->begin(); catIter != this->end(); catIter++) {
196 for (FlagList::const_iterator flagIter = (*catIter)->flags.begin(); flagIter != (*catIter)->flags.end(); flagIter++) {
197 Flag* flag = *flagIter;
198
199 if (flag->flagString.IsEmpty()) { // must be a category header
200 continue;
201 }
202
203 proxyData->Append(new ProxyFlagDataItem(flag->flagString, flag->GetFlagIndex()));
204 }
205 }
206
207 // keep const in the function prototype to avoid corrupting data, but allow for making this one change
208 const_cast<FlagFileData*>(this)->isProxyDataGenerated = true;
209
210 return proxyData;
211 }
212
213 FlagListBoxData* FlagFileData::GenerateFlagListBoxData() const {
214 wxASSERT(!this->allSupportedFlagsByCategory.IsEmpty());
215 wxASSERT_MSG(!this->isFlagListBoxDataGenerated,
216 _T("Attempted to generate flag list box data twice."));
217
218 FlagListBoxData* flagListBoxData = new FlagListBoxData();
219
220 for (FlagCategoryList::const_iterator catIter = this->begin();
221 catIter != this->end();
222 catIter++) {
223 for (FlagList::const_iterator flagIter = (*catIter)->flags.begin();
224 flagIter != (*catIter)->flags.end();
225 flagIter++) {
226 Flag* flag = *flagIter;
227
228 if (flag->flagString.IsEmpty()) {
229 flagListBoxData->Append(
230 new FlagListBoxDataItem(flag->fsoCatagory));
231 } else {
232 flagListBoxData->Append(
233 new FlagListBoxDataItem(
234 flag->shortDescription,
235 flag->flagString,
236 flag->isRecomendedFlag));
237 }
238 }
239 }
240
241 // keep const in the function prototype to avoid corrupting data,
242 // but allow for making this one change
243 const_cast<FlagFileData*>(this)->isFlagListBoxDataGenerated = true;
244
245 return flagListBoxData;
246 }
247
248 size_t FlagFileData::GetItemCount() const {
249 size_t itemCount = 0;
250
251 for (FlagCategoryList::const_iterator iter = this->begin(); iter != this->end(); iter++) {
252 itemCount += (*iter)->flags.GetCount();
253 }
254
255 return itemCount;
256 }
257
258 const FlagSet* FlagFileData::GetFlagSet(const wxString& flagSetName) const {
259 wxCHECK_MSG(!this->flagSets.IsEmpty(), NULL,
260 wxString::Format(
261 _T("Attempted to set flag set '%s' when there are no flag sets."),
262 flagSetName.c_str()));
263 // TODO once new mod.ini supported, may need to rethink this assert,
264 // and possibly also regenerate flag sets
265
266 FlagSetsList::const_iterator flagSetsIter = this->flagSets.begin();
267 while(flagSetsIter != this->flagSets.end()) {
268 if ( (*flagSetsIter)->name.StartsWith(flagSetName) ) {
269 return *flagSetsIter;
270 }
271 flagSetsIter++;
272 }
273 wxLogWarning(_T("GetFlagSet(): could not find set %s"),
274 flagSetName.c_str());
275 return NULL;
276 }
277
278 void FlagFileData::GetFlagSetNames(wxArrayString& arr) const {
279 wxCHECK_RET(!this->flagSets.IsEmpty(),
280 _T("Attempted to get flag sets when there are none."));
281 // TODO once new mod.ini supported, may need to rethink this assert,
282 // and possibly also regenerate flag sets
283
284 FlagSetsList::const_iterator flagSetsIter = this->flagSets.begin();
285 while ( flagSetsIter != this->flagSets.end() ) {
286 arr.Add((*flagSetsIter)->name);
287 flagSetsIter++;
288 }
289 }
290
291 const wxString* FlagFileData::GetWebURL(const int n) const {
292 wxCHECK_MSG(n >= 0 && n < static_cast<int>(this->GetItemCount()),
293 NULL,
294 wxString::Format(_T("GetWebURL(): given invalid index %d"), n));
295
296 int flagIndex = 0;
297
298 FlagCategoryList::const_iterator category = this->begin();
299 while (category != this->end()) {
300 FlagList::const_iterator flag = (*category)->flags.begin();
301 while( flag != (*category)->flags.end() ) {
302 if (flagIndex == n) {
303 return &((*flag)->webURL);
304 }
305 flag++;
306 flagIndex++;
307 }
308 category++;
309 }
310
311 wxFAIL_MSG(_T("GetWebURL(): should never reach here!"));
312 return NULL;
313 }