1 | #include "gwutil.h"
|
---|
2 |
|
---|
3 | #include <fstream>
|
---|
4 | #include <string>
|
---|
5 | #include <sstream>
|
---|
6 | #include <regex>
|
---|
7 | #include <vector>
|
---|
8 | #include <boost/math/tr1.hpp>
|
---|
9 | #include <iostream>
|
---|
10 | #include <cstdlib>
|
---|
11 | #include <boost/filesystem.hpp>
|
---|
12 |
|
---|
13 | boost::posix_time::milliseconds gst_DefaultLockTimeout (60000);
|
---|
14 |
|
---|
15 | namespace geoworld
|
---|
16 | {
|
---|
17 | static const char * MARS_TYCOON_RESULTS_DIR_ENV_KEY = "MarsTycoon_ResultsDir";
|
---|
18 |
|
---|
19 | PNGDEM GLOBALDEM;
|
---|
20 |
|
---|
21 | void rotateGenFiles()
|
---|
22 | {
|
---|
23 | using namespace std;
|
---|
24 | using namespace boost::filesystem;
|
---|
25 |
|
---|
26 | const string sResultsDir (std::getenv(MARS_TYCOON_RESULTS_DIR_ENV_KEY));
|
---|
27 | stringstream ssSpec;
|
---|
28 | int h, r;
|
---|
29 | vector<string> files;
|
---|
30 |
|
---|
31 | ssSpec << sResultsDir << "\\gen.*.png" << std::ends;
|
---|
32 | for (const directory_entry & entry : directory_iterator(ssSpec.str()))
|
---|
33 | {
|
---|
34 | if (entry.status().type() == regular_file)
|
---|
35 | files.push_back(entry.path().stem().string());
|
---|
36 | }
|
---|
37 |
|
---|
38 | sort(files.begin(), files.end());
|
---|
39 | regex rx ("gen\\.([0-9]+)\\.png");
|
---|
40 |
|
---|
41 | for (vector<string>::reverse_iterator i = files.rbegin(); i != files.rend(); ++i)
|
---|
42 | {
|
---|
43 | cmatch m;
|
---|
44 | regex_search(i->c_str(), m, rx);
|
---|
45 |
|
---|
46 | stringstream ssNewName, ssOldName;
|
---|
47 |
|
---|
48 | ssOldName << sResultsDir << "\\" << *i << std::ends;
|
---|
49 | ssNewName << sResultsDir << "\\gen." << std::setw(3) << std::setfill('0') << (atoi(m[1].str().c_str()) + 1) << ".png" << std::ends;
|
---|
50 | rename(ssOldName.str(), ssNewName.str());
|
---|
51 | }
|
---|
52 |
|
---|
53 | stringstream ssDefault;
|
---|
54 | stringstream ssNewName;
|
---|
55 |
|
---|
56 | ssDefault << sResultsDir << "\\gen.png" << std::ends;
|
---|
57 | ssNewName << sResultsDir << "\\gen.001.png" << std::ends;
|
---|
58 | rename (ssDefault.str(), ssNewName.str());
|
---|
59 | }
|
---|
60 |
|
---|
61 | void dumpPNGDEM( PNGDEM & pngdem )
|
---|
62 | {
|
---|
63 | rotateGenFiles();
|
---|
64 | writePNGDEM(pngdem, "gen.png");
|
---|
65 | }
|
---|
66 |
|
---|
67 | void loadPNGDEM( PNGDEM & pngdem, const std::string & sName )
|
---|
68 | {
|
---|
69 | using namespace std;
|
---|
70 |
|
---|
71 | stringstream ssPath;
|
---|
72 |
|
---|
73 | ssPath << std::getenv(MARS_TYCOON_RESULTS_DIR_ENV_KEY) << "\\" << sName << std::ends;
|
---|
74 | std::ifstream ifs (ssPath.str(), ios::binary | ios::in);
|
---|
75 | pngdem << ifs;
|
---|
76 | ifs.close();
|
---|
77 | }
|
---|
78 |
|
---|
79 | void writePNGDEM( PNGDEM & pngdem, const std::string & sName )
|
---|
80 | {
|
---|
81 | using namespace std;
|
---|
82 |
|
---|
83 | const string sResultsDir = std::getenv(MARS_TYCOON_RESULTS_DIR_ENV_KEY);
|
---|
84 | stringstream ssPath;
|
---|
85 |
|
---|
86 | ssPath << sResultsDir << '\\' << sName << std::ends;
|
---|
87 |
|
---|
88 | std::ofstream ofs (ssPath.str(), ios::out | ios::binary | ios::ate);
|
---|
89 |
|
---|
90 | pngdem >> ofs;
|
---|
91 | ofs.close();
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|