Cocos2d-x resources - Extra file utils


Extra file utils (Android + iOS, C++)


While preparing the "On-the-fly resource updating" topic, I remembered that it depends on some extra file utility functions I added myself. I'll make these available here as a separate topic since they are not specific to OTF and I expect they will be added to the cocos2d-x someday, in which case you should use the official functions instead.

Here is the outline of what functionality is included, they should be self-explanatory:
1
2
3
4
  bool copyFile( string src, string dst );
  bool deleteFile( string file );
  bool createFolder( string folder );
  int listFolder( string folder, vector<string>& entries, bool directoriesOnly );

These functions expect full paths, so to work with files in the private folder of your app, you'll need to prefix them with getWritablePath. For example, to create a folder called 'saves' in the private folder, you would do:
1
  createFolder( FileUtils::sharedFileUtils()->getWritablePath() + "saves" );

Source code


iforce2dFileUtils.h
iforce2dFileUtils.cpp
iforce2dFileUtilsJNI.cpp
iforce2dFileUtilsIOS.mm

This code was confirmed to work with Cocos2d-x v3 RC1.


Instructions for use (iOS)


1. Add the files (except for the JNI one) to your project.


Instructions for use (Android)


1. Add the files (except for the IOS one) to your project.

2. In iforce2dFileUtilsJNI.cpp, change the CLASS_NAME define to the main activity class of your app.

3. Add the following functions to your main activity class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  public static boolean copyFile(File src, File dst) {
      try {
          FileInputStream in = new FileInputStream(src);
          FileOutputStream out = new FileOutputStream(dst);
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0) {
              out.write(buf, 0, len);
          }
          in.close();
          out.close();
      }
      catch( IOException e ) {
          Log.v("cocos2d-x", "Could not copy file: " + src + " to " + dst);
          e.printStackTrace();
          return false;
      }
      return true;
  }
  
  public static boolean copyFile( String from, String to ) {
      return copyFile( new File(from), new File(to) );
  }
  
  public static boolean createFolder( String foldername ) {
      boolean ok = true;
      try {
          File directory = new File( foldername );
          if ( ! directory.isDirectory() ) {
              ok = directory.mkdirs();
          }
      }
      catch ( SecurityException e ) {
          Log.v("cocos2d-x", "Could not create folder: " + foldername);
          e.printStackTrace();
          ok = false;
      }
      return ok;
  }
  
  public static boolean deleteFile2( String filename )
  {
      boolean ok = true;
      try {
          File f = new File( filename );
          f.delete();
      }
      catch ( Exception e ) {
          Log.v("cocos2d-x", "Could not delete file (maybe already deleted): " + filename);
          e.printStackTrace();
          ok = false;
      }
      return ok;
  }
You will need to import android.util.Log and java.io.* for this to compile.