Преглед изворни кода

Fixed the lua sample for android.
The lua functions dofile and loadfile were not working on android.
I've exposed FileSystem.createFileFromAsset() to the lua bindings.
This change also exposes Game::canExit().

Darryl Gough пре 13 година
родитељ
комит
94fdfde403

+ 1 - 4
gameplay/src/FileSystem.cpp

@@ -25,9 +25,6 @@ extern AAssetManager* __assetManager;
 namespace gameplay
 namespace gameplay
 {
 {
 
 
-// Creates a file on the file system from the specified asset (Android-specific).
-static void createFileFromAsset(const char* path);
-
 #ifdef __ANDROID__
 #ifdef __ANDROID__
 #include <unistd.h>
 #include <unistd.h>
 
 
@@ -334,7 +331,7 @@ bool FileSystem::isAbsolutePath(const char* filePath)
 #endif
 #endif
 }
 }
 
 
-void createFileFromAsset(const char* path)
+void FileSystem::createFileFromAsset(const char* path)
 {
 {
 #ifdef __ANDROID__
 #ifdef __ANDROID__
     static std::set<std::string> upToDateAssets;
     static std::set<std::string> upToDateAssets;

+ 7 - 0
gameplay/src/FileSystem.h

@@ -146,6 +146,13 @@ public:
      */
      */
     static bool isAbsolutePath(const char* filePath);
     static bool isAbsolutePath(const char* filePath);
 
 
+    /**
+     * Creates a file on the file system from the specified asset (Android-specific).
+     * 
+     * @param path The path to the file.
+     */
+    static void createFileFromAsset(const char* path);
+
 private:
 private:
 
 
     /**
     /**

+ 2 - 0
gameplay/src/ScriptController.cpp

@@ -550,6 +550,7 @@ static const char* lua_loadfile_function =
     "    local oldLoadfile = loadfile\n"
     "    local oldLoadfile = loadfile\n"
     "    loadfile = function(filename)\n"
     "    loadfile = function(filename)\n"
     "        if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
     "        if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
+    "            FileSystem.createFileFromAsset(filename)\n"
     "            filename = FileSystem.getResourcePath() .. filename\n"
     "            filename = FileSystem.getResourcePath() .. filename\n"
     "        end\n"
     "        end\n"
     "        return oldLoadfile(filename)\n"
     "        return oldLoadfile(filename)\n"
@@ -561,6 +562,7 @@ static const char* lua_dofile_function =
     "    local oldDofile = dofile\n"
     "    local oldDofile = dofile\n"
     "    dofile = function(filename)\n"
     "    dofile = function(filename)\n"
     "        if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
     "        if filename ~= nil and not FileSystem.isAbsolutePath(filename) then\n"
+    "            FileSystem.createFileFromAsset(filename)\n"
     "            filename = FileSystem.getResourcePath() .. filename\n"
     "            filename = FileSystem.getResourcePath() .. filename\n"
     "        end\n"
     "        end\n"
     "        return oldDofile(filename)\n"
     "        return oldDofile(filename)\n"

+ 37 - 0
gameplay/src/lua/lua_FileSystem.cpp

@@ -16,6 +16,7 @@ void luaRegister_FileSystem()
     };
     };
     const luaL_Reg lua_statics[] = 
     const luaL_Reg lua_statics[] = 
     {
     {
+        {"createFileFromAsset", lua_FileSystem_static_createFileFromAsset},
         {"fileExists", lua_FileSystem_static_fileExists},
         {"fileExists", lua_FileSystem_static_fileExists},
         {"getResourcePath", lua_FileSystem_static_getResourcePath},
         {"getResourcePath", lua_FileSystem_static_getResourcePath},
         {"isAbsolutePath", lua_FileSystem_static_isAbsolutePath},
         {"isAbsolutePath", lua_FileSystem_static_isAbsolutePath},
@@ -77,6 +78,42 @@ int lua_FileSystem__gc(lua_State* state)
     return 0;
     return 0;
 }
 }
 
 
+int lua_FileSystem_static_createFileFromAsset(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
+            {
+                // Get parameter 1 off the stack.
+                ScriptUtil::LuaArray<const char> param1 = ScriptUtil::getString(1, false);
+
+                FileSystem::createFileFromAsset(param1);
+                
+                return 0;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_FileSystem_static_createFileFromAsset - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_FileSystem_static_fileExists(lua_State* state)
 int lua_FileSystem_static_fileExists(lua_State* state)
 {
 {
     // Get the number of parameters.
     // Get the number of parameters.

+ 1 - 0
gameplay/src/lua/lua_FileSystem.h

@@ -6,6 +6,7 @@ namespace gameplay
 
 
 // Lua bindings for FileSystem.
 // Lua bindings for FileSystem.
 int lua_FileSystem__gc(lua_State* state);
 int lua_FileSystem__gc(lua_State* state);
+int lua_FileSystem_static_createFileFromAsset(lua_State* state);
 int lua_FileSystem_static_fileExists(lua_State* state);
 int lua_FileSystem_static_fileExists(lua_State* state);
 int lua_FileSystem_static_getResourcePath(lua_State* state);
 int lua_FileSystem_static_getResourcePath(lua_State* state);
 int lua_FileSystem_static_isAbsolutePath(lua_State* state);
 int lua_FileSystem_static_isAbsolutePath(lua_State* state);

+ 38 - 0
gameplay/src/lua/lua_Game.cpp

@@ -23,6 +23,7 @@ void luaRegister_Game()
 {
 {
     const luaL_Reg lua_members[] = 
     const luaL_Reg lua_members[] = 
     {
     {
+        {"canExit", lua_Game_canExit},
         {"clear", lua_Game_clear},
         {"clear", lua_Game_clear},
         {"displayKeyboard", lua_Game_displayKeyboard},
         {"displayKeyboard", lua_Game_displayKeyboard},
         {"exit", lua_Game_exit},
         {"exit", lua_Game_exit},
@@ -132,6 +133,43 @@ int lua_Game__gc(lua_State* state)
     return 0;
     return 0;
 }
 }
 
 
+int lua_Game_canExit(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                Game* instance = getInstance(state);
+                bool result = instance->canExit();
+
+                // Push the return value onto the stack.
+                lua_pushboolean(state, result);
+
+                return 1;
+            }
+            else
+            {
+                lua_pushstring(state, "lua_Game_canExit - Failed to match the given parameters to a valid function signature.");
+                lua_error(state);
+            }
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_Game_clear(lua_State* state)
 int lua_Game_clear(lua_State* state)
 {
 {
     // Get the number of parameters.
     // Get the number of parameters.

+ 1 - 0
gameplay/src/lua/lua_Game.h

@@ -6,6 +6,7 @@ namespace gameplay
 
 
 // Lua bindings for Game.
 // Lua bindings for Game.
 int lua_Game__gc(lua_State* state);
 int lua_Game__gc(lua_State* state);
+int lua_Game_canExit(lua_State* state);
 int lua_Game_clear(lua_State* state);
 int lua_Game_clear(lua_State* state);
 int lua_Game_displayKeyboard(lua_State* state);
 int lua_Game_displayKeyboard(lua_State* state);
 int lua_Game_exit(lua_State* state);
 int lua_Game_exit(lua_State* state);

+ 29 - 0
gameplay/src/lua/lua_Platform.cpp

@@ -19,6 +19,7 @@ void luaRegister_Platform()
     };
     };
     const luaL_Reg lua_statics[] = 
     const luaL_Reg lua_statics[] = 
     {
     {
+        {"canExit", lua_Platform_static_canExit},
         {"displayKeyboard", lua_Platform_static_displayKeyboard},
         {"displayKeyboard", lua_Platform_static_displayKeyboard},
         {"getAbsoluteTime", lua_Platform_static_getAbsoluteTime},
         {"getAbsoluteTime", lua_Platform_static_getAbsoluteTime},
         {"getAccelerometerValues", lua_Platform_static_getAccelerometerValues},
         {"getAccelerometerValues", lua_Platform_static_getAccelerometerValues},
@@ -147,6 +148,34 @@ int lua_Platform_enterMessagePump(lua_State* state)
     return 0;
     return 0;
 }
 }
 
 
+int lua_Platform_static_canExit(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 0:
+        {
+            bool result = Platform::canExit();
+
+            // Push the return value onto the stack.
+            lua_pushboolean(state, result);
+
+            return 1;
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 0).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_Platform_static_displayKeyboard(lua_State* state)
 int lua_Platform_static_displayKeyboard(lua_State* state)
 {
 {
     // Get the number of parameters.
     // Get the number of parameters.

+ 1 - 0
gameplay/src/lua/lua_Platform.h

@@ -7,6 +7,7 @@ namespace gameplay
 // Lua bindings for Platform.
 // Lua bindings for Platform.
 int lua_Platform__gc(lua_State* state);
 int lua_Platform__gc(lua_State* state);
 int lua_Platform_enterMessagePump(lua_State* state);
 int lua_Platform_enterMessagePump(lua_State* state);
+int lua_Platform_static_canExit(lua_State* state);
 int lua_Platform_static_displayKeyboard(lua_State* state);
 int lua_Platform_static_displayKeyboard(lua_State* state);
 int lua_Platform_static_getAbsoluteTime(lua_State* state);
 int lua_Platform_static_getAbsoluteTime(lua_State* state);
 int lua_Platform_static_getAccelerometerValues(lua_State* state);
 int lua_Platform_static_getAccelerometerValues(lua_State* state);