Debian Codegraph @

prboom2/src/dsda/data_organizer.c

raw
1 //
2 // Copyright(C) 2021 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA Data Organizer
16 //
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22
23 #include "doomtype.h"
24 #include "lprintf.h"
25 #include "m_file.h"
26 #include "w_wad.h"
27 #include "i_system.h"
28 #include "z_zone.h"
29 #include "e6y.h"
30
31 #include "dsda/args.h"
32 #include "dsda/utility.h"
33
34 #include "data_organizer.h"
35
36 #define DATA_DIR_LIMIT 9
37 static const char* dsda_data_root = "dsda_doom_data";
38 static char* dsda_data_dir_strings[DATA_DIR_LIMIT];
39 static char* dsda_base_data_dir;
40 static char* dsda_wad_data_dir;
41
42 char* dsda_DetectDirectory(const char* env_key, int arg_id) {
43 dsda_arg_t* arg;
44 char* result = NULL;
45 const char* default_directory;
46
47 default_directory = M_getenv(env_key);
48
49 if (!default_directory)
50 default_directory = I_ConfigDir();
51
52 arg = dsda_Arg(arg_id);
53 if (arg->found) {
54 if (M_IsDir(arg->value.v_string)) {
55 if (result) Z_Free(result);
56 result = Z_Strdup(arg->value.v_string);
57 }
58 else
59 lprintf(LO_ERROR, "Error: path %s does not exist. Using %s\n",
60 arg->value.v_string, default_directory);
61 }
62
63 if (!result)
64 result = Z_Strdup(default_directory);
65
66 return result;
67 }
68
69 void dsda_InitDataDir(void) {
70 char* parent_directory;
71 dsda_string_t str;
72
73 parent_directory = dsda_DetectDirectory("DOOMDATADIR", dsda_arg_data);
74
75 dsda_StringPrintF(&str, "%s/%s", parent_directory, dsda_data_root);
76
77 dsda_base_data_dir = str.string;
78 M_MakeDir(dsda_base_data_dir, false);
79
80 Z_Free(parent_directory);
81 }
82
83 static void dsda_InitWadDataDir(void) {
84 int i;
85 const int iwad_index = 0;
86 int pwad_index = 1;
87 dsda_string_t str;
88
89 for (i = 0; i < numwadfiles; ++i) {
90 const char* start;
91 char* result;
92 int length;
93
94 start = PathFindFileName(wadfiles[i].name);
95
96 length = strlen(start) - 4;
97
98 if (length > 0 && !strcasecmp(start + length, ".wad")) {
99 int dir_index;
100
101 if (wadfiles[i].src == 0)
102 dir_index = iwad_index;
103 else if (wadfiles[i].src == 3)
104 dir_index = pwad_index;
105 else
106 dir_index = -1;
107
108 if (dir_index >= 0 && dir_index < DATA_DIR_LIMIT) {
109 dsda_data_dir_strings[dir_index] = Z_Malloc(length + 1);
110 strncpy(dsda_data_dir_strings[dir_index], start, length);
111 dsda_data_dir_strings[dir_index][length] = '\0';
112
113 for (result = dsda_data_dir_strings[dir_index]; *result; ++result)
114 *result = tolower(*result);
115
116 if (dir_index == pwad_index)
117 pwad_index++;
118 }
119 }
120 }
121
122 dsda_InitString(&str, dsda_base_data_dir);
123
124 for (i = 0; i < DATA_DIR_LIMIT; ++i)
125 if (dsda_data_dir_strings[i]) {
126 dsda_StringCatF(&str, "/%s", dsda_data_dir_strings[i]);
127 M_MakeDir(str.string, false);
128 }
129
130 dsda_wad_data_dir = str.string;
131
132 lprintf(LO_INFO, "Using data file directory: %s\n", dsda_wad_data_dir);
133 }
134
135 char* dsda_DataDir(void) {
136 if (!dsda_wad_data_dir)
137 dsda_InitWadDataDir();
138
139 return dsda_wad_data_dir;
140 }
141
142 const char* dsda_DataRoot(void) {
143 return dsda_base_data_dir;
144 }