-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
StartNelsonUserModules.cpp
79 lines (79 loc) · 3.19 KB
/
StartNelsonUserModules.cpp
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
//=============================================================================
// Copyright (c) 2016-present Allan CORNET (Nelson)
//=============================================================================
// This file is part of the Nelson.
//=============================================================================
// LICENCE_BLOCK_BEGIN
// SPDX-License-Identifier: LGPL-3.0-or-later
// LICENCE_BLOCK_END
//=============================================================================
#include <boost/filesystem.hpp>
#include "StartNelsonUserModules.hpp"
#include "CloseAllFiles.hpp"
#include "EvaluateScriptFile.hpp"
#include "Interface.hpp"
#include "ReadUserModules.hpp"
#include "ModulesManager.hpp"
#include "NelsonConfiguration.hpp"
//=============================================================================
namespace Nelson {
//=============================================================================
static void
printError(Interface* io, const std::wstring& message)
{
if (io != nullptr) {
io->warningMessage(message);
} else {
const wchar_t* format = L"%s\n";
fwprintf(stdout, format, message.c_str());
}
}
//=============================================================================
bool
StartNelsonUserModules(Evaluator* eval)
{
std::vector<std::tuple<std::wstring, std::wstring, bool>> userModules;
bool bOK = ReadUserModules(userModules);
if (bOK) {
Context* ctx = eval->getContext();
if (ctx != nullptr) {
for (auto element : userModules) {
std::wstring name = std::get<0>(element);
if (IsExistingModuleName(name)) {
UnregisterModule(name);
Interface* io = eval->getInterface();
std::wstring msg = _W("Module already loaded: ") + name;
printError(io, msg);
} else {
std::wstring path = std::get<1>(element);
bool load = std::get<2>(element);
if (load) {
std::wstring loader = path + std::wstring(L"/loader.m");
try {
EvaluateScriptFile(eval, loader, true);
} catch (const Exception& e) {
if (IsExistingModuleName(name)) {
UnregisterModule(name);
}
CloseAllFiles();
Interface* io = eval->getInterface();
eval->setLastErrorException(e);
std::wstring errmsg = _W("Impossible to load module: ") + name;
printError(io, errmsg);
}
}
}
}
}
} else {
if (!userModules.empty()) {
Interface* io = eval->getInterface();
std::wstring msg = _W("Impossible to read modules.json");
printError(io, msg);
}
}
return false;
}
//=============================================================================
} // namespace Nelson
//=============================================================================