-
Notifications
You must be signed in to change notification settings - Fork 2
/
AtlasExtension.cpp
119 lines (102 loc) · 2.57 KB
/
AtlasExtension.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
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
#include "stdafx.h"
#include <string>
#include <windows.h>
#include "AtlasExtension.h"
#include "AtlasLogger.h"
#include "AtlasCore.h"
using namespace std;
ExtensionManager::ExtensionManager(VariableMap* Map)
{
VarMap = Map;
}
bool ExtensionManager::LoadExtension(string& ExtId, string& ExtensionFile)
{
// Use file extension to pick which derived AtlasExtension to use
AtlasExtension* Ext;
size_t Pos = ExtensionFile.find_last_of('.');
if(Pos == string::npos || Pos >= ExtensionFile.length()-1)
return false;
else if(ExtensionFile.substr(Pos+1, ExtensionFile.length()-1) == "dll") // dll
{
}
else
{
Logger.ReportError(CurrentLine, "Unsupported file format used in LOADEXT");
return false;
}
Ext = (AtlasExtension*)VarMap->GetVar(ExtId)->GetData();
if(Ext != NULL)
{
Logger.ReportError(CurrentLine, "%s has alrady been initialized with LOADEXT", ExtId.c_str());
delete Ext;
return false;
}
Ext = NULL;
Ext = new AtlasExtension;
if(!Ext->LoadExtension(ExtensionFile))
{
Logger.ReportError(CurrentLine, "%s could not be loaded", ExtensionFile.c_str());
delete Ext;
return false;
}
VarMap->SetVarData(ExtId, Ext, P_EXTENSION);
return true;
}
unsigned int ExtensionManager::ExecuteExtension(string& ExtId, string& FunctionName, AtlasContext** Context)
{
AtlasExtension* Ext;
Ext = (AtlasExtension*)VarMap->GetVar(ExtId)->GetData();
if(Ext == NULL)
{
Logger.ReportError(CurrentLine, "%s has not been initialized by LOADEXT", ExtId.c_str());
return -1;
}
if(!Ext->IsLoaded())
{
ReportBug("Extension not loaded but initialized in ExtensionManager::ExecuteExtension");
return -1;
}
ExtensionFunction Func = Ext->GetFunction(FunctionName);
if(Func == NULL)
{
Logger.ReportError(CurrentLine, "Function %s was not found in the extension file", FunctionName.c_str());
return -1;
}
unsigned int Res = Func(Context);
if(Res > MAX_RETURN_VAL)
{
Logger.ReportWarning(CurrentLine, "Extension returned invalid value %u", Res);
Res = NO_ACTION;
}
return Res;
}
AtlasExtension::AtlasExtension() : Extension(NULL)
{
}
AtlasExtension::~AtlasExtension()
{
if(Extension)
FreeLibrary(Extension);
}
bool AtlasExtension::LoadExtension(string& ExtensionName)
{
Extension = LoadLibraryA(ExtensionName.c_str());
if(Extension)
return true;
else
return false;
}
bool AtlasExtension::IsLoaded()
{
if(Extension)
return true;
else
return false;
}
ExtensionFunction AtlasExtension::GetFunction(string& FunctionName)
{
if(NULL == Extension)
return NULL;
ExtensionFunction func = (ExtensionFunction)GetProcAddress(Extension, FunctionName.c_str());
return func;
}