Finality Engine
Toggle main menu visibility
Loading...
Searching...
No Matches
AssetChangeMonitor.h
Go to the documentation of this file.
1
6
#pragma once
7
#include <filesystem>
8
#include <future>
9
#include <vector>
10
#include <chrono>
11
#include <algorithm>
12
13
namespace
FINALITY
14
{
16
class
AssetChangeMonitor
17
{
19
struct
Stamp
20
{
21
std::filesystem::path Path;
22
std::filesystem::file_time_type Time;
23
uintmax_t Size;
24
bool
operator==(
const
Stamp&)
const
=
default
;
25
};
27
struct
Result { std::filesystem::path Root; std::vector<Stamp> Files;
bool
Valid=
true
; };
28
std::filesystem::path m_Root;
29
std::vector<Stamp> m_Previous;
30
std::future<Result> m_Work;
31
bool
m_Initialized=
false
;
32
std::chrono::steady_clock::time_point m_Next{};
33
public
:
34
bool
Poll
(
const
std::filesystem::path& root,
float
interval)
35
{
36
using namespace
std::chrono;
37
if
(root!=m_Root) { m_Root=root; m_Previous.clear(); m_Initialized=
false
; m_Next={}; }
38
bool
changed=
false
;
39
if
(m_Work.valid() && m_Work.wait_for(seconds(0))==std::future_status::ready)
40
{
41
auto
result=m_Work.get();
42
if
(result.Root==m_Root && result.Valid)
43
{ changed=m_Initialized && result.Files!=m_Previous; m_Previous=std::move(result.Files); m_Initialized=
true
; }
44
}
45
const
auto
now=steady_clock::now();
46
if
(!root.empty() && !m_Work.valid() && now>=m_Next)
47
{
48
m_Next=now+duration_cast<steady_clock::duration>(duration<float>(std::max(interval,0.25f)));
49
m_Work=std::async(std::launch::async,[root]
50
{
51
Result result; result.Root=root;
52
try
53
{
54
for
(
const
auto
& entry:std::filesystem::recursive_directory_iterator(root,std::filesystem::directory_options::skip_permission_denied))
55
{
56
if
(!entry.is_regular_file())
continue
;
57
const
auto
extension=entry.path().extension();
if
(extension==
".spv"
|| extension==
".tmp"
|| extension==
".ini"
)
continue
;
58
result.Files.push_back({entry.path(),entry.last_write_time(),entry.file_size()});
59
}
60
std::sort(result.Files.begin(),result.Files.end(),[](
const
Stamp& a,
const
Stamp& b){return a.Path<b.Path;});
61
}
62
catch
(
const
std::filesystem::filesystem_error&) { result.Valid=
false
; }
63
return
result;
64
});
65
}
66
return
changed;
67
}
68
};
69
}
FINALITY::AssetChangeMonitor
Definition
AssetChangeMonitor.h:17
FINALITY::AssetChangeMonitor::Poll
bool Poll(const std::filesystem::path &root, float interval)
Definition
AssetChangeMonitor.h:34
FINALITY
Definition
Asset.h:12
FinalityEditor
src
Editor
AssetChangeMonitor.h
Generated by
1.18.0