git repos / blockattack-game

blame: source/code/HelpModInfoState.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
bfb4fed7 Poul Sander 2022-02-14 19:35 4
Copyright (C) 2005-2022 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
https://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
bfb4fed7 Poul Sander 2022-02-14 19:35 24
#include "DialogBox.hpp"
bfb4fed7 Poul Sander 2022-02-14 19:35 25
#include "HelpModInfoState.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 26
#include "global.hpp"
bfb4fed7 Poul Sander 2022-02-14 19:35 27
#include "common.h"
3affd2d6 Poul Sander 2020-12-27 15:56 28
#include "sago/SagoMisc.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 29
#include "os.hpp"
3affd2d6 Poul Sander 2020-12-27 15:56 30
#include <boost/algorithm/string/predicate.hpp>
bfb4fed7 Poul Sander 2022-02-14 19:35 31
#include <sstream>
3affd2d6 Poul Sander 2020-12-27 15:56 32
bfb4fed7 Poul Sander 2022-02-14 19:35 33
struct Mod {
bfb4fed7 Poul Sander 2022-02-14 19:35 34
	std::string name;
bfb4fed7 Poul Sander 2022-02-14 19:35 35
	std::string filename;
bfb4fed7 Poul Sander 2022-02-14 19:35 36
	bool enabled = false;
bfb4fed7 Poul Sander 2022-02-14 19:35 37
	int order = 0;
bfb4fed7 Poul Sander 2022-02-14 19:35 38
};
3affd2d6 Poul Sander 2020-12-27 15:56 39
bfb4fed7 Poul Sander 2022-02-14 19:35 40
static bool sort_mods_enabled_order (const Mod& i,const Mod& j) {
3affd2d6 Poul Sander 2020-12-27 15:56 41
	if (i.enabled && !j.enabled) {
3affd2d6 Poul Sander 2020-12-27 15:56 42
		//Enabled mods always goes before disabled ones
3affd2d6 Poul Sander 2020-12-27 15:56 43
		return true;
3affd2d6 Poul Sander 2020-12-27 15:56 44
	}
3affd2d6 Poul Sander 2020-12-27 15:56 45
	if (i.enabled) {
3affd2d6 Poul Sander 2020-12-27 15:56 46
		return i.order < j.order;
3affd2d6 Poul Sander 2020-12-27 15:56 47
	}
3affd2d6 Poul Sander 2020-12-27 15:56 48
	return i.name < j.name;
3affd2d6 Poul Sander 2020-12-27 15:56 49
}
3affd2d6 Poul Sander 2020-12-27 15:56 50
3affd2d6 Poul Sander 2020-12-27 15:56 51
static void initMods(std::vector<Mod>& mod_list) {
3affd2d6 Poul Sander 2020-12-27 15:56 52
	for (size_t i=0; i < globalData.modList.size(); ++i) {
3affd2d6 Poul Sander 2020-12-27 15:56 53
		for (Mod& m : mod_list) {
3affd2d6 Poul Sander 2020-12-27 15:56 54
			if (m.name == globalData.modList[i]) {
3affd2d6 Poul Sander 2020-12-27 15:56 55
				m.order = i;
3affd2d6 Poul Sander 2020-12-27 15:56 56
				m.enabled = true;
3affd2d6 Poul Sander 2020-12-27 15:56 57
			}
3affd2d6 Poul Sander 2020-12-27 15:56 58
		}
3affd2d6 Poul Sander 2020-12-27 15:56 59
	}
3affd2d6 Poul Sander 2020-12-27 15:56 60
	std::sort(mod_list.begin(), mod_list.end(), sort_mods_enabled_order);
5e83af06 Poul Sander 2022-02-17 16:17 61
	std::vector<Mod> mod_list_no_duplicates;
5e83af06 Poul Sander 2022-02-17 16:17 62
	std::string last_mod = "";
5e83af06 Poul Sander 2022-02-17 16:17 63
	for (const Mod& m : mod_list) {
5e83af06 Poul Sander 2022-02-17 16:17 64
		if (m.name != last_mod) {
5e83af06 Poul Sander 2022-02-17 16:17 65
			mod_list_no_duplicates.push_back(m);
5e83af06 Poul Sander 2022-02-17 16:17 66
		}
5e83af06 Poul Sander 2022-02-17 16:17 67
		last_mod = m.name;
5e83af06 Poul Sander 2022-02-17 16:17 68
	}
5e83af06 Poul Sander 2022-02-17 16:17 69
	std::swap(mod_list, mod_list_no_duplicates);
3affd2d6 Poul Sander 2020-12-27 15:56 70
}
3affd2d6 Poul Sander 2020-12-27 15:56 71
3affd2d6 Poul Sander 2020-12-27 15:56 72
static void appendMods(const std::vector<std::string>& mod_files, const std::string& dir, std::vector<Mod>& mods_available) {
3affd2d6 Poul Sander 2020-12-27 15:56 73
	for (const std::string& mod : mod_files) {
3affd2d6 Poul Sander 2020-12-27 15:56 74
		if (!boost::ends_with(mod, ".data")) {
3affd2d6 Poul Sander 2020-12-27 15:56 75
			continue;
3affd2d6 Poul Sander 2020-12-27 15:56 76
		}
3affd2d6 Poul Sander 2020-12-27 15:56 77
		Mod m;
3affd2d6 Poul Sander 2020-12-27 15:56 78
		m.name = mod.substr(0, mod.length()-5);
3affd2d6 Poul Sander 2020-12-27 15:56 79
		m.filename = dir + "/" + mod;
3affd2d6 Poul Sander 2020-12-27 15:56 80
		mods_available.push_back(m);
3affd2d6 Poul Sander 2020-12-27 15:56 81
	}
3affd2d6 Poul Sander 2020-12-27 15:56 82
}
3affd2d6 Poul Sander 2020-12-27 15:56 83
bfb4fed7 Poul Sander 2022-02-14 19:35 84
static std::vector<Mod> get_mods() {
bfb4fed7 Poul Sander 2022-02-14 19:35 85
	std::vector<Mod> mods_available;
3affd2d6 Poul Sander 2020-12-27 15:56 86
	std::string baseMods = std::string(PHYSFS_getBaseDir())+ "/mods";
3affd2d6 Poul Sander 2020-12-27 15:56 87
	std::vector<std::string> baseModFiles = OsGetDirFileList(baseMods);
3affd2d6 Poul Sander 2020-12-27 15:56 88
	appendMods(baseModFiles, baseMods, mods_available);
cb995956 Poul Sander 2022-01-15 14:06 89
	std::string sharedMods = std::string(SHAREDIR)+ "/mods";
cb995956 Poul Sander 2022-01-15 14:06 90
	std::vector<std::string> sharedModFiles = OsGetDirFileList(sharedMods);
cb995956 Poul Sander 2022-01-15 14:06 91
	appendMods(sharedModFiles, sharedMods, mods_available);
3affd2d6 Poul Sander 2020-12-27 15:56 92
	std::string userMods = getPathToSaveFiles()+"/mods";
3affd2d6 Poul Sander 2020-12-27 15:56 93
	std::vector<std::string> userModFiles = OsGetDirFileList(userMods);
3affd2d6 Poul Sander 2020-12-27 15:56 94
	appendMods(userModFiles, userMods, mods_available);
3affd2d6 Poul Sander 2020-12-27 15:56 95
	initMods(mods_available);
bfb4fed7 Poul Sander 2022-02-14 19:35 96
	return mods_available;
3affd2d6 Poul Sander 2020-12-27 15:56 97
}
3affd2d6 Poul Sander 2020-12-27 15:56 98
bfb4fed7 Poul Sander 2022-02-14 19:35 99
HelpModInfoState::HelpModInfoState() {
bfb4fed7 Poul Sander 2022-02-14 19:35 100
	std::vector<Mod> mods_available = get_mods();
bfb4fed7 Poul Sander 2022-02-14 19:35 101
	std::stringstream infoStream;
bfb4fed7 Poul Sander 2022-02-14 19:35 102
	infoStream << _("Load order:") << "\n";
bfb4fed7 Poul Sander 2022-02-14 19:35 103
	for (size_t i=0; i < mods_available.size(); ++i) {
bfb4fed7 Poul Sander 2022-02-14 19:35 104
		const Mod& mod = mods_available[i];
bfb4fed7 Poul Sander 2022-02-14 19:35 105
		if (mod.enabled) {
bfb4fed7 Poul Sander 2022-02-14 19:35 106
			infoStream << mod.order << " : " << mod.name << " ";
3affd2d6 Poul Sander 2020-12-27 15:56 107
		}
bfb4fed7 Poul Sander 2022-02-14 19:35 108
		else {
bfb4fed7 Poul Sander 2022-02-14 19:35 109
			infoStream << "- : " << mod.name << " " << _("(Disabled)");
3affd2d6 Poul Sander 2020-12-27 15:56 110
		}
bfb4fed7 Poul Sander 2022-02-14 19:35 111
		infoStream << "\n";
3affd2d6 Poul Sander 2020-12-27 15:56 112
	}
bfb4fed7 Poul Sander 2022-02-14 19:35 113
	setHelp30FontThinOutline(&globalData.spriteHolder->GetDataHolder(), titleField, _("Mod info"));
bfb4fed7 Poul Sander 2022-02-14 19:35 114
	setHelpBoxFont(&globalData.spriteHolder->GetDataHolder(), infoBox, infoStream.str().c_str());
3affd2d6 Poul Sander 2020-12-27 15:56 115
}
bfb4fed7 Poul Sander 2022-02-14 19:35 116
bfb4fed7 Poul Sander 2022-02-14 19:35 117
HelpModInfoState::~HelpModInfoState() {
bfb4fed7 Poul Sander 2022-02-14 19:35 118
}