Cocos2dx的cache细节,资源路径不规范,容易导致cache中存在多份

简介: Cocos2dx的cache细节,资源路径不规范,容易导致cache中存在多份

SpriteFrameCache

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("");
复制代码

TextureCache

std::unordered_map<std::string, TexInfo*> _textures;
复制代码

key的来源

Texture2D * TextureCache::addImage(const std::string &path){
    std::string fullpath = GetFullPath(path);
    auto it = _textures.find(fullpath);
}
std::string GetFullPath(std::string path){
    std::string fullPath = FileUtils::getInstance()->fullPathForFilename(path);
    if (fullPath.length() > 0){
        return fullPath;
    }else{
        return path;
    }
}
std::string FileUtils::fullPathForFilename(const std::string &filename) const
{
    if (path[0] == '/')
    {
        return filename;
    }
    std::string name = filename;
    int index = name.find("/../");
    while (index > 0) {
            int last = name.find_last_of("/", index-1);
            if (last > 0) {
                    name = name.substr(0, last) + name.substr(index+3, name.length());
            }
            else {
                    break;
            }
            index = name.find("/../");
    }
    // Already Cached ?
    auto cacheIter = _fullPathCache.find(name);
    // 又一个很重要的cache: mutable std::unordered_map<std::string, std::string> _fullPathCache;
    if(cacheIter != _fullPathCache.end())
    {
        return cacheIter->second;
    }
    // Get the new file name.
    const std::string newFilename(getNewFilename(name));
    std::string fullpath;
    for (const auto& searchIt : _searchPathArray){
        for (const auto& resolutionIt : _searchResolutionsOrderArray){
            fullpath = this->getPathForFilename(newFilename, resolutionIt, searchIt);
            if (!fullpath.empty()){
                // Using the filename passed in as key.
                _fullPathCache.emplace(name, fullpath);
                return fullpath;
            }
        }
    }
    return "";
}
复制代码

测试结果

网络异常,图片无法展示
|

  • name为ani1/test.png,在textureCache中的映射key为res/ani1/test.png
  • name为res/ani1/test.png,在textureCache中的映射key为exePath/res/ani1/test.png

相当于加载的是磁盘的同一份图片,结果却产生了2个纹理,理想情况是产生1份,产生这个问题的原因是:

TextureCache的key受filePathCache的影响,而filePathCache受searchPath的影响

searchPath的目录的顺序为:

  • exePath
  • src
  • res

ani1/test.png只能在res目录下找到,res/ani1/test.png只能在exePath目录下找到,所以最终2者的key是完全不同的。

造成这个问题是因为我设置了一个无效的searchPath,而在win32平台判断文件是否存在发生了异常

bool FileUtilsWin32::isFileExistInternal(const std::string& strFilePath) const
{
    if (strFilePath.empty())
    {
        return false;
    }
    std::string strPath = strFilePath;
    if (!isAbsolutePath(strPath))
    { // Not absolute path, add the default root path at the beginning.
        strPath.insert(0, _defaultResRootPath);
    }
    DWORD attr = GetFileAttributesW(StringUtf8ToWideChar(strPath).c_str());
    if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
        return false;   //  not a file
    return true;
}
复制代码

当strFile为res/ani1/test.png返回的是FILE_ATTRIBUTE_ARCHIVE,isFileExistInternal直接返回了true,认为存在,准确说这个文件是相对于exe是存在的,但是这个路径不是完整路径

  • 测试代码:
char* file = "res/ani2/test.png"; // exe运行目录下真的有这个文件
//file = "c://a/1.png";
DWORD attr = GetFileAttributesW(StringUtf8ToWideChar(file).c_str());
if (attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
{
    cocos2d::log("file not exist");
}
else{
    cocos2d::log("file exist");
}
复制代码

为什么会出现search paths异常

FileUtils::getInstance()->addSearchPath("res");
void FileUtils::addSearchPath(const std::string &searchpath,const bool front)
{
    if (!isAbsolutePath(searchpath))
        prefix = _defaultResRootPath;
}
复制代码

当添加searchPaths的时候,如果是相对路径,则会追加_defaultResRootPath,而_defaultResRootPath在win32上的来源是ProjectConfig,在SimulatorWin::run中就会从命令行参数中解析workdir最终传递给_defaultResRootPath

void FileUtils::setDefaultResourceRootPath(const std::string& path)
{
    // 最主要的原因是这里缺少对path的校验
    if (_defaultResRootPath != path)
    {
        _fullPathCache.clear();
        _defaultResRootPath = path;
        if (!_defaultResRootPath.empty() && _defaultResRootPath[_defaultResRootPath.length()-1] != '/')
        {
            _defaultResRootPath += '/';
        }
        // Updates search paths
        setSearchPaths(_originalSearchPaths);
    }
}
复制代码

release出现问题就是因为args中缺少workdir

网络异常,图片无法展示
|
原始的win32项目填写了Command Arguments,所以规避了这个问题

结论

SearchPath的影响还是非常大的,写资源路径的时候,最好统一相对于一个目录写,不然cache中容易出现多份相同的资源。



目录
相关文章
|
JSON 应用服务中间件 nginx
filebeat收集json格式的nginx程序日志(二)
filebeat收集json格式的nginx日志 1.为什么要收集json格式的日志类型 由于nginx普通日志收集过来的日志内容都是存在一个字段中的值,我们想单独对日志中的某一项进行查询统计,比如我只想查看某个IP请求了我那些页面,一共访问了多少次,在普通的日志中是无法过滤的,不是很满意
1651 0
filebeat收集json格式的nginx程序日志(二)
|
5月前
|
消息中间件 运维 监控
短信isv.BUSINESS_LIMIT_CONTROL错误?阿里云国际站(云老大):原因与限流排查方法
线上做活动的关键时刻,用户注册、下单的短信一条都发不出去,后台日志里反复出现 isv.BUSINESS_LIMIT_CONTROL。这个错误不像欠费或签名审核失败那样直观,很多团队的第一反应是“被限流了”——但究竟限在哪一层,光是这几个字远不够。真正有效的阿里云短信isv.BUSINESS_LIMIT_CONTROL排查,得从理解它到底在拦截什么开始
【5分钟+】计算机系统结构:CPU性能公式
【5分钟+】计算机系统结构:CPU性能公式
2116 0
【5分钟+】计算机系统结构:CPU性能公式
|
Windows
Lua在Windows下的配置、安装、运行
Windows下安装、运行Lua! 本文提供全流程,中文翻译。Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) 1↓ 进入Lua官网:http://www.
2367 0
|
SQL Oracle 关系型数据库
"ORA-00942: 表或视图不存在 "的原因和解决方法
采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或视图不存在 ”,很是郁闷,这个问题以前出现过,当初解决了,但因好久没有使用,这次竟然忘了,害得我浪费了好些时间,为了避免再次忘记,将它记下来,同时和大家一起分享。
3292 0
数据结构学习笔记——前、中、后缀表达式的转换(栈的应用)
数据结构学习笔记——前、中、后缀表达式的转换(栈的应用)
数据结构学习笔记——前、中、后缀表达式的转换(栈的应用)
|
人工智能 自然语言处理 Java
IntelliJ IDEA常用插件推荐
“工欲善其事,必先利其器”,通过使用一些优秀的工具,可以帮助提高开发效率,本文给大家安利几款不错的IDEA插件。
29291 1
IntelliJ IDEA常用插件推荐
|
监控 Java 应用服务中间件
美团动态线程池思路开源框架(DynamicTp),动态调整Tomcat、Jetty、Undertow线程池参数篇
大家好,这篇文章我们来介绍下动态线程池框架(DynamicTp)的adapter模块,上篇文章也大概介绍过了,该模块主要是用来适配一些第三方组件的线程池管理,让第三方组件内置的线程池也能享受到动态参数调整,监控告警这些增强功能。
1625 1
美团动态线程池思路开源框架(DynamicTp),动态调整Tomcat、Jetty、Undertow线程池参数篇
|
安全 API Go
我的免杀之路:远程线程注入
远程线程注入技术能实现在Windows系统下进程的隐藏。其主要核心在于一个Windows API函数CreateRemoteThread,通过它可以在另外一个进程中注入一个线程并执行。
1798 0
我的免杀之路:远程线程注入
|
IDE 测试技术 API
手机自动化测试IDE-----Airtest实战篇
手机自动化测试IDE-----Airtest实战篇
1009 0
手机自动化测试IDE-----Airtest实战篇