-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaScriptManager.cpp
More file actions
150 lines (124 loc) · 3.76 KB
/
LuaScriptManager.cpp
File metadata and controls
150 lines (124 loc) · 3.76 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2006-12 HumaNature Studios Inc.
#include "CorePch.h"
#include "LuaScriptManager.h"
DEFINE_SINGLETON_TYPE(core, LuaScriptManager);
namespace core {
void LuaScriptManager::initialize()
{
bindToDebug();
}
void LuaScriptManager::update(float)
{
static bool tryConsoleFile = true;
if (!tryConsoleFile)
{
return;
}
PStreamReaderFile consoleFile("console");
if(consoleFile.isOpen())
{
static PInt64 modTime = consoleFile.getModTime();
static int caretPos = consoleFile.getRemainingSize();
int fileSize = consoleFile.getRemainingSize();
if(modTime != consoleFile.getModTime()
|| fileSize != caretPos)
{
if(consoleFile.getRemainingSize() == 0)
{
caretPos = 0;
}
modTime = consoleFile.getModTime();
// file updated, execute
// first read, skip to end
consoleFile.PStreamFile::skip(caretPos);
char commandBuffer[255];
memset(commandBuffer, 0, sizeof(commandBuffer));
int readBytes = consoleFile.read(commandBuffer, 256);
if(readBytes > 0)
{
caretPos += readBytes;
logInfo("Lua CLI", "Read %s from CLI", commandBuffer);
runScriptString(commandBuffer);
}
}
consoleFile.close();
}
else
{
tryConsoleFile = false;
}
}
void LuaScriptManager::finalize()
{
for (auto iter = mContextMap.begin(); iter != mContextMap.end(); ++iter)
{
logInfo("Lua", "Bound context: '%s'", iter->first.c_str());
SafeDelete(iter->second);
}
}
void LuaScriptManager::runScriptString(const char* string)
{
char commandBuffer[256];
PHYRE_STRNCPY(commandBuffer, string, sizeof(commandBuffer));
char* context = commandBuffer;
char* command = strstr(context, "::");
if(command == nullptr)
{
context = nullptr;
command = commandBuffer;
}
else
{
// bump to the command
command[0] = 0;
command += 2;
}
if(context != nullptr)
{
LuaScriptManager::Instance().getContext(context)->executeString(command);
}
else
{
LuaScriptManager::Instance().getGlobalContext()->executeString(command);
}
}
LuaContext* LuaScriptManager::getContext(const char* context)
{
LuaContext* context = nullptr;
ContextMap::iterator find = mContextMap.find(context);
if(find == mContextMap.end())
{
context = HNS_NEW(LuaContext)();
mContextMap[context] = context;
}
else
{
context = find->second;
}
return context;
}
void LuaScriptManager::bindToDebug()
{
#ifdef __HNS_LOGGING_ENABLED__
LuaContext* context = getGlobalContext();
ConsoleLogger::Instance().bindFunctions(context);
ScreenReporter::Instance().bindFunctions(context);
ScreenText::Instance().bindFunctions(context);
DebugDraw::Instance().bindFunctions(context);
#endif
}
bool LuaScriptManager::freeContext(const char* context)
{
ContextMap::iterator find = mContextMap.find(context);
if(find != mContextMap.end())
{
SafeDelete(find->second);
mContextMap.erase(find);
return true;
}
else
{
return false;
}
}
} // namespace core