· 8 years ago · Jan 24, 2018, 04:34 PM
1#include <cstdio>
2#include <string>
3#include <map>
4
5#include "TileAssembler.h"
6
7using namespace::VMAP;
8
9typedef std::map<std::pair<uint32, uint32>, ModelSpawn> ModelSpawnMap;
10
11void extract_info(ModelSpawnMap const &spawnMap)
12{
13 FILE *sql = fopen("model_spawns.sql", "w");
14 if (!sql)
15 return;
16
17 fprintf(sql,
18 "DROP TABLE IF EXISTS model_spawns;\n"
19 "CREATE TABLE model_spawns (\n"
20 "\tmap int(10) unsigned default 0,\n"
21 "\tid int(10) unsigned default 0,\n"
22 "\tpos_x float default 0,\n"
23 "\tpos_y float default 0,\n"
24 "\tpos_z float default 0,\n"
25 "\trot_x float default 0,\n"
26 "\trot_y float default 0,\n"
27 "\trot_z float default 0,\n"
28 "\tscale float default 0,\n"
29 "\tfilename varchar(255) default NULL,\n"
30 "\tPRIMARY KEY (map, id)\n"
31 ") ENGINE=MyISAM DEFAULT CHARSET=utf8;\n\n"
32 );
33
34 int n=0;
35 fprintf(sql, "INSERT INTO model_spawns VALUES\n");
36 ModelSpawnMap::const_iterator i=spawnMap.begin();
37 while (i != spawnMap.end())
38 {
39 // prevent queries from getting too big
40 if (n>10)
41 {
42 fprintf(sql, "INSERT INTO model_spawns VALUES\n");
43 n=0;
44 }
45 const ModelSpawn &ms = i->second;
46 fprintf(sql, "(%u, %u, %f, %f, %f, %f, %f, %f, %f, '%s')",
47 i->first.first, i->first.second, ms.iPos.x, ms.iPos.y, ms.iPos.z,
48 ms.iRot.x, ms.iRot.y, ms.iRot.z, ms.iScale, ms.name.c_str());
49 ++n;
50 ++i;
51 if (i != spawnMap.end() && n <= 10)
52 fprintf(sql, ",\n");
53 else
54 fprintf(sql, ";\n");
55 }
56 fclose(sql);
57}
58
59
60int main(int argc, char* argv[])
61{
62 if (argc < 2)
63 {
64 printf("need to specify patch to extracted files (where dir_bin resides)");
65 return 1;
66 }
67
68 std::string fname = std::string(argv[1]) + "/dir_bin";
69 FILE *dirf = fopen(fname.c_str(), "rb");
70 if (!dirf)
71 {
72 printf("Could not read dir_bin file!\nCheck path for correctness.\n");
73 return false;
74 }
75 printf("Read coordinate mapping...\n");
76 ModelSpawnMap spawnMap;
77 uint32 mapID, tileX, tileY, check=0;
78 G3D::Vector3 v1, v2;
79 ModelSpawn spawn;
80 while (!feof(dirf))
81 {
82 check = 0;
83 // read mapID, tileX, tileY, Flags, adtID, ID, Pos, Rot, Scale, Bound_lo, Bound_hi, name
84 check += fread(&mapID, sizeof(uint32), 1, dirf);
85 if (check == 0) // EoF...
86 break;
87 check += fread(&tileX, sizeof(uint32), 1, dirf);
88 check += fread(&tileY, sizeof(uint32), 1, dirf);
89 if (!ModelSpawn::readFromFile(dirf, spawn))
90 break;
91
92 ModelSpawnMap::iterator iter = spawnMap.find(std::pair<uint32, uint32>(mapID,spawn.ID));
93 if (iter == spawnMap.end())
94 {
95 spawnMap[std::pair<uint32, uint32>(mapID,spawn.ID)] = spawn;
96 }
97 }
98 bool success = (ferror(dirf) == 0);
99 fclose(dirf);
100
101 if (!success)
102 printf("waring: reading dir_bin ended unexpected, information possibly incomplete.");
103
104 extract_info(spawnMap);
105 return success;
106}