CocosBuilder icon indicating copy to clipboard operation
CocosBuilder copied to clipboard

Resource finding is not robust

Open danielhaaser opened this issue 12 years ago • 1 comments

There are some corner cases in which certain directory structures will not work with CocosBuilder. The problem lies with the relativePathFromAbsolutePath method in ResourceManagerUtil.

Here's an example of a project setup that's broken:

cocosbuilder-directory-structure

cocosbuilder-resource-settings

When I set a sprite frame to be a resource in the "Cocosbuilder Raw Assets Android" directory, the relativePathFromAbsolute path returns a bad relative path. The issue lies with "Cocosbuilder Raw Assets Android" having the same prefix as the "Cocosbuilder" directory in which the project file lives.

+ (NSString*) relativePathFromAbsolutePath: (NSString*) path
{
    NSArray* activeDirs = [[ResourceManager sharedManager] activeDirectories];

    for (RMDirectory* dir in activeDirs)
    {
        // Old Code
        //NSString* base = dir.dirPath; 

        // New Code
        NSString* base = [NSString stringWithFormat:@"%@/",  dir.dirPath];  

        if ([path hasPrefix:base])
        {
            // Old Code
            //NSString* relPath = [path substringFromIndex:[base length]+1];

            // New Code
            NSString* relPath = [path substringFromIndex:[base length]];
            return relPath;
        }
    }

    NSLog(@"WARNING! ResourceManagerUtil: No relative path");
    return NULL;
}

Replacing the two lines indicated seems to fix the issue for me, and works in brief testing. I'm new to Git, so sorry about not having a pull request. I'm also not sure this change won't introduce other bugs or side-effects.

danielhaaser avatar Apr 09 '13 15:04 danielhaaser

This needs further investigation!

vlidholt avatar Apr 18 '13 21:04 vlidholt