How to Delete/Remove a Property from a SharedObject in AS3 - actionscript-3

I have a SharedObject in which I'm saving some data in a flash app. I'm trying to remove some saved data but it doesn't work.
trace(delete(localData.data[key]));
The delete command returns true, but when I reload the data it looks it was not deleted.
As of now the best solution was to "localData.clear();" all the data and then to save it without the fields I want to remove.

You need to call the flush() method to "commit" your changes:
Immediately writes a locally persistent shared object to a local file.
If you don't use this method, Flash Player writes the shared object to
a file when the shared object session ends — that is, when the SWF
file is closed, when the shared object is garbage-collected because it
no longer has any references to it, or when you call
SharedObject.clear() or SharedObject.close().
More info here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html#flush()

Related

AS3 Sending a Shared Object

I'm working on an application that would allow users to create a custom character sheet for role play games. I have most of the code figured out, but I want users to be able to send their character sheets between devices.
So here's the question: is there a way to save and send a shared object file, or a way to create a txt file that can easily be saved and copied?
I don't believe you can send a SharedObject from one device to another, at least without a lot of work. You could however create an XML file containing the data and save that up to a server. You could allow the user to then download character sheets from your server and the app would read the XML data before converting to a SharedObject. Can't really provide any code for this as the details are lacking.
If I understand you correctly, you could sort of do this.
You cannot literally "send and receive" a SharedObject (well, you might be able to copy your shared object data on the file system directly, but not from Flash).
What you can do is provide options to the user to save and load a file that encodes all the shared data in AMF bytes. Here's the general idea:
First you need to give the user an option to save their data. You can use ByteArray/writeObject() to write your data using the same AMF format that SharedObjectuses, and FileReference/save() to allow the user to save it to a file on their file-system.
Next, you can use FileReference/load() to load the file and ByteArray/readObject() to read all the data into AS3. Now you can simply store it in the SharedObject however you want, just like you did before.

delete external text file from .swf file

I have a swf file (game file) which reads the content of external text file which I have created. This swf file will not be used in any browser. It's for windows only. I was wondering if I can delete or update that text file after I read it's content without using air.
Your question isn't very clear, I'm not sure what exactly you wanted to know. But if I understood you correctly:
You cannot delete files from the filesystem in the through Flash Player, but you can however delete it from SWF file's memory.
Deleting from the file system (basically like putting into trash) is something that only AIR can help you with.
Deleting from memory would mean to remove all the listeners from URLLoader or Loader object instance and setting it to null.
You cannot write the file with Flash player to edit it's content (update), but you can load it again and check if anything changed in that file's content that also would be considered as updating.
Hope that answers your question.
you cant do that without air. but you can use an external application using fscommand().
or you can use Multimedia Zinc

Flash AS3 Main swf's binary checksum

I have a 'Shell' swf as the main swf to protect other things in the project.
Shell swf's procedure is:
check stage property, so it can't be loaded as a child.
check Capabilities.isDebugger, so it can't be run in a debugger.
load a decoder lib. the decoder lib is built by flascc, quite difficult to decompile.
use decoder to validate self. if failed or skipped, decoder won't do any decoding job.
load encoded main entry of the project, decode it & add it to stage.
The problem is step 4, I want to do binary checksum of Shell swf.
Questiones are:
How can i get the original binary content of the Shell swf file on startup ?
if not, is Shell swf's LoaderInfo.bytes consistent in different versions of flash player ?
if not, is there any good way to do the validation job in step 4 ?
If you create an AIR app you can temporarily load original file ("Shell.swf") in application bundle using standard URLLoader or File and FileStream.
If your app will work in browser then you can get self-file-name and URL from loaderInfo and load it using URLLoader.
Anyway after your verification process complete you need unload it.

sharedObject location

i'm using this method to save shared object in specific location but i'm not able to give path
SharedObject.getLocal("save","how to give path here for window");
and this gives following error
Error: Error #2130: Unable to flush SharedObject.
at Error$/throwError()
at flash.net::SharedObject/flush()
at OnlineDiagramEditor/SaveData_mouseDownHandler()[C:\Users\ruchidod\Adobe Flash Builder 4.6\OnlineDiagramEditor\src\OnlineDiagramEditor.mxml:3240]
at OnlineDiagramEditor/__SaveData_mouseDown()[C:\Users\ruchidod\Adobe Flash Builder 4.6\OnlineDiagramEditor\src\OnlineDiagramEditor.mxml:3522]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152]
at mx.managers::SystemManager/mouseEventHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2918]
some times it gives
Error: Error #2134: Cannot create SharedObject.
at flash.net::SharedObject$/getLocal()
at OnlineDiagramEditor/SaveData_mouseDownHandler()[C:\Users\ruchidod\Adobe Flash Builder 4.6\OnlineDiagramEditor\src\OnlineDiagramEditor.mxml:3236]
at OnlineDiagramEditor/__SaveData_mouseDown()[C:\Users\ruchidod\Adobe Flash Builder 4.6\OnlineDiagramEditor\src\OnlineDiagramEditor.mxml:3522]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152]
at mx.managers::SystemManager/mouseEventHandler()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\SystemManager.as:2918]
SharedObjects are automatically saved in a predefined location and they aren't cross-domain, that means that each domain can read only its own SO.
The localPath is only a parameter that defines in which subfolder of the predefined location Flash player should save the SO. This is, by default, referred to the SWF that is saving the SO itself.
localPath: String [optional]- A string that specifies the full or partial path to the SWF file that created the shared object, and that determines where the shared object is stored locally. The default value is the full path.
Also:
Although the localPath parameter is optional, you should give some thought to its use, especially if other SWF files need to access the shared object. If the data in the shared object is specific to one SWF file that will not be moved to another location, then use of the default value makes sense. If other SWF files need access to the shared object, or if the SWF file that creates the shared object will later be moved, then the value of this parameter affects whether any SWF files are able to access the shared object. For example, if you create a shared object with localPath set to the default value of the full path to the SWF file, then no other SWF file can access that shared object. If you later move the original SWF file to another location, then not even that SWF file can access the data already stored in the shared object.
So if you've multiple SWF on the same domain which need to access the same SO, you need to give a path to getLocal(), the same for each SWF.
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001508.html
For example:
In Windows, SO are stored in %APPDATA%\Macromedia\Flash Player\#SharedObjects\.
Here is a subfolder for your user, like 5D3RT21F.
Then we have a subfolder for the domain that originated the SO file, like www.flickr.com.
Then several subfolders to reflect the relative path of the SWF on the domain, like \apps\slideshow\show.swf\, you can find the SO file in here.
But if you specify / (root) as localPath then you'll find the SO file directly in www.flickr.com and other SWF on the same domain will access it freely.

How to load a local directory in as3

I want to load a directory in actionscript. To do that I use the File class but sadly it doesn't work.
Here is what I did. First I created the directory in the applicationStorageDirectory using this code:
var root:File = File.applicationStorageDirectory.resolvePath(UIDUtil.createUID());
root.createDirectory();
After that I dispatch an event so that the application copies some files into that folder. To do that I use the method File.copyToAsync (that works, so this is not the problem). When all files are copied to that directory I try to load that directory using the File call again. The reason I want to do that is, that I need the content/data as a byte array. The data property of the File class is only set when the load method is called successfully before.
Since the directory is in the application storage directory (the url property of the file object has this content: app-storage:/FF011DBC-2E92-46A8-D5F8-29FE1DD8FA7A) I thought just calling the load method would work. I was wrong... Although the documentation of that method says I can just call that method when the application runs in AIR and the file to load is inside the application sandbox, I get the following error:
Error #2044: Unhandled IOErrorEvent:. text=Error #2038: File I/O Error.
at flash.filesystem::File/resolveComponents()
at flash.filesystem::File/resolvePath()
...
Do you have any ideas what could be wrong? Is it even possible to load a directory using the File class? Or does that error occur because the isDirectory flag is set to true in the file object?
kind regards
Markus
I believe a File, when pointing to a directory, does not contains the data of the files contained inside it. You would have to use File.getDirectoryListing() to loop over all files the folder, and append their bytes to the ZIP archive separately. Don't forget to check if you come across sub-directory...
You can check this link for an example of how to do this.