file inside a package - actionscript-3

I want to use the as3 File() method to import an xml into a file.
The file is inside my project in the package resources/xml/baseXml.xml
Now the File() method has several properties like:
applicationDirectory, applicationStorageDirectory, desktopDirectory, documentsDirectory
But none of them points me in the right direction. So how should i do this? To get the xml file inside the package?
I have tried to embed the file to be then i have a Class and not a File.

Do you NEED to use File()? What if you were to just get the xml file with httpService?
Try:
<s:HTTPService url="resources/xml/baseXml.xml" result="yourResultHandlerToParseTheXML(event)"/>
Or you could just do:
<fx:XML source="resources/xml/baseXml.xml"/>
And parse off of that.
(Both would be located inside your tags.
Unless you need an ActionScript only solution in which case the below SHOULD work:
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.load("resources/xml/baseXml.xml");

Related

Using a relative path for File() in Flutter

I am working in Flutter and trying to open a json file in my assets folder. I found I can use the File() method, but it only seems to take an absolute path. Is there a way I can convert this to a relative path? I've tried using the relative path to the file already, but it returns an error saying no such file.
Here is the code so far. Basically I want to get the json file, and return it as a string (in the function readFileSync() below). Then I use that data to create a List object. If there's a better way to read a file into Flutter, I'm open to that too!
List<Answers> myFunction2() {
String arrayObjsText = readFileSync();
//print(arrayObjsText);
var tagObjsJson = jsonDecode(arrayObjsText)['tags'] as List;
var tagObjs =
tagObjsJson.map((tagJson) => Answers.fromJson(tagJson)).toList();
return tagObjs;
}
String readFileSync() {
String contents = new File(
'/Users/pstumbaugh/Documents/Computer Science/CS492 Mobile Dev/Dart-Flutter-CallMeMaybe/project3/assets/answers.json')
.readAsStringSync();
return contents;
}
I don't know much about how Futures work. I tried with those, but it seems like it always returns a Future and I'm not sure how to unpack that down to just a string without having to make everything async functions, which then led to problems when I try to get the List in my widgets on the main page...
You should to get assets not from relative path from your PC. When you install an app for a device or a emulator/simulator, it is can't access files on your computer. In few words, you can do it with loadString method from flutter/services.dart package (it is in Flutter SDK by default):
import 'package:flutter/services.dart' show rootBundle;
final data = rootBundle.loadString('assets/answers.json');
And make sure that you declared assets in pubspec.yaml config. Here is an official tutorial for how to work with assets.

Embedding a file with a variable in AS3 + flixel

I have recently picked up flixel (I have programmed before, but I have not in a while) and I have come across a problem. I am attempting to create maps, and eventually there will be multiple maps available.
I currently have a .txt file that has information that eventually goes into an array. Then I go from array to map with loadmap. It is maybe a simple way to accomplish this task, and maybe their are better ways (I have not explored all the possibilities with flixel, and if there are any opinions, go ahead and let me know) but it works good for now.
As I have previously said, I am trying to do this with multiple maps. I could do this by using [Embed(source = "")] for each .txt file, but this may end up being annoying. So, here is my question: Is there a possible way Embed a file based upon a variable?
My Map class looks like this:
public function Map(MapSet:String, TileSet:String)
{
super(MapSet, TileSet);
//more stuff
}
Now I have tried:
[Embed(scource="data/MapSets/" + MapSet + ".txt", mimeType = "application/octet-stream")]private var loadedMap:Class
and then I use:
map = new Map("Map1x1", "ForestTiles");
add(map);
Is there a possibility of doing this in a different way? Or maybe I am doing something wrong? All opinions are welcome.
It's beneficial to know what code does when using it.
Embed is a meta tag. It tells the compiler to include a certain file into the .swf file.
That means this does not happen at runtime.
When this embed code is "executed", your variables don't even exist yet.
That's why your code cannot work.
Despite not working, your solution is still valid:
If you find it tedious to generate code, write a program that does this for you. Create/use a program that finds all valid files in the given directory and creates all the embed tags. Run this program before the compiler.
To embed a text file and use as a string, try this:
// create source var TextSource
[Embed(source="textFile.txt",mimeType="application/octet-stream")]
private var TextSource:Class;
var myByteArray:ByteArray = new TextSource();
var myString:String = myByteArray.readUTFBytes(myByteArray.length);
// then use for your function
map = new Map("Map1x1", myString);

Design time instantiation issues when accessing xml file using XDocument.Load

In my windows store app using the Visual Studio 2012 designer I want to be able to load some model objects for the designer. I've done this plenty of times before where I supply a xaml file using the ms-appx:/// uri without error. However, for this project I need to be able to instantiate a class and have it convert raw xml of a different format into my model objects.
I'm using the following xaml to instantiate my class for the designer:
d:DataContext="{Binding Source={d:DesignInstance Type=model:Walkthroughs, IsDesignTimeCreatable=True}}"
In my Walkthroughs class had code that did this initially:
public Walkthroughs()
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
AppDataLoader.LoadWalkthroughs(this, XDocument.Load("ms-appx:///SampleData/walkthroughs.xml"));
}
I first ran into an issue where the XDocument.Load did not understand the ms-appx:/// uri so I modified my code to something very simplistic:
AppDataLoader.LoadWalkthroughs(this, XDocument.Load(#"C:\walkthroughs.xml"));
Now I get access to path '' is denied.
I've tried several directories as well to no avail. I'm even running Visual Studio as an Administrator. If I remove the prefix altogether I get the following error:
Could not find file 'C:\Users\{me}\AppData\Local\Microsoft\VisualStudio\11.0\Designer\ShadowCache\omxyijbu.m4y\yofsmg1x.avh\walkthroughs.xml'.
Has anyone been able to load files from the file system when the designer instantiates objects?
Thanks,
-jeff
XDocument.Load(string uri) seems to have problems with loading Project resources from ms-appx:/
Regarding your second approach: Direct access to "C:" is not permitted. Ther is only a handful of special folders that you can access. Check out my workaround for this (my xml file is within the Assets folder of my project:
var storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
storageFolder = await storageFolder.GetFolderAsync("Assets");
var xmlFile = await storageFolder.GetFileAsync("data.xml");
var stream = await xmlFile.OpenReadAsync();
var rdr = new StreamReader(stream.AsStream(), System.Text.Encoding.GetEncoding("ISO-8859-1")); //needed if you have "ä,ß..." in your xml file
var doc = XDocument.Load(rdr);

Include XML (or JSON) data during publish time- Flash

I have a .fla that gets opened up, a few actionscript variables (a url, title etc.) are changed, and published. This happens a lot, and it's the same variables. I would just keep the .swf file with an XML file, but it being uploaded to a third-party platform, so all of the information needs to be contained in the .swf file. There is no way to add the variable information to the third-party site.
I want to know if there's a way to take the new variable information from an xml file or something at publish time (like a script?) without having to open up the Flash IDE. To be able to do a bunch of these at once would also be great.
Any help/links/leads would be appreciated. Google did not help me. Is this even possible?
Without opening the Flash IDE would require loading external resources or a build script using ANT or simply with the mxmlc compiler, something to the effect of:
mxmlc -o output.swf -source-path="src/" -library-path+=library.swc.
Flex compilers
About mxmlc
As you indicate embedding XML at compile time, you could either embed the XML using the [Embed] metadata tag or paste your XML in a class.
Embed XML
package
{
public class XmlData
{
[Embed(source = "data.xml", mimeType = "application/octet-stream")]
public static const Xml:Class;
}
}
To use the XML, instantiate the xml as:
var xml:XML = new XML( new XmlData.Xml );
XML variable
Otherwise, you can simply paste your xml in a class like so:
package
{
public class XmlData
{
public static const xml:XML =
<root>
<element />
<element attribute="value">data</element>
</root>;
}
}
Although you must compile your SWF, this approach is easy because you can simply paste your XML document to the class.
This would be referenced as normal with e4x and no asynchronous load required.
var data:String = XmlData.xml.element.#attribute;

Packaging External File Dependencies in Flash

I just started learning ActionScript, so maybe this is obvious. I'm loading in .txt files to generate content for my Flash application. However, I can't seem to find a way to package the .txt files with the .swf when I publish my application. I'd like to be able to run the .swf from anywhere without it depending on the files. Is there a solution for this?
Thanks!
this is an excellent post by Emanuele Feronato:
how-to-embed-a-text-file-in-flash
Basically you use the embed syntax to embed the file as a ByteArray. The key method here is to call the toString() method on the ByteArray to convert to a string.
Hope this helps
Try loading .as files, with variables defined inside them. They're, technically, still text files and still easy to edit, but they get automatically encapsulated in the .swf.
In your main stage, you just add include 'included.as';. In your included.as file, you just define whatever variables you want, for example:
var someData:Array = new Array();
someData.push('This is some string');
someData.push('This is some other string');
After you include the as, you can call whatever variable you defined. And when you export, the .as file is embedded into the .swf.
Even better then just embedding a .txt with your flash, if you're more versed with programming I would suggest you to create a static class to store the values of your application.
Just like:
class StaticData {
static public var siteTitle:String = "My site name";
static public var homeMessage:String = "Lorem Ipsum dolor sit amet...";
static public var welcomeMessage:String = "Hello World";
static public var siteWidth:Number = 1024;
static public var siteHeight:Number = 780;
}
This is a safe and realiable way to store static values using a simple class structure, to use a value from the class, you just need to call inside your flash:
my_textfield.text = StaticData.siteTitle;
my_shape.width = StaticData.siteWidth;