Leviathan  0.8.0.0
Leviathan game engine
ResourceRefreshHandler.h
Go to the documentation of this file.
1 #pragma once
2 // ------------------------------------ //
3 #include "Define.h"
4 // ------------------------------------ //
5 #include "Common/ThreadSafe.h"
6 #include <functional>
7 #include <thread>
8 #include "../TimeIncludes.h"
9 #include <vector>
10 
11 #ifdef _WIN32
12 #include "WindowsInclude.h"
13 #endif
14 
15 namespace Leviathan{
16 
20 public:
23  ResourceFolderListener(const std::vector<const std::string*> &filestowatch,
24  std::function<void (const std::string &, ResourceFolderListener&)> notifyfunction);
26 
28  DLLEXPORT int GetID() const;
29 
32  bool StartListening();
33 
34 
36  void StopThread();
37 
40  void CheckUpdatesEnded();
41 
46 
48  DLLEXPORT bool IsAFileStillUpdated() const;
49 
50 protected:
51 
52  void _RunListeningThread();
53  // ------------------------------------ //
54 
56  std::thread ListenerThread;
57 
59  std::string TargetFolder;
60 
62  std::vector<std::unique_ptr<std::string>> ListenedFiles;
63 
65  std::vector<bool> UpdatedFiles;
66 
68  bool ShouldQuit = false;
69 
71  int ID;
72 
74  std::function<void (const std::string &, ResourceFolderListener&)> CallbackFunction;
75 
76 #ifdef _WIN32
77  // Windows specific listener resources
78 
82  std::vector<HANDLE> SignalingHandles;
83 
84 
85  FILE_NOTIFY_INFORMATION* OurReadBuffer = nullptr;
86 
87  HANDLE TargetFolderHandle;
88 
89  OVERLAPPED* OverlappedInfo = nullptr;
90 
91 #else
92  // inotify should do the trick on linux //
93 
95  int InotifyID = -1;
96 
98  int InotifyWatches = -1;
99 
101  char* ReadBuffer = nullptr;
102 
103 
104 #endif // _WIN32
105 
106 };
107 
108 
109 
110 
118 public:
121 
122  DLLEXPORT virtual bool Init();
123  DLLEXPORT virtual void Release();
124 
125 
137  DLLEXPORT bool ListenForFileChanges(const std::vector<const std::string*> &filestowatch,
138  std::function<void (const std::string &, ResourceFolderListener&)> notifyfunction,
139  int &createdid);
140 
145  DLLEXPORT void StopListeningForFileChanges(int idoflistener);
146 
147 
149  DLLEXPORT void CheckFileStatus();
150 
151 
153  DLLEXPORT void MarkListenersAsNotUpdated(const std::vector<int> &ids);
154 
155 
157 
158 protected:
159 
161  std::vector<std::unique_ptr<ResourceFolderListener>> ActiveFileListeners;
162 
163 
164  WantedClockType::time_point NextUpdateTime;
165 
167 
168 };
169 
170 }
171 
std::thread ListenerThread
The listening thread.
bool StartListening()
Starts a listening thread.
void CheckUpdatesEnded()
Checks if files marked as updated (only the first is actually checked) are available for reading.
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
DLLEXPORT void CheckFileStatus()
Called by Engine to check are updated files available.
DLLEXPORT int GetID() const
Gets the ID of this object.
DLLEXPORT void StopListeningForFileChanges(int idoflistener)
Stops a listener with a specific id.
int InotifyID
The ID of our inotify instance thing.
WantedClockType::time_point NextUpdateTime
void StopThread()
Sets the internal thread to die and signals it.
ResourceFolderListener(const std::vector< const std::string * > &filestowatch, std::function< void(const std::string &, ResourceFolderListener &)> notifyfunction)
Creates a new listener.
Allows various resource loaders to get notified when the file on disk changes.
std::vector< std::unique_ptr< ResourceFolderListener > > ActiveFileListeners
Holds all the active listeners.
DLLEXPORT void MarkAllAsNotUpdated()
Marks all files as not updated.
std::string TargetFolder
The folder in which to listen for stuff.
DLLEXPORT bool ListenForFileChanges(const std::vector< const std::string * > &filestowatch, std::function< void(const std::string &, ResourceFolderListener &)> notifyfunction, int &createdid)
Starts listening for changes made to filestowatch.
A file listener instance which listens for file changes in a folder.
DLLEXPORT void MarkListenersAsNotUpdated(const std::vector< int > &ids)
Marks all files in all listeners matching any of the IDs as not updated.
char * ReadBuffer
The read result buffer.
bool ShouldQuit
Property set when quitting.
#define DLLEXPORT
Definition: Include.h:84
std::vector< bool > UpdatedFiles
Marks the files that have been updated.
int ID
ID used to find this specific object.
std::vector< std::unique_ptr< std::string > > ListenedFiles
The files which are listened for.
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT bool IsAFileStillUpdated() const
Checks whether a file is still marked as updated.
int InotifyWatches
Inotify's folder id which is being monitored.
static DLLEXPORT ResourceRefreshHandler * Get()
std::function< void(const std::string &, ResourceFolderListener &)> CallbackFunction
The function called when a change is detected.
static ResourceRefreshHandler * Staticaccess