2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-10 15:32:48 +08:00
|
|
|
#include "lua_assetsmanager_test_sample.h"
|
|
|
|
|
2016-03-21 21:04:06 +08:00
|
|
|
#include "scripting/lua-bindings/manual/tolua_fix.h"
|
2014-03-10 15:32:48 +08:00
|
|
|
|
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "extensions/cocos-ext.h"
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM != AX_PLATFORM_WIN32)
|
2021-12-28 16:06:23 +08:00
|
|
|
# include <dirent.h>
|
|
|
|
# include <sys/stat.h>
|
2014-03-10 15:32:48 +08:00
|
|
|
#endif
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
USING_NS_AX_EXT;
|
2014-03-10 15:32:48 +08:00
|
|
|
|
|
|
|
static int lua_cocos2dx_createDownloadDir(lua_State* L)
|
|
|
|
{
|
|
|
|
if (nullptr == L)
|
|
|
|
return 0;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-03-10 15:32:48 +08:00
|
|
|
int argc = lua_gettop(L);
|
|
|
|
|
|
|
|
if (0 == argc)
|
|
|
|
{
|
|
|
|
std::string pathToSave = FileUtils::getInstance()->getWritablePath();
|
|
|
|
pathToSave += "tmpdir";
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM != AX_PLATFORM_WIN32)
|
2021-12-28 16:06:23 +08:00
|
|
|
DIR* pDir = NULL;
|
|
|
|
|
|
|
|
pDir = opendir(pathToSave.c_str());
|
|
|
|
if (!pDir)
|
2014-03-10 15:32:48 +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-28 16:06:23 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("'createDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 0);
|
2014-03-10 15:32:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lua_cocos2dx_deleteDownloadDir(lua_State* L)
|
|
|
|
{
|
|
|
|
if (nullptr == L)
|
|
|
|
return 0;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-03-10 15:32:48 +08:00
|
|
|
int argc = lua_gettop(L);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#if _AX_DEBUG >= 1
|
2014-03-10 15:32:48 +08:00
|
|
|
tolua_Error tolua_err;
|
|
|
|
#endif
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-03-10 15:32:48 +08:00
|
|
|
if (1 == argc)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
#if _AX_DEBUG >= 1
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!tolua_isstring(L, 1, 0, &tolua_err))
|
|
|
|
goto tolua_lerror;
|
2014-03-10 15:32:48 +08:00
|
|
|
#endif
|
|
|
|
std::string pathToSave = tolua_tostring(L, 1, "");
|
2015-12-29 07:59:36 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_TARGET_OS_TVOS
|
2015-12-29 07:59:36 +08:00
|
|
|
// Not implemented. "system" is not present on tvOS
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("'lua_axmol_deleteDownloadDir' not implemented on tvOS");
|
2015-12-29 07:59:36 +08:00
|
|
|
return 0;
|
2014-03-10 15:32:48 +08:00
|
|
|
#endif
|
2016-06-22 14:31:26 +08:00
|
|
|
|
|
|
|
FileUtils::getInstance()->removeDirectory(pathToSave);
|
2014-03-10 15:32:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("'resetDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 1);
|
2014-03-10 15:32:48 +08:00
|
|
|
return 0;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#if _AX_DEBUG >= 1
|
2014-03-10 15:32:48 +08:00
|
|
|
tolua_lerror:
|
2021-12-28 16:06:23 +08:00
|
|
|
tolua_error(L, "#ferror in function 'resetDownloadDir'.", &tolua_err);
|
2014-03-10 15:32:48 +08:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lua_cocos2dx_addSearchPath(lua_State* L)
|
|
|
|
{
|
|
|
|
if (nullptr == L)
|
|
|
|
return 0;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-03-10 15:32:48 +08:00
|
|
|
int argc = lua_gettop(L);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#if _AX_DEBUG >= 1
|
2014-03-10 15:32:48 +08:00
|
|
|
tolua_Error tolua_err;
|
|
|
|
#endif
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-03-10 15:32:48 +08:00
|
|
|
if (2 == argc)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
#if _AX_DEBUG >= 1
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!tolua_isstring(L, 1, 0, &tolua_err) || !tolua_isboolean(L, 2, 0, &tolua_err))
|
2014-03-10 15:32:48 +08:00
|
|
|
goto tolua_lerror;
|
|
|
|
#endif
|
|
|
|
std::string pathToSave = tolua_tostring(L, 1, "");
|
2021-12-28 16:06:23 +08:00
|
|
|
bool before = tolua_toboolean(L, 2, 0);
|
2017-03-22 11:17:52 +08:00
|
|
|
FileUtils::getInstance()->addSearchPath(pathToSave, before);
|
2014-03-10 15:32:48 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("'addSearchPath' function wrong number of arguments: %d, was expecting %d\n", argc, 2);
|
2014-03-10 15:32:48 +08:00
|
|
|
return 0;
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#if _AX_DEBUG >= 1
|
2014-03-10 15:32:48 +08:00
|
|
|
tolua_lerror:
|
2021-12-28 16:06:23 +08:00
|
|
|
tolua_error(L, "#ferror in function 'addSearchPath'.", &tolua_err);
|
2014-03-10 15:32:48 +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;
|
|
|
|
}
|