Debian Codegraph @

prboom2/src/dsda/text_file.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 Text File
16 //
17
18 #include "doomstat.h"
19 #include "lprintf.h"
20 #include "m_file.h"
21 #include "e6y.h"
22
23 #include "dsda.h"
24 #include "dsda/analysis.h"
25 #include "dsda/args.h"
26 #include "dsda/configuration.h"
27 #include "dsda/mapinfo.h"
28 #include "dsda/playback.h"
29
30 #include "text_file.h"
31
32 extern int dsda_last_leveltime, dsda_last_gamemap, dsda_startmap;
33
34 static char* dsda_TextFileName(void) {
35 int name_length;
36 char* name;
37 char* playdemo;
38 const char* playback_name;
39
40 playback_name = dsda_PlaybackName();
41
42 if (!playback_name)
43 return NULL;
44
45 playdemo = Z_Strdup(playback_name);
46 name_length = strlen(playdemo);
47
48 if (name_length > 4 && !stricmp(playdemo + name_length - 4, ".lmp")) {
49 name = Z_Strdup(playdemo);
50 name[name_length - 4] = '\0';
51 }
52 else {
53 name = Z_Calloc(name_length + 4, 1);
54 strcat(name, playdemo);
55 }
56
57 strcat(name, ".txt");
58
59 Z_Free(playdemo);
60
61 return name;
62 }
63
64 static int dsda_IL(void) {
65 extern int dsda_startmap;
66
67 return dsda_startmap == dsda_last_gamemap;
68 }
69
70 static const char* dsda_Movie(void) {
71 if (gamemode == commercial) {
72 if (dsda_startmap == 1 && dsda_last_gamemap == 10)
73 return "Episode 1";
74 if (dsda_startmap == 11 && dsda_last_gamemap == 20)
75 return "Episode 2";
76 if (dsda_startmap == 21 && dsda_last_gamemap == 30)
77 return "Episode 3";
78 if (dsda_startmap == 1 && dsda_last_gamemap == 30)
79 return "D2All";
80 }
81 else {
82 if (dsda_startmap == 1 && dsda_last_gamemap == 8) {
83 if (gameepisode == 1)
84 return "Episode 1";
85 if (gameepisode == 2)
86 return "Episode 2";
87 if (gameepisode == 3)
88 return "Episode 3";
89 if (gameepisode == 4)
90 return "Episode 4";
91 }
92 }
93
94 return NULL;
95 }
96
97 static char* dsda_TextFileTime(void) {
98 char* text_file_time;
99
100 text_file_time = Z_Malloc(16);
101
102 if (dsda_IL())
103 snprintf(
104 text_file_time,
105 16,
106 "%d:%05.2f",
107 dsda_last_leveltime / 35 / 60,
108 (float)(dsda_last_leveltime % (60 * 35)) / 35
109 );
110 else
111 snprintf(
112 text_file_time,
113 16,
114 "%d:%02d",
115 totalleveltimes / 35 / 60,
116 (totalleveltimes / 35) % 60
117 );
118
119 return text_file_time;
120 }
121
122 void dsda_ExportTextFile(void) {
123 dsda_arg_t* arg;
124 char* name;
125 const char* iwad = NULL;
126 const char* pwad = NULL;
127 const char* dsda_player_name;
128 FILE* file;
129
130 if (!dsda_Flag(dsda_arg_export_text_file))
131 return;
132
133 name = dsda_TextFileName();
134
135 if (!name)
136 return;
137
138 file = M_OpenFile(name, "wb");
139 Z_Free(name);
140
141 if (!file)
142 I_Error("Unable to export text file!");
143
144 arg = dsda_Arg(dsda_arg_iwad);
145 if (arg->found)
146 iwad = PathFindFileName(arg->value.v_string);
147
148 arg = dsda_Arg(dsda_arg_file);
149 if (arg->found)
150 pwad = PathFindFileName(arg->value.v_string_array[0]);
151
152 fprintf(file, "Doom Speed Demo Archive\n");
153 fprintf(file, "https://dsdarchive.com/\n");
154 fprintf(file, "\n");
155 if (iwad)
156 fprintf(file, "Iwad: %s\n", iwad);
157 if (pwad)
158 fprintf(file, "Pwad: %s\n", pwad);
159
160 if (dsda_IL())
161 fprintf(file, "Map: %s\n", dsda_MapLumpName(gameepisode, dsda_startmap));
162 else {
163 const char* movie;
164
165 movie = dsda_Movie();
166
167 if (movie)
168 fprintf(file, "Movie: %s\n", movie);
169 else {
170 fprintf(file, "Movie: %s", dsda_MapLumpName(gameepisode, dsda_startmap));
171 fprintf(file, " - %s\n", dsda_MapLumpName(gameepisode, dsda_last_gamemap));
172 }
173 }
174
175 fprintf(file, "Skill: %i\n", gameskill + 1);
176 fprintf(file, "Category: %s\n", dsda_DetectCategory());
177 fprintf(file, "Exe: %s -complevel %i\n", PROJECT_STRING, compatibility_level);
178 fprintf(file, "\n");
179
180 name = dsda_TextFileTime();
181 fprintf(file, "Time: %s\n", name);
182 Z_Free(name);
183
184 dsda_player_name = dsda_StringConfig(dsda_config_player_name);
185
186 fprintf(file, "\n");
187 fprintf(file, "Author: %s\n", dsda_player_name);
188 fprintf(file, "\n");
189 fprintf(file, "Comments:\n");
190
191 fclose(file);
192 }