OSDN Git Service

macOS: fix mistakes in merge
[hengbandforosx/hengbandosx.git] / src / cmd-io / cmd-process-screen.cpp
1 /*!
2  * @brief 記念撮影のセーブとロード
3  * @date 2020/04/22
4  * @Author Hourier
5  */
6
7 #include "cmd-io/cmd-process-screen.h"
8 #include "cmd-visual/cmd-draw.h"
9 #include "core/asking-player.h"
10 #include "core/stuff-handler.h"
11 #include "core/visuals-reseter.h"
12 #include "game-option/special-options.h"
13 #include "io/files-util.h"
14 #include "io/input-key-acceptor.h"
15 #include "system/angband-exceptions.h"
16 #include "system/player-type-definition.h"
17 #include "system/redrawing-flags-updater.h"
18 #include "term/gameterm.h"
19 #include "term/screen-processor.h"
20 #include "term/term-color-types.h"
21 #include "util/angband-files.h"
22 #include "view/display-messages.h"
23 #include <optional>
24 #include <string>
25 #include <string_view>
26
27 // Encode the screen colors
28 static char hack[17] = "dwsorgbuDWvyRGBU";
29
30 static concptr tags[4] = {
31     "HEADER_START:",
32     "HEADER_END:",
33     "FOOTER_START:",
34     "FOOTER_END:",
35 };
36 static concptr html_head[3] = {
37     "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
38     "<pre>",
39     0,
40 };
41 static concptr html_foot[3] = {
42     "</pre>\n",
43     "</body>\n</html>\n",
44     0,
45 };
46
47 /*!
48  * @brief 一時ファイルを読み込み、ファイルに書き出す
49  * @param fff ファイルへの参照ポインタ
50  * @param tempfff 一時ファイルへの参照ポインタ
51  * @param num_tag タグ番号
52  * @todo io/ 以下に移したいところだが、このファイルの行数も大したことがないので一旦保留
53  */
54 static void read_temporary_file(FILE *fff, FILE *tmpfff, int num_tag)
55 {
56     bool is_first_line = true;
57     int next_tag = num_tag + 1;
58     char buf[2048]{};
59     while (!angband_fgets(tmpfff, buf, sizeof(buf))) {
60         if (is_first_line) {
61             if (strncmp(buf, tags[num_tag], strlen(tags[num_tag])) == 0) {
62                 is_first_line = false;
63             }
64
65             continue;
66         }
67
68         if (strncmp(buf, tags[next_tag], strlen(tags[next_tag])) == 0) {
69             break;
70         }
71
72         fprintf(fff, "%s\n", buf);
73     }
74 }
75
76 /*!
77  * @brief 記念撮影を1行ダンプする
78  * @param wid 幅
79  * @param y 現在の行位置
80  * @param fff 記念撮影ファイルへの参照ポインタ
81  */
82 static void screen_dump_one_line(int wid, int y, FILE *fff)
83 {
84     TERM_COLOR a = 0, old_a = 0;
85     char c = ' ';
86     for (TERM_LEN x = 0; x < wid - 1; x++) {
87         concptr cc = nullptr;
88         (void)(term_what(x, y, &a, &c));
89         switch (c) {
90         case '&':
91             cc = "&amp;";
92             break;
93         case '<':
94             cc = "&lt;";
95             break;
96         case '>':
97             cc = "&gt;";
98             break;
99 #ifdef WINDOWS
100         case 0x1f:
101             c = '.';
102             break;
103         case 0x7f:
104             c = (a == 0x09) ? '%' : '#';
105             break;
106 #endif
107         }
108
109         a = a & 0x0F;
110         if ((y == 0 && x == 0) || a != old_a) {
111             int rv = angband_color_table[a][1];
112             int gv = angband_color_table[a][2];
113             int bv = angband_color_table[a][3];
114             fprintf(fff, "%s<font color=\"#%02x%02x%02x\">",
115                 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
116             old_a = a;
117         }
118
119         if (cc) {
120             fprintf(fff, "%s", cc);
121         } else {
122             fprintf(fff, "%c", c);
123         }
124     }
125 }
126
127 /*!
128  * @brief 記念撮影を行方向にスイープする
129  * @param wid 幅
130  * @param hgt 高さ
131  * @param fff 記念撮影ファイルへの参照ポインタ
132  */
133 static void screen_dump_lines(int wid, int hgt, FILE *fff)
134 {
135     for (TERM_LEN y = 0; y < hgt; y++) {
136         if (y != 0) {
137             fprintf(fff, "\n");
138         }
139
140         screen_dump_one_line(wid, y, fff);
141     }
142 }
143
144 /*!
145  * @brief ファイルへ書き込めない場合にエラーを表示する
146  * @param fff ダンプファイルへの参照ポインタ
147  * @param path 保存先HTMLファイルのパス
148  * @param need_message メッセージ表示の必要性
149  * @return メッセージを表示して後続処理を実行するか否か
150  */
151 static bool check_screen_html_can_open(FILE *fff, const std::filesystem::path &path, bool need_message)
152 {
153     if (fff) {
154         return true;
155     }
156
157     if (!need_message) {
158         return false;
159     }
160
161     const auto &path_str = path.string();
162     const auto mes = format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), path_str.data());
163     THROW_EXCEPTION(std::runtime_error, mes);
164 }
165
166 /*!
167  * @brief HTMLヘッダを書き込む
168  * @param tmpfff 一時ファイルへの参照ポインタ
169  * @param fff 記念撮影ファイルへの参照ポインタ
170  */
171 static void write_html_header(FILE *tmpfff, FILE *fff)
172 {
173     if (tmpfff) {
174         read_temporary_file(fff, tmpfff, 0);
175         return;
176     }
177
178     for (int i = 0; html_head[i]; i++) {
179         fputs(html_head[i], fff);
180     }
181 }
182
183 /*!
184  * @brief HTMLフッタを書き込む
185  * @param tmpfff 一時ファイルへの参照ポインタ
186  * @param fff 記念撮影ファイルへの参照ポインタ
187  */
188 static void write_html_footer(FILE *tmpfff, FILE *fff)
189 {
190     fprintf(fff, "</font>");
191     if (!tmpfff) {
192         for (int i = 0; html_foot[i]; i++) {
193             fputs(html_foot[i], fff);
194         }
195     } else {
196         rewind(tmpfff);
197         read_temporary_file(fff, tmpfff, 2);
198         angband_fclose(tmpfff);
199     }
200
201     fprintf(fff, "\n");
202 }
203
204 void exe_cmd_save_screen_html(const std::filesystem::path &path, bool need_message)
205 {
206     TERM_LEN wid, hgt;
207     term_get_size(&wid, &hgt);
208     auto *fff = angband_fopen(path, FileOpenMode::WRITE, false, FileOpenType::HTML);
209     if (!check_screen_html_can_open(fff, path, need_message)) {
210         return;
211     }
212
213     if (need_message) {
214         screen_save();
215     }
216
217     const auto &path_prf = path_build(ANGBAND_DIR_USER, "htmldump.prf");
218     auto *tmpfff = angband_fopen(path_prf, FileOpenMode::READ);
219     write_html_header(tmpfff, fff);
220     screen_dump_lines(wid, hgt, fff);
221     write_html_footer(tmpfff, fff);
222     angband_fclose(fff);
223     if (!need_message) {
224         return;
225     }
226
227     msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
228     msg_print(nullptr);
229     screen_load();
230 }
231
232 /*!
233  * @brief HTML方式で記念撮影する / Save a screen dump to a file
234  * @param なし
235  */
236 static void exe_cmd_save_screen_html_with_naming()
237 {
238     char tmp[256] = "screen.html";
239     if (!get_string(_("ファイル名: ", "File name: "), tmp, 80)) {
240         return;
241     }
242
243     auto path = path_build(ANGBAND_DIR_USER, tmp);
244     msg_print(nullptr);
245     exe_cmd_save_screen_html(path, true);
246 }
247
248 /*!
249  * @brief 記念撮影の方式を問い合わせる
250  * @param html_dump HTMLダンプするか否か
251  * @return ダンプするならTRUE、キャンセルならFALSE
252  */
253 static bool ask_html_dump(bool *html_dump)
254 {
255     while (true) {
256         char c = inkey();
257         if (c == 'Y' || c == 'y') {
258             *html_dump = false;
259             return true;
260         }
261
262         if (c == 'H' || c == 'h') {
263             *html_dump = true;
264             return true;
265         }
266
267         prt("", 0, 0);
268         return false;
269     }
270
271     // コンパイル警告対応.
272     return false;
273 }
274
275 /*!
276  * @brief ファイルへ書き込めない場合にエラーを表示する
277  * @param fff ダンプファイルへの参照ポインタ
278  * @return ファイルへ書き込めるならTRUE、書き込めないならFALSE
279  */
280 static bool check_screen_text_can_open(FILE *fff, const std::string_view filename)
281 {
282     if (fff) {
283         return true;
284     }
285
286     msg_format(_("ファイル %s を開けませんでした。", "Failed to open file %s."), filename.data());
287     msg_print(nullptr);
288     return false;
289 }
290
291 /*!
292  * @brief テキスト方式で記念撮影する
293  * @param wid 幅
294  * @param hgt 高さ
295  * @return 記念撮影に成功したらTRUE、ファイルが開けなかったらFALSE
296  * @todo どこかバグっていて、(恐らく初期化されていない)変な文字列まで出力される
297  */
298 static bool do_cmd_save_screen_text(int wid, int hgt)
299 {
300     TERM_COLOR a = 0;
301     auto c = ' ';
302     const auto &path = path_build(ANGBAND_DIR_USER, "dump.txt");
303     auto *fff = angband_fopen(path, FileOpenMode::WRITE);
304     if (!check_screen_text_can_open(fff, path.string())) {
305         return false;
306     }
307
308     screen_save();
309     for (TERM_LEN y = 0; y < hgt; y++) {
310         TERM_LEN x;
311         char buf[1024]{};
312         for (x = 0; x < wid - 1; x++) {
313             (void)(term_what(x, y, &a, &c));
314             buf[x] = c;
315         }
316
317         buf[x] = '\0';
318         fprintf(fff, "%s\n", buf);
319     }
320
321     fprintf(fff, "\n");
322     for (TERM_LEN y = 0; y < hgt; y++) {
323         TERM_LEN x;
324         char buf[1024]{};
325         for (x = 0; x < wid - 1; x++) {
326             (void)(term_what(x, y, &a, &c));
327             buf[x] = hack[a & 0x0F];
328         }
329
330         buf[x] = '\0';
331         fprintf(fff, "%s\n", buf);
332     }
333
334     fprintf(fff, "\n");
335     angband_fclose(fff);
336     msg_print(_("画面(記念撮影)をファイルに書き出しました。", "Screen dump saved."));
337     msg_print(nullptr);
338     screen_load();
339     return true;
340 }
341
342 /*!
343  * @brief 記念撮影のためにグラフィック使用をOFFにする
344  * @param player_ptr プレイヤーへの参照ポインタ
345  * @return 記念撮影直前のグラフィックオプション
346  */
347 static bool update_use_graphics(PlayerType *player_ptr)
348 {
349     if (!use_graphics) {
350         return true;
351     }
352
353     use_graphics = false;
354     reset_visuals(player_ptr);
355     static constexpr auto flags = {
356         MainWindowRedrawingFlag::WIPE,
357         MainWindowRedrawingFlag::BASIC,
358         MainWindowRedrawingFlag::EXTRA,
359         MainWindowRedrawingFlag::MAP,
360         MainWindowRedrawingFlag::EQUIPPY,
361     };
362     RedrawingFlagsUpdater::get_instance().set_flags(flags);
363     handle_stuff(player_ptr);
364     return false;
365 }
366
367 /*
368  * Save a screen dump to a file
369  * @param player_ptr プレイヤーへの参照ポインタ
370  */
371 void do_cmd_save_screen(PlayerType *player_ptr)
372 {
373     prt(_("記念撮影しますか? [(y)es/(h)tml/(n)o] ", "Save screen dump? [(y)es/(h)tml/(n)o] "), 0, 0);
374     bool html_dump;
375     if (!ask_html_dump(&html_dump)) {
376         return;
377     }
378
379     int wid, hgt;
380     term_get_size(&wid, &hgt);
381
382     bool old_use_graphics = update_use_graphics(player_ptr);
383
384     if (html_dump) {
385         exe_cmd_save_screen_html_with_naming();
386         do_cmd_redraw(player_ptr);
387     } else if (!do_cmd_save_screen_text(wid, hgt)) {
388         return;
389     }
390
391     if (old_use_graphics) {
392         return;
393     }
394
395     use_graphics = true;
396     reset_visuals(player_ptr);
397     static constexpr auto flags = {
398         MainWindowRedrawingFlag::WIPE,
399         MainWindowRedrawingFlag::BASIC,
400         MainWindowRedrawingFlag::EXTRA,
401         MainWindowRedrawingFlag::MAP,
402         MainWindowRedrawingFlag::EQUIPPY,
403     };
404     RedrawingFlagsUpdater::get_instance().set_flags(flags);
405     handle_stuff(player_ptr);
406 }
407
408 /*!
409  * @brief 白文字だけ画面に描画する
410  * @param buf 描画用バッファ
411  * @param fff 記念撮影ファイルへの参照ポインタ
412  * @param wid 幅
413  * @param hgt 高さ
414  * @todo 目的は不明瞭
415  * @return ファイルが読み込めなくなったらFALSEで抜ける
416  */
417 static bool draw_white_characters(FILE *fff, int wid, int hgt)
418 {
419     bool okay = true;
420     for (TERM_LEN y = 0; okay; y++) {
421         char buf[1024]{};
422         if (!fgets(buf, sizeof(buf), fff)) {
423             okay = false;
424         }
425
426         if (buf[0] == '\n' || buf[0] == '\0') {
427             break;
428         }
429         if (y >= hgt) {
430             continue;
431         }
432
433         for (TERM_LEN x = 0; x < wid - 1; x++) {
434             if (buf[x] == '\n' || buf[x] == '\0') {
435                 break;
436             }
437
438             term_draw(x, y, TERM_WHITE, buf[x]);
439         }
440     }
441
442     return okay;
443 }
444
445 /*!
446  * @brief 白以外の文字を画面に描画する
447  * @param fff 記念撮影ファイルへの参照ポインタ
448  * @param wid 幅
449  * @param hgt 高さ
450  * @param 白文字が途中で読み込めなくなっていたらTRUE
451  * @todo 目的は不明瞭
452  */
453 static void draw_colored_characters(FILE *fff, int wid, int hgt, bool okay)
454 {
455     TERM_COLOR a = TERM_DARK;
456     auto c = ' ';
457     for (TERM_LEN y = 0; okay; y++) {
458         char buf[1024]{};
459         if (!fgets(buf, sizeof(buf), fff)) {
460             okay = false;
461         }
462
463         if (buf[0] == '\n' || buf[0] == '\0') {
464             break;
465         }
466         if (y >= hgt) {
467             continue;
468         }
469
470         for (TERM_LEN x = 0; x < wid - 1; x++) {
471             if (buf[x] == '\n' || buf[x] == '\0') {
472                 break;
473             }
474
475             (void)(term_what(x, y, &a, &c));
476             for (int i = 0; i < 16; i++) {
477                 if (hack[i] == buf[x]) {
478                     a = (byte)i;
479                 }
480             }
481
482             term_draw(x, y, a, c);
483         }
484     }
485 }
486
487 /*
488  * @brief Load a screen dump from a file
489  * @param なし
490  */
491 void do_cmd_load_screen(void)
492 {
493     TERM_LEN wid, hgt;
494     term_get_size(&wid, &hgt);
495     const auto path = path_build(ANGBAND_DIR_USER, "dump.txt");
496     auto *fff = angband_fopen(path, FileOpenMode::READ);
497     if (!fff) {
498         const auto filename = path.string();
499         msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), filename.data());
500         msg_print(nullptr);
501         return;
502     }
503
504     screen_save();
505     term_clear();
506     bool okay = draw_white_characters(fff, wid, hgt);
507     draw_colored_characters(fff, wid, hgt, okay);
508
509     angband_fclose(fff);
510     prt(_("ファイルに書き出された画面(記念撮影)をロードしました。", "Screen dump loaded."), 0, 0);
511     flush();
512     inkey();
513     screen_load();
514 }