[Bugfix]Quick-Cocos2dx-Community 3.6 release 在Android上不能解密 encrypt_res 加密的资源文件

存在 bug 的版本

Quick-Cocos2dx-Community 3.6 Release。

bug 将在 Quick-Cocos2dx-Community 3.6.1 中修正。

bug 描述

在 3.6 中加入了encrypt_res进行资源加密,
在 FileUtils 中读取文件时会自动判断加密的文件并进行解密。

然而由于 Cocos2d-x 3.3 糟糕的移植层代码,加上我的粗心,没有测试Android上的解密,从到引入了这个bug。

Cocos2d-x 3.3 文件这块的移植层代码存在于cocos/platform下,Android与win32对应的porting代码,错误的override了父类的getStringFromFilegetDataFromFile函数。从而导致解密逻辑被错误的重写。

解决方案

去掉这部分错误的重写,并让getData被重写。

patch 信息如下:

From 01c61278651a2f69c82baeaf1f8f6da35d700ee3 Mon Sep 17 00:00:00 2001
From: u0u0 <lkncastle@gmail.com>
Date: Thu, 12 Nov 2015 21:51:38 +0800
Subject: [PATCH] remove wrong override function in Android and win32, make
 xxtea file decode correctly.

---
 cocos/platform/CCFileUtils.cpp                 |  2 +-
 cocos/platform/CCFileUtils.h                   | 14 ++++++++++++--
 cocos/platform/android/CCFileUtils-android.cpp | 15 ---------------
 cocos/platform/android/CCFileUtils-android.h   | 13 +------------
 cocos/platform/win32/CCFileUtils-win32.cpp     | 19 +------------------
 cocos/platform/win32/CCFileUtils-win32.h       | 18 ++----------------
 6 files changed, 17 insertions(+), 64 deletions(-)

diff --git a/cocos/platform/CCFileUtils.cpp b/cocos/platform/CCFileUtils.cpp
index 9e3ade8..e626757 100644
--- a/cocos/platform/CCFileUtils.cpp
+++ b/cocos/platform/CCFileUtils.cpp
@@ -534,7 +534,7 @@ void FileUtils::purgeCachedEntries()
     _fullPathCache.clear();
 }
 
-static Data getData(const std::string& filename, bool forString)
+Data FileUtils::getData(const std::string& filename, bool forString)
 {
     if (filename.empty())
     {
diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h
index 3b9f278..3a765a6 100644
--- a/cocos/platform/CCFileUtils.h
+++ b/cocos/platform/CCFileUtils.h
@@ -80,14 +80,16 @@ public:
     
     /**
      *  Gets string from a file.
+     *  IMPORTANT: DO NOT OVERRIDE ME!
      */
-    virtual std::string getStringFromFile(const std::string& filename);
+    std::string getStringFromFile(const std::string& filename);
     
     /**
      *  Creates binary data from a file.
+     *  IMPORTANT: DO NOT OVERRIDE ME!
      *  @return A data object.
      */
-    virtual Data getDataFromFile(const std::string& filename);
+    Data getDataFromFile(const std::string& filename);
     
     /**
      *  Gets resource file data
@@ -101,6 +103,14 @@ public:
     CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t *size);
 
     /**
+     *  Get File date from file.
+     *  OVERRIDE ME, if not use fopen to get file data
+     *  @param[in] forString, openfile with string mode or not.
+     *  @return A data object
+     */
+    virtual Data getData(const std::string& filename, bool forString);
+
+    /**
      *  Gets resource file data from a zip file.
      *
      *  @param[in]  filename The resource file name which contains the relative path of the zip file.
diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp
index a196626..4ab006a 100644
--- a/cocos/platform/android/CCFileUtils-android.cpp
+++ b/cocos/platform/android/CCFileUtils-android.cpp
@@ -292,21 +292,6 @@ Data FileUtilsAndroid::getData(const std::string& filename, bool forString)
     return ret;
 }
 
-std::string FileUtilsAndroid::getStringFromFile(const std::string& filename)
-{
-    Data data = getData(filename, true);
-    if (data.isNull())
-        return "";
-
-    std::string ret((const char*)data.getBytes());
-    return ret;
-}
-    
-Data FileUtilsAndroid::getDataFromFile(const std::string& filename)
-{
-    return getData(filename, false);
-}
-
 unsigned char* FileUtilsAndroid::getFileData(const std::string& filename, const char* mode, ssize_t * size)
 {    
     unsigned char * data = 0;
diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h
index 7f65d4a..0e618c4 100644
--- a/cocos/platform/android/CCFileUtils-android.h
+++ b/cocos/platform/android/CCFileUtils-android.h
@@ -62,27 +62,16 @@ public:
     bool init();
 
     virtual std::string getNewFilename(const std::string &filename) const override;
+    virtual Data getData(const std::string& filename, bool forString) override;
 
     /** @deprecated Please use FileUtils::getDataFromFile or FileUtils::getStringFromFile instead. */
     CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t * size) override;
 
-    /**
-     *  Gets string from a file.
-     */
-    virtual std::string getStringFromFile(const std::string& filename) override;
-    
-    /**
-     *  Creates binary data from a file.
-     *  @return A data object.
-     */
-    virtual Data getDataFromFile(const std::string& filename) override;
-
     virtual std::string getWritablePath() const;
     virtual bool isAbsolutePath(const std::string& strPath) const;
     
 private:
     virtual bool isFileExistInternal(const std::string& strFilePath) const;
-    Data getData(const std::string& filename, bool forString);
 
     static AAssetManager* assetmanager;
 };
diff --git a/cocos/platform/win32/CCFileUtils-win32.cpp b/cocos/platform/win32/CCFileUtils-win32.cpp
index 1497b37..c4befa2 100644
--- a/cocos/platform/win32/CCFileUtils-win32.cpp
+++ b/cocos/platform/win32/CCFileUtils-win32.cpp
@@ -143,7 +143,7 @@ bool FileUtilsWin32::isAbsolutePath(const std::string& strPath) const
     return false;
 }
 
-static Data getData(const std::string& filename, bool forString)
+Data FileUtilsWin32::getData(const std::string& filename, bool forString)
 {
     if (filename.empty())
     {
@@ -207,23 +207,6 @@ static Data getData(const std::string& filename, bool forString)
     return ret;
 }
 
-std::string FileUtilsWin32::getStringFromFile(const std::string& filename)
-{
-    Data data = getData(filename, true);
-   if (data.isNull())
-   {
-       return "";
-   }
-
-    std::string ret((const char*)data.getBytes());
-    return ret;
-}
-    
-Data FileUtilsWin32::getDataFromFile(const std::string& filename)
-{
-    return getData(filename, false);
-}
-
 unsigned char* FileUtilsWin32::getFileData(const std::string& filename, const char* mode, ssize_t* size)
 {
     unsigned char * pBuffer = nullptr;
diff --git a/cocos/platform/win32/CCFileUtils-win32.h b/cocos/platform/win32/CCFileUtils-win32.h
index 9495888..8617f18 100644
--- a/cocos/platform/win32/CCFileUtils-win32.h
+++ b/cocos/platform/win32/CCFileUtils-win32.h
@@ -56,27 +56,13 @@ protected:
     virtual bool isFileExistInternal(const std::string& strFilePath) const;
     
     /**
-     *  Gets resource file data
-     *
-     *  @param[in]  filename    The resource file name which contains the path.
-     *  @param[in]  mode        The read mode of the file.
-     *  @param[out] size        If the file read operation succeeds, it will be the data size, otherwise 0.
-     *  @return Upon success, a pointer to the data is returned, otherwise NULL.
-     *  @warning Recall: you are responsible for calling delete[] on any Non-NULL pointer returned.
-     */
-    virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t * size) override;
-
-    /**
-     *  Gets string from a file.
-     */
-    virtual std::string getStringFromFile(const std::string& filename) override;
-    
-    /**
      *  Creates binary data from a file.
      *  @return A data object.
      */
     virtual Data getDataFromFile(const std::string& filename) override;
 
+   virtual Data getData(const std::string& filename, bool forString) override;
+
     /**
      *  Gets full path for filename, resolution directory and search path.
      *
-- 
1.8.4.2

commit 信息01c61278651a2f69c82baeaf1f8f6da35d700ee3

标签: lua

?>