/
ModelPopulator.h
157 lines (125 loc) · 4.17 KB
/
ModelPopulator.h
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once
#include "wxutil/VFSTreePopulator.h"
#include "wxutil/ModalProgressDialog.h"
#include "imainframe.h"
#include "iregistry.h"
#include "igame.h"
#include "EventRateLimiter.h"
#include "ifilesystem.h"
#include "i18n.h"
#include "string/string.h"
#include "os/path.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <fmt/format.h>
#include "ModelSelector.h"
#include "ModelDataInserter.h"
namespace ui
{
/**
* Threaded functor object to visit the global VFS and add model paths
* to a new TreeModel object. Fires a PopulationFinished event once
* its work is done.
*/
class ModelPopulator :
public wxThread
{
const ModelSelector::TreeColumns& _columns;
// The working copy to populate
wxutil::TreeModel::Ptr _treeStore;
// VFSTreePopulator to populate
wxutil::VFSTreePopulator _populator;
// Progress dialog and model count
std::size_t _count;
// Event rate limiter for progress dialog
EventRateLimiter _evLimiter;
std::set<std::string> _allowedExtensions;
// The event handler to notify on completion
wxEvtHandler* _finishedHandler;
class ThreadAbortedException : public std::exception
{};
public:
// Constructor sets the populator
ModelPopulator(const ModelSelector::TreeColumns& columns,
wxEvtHandler* finishedHandler) :
wxThread(wxTHREAD_JOINABLE),
_columns(columns),
_treeStore(new wxutil::TreeModel(_columns)),
_populator(_treeStore),
//_progress(_("Loading models")),
_count(0),
_evLimiter(50),
_finishedHandler(finishedHandler)
{
//_progress.setText(_("Searching"));
// Load the allowed extensions
std::string extensions = GlobalGameManager().currentGame()->getKeyValue("modeltypes");
boost::algorithm::split(_allowedExtensions, extensions, boost::algorithm::is_any_of(" "));
}
~ModelPopulator()
{
// We might have a running thread, wait for it
if (IsRunning())
{
Delete();
}
}
// Thread entry point
ExitCode Entry()
{
try
{
// Search for model files
GlobalFileSystem().forEachFile(MODELS_FOLDER,
"*",
[&](const std::string& filename) { visitModelFile(filename); },
0);
if (TestDestroy()) return static_cast<wxThread::ExitCode>(0);
reportProgress(_("Building tree..."));
// Fill in the column data (TRUE = including skins)
ModelDataInserter inserterSkins(_columns, true);
_populator.forEachNode(inserterSkins);
if (TestDestroy()) return static_cast<wxThread::ExitCode>(0);
// Sort the model before returning it
_treeStore->SortModelFoldersFirst(_columns.filename, _columns.isFolder);
if (!TestDestroy())
{
// Send the event to our listener, only if we are not forced to finish
wxQueueEvent(_finishedHandler, new wxutil::TreeModel::PopulationFinishedEvent(_treeStore));
}
return static_cast<wxThread::ExitCode>(0);
}
catch (ThreadAbortedException)
{
return static_cast<wxThread::ExitCode>(0);
}
}
void visitModelFile(const std::string& file)
{
if (TestDestroy())
{
throw ThreadAbortedException();
}
std::string ext = os::getExtension(file);
boost::algorithm::to_lower(ext);
// Test the extension. If it is not matching any of the known extensions,
// not interested
if (_allowedExtensions.find(ext) != _allowedExtensions.end())
{
_count++;
_populator.addPath(file);
if (_evLimiter.readyForEvent())
{
reportProgress(fmt::format(_("{0:d} models loaded"), _count));
}
}
}
void reportProgress(const std::string& message)
{
wxQueueEvent(_finishedHandler, new wxutil::TreeModel::PopulationProgressEvent(
fmt::format(_("{0:d} models loaded"), _count)));
}
};
}