BundleDataByteParser.h
1 
2 
3 #pragma once
4 
5 #include <string>
6 #include <fstream>
7 #include <map>
8 #include <Windows.h>
9 
10 
11 namespace VoxelFarm
12 {
13  namespace Bundle
14  {
15 
17  {
18  public:
19 
20 #define DLL_LOADS_FILES
21 #ifdef DLL_LOADS_FILES
22  /*
23  **
24  */
25  /*
26  CBundleDataByteParser(const std::string& project_file, const std::string& bundle_file)
27  {
28  keepTrackOfMemory = true;
29 
31  {
32  std::ifstream t(project_file);
33 
34  if (t.is_open())
35  {
36  t.seekg(0, std::ios::end);
37  propFileContents.reserve((size_t)t.tellg());
38  t.seekg(0, std::ios::beg);
39 
40  propFileContents.assign((std::istreambuf_iterator<char>(t)),
41  std::istreambuf_iterator<char>());
42 
43  t.close();
44  }
45  }
47 
48  {
49  std::ifstream t(bundle_file, std::ios::binary);
50 
51  if (t.is_open())
52  {
53 
54  t.seekg(0, std::ios::end);
55  size_t len = (size_t)t.tellg();
56  dataFileContents = new char[len];
57  t.seekg(0, std::ios::beg);
58  t.read(dataFileContents, len);
59  t.close();
60  }
61  }
62 
64 
65  extractOffsetsInBundle();
66  }
67  */
68 #else
69  /*
70  **
71  */
72  /*
73  CBundleDataByteParser(const std::string& project_file, const char* bundle_file_data)
74  {
75  keepTrackOfMemory = false;
76 
77  propFileContents = project_file;
78  dataFileContents = const_cast<char*>(bundle_file_data);
79 
80  extractOffsetsInBundle();
81  }*/
82 #endif
83 
84  /*
85  **
86  */
88  {
89  if (keepTrackOfMemory)
90  {
91  VF_DELETE[] dataFileContents;
92  }
93  }
94 
95  /*
96  **
97  */
99  {
100  }
101 
102  /*
103  **
104  */
105  bool initFromFiles(const std::string& project_file, const std::string& bundle_file)
106  {
107  keepTrackOfMemory = true;
108 
110  {
111  std::ifstream t(project_file);
112 
113  if (t.is_open())
114  {
115  t.seekg(0, std::ios::end);
116  propFileContents.reserve((size_t)t.tellg());
117  t.seekg(0, std::ios::beg);
118 
119  propFileContents.assign((std::istreambuf_iterator<char>(t)),
120  std::istreambuf_iterator<char>());
121 
122  t.close();
123  }
124  }
126 
127  {
128  std::ifstream t(bundle_file, std::ios::binary);
129 
130  if (t.is_open())
131  {
132 
133  t.seekg(0, std::ios::end);
134  size_t len = (size_t)t.tellg();
135  dataFileContents = new char[len];
136  t.seekg(0, std::ios::beg);
137  t.read(dataFileContents, len);
138  t.close();
139  }
140  }
141 
143 
144  extractOffsetsInBundle();
145 
146  return true;
147  }
148 
149  /*
150  **
151  */
152  bool initFromData(const std::string& project_file, const char* bundle_file_data)
153  {
154  keepTrackOfMemory = false;
155 
156  propFileContents = project_file;
157  dataFileContents = const_cast<char*>(bundle_file_data);
158 
159  extractOffsetsInBundle();
160 
161  return true;
162  }
163 
165  // FIELDS
167 
168  public:
169  bool getBool(const std::string& section, const std::string& field, const bool defaultValue) const
170  {
171  std::string field_val;
172  if (!getSectionFieldString(section, field, field_val))
173  {
174  return defaultValue;
175  }
176 
177  return (field_val.c_str()[0] == 'T')?true:false;
178  }
179 
180  int getInt(const std::string& section, const std::string& field, const int defaultValue) const
181  {
182  std::string field_val;
183  if (!getSectionFieldString(section, field, field_val))
184  {
185  return defaultValue;
186  }
187  return atoi(field_val.c_str());
188  }
189 
190  double getFloat(const std::string& section, const std::string& field, const double defaultValue) const
191  {
192  std::string field_val;
193  if (!getSectionFieldString(section, field, field_val))
194  {
195  return defaultValue;
196  }
197  return atof(field_val.c_str());
198  }
199 
200  std::string getString(const std::string& section, const std::string& field, const std::string defaultValue) const
201  {
202  std::string field_val;
203  if (!getSectionFieldString(section, field, field_val))
204  {
205  return defaultValue;
206  }
207  return field_val;
208  }
209 
210  void getString(const std::string& section, const std::string& field, const char* defaultValue, char* str_out, const size_t max_str_size) const
211  {
212  std::string field_val;
213  if (!getSectionFieldString(section, field, field_val))
214  {
215  size_t len = min(strlen(defaultValue), max_str_size - 2);
216  for (size_t i = 0; i<len; ++i)
217  {
218  str_out[i] = defaultValue[i];
219  }
220  str_out[len] = '\0';
221  }
222  else
223  {
224  size_t len = min(strlen(field_val.c_str()), max_str_size - 2);
225  const char* str = field_val.c_str();
226  for (size_t i=0; i<len; ++i)
227  {
228  str_out[i] = str[i];
229  }
230  str_out[len] = '\0';
231  }
232  }
233 
234  private:
235  bool getSectionFieldString(const std::string& section_name_in, const std::string& field_name_in, std::string& field_str_out) const
236  {
237  // extract the section fields
238  const std::size_t sectionTagPos = propFileContents.find("[" + section_name_in + "]");
239  if (sectionTagPos == std::string::npos)
240  {
241  return false;
242  }
243 
244  const size_t sectionStrLen = section_name_in.length() + 2;
245 
246  const std::size_t endSectionPos = propFileContents.find("[", sectionTagPos + sectionStrLen);
247  const std::string fields = propFileContents.substr(sectionTagPos, endSectionPos - sectionTagPos);
248 
249  const size_t fieldStrLen = field_name_in.length();
250 
251  // extract the field value
252 
253  std::size_t fieldPos = fields.find(field_name_in);
254 
255  while (fieldPos != std::string::npos)
256  {
257  const std::size_t valStartPos = fields.find("=", fieldPos) + 1;
258 
259  if (fieldStrLen == valStartPos - fieldPos - 1)
260  {
261  field_str_out = fields.substr(valStartPos, fields.find('\n', valStartPos) - fieldPos - fieldStrLen - 1);
262  return true;
263  }
264 
265  fieldPos = fields.find(field_name_in, valStartPos);
266  }
267 
268  return false;
269  }
270 
271 
273  // MAPS
275 
276  public:
277  template<typename T> T* loadMap(const char* id, const int size, const HWND hostHWND) const
278  {
279  std::string clean_id(id);
280 
281  // remove new line characters from the end
282  while (clean_id[clean_id.length() - 1] == '\n' || clean_id[clean_id.length() - 1] == '\r')
283  {
284  clean_id.erase(clean_id.length() - 1, 1);
285  }
286 
287  T* maskColors = new T[size*size];
288 
289  TMap<std::string, size_t>::const_iterator offset_iter = offsetMap.find(clean_id);
290 
291  if (offset_iter == offsetMap.end())
292  {
293  memcpy(maskColors, 0x0, sizeof(T)*size*size);
294  MessageBox(hostHWND, clean_id.c_str(), "Could not find the id in the bundle!", MB_ICONERROR | MB_OK);
295  }
296  else
297  {
298  size_t offset = offset_iter->second;
299  memcpy(maskColors, dataFileContents + offset, sizeof(T)*size*size);
300  }
301 
302  return maskColors;
303  }
304 
305  /*
306  size_t getDataOffset(const char* id) const
307  {
308  TMap<std::string, size_t>::const_iterator offset_iter = offsetMap.find(id);
309 
310  if (offset_iter == offsetMap.end()) return -1;
311  return offset_iter->second;
312  }
313  */
314 
315  char* getDataWithOffset(const char* id) const
316  {
317  std::string clean_id(id);
318  // remove new line characters from the end
319  while (clean_id[clean_id.length() - 1] == '\n' || clean_id[clean_id.length() - 1] == '\r')
320  {
321  clean_id.erase(clean_id.length() - 1, 1);
322  }
323 
324  TMap<std::string, size_t>::const_iterator offset_iter = offsetMap.find(clean_id);
325 
326  if (offset_iter == offsetMap.end())
327  {
328  return NULL;
329  }
330  return dataFileContents + offset_iter->second;
331  }
332 
333  private:
334 
335  /*
336  **
337  */
338  void extractOffsetsInBundle()
339  {
340  // extract the bundle file offsets
341  const std::size_t bundleTagPos = propFileContents.find("[Bundle]");
342  if (bundleTagPos == std::string::npos)
343  {
344  return;
345  }
346 
347  const std::size_t endBundlePos = propFileContents.find("[", bundleTagPos + 8);
348  const std::string fileOffsetsStr = propFileContents.substr(bundleTagPos + 8, endBundlePos - bundleTagPos - 8); // 8 is to account for [Bundle]
349 
350 
352 
353  // create the file offset map
354 
355  std::size_t markOfInterestPos = fileOffsetsStr.find(".");
356 
357  while (markOfInterestPos != std::string::npos)
358  {
359  const size_t sectionStartPos = markOfInterestPos + 1;
360  markOfInterestPos = fileOffsetsStr.find("=", sectionStartPos);
361 
362  const std::string id_str = fileOffsetsStr.substr(sectionStartPos, markOfInterestPos - sectionStartPos);
363 
364 
365  const std::string offsetStr = fileOffsetsStr.substr(markOfInterestPos + 1, fileOffsetsStr.find('\n', sectionStartPos) - markOfInterestPos - 1);
366 
367  const size_t offset = atoi(offsetStr.c_str());
368 
369  offsetMap[id_str] = offset;
370 
371 
372  markOfInterestPos = fileOffsetsStr.find(".", markOfInterestPos + 1);
373  }
374  }
375 
376  private:
377  std::string propFileContents = "";
378  char* dataFileContents = NULL;
379 
380  TMap<std::string, size_t> offsetMap;
381 
382  private:
383  bool keepTrackOfMemory;
384 
385 
386  public:
387  static bool extractRawDataFromFiles(const std::string& project_dat_filename, const std::string& bundle_filename, std::string& priject_data_out,
388  std::string& bundle_data_out)
389  {
390  {
391  std::ifstream t(project_dat_filename);
392 
393  if (t.is_open())
394  {
395  priject_data_out = "";
396 
397  t.seekg(0, std::ios::end);
398  priject_data_out.reserve((size_t)t.tellg());
399  t.seekg(0, std::ios::beg);
400 
401  priject_data_out.assign((std::istreambuf_iterator<char>(t)),
402  std::istreambuf_iterator<char>());
403 
404  t.close();
405  }
406  else
407  {
408  return false;
409  }
410  }
412 
413  {
414  std::ifstream t(bundle_filename, std::ios::binary);
415 
416  if (t.is_open())
417  {
418  bundle_data_out = "";
419 
420  t.seekg(0, std::ios::end);
421  size_t len = (size_t)t.tellg();
422  bundle_data_out.reserve(len);
423  //dataFileContents = new char[len];
424  t.seekg(0, std::ios::beg);
425  //t.read(dataFileContents, len);
426 
427  bundle_data_out.assign((std::istreambuf_iterator<char>(t)),
428  std::istreambuf_iterator<char>());
429 
430  t.close();
431  }
432  else
433  {
434  return false;
435  }
436  }
437 
438  return true;
439  }
440  };
441 
442 
443 
444 
445  };
446 };
Contains all classes and functions for the VoxelFarm engine.