当前位置: 首页 > news >正文

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

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

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

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

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

源代码

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

http://www.lryc.cn/news/366153.html

相关文章:

  • 通过Excel,生成sql,将A表数据插入B表
  • 如何在MySQL中实现upsert:如果不存在则插入?
  • MyBatis中 set标签
  • mysql自带分页
  • 小学一年级数学上册,我终于学完了
  • 使用wireshark分析tcp握手过程
  • 在ArcGIS中,矢量数据有.shp,.mdb和.gdb,为啥建议使用gdb?
  • C++STL---stack queue模拟实现
  • Spring Cloud系列——使用Sentinel进行微服务保护
  • Android开机动画,framework修改Bootanimation绘制文字。
  • 2024河南高考作文ChatGPT
  • 整理好了!2024年最常见 20 道分布式、微服务面试题(一)
  • 要想数据形成好的数据集,必须数据治理(目的之一是防止大模型产生灰色数据等),用于炼丹(训练数据私有化模型)的数据才是好数据
  • 外部mysql导入
  • Qwen-VL论文阅读
  • 超详细的java Comparable,Comparator接口解析
  • Java使用GDAL来解析KMZ及KML实战
  • 【vuex小试牛刀】
  • React - 实现走马灯组件
  • 【学习笔记】Windows GDI绘图(十三)动画播放ImageAnimator(可调速)
  • fps游戏如何快速定位矩阵
  • 【机器学习基础】Python编程06:五个实用练习题的解析与总结
  • R可视化:生存分析森林图
  • 一个 python+tensorFlow训练1万张图片分类的简单直观例子( 回答由百度 AI 给出 )
  • DBeaver无法连接Clickhouse,连接失败
  • python基础实例
  • ADASIS V2 协议-1
  • 人工智能安全风险分析及应对策略
  • Python驱动下的AI革命:技术赋能与案例解析
  • JavaScrip轮播图