5 * @author Hengband Team
8 #define _GNU_SOURCE /*!< 未使用*/
20 #elif defined(MACINTOSH)
21 #include <OpenTransport.h>
22 #include <OpenTptInternet.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
35 * internet resource value
37 #define HTTP_PROXY "" /*!< デフォルトのプロキシURL / Default proxy url */
38 #define HTTP_PROXY_PORT 0 /*!< デフォルトのプロキシポート / Default proxy port */
39 #define HTTP_TIMEOUT 20 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
40 #define SCORE_SERVER "moon.kmc.gr.jp" /*!< デフォルトのスコアサーバURL / Default score server url */
41 #define SCORE_PORT 80 /*!< デフォルトのスコアサーバポート / Default score server port */
44 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore/score.cgi" /*!< スコア開示URL */
46 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
51 #define SCORE_PATH "http://moon.kmc.gr.jp/hengband/scoretest/score.cgi" /*!< スコア開示URL */
55 * simple buffer library
63 #define BUFSIZE (65536) /*!< スコアサーバ転送バッファサイズ */
67 * @return 確保したバッファの参照ポインタ
69 static BUF* buf_new(void)
73 if ((p = malloc(sizeof(BUF))) == NULL)
77 p->max_size = BUFSIZE;
78 if ((p->data = malloc(BUFSIZE)) == NULL)
88 * @param b 解放するバッファの参照ポインタ
90 static void buf_delete(BUF *b)
97 * @brief 転送用バッファにデータを追加する
98 * @param buf 追加先バッファの参照ポインタ
103 static int buf_append(BUF *buf, const char *data, size_t size)
105 while (buf->size + size > buf->max_size)
108 if ((tmp = malloc(buf->max_size * 2)) == NULL) return -1;
110 memcpy(tmp, buf->data, buf->max_size);
117 memcpy(buf->data + buf->size, data, size);
124 * @brief 転送用バッファにフォーマット指定した文字列データを追加する
125 * @param buf 追加先バッファの参照ポインタ
126 * @param fmt 文字列フォーマット
129 static int buf_sprintf(BUF *buf, const char *fmt, ...)
136 #if defined(HAVE_VSNPRINTF)
137 ret = vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
139 ret = vsprintf(tmpbuf, fmt, ap);
143 if (ret < 0) return -1;
145 #if ('\r' == 0x0a && '\n' == 0x0d)
148 * Originally '\r'= CR (= 0x0d) and '\n'= LF (= 0x0a)
149 * But for MPW (Macintosh Programers Workbench), these
150 * are reversed so that '\r'=LF and '\n'=CR unless the
151 * -noMapCR option is not defined.
153 * We need to swap back these here since the score
154 * dump text should be written using LF as the end of
158 for (ptr = tmpbuf; *ptr; ptr++)
160 if (0x0d == *ptr) *ptr = 0x0a;
165 ret = buf_append(buf, tmpbuf, strlen(tmpbuf));
171 static int buf_read(BUF *buf, int fd)
179 tmp = calloc( BUFSIZE , sizeof(char) );
182 while ((len = read(fd, tmp, BUFSIZE)) > 0)
183 buf_append(buf, tmp, len);
190 static int buf_write(BUF *buf, int fd)
192 write(fd, buf->data, buf->size);
197 static int buf_search(BUF *buf, const char *str)
201 ret = my_strstr(buf->data, str);
205 return ret - buf->data;
208 static BUF * buf_subbuf(BUF *buf, int pos1, size_t sz)
212 if (pos1 < 0) return NULL;
216 if (sz <= 0) sz = buf->size - pos1;
218 buf_append(ret, buf->data + pos1, sz);
225 * @brief HTTPによるダンプ内容伝送
228 * @param buf 伝送内容バッファ
231 static void http_post(int sd, cptr url, BUF *buf)
236 buf_sprintf(output, "POST %s HTTP/1.0\n", url);
237 buf_sprintf(output, "User-Agent: Hengband %d.%d.%d\n",
238 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
240 buf_sprintf(output, "Content-Length: %d\n", buf->size);
241 buf_sprintf(output, "Content-Encoding: binary\n");
242 buf_sprintf(output, "Content-Type: application/octet-stream\n");
243 buf_sprintf(output, "\n");
244 buf_append(output, buf->data, buf->size);
246 soc_write(sd, output->data, output->size);
250 * @brief キャラクタダンプを作って BUFに保存
251 * @param dumpbuf 伝送内容バッファ
254 static errr make_dump(BUF* dumpbuf)
258 char file_name[1024];
260 /* Open a new file */
261 fff = my_fopen_temp(file_name, 1024);
265 msg_format("一時ファイル %s を作成できませんでした。", file_name);
267 msg_format("Failed to create temporary file %s.", file_name);
273 /* 一旦一時ファイルを作る。通常のダンプ出力と共通化するため。 */
274 (void)make_character_dump(fff);
280 fff = my_fopen(file_name, "r");
282 while (fgets(buf, 1024, fff))
284 (void)buf_sprintf(dumpbuf, "%s", buf);
290 /* Remove the file */
298 * @brief スクリーンダンプを作成する/ Make screen dump to buffer
299 * @return 作成したスクリーンダンプの参照ポインタ
301 cptr make_screen_dump(void)
307 byte a = 0, old_a = 0;
310 static cptr html_head[] = {
311 "<html>\n<body text=\"#ffffff\" bgcolor=\"#000000\">\n",
315 static cptr html_foot[] = {
317 "</body>\n</html>\n",
321 bool old_use_graphics = use_graphics;
325 Term_get_size(&wid, &hgt);
328 screen_buf = buf_new();
329 if (screen_buf == NULL) return (NULL);
331 if (old_use_graphics)
333 /* Clear -more- prompt first */
336 use_graphics = FALSE;
339 /* Redraw everything */
340 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
346 for (i = 0; html_head[i]; i++)
347 buf_sprintf(screen_buf, html_head[i]);
349 /* Dump the screen */
350 for (y = 0; y < hgt; y++)
354 buf_sprintf(screen_buf, "\n");
357 for (x = 0; x < wid - 1; x++)
361 /* Get the attr/char */
362 (void)(Term_what(x, y, &a, &c));
366 case '&': cc = "&"; break;
367 case '<': cc = "<"; break;
368 case '>': cc = ">"; break;
370 case 0x1f: c = '.'; break;
371 case 0x7f: c = (a == 0x09) ? '%' : '#'; break;
376 if ((y == 0 && x == 0) || a != old_a) {
377 rv = angband_color_table[a][1];
378 gv = angband_color_table[a][2];
379 bv = angband_color_table[a][3];
380 buf_sprintf(screen_buf, "%s<font color=\"#%02x%02x%02x\">",
381 ((y == 0 && x == 0) ? "" : "</font>"), rv, gv, bv);
385 buf_sprintf(screen_buf, "%s", cc);
387 buf_sprintf(screen_buf, "%c", c);
390 buf_sprintf(screen_buf, "</font>");
392 for (i = 0; html_foot[i]; i++)
393 buf_sprintf(screen_buf, html_foot[i]);
395 /* Screen dump size is too big ? */
396 if (screen_buf->size + 1> SCREEN_BUF_SIZE)
402 /* Terminate string */
403 buf_append(screen_buf, "", 1);
405 ret = string_make(screen_buf->data);
409 buf_delete(screen_buf);
411 if (old_use_graphics)
416 /* Redraw everything */
417 p_ptr->redraw |= (PR_WIPE | PR_BASIC | PR_EXTRA | PR_MAP | PR_EQUIPPY);
427 * @brief スコア転送処理のメインルーチン
430 errr report_score(void)
440 WORD wVersionRequested =(WORD) (( 1) | ( 1 << 8));
445 char seikakutmp[128];
450 sprintf(seikakutmp, "%s%s", ap_ptr->title, (ap_ptr->no ? "の" : ""));
452 sprintf(seikakutmp, "%s ", ap_ptr->title);
455 buf_sprintf(score, "name: %s\n", p_ptr->name);
457 buf_sprintf(score, "version: 変愚蛮怒 %d.%d.%d\n",
458 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
460 buf_sprintf(score, "version: Hengband %d.%d.%d\n",
461 FAKE_VER_MAJOR-10, FAKE_VER_MINOR, FAKE_VER_PATCH);
463 buf_sprintf(score, "score: %d\n", total_points());
464 buf_sprintf(score, "level: %d\n", p_ptr->lev);
465 buf_sprintf(score, "depth: %d\n", dun_level);
466 buf_sprintf(score, "maxlv: %d\n", p_ptr->max_plv);
467 buf_sprintf(score, "maxdp: %d\n", max_dlv[DUNGEON_ANGBAND]);
468 buf_sprintf(score, "au: %d\n", p_ptr->au);
469 buf_sprintf(score, "turns: %d\n", turn_real(turn));
470 buf_sprintf(score, "sex: %d\n", p_ptr->psex);
471 buf_sprintf(score, "race: %s\n", rp_ptr->title);
472 buf_sprintf(score, "class: %s\n", cp_ptr->title);
473 buf_sprintf(score, "seikaku: %s\n", seikakutmp);
474 buf_sprintf(score, "realm1: %s\n", realm_names[p_ptr->realm1]);
475 buf_sprintf(score, "realm2: %s\n", realm_names[p_ptr->realm2]);
476 buf_sprintf(score, "killer: %s\n", p_ptr->died_from);
477 buf_sprintf(score, "-----charcter dump-----\n");
483 buf_sprintf(score, "-----screen shot-----\n");
484 buf_append(score, screen_dump, strlen(screen_dump));
488 if (WSAStartup(wVersionRequested, &wsaData))
490 msg_print("Report: WSAStartup failed.");
496 #if TARGET_API_MAC_CARBON
497 err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL);
499 err = InitOpenTransport();
503 msg_print("Report: OpenTransport failed.");
516 prt("connecting...", 0, 0);
521 set_proxy(HTTP_PROXY, HTTP_PROXY_PORT);
523 /* Connect to the score server */
524 sd = connect_server(HTTP_TIMEOUT, SCORE_SERVER, SCORE_PORT);
527 if (!(sd < 0)) break;
529 sprintf(buff, "スコア・サーバへの接続に失敗しました。(%s)", soc_err());
531 sprintf(buff, "Failed to connect to the score server.(%s)", soc_err());
537 if (!get_check_strict("もう一度接続を試みますか? ", CHECK_NO_HISTORY))
539 if (!get_check_strict("Try again? ", CHECK_NO_HISTORY))
547 prt("スコア送信中...", 0, 0);
549 prt("Sending the score...", 0, 0);
552 http_post(sd, SCORE_PATH, score);
554 disconnect_server(sd);
561 #if TARGET_API_MAC_CARBON
562 CloseOpenTransportInContext(NULL);
564 CloseOpenTransport();
571 #endif /* WORLD_SCORE */