Add online content repository
[oweals/minetest.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 init_is((spec.path + DIR_DELIM + "init.lua").c_str());
45         if (init_is.good()) {
46                 init_is.close();
47                 return ECT_MOD;
48         }
49
50         std::ifstream game_is((spec.path + DIR_DELIM + "game.conf").c_str());
51         if (game_is.good()) {
52                 game_is.close();
53                 return ECT_GAME;
54         }
55
56         std::ifstream txp_is((spec.path + DIR_DELIM + "texture_pack.conf").c_str());
57         if (txp_is.good()) {
58                 txp_is.close();
59                 return ECT_TXP;
60         }
61
62         return ECT_UNKNOWN;
63 }
64
65 void parseContentInfo(ContentSpec &spec)
66 {
67         std::string conf_path;
68
69         switch (getContentType(spec)) {
70         case ECT_MOD:
71                 spec.type = "mod";
72                 conf_path = spec.path + DIR_DELIM + "mod.conf";
73                 break;
74         case ECT_MODPACK:
75                 spec.type = "modpack";
76                 conf_path = spec.path + DIR_DELIM + "mod.conf";
77                 break;
78         case ECT_GAME:
79                 spec.type = "game";
80                 conf_path = spec.path + DIR_DELIM + "game.conf";
81                 break;
82         case ECT_TXP:
83                 spec.type = "txp";
84                 conf_path = spec.path + DIR_DELIM + "texture_pack.conf";
85                 break;
86         default:
87                 spec.type = "unknown";
88                 break;
89         }
90
91         Settings conf;
92         if (!conf_path.empty() && conf.readConfigFile(conf_path.c_str())) {
93                 if (conf.exists("name"))
94                         spec.name = conf.get("name");
95
96                 if (conf.exists("description"))
97                         spec.desc = conf.get("description");
98
99                 if (conf.exists("author"))
100                         spec.author = conf.get("author");
101         }
102
103         if (spec.desc.empty()) {
104                 std::ifstream is((spec.path + DIR_DELIM + "description.txt").c_str());
105                 spec.desc = std::string((std::istreambuf_iterator<char>(is)),
106                                 std::istreambuf_iterator<char>());
107         }
108 }