From f3290d84b7582b508e9469044ff9d494e69ea006 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 18 Mar 2013 22:55:41 -0300 Subject: [PATCH] Improved error handling and error messages when love.graphics.newShader fails --- src/modules/graphics/opengl/wrap_Graphics.cpp | 41 +++++++++++++++-- src/scripts/graphics.lua | 22 ++++----- src/scripts/graphics.lua.h | 46 +++++-------------- 3 files changed, 58 insertions(+), 51 deletions(-) diff --git a/src/modules/graphics/opengl/wrap_Graphics.cpp b/src/modules/graphics/opengl/wrap_Graphics.cpp index 5c6326afe..b1d60d6c8 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.cpp +++ b/src/modules/graphics/opengl/wrap_Graphics.cpp @@ -508,14 +508,45 @@ int w_newShader(lua_State *L) // clamp stack to 2 elements lua_settop(L, 2); + // read any filepath arguments + for (int i = 1; i <= 2; i++) + { + if (!lua_isstring(L, i)) + continue; + + // call love.filesystem.isFile(arg_i) + luax_getfunction(L, "filesystem", "isFile"); + lua_pushvalue(L, i); + lua_call(L, 1, 1); + + bool isFile = luax_toboolean(L, -1); + lua_pop(L, 1); + + if (isFile) + { + luax_getfunction(L, "filesystem", "read"); + lua_pushvalue(L, i); + lua_call(L, 1, 1); + lua_replace(L, i); + } + } + + bool has_arg1 = lua_isstring(L, 1); + bool has_arg2 = lua_isstring(L, 2); + + // require at least one string argument + if (!(has_arg1 || has_arg2)) + luaL_checkstring(L, 1); + luax_getfunction(L, "graphics", "_shaderCodeToGLSL"); - // push vertexcode and pixelcode strings to the top of the stack so they become arguments for the function + // push vertexcode and pixelcode strings to the top of the stack lua_pushvalue(L, 1); lua_pushvalue(L, 2); // call effectCodeToGLSL, returned values will be at the top of the stack - lua_pcall(L, 2, 2, 0); + if (lua_pcall(L, 2, 2, 0) != 0) + return luaL_error(L, "%s", lua_tostring(L, -1)); Shader::ShaderSources sources; @@ -525,6 +556,8 @@ int w_newShader(lua_State *L) std::string vertexcode(luaL_checkstring(L, -2)); sources[Shader::TYPE_VERTEX] = vertexcode; } + else if (has_arg1 && has_arg2) + return luaL_error(L, "Could not parse vertex shader code (missing 'position' function?)"); // pixel shader code if (lua_isstring(L, -1)) @@ -532,6 +565,8 @@ int w_newShader(lua_State *L) std::string pixelcode(luaL_checkstring(L, -1)); sources[Shader::TYPE_PIXEL] = pixelcode; } + else if (has_arg1 && has_arg2) + return luaL_error(L, "Could not parse pixel shader code (missing 'effect' function?)"); if (sources.empty()) { @@ -539,7 +574,7 @@ int w_newShader(lua_State *L) for (int i = 1; i <= 2; i++) { if (lua_isstring(L, i)) - return luaL_argerror(L, i, "invalid or incomplete shader code"); + return luaL_argerror(L, i, "missing 'position' or 'effect' function?"); } } diff --git a/src/scripts/graphics.lua b/src/scripts/graphics.lua index d016d2473..4ca933062 100644 --- a/src/scripts/graphics.lua +++ b/src/scripts/graphics.lua @@ -1361,9 +1361,11 @@ void main() { end function love.graphics._shaderCodeToGLSL(vertexcode, pixelcode) + local vertexarg, pixelarg = vertexcode, pixelcode + if vertexcode then local s = vertexcode:gsub("\r\n\t", " ") - s = s:gsub("%w+(%s+)%(", "") + s = s:gsub("(%w+)(%s+)%(", "%1(") if s:match("vec4%s*effect%(") then pixelcode = vertexcode -- first argument contains pixel shader code end @@ -1371,9 +1373,10 @@ void main() { vertexcode = nil -- first argument doesn't contain vertex shader code end end + if pixelcode then local s = pixelcode:gsub("\r\n\t", " ") - s = s:gsub("%w+(%s+)%(", "") + s = s:gsub("(%w+)(%s+)%(", "%1(") if s:match("vec4%s*position%(") then vertexcode = pixelcode -- second argument contains vertex shader code end @@ -1385,6 +1388,7 @@ void main() { if vertexcode then vertexcode = createVertexCode(vertexcode) end + if pixelcode then pixelcode = createPixelCode(pixelcode) end @@ -1464,19 +1468,9 @@ void main() { local newShader = love.graphics.newShader function love.graphics.newShader(vertexcode, pixelcode) - love.graphics.newShader = function(vertexcode, pixelcode) - if love.filesystem then - if vertexcode and love.filesystem.exists(vertexcode) then - vertexcode = love.filesystem.read(vertexcode) - end - if pixelcode and love.filesystem.exists(pixelcode) then - pixelcode = love.filesystem.read(pixelcode) - end - end - return newShader(vertexcode, pixelcode) - end + love.graphics.newShader = newShader - local shader = love.graphics.newShader(vertexcode, pixelcode) + local shader = newShader(vertexcode, pixelcode) local meta = getmetatable(shader) meta.send = shader_dispatch_send meta.sendBoolean = meta.sendFloat diff --git a/src/scripts/graphics.lua.h b/src/scripts/graphics.lua.h index 3d55864a8..189d3f71b 100644 --- a/src/scripts/graphics.lua.h +++ b/src/scripts/graphics.lua.h @@ -6388,13 +6388,16 @@ const unsigned char graphics_lua[] = 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x5f, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x54, 0x6f, 0x47, 0x4c, 0x53, 0x4c, 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, + 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x61, 0x72, 0x67, 0x2c, + 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x61, 0x72, 0x67, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, + 0x63, 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x0a, 0x09, 0x09, 0x69, 0x66, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, 0x09, 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x73, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x3a, 0x67, 0x73, 0x75, 0x62, 0x28, 0x22, 0x5c, 0x72, 0x5c, 0x6e, 0x5c, 0x74, 0x22, 0x2c, 0x20, 0x22, 0x20, 0x22, 0x29, 0x0a, - 0x09, 0x09, 0x09, 0x73, 0x20, 0x3d, 0x20, 0x73, 0x3a, 0x67, 0x73, 0x75, 0x62, 0x28, 0x22, 0x25, 0x77, 0x2b, - 0x28, 0x25, 0x73, 0x2b, 0x29, 0x25, 0x28, 0x22, 0x2c, 0x20, 0x22, 0x22, 0x29, 0x0a, + 0x09, 0x09, 0x09, 0x73, 0x20, 0x3d, 0x20, 0x73, 0x3a, 0x67, 0x73, 0x75, 0x62, 0x28, 0x22, 0x28, 0x25, 0x77, + 0x2b, 0x29, 0x28, 0x25, 0x73, 0x2b, 0x29, 0x25, 0x28, 0x22, 0x2c, 0x20, 0x22, 0x25, 0x31, 0x28, 0x22, 0x29, 0x0a, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x73, 0x3a, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x28, 0x22, 0x76, 0x65, 0x63, 0x34, 0x25, 0x73, 0x2a, 0x65, 0x66, 0x66, 0x65, 0x63, 0x74, 0x25, 0x28, 0x22, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, @@ -6418,8 +6421,8 @@ const unsigned char graphics_lua[] = 0x09, 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x73, 0x20, 0x3d, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x3a, 0x67, 0x73, 0x75, 0x62, 0x28, 0x22, 0x5c, 0x72, 0x5c, 0x6e, 0x5c, 0x74, 0x22, 0x2c, 0x20, 0x22, 0x20, 0x22, 0x29, 0x0a, - 0x09, 0x09, 0x09, 0x73, 0x20, 0x3d, 0x20, 0x73, 0x3a, 0x67, 0x73, 0x75, 0x62, 0x28, 0x22, 0x25, 0x77, 0x2b, - 0x28, 0x25, 0x73, 0x2b, 0x29, 0x25, 0x28, 0x22, 0x2c, 0x20, 0x22, 0x22, 0x29, 0x0a, + 0x09, 0x09, 0x09, 0x73, 0x20, 0x3d, 0x20, 0x73, 0x3a, 0x67, 0x73, 0x75, 0x62, 0x28, 0x22, 0x28, 0x25, 0x77, + 0x2b, 0x29, 0x28, 0x25, 0x73, 0x2b, 0x29, 0x25, 0x28, 0x22, 0x2c, 0x20, 0x22, 0x25, 0x31, 0x28, 0x22, 0x29, 0x0a, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x73, 0x3a, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x28, 0x22, 0x76, 0x65, 0x63, 0x34, 0x25, 0x73, 0x2a, 0x70, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x25, 0x28, 0x22, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, @@ -6626,36 +6629,11 @@ const unsigned char graphics_lua[] = 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x6e, 0x65, - 0x77, 0x53, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, - 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, - 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, - 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x79, 0x73, - 0x74, 0x65, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, - 0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x20, - 0x61, 0x6e, 0x64, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x79, 0x73, 0x74, 0x65, - 0x6d, 0x2e, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, - 0x65, 0x29, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, - 0x09, 0x09, 0x09, 0x09, 0x09, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, - 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x72, 0x65, - 0x61, 0x64, 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, - 0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x61, - 0x6e, 0x64, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, - 0x2e, 0x65, 0x78, 0x69, 0x73, 0x74, 0x73, 0x28, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, - 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, - 0x09, 0x09, 0x09, 0x09, 0x09, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x20, 0x3d, 0x20, 0x6c, - 0x6f, 0x76, 0x65, 0x2e, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x72, 0x65, 0x61, - 0x64, 0x28, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, - 0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6e, 0x65, 0x77, 0x53, 0x68, 0x61, 0x64, 0x65, - 0x72, 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x70, 0x69, 0x78, 0x65, - 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, - 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, - 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x6c, - 0x6f, 0x76, 0x65, 0x2e, 0x67, 0x72, 0x61, 0x70, 0x68, 0x69, 0x63, 0x73, 0x2e, 0x6e, 0x65, 0x77, 0x53, 0x68, - 0x61, 0x64, 0x65, 0x72, 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, 0x65, 0x2c, 0x20, 0x70, - 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, + 0x77, 0x53, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x6e, 0x65, 0x77, 0x53, 0x68, 0x61, 0x64, 0x65, + 0x72, 0x0a, + 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x3d, 0x20, 0x6e, + 0x65, 0x77, 0x53, 0x68, 0x61, 0x64, 0x65, 0x72, 0x28, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x64, + 0x65, 0x2c, 0x20, 0x70, 0x69, 0x78, 0x65, 0x6c, 0x63, 0x6f, 0x64, 0x65, 0x29, 0x0a, 0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x3d, 0x20, 0x67, 0x65, 0x74, 0x6d, 0x65, 0x74, 0x61, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x28, 0x73, 0x68, 0x61, 0x64, 0x65, 0x72, 0x29, 0x0a, 0x09, 0x09, 0x6d, 0x65, 0x74, 0x61, 0x2e, 0x73, 0x65, 0x6e, 0x64, 0x20, 0x3d, 0x20, 0x73, 0x68, 0x61, 0x64,