ManaPlus
palettedb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-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 "resources/db/palettedb.h"
23 
24 #include "configuration.h"
25 
26 #include "fs/virtfs/tools.h"
27 
28 #include "utils/checkutils.h"
29 
30 #include "resources/dye/dyecolor.h"
31 
32 #include "debug.h"
33 
34 namespace
35 {
36  bool mLoaded = false;
37  std::map<std::string, DyeColor> mColors;
38 } // namespace
39 
41 {
42  if (mLoaded)
43  unload();
44 
45  logger->log1("Initializing palette database...");
46  loadPalette();
47 }
48 
50 {
51  mLoaded = true;
52  StringVect lines;
54  paths.getStringValue("defaultPaletteFile"),
55  lines);
56  StringVectCIter it = lines.begin();
57  if (it == lines.end())
58  {
59  logger->log("missing GIMP palette file");
60  return;
61  }
62  if (*it != "GIMP Palette")
63  {
64  reportAlways("wrong GIMP palette file")
65  return;
66  }
67  ++ it;
68  // skip header
69  while (it != lines.end())
70  {
71  const std::string line = *it;
72  if (!line.empty() && line[0] == '#')
73  break;
74  ++ it;
75  }
76 
77  char name[101];
78 
79  // process colors and ignore commets
80  while (it != lines.end())
81  {
82  const std::string line = *it;
83  ++ it;
84 
85  if (line.empty() || line[0] == '#')
86  continue;
87 
88  unsigned int r;
89  unsigned int g;
90  unsigned int b;
91 
92  if (sscanf(line.c_str(), "%10u %10u %10u\t%100s",
93  &r, &g, &b, name) == 4)
94  {
95  name[100] = 0;
96  mColors[name] = DyeColor(CAST_U8(r),
97  CAST_U8(g),
98  CAST_U8(b));
99  }
100  }
101 }
102 
104 {
105  logger->log1("Unloading palette database...");
106  mColors.clear();
107 }
108 
109 const DyeColor *PaletteDB::getColor(const std::string &name)
110 {
111  const std::map<std::string, DyeColor>::const_iterator it =
112  mColors.find(name);
113  if (it != mColors.end())
114  return &(*it).second;
115  return nullptr;
116 }
#define CAST_U8
Definition: cast.h:27
#define reportAlways(...)
Definition: checkutils.h:253
std::string getStringValue(const std::string &key) const
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: net.cpp:180
void loadPalette()
Definition: palettedb.cpp:49
void unload()
Definition: palettedb.cpp:103
const DyeColor * getColor(const std::string &name)
Definition: palettedb.cpp:109
void load()
Definition: palettedb.cpp:40
bool loadTextFile(const std::string &fileName, StringVect &lines)
Definition: tools.cpp:137
std::map< std::string, DyeColor > mColors
Definition: palettedb.cpp:37
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29