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

相关推荐
Trouvaille ~21 小时前
【项目篇】从零手写高并发服务器(九):HTTP协议支持——从TCP到应用层
linux·服务器·c++·tcp/ip·http·高并发·应用层
小此方21 小时前
Re:从零开始的 C++ STL篇(八)深度解构AVL树自平衡机制:平衡维护与旋转调整背后的严密逻辑
开发语言·数据结构·c++·算法·stl
2301_7890156221 小时前
封装哈希表实现unordered_set/undered_map
c语言·数据结构·c++·算法·哈希算法
落羽的落羽21 小时前
【Linux系统】中断机制、用户态与内核态、虚拟地址与页表的本质
java·linux·服务器·c++·人工智能·算法·机器学习
bksczm21 小时前
C++ iostream , sstream的基本理解
开发语言·c++
lay_liu21 小时前
Node.JS 版本管理工具 Fnm 安装及配置(Windows)
windows·node.js
tgethe1 天前
java并发——1
java·开发语言·面试
郑州光合科技余经理1 天前
海外O2O系统源码剖析:多语言、多货币架构设计与二次开发实践
java·开发语言·前端·小程序·系统架构·uni-app·php
sqmw1 天前
MFCMouseEffect:把桌面输入反馈这件事,做成一个真正可扩展的引擎
c++·插件·引擎·鼠标特效·键鼠指示·鼠标伴宠
globaldomain1 天前
什么是用于长距离高速传输的TCP窗口扩展?
开发语言·网络·php