Return Array from NPAPI plugin to java script - npapi

I want to return array of string from NPAPI plugin to Javascript. Currently I am using only plain NPAPI. I have read the following links:
NPVariant to string array
http://www.m0interactive.com/archives/2010/10/21/how_to_communicate_back_to_javascript_from_npapi_plugin.html
I am able to return alert() from plugin to javascript and I can get the NPNVWindowObject, but I am stuck right now on figuring out how to push elements onto the array and return it to javascript.
Working code samples would be appreciated, thanks

You're already close; you just need to fill in a few details. The FireBreath codebase has examples of doing this, but the actual implementation is a bit abstracted. I don't have any raw NPAPI code that does this; I build plugins in FireBreath and it's almost ridiculously simple there. I can tell you what you need to do, however.
The problem gets simpler if you break it down into a few steps:
Get the NPObject for the window (sounds like you have this)
Create a new array and get the NPObject for that array
Invoke "push" on that NPObject for each item you want to send to the DOM
Retain the NPObject for the array and return it in the return value NPVariant
I'll take a stab at the code you'd use for these; there might be some minor errors.
1) Get the NPObject for the window
// Get window object.
NPObject* window = NULL;
NPN_GetValue(npp_, NPNVWindowNPObject, &window);
// Remember that when we're done we need to NPN_ReleaseObject the window!
2) Create a new array and get the NPObject for that array
Basically we do this by calling window.Array(), which you do by invoking Array on the window.
// Get the array object
NPObject* array = NULL;
NPVariant arrayVar;
NPN_Invoke(_npp, window, NPN_GetStringIdentifier("Array"), NULL, 0, &arrayVar);
array = arrayVar.value.objectValue;
// Note that we don't release the arrayVar because we'll be holding onto the pointer and returning it later
3) Invoke "push" on that NPObject for each item you want to send to the DOM
NPIdentifier pushId = NPN_GetStringIdentifier("push");
for (std::vector<std::string>::iterator it = stringList.begin(); it != stringList.end(); ++it) {
NPVariant argToPush;
NPVariant res;
STRINGN_TO_NPVARIANT(it->c_str(), it->size(), argToPush);
NPN_Invoke(_npp, array, pushId, &argToPush, 1, &res);
// Discard the result
NPN_ReleaseVariantValue(&res);
}
4) Retain the NPObject for the array and return it in the return value NPVariant
// Actually we don't need to retain the NPObject; we just won't release it. Same thing.
OBJECT_TO_NPVARIANT(array, *retVal);
// We're assuming that the NPVariant* param passed into this function is called retVal
That should pretty much do it. Make sure you understand how memory management works; read http://npapi.com/memory if you haven't.
Good luck

Related

Create delay in ActionScript 3 function

I have a function in Adobe Flex 4 (ActionScript 3) that accepts an object and returns an ArrayCollection...
If a certain global variable is set to true, I want the function to delay itself for 3 seconds before running. Otherwise I want the function to run as normal.
The problem is, if I use a Timer, that timer calls a separate function, and that function cannot return anything to my calling function, nor can the function it calls accept any parameters, so it's not like I can call my own function recursively after the TimerComplete event fires... And a recursive call wouldn't work anyway, because it would return the ArrayCollection to the timer-result function, not to the original calling function...
I need a delay within the function, not a delay that causes me to go outside that function. But I cannot figure out how to do it.
Something like this is what I need to do:
private function createArrayCollection(myObject:Object):ArrayCollection {
var myArrayCollection:ArrayCollection = new ArrayCollection();
if (globalWaitBoolean) {
//delay here for 3 seconds, somehow
}
//Here I do the stuff that uses the info in myObject to figure out what to
//put into the ArrayCollection I want to return
return (myArrayCollection);
}
So... Any ideas on how to accomplish this without calling an external Timer function that cannot return an object back to my original function?
Thanks,
The way you want it you will have your whole application to lag for 3 seconds, unresponsive to any user input and external events. But it is possible, sure:
import flash.utils.getTimer;
private function createArrayCollection(myObject:Object):ArrayCollection
{
var myArrayCollection:ArrayCollection = new ArrayCollection;
if (globalWaitBoolean)
{
var waitUntil:int = getTimer() + 3000;
// Method getTimer() returns time in ms passed since app start.
// So you just have to wait until it is greater than appointed time.
while (getTimer() < waitUntil)
{
// Do nothing.
}
}
return (myArrayCollection);
}
Still, if you want to do it in a correct way of doing it:
import flash.utils.setTimeout;
private function callerMethod():void
{
// Blah blah blah.
// ...
// Finally.
createArrayCollection(sourceData, asyncResult);
}
private function createArrayCollection(myObject:Object, handler:Function):void
{
var result:ArrayCollection = new ArrayCollection;
if (globalWaitBoolean) setTimeout(handler, 3000, result);
else handler(result);
}
private function asyncResult(source:ArrayCollection):void
{
// The rest of your processing code.
}
Normal (synchronous) code flow would not return until the value is prepared, so should you desire to actually wait for 3 seconds while not allowing your app to do anything, use getTimer() approach from #Organis's answer. If you'll go for an asynchronus result, you'll need to face and overcome some more problems.
First, when do you expect your returned ArrayCollection to actually arrive. Speaking of code design, asynchronous code requires a whole lot of assumptions, thread safety etc etc, and even while AS3/Flash does not have true multithreading unless you count Workers, the code flow with events is not as obvious. So, whoever called your createArrayCollection() MUST NOT expect value returned from it right away. So, speaking about your direct question, NO, you can't avoid timers of some sort if you desire a responsive application. But you can use them with an approach that would involve an indirectly returned result.
Second, whether there might be concurring requests for more array collections from objects if your app would require these - you have to prepare for any kind of interference that might be caused by this. Say your function is triggered by a button click - what if that button would get clicked more than once in 3 seconds?
Third, actual route to processing code is not direct with asynchronous return. You need either a callback, an event handler (which is essentially a semi-native callback), a code that periodically checks for value presence (enter frame handler, etc) or a similar trick to gather the value that's returned asynchronously, and then transfer it to any relevant code that would process it further. Therefore, you would need to design an interface capable of receiving complex data (source object forward, array collection backward) and then carefully test it against all the possible cases and flaws.
An example of implementing all that is very long, I'll try to outline it somehow. Ler's assume you have a sort of "server" class that accepts requests for data and processes it synchronously (no wait) or asynchronously (wait). It accepts a source object of type "T" and provides a newly created object of type ArrayCollection, supplied as a parameter to whatever callback function sent to it. Also it accepts a delay (a simple way to show sync/async return would be a boolean, but why not getting an int?) as a parameter, and guarantees (to the extent of event model limitations) that after this delay the callback will be called ASAP. The architecture will then look like this:
class Processor {
Dictionary requests; // here all the requests that are delayed will be stored
public function dpr(source:T,callback:Function,delay:int=0):void{...}
// creates requests and stores them
private function syncProcess(source:T):ArrayCollection {...}
// whatever routine you want to get variably delayed
private function processTimeout(e:Event=null):void {...}
// processes events from "setTimeout()" and calls callbacks
}
Note that asynchronous approach forced to create three more entities than a synchronous one. First is the request holding structure (the dictionary here), second is timeout event handler, third is whatever callback you'll desire to get called when the data is ready. The code flow would go like this:
Synchronous call would result in the callback directly called from within the class: request->processTimeout->syncProcess()->callback. Asynchronous call will have the callback called from within Timer::timerComplete event handler via setTimeout called within request, with data that originally came from request stored in requests.
You could use an embedded/inline function:
private function createArrayCollection(myObject:Object):ArrayCollection {
var myArrayCollection:ArrayCollection = new ArrayCollection();
if (globalWaitBoolean) {
var milliseconds:int = 3000;
//delay here for 3 seconds
setTimeout(function()
{
//Here I do the stuff that uses the info in myObject to figure out what to
//put into the ArrayCollection I want to return
return (myArrayCollection);
},
milliseconds);
}
else
{
//Here I do the stuff that uses the info in myObject to figure out what to
//put into the ArrayCollection I want to return
return (myArrayCollection);
}
}
The inner function will have access to all local vars of the outer function.

Cocos2D-X Layer::init() gets stuck when waiting for Leaderboard Service

I'm trying to integrate App42 Leaderboard Service to my Cocos2D-X Game. The core functionality (Sending Scores to Server and retrieving them, just the way shown on the App42 site...) is working fine.
Now i want to visualize my leaderboard data using a CCTableView.
So I got a Leaderboard class (inherited from CCLayer) and am doing something like this :
bool Leaderboard::init() {
...
// Initialize and send App42 Scoreboard API call
App42API::Initialize(API_KEY, SECRET_KEY);
ScoreBoardService *scoreBoardService = App42API::BuildScoreBoardService();
scoreBoardService->GetTopNRankers(gameName,MAX_SCORES, this,app42callfuncND_selector(Leaderboard::onGetTopNRankings));
// responseArrived is boolean, indicates if onGetTopRankings was called
while(!responseArrived);
CCTableView* tableView = CCTableView::create(this, CCSizeMake(400, 100));
tableView->setDirection(kCCScrollViewDirectionVertical);
tableView->setPosition(winSize.width/3 , winSize.height/2);
tableView->setDelegate(this);
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(tableView,5);
tableView->reloadData();
return true;
}
void HelloWorld::onGetTopNRankings(App42CallBack *sender, void *response){
App42GameResponse *scoreResponse = (App42GameResponse*)response;
if (scoreResponse->isSuccess)
{
// Save User scores to Array
responseScores = scoreResponse->scores;
}
else
{
printf("\nerrordetails:%s",scoreResponse->errorDetails.c_str());
printf("\nerrorMessage:%s",scoreResponse->errorMessage.c_str());
printf("\nappErrorCode:%d",scoreResponse->appErrorCode);
printf("\nhttpErrorCode:%d",scoreResponse->httpErrorCode);
}
// Response Data is saved, or Error occured, go back to init()
responseArrived = true;
}
So as you see, I am waiting for onGetTopNRankings to get called, because the data for my TableView would be empty else. But what happens is, that the I can't get back to init() when onGetTopNRankings returns, it gets stuck.
So anybody got an idea why i can't return to Leaderboard::init() or got any good idea to solve this in any other way, I am open for each suggestion ?
while(!responseArrived);
This blocks the thread (endless loop). You need to fill your table view in the callback method when you have actual data. It will be empty until then. That's something your app's design has to deal with. For instance you could display a "loading" animation in the meantime, with a cancel button on it.
I tested your code and it is working fine in my App42Cocos2dXSample
The only possible reason for the issue you are getting is the owner class name of callback method in your code snippet.
scoreBoardService->GetTopNRankers(gameName,MAX_SCORES,
this,app42callfuncND_selector(Leaderboard::onGetTopNRankings));
In the above statement, onGetTopNRankings belong to the class Leaderboard, but while defining the callback method, it belongs to the class Helloworld:
void HelloWorld::onGetTopNRankings(App42CallBack *sender, void *response){
So, try changing the class name from Helloworld to Leaderboard in the above statement. I hope it will work.

How to clone an object without knowing the exact type in AIR for iOS

I am writing an iOS game in Flash and I need a way to clone polymorphic objects.
I have BaseClass, SubClass1, SubClass2 (and so on...) and I need a clone() method in BaseClass, that will create a copy of the current object, without a conditional such as
var obj:BaseClass;
if(this is SubClass1) {
obj = new SubClass1();
}else if(this is SubClass2) {
obj = new SubClass2();
}else...
I need a way to create an object and create the exact bytes (yes, a shallow copy is enough for my purpose) of the object. I've looked at:
AS3 - Clone an object
As3 Copy object
http://actionscripthowto.com/how-to-clone-objects-in-as3/
But none seem to work. Probably not available in AIR 3.3 for iOS SDK. (they compile, but the code doesn't work in my case)
Is there any other way, or did anybody achieve to clone an object in AIR for iOS?
Thanks,
Can.
Bit-by-bit cloning cannot be done with ActionScript, unless your class only contains primitive values (i.e. a simple data structure). That's what the ByteArray approach you've linked to in this question's answer is used for - but when you're dealing with complex types, especially display objects, you'll soon come to the limits (as, I gather, you have already realized).
So this more or less leaves you with two options:
Create a new object and copy all of its fields and properties.
This is the way to go if you're going to need behavior and field values, and you didn't use any drawing methods (i.e., you can not copy vector graphics this way). Creating a new class instance without knowing its exact type can be done in a generalized way using reflections, getQualifiedClassName() and getDefinitionByName() will help you there, and if you need more than just the name, describeType(). This does have limits, too, though:private fields will not be available (they don't appear in the information provided by describeType()), and in order to not run into performance problems, you will have to use some sort of cacheing. Luckily, as3commons-reflect has already solved this, so implementing the rest of what you need for a fully functional shallow copy mechanism is not too complex.
Create a new instance like this:
var newObject:* = new Type.forInstance( myObject ).clazz();
Then iterate over all accessors, variables and dynamic properties and assign the old instance's values.
I have implemented a method like this myself, for an open source framework I am working on. You can download or fork it at github. There isn't any documentation yet, but its use is as simple as writing:
var myCopy:* = shallowCopy( myObject );
I also have a copy() method there, which creates a true deep copy. This, however, has not been tested with anything but data structures (albeit large ones), so use at your own risk ;)
Create a bitmap copy.
If you do have vector graphics in place, this is often easier than recreating an image: Simply draw the content of the object's graphics to a new Bitmap.
function bitmapCopy( source:Sprite ):Bitmap {
source.cacheAsBitmap = true;
var bitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0xFFFFFF );
bitmapData.draw( source, new Matrix(), null, null, null, true );
return new Bitmap( bitmapData, PixelSnapping.AUTO, true );
}
You need to create an abstract clone method in the base class and implement it for each subclass. In the specific implementations, you would copy all of the properties of the object to the new one.
public class BaseClass {
public function clone():BaseClass
{
// throw an error so you quickly see the places where you forgot to override it
throw new Error("clone() should be overridden in subclasses!");
return null;
}
}
public class Subclass1 extends BaseClass {
public override function clone():BaseClass
{
var copy:Subclass1 = new Subclass1();
copy.prop1 = prop1;
copy.prop2 = prop2;
// .. etc
return copy;
}
}
If you wanted to create a generic default implementation of clone, you could use describeType to access the properties and copy them over:
public function clone():BaseClass
{
var defn:XML = describeType(this);
var clsName:String = defn.#name;
var cls:Class = getDefinitionByName(clsName) as Class;
var inst:* = new cls();
for each(var prop:String in (defn.variable + defn.accessor.(#access == 'readwrite')).#name )
{
inst[prop] = this[prop];
}
return inst;
}
The main issue with this is that the describeType XML can get quite large - especially if you are dealing with objects that extend DisplayObject. That could use a lot of memory and be slow on iOS.

Flex: Cannot get value in array collection

I have an array collection object 'invArrayCol' which holds some data. I also have a datagrid. I have set dataProvider as invArrayCol.I displays the data properly when i use it with data grid. But the same invArrayCol shows null when used anywhere other than datagrid. I wrote this code
protected function titlewindow1_creationCompleteHandler(event:FlexEvent):void
{
Cgt=new CgtSRObject();
var autoobj:CSAutoNumberType=new CSAutoNumberType();
autoobj.addEventListener(ResultEvent.RESULT,getInvNubmer);
autoobj.getInvNo(invoiceType);
trace(robj.salesPerson_Id);
getSalesReturnCgt.token=csInvoicePrint.getCgtData(robj.receive_Id);
getSalesReturnCgt.addEventListener(ResultEvent.RESULT,getInvArrList);
trace(Cgt.sr_no);
datagrid_dataprovider=new ArrayCollection();
datagrid_dataprovider=invArrayCol;
calculateTotal();
}
This 2 lines set data to invArrayCol
getSalesReturnCgt.token=csInvoicePrint.getCgtData(robj.receive_Id);
getSalesReturnCgt.addEventListener(ResultEvent.RESULT,getInvArrList);
But here it gives value of invArrayCol as null.
datagrid_dataprovider=new ArrayCollection();
datagrid_dataprovider=invArrayCol;
Please tell me some way out of this.
The ResultEvent's result may return an ObjectProxy, in case the data is of length 1. Casting via 'as' would lead to a silent failing of the cast. So simply checking the type of the result would let you determine if the result can be used directly or if you have to wrap an ArrayCollection around it.
// This happens asynchronously, should have no effect in the function
getSalesReturnCgt.addEventListener(ResultEvent.RESULT,getInvArrList);
Also, the
// datagrid_dataprovider=new ArrayCollection(); // This line is obsoloete
datagrid_dataprovider=invArrayCol; // invArrayCol will get its value later
So, it looks like your expectation is for some code to have it executed synchronously, but it is always working asynchronously.

Trying to understand a function

Im trying to understan a function that I found on the web.
Iknow what the function does, It get the information about the webcam in your computer and post it on the textArea,
But the individual line are just a bit confused.
Any help ?
Thanks
private var camera:Camera;
private function list_change(evt:ListEvent):void {
var tList:List = evt.currentTarget as List;
var cameraName:String = tList.selectedIndex.toString();
camera = Camera.getCamera(cameraName);
textArea.text = ObjectUtil.toString(camera);
}
private var camera:Camera;
This line creates a variable of the class type Camera. It does not create an instance of the variable.
private function list_change(evt:ListEvent):void {
This line is a standard function heading. Because the argument is a ListEvent, it makes me think that this function is probably written as an event handler. Because of the name of the function, it is most like listening to the change event on a list.
var tList:List = evt.currentTarget as List;
This line creates a reference to the list that dispatched the event, which caused this handler to be executed.
var cameraName:String = tList.selectedIndex.toString();
This line converts the selectedIndex to a string. It's a bit odd to convert an index to a string, as opposed to some value. But the reason they do that looks to be on the next line..
camera = Camera.getCamera(cameraName);
This uses that camera variable (defined back in line 1) and actually gets an instance of the camera. It uses the "cameraName" which makes me think that the list that dispatched this change event contains a list of cameras available on the system.
textArea.text = ObjectUtil.toString(camera);
This converts the camera object to a string and displays it in a text area. Normally you wouldn't try to do this as it provides no valuable data. A default object will display strings as [Object object] or something similar. Perhaps the camera object has a custom string function; I don't have experience with that. Normally, you'd want to access properties of the object to get useful information, not try this on the object itself.
}
This line is the end of the function. The open bracket was in the 2nd line of code in the function definition.