mirror of https://github.com/axmolengine/axmol.git
164 lines
4.6 KiB
C++
164 lines
4.6 KiB
C++
|
/****************************************************************************
|
||
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
||
|
|
||
|
http://www.cocos2d-x.org
|
||
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
of this software and associated documentation files (the "Software"), to deal
|
||
|
in the Software without restriction, including without limitation the rights
|
||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
copies of the Software, and to permit persons to whom the Software is
|
||
|
furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in
|
||
|
all copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
|
THE SOFTWARE.
|
||
|
****************************************************************************/
|
||
|
|
||
|
#include "lua_assetsmanager_test_sample.h"
|
||
|
|
||
|
#ifdef __cplusplus
|
||
|
extern "C" {
|
||
|
#endif
|
||
|
#include "scripting/lua-bindings/manual/tolua_fix.h"
|
||
|
#ifdef __cplusplus
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
#include "cocos2d.h"
|
||
|
#include "extensions/cocos-ext.h"
|
||
|
|
||
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||
|
#include <dirent.h>
|
||
|
#include <sys/stat.h>
|
||
|
#endif
|
||
|
|
||
|
USING_NS_CC;
|
||
|
USING_NS_CC_EXT;
|
||
|
|
||
|
|
||
|
static int lua_cocos2dx_createDownloadDir(lua_State* L)
|
||
|
{
|
||
|
if (nullptr == L)
|
||
|
return 0;
|
||
|
|
||
|
int argc = lua_gettop(L);
|
||
|
|
||
|
if (0 == argc)
|
||
|
{
|
||
|
std::string pathToSave = FileUtils::getInstance()->getWritablePath();
|
||
|
pathToSave += "tmpdir";
|
||
|
|
||
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
||
|
DIR *pDir = NULL;
|
||
|
|
||
|
pDir = opendir (pathToSave.c_str());
|
||
|
if (! pDir)
|
||
|
{
|
||
|
mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
|
||
|
}
|
||
|
#else
|
||
|
if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES)
|
||
|
{
|
||
|
CreateDirectoryA(pathToSave.c_str(), 0);
|
||
|
}
|
||
|
#endif
|
||
|
tolua_pushstring(L, pathToSave.c_str());
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
CCLOG("'createDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 0);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int lua_cocos2dx_deleteDownloadDir(lua_State* L)
|
||
|
{
|
||
|
if (nullptr == L)
|
||
|
return 0;
|
||
|
|
||
|
int argc = lua_gettop(L);
|
||
|
|
||
|
#if COCOS2D_DEBUG >= 1
|
||
|
tolua_Error tolua_err;
|
||
|
#endif
|
||
|
|
||
|
if (1 == argc)
|
||
|
{
|
||
|
#if COCOS2D_DEBUG >= 1
|
||
|
if (!tolua_isstring(L, 1, 0, &tolua_err)) goto tolua_lerror;
|
||
|
#endif
|
||
|
std::string pathToSave = tolua_tostring(L, 1, "");
|
||
|
|
||
|
#if CC_TARGET_OS_TVOS
|
||
|
// Not implemented. "system" is not present on tvOS
|
||
|
CCLOG("'lua_cocos2dx_deleteDownloadDir' not implemented on tvOS");
|
||
|
return 0;
|
||
|
#endif
|
||
|
|
||
|
FileUtils::getInstance()->removeDirectory(pathToSave);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
CCLOG("'resetDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 1);
|
||
|
return 0;
|
||
|
|
||
|
#if COCOS2D_DEBUG >= 1
|
||
|
tolua_lerror:
|
||
|
tolua_error(L,"#ferror in function 'resetDownloadDir'.",&tolua_err);
|
||
|
return 0;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
static int lua_cocos2dx_addSearchPath(lua_State* L)
|
||
|
{
|
||
|
if (nullptr == L)
|
||
|
return 0;
|
||
|
|
||
|
int argc = lua_gettop(L);
|
||
|
|
||
|
#if COCOS2D_DEBUG >= 1
|
||
|
tolua_Error tolua_err;
|
||
|
#endif
|
||
|
|
||
|
|
||
|
if (2 == argc)
|
||
|
{
|
||
|
#if COCOS2D_DEBUG >= 1
|
||
|
if (!tolua_isstring(L, 1, 0, &tolua_err) ||
|
||
|
!tolua_isboolean(L, 2, 0, &tolua_err))
|
||
|
goto tolua_lerror;
|
||
|
#endif
|
||
|
std::string pathToSave = tolua_tostring(L, 1, "");
|
||
|
bool before = tolua_toboolean(L, 2, 0);
|
||
|
FileUtils::getInstance()->addSearchPath(pathToSave, before);
|
||
|
return 0;
|
||
|
}
|
||
|
CCLOG("'addSearchPath' function wrong number of arguments: %d, was expecting %d\n", argc, 2);
|
||
|
return 0;
|
||
|
|
||
|
#if COCOS2D_DEBUG >= 1
|
||
|
tolua_lerror:
|
||
|
tolua_error(L,"#ferror in function 'addSearchPath'.",&tolua_err);
|
||
|
return 0;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
int register_assetsmanager_test_sample(lua_State* L)
|
||
|
{
|
||
|
tolua_open(L);
|
||
|
tolua_module(L, NULL, 0);
|
||
|
tolua_beginmodule(L, NULL);
|
||
|
tolua_function(L, "createDownloadDir", lua_cocos2dx_createDownloadDir);
|
||
|
tolua_function(L, "deleteDownloadDir", lua_cocos2dx_deleteDownloadDir);
|
||
|
tolua_function(L, "addSearchPath", lua_cocos2dx_addSearchPath);
|
||
|
tolua_endmodule(L);
|
||
|
return 0;
|
||
|
}
|