forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_loader.h
87 lines (73 loc) · 3.03 KB
/
color_loader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#ifndef CATA_SRC_COLOR_LOADER_H
#define CATA_SRC_COLOR_LOADER_H
#include <array>
#include <map>
#include <string>
#include "debug.h"
#include "filesystem.h"
#include "json.h"
#include "path_info.h"
template<typename ColorType>
class color_loader
{
public:
static constexpr size_t COLOR_NAMES_COUNT = 16;
private:
static ColorType from_rgb( int r, int g, int b );
std::map<std::string, ColorType> consolecolors;
// color names as read from the json file
static const std::array<std::string, COLOR_NAMES_COUNT> &main_color_names() {
static const std::array<std::string, COLOR_NAMES_COUNT> names{ { "BLACK", "RED", "GREEN",
"BROWN", "BLUE", "MAGENTA", "CYAN", "GRAY", "DGRAY", "LRED", "LGREEN", "YELLOW",
"LBLUE", "LMAGENTA", "LCYAN", "WHITE"
} };
return names;
}
void load_colors( const JsonObject &jsobj ) {
for( size_t c = 0; c < main_color_names().size(); c++ ) {
const std::string &color = main_color_names()[c];
JsonArray jsarr = jsobj.get_array( color );
consolecolors[color] = from_rgb( jsarr.get_int( 0 ), jsarr.get_int( 1 ), jsarr.get_int( 2 ) );
}
}
ColorType ccolor( const std::string &color ) const {
const auto it = consolecolors.find( color );
if( it == consolecolors.end() ) {
throw std::runtime_error( std::string( "requested non-existing color " ) + color );
}
return it->second;
}
void load_colorfile( const std::string &path ) {
cata::ifstream colorfile( fs::u8path( path ), std::ifstream::in | std::ifstream::binary );
JsonIn jsin( colorfile );
jsin.start_array();
while( !jsin.end_array() ) {
JsonObject jo = jsin.get_object();
// This isn't actually read (here), so just ignore it
jo.get_string( "type" );
load_colors( jo );
jo.finish();
}
}
public:
/// @throws std::exception upon any kind of error.
void load( std::array<ColorType, COLOR_NAMES_COUNT> &windowsPalette ) {
const std::string default_path = PATH_INFO::colors();
const std::string custom_path = PATH_INFO::base_colors();
if( !file_exist( custom_path ) ) {
copy_file( default_path, custom_path );
}
try {
load_colorfile( custom_path );
} catch( const JsonError &err ) {
debugmsg( "Failed to load color data from \"%s\": %s", custom_path, err.what() );
// this should succeed, otherwise the installation is botched
load_colorfile( default_path );
}
for( size_t c = 0; c < main_color_names().size(); c++ ) {
windowsPalette[c] = ccolor( main_color_names()[c] );
}
}
};
#endif // CATA_SRC_COLOR_LOADER_H