0

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.

5
  • Hi - plist are quite common in the iOS so I would suggest keeping it and I am sure you'll have functionality to manipulate it in Unity. The idea of a maintenance release will give you headaches a plenty, some users will skip it and just upgrade to the last version. The important question is - where do you add this plist file? Is it already overwritten when you update - then you have trouble. Do you keep it between (old ObjC) upgrades - then it should be easy. Commented Nov 9, 2020 at 6:09
  • @skaak thank you, good points. ...especially about maintenance release. Commented Nov 9, 2020 at 7:48
  • I do something similar - have an app that used to stored everything under Documents, even non-document related files. I want to clean that up a bit and move some of those files into Application Support. For me this is easy, I check on startup for a BOOL in user defaults and if not yet set, I move all the non-document stuff to Application Support and set the BOOL. If your plist is not overwritten between installs then you have an even easier task provided you can access it from Unity / C#. Then you need not take any action. Commented Nov 9, 2020 at 8:33
  • @skaak I am trying something similar and try to list the apps Documents folders files as well as the directories one level up just to see what is there. I get access denied. Do you know how to solve that if it is even possible? Commented Nov 9, 2020 at 10:36
  • I don't think you can go up from Documents ... this may help developer.apple.com/library/archive/documentation/… in terms of what is accessible and where to put what. Commented Nov 9, 2020 at 11:16

2 Answers 2

1

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:.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.