comparison utils/dat_extract/main.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2007 by the FIFE Team *
3 * fife-public@lists.sourceforge.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <cassert>
24 #include <iostream>
25 #include <string>
26
27 // Platform dependent includes
28 #include <iostream>
29 #ifndef __WIN32
30 #include <libgen.h>
31 #else
32 #include <getopt.h>
33 #endif
34
35 // 3rd party library includes
36
37 // FIFE includes
38 // These includes are split up in two parts, separated by one empty line
39 // First block: files included from the FIFE root src directory
40 // Second block: files included from the same folder
41 #include "loaders/fallout/vfs_loaders/dat1.h"
42 #include "loaders/fallout/vfs_loaders/dat2.h"
43 #include "vfs/raw/rawdata.h"
44 #include "vfs/vfshostsystem.h"
45 #include "vfs/vfssourcefactory.h"
46 #include "controller/engine.h"
47 #include "util/base/exception.h"
48 #include "util/settingsmanager.h"
49
50 using namespace FIFE;
51
52 extern int print_usage(const char* prg_name, int exit_code);
53
54 int main(int argc, char* argv[]) {
55 Engine* engine = NULL;
56 VFS* vfs = NULL;
57 RawDataPtr rd;
58 std::string src_file("critter.dat");
59 std::string target_file("");
60 int fo_format = 1;
61 int c;
62
63 while ((c = getopt (argc, argv, "f:s:t:h")) != -1)
64 switch(c) {
65 case 'f':
66 fo_format = atoi(optarg);
67 break;
68 case 's':
69 src_file = std::string(optarg);
70 break;
71 case 't':
72 target_file = std::string(optarg);
73 break;
74 case 'h':
75 print_usage(argv[0], 0);
76 }
77
78 char *argv_fake[1] = {argv[0]};
79 try {
80 engine = new Engine(true);
81 }
82
83 catch (const Exception& exception) {
84 std::cerr << "Error: could not start engine: " << exception.getMessage() << std::endl;
85 }
86
87 if (src_file.length() == 0) {
88 std::cout << "Invalid DAT source file";
89 print_usage(argv[0], 1);
90 }
91
92 if (target_file.length() == 0) {
93 std::cout << "Invalid target file to extract";
94 print_usage(argv[0], 1);
95 }
96
97 if (!(fo_format == 1 || fo_format == 2)) {
98 std::cout << "Unknown format " << fo_format;
99 print_usage(argv[0], 1);
100 }
101
102 try {
103 VFSSourceFactory::instance();
104 vfs = VFS::instance();
105 vfs->addSource(new VFSHostSystem());
106
107 if (vfs->exists(src_file)) {
108 if (fo_format == 1) {
109 vfs->addSource(new map::loaders::fallout::DAT1(src_file));
110 } else {
111 vfs->addSource(new map::loaders::fallout::DAT2(src_file));
112 }
113 } else {
114 throw NotFound(src_file);
115 }
116
117 if (!vfs->exists(target_file)) {
118 throw NotFound(target_file);
119 }
120
121 rd = vfs->open(target_file);
122
123 int len = rd->getDataLength();
124 uint8_t * buf = new uint8_t[len];
125 rd->readInto(buf, len);
126
127 std::string target_filename(target_file);
128 if (target_file.find("/") != std::string::npos) {
129 target_filename.assign(target_file, target_file.rfind("/") + 1,
130 target_file.length() - target_file.rfind("/") - 1);
131 }
132
133 FILE* out_fd = fopen(target_filename.c_str(), "wb");
134 assert(out_fd);
135 fwrite(buf, sizeof(uint8_t), len, out_fd);
136 fclose(out_fd);
137
138 delete buf;
139 }
140
141 catch (const Exception& exception) {
142 std::cerr << "exception: " << exception.getMessage() << std::endl;
143 return 1;
144 }
145
146 if (engine) {
147 delete engine;
148 }
149 }
150 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */