【C++】植物大战僵尸杂交版自动存档——防闪退存档消失

植物大战僵尸杂交版现已更新到v2.0.88,闪退问题还是偶有发生,参考网上现有的方案,简单实现了一个。

原理就是监控存档目录的文件变化,一旦有新的存档,则将其备份。如发生闪退,则还原备份即可。

原目录:C:/ProgramData/PopCap Games/PlantsVsZombies/pvzHE/yourdata

备份目录:C:/ProgramData/PopCap Games/PlantsVsZombies/pvzHE/yourdata_Backup

源代码

cpp 复制代码
#include <windows.h>
#include <iostream>
#include <filesystem>
#include <string>
#include <vector>

namespace fs = std::filesystem;

void copy_directory(const fs::path &source, const fs::path &destination)
{
    try
    {
        if (!fs::exists(destination))
        {
            fs::create_directories(destination);
        }
        for (const auto &entry : fs::recursive_directory_iterator(source))
        {
            const auto &path = entry.path();
            auto relativePathStr = path.lexically_relative(source).string();
            fs::copy(path, destination / relativePathStr, fs::copy_options::overwrite_existing);
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
    }
}

void monitor_directory(const std::wstring &path, const std::wstring &backup_path)
{
    HANDLE hDir = CreateFileW(
        path.c_str(),
        FILE_LIST_DIRECTORY,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_BACKUP_SEMANTICS,
        NULL);

    if (hDir == INVALID_HANDLE_VALUE)
    {
        std::cerr << "CreateFile failed with " << GetLastError() << std::endl;
        return;
    }

    char buffer[1024];
    DWORD bytesReturned;
    FILE_NOTIFY_INFORMATION *pNotify;
    std::vector<char> filenameBuffer(MAX_PATH);

    while (true)
    {
        if (ReadDirectoryChangesW(
                hDir,
                buffer,
                sizeof(buffer),
                FALSE,
                FILE_NOTIFY_CHANGE_FILE_NAME,
                &bytesReturned,
                NULL,
                NULL))
        {
            pNotify = (FILE_NOTIFY_INFORMATION *)buffer;
            do
            {
                std::wstring filename(pNotify->FileName, pNotify->FileNameLength / sizeof(WCHAR));
                if (pNotify->Action == FILE_ACTION_ADDED)
                {
                    std::wcout << L"New file created: " << filename << std::endl;
                    copy_directory(path, backup_path);
                }
                pNotify = (FILE_NOTIFY_INFORMATION *)((char *)pNotify + pNotify->NextEntryOffset);
            } while (pNotify->NextEntryOffset != 0);
        }
        else
        {
            std::cerr << "ReadDirectoryChangesW failed with " << GetLastError() << std::endl;
            break;
        }
    }

    CloseHandle(hDir);
}

int main()
{

    std::wstring directory_to_monitor = L"C:/ProgramData/PopCap Games/PlantsVsZombies/pvzHE/yourdata";
    std::wstring backup_directory = L"C:/ProgramData/PopCap Games/PlantsVsZombies/pvzHE/yourdata_Backup";

    std::cout << "开始运行" << std::endl;

    monitor_directory(directory_to_monitor, backup_directory);

    return EXIT_SUCCESS;
}

注意

需要使用MSVC(VS 2022)以GBK编码保存代码 编译,如需用MinGW编译,则要手动修改部分代码。

相关推荐
我是无敌小恐龙23 分钟前
Java基础入门Day10 | Object类、包装类、大数/日期类、冒泡排序与Arrays工具类 超详细总结
java·开发语言·数据结构·算法·贪心算法·排序算法·动态规划
yuanyuan2o239 分钟前
从最小项目开始的 CMake 教程
c语言·开发语言·arm开发·c++·makefile·make·cmake
lifewange40 分钟前
pytest 找不到文件?直接在 pytest.ini 配置根目录 + 路径(最简单方案)
开发语言·python·pytest
大鹏说大话1 小时前
MySQL + Redis + Caffeine:Java后端通用三级缓存架构实战
开发语言
yuanpan1 小时前
Python 桌面 GUI 入门开发:从 tkinter 窗口到简易记事本
开发语言·python
User_芊芊君子1 小时前
聊聊自由开发者常用的学习机会全解析
开发语言·人工智能·python
Hical_W1 小时前
Hical 踩坑实录五部曲(二):MSVC / GCC / Clang 三平台 C++20 编译差异
linux·windows·经验分享·嵌入式硬件·macos·开源·c++20
AI人工智能+电脑小能手2 小时前
【大白话说Java面试题】【Java基础篇】第40题:Java中的深拷贝和浅拷贝有什么区别
java·开发语言·后端·面试
mianfeixz2 小时前
生化危机9安魂曲修改器2026最新版
windows
xh didida2 小时前
算法 -- 位运算
开发语言·c++·算法