Debian Codegraph @

source/code/HelpModInfoState.cpp

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