File: path_info.cpp

package info (click to toggle)
cataclysm-dda 0.C+git20190228.faafa3a-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 181,636 kB
  • sloc: cpp: 256,609; python: 2,621; makefile: 862; sh: 495; perl: 37; xml: 33
file content (275 lines) | stat: -rw-r--r-- 12,350 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "path_info.h"

#include <clocale>
#include <cstdlib>

#include "filesystem.h"
#include "options.h"
#include "translations.h"

#if (defined _WIN32 || defined WINDOW)
#include <windows.h>
#endif

/** Map where we store filenames */
std::map<std::string, std::string> FILENAMES;

void PATH_INFO::init_base_path( std::string path )
{
    if( !path.empty() ) {
        char ch = path.at( path.length() - 1 );
        if( ch != '/' && ch != '\\' ) {
            path.push_back( '/' );
        }
    }

    //FILENAMES.insert(std::pair<std::string,std::string>("base_path", path));
    FILENAMES["base_path"] = path;
}

void PATH_INFO::init_user_dir( const char *ud )
{
    std::string dir = std::string( ud );

    if( dir.empty() ) {
        const char *user_dir;
#if (defined _WIN32 || defined WINDOW)
        user_dir = getenv( "LOCALAPPDATA" );
        // On Windows userdir without dot
        dir = std::string( user_dir ) + "/cataclysm-dda/";
#elif defined MACOSX
        user_dir = getenv( "HOME" );
        dir = std::string( user_dir ) + "/Library/Application Support/Cataclysm/";
#elif (defined USE_XDG_DIR)
        if( ( user_dir = getenv( "XDG_DATA_HOME" ) ) ) {
            dir = std::string( user_dir ) + "/cataclysm-dda/";
        } else {
            user_dir = getenv( "HOME" );
            dir = std::string( user_dir ) + "/.local/share/cataclysm-dda/";
        }
#else
        user_dir = getenv( "HOME" );
        dir = std::string( user_dir ) + "/.cataclysm-dda/";
#endif
    }

    FILENAMES["user_dir"] = dir;
}

void PATH_INFO::update_pathname( const std::string &name, const std::string &path )
{
    std::map<std::string, std::string>::iterator iter = FILENAMES.find( name );
    if( iter != FILENAMES.end() ) {
        FILENAMES[name] = path;
    } else {
        FILENAMES.insert( std::pair<std::string, std::string>( name, path ) );
    }
}

void PATH_INFO::update_datadir()
{
    // Shared dirs
    update_pathname( "gfxdir", FILENAMES["datadir"] + "gfx/" );
    update_pathname( "luadir", FILENAMES["datadir"] + "lua/" );
    update_pathname( "fontdir", FILENAMES["datadir"] + "font/" );
    update_pathname( "rawdir", FILENAMES["datadir"] + "raw/" );
    update_pathname( "jsondir", FILENAMES["datadir"] + "core/" );
    update_pathname( "moddir", FILENAMES["datadir"] + "mods/" );
    update_pathname( "recycledir", FILENAMES["datadir"] + "recycling/" );
    update_pathname( "namesdir", FILENAMES["datadir"] + "names/" );
    update_pathname( "titledir", FILENAMES["datadir"] + "title/" );
    update_pathname( "motddir", FILENAMES["datadir"] + "motd/" );
    update_pathname( "creditsdir", FILENAMES["datadir"] + "credits/" );
    update_pathname( "data_sound", FILENAMES["datadir"] + "sound" );
    update_pathname( "helpdir", FILENAMES["datadir"] + "help/" );

    // Shared files
    update_pathname( "autoexeclua", FILENAMES["luadir"] + "autoexec.lua" );
    update_pathname( "class_defslua", FILENAMES["luadir"] + "class_definitions.lua" );
    update_pathname( "title", FILENAMES["titledir"] + "en.title" );
    update_pathname( "halloween", FILENAMES["titledir"] + "en.halloween" );
    update_pathname( "motd", FILENAMES["motddir"] + "en.motd" );
    update_pathname( "credits", FILENAMES["creditsdir"] + "en.credits" );
    update_pathname( "names", FILENAMES["namesdir"] + "en.json" );
    update_pathname( "colors", FILENAMES["rawdir"] + "colors.json" );
    update_pathname( "keybindings", FILENAMES["rawdir"] + "keybindings.json" );
    update_pathname( "keybindings_vehicle", FILENAMES["rawdir"] + "keybindings/vehicle.json" );
    update_pathname( "legacy_fontdata", FILENAMES["datadir"] + "fontdata.json" );
    update_pathname( "sokoban", FILENAMES["rawdir"] + "sokoban.txt" );
    update_pathname( "defaulttilejson", FILENAMES["gfx"] + "tile_config.json" );
    update_pathname( "defaulttilepng", FILENAMES["gfx"] + "tinytile.png" );
    update_pathname( "mods-dev-default", FILENAMES["moddir"] + "default.json" );
    update_pathname( "mods-replacements", FILENAMES["moddir"] + "replacements.json" );
    update_pathname( "defaultsounddir", FILENAMES["datadir"] + "sound" );
    update_pathname( "help", FILENAMES["helpdir"] + "texts.json" );
}

void PATH_INFO::update_config_dir()
{
    update_pathname( "options", FILENAMES["config_dir"] + "options.json" );
    update_pathname( "keymap", FILENAMES["config_dir"] + "keymap.txt" );
    update_pathname( "debug", FILENAMES["config_dir"] + "debug.log" );
    update_pathname( "crash", FILENAMES["config_dir"] + "crash.log" );
    update_pathname( "fontlist", FILENAMES["config_dir"] + "fontlist.txt" );
    update_pathname( "fontdata", FILENAMES["config_dir"] + "fonts.json" );
    update_pathname( "autopickup", FILENAMES["config_dir"] + "auto_pickup.json" );
    update_pathname( "safemode", FILENAMES["config_dir"] + "safemode.json" );
    update_pathname( "base_colors", FILENAMES["config_dir"] + "base_colors.json" );
    update_pathname( "custom_colors", FILENAMES["config_dir"] + "custom_colors.json" );
    update_pathname( "mods-user-default", FILENAMES["config_dir"] + "user-default-mods.json" );
}

void PATH_INFO::set_standard_filenames()
{
    // Special: data_dir lua_dir and gfx_dir
    if( !FILENAMES["base_path"].empty() ) {
#ifdef DATA_DIR_PREFIX
        update_pathname( "datadir", FILENAMES["base_path"] + "share/games/cataclysm-dda/" );
        update_pathname( "gfxdir", FILENAMES["datadir"] + "gfx/" );
        update_pathname( "luadir", FILENAMES["datadir"] + "lua/" );
#else
        update_pathname( "datadir", FILENAMES["base_path"] + "data/" );
        update_pathname( "gfxdir", FILENAMES["base_path"] + "gfx/" );
        update_pathname( "luadir", FILENAMES["base_path"] + "lua/" );
#endif
    } else {
        update_pathname( "datadir", "data/" );
        update_pathname( "gfxdir", "gfx/" );
        update_pathname( "luadir", "lua/" );
    }

    // Shared dirs
    update_pathname( "autoexeclua", FILENAMES["luadir"] + "autoexec.lua" );
    update_pathname( "class_defslua", FILENAMES["luadir"] + "class_definitions.lua" );
    update_pathname( "fontdir", FILENAMES["datadir"] + "font/" );
    update_pathname( "rawdir", FILENAMES["datadir"] + "raw/" );
    update_pathname( "jsondir", FILENAMES["datadir"] + "core/" );
    update_pathname( "moddir", FILENAMES["datadir"] + "mods/" );
    update_pathname( "namesdir", FILENAMES["datadir"] + "names/" );
    update_pathname( "titledir", FILENAMES["datadir"] + "title/" );
    update_pathname( "motddir", FILENAMES["datadir"] + "motd/" );
    update_pathname( "creditsdir", FILENAMES["datadir"] + "credits/" );
    update_pathname( "color_templates", FILENAMES["rawdir"] + "color_templates/" );
    update_pathname( "data_sound", FILENAMES["datadir"] + "sound" );
    update_pathname( "helpdir", FILENAMES["datadir"] + "help/" );

    // Shared files
    update_pathname( "title", FILENAMES["titledir"] + "en.title" );
    update_pathname( "halloween", FILENAMES["titledir"] + "en.halloween" );
    update_pathname( "motd", FILENAMES["motddir"] + "en.motd" );
    update_pathname( "credits", FILENAMES["creditsdir"] + "en.credits" );
    update_pathname( "names", FILENAMES["namesdir"] + "en.json" );
    update_pathname( "colors", FILENAMES["rawdir"] + "colors.json" );
    update_pathname( "keybindings", FILENAMES["rawdir"] + "keybindings.json" );
    update_pathname( "keybindings_vehicle", FILENAMES["rawdir"] + "keybindings/vehicle.json" );
    update_pathname( "sokoban", FILENAMES["rawdir"] + "sokoban.txt" );
    update_pathname( "defaulttilejson", FILENAMES["gfx"] + "tile_config.json" );
    update_pathname( "defaulttilepng", FILENAMES["gfx"] + "tinytile.png" );
    update_pathname( "mods-dev-default", FILENAMES["moddir"] + "default.json" );
    update_pathname( "mods-replacements", FILENAMES["moddir"] + "replacements.json" );
    update_pathname( "defaultsounddir", FILENAMES["datadir"] + "sound" );
    update_pathname( "help", FILENAMES["helpdir"] + "texts.json" );

    update_pathname( "savedir", FILENAMES["user_dir"] + "save/" );
    update_pathname( "memorialdir", FILENAMES["user_dir"] + "memorial/" );
    update_pathname( "templatedir", FILENAMES["user_dir"] + "templates/" );
    update_pathname( "user_sound", FILENAMES["user_dir"] + "sound/" );
#ifdef USE_XDG_DIR
    const char *user_dir;
    std::string dir;
    if( ( user_dir = getenv( "XDG_CONFIG_HOME" ) ) ) {
        dir = std::string( user_dir ) + "/cataclysm-dda/";
    } else {
        user_dir = getenv( "HOME" );
        dir = std::string( user_dir ) + "/.config/cataclysm-dda/";
    }
    update_pathname( "config_dir", dir );
#else
    update_pathname( "config_dir", FILENAMES["user_dir"] + "config/" );
#endif
    update_pathname( "graveyarddir", FILENAMES["user_dir"] + "graveyard/" );

    update_pathname( "options", FILENAMES["config_dir"] + "options.json" );
    update_pathname( "keymap", FILENAMES["config_dir"] + "keymap.txt" );
    update_pathname( "user_keybindings", FILENAMES["config_dir"] + "keybindings.json" );
    update_pathname( "debug", FILENAMES["config_dir"] + "debug.log" );
    update_pathname( "crash", FILENAMES["config_dir"] + "crash.log" );
    update_pathname( "fontlist", FILENAMES["config_dir"] + "fontlist.txt" );
    update_pathname( "fontdata", FILENAMES["config_dir"] + "fonts.json" );
    update_pathname( "autopickup", FILENAMES["config_dir"] + "auto_pickup.json" );
    update_pathname( "safemode", FILENAMES["config_dir"] + "safemode.json" );
    update_pathname( "base_colors", FILENAMES["config_dir"] + "base_colors.json" );
    update_pathname( "custom_colors", FILENAMES["config_dir"] + "custom_colors.json" );
    update_pathname( "mods-user-default", FILENAMES["config_dir"] + "user-default-mods.json" );
    update_pathname( "user_moddir", FILENAMES["user_dir"] + "mods/" );
    update_pathname( "worldoptions", "worldoptions.json" );

    // Needed to move files from these legacy locations to the new config directory.
    update_pathname( "legacy_options", "data/options.txt" );
    update_pathname( "legacy_options2", FILENAMES["config_dir"] + "options.txt" );
    update_pathname( "legacy_keymap", "data/keymap.txt" );
    update_pathname( "legacy_autopickup", "data/auto_pickup.txt" );
    update_pathname( "legacy_autopickup2", FILENAMES["config_dir"] + "auto_pickup.txt" );
    update_pathname( "legacy_fontdata", FILENAMES["datadir"] + "fontdata.json" );
    update_pathname( "legacy_worldoptions", "worldoptions.txt" );
#ifdef TILES
    // Default tileset config file.
    update_pathname( "tileset-conf", "tileset.txt" );
#endif
#ifdef SDL_SOUND
    // Default soundpack config file.
    update_pathname( "soundpack-conf", "soundpack.txt" );
#endif
}

std::string PATH_INFO::find_translated_file( const std::string &pathid,
        const std::string &extension, const std::string &fallbackid )
{
    const std::string base_path = FILENAMES[pathid];

#if defined LOCALIZE && ! defined __CYGWIN__
    std::string loc_name;
    if( get_option<std::string>( "USE_LANG" ).empty() ) {
#if (defined _WIN32 || defined WINDOWS)
        loc_name = getLangFromLCID( GetUserDefaultLCID() );
        if( !loc_name.empty() ) {
            const std::string local_path = base_path + loc_name + extension;
            if( file_exist( local_path ) ) {
                return local_path;
            }
        }
#endif

        const char *v = setlocale( LC_ALL, NULL );
        if( v != nullptr ) {
            loc_name = v;
        }
    } else {
        loc_name = get_option<std::string>( "USE_LANG" );
    }
    if( loc_name == "C" ) {
        loc_name = "en";
    }
    if( !loc_name.empty() ) {
        const size_t dotpos = loc_name.find( '.' );
        if( dotpos != std::string::npos ) {
            loc_name.erase( dotpos );
        }
        // complete locale: en_NZ
        const std::string local_path = base_path + loc_name + extension;
        if( file_exist( local_path ) ) {
            return local_path;
        }
        const size_t p = loc_name.find( '_' );
        if( p != std::string::npos ) {
            // only the first part: en
            const std::string local_path = base_path + loc_name.substr( 0, p ) + extension;
            if( file_exist( local_path ) ) {
                return local_path;
            }
        }
    }
#endif
    ( void ) extension;
    return FILENAMES[fallbackid];
}