skip to Main Content

I have an old Xcode/ObC quiz game that I launched quite a few years ago, pre-swift, that has been, and still is quite successful for me. At least the local version.

I am now at the finish line rewriting this game in Unity3d c#.

Something I have been thinking about lately is how to to maintain the "old" statistics that is saved in a plist file in IOS. I have been google this but I would need some more information to really understand how to proceed.

  1. What will happen with the stats-plist file when I upgrade the current Xcode/ObC with the new Unity-based project, will it still be there and is it possible to easily find it? This particular plist is added when the first player is added and then updated with stats and new players.

  2. Is there a good, and easy, way reading plist from Unity and convert to a normal text file?

  3. To be able to find the file from Unity I am thinking of launching a maintenance release of the ObC based game and only copy this plist file to another directory (Document) to prepare for the new big release. When starting the Unity-based game for the first time I could then read the copied file and process so the player do not lose his/her stats.

The problem I have is that the only time I have updated the actual ObC code the last 5 – 6 years is when I updated the app from 32 to 64 bit so my skills on ObC is very limited at the moment.

I have been thinking of using something like this for the plist:

NSFileManager *filemgr;

filemgr = [NSFileManager defaultManager];

if ([filemgr copyItemAtPath: @"/tmp/myfile.txt" toPath: @"/Users/demo/newfile.txt" error: NULL]  == YES)
    NSLog (@"Copy successful");
else
    NSLog (@"Copy failed");

I would really appreciate some advise how I should process this.

2

Answers


  1. Chosen as BEST ANSWER

    Well, today I started to look back on this "problem" and after testing it turned out not to be a problem at all. I tried initially to find the path via my Objective-C code (this post) but when testing I realised that the files were stored in the (IOS game) Document directory, the same as "Application.persistentDataPath". In Unity I used the following path's:

    filePathPlayerData = Application.persistentDataPath + "/playerdata.plist";
    filePathPlayerStats = Application.persistentDataPath + "/playerstatistics.plist";
    

    Reading and process the file is easy as well as it is XML structure. However due to the simple format I opened the files as text files wrote my own simple parser in c# (unity), rather than using an XML parser. The whole "problem" turned out not to be any problem.


  2. Here is some code you can use to list the doc and app support contents. I guard this with the #define as I do not want this in the final app. I also use it to perform some cleanup (the commented out stuff) that you can use if you need to delete something.

    #ifdef DEBUG
    // Auxiliary function to list directory contents
    + (void) docList
    {
        NSFileManager * fileManager = NSFileManager.defaultManager;
        NSURL         * docUrl      = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
        NSString      * docPath     = docUrl.path;
    
        NSLog( @"User document directory" );
        NSLog( @"%@ listing follows", docPath );
        [fmUtil traverse:docPath tab:@"t" fileManager:fileManager];
    }
    
    // Auxiliary function to list application support path
    + (void) supList
    {
        NSFileManager * fileManager = NSFileManager.defaultManager;
        NSURL         * docUrl      = [NSFileManager.defaultManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask].firstObject;
        NSString      * docPath     = docUrl.path;
    
        NSLog( @"Application support directory" );
        NSLog( @"t%@", docPath );
        NSLog( @"tAppending bundle identifier" );
        docPath = [docPath stringByAppendingPathComponent:NSBundle.mainBundle.bundleIdentifier];
        NSLog( @"t%@", docPath );
        NSLog( @"%@ listing follows", docPath );
    
        [fmUtil traverse:docPath tab:@"t" fileManager:fileManager];
    }
    
    + (void) traverse:(NSString *)root tab:(NSString *)tab fileManager:(NSFileManager *)fileManager
    {
        NSArray * dir = [fileManager contentsOfDirectoryAtPath:root error:NULL];
    
        for ( NSString * s in dir )
        {
            // See if this is a directory or not
            NSString * t = [root stringByAppendingPathComponent:s];
            BOOL isDir = NO;
    
            [fileManager fileExistsAtPath:t isDirectory:& isDir];
    
            if ( isDir )
            {
                // Report
                NSLog(@"%@%@/",tab,s);
    
                // Traverse
                [fmUtil traverse:t tab:[tab stringByAppendingString:@"t"]
                        fileManager:fileManager];
            }
            else
            {
                // Get size of normal file
                NSDictionary       * fa = [fileManager attributesOfItemAtPath:t error:NULL];
                NSNumber           * fz = [fa objectForKey:NSFileSize];
                NSDate             * fm = [fa objectForKey:NSFileModificationDate];
                unsigned long long    n = fz.unsignedLongLongValue;
    
                // Some formatting
                NSString * f = s;
    
                while ( f.length < 50 )
                {
                    f = [f stringByAppendingString:@" "];
                }
    
                NSLog(@"%@%@ %15llu bytes (%@)", tab, f, n, [fm descriptionWithLocale:NSLocale.currentLocale] );
    
                // To delete something for test purposes ...
                /*
                if ( [t.lastPathComponent isEqualToString:@"P5041-1-BuildingStatistics"] && [fileManager removeItemAtPath:t error:NULL] )
                {
                    NSLog( @"%@%@ now xxxxxxxxxxxxxxxxxx", tab, f );
                }
                if ( [t.lastPathComponent isEqualToString:@"index"] && [fileManager removeItemAtPath:t error:NULL] )
                {
                    NSLog( @"%@%@ now xxxxxxxxxxxxxxxxxx", tab, f );
                }
                */
            }
        }
    }
    #endif
    

    Put this in your app delegate e.g. inside application:didFinishLaunchingWithOptions:.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search