Debian Codegraph @

source/code/ModInfo.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2020 Poul Sander
5
6 This program 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, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 http://blockattack.net
21 ===========================================================================
22 */
23
24 #include "ModInfo.hpp"
25 #include "sago/SagoMisc.hpp"
26 #include "rapidjson/document.h"
27 #include <iostream>
28 #include <sstream>
29 #include <boost/algorithm/string.hpp>
30
31 void ModInfo::InitModList(const std::vector<std::string>& modlist) {
32 this->mod_list = modlist;
33 sprite_filename_list.clear();
34 for (const std::string& s : mod_list) {
35 std::string filename_info = std::string("modinfo/") + s + ".json";
36 if (sago::FileExists(filename_info.c_str())) {
37 std::string content = sago::GetFileContent(filename_info.c_str());
38 rapidjson::Document document;
39 document.Parse(content.c_str());
40 if ( !document.IsObject() ) {
41 std::cerr << "Failed to parse: " << filename_info << "\n";
42 continue;
43 }
44 const auto& t = document.GetObject().FindMember("sprites");
45 if (t != document.MemberEnd() && t->value.IsString()) {
46 std::string sprites = t->value.GetString();
47 std::vector<std::string> sprites_vector;
48 boost::split(sprites_vector, sprites, boost::is_any_of(","));
49 sprite_filename_list.insert(sprite_filename_list.end(), sprites_vector.begin(), sprites_vector.end());
50 }
51 }
52 }
53 }
54
55 void ModInfo::ParseModFile(const std::string& content) {
56 std::istringstream ss(content);
57 std::string line;
58 while (std::getline(ss, line)) {
59 std::vector<std::string> line_vector;
60 boost::split(line_vector, line, boost::is_any_of(","));
61 if (line_vector.size() < 2) {
62 std::cerr << "skipping " << line << "\n";
63 continue;
64 }
65 if (line_vector[1] == "1") {
66 this->mod_list.push_back(line_vector[0]);
67 }
68 }
69 InitModList(this->mod_list);
70 }
71
72 const std::vector<std::string>& ModInfo::getModList() {
73 return this->mod_list;
74 }
75
76 const std::vector<std::string>& ModInfo::getModSpriteFiles() {
77 return this->sprite_filename_list;
78 }