-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompile.cpp
More file actions
91 lines (81 loc) · 2.15 KB
/
Compile.cpp
File metadata and controls
91 lines (81 loc) · 2.15 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
#include <apollo/Apollo.h>
#include <apollo/Scripting/Compile.h>
#include <apollo/Utilities/ResourceManager.h>
#include <apollo/Logging.h>
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
struct CompileInstructions
{
std::string sourcePath;
std::string destPath;
std::string name;
};
#define LUA_READER_BUFFER_SIZE 4096
static const char* luaReader ( lua_State* L, void* data, size_t* size )
{
static char buffer[LUA_READER_BUFFER_SIZE];
size_t len = SDL_RWread((SDL_RWops*)data, buffer, 1, LUA_READER_BUFFER_SIZE);
if (len <= 0)
{
*size = 0;
return NULL;
}
else
{
*size = len;
return buffer;
}
}
static int luaWriter ( lua_State* L, const void* p, size_t len, void* ud )
{
SDL_RWops* rwops = (SDL_RWops*)ud;
return SDL_RWwrite(rwops, p, len, 1) == 1 ? 0 : -1;
}
static void luaLoad ( lua_State* L, const std::string& path )
{
SDL_RWops* rwops = ResourceManager::OpenFile(path);
if (rwops)
{
int rc = lua_load(L, luaReader, (void*)rwops, path.c_str());
SDL_RWclose(rwops);
if (rc != 0)
{
LOG("Scripting::Compiler", LOG_WARNING, "Unable to load script %s", path.c_str());
return;
}
}
else
{
LOG("Scripting::Compiler", LOG_WARNING, "Unable to open script %s", path.c_str());
}
}
static int CompileScript_Thread ( void* data )
{
CompileInstructions* command = (CompileInstructions*)data;
if (!ResourceManager::FileExists(command->destPath))
{
// you just lost the game
lua_State* L = luaL_newstate();
luaLoad(L, command->sourcePath);
SDL_RWops* writerRWops = ResourceManager::WriteFile(command->destPath);
assert(writerRWops);
lua_dump(L, luaWriter, (void*)writerRWops);
SDL_RWclose(writerRWops);
lua_close(L);
printf("[Compiler] Compiled script %s\n", command->name.c_str());
}
delete command;
return 0;
}
void CompileScript ( const std::string& scriptName )
{
CompileInstructions* instrs = new CompileInstructions;
instrs->name = scriptName;
instrs->sourcePath = "Scripts/" + scriptName + ".lua";
instrs->destPath = "Scripts/" + scriptName + ".lo";
SDL_CreateThread(CompileScript_Thread, (void*)instrs);
}