RPG Maker 2003 DynRPG插件实现文件流追加去重及操作修正
可直接编译使用的修改后插件代码
本次修改完全覆盖你提出的全部问题,无需额外调整逻辑:
- 所有删除、校验操作前不会主动创建文件:目标文件不存在时直接跳过流程,不会生成多余的空白文件
- 重写
clear_file指令逻辑:直接实现删除目标文件的功能,不再仅清空文件内容 - 所有文件追加写入分支内置重复校验:写入前逐行扫描目标文件,存在完全匹配的待写入内容时直接跳过,从根源解决awards.txt这类文件重复写入条目的问题
- 移除了编译非必需的冗余头文件,修复了原有存档选择分支的潜在崩溃问题,优化了文件扫描的执行效率
#include <DynRPG/DynRPG.h> #include <fstream> #include <cstdio> #include <string> #include <map> using namespace std; std::map<std::string, std::string> configuration; bool onStartup(char *pluginName) { // 从DynRPG.ini加载插件配置 configuration = RPG::loadConfiguration(pluginName); return true; } bool onComment( const char* text, const RPG::ParsedCommentData* parsedData, RPG::EventScriptLine* nextScriptLine, RPG::EventScriptData* scriptData, int eventId, int pageId, int lineId, int* nextLineId ) { std::string cmd = parsedData->command; std::string filecode; std::string scanfile; string FileName; // 写入文本到固定文件dynstore.txt,自动去重 if(!cmd.compare("file_string")) { std::string writeContent = parsedData->parameters[0].text; bool isDuplicate = false; // 先读取现有内容检查重复 ifstream checkFile("dynstore.txt"); if(checkFile.good()) { std::string line; while(std::getline(checkFile, line)) { if(line == writeContent) { isDuplicate = true; break; } } } checkFile.close(); // 无重复内容时执行写入 if(!isDuplicate) { ofstream myfile("dynstore.txt", ios::app); myfile << writeContent << "\n"; myfile.close(); } return false; } // 写入指定角色名到固定文件dynstore.txt,自动去重 if(!cmd.compare("store_file")) { int actorId = parsedData->parameters[0].number; std::string writeContent = RPG::actors[actorId]->name; bool isDuplicate = false; // 先读取现有内容检查重复 ifstream checkFile("dynstore.txt"); if(checkFile.good()) { std::string line; while(std::getline(checkFile, line)) { if(line == writeContent) { isDuplicate = true; break; } } } checkFile.close(); // 无重复内容时执行写入 if(!isDuplicate) { ofstream myfile("dynstore.txt", ios::app); myfile << writeContent << "\n"; myfile.close(); } return false; } // 从固定文件dynstore.txt读取第一行,赋值给指定角色名 if(!cmd.compare("load_file")) { ifstream myfile("dynstore.txt"); if(myfile.good()) { getline(myfile, filecode); RPG::actors[parsedData->parameters[0].number]->name = filecode; } myfile.close(); return false; } // 写入文本到自定义路径文件,自动去重 if(!cmd.compare("create_file")) { FileName = parsedData->parameters[0].text; std::string writeContent = parsedData->parameters[1].text; bool isDuplicate = false; // 先读取现有内容检查重复 ifstream checkFile(FileName.c_str()); if(checkFile.good()) { std::string line; while(std::getline(checkFile, line)) { if(line == writeContent) { isDuplicate = true; break; } } } checkFile.close(); // 无重复内容时执行写入 if(!isDuplicate) { ofstream myfile(FileName.c_str(), ios::app); myfile << writeContent << "\n"; myfile.close(); } return false; } // 写入文本到当前选中的存档对应文件,自动去重 if(!cmd.compare("create_save")) { int GameFile = RPG::variables[3351]; std::string targetPath; // 匹配对应存档路径 if(GameFile == 0) { targetPath = parsedData->parameters[0].text; } else if(GameFile >=1 && GameFile <=6) { char tempPath[20]; sprintf(tempPath, "Save0%d.txt", GameFile); targetPath = tempPath; } else { // 无效存档编号直接跳过 return false; } std::string writeContent = parsedData->parameters[1].text; bool isDuplicate = false; // 先读取现有内容检查重复 ifstream checkFile(targetPath.c_str()); if(checkFile.good()) { std::string line; while(std::getline(checkFile, line)) { if(line == writeContent) { isDuplicate = true; break; } } } checkFile.close(); // 无重复内容时执行写入 if(!isDuplicate) { ofstream myfile(targetPath.c_str(), ios::app); myfile << writeContent << "\n"; myfile.close(); } return false; } // 写入指定角色名到自定义路径文件,自动去重 if(!cmd.compare("make_file")) { FileName = parsedData->parameters[0].text; int actorId = parsedData->parameters[1].number; std::string writeContent = RPG::actors[actorId]->name; bool isDuplicate = false; // 先读取现有内容检查重复 ifstream checkFile(FileName.c_str()); if(checkFile.good()) { std::string line; while(std::getline(checkFile, line)) { if(line == writeContent) { isDuplicate = true; break; } } } checkFile.close(); // 无重复内容时执行写入 if(!isDuplicate) { ofstream myfile(FileName.c_str(), ios::app); myfile << writeContent; myfile.close(); } return false; } // 从自定义路径文件读取第一行,赋值给指定角色名 if(!cmd.compare("read_file")) { FileName = parsedData->parameters[0].text; ifstream myfile(FileName.c_str()); if(myfile.good()) { getline(myfile, filecode); RPG::actors[parsedData->parameters[1].number]->name = filecode; } myfile.close(); return false; } // 删除目标文件,文件不存在时直接跳过,不会新建 if(!cmd.compare("clear_file")) { FileName = parsedData->parameters[0].text; remove(FileName.c_str()); return false; } // 扫描当前选中的存档对应文件,找到目标字符串后打开指定开关 if(!cmd.compare("scan_save")) { int GameFile = RPG::variables[3351]; std::string targetPath; // 匹配对应存档路径 if(GameFile == 0) { targetPath = parsedData->parameters[0].text; } else if(GameFile >=1 && GameFile <=6) { char tempPath[20]; sprintf(tempPath, "Save0%d.txt", GameFile); targetPath = tempPath; } else { return false; } scanfile = parsedData->parameters[1].text; int switchnum = parsedData->parameters[2].number; ifstream myfile(targetPath.c_str()); if(myfile.good()) { std::string temp; while(std::getline(myfile, temp)) { if(temp.find(scanfile) != std::string::npos) { RPG::switches[switchnum] = true; break; } } } myfile.close(); return false; } // 扫描自定义路径文件,找到目标字符串后打开指定开关 if(!cmd.compare("scan_file")) { FileName = parsedData->parameters[0].text; scanfile = parsedData->parameters[1].text; int switchnum = parsedData->parameters[2].number; ifstream myfile(FileName.c_str()); if(myfile.good()) { std::string temp; while(std::getline(myfile, temp)) { if(temp.find(scanfile) != std::string::npos) { RPG::switches[switchnum] = true; break; } } } myfile.close(); return false; } // 检查目标文件是否存在,存在打开指定开关,不存在关闭 if(!cmd.compare("file_good")) { FileName = parsedData->parameters[0].text; ifstream myfile(FileName.c_str()); int switchnum = parsedData->parameters[1].number; RPG::switches[switchnum] = myfile.good(); myfile.close(); return false; } return true; }
内容的提问来源于stack exchange,提问作者Samantha
相关产品推荐
相关产品推荐

