-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionManager.cpp
74 lines (65 loc) · 2.46 KB
/
FunctionManager.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
#include "FunctionManager.h"
/**
* \brief Helper for managing dynamic functions.
*/
FunctionManager::FunctionManager()
{
functionContainer["MoveToXY"] = genericTemplate(ScriptableFunctions::MoveToXY);
functionContainer["PrintDialogue"] = genericTemplate(ScriptableFunctions::PrintDialogue);
}
/**
* \brief Default deconstructor.
*/
FunctionManager::~FunctionManager() = default;
/**
* \brief Dynamically run a function which exists in the functionContainer map by passing it's name & args.
* \param functionName Name of the dynamic function to run (must exist in the functionContainer)
* \param args List of arguments to pass into the function.
*/
void FunctionManager::Invoke(const std::string & functionName, std::vector<std::string> & args)
{
// check if the function name exists in the map
if (functionContainer.find(functionName) == functionContainer.end())
return;
// invoke the function
reinterpret_cast<void(*)(std::vector<std::string>)>(functionContainer[functionName])(args);
}
/**
* \brief Dynamically run a function which exists in the functionContainer map by passing it an actual ScriptableFunction object.
* \param scriptableFunction ScriptableFunction to dynamically run.
*/
void FunctionManager::Invoke(const ScriptableFunction & scriptableFunction)
{
// check if the object is valid
if (&scriptableFunction == nullptr) // NOLINT(clang-diagnostic-tautological-undefined-compare)
return;
// check if the function name exists in the map
if (functionContainer.find(scriptableFunction.FunctionName) == functionContainer.end())
return;
// invoke the function
reinterpret_cast<void(*)(std::vector<std::string>)>(functionContainer[scriptableFunction.FunctionName])(scriptableFunction.Arguments);
}
/**
* \brief Runs all functions in the functionObject collection.
*/
void FunctionManager::InvokeAll()
{
// run all the dynamic functions
for (auto function : functionObjects) {
Invoke(*function);
}
}
/**
* \brief Parses and runs all functions in a script file.
* \param scriptPath The path on disk where the script can be found.
*/
void FunctionManager::RunScript(const std::string & scriptPath)
{
const ScriptParser scriptParser;
const FileManager fileManager;
auto lines = fileManager.GetFileLines("test.txt");
// load our script objects from the file
functionObjects = scriptParser.Parse(lines, variableMap);
// run all the dynamic functions
InvokeAll();
}