Moved the serverless game stuff out of msoy and back into the vilya library.
We'll be using this in game gardens, at least. Note that the actionscript side currently doesn't compile because of limitations in building a .swc file, but that'll be fixed soon. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@47 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package com.threerings.ezgame.util {
|
||||
|
||||
import flash.net.ObjectEncoding;
|
||||
|
||||
import flash.system.ApplicationDomain;
|
||||
|
||||
import flash.utils.ByteArray;
|
||||
import flash.utils.Endian;
|
||||
|
||||
import com.threerings.io.TypedArray;
|
||||
|
||||
/**
|
||||
* Utility methods for transferring flash properties via
|
||||
* the presents dobj system.
|
||||
*/
|
||||
public class EZObjectMarshaller
|
||||
{
|
||||
/**
|
||||
* Encode the specified object as either a byte[] or a byte[][] if it
|
||||
* is an array. The specific mechanism of encoding is not important,
|
||||
* as long as decode returns a clone of the original object.
|
||||
*
|
||||
* Currently, cycles in the object graph are preserved on the other end.
|
||||
* TODO: serialize IExternalizable implementations, and take
|
||||
* into account the ApplicationDomain.
|
||||
*/
|
||||
public static function encode (
|
||||
obj :Object, encodeArrayElements :Boolean = true) :Object
|
||||
{
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
if (encodeArrayElements && obj is Array) {
|
||||
var src :Array = (obj as Array);
|
||||
var dest :TypedArray = TypedArray.create(ByteArray);
|
||||
for each (var o :Object in src) {
|
||||
dest.push(encode(o, false));
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
// TODO: Our own encoding, that takes into account
|
||||
// the ApplicationDomain
|
||||
var bytes :ByteArray = new ByteArray();
|
||||
bytes.endian = Endian.BIG_ENDIAN;
|
||||
bytes.objectEncoding = ObjectEncoding.AMF3;
|
||||
bytes.writeObject(obj);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static function decode (encoded :Object) :Object
|
||||
{
|
||||
if (encoded == null) {
|
||||
return null;
|
||||
}
|
||||
if (encoded is TypedArray) {
|
||||
var src :TypedArray = (encoded as TypedArray);
|
||||
var dest :Array = [];
|
||||
for each (var b :ByteArray in src) {
|
||||
dest.push(decode(b));
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
var bytes :ByteArray = (encoded as ByteArray);
|
||||
|
||||
// TODO: Our own decoding, that takes into account
|
||||
// the ApplicationDomain
|
||||
bytes.endian = Endian.BIG_ENDIAN;
|
||||
bytes.objectEncoding = ObjectEncoding.AMF3;
|
||||
return bytes.readObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user