OSDN Git Service

タイル表示でもスクリーンショットを送信できるようにした。
[hengbandforosx/hengbandosx.git] / src / report.c
1 /* File: report.c */
2
3 #define _GNU_SOURCE
4 #include "angband.h"
5
6 #ifdef WORLD_SCORE
7
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <ctype.h>
11 #include <string.h>
12
13 #if defined(WINDOWS)
14 #include <winsock.h>
15 #elif defined(MACINTOSH)
16 #include <OpenTransport.h>
17 #include <OpenTptInternet.h>
18 #else
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <sys/time.h>
24
25 #include <setjmp.h>
26 #include <signal.h>
27 #endif
28
29 #ifdef JP
30 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore/score.cgi"
31 #else
32 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/hengscore-en/score.cgi"
33 #endif
34
35 /* for debug */
36 #if 0
37 #define SCORE_PATH "http://www.kmc.gr.jp/~habu/local/scoretest/score.cgi"
38 #endif
39
40 /*
41  * simple buffer library
42  */
43
44 typedef struct {
45         size_t max_size;
46         size_t size;
47         char *data;
48 } BUF;
49
50 #define BUFSIZE (65536)
51
52 #ifndef HAVE_VASPRINTF
53 #define vasprintf       Vasprintf
54
55 static int Vasprintf(char **buf, const char *fmt, va_list ap)
56 {
57         int ret;
58
59         *buf = malloc(1024);
60
61 #if defined(HAVE_VSNPRINTF)
62         ret = vsnprintf(*buf, 1024, fmt, ap);
63 #else
64         ret = vsprintf(*buf, fmt, ap);
65 #endif
66         return ret;
67 }
68
69 #endif /* ifndef HAVE_VASPRINTF */ 
70
71 static BUF* buf_new(void)
72 {
73         BUF *p;
74
75         if ((p = malloc(sizeof(BUF))) == NULL)
76                 return NULL;
77
78         p->size = 0;
79         p->max_size = BUFSIZE;
80         if ((p->data = malloc(BUFSIZE)) == NULL)
81         {
82                 free(p);
83                 return NULL;
84         }
85         return p;
86 }
87
88 static void buf_delete(BUF *b)
89 {
90         free(b->data);
91         free(b);
92 }
93
94 static int buf_append(BUF *buf, const char *data, size_t size)
95 {
96         while (buf->size + size > buf->max_size)
97         {
98                 char *tmp;
99                 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
100
101                 memcpy(tmp, buf->data, buf->max_size);
102                 free(buf->data);
103
104                 buf->data = tmp;
105
106                 buf->max_size *= 2;
107         }
108         memcpy(buf->data + buf->size, data, size);
109         buf->size += size;
110
111         return buf->size;
112 }
113
114 static int buf_sprintf(BUF *buf, const char *fmt, ...)
115 {
116         int             ret;
117         char    *tmpbuf;
118         va_list ap;
119
120         va_start(ap, fmt);
121         vasprintf(&tmpbuf, fmt, ap);
122         va_end(ap);
123
124         if(!tmpbuf) return -1;
125
126 #ifdef MAC_MPW
127         {
128                 /* '\n' is 0x0D and '\r' is 0x0A in MPW. Swap back these. */
129                 char *ptr;
130                 for (ptr = tmpbuf; *ptr; ptr++)
131                         if ('\n' == *ptr) *ptr = '\r';
132         }
133 #endif
134
135         ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
136
137         free(tmpbuf);
138
139         return ret;
140 }
141
142 #if 0
143 static int buf_read(BUF *buf, int fd)
144 {
145         int len;
146 #ifndef MACINTOSH
147         char tmp[BUFSIZE];
148 #else
149         char *tmp;
150         
151         tmp = calloc( BUFSIZE , sizeof(char) );
152 #endif
153
154         while ((len = read(fd, tmp, BUFSIZE)) > 0)
155                 buf_append(buf, tmp, len);
156
157         return buf->size;
158 }
159 #endif
160
161 #if 0
162 static int buf_write(BUF *buf, int fd)
163 {
164         write(fd, buf->data, buf->size);
165
166         return buf->size;
167 }
168
169 static int buf_search(BUF *buf, const char *str)
170 {
171         char *ret;
172
173         ret = strstr(buf->data, str);
174
175         if (!ret) return -1;
176
177         return ret - buf->data;
178 }
179
180 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
181 {
182         BUF *ret;
183
184         if (pos1 < 0) return NULL;
185
186         ret = buf_new();
187
188         if (sz <= 0) sz = buf->size - pos1;
189
190         buf_append(ret, buf->data + pos1, sz);
191
192         return ret;
193 }
194 #endif
195
196 static void http_post(int sd, char *url, BUF *buf)
197 {
198         BUF *output;
199
200         output = buf_new();
201         buf_sprintf(output, "POST %s HTTP/1.0\n", url);
202         buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\n",
203                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
204
205         buf_sprintf(output, "Content-Length: %d\n", buf->size);
206         buf_sprintf(output, "Content-Encoding: binary\n");
207         buf_sprintf(output, "Content-Type: application/octet-stream\n");
208         buf_sprintf(output, "\n");
209         buf_append(output, buf->data, buf->size);
210
211         soc_write(sd, output->data, output->size);
212 }
213
214
215 /* ¥­¥ã¥é¥¯¥¿¥À¥ó¥×¤òºî¤Ã¤Æ BUF¤ËÊݸ */
216 static errr make_dump(BUF* dumpbuf)
217 {
218         errr make_character_dump(FILE *fff);
219
220         char            buf[1024];
221         FILE *fff;
222         char file_name[1024];
223
224         /* Open a new file */
225         fff = my_fopen_temp(file_name, 1024);
226         if (!fff)
227         {
228 #ifdef JP
229                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", file_name);
230 #else
231                 msg_format("Failed to create temporary file %s.", file_name);
232 #endif
233                 msg_print(NULL);
234                 return 1;
235         }
236
237         /* °ìö°ì»þ¥Õ¥¡¥¤¥ë¤òºî¤ë¡£Ä̾ï¤Î¥À¥ó¥×½ÐÎϤȶ¦Ä̲½¤¹¤ë¤¿¤á¡£ */
238         (void)make_character_dump(fff);
239
240         /* Close the file */
241         my_fclose(fff);
242
243         /* Open for read */
244         fff = my_fopen(file_name, "r");
245
246         while (fgets(buf, 1024, fff))
247         {
248                 (void)buf_sprintf(dumpbuf, "%s", buf);
249         }
250
251         /* Close the file */
252         my_fclose(fff);
253
254         /* Remove the file */
255         fd_kill(file_name);
256
257         /* Success */
258         return (0);
259 }
260
261 /*
262  * Make screen dump to buffer
263  */
264 cptr make_screen_dump(void)
265 {
266         BUF *screen_buf;
267         int y, x, i;
268         cptr ret;
269
270         byte a = 0, old_a = 0;
271         char c = ' ';
272
273         static char *html_head[] = {
274                 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
275                 "<pre>",
276                 0,
277         };
278         static char *html_foot[] = {
279                 "</pre>\n",
280                 "</body>\n</html>\n",
281                 0,
282         };
283
284         bool old_use_graphics = use_graphics;
285
286         /* Alloc buffer */
287         screen_buf = buf_new();
288         if (screen_buf == NULL) return (NULL);
289
290         if (old_use_graphics)
291         {
292                 use_graphics = FALSE;
293                 reset_visuals();
294
295                 /* Redraw everything */
296                 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
297
298                 /* Hack -- update */
299                 handle_stuff();
300         }
301
302         for (i = 0; html_head[i]; i++)
303                 buf_sprintf(screen_buf, html_head[i]);
304
305         /* Dump the screen */
306         for (y = 0; y < 24; y++)
307         {
308                 /* Start the row */
309                 if (y != 0)
310                         buf_sprintf(screen_buf, "\n");
311
312                 /* Dump each row */
313                 for (x = 0; x < 79; x++)
314                 {
315                         int rv, gv, bv;
316                         char *cc = NULL;
317                         /* Get the attr/char */
318                         (void)(Term_what(x, y, &a, &c));
319
320                         switch (c)
321                         {
322                         case '&': cc = "&amp;"; break;
323                         case '<': cc = "&lt;"; break;
324                         case '>': cc = "&gt;"; break;
325 #ifdef WINDOWS
326                         case 0x1f: c = '.'; break;
327                         case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
328 #endif
329                         }
330
331                         a = a & 0x0F;
332                         if ((y == 0 && x == 0) || a != old_a) {
333                                 rv = angband_color_table[a][1];
334                                 gv = angband_color_table[a][2];
335                                 bv = angband_color_table[a][3];
336                                 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">", 
337                                             ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
338                                 old_a = a;
339                         }
340                         if (cc)
341                                 buf_sprintf(screen_buf, "%s", cc);
342                         else
343                                 buf_sprintf(screen_buf, "%c", c);
344                 }
345         }
346         buf_sprintf(screen_buf, "</font>");
347
348         for (i = 0; html_foot[i]; i++)
349                 buf_sprintf(screen_buf, html_foot[i]);
350
351         /* Screen dump size is too big ? */
352         if (screen_buf->size + 1> SCREEN_BUF_SIZE)
353         {
354                 ret = NULL;
355         }
356         else
357         {
358                 /* Terminate string */
359                 buf_append(screen_buf, "", 1);
360
361                 ret = string_make(screen_buf->data);
362         }
363
364         /* Free buffer */
365         buf_delete(screen_buf);
366
367         if (old_use_graphics)
368         {
369                 use_graphics = TRUE;
370                 reset_visuals();
371
372                 /* Redraw everything */
373                 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
374
375                 /* Hack -- update */
376                 handle_stuff();
377         }
378
379         return ret;
380 }
381
382
383 errr report_score(void)
384 {
385 #ifdef MACINTOSH
386         OSStatus err;
387 #else
388         errr err = 0;
389 #endif
390
391 #ifdef WINDOWS
392         WSADATA wsaData;
393         WORD wVersionRequested =(WORD) (( 1) |  ( 1 << 8));
394 #endif
395
396         BUF *score;
397         int sd;
398         char seikakutmp[128];
399
400         score = buf_new();
401
402 #ifdef JP
403         sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "¤Î" : ""));
404 #else
405         sprintf(seikakutmp, "%s ", ap_ptr->title);
406 #endif
407
408         buf_sprintf(score, "name: %s\n", player_name);
409 #ifdef JP
410         buf_sprintf(score, "version: ÊѶòÈÚÅÜ %d.%d.%d\n",
411                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
412 #else
413         buf_sprintf(score, "version: Hengband %d.%d.%d\n",
414                     FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
415 #endif
416         buf_sprintf(score, "score: %d\n", total_points());
417         buf_sprintf(score, "level: %d\n", p_ptr->lev);
418         buf_sprintf(score, "depth: %d\n", dun_level);
419         buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
420         buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
421         buf_sprintf(score, "au: %d\n", p_ptr->au);
422         buf_sprintf(score, "turns: %d\n", turn_real(turn));
423         buf_sprintf(score, "sex: %d\n", p_ptr->psex);
424         buf_sprintf(score, "race: %s\n", rp_ptr->title);
425         buf_sprintf(score, "class: %s\n", cp_ptr->title);
426         buf_sprintf(score, "seikaku: %s\n", seikakutmp);
427         buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
428         buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
429         buf_sprintf(score, "killer: %s\n", died_from);
430         buf_sprintf(score, "-----charcter dump-----\n");
431
432         make_dump(score);
433
434         if (screen_dump)
435         {
436                 buf_sprintf(score, "-----screen shot-----\n");
437                 buf_append(score, screen_dump, strlen(screen_dump));
438         }
439         
440 #ifdef WINDOWS
441         if (WSAStartup(wVersionRequested, &wsaData))
442         {
443                 msg_print("Report: WSAStartup failed.");
444                 goto report_end;
445         }
446 #endif
447
448 #ifdef MACINTOSH
449 #if TARGET_API_MAC_CARBON
450         err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
451 #else
452         err = InitOpenTransport();
453 #endif
454         if (err != noErr)
455         {
456                 msg_print("Report: OpenTransport failed.");
457                 return 1;
458         }
459 #endif
460
461         Term_clear();
462
463         while (1)
464         {
465                 char buff[160];
466 #ifdef JP
467                 prt("ÀܳÃæ...", 0, 0);
468 #else
469                 prt("connecting...", 0, 0);
470 #endif
471                 Term_fresh();
472                 
473                 sd = connect_scoreserver();
474                 if (!(sd < 0)) break;
475 #ifdef JP
476                 sprintf(buff, "¥¹¥³¥¢¡¦¥µ¡¼¥Ð¤Ø¤ÎÀܳ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£(%s)", soc_err());
477 #else
478                 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
479 #endif
480                 prt(buff, 0, 0);
481                 (void)inkey();
482                 
483 #ifdef JP
484                 if (!get_check("¤â¤¦°ìÅÙÀܳ¤ò»î¤ß¤Þ¤¹¤«? "))
485 #else
486                 if (!get_check("Try again? "))
487 #endif
488                 {
489                         err = 1;
490                         goto report_end;
491                 }
492         }
493 #ifdef JP
494         prt("¥¹¥³¥¢Á÷¿®Ãæ...", 0, 0);
495 #else
496         prt("Sending the score...", 0, 0);
497 #endif
498         Term_fresh();
499         http_post(sd, SCORE_PATH, score);
500
501         disconnect_server(sd);
502  report_end:
503 #ifdef WINDOWS
504         WSACleanup();
505 #endif
506
507 #ifdef MACINTOSH
508 #if TARGET_API_MAC_CARBON
509         CloseOpenTransportInContext(NULL);
510 #else
511         CloseOpenTransport();
512 #endif
513 #endif
514
515         return err;
516 }
517
518 #endif /* WORLD_SCORE */