]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content/content.cpp
Deprecate game.conf name, use title instead (#12030)
[dragonfireclient.git] / src / content / content.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 rubenwardy <rw@rubenwardy.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <fstream>
21 #include "content/content.h"
22 #include "content/subgames.h"
23 #include "content/mods.h"
24 #include "filesys.h"
25 #include "settings.h"
26
27 enum ContentType
28 {
29         ECT_UNKNOWN,
30         ECT_MOD,
31         ECT_MODPACK,
32         ECT_GAME,
33         ECT_TXP
34 };
35
36 ContentType getContentType(const ContentSpec &spec)
37 {
38         std::ifstream modpack_is((spec.path + DIR_DELIM + "modpack.txt").c_str());
39         if (modpack_is.good()) {
40                 modpack_is.close();
41                 return ECT_MODPACK;
42         }
43
44         std::ifstream modpack2_is((spec.path + DIR_DELIM + "modpack.conf").c_str());
45         if (modpack2_is.good()) {
46                 modpack2_is.close();
47                 return ECT_MODPACK;
48         }
49
50         std::ifstream init_is((spec.path + DIR_DELIM + "init.lua").c_str());
51         if (init_is.good()) {
52                 init_is.close();
53                 return ECT_MOD;
54         }
55
56         std::ifstream game_is((spec.path + DIR_DELIM + "game.conf").c_str());
57         if (game_is.good()) {
58                 game_is.close();
59                 return ECT_GAME;
60         }
61
62         std::ifstream txp_is((spec.path + DIR_DELIM + "texture_pack.conf").c_str());
63         if (txp_is.good()) {
64                 txp_is.close();
65                 return ECT_TXP;
66         }
67
68         return ECT_UNKNOWN;
69 }
70
71 void parseContentInfo(ContentSpec &spec)
72 {
73         std::string conf_path;
74
75         switch (getContentType(spec)) {
76         case ECT_MOD:
77                 spec.type = "mod";
78                 conf_path = spec.path + DIR_DELIM + "mod.conf";
79                 break;
80         case ECT_MODPACK:
81                 spec.type = "modpack";
82                 conf_path = spec.path + DIR_DELIM + "modpack.conf";
83                 break;
84         case ECT_GAME:
85                 spec.type = "game";
86                 conf_path = spec.path + DIR_DELIM + "game.conf";
87                 break;
88         case ECT_TXP:
89                 spec.type = "txp";
90                 conf_path = spec.path + DIR_DELIM + "texture_pack.conf";
91                 break;
92         default:
93                 spec.type = "unknown";
94                 break;
95         }
96
97         Settings conf;
98         if (!conf_path.empty() && conf.readConfigFile(conf_path.c_str())) {
99                 if (conf.exists("title"))
100                         spec.title = conf.get("title");
101                 else if (spec.type == "game" && conf.exists("name"))
102                         spec.title = conf.get("name");
103
104                 if (spec.type != "game" && conf.exists("name"))
105                         spec.name = conf.get("name");
106
107                 if (conf.exists("description"))
108                         spec.desc = conf.get("description");
109
110                 if (conf.exists("author"))
111                         spec.author = conf.get("author");
112
113                 if (conf.exists("release"))
114                         spec.release = conf.getS32("release");
115         }
116
117         if (spec.desc.empty()) {
118                 std::ifstream is((spec.path + DIR_DELIM + "description.txt").c_str());
119                 spec.desc = std::string((std::istreambuf_iterator<char>(is)),
120                                 std::istreambuf_iterator<char>());
121         }
122 }