1 // Copyright (C) 2011, 2014 Ben Asselstine
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17
18 #include <config.h>
19
20 #include <iostream>
21 #include "xmlhelper.h"
22 #include "tarhelper.h"
23 #include "File.h"
24 #include "profilelist.h"
25 #include "recently-played-game-list.h"
26 #include "gamelist.h"
27 #include "ucompose.hpp"
28 #include "file-compat.h"
29 #include "armyset.h"
30
31 int max_vector_width;
32
main(int argc,char * argv[])33 int main(int argc, char* argv[])
34 {
35 int err = EXIT_SUCCESS;
36 Glib::ustring filename;
37 Glib::ustring rewrite;
38 bool identify_file = false;
39 Vector<int>::setMaximumWidth(1000);
40
41 Gtk::Main kit(argc, argv);
42 if (argc > 1)
43 {
44 for (int i = 2; i <= argc; i++)
45 {
46 Glib::ustring parameter(argv[i-1]);
47 if (parameter == "--identify" || parameter == "-i")
48 {
49 identify_file = true;
50 }
51 else if (parameter == "--rewrite" || parameter == "-r")
52 {
53 rewrite = parameter;
54 }
55 else if (parameter == "--help" || parameter == "-?")
56 {
57 std::cout << File::get_basename(argv[0], true) << " [OPTION]... FILE" << std::endl << std::endl;
58 std::cout << "LordsAWar! File Upgrading Tool " << _("version") <<
59 " " << VERSION << std::endl << std::endl;
60 std::cout << _("Options:") << std::endl << std::endl;
61 std::cout << " -?, --help " << _("Display this help and exit") <<std::endl;
62 std::cout << " -i, --identify " << _("Show the file type instead of upgrading") << std::endl;
63 //std::cout << " -r, --rewrite VERSION " << _("Just change the version instead of upgrading") << std::endl;
64 std::cout << std::endl;
65 std::cout << _("Report bugs to") << " <" << PACKAGE_BUGREPORT ">." << std::endl;
66 exit(0);
67 }
68 else
69 filename = parameter;
70 }
71 }
72
73 bool same_version = false;
74 Armyset::support_backward_compatibility();
75 Profilelist::support_backward_compatibility();
76 RecentlyPlayedGameList::support_backward_compatibility();
77 Gamelist::support_backward_compatibility();
78 FileCompat::support_backward_compatibility_for_common_files();
79 if (identify_file == false && rewrite == "")
80 {
81 Glib::ustring ext = File::get_extension(filename);
82 Glib::ustring tmpfile = File::get_tmp_file(ext);
83 File::copy(filename, tmpfile);
84 bool upgraded = FileCompat::getInstance()->upgrade(tmpfile,
85 same_version);
86
87 if (same_version)
88 {
89 std::cout << String::ucompose(_("%1 is already the latest version."),
90 filename) << std::endl;
91
92 bool is_tar_file = false;
93 FileCompat::Type type =
94 FileCompat::getInstance()->getTypeByFileInspection(tmpfile,
95 is_tar_file);
96 if (type == FileCompat::GAMESCENARIO && is_tar_file)
97 {
98 bool armyset = false, tileset = false, cityset = false,
99 shieldset = false;
100 std::cout << _("Trying to upgrade the other files inside the tar file...") << std::endl;
101 upgraded = FileCompat::getInstance()->upgradeGameScenario
102 (tmpfile, LORDSAWAR_SAVEGAME_VERSION,
103 armyset, tileset, cityset, shieldset);
104 if (upgraded)
105 {
106 if (armyset)
107 std::cout << _("Armyset has been upgraded.") << std::endl;
108 if (tileset)
109 std::cout << _("Tileset has been upgraded.") << std::endl;
110 if (cityset)
111 std::cout << _("Cityset has been upgraded.") << std::endl;
112 if (shieldset)
113 std::cout << _("Shieldset has been upgraded.") << std::endl;
114 if (armyset || tileset || cityset || shieldset)
115 File::copy(tmpfile, filename);
116 if (!armyset && !tileset && !cityset && !shieldset)
117 std::cout << _("None of the other files needed to be upgraded.") << std::endl;
118 }
119 }
120 File::erase(tmpfile);
121 }
122 else if (!upgraded && !same_version)
123 {
124 std::cout << String::ucompose(_("Error: %1 could not be upgraded."),
125 filename) << std::endl;
126 File::erase(tmpfile);
127 }
128 else
129 {
130 File::copy(tmpfile, filename);
131 File::erase(tmpfile);
132 }
133 if (!upgraded)
134 err = EXIT_FAILURE;
135 }
136 else if (identify_file && rewrite == "")
137 {
138 Glib::ustring tag, version;
139 FileCompat::Type type = FileCompat::getInstance()->getType(filename);
140 FileCompat::getInstance()->get_tag_and_version_from_file(filename, type, tag, version);
141 std::cout << String::ucompose("%1 (%2 %3)",
142 FileCompat::typeToString(type),
143 tag, version) << std::endl;
144 }
145 else if (identify_file == false && rewrite != "")
146 {
147 FileCompat *fc = FileCompat::getInstance();
148 Glib::ustring tag, version;
149 FileCompat::Type type = fc->getType(filename);
150 if (fc->get_tag_and_version_from_file(filename, type, tag, version))
151 {
152 if (!fc->rewrite_with_updated_version(filename, type, tag, rewrite))
153 err = EXIT_FAILURE;
154 }
155 else
156 err = EXIT_FAILURE;
157 }
158 else if (identify_file && rewrite != "")
159 {
160 std::cout << _("Error: The --identify and --rewrite options cannot be used at the same time.") << std::endl;
161 err = EXIT_FAILURE;
162 }
163 return err;
164 }
165
166