-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_watcher.h
More file actions
128 lines (111 loc) · 4.16 KB
/
Copy pathfile_watcher.h
File metadata and controls
128 lines (111 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include "globals.h"
/**
* Unity 프로젝트 폴더의 파일 변경사항을 실시간으로 감지 <br/>
* Windows ReadDirectoryChangesW API 사용
*/
class FileWatcher {
private:
// 감시 중인 프로젝트 정보
struct WatchedProject {
std::string projectPath; // 프로젝트 경로
std::string projectName; // 프로젝트 이름
std::string unityVersion; // 유니티 버전
HANDLE directoryHandle; // 디렉토리 핸들
std::thread watchThread; // 감시 스레드
std::atomic<bool> shouldStop; // 스레드 종료 플래그
OVERLAPPED overlapped; // 비동기 I/O용 구조체
char buffer[4096]; // 변경 정보를 받을 버퍼
HANDLE stopEvent;
HANDLE ioEvent;
WatchedProject() :
directoryHandle(INVALID_HANDLE_VALUE),
shouldStop(false),
stopEvent(nullptr),
ioEvent(nullptr)
{
stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
ioEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
ZeroMemory(&overlapped, sizeof(OVERLAPPED));
overlapped.hEvent = ioEvent;
ZeroMemory(buffer, sizeof(buffer));
}
~WatchedProject() {
if (stopEvent != nullptr) {
CloseHandle(stopEvent);
stopEvent = nullptr;
}
if (ioEvent != nullptr) {
CloseHandle(ioEvent);
ioEvent = nullptr;
}
if (directoryHandle != nullptr && directoryHandle != INVALID_HANDLE_VALUE) {
CloseHandle(directoryHandle);
directoryHandle = INVALID_HANDLE_VALUE;
}
}
};
std::vector<std::unique_ptr<WatchedProject>> watchedProjects;
mutable std::mutex projectsMutex; // 스레드 안전성을 위한 뮤텍스
// 파일 변경 이벤트 콜백 함수
std::function<void(const FileChangeEvent&)> changeCallback;
/**
* 특정 프로젝트 폴더를 감시하는 워커 스레드 함수
* @param project 감시할 프로젝트 정보
*/
void WatchProjectThread(WatchedProject* project);
/**
* ReadDirectoryChangesW로부터 받은 데이터를 파싱
* @param buffer 변경 정보 버퍼
* @param bytesReturned 받은 데이터 크기
* @param project 해당 프로젝트 정보
*/
void ProcessFileChanges(char* buffer, DWORD bytesReturned, WatchedProject* project);
/**
* 파일이 Unity 관련 파일인지 확인
* @param fileName 파일 이름
* @return Unity 파일이면 true
*/
bool IsUnityFile(const std::string& fileName) const;
/**
* 무시해야 할 폴더인지 확인
* @param folderName 폴더 이름
* @return 무시해야 하면 true
*/
bool ShouldIgnoreFolder(const std::string& folderName) const;
public:
FileWatcher();
~FileWatcher();
/**
* 파일 변경 이벤트 콜백 함수 설정
* @param callback 파일이 변경될 때 호출될 함수
*/
void SetChangeCallback(std::function<void(const FileChangeEvent&)> callback);
/**
* Unity 프로젝트 감시 시작
* @param projectPath 감시할 프로젝트 경로
* @param projectName 프로젝트 이름
* @param unityVersion 유니티 에디터 버전
* @return 성공하면 true
*/
bool StartWatching(const std::string& projectPath, const std::string& projectName, const std::string& unityVersion);
/**
* 특정 프로젝트 감시 중지
* @param projectPath 중지할 프로젝트 경로
*/
void StopWatching(const std::string& projectPath);
/**
* 모든 프로젝트 감시 중지
*/
void StopAllWatching();
/**
* 현재 감시 중인 프로젝트 수 반환
* @return 감시 중인 프로젝트 개수
*/
size_t GetWatchedProjectCount() const;
/**
* 감시 중인 모든 프로젝트 경로 반환
* @return 프로젝트 경로들
*/
std::vector<WatchedProjectInfo> GetWatchedProjects() const;
};