When using Quick-cocos2d-x to do a project hot update, I need to create a temporary folder to save the downloaded update package. After the update is complete, I need to delete these temporary files and folders.
Cocos2d-x and Quick-cocos2d-x do not provide the Delete folder feature. I did the following 2 attempts:
1. Use C + +
A CreateDirectory method is provided in the Assetsmanager package in Cocos2d-x 2.x. This method allows you to create folders across platform support. There is no problem running in the actual project.
Copy Code code as follows:
BOOL Assetsmanager::createdirectory (const char *path)
{
#if (cc_target_platform!= cc_platform_win32)
mode_t processmask = umask (0);
int ret = mkdir (path, S_irwxu | S_irwxg | S_IRWXO);
Umask (Processmask);
if (Ret!= 0 && (errno!= eexist))
{
return false;
}
return true;
#else
BOOL ret = Createdirectorya (path, NULL);
if (!ret && error_already_exists!= GetLastError ())
{
return false;
}
return true;
#endif
}
A reset method is provided in the Assetsmanager sample example of Cocos2d-x 2.x, which recursively deletes a folder using system commands.
Copy Code code as follows:
void Updatelayer::reset (Cocos2d::ccobject *psender)
{
Pprogresslabel->setstring ("");
Remove Downloaded files
#if (cc_target_platform!= cc_platform_win32)
string command = "Rm-r";
Path may include spaces.
Command + + "" + Pathtosave + "" ";
System (COMMAND.C_STR ());
#else
string command = "rd/s/q";
Path may include spaces.
Command + + "" + Pathtosave + "" ";
System (COMMAND.C_STR ());
#endif
Delete recorded version codes.
Getassetsmanager ()->deleteversion ();
Createdownloadeddir ();
}
However, when this reset is running in the iOS emulator, Xcode will report this warinng:
The IOS Simulator Libsystem is initialized out of order. This is most often caused by running host executables or inserting host dylibs. In the future, this would cause an abort.
So I turned to another option.
2. Pure LUA
Pure Lua is actually a gimmick. It's still dependent on LFS (LUA file sytem), but Quick-cocos2d-x already contains the library.
The Lfs.rmdir command, like the Os.remove command, deletes only empty folders. As a result, you need to recursively delete all files and subfolders in a folder to implement a function similar to RM-RF.
Let's expand the OS package.
Copy Code code as follows:
Require ("LFS")
function os.exists (path)
Return Ccfileutils:sharedfileutils (): isfileexist (Path)
End
function Os.mkdir (path)
If not os.exists (path) Then
return Lfs.mkdir (PATH)
End
return True
End
function Os.rmdir (path)
Print ("Os.rmdir:", Path)
If os.exists (path) Then
Local function _rmdir (path)
Local iter, dir_obj = lfs.dir (path)
While True
Local dir = iter (dir_obj)
if dir = = Nil Then break end
If dir ~= "." and dir ~= "..." Then
Local CurDir = path. Dir
Local mode = Lfs.attributes (CurDir, "mode")
if mode = = "Directory" Then
_rmdir (CurDir.) /")
ElseIf mode = = "File" Then
Os.remove (CurDir)
End
End
End
Local succ, des = os.remove (path)
If Des then print (DES) end
return SUCC
End
_rmdir (PATH)
End
return True
End
The above code was successfully tested on the IOS emulator and Android real machine. Windows systems, Mac OS X, and IOS real machines have not been tested yet. I will update it immediately after the test.