1
votes

I have two apps, one is a trial version the other the full version of a game, both made with adobe air. While saving data via the sharedobjects solution is no problem, I would like to use "one" savegame for both appsm, so users can keep their progress when upgrading to the full version. I tried around a little. But code like e.g. ...:

SharedObject.getLocal("myApp","/");

... doesnt work. So the question is, is there a way to have two Air apps using the same shared object? Or maybe if not using, at least "read" the shared object of another Air app?

Thanks in advance, ANB_Seth

1
I'm not sure if Android sandbox apps similar to how iOS does but is it does this won't be possible.crooksy88

1 Answers

0
votes

The answer is yes, I actually made a game transfer system for iOS and Android via network connection and 6 digit hash the user has to enter in the newly installed app to fetch the SO from the server. You could do this with a simple file stored locally on the SD card or other local storage device.

/**
 * send this user's current save data to the server
 */
public function send():void{

    var ba:ByteArray = new ByteArray();

    // Main.sv.user - is the registerClassAlias object we write/read locally via SharedObject
    ba.writeObject(Main.sv.user);

    var name:String = Crypto.hash("Random Secrect Salt - typically user score, name, etc.");
    // create 6 digit hash
    var key:String = Crypto.hash(name).slice(0, 6).toUpperCase();

    var request:URLRequest = new URLRequest ( 'https://sharedobject.com/transfer/save/name/'+name+'/key/'+key );
    var loader: URLLoader = new URLLoader();
    request.contentType = 'application/octet-stream';
    request.method = URLRequestMethod.POST;
    request.data = ba;

    loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
        trace("error - network");
        onSaveRestoreEvent(1);
    });

    loader.addEventListener(Event.COMPLETE, function (evt:Event) {
        addChild(new BaseDialog("Save Sent To Server", "Your data has been sent to the server. To get this data back from the server " +
            "you will need your secret key. Please write this six digit key down:\n"+name));
    });

    loader.load( request );
}

/**
 * create a GET SO dialog
 */
public function receive():void{
    var text:Sprite = new Sprite();
    var textInput:TextInput = new TextInput();
    textInput.width = Constants.SCREEN_WIDTH-100;
    textInput.y = -50;
    text.addChild(textInput);

    var dialog:BaseDialog = new BaseDialog("Enter Secret Save Key", "Please enter your six digit secret save key in the field below, then press \"Get\".\n\n",
        "Get", function():void{
            text.removeChildren();

            var url:String = "https://sharedobject.com/transfer/get/name/"+textInput.text; //servlet url
            var request:URLRequest = new URLRequest(url);

            //get rid of the cache issue:
            var urlVariables:URLVariables = new URLVariables();
            urlVariables.nocache = new Date().getTime();
            request.data = urlVariables;
            request.method = URLRequestMethod.GET;

            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.BINARY;

            loader.addEventListener(Event.COMPLETE, function (evt:Event) {
                var loader:URLLoader = URLLoader(evt.target);

                var bytes:ByteArray = loader.data as ByteArray;
                bytes.position = 0;
                if(bytes.length <= 10 || !(bytes.readObject() is User)){
                    onSaveRestoreEvent(2);
                }else{
                    try{
                        bytes.position = 0;
                        Main.sv.user = (bytes.readObject() as User);
                        Main.sv.save();
                        onSaveRestoreEvent(0);
                    }
                    catch( e : EOFError ){
                        onSaveRestoreEvent(3);
                    }

                }
            });

            loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
                trace("error - network");
                onSaveRestoreEvent(1);
            });

            loader.load(request);

        },
        "Close", function():void{text.removeChildren();}, null, null, text);

    dispatchEvent(new CreateBaseDialogEvent(dialog));
}


/**
 * called after the restore save system is done
 * @param prompt int [0 = complete][1 = error network][2 = error key][3 = error EOF]
 */
private function onSaveRestoreEvent(prompt:int):void{
    var dialog:BaseDialog;
    if(prompt == 0){
        dialog = new BaseDialog("Restore Complete!", "All save data has been restored.");
    }else if(prompt == 1){
        dialog = new BaseDialog("Network Error!", "Please seek an internet connection and try again.");
    }else if(prompt == 2){
        dialog = new BaseDialog("Invalid Secret Key!", "The key you've entered seems to be invalid, or the save data has expired on the server. " +
            "Data only lasts on the server for 24 hours.");
    }else{
        dialog = new BaseDialog("Error!", "There was an issue getting the file from the server. Please try the transfer again.");
    }
    dispatchEvent(new CreateBaseDialogEvent(dialog));
}