Skip to content

Instantly share code, notes, and snippets.

@styblope
Last active June 25, 2024 22:19
Show Gist options
  • Save styblope/fced9f3ee475eafc887a6745083d2073 to your computer and use it in GitHub Desktop.
Save styblope/fced9f3ee475eafc887a6745083d2073 to your computer and use it in GitHub Desktop.
src/lua/Task.cpp: Load new task in XCSoar's natiove .tsk data format
// Load task in XCSoar's .tsk format
static int
l_load_task(lua_State *L)
{
if (lua_gettop(L) != 1)
return luaL_error(L, "Invalid parameters");
size_t l;
const auto doc = luaL_checklstring(L, 1, &l);
if (l == 0) return 0;
const auto xml_root = XML::ParseString(doc);
const ConstDataNodeXML root(xml_root);
// Create a blank task
const auto &settings = CommonInterface::GetComputerSettings();
auto task = std::make_unique<OrderedTask>(settings.task);
LoadTask(*task, root, data_components->waypoints.get());
/*task->GetFactory().CheckAddFinish();*/
task->UpdateStatsGeometry();
const auto errors = task->CheckTask();
if (!task->TaskSize() || !IsError(errors)) {
ScopeSuspendAllThreads suspend;
task->CheckDuplicateWaypoints(*data_components->waypoints);
data_components->waypoints->Optimise();
}
backend_components->protected_task_manager->TaskCommit(*task);
try {
backend_components->protected_task_manager->TaskSaveDefault();
} catch (...) {
ShowError(std::current_exception(), _("Failed to save task file."));
return false;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment