Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/MapGenerator.cpp
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
CMapGeneratorWorker::~CMapGeneratorWorker() | CMapGeneratorWorker::~CMapGeneratorWorker() | ||||
{ | { | ||||
// Cancel or wait for the task to end. | // Cancel or wait for the task to end. | ||||
m_WorkerThread.CancelOrWait(); | m_WorkerThread.CancelOrWait(); | ||||
} | } | ||||
void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::string& settings) | void CMapGeneratorWorker::FutureFunction::operator ()() | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(m_WorkerMutex); | |||||
// Set progress to positive value | |||||
m_Progress = 1; | |||||
m_ScriptPath = scriptFile; | |||||
m_Settings = settings; | |||||
// Start generating the map asynchronously. | |||||
m_WorkerThread = Threading::TaskManager::Instance().PushTask([this]() { | |||||
PROFILE2("Map Generation"); | PROFILE2("Map Generation"); | ||||
std::shared_ptr<ScriptContext> mapgenContext = ScriptContext::CreateContext(RMS_CONTEXT_SIZE); | std::shared_ptr<ScriptContext> mapgenContext = ScriptContext::CreateContext(RMS_CONTEXT_SIZE); | ||||
// Enable the script to be aborted | // Enable the script to be aborted | ||||
JS_AddInterruptCallback(mapgenContext->GetGeneralJSContext(), MapGeneratorInterruptCallback); | JS_AddInterruptCallback(mapgenContext->GetGeneralJSContext(), MapGeneratorInterruptCallback); | ||||
m_ScriptInterface = new ScriptInterface("Engine", "MapGenerator", mapgenContext); | self.m_ScriptInterface = new ScriptInterface("Engine", "MapGenerator", mapgenContext); | ||||
// Run map generation scripts | // Run map generation scripts | ||||
if (!Run() || m_Progress > 0) | if (!self.Run() || self.m_Progress > 0) | ||||
{ | { | ||||
// Don't leave progress in an unknown state, if generator failed, set it to -1 | // Don't leave progress in an unknown state, if generator failed, set it to -1 | ||||
std::lock_guard<std::mutex> lock(m_WorkerMutex); | std::lock_guard<std::mutex> lock(self.m_WorkerMutex); | ||||
m_Progress = -1; | self.m_Progress = -1; | ||||
} | } | ||||
SAFE_DELETE(m_ScriptInterface); | SAFE_DELETE(self.m_ScriptInterface); | ||||
// At this point the random map scripts are done running, so the thread has no further purpose | // At this point the random map scripts are done running, so the thread has no further purpose | ||||
// and can die. The data will be stored in m_MapData already if successful, or m_Progress | // and can die. The data will be stored in m_MapData already if successful, or m_Progress | ||||
// will contain an error value on failure. | // will contain an error value on failure. | ||||
}); | } | ||||
void CMapGeneratorWorker::Initialize(const VfsPath& scriptFile, const std::string& settings) | |||||
{ | |||||
std::lock_guard<std::mutex> lock(m_WorkerMutex); | |||||
// Set progress to positive value | |||||
m_Progress = 1; | |||||
m_ScriptPath = scriptFile; | |||||
m_Settings = settings; | |||||
// Start generating the map asynchronously. | |||||
m_WorkerThread = Threading::TaskManager::Instance().PushTask(FutureFunction{*this}); | |||||
} | } | ||||
bool CMapGeneratorWorker::Run() | bool CMapGeneratorWorker::Run() | ||||
{ | { | ||||
ScriptRequest rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
// Parse settings | // Parse settings | ||||
JS::RootedValue settingsVal(rq.cx); | JS::RootedValue settingsVal(rq.cx); | ||||
▲ Show 20 Lines • Show All 307 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator