ManaPlus
browserboxtools.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "utils/browserboxtools.h"
23 
24 #include "itemcolormanager.h"
25 #include "main.h"
26 #include "settings.h"
27 
28 #include "input/inputmanager.h"
29 
30 #include "utils/stringutils.h"
31 
32 #ifndef DYECMD
34 
35 #include "resources/beinginfo.h"
36 #include "resources/iteminfo.h"
37 
39 #include "resources/db/itemdb.h"
41 #include "resources/db/monsterdb.h"
42 #include "resources/db/questdb.h"
43 #include "resources/db/petdb.h"
44 #endif // DYECMD
45 
46 #include "debug.h"
47 
49 {
50  data = replaceAll(data, "%VER%", SMALL_VERSION);
51  data = replaceAll(data, "%SUPPORTURL%", settings.supportUrl);
52 }
53 
55 {
56  size_t idx1 = data.find("###");
57  while (idx1 != std::string::npos)
58  {
59  const size_t idx2 = data.find(';', idx1);
60  if (idx2 == std::string::npos)
61  break;
62 
63  const std::string str = inputManager.getKeyValueByNameLong(
64  data.substr(idx1 + 3, idx2 - idx1 - 3));
65  data.replace(idx1, idx2 - idx1 + 1, str);
66 
67  idx1 = data.find("###");
68  }
69 }
70 
71 std::string BrowserBoxTools::replaceLinkCommands(const std::string &link)
72 {
73 #ifdef DYECMD
74  return link;
75 #else // DYECMD
76 
77  std::string data = link;
78 
79  if (strStartWith(data, "http://") ||
80  strStartWith(data, "https://"))
81  {
82  return data;
83  }
84 
85  if (!link.empty() && link[0] == 'm')
86  { // monster link
87  const BeingTypeId id = static_cast<BeingTypeId>(
88  atoi(link.substr(1).c_str()));
89  BeingInfo *const info = MonsterDB::get(id);
90  if (info != nullptr)
91  data = info->getName();
92  }
93  else if (!link.empty() && link[0] == 'p')
94  { // pet link
95  const BeingTypeId id = static_cast<BeingTypeId>(
96  atoi(link.substr(1).c_str()));
97  BeingInfo *const info = PETDB::get(id);
98  if (info != nullptr)
99  data = info->getName();
100  }
101  else if (!link.empty() && link[0] == 'h')
102  { // homunculus link
103  const BeingTypeId id = static_cast<BeingTypeId>(
104  atoi(link.substr(1).c_str()));
105  BeingInfo *const info = HomunculusDB::get(id);
106  if (info != nullptr)
107  data = info->getName();
108  }
109  else if (!link.empty() && link[0] == 'M')
110  { // mercenary link
111  const BeingTypeId id = static_cast<BeingTypeId>(
112  atoi(link.substr(1).c_str()));
113  BeingInfo *const info = MercenaryDB::get(id);
114  if (info != nullptr)
115  data = info->getName();
116  }
117  else if (!link.empty() && link[0] == 'q')
118  { // quest link
120  atoi(link.substr(1).c_str()));
121  }
122  else
123  { // item link
124  size_t idx = link.find(',');
125  if (idx != std::string::npos)
126  {
127  const int id = atoi(link.substr(0, idx).c_str());
128  if (id != 0)
129  {
130  STD_VECTOR<int> parts;
131  splitToIntVector(parts,
132  link.substr(idx), ',');
133  while (parts.size() < maxCards)
134  parts.push_back(0);
135  const ItemColor itemColor =
137  data = ItemDB::get(id).getName(itemColor);
138  }
139  }
140  else
141  {
142  const int id = atoi(link.c_str());
143  if (id != 0)
144  data = ItemDB::get(id).getName();
145  }
146  }
147  return data;
148 #endif // DYECMD
149 }
150 
152 {
153  size_t idx1 = data.find("\\t");
154  while (idx1 != std::string::npos)
155  {
156  const size_t idx2 = data.find(';', idx1);
157  if (idx2 == std::string::npos)
158  break;
159 
160  const unsigned int newSize = CAST_U32(
161  atoi(data.substr(
162  idx1 + 2, idx2 - idx1 - 2).c_str()));
163  std::string str = data.substr(0, idx1);
164  while (str.size() < newSize)
165  str.append(" ");
166  str.append(data.substr(idx2 + 1));
167  data = str;
168  idx1 = data.find("\\t");
169  }
170 }
int BeingTypeId
Definition: beingtypeid.h:30
#define maxCards
Definition: cards.h:25
#define CAST_U32
Definition: cast.h:31
std::string getKeyValueByNameLong(const std::string &keyName)
static ItemColor getColorFromCards(const int *const cards)
const std::string & getName() const
Definition: iteminfo.h:74
std::string supportUrl
Definition: settings.h:121
InputManager inputManager
uint16_t ItemColor
Definition: itemcolor.h:30
#define SMALL_VERSION
Definition: main.h:44
uint32_t data
bool info(InputEvent &event)
Definition: commands.cpp:57
void replaceKeys(std::string &data)
void replaceVars(std::string &data)
std::string replaceLinkCommands(const std::string &link)
void replaceTabs(std::string &data)
BeingInfo * get(const BeingTypeId id)
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
BeingInfo * get(const BeingTypeId id)
BeingInfo * get(const BeingTypeId id)
Definition: monsterdb.cpp:145
BeingInfo * get(const BeingTypeId id)
Definition: petdb.cpp:157
std::string getName(const int id)
Definition: questdb.cpp:249
Settings settings
Definition: settings.cpp:32
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
void splitToIntVector(std::vector< int > &tokens, const std::string &text, const char separator)
bool strStartWith(const std::string &str1, const std::string &str2)