windows下檢測檔案改變,windows檢測檔案
這個主要是應用在我前一篇部落格裡提到的指令碼熱載入功能。主要實現的功能檢測檔案夾內檔案的變化(改變、新增、刪除、重新命名),當發現改變的時候通知lua重新載入指令碼。基本上就是一個windows api的使用。實際應用中會有一些細節需要注意,比如我習慣使用sublime text編輯,而sublime text儲存檔案不是直接改變檔案內容,而是新增一個檔案。這些細節情況需要實際使用中微調。
代碼如下:
#include "FileWatcher.h"#include "cocos2d.h"#include "CCLuaEngine.h"using namespace cocos2d;#ifdef WIN32// 函數: WatchChanges(LPVOID lpParameter) // // 目的: 監控目錄的程式 // // 注釋:主函數建立線程時制定了這個函數的入口 // 屆時該子程式將自動啟動執行。 // 備忘:因為代碼不全,看下面的代碼時,主要參考紅色的字型部分 static int lastChangeTime = 0;void reloadGame(){int time = GetTickCount();if (time - lastChangeTime <= 1000) {// 忽略短時間內的重新載入請求return;}Director::getInstance()->getScheduler()->performFunctionInCocosThread([](){auto engine = LuaEngine::getInstance();ScriptEngineManager::getInstance()->setScriptEngine(engine);lua_State* L = engine->getLuaStack()->getLuaState();lua_getglobal(L, "reloadGame");lua_call(L, 0, 0);});}DWORD WINAPI WatchChanges(LPVOID lpParameter)//返回版本資訊 {wchar_t watchDirectory[512] = {0};MultiByteToWideChar(CP_ACP, 0, (char*)lpParameter, strlen((char*)lpParameter), watchDirectory, sizeof(watchDirectory) / sizeof(wchar_t));//建立一個目錄控制代碼 HANDLE handle_directory=CreateFile(watchDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);if(handle_directory==INVALID_HANDLE_VALUE) { DWORD ERROR_DIR=GetLastError();CCLOG("Open Directory Error");} BOOL watch_state; while (TRUE) {char buffer[1024] = {0};DWORD bytesReturned = 0;FILE_NOTIFY_INFORMATION* notify = (FILE_NOTIFY_INFORMATION*)buffer;watch_state=ReadDirectoryChangesW(handle_directory, (LPVOID)buffer, sizeof(buffer), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE, (LPDWORD)&bytesReturned, NULL, NULL);int err = GetLastError();if (err == ERROR_INVALID_FUNCTION || err == ERROR_NOTIFY_ENUM_DIR) {return -1;} if(watch_state != 0) {DWORD length=WideCharToMultiByte(0,0,notify->FileName,-1,NULL,0,NULL,NULL); char fileName[256] = {0};WideCharToMultiByte(0,0,notify->FileName,-1,fileName,length,NULL,NULL); //這裡主要就是檢測返回的資訊,需要注意FILE_NOTIFY_INFORMATION結構體的定義,以便正確調用返回資訊if (notify->Action==FILE_ACTION_ADDED) {CCLOG("file add: %s", fileName);// sublime 修改檔案是新增一個臨時檔案,這個是相容措施reloadGame();} if (notify->Action==FILE_ACTION_REMOVED) {CCLOG("file delete: %s", fileName);} if (notify->Action==FILE_ACTION_MODIFIED) { CCLOG("file changed: %s", fileName);reloadGame();} //對於下面兩種情況,Action本身也是檔案名稱(可能是old_name也可能是new_name) if (notify->Action==FILE_ACTION_RENAMED_OLD_NAME) {CCLOG("file rename old name: %s", fileName); } if (notify->Action==FILE_ACTION_RENAMED_NEW_NAME) {CCLOG("file rename new name: %s", fileName);}} Sleep(500); } return 0; }#endifvoid startWatch(const char* path){#ifdef WIN32static std::string s_path = path;//建立一個線程專門用於監控檔案變化 HANDLE TrheadWatch=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WatchChanges,(void*)s_path.c_str(),0,NULL); CloseHandle(TrheadWatch);#endif}
註冊給lua的實現:
#include "lua_cutil.h"//#include <conio.h>#include <stdlib.h>#include <ctype.h>#include <stdio.h>#include <string>#include "FileWatcher.h"#include <lua.h>#include <lauxlib.h>int lua_cutil_watch(lua_State *pL){std::string path = luaL_checkstring(pL, 1);startWatch(path.c_str());return 0;}int luaopen_cutil(lua_State *pL){// 註冊lua函數luaL_Reg reg [] = {{"watch", lua_cutil_watch},{NULL, NULL}};luaL_register(pL, "cutil", reg);return 1;}
lua中調用的方式:
local mainPath = cc.FileUtils:getInstance():fullPathForFilename('Main.lua'); mainPath = string.sub(mainPath, 1, string.find(mainPath, 'Main%.lua') - 1); print(mainPath); cutil.watch(mainPath);