77
88#include " JPAK.h"
99
10+ /* Extensions to help */
11+ /* I got this from http://stackoverflow.com/a/236803 */
12+ vector<string> &split (const string &s, char delim, vector<string> &elems) {
13+ std::stringstream ss (s);
14+ std::string item;
15+ while (std::getline (ss, item, delim)) {
16+ elems.push_back (item);
17+ }
18+ return elems;
19+ }
20+
21+
22+ vector<string> split (const string &s, char delim) {
23+ vector<string> elems;
24+ split (s, delim, elems);
25+ return elems;
26+ }
27+
28+ /* JPAK Stuff */
1029JPAK::JPAK () {
1130 ready = false ;
1231}
@@ -17,7 +36,10 @@ JPAK::~JPAK() {
1736 root.clear ();
1837}
1938bool JPAK::ProcessTable (string &table) {
20- return reader.parse (table.c_str (), root);
39+ bool ok = reader.parse (table.c_str (), root);
40+ if (!ok)
41+ cout << " Failed to parse table: " << reader.getFormattedErrorMessages () << endl;
42+ return ok;
2143}
2244bool JPAK::LoadFromFile (string &filename) {
2345 char magic[5 ];
@@ -30,15 +52,16 @@ bool JPAK::LoadFromFile(string &filename) {
3052 jpakfile.seekg (0 ,ios_base::end);
3153 fsize = jpakfile.tellg ();
3254 jpakfile.seekg (-4 ,ios_base::end);
33- jpakfile >> tableoffset;
55+ jpakfile. read (( char *)& tableoffset, 4 ) ;
3456 jpakfile.seekg (tableoffset, ios_base::beg);
35- char table[fsize-tableoffset-4 ];
57+ char table[fsize-tableoffset-3 ];
3658 jpakfile.read (table, fsize-tableoffset-4 );
59+ table[fsize-tableoffset-4 ] = 0x00 ;
3760 string table_s = table;
3861 jpakfile.seekg (0 ,ios_base::beg);
3962 if (ProcessTable (table_s)) {
4063 ready = true ;
41- return false ;
64+ return true ;
4265 }else {
4366 cout << " Cannot parse JPAK Table!" << endl;
4467 return false ;
@@ -53,6 +76,56 @@ bool JPAK::LoadFromFile(string &filename) {
5376 }
5477}
5578
56- void JPAK::GetFile (string &path, char *buffer) {
79+ void JPAK::DecompressFile (char *in, char ** out) {
80+ // TODO: Zlib decompressor
81+ }
82+
83+ void JPAK::PrintTree (Json::Value &outroot) {
84+ Json::Value files = outroot[" files" ];
85+ Json::Value dirs = outroot[" directories" ];
86+ cout << " Files: " << endl;
87+ for (auto file: files) {
88+ cout << " - " << file[" name" ] << endl;
89+ }
5790
91+ cout << " Dirs: " << endl;
92+ for (auto dir: dirs) {
93+ cout << " - " << dir[" name" ] << endl;
94+ PrintTree (dir);
95+ }
96+ }
97+
98+ void JPAK::PrintTree () {
99+ PrintTree (root);
100+ }
101+
102+ Json::Value JPAK::FindFileEntry (vector<string> &path, Json::Value &root) {
103+ if (path.size () == 1 ) {
104+ return root[" files" ].get (path[0 ], JPAK_NOT_FOUND_DATA);
105+ }else {
106+ string nextdir = path[0 ];
107+ path.erase (path.begin ());
108+ if (root[" directories" ].get (nextdir, JPAK_NOT_FOUND_DATA) != " JPAK_NOT_FOUND_DATA" )
109+ return FindFileEntry (path, root[" directories" ][nextdir]);
110+ else
111+ return Json::Value (JPAK_NOT_FOUND_DATA);
112+ }
113+ }
114+ bool JPAK::GetFile (string &path, char **buffer, int *size) {
115+ vector<string> paths = split (path, ' /' );
116+ // Here is relative to root, so the first will be blank if starts with "/"
117+ if (paths[0 ].size () == 0 )
118+ paths.erase (paths.begin ());
119+
120+ Json::Value fileentry = FindFileEntry (paths, root);
121+ if (fileentry != JPAK_NOT_FOUND_DATA) {
122+ jpakfile.seekg (fileentry[" offset" ].asInt (), ios_base::beg);
123+ *buffer = new char [fileentry[" size" ].asInt ()];
124+ jpakfile.read (*buffer, fileentry[" size" ].asInt ());
125+ *size = fileentry[" size" ].asInt ();
126+ return true ;
127+ }else {
128+ cout << " File not found: " << path << endl;
129+ return false ;
130+ }
58131}
0 commit comments