From: Hourier Date: Sun, 26 Apr 2020 01:49:29 +0000 (+0900) Subject: [Refactor] #39964 Separated autopick-registry.c/h from autopick.c/h X-Git-Url: http://git.osdn.net/view?p=hengband%2Fhengband.git;a=commitdiff_plain;h=53e914abc3065f2057ef947648ec8a944f2169d5 [Refactor] #39964 Separated autopick-registry.c/h from autopick.c/h --- diff --git a/Hengband_vcs2017/Hengband/Hengband.vcxproj b/Hengband_vcs2017/Hengband/Hengband.vcxproj index 9b217d383..34fcd28da 100644 --- a/Hengband_vcs2017/Hengband/Hengband.vcxproj +++ b/Hengband_vcs2017/Hengband/Hengband.vcxproj @@ -161,6 +161,7 @@ + @@ -320,6 +321,7 @@ + diff --git a/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters b/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters index 556ffa3fd..270b59047 100644 --- a/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters +++ b/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters @@ -679,6 +679,9 @@ autopick + + autopick + @@ -1361,6 +1364,9 @@ autopick + + autopick + diff --git a/src/Makefile.am b/src/Makefile.am index 883fc4ed9..7a8c0a783 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,6 +28,7 @@ hengband_SOURCES = \ autopick/autopick-drawer.c autopick/autopick-drawer.h \ autopick/autopick-searcher.c autopick/autopick-searcher.h \ autopick/autopick-inserter-killer.c autopick/autopick-inserter-killer.h \ + autopick/autopick-registry.c autopick/autopick-registry.h \ \ avatar.h avatar.c birth.c birth.h \ \ diff --git a/src/autopick/autopick-registry.c b/src/autopick/autopick-registry.c new file mode 100644 index 000000000..c196599d5 --- /dev/null +++ b/src/autopick/autopick-registry.c @@ -0,0 +1,218 @@ +/*! + * @brief 自動拾いにアイテムを登録する + * @date 2020/04/26 + * @author Hourier + */ + +#include "angband.h" +#include "autopick/autopick-registry.h" +#include "autopick/autopick-util.h" +#include "autopick/autopick-methods-table.h" +#include "autopick/autopick-reader-writer.h" +#include "autopick/autopick-entry.h" +#include "autopick/autopick-finder.h" +#include "autopick/autopick-adder.h" +#include "object-hook.h" +#include "object/object-kind.h" +#include "object-flavor.h" +#include "util.h" +#include "files.h" + +static const char autoregister_header[] = "?:$AUTOREGISTER"; + +/* + * Clear auto registered lines in the picktype.prf . + */ +static bool clear_auto_register(player_type *player_ptr) +{ + char pref_file[1024]; + path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_WITH_PNAME)); + FILE *pref_fff; + pref_fff = my_fopen(pref_file, "r"); + + if (!pref_fff) + { + path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_DEFAULT)); + pref_fff = my_fopen(pref_file, "r"); + } + + if (!pref_fff) + { + return TRUE; + } + + char tmp_file[1024]; + FILE *tmp_fff; + tmp_fff = my_fopen_temp(tmp_file, sizeof(tmp_file)); + if (!tmp_fff) + { + fclose(pref_fff); + msg_format(_("一時ファイル %s を作成できませんでした。", "Failed to create temporary file %s."), tmp_file); + msg_print(NULL); + return FALSE; + } + + bool autoregister = FALSE; + int num = 0; + char buf[1024]; + while (TRUE) + { + if (my_fgets(pref_fff, buf, sizeof(buf))) break; + + if (autoregister) + { + if (buf[0] != '#' && buf[0] != '?') num++; + continue; + } + + if (streq(buf, autoregister_header)) + { + autoregister = TRUE; + } + else + { + fprintf(tmp_fff, "%s\n", buf); + } + } + + my_fclose(pref_fff); + my_fclose(tmp_fff); + + bool okay = TRUE; + if (num) + { + msg_format(_("以前のキャラクター用の自動設定(%d行)が残っています。", + "Auto registered lines (%d lines) for previous character are remaining."), num); + strcpy(buf, _("古い設定行は削除します。よろしいですか?", "These lines will be deleted. Are you sure? ")); + + if (!get_check(buf)) + { + okay = FALSE; + autoregister = FALSE; + + msg_print(_("エディタのカット&ペースト等を使って必要な行を避難してください。", + "Use cut & paste of auto picker editor (_) to keep old prefs.")); + } + } + + if (autoregister) + { + tmp_fff = my_fopen(tmp_file, "r"); + pref_fff = my_fopen(pref_file, "w"); + + while (!my_fgets(tmp_fff, buf, sizeof(buf))) + fprintf(pref_fff, "%s\n", buf); + + my_fclose(pref_fff); + my_fclose(tmp_fff); + } + + fd_kill(tmp_file); + return okay; +} + + +/* + * Automatically register an auto-destroy preference line + */ +bool autopick_autoregister(player_type *player_ptr, object_type *o_ptr) +{ + autopick_type an_entry, *entry = &an_entry; + int match_autopick = find_autopick_list(player_ptr, o_ptr); + if (match_autopick != -1) + { + concptr what; + byte act = autopick_list[match_autopick].action; + if (act & DO_AUTOPICK) what = _("自動で拾う", "auto-pickup"); + else if (act & DO_AUTODESTROY) what = _("自動破壊する", "auto-destroy"); + else if (act & DONT_AUTOPICK) what = _("放置する", "leave on floor"); + else what = _("確認して拾う", "query auto-pickup"); + + msg_format(_("そのアイテムは既に%sように設定されています。", "The object is already registered to %s."), what); + return FALSE; + } + + if ((object_is_known(o_ptr) && object_is_artifact(o_ptr)) || + ((o_ptr->ident & IDENT_SENSE) && + (o_ptr->feeling == FEEL_TERRIBLE || o_ptr->feeling == FEEL_SPECIAL))) + { + GAME_TEXT o_name[MAX_NLEN]; + object_desc(player_ptr, o_name, o_ptr, 0); + msg_format(_("%sは破壊不能だ。", "You cannot auto-destroy %s."), o_name); + return FALSE; + } + + if (!player_ptr->autopick_autoregister) + { + if (!clear_auto_register(player_ptr)) return FALSE; + } + + char buf[1024]; + char pref_file[1024]; + FILE *pref_fff; + path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_WITH_PNAME)); + pref_fff = my_fopen(pref_file, "r"); + + if (!pref_fff) + { + path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_DEFAULT)); + pref_fff = my_fopen(pref_file, "r"); + } + + if (pref_fff) + { + while (TRUE) + { + if (my_fgets(pref_fff, buf, sizeof(buf))) + { + player_ptr->autopick_autoregister = FALSE; + break; + } + + if (streq(buf, autoregister_header)) + { + player_ptr->autopick_autoregister = TRUE; + break; + } + } + + fclose(pref_fff); + } + else + { + /* + * File could not be opened for reading. Assume header not + * present. + */ + player_ptr->autopick_autoregister = FALSE; + } + + pref_fff = my_fopen(pref_file, "a"); + if (!pref_fff) + { + msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), pref_file); + msg_print(NULL); + return FALSE; + } + + if (!player_ptr->autopick_autoregister) + { + fprintf(pref_fff, "%s\n", autoregister_header); + + fprintf(pref_fff, "%s\n", _("# *警告!!* 以降の行は自動登録されたものです。", + "# *Warning!* The lines below will be deleted later.")); + fprintf(pref_fff, "%s\n", _("# 後で自動的に削除されますので、必要な行は上の方へ移動しておいてください。", + "# Keep it by cut & paste if you need these lines for future characters.")); + player_ptr->autopick_autoregister = TRUE; + } + + autopick_entry_from_object(player_ptr, entry, o_ptr); + entry->action = DO_AUTODESTROY; + add_autopick_list(entry); + + concptr tmp = autopick_line_from_entry(entry); + fprintf(pref_fff, "%s\n", tmp); + string_free(tmp); + fclose(pref_fff); + return TRUE; +} diff --git a/src/autopick/autopick-registry.h b/src/autopick/autopick-registry.h new file mode 100644 index 000000000..4b0375f8f --- /dev/null +++ b/src/autopick/autopick-registry.h @@ -0,0 +1,3 @@ +#pragma once + +bool autopick_autoregister(player_type *player_ptr, object_type *o_ptr); diff --git a/src/autopick/autopick.c b/src/autopick/autopick.c index dfdc74a1b..2309a2c68 100644 --- a/src/autopick/autopick.c +++ b/src/autopick/autopick.c @@ -24,10 +24,10 @@ #include "autopick/autopick-entry.h" #include "autopick/autopick-reader-writer.h" #include "autopick/autopick-finder.h" -#include "autopick/autopick-adder.h" #include "autopick/autopick-drawer.h" #include "autopick/autopick-searcher.h" #include "autopick/autopick-inserter-killer.h" +#include "autopick/autopick-registry.h" #include "gameterm.h" #include "autopick/autopick.h" #include "core/show-file.h" @@ -39,11 +39,8 @@ #include "market/store.h" #include "player-move.h" #include "player-class.h" -#include "player-race.h" #include "view/display-player.h" // 暫定。後で消す. -#include "object/object-kind.h" #include "object-flavor.h" -#include "object-hook.h" #include "world.h" #include "view/display-main-window.h" // 暫定。後で消す. @@ -172,206 +169,6 @@ void autopick_pickup_items(player_type* player_ptr, grid_type *g_ptr) } -static const char autoregister_header[] = "?:$AUTOREGISTER"; - -/* - * Clear auto registered lines in the picktype.prf . - */ -static bool clear_auto_register(player_type *player_ptr) -{ - char pref_file[1024]; - path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_WITH_PNAME)); - FILE *pref_fff; - pref_fff = my_fopen(pref_file, "r"); - - if (!pref_fff) - { - path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_DEFAULT)); - pref_fff = my_fopen(pref_file, "r"); - } - - if (!pref_fff) - { - return TRUE; - } - - char tmp_file[1024]; - FILE *tmp_fff; - tmp_fff = my_fopen_temp(tmp_file, sizeof(tmp_file)); - if (!tmp_fff) - { - fclose(pref_fff); - msg_format(_("一時ファイル %s を作成できませんでした。", "Failed to create temporary file %s."), tmp_file); - msg_print(NULL); - return FALSE; - } - - bool autoregister = FALSE; - int num = 0; - char buf[1024]; - while (TRUE) - { - if (my_fgets(pref_fff, buf, sizeof(buf))) break; - - if (autoregister) - { - if (buf[0] != '#' && buf[0] != '?') num++; - continue; - } - - if (streq(buf, autoregister_header)) - { - autoregister = TRUE; - } - else - { - fprintf(tmp_fff, "%s\n", buf); - } - } - - my_fclose(pref_fff); - my_fclose(tmp_fff); - - bool okay = TRUE; - if (num) - { - msg_format(_("以前のキャラクター用の自動設定(%d行)が残っています。", - "Auto registered lines (%d lines) for previous character are remaining."), num); - strcpy(buf, _("古い設定行は削除します。よろしいですか?", "These lines will be deleted. Are you sure? ")); - - if (!get_check(buf)) - { - okay = FALSE; - autoregister = FALSE; - - msg_print(_("エディタのカット&ペースト等を使って必要な行を避難してください。", - "Use cut & paste of auto picker editor (_) to keep old prefs.")); - } - } - - if (autoregister) - { - tmp_fff = my_fopen(tmp_file, "r"); - pref_fff = my_fopen(pref_file, "w"); - - while (!my_fgets(tmp_fff, buf, sizeof(buf))) - fprintf(pref_fff, "%s\n", buf); - - my_fclose(pref_fff); - my_fclose(tmp_fff); - } - - fd_kill(tmp_file); - return okay; -} - - -/* - * Automatically register an auto-destroy preference line - */ -bool autopick_autoregister(player_type *player_ptr, object_type *o_ptr) -{ - char buf[1024]; - char pref_file[1024]; - FILE *pref_fff; - autopick_type an_entry, *entry = &an_entry; - int match_autopick = find_autopick_list(player_ptr, o_ptr); - if (match_autopick != -1) - { - concptr what; - byte act = autopick_list[match_autopick].action; - if (act & DO_AUTOPICK) what = _("自動で拾う", "auto-pickup"); - else if (act & DO_AUTODESTROY) what = _("自動破壊する", "auto-destroy"); - else if (act & DONT_AUTOPICK) what = _("放置する", "leave on floor"); - else what = _("確認して拾う", "query auto-pickup"); - - msg_format(_("そのアイテムは既に%sように設定されています。", "The object is already registered to %s."), what); - return FALSE; - } - - if ((object_is_known(o_ptr) && object_is_artifact(o_ptr)) || - ((o_ptr->ident & IDENT_SENSE) && - (o_ptr->feeling == FEEL_TERRIBLE || o_ptr->feeling == FEEL_SPECIAL))) - { - GAME_TEXT o_name[MAX_NLEN]; - object_desc(player_ptr, o_name, o_ptr, 0); - msg_format(_("%sは破壊不能だ。", "You cannot auto-destroy %s."), o_name); - return FALSE; - } - - if (!player_ptr->autopick_autoregister) - { - if (!clear_auto_register(player_ptr)) return FALSE; - } - - path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_WITH_PNAME)); - pref_fff = my_fopen(pref_file, "r"); - - if (!pref_fff) - { - path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(player_ptr, PT_DEFAULT)); - pref_fff = my_fopen(pref_file, "r"); - } - - if (pref_fff) - { - while (TRUE) - { - if (my_fgets(pref_fff, buf, sizeof(buf))) - { - player_ptr->autopick_autoregister = FALSE; - break; - } - - if (streq(buf, autoregister_header)) - { - player_ptr->autopick_autoregister = TRUE; - break; - } - } - - fclose(pref_fff); - } - else - { - /* - * File could not be opened for reading. Assume header not - * present. - */ - player_ptr->autopick_autoregister = FALSE; - } - - pref_fff = my_fopen(pref_file, "a"); - if (!pref_fff) - { - msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), pref_file); - msg_print(NULL); - return FALSE; - } - - if (!player_ptr->autopick_autoregister) - { - fprintf(pref_fff, "%s\n", autoregister_header); - - fprintf(pref_fff, "%s\n", _("# *警告!!* 以降の行は自動登録されたものです。", - "# *Warning!* The lines below will be deleted later.")); - fprintf(pref_fff, "%s\n", _("# 後で自動的に削除されますので、必要な行は上の方へ移動しておいてください。", - "# Keep it by cut & paste if you need these lines for future characters.")); - player_ptr->autopick_autoregister = TRUE; - } - - autopick_entry_from_object(player_ptr, entry, o_ptr); - entry->action = DO_AUTODESTROY; - add_autopick_list(entry); - - concptr tmp = autopick_line_from_entry(entry); - fprintf(pref_fff, "%s\n", tmp); - string_free(tmp); - fclose(pref_fff); - return TRUE; -} - - /* * Delete or insert string */ diff --git a/src/autopick/autopick.h b/src/autopick/autopick.h index d8193bce8..3fb2fbf68 100644 --- a/src/autopick/autopick.h +++ b/src/autopick/autopick.h @@ -5,5 +5,4 @@ extern void autopick_alter_item(player_type *player_ptr, INVENTORY_IDX item, bool destroy); extern void autopick_delayed_alter(player_type *player_ptr); extern void autopick_pickup_items(player_type *player_ptr, grid_type *g_ptr); -extern bool autopick_autoregister(player_type *player_ptr, object_type *o_ptr); extern void do_cmd_edit_autopick(player_type *player_ptr); diff --git a/src/cmd/cmd-item.c b/src/cmd/cmd-item.c index dea4ddbd1..c56a95c6e 100644 --- a/src/cmd/cmd-item.c +++ b/src/cmd/cmd-item.c @@ -10,7 +10,6 @@ * are included in all such copies. Other copyrights may also apply. */ - #include "angband.h" #include "core.h" #include "util.h" @@ -44,6 +43,7 @@ #include "spells.h" #include "object/object-kind.h" #include "autopick/autopick.h" +#include "autopick/autopick-registry.h" #include "targeting.h" #include "snipe.h" #include "player-race.h"