git repos / blockattack-game

blame: source/code/ModInfo.cpp

normal view · raw

3affd2d6 Poul Sander 2020-12-27 15:56 1
/*
3affd2d6 Poul Sander 2020-12-27 15:56 2
===========================================================================
3affd2d6 Poul Sander 2020-12-27 15:56 3
blockattack - Block Attack - Rise of the Blocks
3affd2d6 Poul Sander 2020-12-27 15:56 4
Copyright (C) 2005-2020 Poul Sander
3affd2d6 Poul Sander 2020-12-27 15:56 5
3affd2d6 Poul Sander 2020-12-27 15:56 6
This program is free software: you can redistribute it and/or modify
3affd2d6 Poul Sander 2020-12-27 15:56 7
it under the terms of the GNU General Public License as published by
3affd2d6 Poul Sander 2020-12-27 15:56 8
the Free Software Foundation, either version 2 of the License, or
3affd2d6 Poul Sander 2020-12-27 15:56 9
(at your option) any later version.
3affd2d6 Poul Sander 2020-12-27 15:56 10
3affd2d6 Poul Sander 2020-12-27 15:56 11
This program is distributed in the hope that it will be useful,
3affd2d6 Poul Sander 2020-12-27 15:56 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
3affd2d6 Poul Sander 2020-12-27 15:56 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3affd2d6 Poul Sander 2020-12-27 15:56 14
GNU General Public License for more details.
3affd2d6 Poul Sander 2020-12-27 15:56 15
3affd2d6 Poul Sander 2020-12-27 15:56 16
You should have received a copy of the GNU General Public License
3affd2d6 Poul Sander 2020-12-27 15:56 17
along with this program.  If not, see http://www.gnu.org/licenses/
3affd2d6 Poul Sander 2020-12-27 15:56 18
3affd2d6 Poul Sander 2020-12-27 15:56 19
Source information and contacts persons can be found at
3affd2d6 Poul Sander 2020-12-27 15:56 20
http://blockattack.net
3affd2d6 Poul Sander 2020-12-27 15:56 21
===========================================================================
3affd2d6 Poul Sander 2020-12-27 15:56 22
*/
3affd2d6 Poul Sander 2020-12-27 15:56 23
3affd2d6 Poul Sander 2020-12-27 15:56 24
#include "ModInfo.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 25
#include "sago/SagoMisc.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 26
#include "rapidjson/document.h"
3affd2d6 Poul Sander 2020-12-27 15:56 27
#include <iostream>
c93baeed Poul Sander 2021-10-10 14:09 28
#include <sstream>
3affd2d6 Poul Sander 2020-12-27 15:56 29
#include <boost/algorithm/string.hpp>
3affd2d6 Poul Sander 2020-12-27 15:56 30
3affd2d6 Poul Sander 2020-12-27 15:56 31
void ModInfo::InitModList(const std::vector<std::string>& modlist) {
3affd2d6 Poul Sander 2020-12-27 15:56 32
	this->mod_list = modlist;
3affd2d6 Poul Sander 2020-12-27 15:56 33
	sprite_filename_list.clear();
3affd2d6 Poul Sander 2020-12-27 15:56 34
	for (const std::string& s : mod_list) {
3affd2d6 Poul Sander 2020-12-27 15:56 35
		std::string filename_info = std::string("modinfo/") + s + ".json";
3affd2d6 Poul Sander 2020-12-27 15:56 36
		if (sago::FileExists(filename_info.c_str())) {
3affd2d6 Poul Sander 2020-12-27 15:56 37
			std::string content = sago::GetFileContent(filename_info.c_str());
3affd2d6 Poul Sander 2020-12-27 15:56 38
			rapidjson::Document document;
3affd2d6 Poul Sander 2020-12-27 15:56 39
			document.Parse(content.c_str());
3affd2d6 Poul Sander 2020-12-27 15:56 40
			if ( !document.IsObject() ) {
3affd2d6 Poul Sander 2020-12-27 15:56 41
				std::cerr << "Failed to parse: " << filename_info << "\n";
3affd2d6 Poul Sander 2020-12-27 15:56 42
				continue;
3affd2d6 Poul Sander 2020-12-27 15:56 43
			}
3affd2d6 Poul Sander 2020-12-27 15:56 44
			const auto& t = document.GetObject().FindMember("sprites");
3affd2d6 Poul Sander 2020-12-27 15:56 45
			if (t != document.MemberEnd() && t->value.IsString()) {
3affd2d6 Poul Sander 2020-12-27 15:56 46
				std::string sprites = t->value.GetString();
3affd2d6 Poul Sander 2020-12-27 15:56 47
				std::vector<std::string> sprites_vector;
3affd2d6 Poul Sander 2020-12-27 15:56 48
				boost::split(sprites_vector, sprites, boost::is_any_of(","));
3affd2d6 Poul Sander 2020-12-27 15:56 49
				sprite_filename_list.insert(sprite_filename_list.end(), sprites_vector.begin(), sprites_vector.end());
3affd2d6 Poul Sander 2020-12-27 15:56 50
			}
3affd2d6 Poul Sander 2020-12-27 15:56 51
		}
3affd2d6 Poul Sander 2020-12-27 15:56 52
	}
3affd2d6 Poul Sander 2020-12-27 15:56 53
}
3affd2d6 Poul Sander 2020-12-27 15:56 54
c93baeed Poul Sander 2021-10-10 14:09 55
void ModInfo::ParseModFile(const std::string& content) {
c93baeed Poul Sander 2021-10-10 14:09 56
	std::istringstream ss(content);
c93baeed Poul Sander 2021-10-10 14:09 57
	std::string line;
c93baeed Poul Sander 2021-10-10 14:09 58
	while (std::getline(ss, line)) {
c93baeed Poul Sander 2021-10-10 14:09 59
		std::vector<std::string> line_vector;
c93baeed Poul Sander 2021-10-10 14:09 60
		boost::split(line_vector, line, boost::is_any_of(","));
c93baeed Poul Sander 2021-10-10 14:09 61
		if (line_vector.size() < 2) {
c93baeed Poul Sander 2021-10-10 14:09 62
			std::cerr << "skipping " << line << "\n";
c93baeed Poul Sander 2021-10-10 14:09 63
			continue;
c93baeed Poul Sander 2021-10-10 14:09 64
		}
c93baeed Poul Sander 2021-10-10 14:09 65
		if (line_vector[1] == "1") {
c93baeed Poul Sander 2021-10-10 14:09 66
			this->mod_list.push_back(line_vector[0]);
c93baeed Poul Sander 2021-10-10 14:09 67
		}
c93baeed Poul Sander 2021-10-10 14:09 68
	}
c93baeed Poul Sander 2021-10-10 14:09 69
	InitModList(this->mod_list);
c93baeed Poul Sander 2021-10-10 14:09 70
}
c93baeed Poul Sander 2021-10-10 14:09 71
3affd2d6 Poul Sander 2020-12-27 15:56 72
const std::vector<std::string>& ModInfo::getModList() {
3affd2d6 Poul Sander 2020-12-27 15:56 73
	return this->mod_list;
3affd2d6 Poul Sander 2020-12-27 15:56 74
}
3affd2d6 Poul Sander 2020-12-27 15:56 75
3affd2d6 Poul Sander 2020-12-27 15:56 76
const std::vector<std::string>& ModInfo::getModSpriteFiles() {
3affd2d6 Poul Sander 2020-12-27 15:56 77
	return this->sprite_filename_list;
3affd2d6 Poul Sander 2020-12-27 15:56 78
}
1970-01-01 00:00 79