2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-02-24 18:45:25 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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"
|
|
|
|
|
|
|
|
#include "scripting/lua-bindings/manual/tolua_fix.h"
|
|
|
|
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "extensions/cocos-ext.h"
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
2021-12-31 12:12:40 +08:00
|
|
|
# include <dirent.h>
|
|
|
|
# include <sys/stat.h>
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
|
|
|
static int lua_cocos2dx_createDownloadDir(lua_State* L)
|
|
|
|
{
|
|
|
|
if (nullptr == L)
|
|
|
|
return 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int argc = lua_gettop(L);
|
|
|
|
|
|
|
|
if (0 == argc)
|
|
|
|
{
|
|
|
|
std::string pathToSave = FileUtils::getInstance()->getWritablePath();
|
|
|
|
pathToSave += "tmpdir";
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
|
2021-12-31 12:12:40 +08:00
|
|
|
DIR* pDir = NULL;
|
|
|
|
|
|
|
|
pDir = opendir(pathToSave.c_str());
|
|
|
|
if (!pDir)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int argc = lua_gettop(L);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if COCOS2D_DEBUG >= 1
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
#endif
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (1 == argc)
|
|
|
|
{
|
|
|
|
#if COCOS2D_DEBUG >= 1
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!tolua_isstring(L, 1, 0, &tolua_err))
|
|
|
|
goto tolua_lerror;
|
2019-11-23 20:27:39 +08:00
|
|
|
#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;
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOG("'resetDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 1);
|
|
|
|
return 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if COCOS2D_DEBUG >= 1
|
|
|
|
tolua_lerror:
|
2021-12-31 12:12:40 +08:00
|
|
|
tolua_error(L, "#ferror in function 'resetDownloadDir'.", &tolua_err);
|
2019-11-23 20:27:39 +08:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lua_cocos2dx_addSearchPath(lua_State* L)
|
|
|
|
{
|
|
|
|
if (nullptr == L)
|
|
|
|
return 0;
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
int argc = lua_gettop(L);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
#if COCOS2D_DEBUG >= 1
|
|
|
|
tolua_Error tolua_err;
|
|
|
|
#endif
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (2 == argc)
|
|
|
|
{
|
|
|
|
#if COCOS2D_DEBUG >= 1
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!tolua_isstring(L, 1, 0, &tolua_err) || !tolua_isboolean(L, 2, 0, &tolua_err))
|
2019-11-23 20:27:39 +08:00
|
|
|
goto tolua_lerror;
|
|
|
|
#endif
|
|
|
|
std::string pathToSave = tolua_tostring(L, 1, "");
|
2021-12-31 12:12:40 +08:00
|
|
|
bool before = tolua_toboolean(L, 2, 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
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:
|
2021-12-31 12:12:40 +08:00
|
|
|
tolua_error(L, "#ferror in function 'addSearchPath'.", &tolua_err);
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|