it works!! mostly
This commit is contained in:
@@ -15,123 +15,134 @@ typedef void (*Shutdown)();
|
||||
constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1;
|
||||
|
||||
#ifdef VISUAL_STUDIO
|
||||
const char* DLLPath = "PuzGame2.dll";
|
||||
constexpr char* DLLPath = "PuzGame.dll";
|
||||
constexpr wchar_t* DLLWatch = L"PuzGame2.dll";
|
||||
#else
|
||||
const char* DLLPath = "libPuzGame.dll";
|
||||
constexpr char* DLLPath = "libPuzGame.dll";
|
||||
constexpr wchar_t* DLLWatch = L"libPuzGame2.dll";
|
||||
#endif
|
||||
|
||||
struct FileWatcherData
|
||||
{
|
||||
bool Change = false;
|
||||
bool Change = false;
|
||||
};
|
||||
|
||||
struct DevelopmentData
|
||||
{
|
||||
uint8_t FileChangeBuffer[1024]{0};
|
||||
HMODULE GameLib = NULL;
|
||||
HANDLE hDevDir = NULL;
|
||||
FileWatcherData FileWatcher;
|
||||
uint8_t FileChangeBuffer[1024]{ 0 };
|
||||
HMODULE GameLib = NULL;
|
||||
HANDLE hDevDir = NULL;
|
||||
FileWatcherData FileWatcher;
|
||||
};
|
||||
|
||||
namespace
|
||||
{
|
||||
DevelopmentData DevData;
|
||||
Startup StartupFunc;
|
||||
Update UpdateFunc;
|
||||
Shutdown ShutdownFunc;
|
||||
DevelopmentData DevData;
|
||||
Startup StartupFunc;
|
||||
Update UpdateFunc;
|
||||
Shutdown ShutdownFunc;
|
||||
}
|
||||
|
||||
unsigned long FileWatcherThread(void* data)
|
||||
{
|
||||
DWORD bytesReturned = 0;
|
||||
bool isRunning = DevData.hDevDir;
|
||||
while (isRunning)
|
||||
{
|
||||
bx::memSet(DevData.FileChangeBuffer, 0, sizeof(DevData.FileChangeBuffer));
|
||||
if (ReadDirectoryChangesW(DevData.hDevDir, DevData.FileChangeBuffer, sizeof(DevData.FileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL))
|
||||
{
|
||||
uint32_t offset = 0;
|
||||
while (true)
|
||||
{
|
||||
FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&DevData.FileChangeBuffer[offset]);
|
||||
wprintf(L"Change: %ls\n", notifyData->FileName);
|
||||
DWORD bytesReturned = 0;
|
||||
bool isRunning = DevData.hDevDir;
|
||||
while (isRunning)
|
||||
{
|
||||
bx::memSet(DevData.FileChangeBuffer, 0, sizeof(DevData.FileChangeBuffer));
|
||||
if (ReadDirectoryChangesW(DevData.hDevDir, DevData.FileChangeBuffer, sizeof(DevData.FileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL))
|
||||
{
|
||||
uint32_t offset = 0;
|
||||
while (true)
|
||||
{
|
||||
FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&DevData.FileChangeBuffer[offset]);
|
||||
wprintf(L"Change: %ls\n", notifyData->FileName);
|
||||
|
||||
if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME)
|
||||
{
|
||||
if (wcscmp(notifyData->FileName, L"libPuzGame2.dll") == 0)
|
||||
{
|
||||
DevData.FileWatcher.Change = true;
|
||||
printf("detected dll change!\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (notifyData->NextEntryOffset == 0) break;
|
||||
offset += notifyData->NextEntryOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("File watcher thread ended!\n");
|
||||
return 0;
|
||||
if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME)
|
||||
{
|
||||
if (wcscmp(notifyData->FileName, DLLWatch) == 0)
|
||||
{
|
||||
DevData.FileWatcher.Change = true;
|
||||
printf("detected dll change!\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (notifyData->NextEntryOffset == 0) break;
|
||||
offset += notifyData->NextEntryOffset;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("File watcher thread ended!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ReloadDLL()
|
||||
{
|
||||
if (DevData.GameLib != NULL)
|
||||
{
|
||||
FreeLibrary(DevData.GameLib);
|
||||
}
|
||||
if (!CopyFile("cmake-build\\libPuzGame2.dll", "cmake-build\\libPuzGame.dll", false))
|
||||
{
|
||||
printf("Failed to copy game DLL!\n");
|
||||
}
|
||||
|
||||
HMODULE gameLibReloaded = LoadLibraryEx(DLLPath, NULL, 0);
|
||||
if (gameLibReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load game DLL from %s!\n", DLLPath);
|
||||
return false;
|
||||
}
|
||||
DevData.GameLib = gameLibReloaded;
|
||||
|
||||
if (DevData.GameLib != NULL)
|
||||
{
|
||||
FreeLibrary(DevData.GameLib);
|
||||
}
|
||||
#ifdef VISUAL_STUDIO
|
||||
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z");
|
||||
if (!CopyFile("PuzGame2.dll", "PuzGame.dll", false))
|
||||
{
|
||||
printf("Failed to copy game DLL!\n");
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv");
|
||||
if (!CopyFile("cmake-build\\libPuzGame2.dll", "cmake-build\\libPuzGame.dll", false))
|
||||
{
|
||||
printf("Failed to copy game DLL!\n");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
if (StartupReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load startup function from game DLL!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
HMODULE gameLibReloaded = LoadLibraryEx(DLLPath, NULL, 0);
|
||||
if (gameLibReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load game DLL from %s!\n", DLLPath);
|
||||
return false;
|
||||
}
|
||||
DevData.GameLib = gameLibReloaded;
|
||||
|
||||
#ifdef VISUAL_STUDIO
|
||||
Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "?Update@Game@@YAXXZ");
|
||||
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z");
|
||||
#else
|
||||
Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "_ZN4Game6UpdateEv");
|
||||
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv");
|
||||
#endif
|
||||
if (UpdateReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load update function from game DLL!\n");
|
||||
return false;
|
||||
}
|
||||
if (StartupReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load startup function from game DLL!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef VISUAL_STUDIO
|
||||
Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "?Shutdown@Game@@YAXXZ");
|
||||
Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "?Update@Game@@YAXXZ");
|
||||
#else
|
||||
Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "_ZN4Game8ShutdownEv");
|
||||
Update UpdateReloaded = (Update)GetProcAddress(DevData.GameLib, "_ZN4Game6UpdateEv");
|
||||
#endif
|
||||
if (ShutdownReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load shutdown function from game DLL\n");
|
||||
return false;
|
||||
}
|
||||
if (UpdateReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load update function from game DLL!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
StartupFunc = StartupReloaded;
|
||||
UpdateFunc = UpdateReloaded;
|
||||
ShutdownFunc = ShutdownReloaded;
|
||||
#ifdef VISUAL_STUDIO
|
||||
Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "?Shutdown@Game@@YAXXZ");
|
||||
#else
|
||||
Shutdown ShutdownReloaded = (Shutdown)GetProcAddress(DevData.GameLib, "_ZN4Game8ShutdownEv");
|
||||
#endif
|
||||
if (ShutdownReloaded == NULL)
|
||||
{
|
||||
printf("Failed to load shutdown function from game DLL\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("Loaded Game DLL successfully!\n");
|
||||
return true;
|
||||
StartupFunc = StartupReloaded;
|
||||
UpdateFunc = UpdateReloaded;
|
||||
ShutdownFunc = ShutdownReloaded;
|
||||
|
||||
printf("Loaded Game DLL successfully!\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -142,6 +153,13 @@ int main()
|
||||
|
||||
if (!ReloadDLL()) return 1;
|
||||
|
||||
DevData.hDevDir = CreateFile(".",
|
||||
FILE_LIST_DIRECTORY,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
||||
NULL);
|
||||
DWORD fileWatcherThreadId = 0;
|
||||
CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user