OSDN Git Service

Merge branch 'develop' into macos-develop
[hengbandforosx/hengbandosx.git] / src / save / save.cpp
index 35b4c1a..904bbb5 100644 (file)
@@ -1,4 +1,4 @@
-/*!
+/*!
  * @file save.c
  * @brief セーブファイル書き込み処理 / Purpose: interact with savefiles
  * @date 2014/07/12
 #include "dungeon/quest.h"
 #include "floor/floor-town.h"
 #include "floor/wild.h"
-#include "game-option/text-display-options.h"
 #include "inventory/inventory-slot-types.h"
 #include "io/files-util.h"
 #include "io/report.h"
 #include "io/uid-checker.h"
-#include "monster-race/monster-race.h"
+#include "locale/character-encoding.h"
 #include "monster/monster-compaction.h"
 #include "monster/monster-status.h"
 #include "player/player-status.h"
 #include "system/monster-race-info.h"
 #include "system/player-type-definition.h"
 #include "util/angband-files.h"
+#include "util/enum-converter.h"
 #include "view/display-messages.h"
 #include "world/world.h"
 #include <algorithm>
+#include <sstream>
+#include <string>
 
 /*!
  * @brief セーブデータの書き込み /
@@ -50,7 +52,7 @@
  * @param player_ptr プレイヤーへの参照ポインタ
  * @return 成功すればtrue
  */
-static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
+static bool wr_savefile_new(PlayerType *player_ptr)
 {
     compact_objects(player_ptr, 0);
     compact_monsters(player_ptr, 0);
@@ -90,26 +92,18 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
 
 #ifdef JP
 #ifdef EUC
-    wr_byte(2);
+    wr_byte(enum2i(CharacterEncoding::EUC_JP));
 #endif
 #ifdef SJIS
-    wr_byte(3);
+    wr_byte(enum2i(CharacterEncoding::SHIFT_JIS));
 #endif
 #else
-    wr_byte(1);
+    wr_byte(enum2i(CharacterEncoding::US_ASCII));
 #endif
 
     wr_randomizer();
-    wr_options(type);
-    uint32_t tmp32u = message_num();
-    if ((compress_savefile || (type == SaveType::DEBUG)) && (tmp32u > 40)) {
-        tmp32u = 40;
-    }
-
-    wr_u32b(tmp32u);
-    for (int i = tmp32u - 1; i >= 0; i--) {
-        wr_string(message_str(i));
-    }
+    wr_options();
+    wr_message_history();
 
     uint16_t tmp16u = static_cast<uint16_t>(monraces_info.size());
     wr_u16b(tmp16u);
@@ -117,7 +111,7 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
         wr_lore(i2enum<MonsterRaceId>(r_idx));
     }
 
-    tmp16u = static_cast<uint16_t>(baseitems_info.size());
+    tmp16u = static_cast<uint16_t>(BaseitemList::get_instance().size());
     wr_u16b(tmp16u);
     for (short bi_id = 0; bi_id < tmp16u; bi_id++) {
         wr_perception(bi_id);
@@ -126,34 +120,34 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
     tmp16u = static_cast<uint16_t>(towns_info.size());
     wr_u16b(tmp16u);
 
-    const auto &quest_list = QuestList::get_instance();
-    tmp16u = static_cast<uint16_t>(quest_list.size());
+    const auto &quests = QuestList::get_instance();
+    tmp16u = static_cast<uint16_t>(quests.size());
     wr_u16b(tmp16u);
 
     tmp8u = MAX_RANDOM_QUEST - MIN_RANDOM_QUEST;
     wr_byte(tmp8u);
 
-    for (const auto &[q_idx, q_ref] : quest_list) {
-        wr_s16b(enum2i(q_idx));
-        wr_s16b(enum2i(q_ref.status));
-        wr_s16b((int16_t)q_ref.level);
-        wr_byte((byte)q_ref.complev);
-        wr_u32b(q_ref.comptime);
+    for (const auto &[quest_id, quest] : quests) {
+        wr_s16b(enum2i(quest_id));
+        wr_s16b(enum2i(quest.status));
+        wr_s16b((int16_t)quest.level);
+        wr_byte((byte)quest.complev);
+        wr_u32b(quest.comptime);
 
-        auto is_quest_running = q_ref.status == QuestStatusType::TAKEN;
-        is_quest_running |= q_ref.status == QuestStatusType::COMPLETED;
-        is_quest_running |= !quest_type::is_fixed(q_idx);
+        auto is_quest_running = quest.status == QuestStatusType::TAKEN;
+        is_quest_running |= quest.status == QuestStatusType::COMPLETED;
+        is_quest_running |= !QuestType::is_fixed(quest_id);
         if (!is_quest_running) {
             continue;
         }
 
-        wr_s16b((int16_t)q_ref.cur_num);
-        wr_s16b((int16_t)q_ref.max_num);
-        wr_s16b(enum2i(q_ref.type));
-        wr_s16b(enum2i(q_ref.r_idx));
-        wr_s16b(enum2i(q_ref.reward_artifact_idx));
-        wr_byte((byte)q_ref.flags);
-        wr_byte((byte)q_ref.dungeon);
+        wr_s16b((int16_t)quest.cur_num);
+        wr_s16b((int16_t)quest.max_num);
+        wr_s16b(enum2i(quest.type));
+        wr_s16b(enum2i(quest.r_idx));
+        wr_s16b(enum2i(quest.reward_fa_id));
+        wr_byte((byte)quest.flags);
+        wr_byte((byte)quest.dungeon);
     }
 
     wr_s32b(player_ptr->wilderness_x);
@@ -168,12 +162,13 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
         }
     }
 
-    auto max_a_num = enum2i(artifacts_info.rbegin()->first);
+    const auto &artifacts = ArtifactList::get_instance();
+    auto max_a_num = enum2i(artifacts.rbegin()->first);
     tmp16u = max_a_num + 1;
     wr_u16b(tmp16u);
     for (auto i = 0U; i < tmp16u; i++) {
         const auto a_idx = i2enum<FixedArtifactId>(i);
-        const auto &artifact = ArtifactsInfo::get_instance().get_artifact(a_idx);
+        const auto &artifact = artifacts.get_artifact(a_idx);
         wr_bool(artifact.is_generated);
         wr_s16b(artifact.floor_id);
     }
@@ -202,13 +197,13 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
     }
 
     for (int i = 0; i < INVEN_TOTAL; i++) {
-        auto *o_ptr = &player_ptr->inventory_list[i];
-        if (!o_ptr->bi_id) {
+        const auto &item = player_ptr->inventory_list[i];
+        if (!item.is_valid()) {
             continue;
         }
 
         wr_u16b((uint16_t)i);
-        wr_item(o_ptr);
+        wr_item(item);
     }
 
     wr_u16b(0xFFFF);
@@ -218,8 +213,8 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
     tmp16u = MAX_STORES;
     wr_u16b(tmp16u);
     for (size_t i = 1; i < towns_info.size(); i++) {
-        for (auto j = 0; j < MAX_STORES; j++) {
-            wr_store(&towns_info[i].store[j]);
+        for (auto sst : STORE_SALE_TYPE_LIST) {
+            wr_store(&towns_info[i].stores[sst]);
         }
     }
 
@@ -246,28 +241,28 @@ static bool wr_savefile_new(PlayerType *player_ptr, SaveType type)
 }
 
 /*!
- * @brief セーブデータ書き込みのサブルーチン /
- * Medium level player saver
+ * @brief セーブデータ書き込みのサブルーチン
  * @param player_ptr プレイヤーへの参照ポインタ
- * @return 成功すればtrue
- * @details
- * Angband 2.8.0 will use "fd" instead of "fff" if possible
+ * @param path セーブデータのフルパス
+ * @param type セーブ後の処理種別
+ * @return セーブの成功可否
  */
-static bool save_player_aux(PlayerType *player_ptr, char *name, SaveType type)
+static bool save_player_aux(PlayerType *player_ptr, const std::filesystem::path &path)
 {
-    safe_setuid_grab(player_ptr);
-    auto fd = fd_make(name);
+    safe_setuid_grab();
+    auto fd = fd_make(path);
     safe_setuid_drop();
 
     bool is_save_successful = false;
     saving_savefile = nullptr;
     if (fd >= 0) {
         (void)fd_close(fd);
-        safe_setuid_grab(player_ptr);
-        saving_savefile = angband_fopen(name, FileOpenMode::WRITE, true);
+        safe_setuid_grab();
+        saving_savefile = angband_fopen(path, FileOpenMode::WRITE, true,
+            FileOpenType::SAVE);
         safe_setuid_drop();
         if (saving_savefile) {
-            if (wr_savefile_new(player_ptr, type)) {
+            if (wr_savefile_new(player_ptr)) {
                 is_save_successful = true;
             }
 
@@ -276,9 +271,9 @@ static bool save_player_aux(PlayerType *player_ptr, char *name, SaveType type)
             }
         }
 
-        safe_setuid_grab(player_ptr);
+        safe_setuid_grab();
         if (!is_save_successful) {
-            (void)fd_kill(name);
+            (void)fd_kill(path);
         }
 
         safe_setuid_drop();
@@ -294,39 +289,35 @@ static bool save_player_aux(PlayerType *player_ptr, char *name, SaveType type)
 }
 
 /*!
- * @brief セーブデータ書き込みのメインルーチン /
- * Attempt to save the player in a savefile
+ * @brief セーブデータ書き込みのメインルーチン
  * @param player_ptr プレイヤーへの参照ポインタ
  * @return 成功すればtrue
+ * @details 以下の順番で書き込みを実行する.
+ * 1. hoge.new にセーブデータを書き込む
+ * 2. hoge をhoge.old にリネームする
+ * 3. hoge.new をhoge にリネームする
+ * 4. hoge.old を削除する
  */
 bool save_player(PlayerType *player_ptr, SaveType type)
 {
-    char safe[1024];
-    strcpy(safe, savefile);
-    strcat(safe, ".new");
-    safe_setuid_grab(player_ptr);
-    fd_kill(safe);
-    safe_setuid_drop();
-    update_playtime();
-    bool result = false;
-    if (save_player_aux(player_ptr, safe, type)) {
-        char temp[1024];
-        char filename[1024];
-        strcpy(temp, savefile);
-        strcat(temp, ".old");
-        safe_setuid_grab(player_ptr);
-        fd_kill(temp);
-
-        if (type == SaveType::DEBUG) {
-            strcpy(filename, debug_savefile);
-        }
-        if (type != SaveType::DEBUG) {
-            strcpy(filename, savefile);
-        }
+    std::stringstream ss_new;
+    ss_new << savefile.string() << ".new";
+    auto savefile_new = ss_new.str();
+    safe_setuid_grab();
+    fd_kill(savefile_new);
 
-        fd_move(filename, temp);
-        fd_move(safe, filename);
-        fd_kill(temp);
+    safe_setuid_drop();
+    w_ptr->update_playtime();
+    auto result = false;
+    if (save_player_aux(player_ptr, savefile_new.data())) {
+        std::stringstream ss_old;
+        ss_old << savefile.string() << ".old";
+        auto savefile_old = ss_old.str();
+        safe_setuid_grab();
+        fd_kill(savefile_old);
+        fd_move(savefile, savefile_old);
+        fd_move(savefile_new, savefile);
+        fd_kill(savefile_old);
         safe_setuid_drop();
         w_ptr->character_loaded = true;
         result = true;