summaryrefslogtreecommitdiff
path: root/Runtime/Scripting
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Scripting')
-rw-r--r--Runtime/Scripting/IO/IO.bind.cpp80
-rw-r--r--Runtime/Scripting/Rendering/Shader.bind.cpp15
2 files changed, 91 insertions, 4 deletions
diff --git a/Runtime/Scripting/IO/IO.bind.cpp b/Runtime/Scripting/IO/IO.bind.cpp
index ba5691f..a46b110 100644
--- a/Runtime/Scripting/IO/IO.bind.cpp
+++ b/Runtime/Scripting/IO/IO.bind.cpp
@@ -1,12 +1,24 @@
#include "Runtime/Lua/LuaHelper.h"
#include "Runtime/Debug/Log.h"
+#include "Runtime/Common/DataBuffer.h"
#include "Runtime/FileSystem/FileJobs.h"
+#include <fstream>
+
+using namespace std;
+using namespace LuaBind;
+
+enum EFileMode
+{
+ FileMode_Text,
+ FileMode_Binary,
+};
+
// IO.ReadFiles({}, callback)
int ReadFilesAsync(lua_State* L)
{
LUA_BIND_STATE(L);
- LUA_BIND_CHECK(L, "TF");
+ LUA_BIND_CHECK(L, "TF!");
std::vector<std::string> files;
for (int i = 1; true; ++i)
@@ -30,9 +42,66 @@ int ReadFilesAsync(lua_State* L)
return 0;
}
+// IO.ReadFile(path[,mode])
+// return DataBuffer
+int ReadFile(lua_State* L)
+{
+ LUA_BIND_STATE(L);
+ LUA_BIND_CHECK(L, "SN!|S!");
+
+ const char* path = state.GetValue(1, "");
+ int mode = state.GetValue<int>(2, EFileMode::FileMode_Binary);
+
+ bool isTextFile = mode == EFileMode::FileMode_Text;
+ bool isBinaryFile = mode == EFileMode::FileMode_Binary;
+
+ int openMode = 0;
+ //if (isBinaryFile)
+ openMode |= ios::binary;
+
+ std::ifstream file = ifstream(path, openMode);
+ if (!file.is_open())
+ {
+ log_error(string("Can't read file. ") + path);
+ state.PushNil();
+ return 1;
+ }
+
+ std::streampos size = file.tellg();
+ file.seekg(0, std::ios::end);
+ size = file.tellg() - size;
+ if (size == 0)
+ {
+ log_error(string("File is Empty. ") + path);
+ state.PushNil();
+ return 1;
+ }
+ file.seekg(0, std::ios::beg);
+
+ int bufSize = size;
+ if (isTextFile)
+ bufSize += 1; // \0
+
+ DataBuffer* buffer = new DataBuffer(state.GetVM());
+ buffer->type = isBinaryFile ? EDataBufferType::DataBufferMode_Binary : EDataBufferType::DataBufferMode_Text;
+ buffer->data = new char[bufSize];
+ buffer->length = bufSize;
+
+ file.read(buffer->data, size);
+ file.close();
+
+ if (isTextFile)
+ buffer->data[bufSize - 1] = '\0';
+
+ buffer->PushUserdata(state);
+
+ return 1;
+}
+
static luaL_Reg ioFuncs[] = {
- {"ReadFilesAsync", ReadFilesAsync},
- {0, 0}
+ { "ReadFilesAsync", ReadFilesAsync },
+ { "ReadFile", ReadFile },
+ { 0, 0 }
};
int luaopen_GameLab_IO(lua_State* L)
@@ -47,5 +116,10 @@ int luaopen_GameLab_IO(lua_State* L)
state.RegisterMethods(ioFuncs);
+ LUA_BIND_REGISTER_ENUM(state, "EFileMode",
+ { "Binary", EFileMode::FileMode_Binary},
+ { "Text", EFileMode::FileMode_Text}
+ );
+
return 1;
} \ No newline at end of file
diff --git a/Runtime/Scripting/Rendering/Shader.bind.cpp b/Runtime/Scripting/Rendering/Shader.bind.cpp
index 0137447..482efb9 100644
--- a/Runtime/Scripting/Rendering/Shader.bind.cpp
+++ b/Runtime/Scripting/Rendering/Shader.bind.cpp
@@ -1,6 +1,7 @@
#include "Runtime/Graphics/Shader.h"
#include "Runtime/Debug/Log.h"
#include "Runtime/Graphics/GfxDevice.h"
+#include "Runtime/Common/DataBuffer.h"
using namespace LuaBind;
@@ -30,10 +31,11 @@ LUA_BIND_POSTPROCESS(Shader)
// Shader.New(glsl[, keepSrc])
// Shader.New(vsh, fsh[, keepSrc])
+// Shader.New(DataBuffer)
LUA_BIND_IMPL_METHOD(Shader, _New)
{
LUA_BIND_STATE(L, Shader);
- LUA_BIND_CHECK(L, "SS!|S!");
+ LUA_BIND_CHECK(L, "SS!|S!|U!");
try {
Shader* shader = NULL;
if(state.CheckParams(1, "SS!"))
@@ -47,6 +49,17 @@ LUA_BIND_IMPL_METHOD(Shader, _New)
std::string glsl = state.GetValue<cc8*>(1, "");
shader = new Shader(state.GetVM(), glsl);
}
+ else if (LuaHelper::IsType(state, "GameLab.DataBuffer", 1))
+ {
+ DataBuffer* buffer = state.GetUserdata<DataBuffer>(1);
+ if (buffer == NULL)
+ {
+ log_error("Unable to create shader, databuffer is empty");
+ return 0;
+ }
+ std::string glsl = buffer->data;
+ shader = new Shader(state.GetVM(), glsl);
+ }
if (shader != NULL)
shader->PushUserdata(state);
else