Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ModConfig.cpp
Go to the documentation of this file.
1 /* Project Starshatter 5.0
2  Destroyer Studios LLC
3  Copyright (C) 1997-2007. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: ModConfig.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Mod file deployment configuration and manager
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "ModConfig.h"
18 #include "ModInfo.h"
19 #include "Campaign.h"
20 #include "ShipDesign.h"
21 #include "WeaponDesign.h"
22 #include "Starshatter.h"
23 #include "ParseUtil.h"
24 #include "DataLoader.h"
25 
26 // +-------------------------------------------------------------------+
27 
28 static ModConfig* mod_config = 0;
29 
30 // +-------------------------------------------------------------------+
31 
33 {
34  mod_config = this;
35 
36  Load();
37  FindMods();
38  Deploy();
39 }
40 
42 {
43  if (mod_config == this)
44  mod_config = 0;
45 
46  Undeploy();
47 
48  enabled.destroy();
49  disabled.destroy();
50  mods.destroy();
51 }
52 
53 // +-------------------------------------------------------------------+
54 
55 void
57 {
58  mod_config = new(__FILE__,__LINE__) ModConfig;
59 }
60 
61 void
63 {
64  delete mod_config;
65  mod_config = 0;
66 }
67 
68 // +-------------------------------------------------------------------+
69 
70 ModConfig*
72 {
73  return mod_config;
74 }
75 
76 // +-------------------------------------------------------------------+
77 
78 void
80 {
81  // read the config file:
82  BYTE* block = 0;
83  int blocklen = 0;
84 
85  char filename[64];
86  strcpy_s(filename, "mod.cfg");
87 
88  FILE* f;
89  ::fopen_s(&f, filename, "rb");
90 
91  if (f) {
92  ::fseek(f, 0, SEEK_END);
93  blocklen = ftell(f);
94  ::fseek(f, 0, SEEK_SET);
95 
96  block = new(__FILE__,__LINE__) BYTE[blocklen+1];
97  block[blocklen] = 0;
98 
99  ::fread(block, blocklen, 1, f);
100  ::fclose(f);
101  }
102 
103  if (blocklen == 0)
104  return;
105 
106  Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
107  Term* term = parser.ParseTerm();
108 
109  if (!term) {
110  Print("ERROR: could not parse '%s'.\n", filename);
111  return;
112  }
113  else {
114  TermText* file_type = term->isText();
115  if (!file_type || file_type->value() != "MOD_CONFIG") {
116  Print("WARNING: invalid '%s' file. No mods deployed\n", filename);
117  return;
118  }
119  }
120 
121  enabled.destroy();
122 
123  do {
124  delete term;
125 
126  term = parser.ParseTerm();
127 
128  if (term) {
129  TermDef* def = term->isDef();
130  if (def) {
131  Text name;
132  GetDefText(name, def, filename);
133  enabled.append(new(__FILE__,__LINE__) Text(name));
134  }
135  }
136  }
137  while (term);
138 
139  delete [] block;
140 }
141 
142 void
144 {
145  FILE* f;
146  fopen_s(&f, "mod.cfg", "w");
147  if (f) {
148  fprintf(f, "MOD_CONFIG\n\n");
149 
150  ListIter<Text> iter = enabled;
151  while (++iter) {
152  Text* name = iter.value();
153  fprintf(f, "mod: \"%s\"\n", name->data());
154  }
155 
156  fclose(f);
157  }
158 }
159 
160 void
162 {
163  disabled.destroy();
164 
165  DataLoader* loader = DataLoader::GetLoader();
166 
167  if (loader) {
168  loader->UseFileSystem(true);
169  loader->ListFiles("*.dat", disabled, true);
171 
172  ListIter<Text> iter = disabled;
173  while (++iter) {
174  Text* name = iter.value();
175  name->setSensitive(false);
176 
177  if (*name == "shatter.dat" ||
178  *name == "beta.dat" ||
179  *name == "start.dat" ||
180  *name == "irunin.dat" ||
181  *name == "vox.dat" ||
182  name->contains("uninstall") ||
183  enabled.contains(name))
184  delete iter.removeItem();
185  }
186  }
187 }
188 
189 // +-------------------------------------------------------------------+
190 
191 ModInfo*
193 {
194  for (int i = 0; i < mods.size(); i++) {
195  ModInfo* mod_info = mods[i];
196 
197  if (mod_info->Filename() == filename && mod_info->IsEnabled())
198  return mod_info;
199  }
200 
201  return NULL;
202 }
203 
204 // +-------------------------------------------------------------------+
205 
206 bool
208 {
209  for (int i = 0; i < mods.size(); i++) {
210  ModInfo* mod_info = mods[i];
211 
212  if (mod_info->Name() == name && mod_info->IsEnabled())
213  return true;
214  }
215 
216  return false;
217 }
218 
219 // +-------------------------------------------------------------------+
220 
221 void
223 {
224  Save();
225 
226  if (enabled.size() < 1)
227  return;
228 
229  Print("\nDEPLOYING MODS\n--------------\n");
230 
231  int i = 1;
232  ListIter<Text> iter = enabled;
233  while (++iter) {
234  Text* name = iter.value();
235 
236  if (IsDeployed(name->data())) {
237  Print(" %d. %s is already deployed (skipping)\n", i++, name->data());
238  continue;
239  }
240 
241  Print(" %d. %s\n", i++, name->data());
242 
243  ModInfo* mod_info = new(__FILE__,__LINE__) ModInfo;
244 
245  if (mod_info->Load(name->data()) && mod_info->Enable()) {
246  mods.append(mod_info);
247  }
248  else {
249  Print(" Could not deploy '%s' - disabling\n", name->data());
250 
251  delete mod_info;
252  iter.removeItem();
253  disabled.append(name);
254  }
255  }
256 
257  Print("\n");
258  Game::UseLocale(0);
259 }
260 
261 void
263 {
264  Print("UNDEPLOYING MODS\n");
265  mods.destroy();
266 
269 }
270 
271 void
273 {
274  Undeploy();
275  Deploy();
276 
277  Campaign::Close();
279  Campaign::SelectCampaign("Single Missions");
280 }
281 
282 // +-------------------------------------------------------------------+
283 
284 void
286 {
287  if (!name || !*name)
288  return;
289 
290  Text* mod_name;
291 
292  ListIter<Text> iter = disabled;
293  while (++iter) {
294  Text* t = iter.value();
295 
296  if (*t == name) {
297  mod_name = t;
298  iter.removeItem();
299  break;
300  }
301  }
302 
303  if (mod_name) {
304  enabled.append(mod_name);
305 
306  if (!IsDeployed(*mod_name)) {
307  ModInfo* mod_info = new(__FILE__,__LINE__) ModInfo;
308 
309  if (mod_info->Load(*mod_name) && mod_info->Enable()) {
310  mods.append(mod_info);
311  }
312  }
313  }
314 }
315 
316 void
318 {
319  if (!name || !*name)
320  return;
321 
322  Text* mod_name;
323 
324  ListIter<Text> iter = enabled;
325  while (++iter) {
326  Text* t = iter.value();
327 
328  if (*t == name) {
329  mod_name = t;
330  iter.removeItem();
331  break;
332  }
333  }
334 
335  if (mod_name) {
336  disabled.append(mod_name);
337 
338  ListIter<ModInfo> iter = mods;
339  while (++iter) {
340  ModInfo* mod_info = iter.value();
341  if (mod_info->Name() == *mod_name) {
342  delete iter.removeItem();
343  break;
344  }
345  }
346  }
347 }
348 
349 // +-------------------------------------------------------------------+
350 
351 void
353 {
354  if (mod_index > 0 && mod_index < enabled.size()) {
355  Text* mod1 = enabled.at(mod_index-1);
356  Text* mod2 = enabled.at(mod_index);
357 
358  enabled.at(mod_index-1) = mod2;
359  enabled.at(mod_index) = mod1;
360  }
361 }
362 
363 void
365 {
366  if (mod_index >= 0 && mod_index < enabled.size()-1) {
367  Text* mod1 = enabled.at(mod_index);
368  Text* mod2 = enabled.at(mod_index+1);
369 
370  enabled.at(mod_index) = mod2;
371  enabled.at(mod_index+1) = mod1;
372  }
373 }
374