【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编译,则要手动修改部分代码。

相关推荐
偷光2 小时前
浏览器中的隐藏IDE: Elements (元素) 面板
开发语言·前端·ide·php
DKPT2 小时前
JVM栈溢出和堆溢出哪个先满?
java·开发语言·jvm·笔记·学习
爱和冰阔落2 小时前
C++模板进阶 非类型模板参数 模板的特化 分离编译的深入探索
c++·面试·编译原理·模板
gopyer5 小时前
180课时吃透Go语言游戏后端开发6:Go语言的循环语句
开发语言·游戏·golang·循环语句
java之迷6 小时前
Windows环境下,源码启动+本地部署和启动开源项目Ragflow失败SRE模块
windows·docker·开源
charlie1145141918 小时前
精读C++20设计模式:行为型设计模式:中介者模式
c++·学习·设计模式·c++20·中介者模式
楼田莉子8 小时前
Qt开发学习——QtCreator深度介绍/程序运行/开发规范/对象树
开发语言·前端·c++·qt·学习
oioihoii8 小时前
超越 std::unique_ptr:探讨自定义删除器的真正力量
c++
摩羯座-185690305948 小时前
爬坑 10 年!京东店铺全量商品接口实战开发:从分页优化、SKU 关联到数据完整性闭环
linux·网络·数据库·windows·爬虫·python
Gohldg9 小时前
C++算法·贪心例题讲解
c++·数学·算法·贪心算法