Entity Editor - How to dynamically generate a list of components? - actionscript-3

I'm making a game and I an in-game editor that is able to create entities on the fly (rather than hard coding them). I'm using a component-aggregation model, so my entities are nothing but a list of components.
What would be the best way to obtain or generate a list of components? I really don't want to have to manually add entries for all possible components in some giant registerAllComponents() method or something.
I was thinking maybe somehow with reflection via either the knowledge that all components inherit from the base Component class, or possibly via custom metatags but I haven't been able to find ways to get a list of all classes that derive from a class or all classes that have custom metatags.
What sort of options am I left with?
Thanks.

For a project I did once, we used a ruby script to generate an AS file containing references to all classes in a certain package (ensuring that they were included in the compilation). It's really easy considering that flash only allows classes with the same name as the file it's in, so no parsing of actual code needed.
It would be trivial to also make that add an entry to a dictionary (or something similar), for a factory class to use later.
I believe it's possible to have a program execute before compilation (at least in flashdevelop), so it would not add any manual work.
Edit: I added a basic FlashDevelop project to demonstrate. It requires that you have ruby installed.
http://dl.dropbox.com/u/340238/share/AutoGen.zip

Unfortunately, there is no proper way of getting all loaded classes or anything like that in the Flash API right now. So finding all sub-classes of Component is out, inspecting all classes for a specific meta tag is out as well.
A while ago I did run into a class/function that inspected the SWF's own bytecode upon loading to retrieve all contained classes. That's the only option for this kind of thing. See this link and the bottom of my post.
So, you're left with having to specify a list of component classes to pick from.
One overly complicated/unfeasible option that comes to mind is creating an external tool that searches your source folders, parses AS3 code and determines all sub-classes of Component, finally producing a list in some XML file. But that's not a task for the faint-hearted...
You can probably think of a bunch of manual solutions yourself, but one approach is to keep an accessible Array or Vector.<Class> somewhere, for example:
public static const COMPONENT_LIST:Vector.<Class> = Vector.<Class>( [
CollisionComponent,
VisualComponent,
StatsComponent,
...
...
] );
One advantage over keeping a list of String names, for example, would be that the component classes are guaranteed to be compiled into your SWF.
If the classes aren't explicitly referenced anywhere else in your code, they are not compiled. This might occur for a simple component which you only update() once per frame or so, and is only specified by a string in some XML file.
To clarify: You could use the code in the link above to get a list of the names of all loaded classes, then use getDefinitionByName(className) for each of them, followed by a call to describeType(classObj) to obtain an XML description of each type. Then, parsing that for the type's super-types, you could determine if it extends Component. I personally would just hardcode a list instead; it feels too messy to me to inspect all loaded classes on startup, but it's up to you.

Related

How do I create a Processing library that adds a new function to the language?

I want to create a Processing library that adds a single function to Processing. A single command. How do I do this?
So I want to be able to write on Processing this:
void setup() {
drawMyCustomShape()
}
In a way that drawMyCustomShape will be on my custom library implementation.
Thanks!
Note: this question is not about creating a new library in processing. Is about creating a library that exports one new command (so you can using without caring of the container class instance).
First of all, are you sure you really need to create an entire library? You could just add that class to your sketch without needing to deploy it as a library. If you're worried about clutter, just put it in its own tab.
If you really need to create a library, then there are three tutorials that you need to read:
Library Overview
Library Basics
Library Guidelines
But basically, you need to create a Java project (in an IDE like eclipse, or with a basic text editor and the command line) that uses Processing as a library. That's where you'd put your MyLibrary class. You'd then export it as a .jar file, and then import that .jar file into Processing. You would then be able to use your class exactly like you can use any other Processing library.
Your proposed setup has some other issues (how are you going to access the sketch variable from the static function?), but I'd suggest treating them as separate questions after you get the basics in place.
It sounds like you are actually looking to create your own extension of the Processing library, as in actually change the core jar file.
You can extend the actual Processing library by forking off of its main branch on Github. By writing your function drawMyCustomShape into the actual core in the forked version, you can then build the Processing Development Environment from your copy of the code. Using that particular copy of the PDE, you could do what you're describing.
Once you compile this build, you could actually distribute this copy of the PDE to your college students. They would be able to use your function as if nothing were changed. (I'm guessing that this is for an intro-level class at the college level, so that's why you would have to hide implementation from your students?)
Here's some links to get you started:
Processing github
Instructions for building the PDE from source
So, finally I found the most adequate answer for my case.
The solution for this is to implement a new Processing Mode that extends the builtin Java Mode. To include static members to the main processing program you will need to add a new static import to the ones that processing adds to your code.
You can do this by forking the Mode Template for 3.0 that #joelmoniz created from #martinleopold:
https://github.com/joelmoniz/TemplateMode/tree/3.0-compatibility
There is a good tutorial here:
http://pvcresin.hatenablog.com/entry/2016/03/17/210135
Why is the most adequate solution? : I think this is the best way to achieve new static methods in processing code and ensure an easy distribution! You just have to set the mode folder in your sketchbook/modes folder. If I were to fork processing it would be a big deal to prepare distributions for all operative systems and also to keep update with main project.
My particular solution:
To add my static imports into Processing I implemented a custom mode where I overrode the PdePreprocessor class which wraps the processing code with all the Java procesing code. So, the idea was to add more imports to the imports that the PdePreprocessor generates on the generated Java source.
In my custom PdePreprocessor I overrode the getCoreImports method to add my custom methods. I did this here because I consider the new imports are part of the core of my custom mode. You could also achieve this by overriding writeImports method.
In order to use my PdePreprocessor implementation I had to overrode the following classes:
Commander
JavaBuild
JavaEditor
JavaMode
JavaEditor
I had to implement a new JavaBuild which preprocesses the Sketch with my custom PdePreprocessor. And also use my custom JavaBuild in all the places where the Processing Java Mode instances the build class. Please share with us if there is a better way to do what I did.
Here is the github for my solution: http://github.com/arypbatista/processing-inpr/

Web API XML Documentation from Linq-to-Sql Models (DB First)

I am using web API to provide some web services.
There is a linq to sql datacontext which is auto-generated from database and I use it in my API methods.
Now for documenting, I need to add XML comments to the classes and the properties and methods of them, from the auto-genereated datacontext.designer.cs
When I open up the .designer.cs file and force add my XML comments to it, it works and showed up in the documentation section of the website. But as you know, it is auto generated file, and when I change the model, all my comments will disappear.
what can I do to solve that?
Thanks in advance.
Create a partial class (in a different file) for the class defined in the designer.cs file and move the property you want to change the XML comments from the designer.cs file to your newly created class. Then add the appropriate comments and they will stay there, no matter how many times the auto generated one is recreated.

How to get the current Skin's file path in Mediawiki?

In MediaWiki skin definitions, the BaseTemplate that gets extended has several attributes for creating links to other pages in the wiki, but I've got a situation where I need the path to the skin's directory, to pull some images used to create the UI. By default, that would just be /skins/mySkin/images/foo.png, by default but if someone changes the $wgStylePath variable, or renames the skin, that would be an issue. Is there an existing variable that has that URL build out, hidden somewhere in the BaseTemplate methods?
Yes, the SkinTemplate class, which contains the code to set up the template variables before executing the template, provides access to $wgStylePath via the 'stylepath' template variable.
When you subclass SkinTemplate to define your skin's main class, you are also expected to override the $stylename member variable, which specifies the subdirectory under which your skin's own stylesheets and images reside. (This would usually be the same as the name of your skin in lower case, but it doesn't have to be; it's perfectly fine to have, say, two related skins using images from the same directory.) This is also made available as a template variable, surprisingly named 'stylename'. So one way to specify an image path in your template would be something like:
<?php $this->text('stylepath') ?>/<?php $this->text('stylename') ?>/images/foo.png
Another way, (formerly) used e.g. by the Vector skin, is to use the getSkinStylePath() method from the Skin class (which is the superclass of SkinTemplate; it's kind of messy and tangled for historical reasons, but basically you can pretty much treat them as one class split into two files).
Update: As of MediaWiki 1.36, getSkinStylePath() has been deprecated. The recommended alternative, according to the release notes, is to "replace usages with the direct path to the resources."
To use it, you pass in the name of the file as a parameter, and the method automatically prepends $wgStylePath and $stylename to it (and appends $wgStyleVersion as a query string). Note that this is not a template method, so you have to escape and print the returned URL yourself:
<?php echo htmlspecialchars( $this->getSkin()->getSkinStylePath( 'images/foo.png' ) ) ?>
There's also a getCommonStylePath() method which does exactly the same thing, except that it uses the string "common" instead of $stylename.
Apparently this is the new way:
$this->getSkin()->getConfig()->get( 'StylePath' ) . '/SkinName/images/foo.png';
Source: https://phabricator.wikimedia.org/T270754

Can the AS3 API reference be accessed trough code

I am making a code completion system for a code editor, and i would like to show a tooltip for every parameter containing its reference data. I would like to emulate the code completion from Eclipse, so i was wondering if the API reference is available in code, or if i have to manually import it using the file system or something like that.
You could reflect the current class in question with flash.utils.describeType
This will return a xml with informations about methods, properties and so on.
This can be very heavy. If you use it all the time, try to use a caching system. The Flex framework has a class for it mx.utils.DescribeTypeCache

reflect swf/swc for class that implements interface in actionscript

Not sure if this is possible but I would like to reflect a swf or swc file selected by the user at runtime to find any classes that implement a certain interface. Can this be done or do you actually need a reference to the class you want to reflect using describeType();
Note - this would be done in actionscript.
Thanks,
You can inspect a swf for the classnames (Source) and then use describeType on them to find the ones which implement the interface(s). But this is probably slow. You can try to extend the getDefinitionNames code to get around the describeType and extract the needed info from the bytes...