From 7c17069a16e86648eba679647334ec426d8b2802 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 28 Jul 2006 02:48:52 +0000 Subject: [PATCH] Some fixups and enhancements. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4294 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/io/TypedArray.as | 24 ++++++++++++++++++- .../com/threerings/presents/client/Client.as | 17 +++++++++---- src/as/com/threerings/util/ArrayUtil.as | 18 +++++++++++++- src/as/com/threerings/util/StringUtil.as | 18 +++++++++++++- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/as/com/threerings/io/TypedArray.as b/src/as/com/threerings/io/TypedArray.as index 3f5721ca3..9aa5c9384 100644 --- a/src/as/com/threerings/io/TypedArray.as +++ b/src/as/com/threerings/io/TypedArray.as @@ -1,8 +1,10 @@ package com.threerings.io { import com.threerings.util.ClassUtil; +import com.threerings.util.Cloneable; public dynamic class TypedArray extends Array + implements Cloneable { /** * Create a TypedArray @@ -21,9 +23,18 @@ public dynamic class TypedArray extends Array */ public static function getJavaType (of :Class) :String { + if (of === Boolean) { + return "[Z"; + + } else if (of === int) { // Number will be int if something like 3.0 + return "[I"; + + } else if (of === Number) { + return "[D"; + } + var cname :String = Translations.getToServer( ClassUtil.getClassName(of)); - // TODO: primitive types return "[L" + cname + ";"; } @@ -41,6 +52,17 @@ public dynamic class TypedArray extends Array return _jtype; } + // from Cloneable + public function clone () :Object + { + var clazz :Class = ClassUtil.getClass(this); + var copy :TypedArray = new clazz(_jtype); + for (var ii :int = length - 1; ii >= 0; ii--) { + copy[ii] = this[ii]; + } + return copy; + } + /** The 'type' of this array, which doesn't really mean anything * except gives it a clue as to how to stream to our server. */ protected var _jtype :String; diff --git a/src/as/com/threerings/presents/client/Client.as b/src/as/com/threerings/presents/client/Client.as index 94c69f622..94533e029 100644 --- a/src/as/com/threerings/presents/client/Client.as +++ b/src/as/com/threerings/presents/client/Client.as @@ -7,6 +7,8 @@ import flash.events.TimerEvent; import flash.utils.Timer; +import mx.core.Application; + import com.threerings.util.ObserverList; import com.threerings.presents.data.ClientObject; @@ -26,10 +28,10 @@ public class Client extends EventDispatcher private static const log :Log = Log.getLog(Client); - public function Client (creds :Credentials, stage :Stage) + public function Client (creds :Credentials, app :Application) { _creds = creds; - _stage = stage; + _app = app; } /** @@ -94,12 +96,17 @@ public class Client extends EventDispatcher _ports = ports; } + public function callLater (fn :Function, args :Array = null) :void + { + _app.callLater(fn, args); + } + /** * @return the Stage object we're living in. */ public function getStage () :Stage { - return _stage; + return _app.stage; } public function getHostname () :String @@ -365,8 +372,8 @@ public class Client extends EventDispatcher /** The credentials we used to authenticate with the server. */ protected var _creds :Credentials; - /** The stage we're on, needed for creating the Communicator. */ - protected var _stage :Stage; + /** The app we're in, needed for creating the Communicator. */ + protected var _app :Application; /** The version string reported to the server at auth time. */ protected var _version :String = ""; diff --git a/src/as/com/threerings/util/ArrayUtil.as b/src/as/com/threerings/util/ArrayUtil.as index 096c587ac..f03804ad9 100644 --- a/src/as/com/threerings/util/ArrayUtil.as +++ b/src/as/com/threerings/util/ArrayUtil.as @@ -1,7 +1,11 @@ package com.threerings.util { /** - * Contains methods that should be in Array, but aren't. + * Contains methods that should be in Array, but aren't. Additionally + * contains methods that understand the interfaces in this package. + * So, for example, removeFirst() understands Equalable and will remove + * an element that is equals() to the specified element, rather than just + * ===. */ public class ArrayUtil { @@ -24,6 +28,18 @@ public class ArrayUtil }); } + public static function indexOf (arr :Array, element :Object) :int + { + if (arr != null) { + for (var ii :int = 0; ii < arr.length; ii++) { + if (Util.equals(arr[ii], element)) { + return ii; + } + } + } + return -1; // never found + } + /** * Remove the first instance of the specified element from the array. */ diff --git a/src/as/com/threerings/util/StringUtil.as b/src/as/com/threerings/util/StringUtil.as index 42d8dd5c2..73603c523 100644 --- a/src/as/com/threerings/util/StringUtil.as +++ b/src/as/com/threerings/util/StringUtil.as @@ -4,6 +4,22 @@ import mx.utils.*; public class StringUtil { + /** + * Get a reasonable hash code for the specified String. + */ + public static function hashCode (str :String) :int + { + var code :int = 0; + if (str != null) { + // sample at most 8 chars + var lastChar :int = Math.min(8, str.length); + for (var ii :int = 0; ii < lastChar; ii++) { + code = code * 31 + str.charCodeAt(ii); + } + } + return code; + } + public static function isBlank (str :String) :Boolean { return (str == null) || (str.search("\\S") == -1); @@ -15,7 +31,7 @@ public class StringUtil public static function endsWith (str :String, substr :String) :Boolean { var startDex :int = str.length - substr.length; - return (startDex >= 0) && (str.indexOf(substr, startDex) > 0); + return (startDex >= 0) && (str.indexOf(substr, startDex) >= 0); } /**