public static void Register(JavaScriptContext context) { JavaScriptValue JSBehaviourPrototype; JavaScriptValue JSBehaviourConstructor = Bridge.CreateConstructor( typeof(JavaScript.JSBehaviour), (args) => { args[0].SetProperty("monoBehaviour", args[1]); args[0].SetProperty("_delegates", JavaScriptValue.CreateArray(0)); }, out JSBehaviourPrototype ); JavaScriptValue.GlobalObject.SetProperty("JSBehaviour", JSBehaviourConstructor); JSBehaviourPrototype.SetProperty( "AddDelegate", Bridge.CreateFunction( "AddDelegate", (args) => { if (args[1].ValueType != JavaScriptValueType.Object) { throw new Exception("Invalid object passed to AddDelegate"); } var _delegates = args[0].GetProperty("_delegates"); _delegates.GetProperty("push").CallFunction(_delegates, args[1]); } ) ); }
public async void ProcessFrame(FrameData frame) { if (callbacks.Count == 0 || busy) { return; } else { busy = true; } RecognitionServer.Inst.RecognizePose(frame.bitmap, (x) => { var o = JsonObject.Parse(x); var detectedPoses = o["PoseDetection"].GetArray(); ProjectRuntime.Inst.DispatchRuntimeCode(() => { var poses = JavaScriptValue.CreateArray(0); var pushFunc = poses.GetProperty(JavaScriptPropertyId.FromString("push")); foreach (var item in detectedPoses) { var pose = item.GetObject(); var id = pose["PoseID"].GetNumber(); var score = pose["PoseScore"].GetNumber(); var keypoints = pose["Keypoints"].GetArray(); var newKeypoints = new JsonObject(); foreach (var jtem in keypoints) { var keypoint = jtem.GetObject().First(); var defaultPosition = new JsonArray(); defaultPosition.Add(JsonValue.CreateNumberValue(0)); defaultPosition.Add(JsonValue.CreateNumberValue(0)); defaultPosition.Add(JsonValue.CreateNumberValue(0)); var position = keypoint.Value.GetObject().GetNamedArray("Position", defaultPosition); var pos = GetEstimatedPositionFromPosition(new Vector3((float)position.GetNumberAt(0), (float)position.GetNumberAt(1), (float)position.GetNumberAt(2)), frame.bitmap); //var pos = GetEstimatedPositionFromPosition(new Vector3(1, 1, 1), frame.bitmap); var newKeypoint = new JsonObject(); var newPosition = new JsonArray(); newPosition.Add(JsonValue.CreateNumberValue(pos.X)); newPosition.Add(JsonValue.CreateNumberValue(pos.Y)); newPosition.Add(JsonValue.CreateNumberValue(pos.Z)); newKeypoint.Add("position", newPosition); newKeypoint.Add("score", JsonValue.CreateNumberValue(keypoint.Value.GetObject().GetNamedNumber("Score", 0.5))); //newKeypoint.Add("score", JsonValue.CreateNumberValue(0)); newKeypoints.Add(keypoint.Key, newKeypoint); } var jsPose = JavaScriptContext.RunScript($"new Pose({id}, {score}, {newKeypoints.Stringify()});"); pushFunc.CallFunction(poses, jsPose); } foreach (var callback in callbacks) { callback.CallFunction(callback, poses); } }); busy = false; }); }
private JavaScriptValue JSGetPushClients(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) { List <string> clientIds = new List <string>(this.WebSocketClients.GetAll().Keys); JavaScriptValue outValue = JavaScriptValue.CreateArray((uint)clientIds.Count); for (int i = 0; i < clientIds.Count; i++) { outValue.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(clientIds[i])); } return(outValue); }
private JavaScriptValue VisitArray(JArray token) { var n = token.Count; var array = JavaScriptValue.CreateArray((uint)n); for (var i = 0; i < n; ++i) { var element = Visit(token[i]); array.SetIndexedProperty(JavaScriptValue.FromInt32(i), element); } return(array); }
/// <summary> /// Converts a host object to a javascript value. /// </summary> public JavaScriptValue ToJsObject(object obj, Type type) { if (null == obj) { return(JavaScriptValue.Null); } if (JsConversions.IsVoidType(type)) { return(ToJsVoid(obj, type)); } if (JsConversions.IsNumberType(type)) { return(ToJsNumber(obj, type)); } if (JsConversions.IsStringType(type)) { return(ToJsString(obj, type)); } if (JsConversions.IsBoolType(type)) { return(ToJsBoolean(obj, type)); } if (JsConversions.IsFunctionType(type)) { return(ToJsFunction(obj, type)); } if (type.IsArray) { var underlyingType = type.GetElementType(); var hostArray = (Array)obj; var newArr = JavaScriptValue.CreateArray((uint)hostArray.Length); for (int i = 0; i < hostArray.Length; ++i) { var jsValue = ToJsObject(hostArray.GetValue(i), underlyingType); newArr.SetIndexedProperty(JavaScriptValue.FromInt32(i), jsValue); } return(newArr); } // Attempt to bind the object and return it return(ToBoundJsObject(obj, type)); }
public async void ProcessFrame(FrameData frame) { if (callbacks.Count == 0 || busy) { return; } else { busy = true; } var bitmap = frame.bitmap; if (!FaceDetector.IsBitmapPixelFormatSupported(bitmap.BitmapPixelFormat)) { bitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Gray8); } var detectedFaces = await faceDetector.DetectFacesAsync(bitmap); int frameKey = -1; if (detectedFaces.Count > 0) { frameKey = SceneCameraManager.Inst.AddFrameToCache(frame); } ProjectRuntime.Inst.DispatchRuntimeCode(() => { var jsImg = JavaScriptValue.CreateObject(); jsImg.SetProperty(JavaScriptPropertyId.FromString("id"), JavaScriptValue.FromInt32(frameKey), true); Native.JsSetObjectBeforeCollectCallback(jsImg, IntPtr.Zero, jsObjectCallback); var faces = JavaScriptValue.CreateArray(0); var pushFunc = faces.GetProperty(JavaScriptPropertyId.FromString("push")); foreach (var face in detectedFaces) { var pos = GetEstimatedPositionFromFaceBounds(face.FaceBox, frame.bitmap); var jsFace = JavaScriptContext.RunScript($"new Face(new Position({pos.X}, {pos.Y}, {pos.Z}), {0});"); jsFace.SetProperty(JavaScriptPropertyId.FromString("frame"), jsImg, true); jsFace.SetProperty(JavaScriptPropertyId.FromString("bounds"), face.FaceBox.ToJavaScriptValue(), true); pushFunc.CallFunction(faces, jsFace); } foreach (var callback in callbacks) { callback.CallFunction(callback, faces); } }); busy = false; }
private JavaScriptValue VisitArray(JArray token) { var n = token.Count; var array = AddRef(JavaScriptValue.CreateArray((uint)n)); for (var i = 0; i < n; ++i) { var value = Visit(token[i]); array.SetIndexedProperty(JavaScriptValue.FromInt32(i), value); value.Release(); } return(array); }
private JavaScriptValue VisitArray(JArray token) { var n = token.Count; var values = new JavaScriptValue[n]; for (var i = 0; i < n; ++i) { values[i] = Visit(token[i]); } var array = JavaScriptValue.CreateArray((uint)n); for (var i = 0; i < n; ++i) { array.SetIndexedProperty(JavaScriptValue.FromInt32(i), values[i]); } return(array); }
public JavaScriptValue GetEmotionForFaces(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) { if (argumentCount != 3) { return(JavaScriptValue.Invalid); } if (emotionRecognizer == null) { emotionRecognizer = new EmotionRecognizer(); } var callback = arguments[2]; if (callback.IsUndefined()) { return(JavaScriptValue.Invalid); } var faces = arguments[1]; var faceCount = faces.Length(); if (!faceCount.HasValue) { return(JavaScriptValue.Invalid); } else if (faceCount.Value == 0) { callback.CallFunction(callback, JavaScriptValue.CreateArray(0)); return(JavaScriptValue.Invalid); } emotionRecognizer.GetEmotionForFaces(faces, callback); return(JavaScriptValue.Invalid); }
private static JavaScriptContext CreateHostContext(JavaScriptRuntime runtime, string[] arguments, int argumentsStart) { // // Create the context. Note that if we had wanted to start debugging from the very // beginning, we would have called JsStartDebugging right after context is created. // JavaScriptContext context = runtime.CreateContext(); // // Now set the execution context as being the current one on this thread. // using (new JavaScriptContext.Scope(context)) { // // Create the host object the script will use. // JavaScriptValue hostObject = JavaScriptValue.CreateObject(); // // Get the global object // JavaScriptValue globalObject = JavaScriptValue.GlobalObject; // // Get the name of the property ("host") that we're going to set on the global object. // JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host"); // // Set the property. // globalObject.SetProperty(hostPropertyId, hostObject, true); // // Now create the host callbacks that we're going to expose to the script. // DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero); // // Create an array for arguments. // JavaScriptValue hostArguments = JavaScriptValue.CreateArray((uint)(arguments.Length - argumentsStart)); for (int index = argumentsStart; index < arguments.Length; index++) { // // Create the argument value. // JavaScriptValue argument = JavaScriptValue.FromString(arguments[index]); // // Create the index. // JavaScriptValue indexValue = JavaScriptValue.FromInt32(index - argumentsStart); // // Set the value. // hostArguments.SetIndexedProperty(indexValue, argument); } // // Get the name of the property that we're going to set on the host object. // JavaScriptPropertyId argumentsPropertyId = JavaScriptPropertyId.FromString("arguments"); // // Set the arguments property. // hostObject.SetProperty(argumentsPropertyId, hostArguments, true); } return(context); }
public JavaScriptValue ToJavaScriptValue(object value) { // Get the token var token = JToken.FromObject(value); if (token == null) { return(JavaScriptValue.Null); } // Loop through supported tokens switch (token.Type) { case JTokenType.Array: var arrayToken = (JArray)token; var arrayCount = arrayToken.Count; var array = AddRef(JavaScriptValue.CreateArray((uint)arrayCount)); for (var i = 0; i < arrayCount; ++i) { var jsValue = ToJavaScriptValue(arrayToken[i]); array.SetIndexedProperty(JavaScriptValue.FromInt32(i), jsValue); jsValue.Release(); } return(array); case JTokenType.Boolean: return(token.Value <bool>() ? JavaScriptValue.True : JavaScriptValue.False); case JTokenType.Float: return(AddRef(JavaScriptValue.FromDouble(token.Value <double>()))); case JTokenType.Integer: return(AddRef(JavaScriptValue.FromDouble(token.Value <double>()))); case JTokenType.Null: return(JavaScriptValue.Null); case JTokenType.Object: { var objectToken = (JObject)token; var jsObject = AddRef(JavaScriptValue.CreateObject()); // Loop through all properties foreach (var entry in objectToken) { var jsValue = ToJavaScriptValue(entry.Value); var propertyId = JavaScriptPropertyId.FromString(entry.Key); jsObject.SetProperty(propertyId, jsValue, true); jsValue.Release(); } // Get the properties and methods var methods = value.GetType().GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) .Where(m => !m.IsSpecialName && m.IsPrivate == false).ToList(); // Loop through all methods foreach (var method in methods) { SetMethod(jsObject, method, value); } return(jsObject); } case JTokenType.String: return(AddRef(JavaScriptValue.FromString(token.Value <string>()))); case JTokenType.Undefined: return(JavaScriptValue.Undefined); default: Console.WriteLine($"Type {token.Type} is not supported!"); return(JavaScriptValue.Undefined); } }
public static void Register(JavaScriptContext context) { JavaScriptValue GameObjectPrototype; JavaScriptValue GameObjectConstructor = Bridge.CreateConstructor( typeof(UnityEngine.GameObject), (args) => { GameObject obj; if (args.Length > 1 && args[1].ValueType == JavaScriptValueType.String) { obj = new GameObject(args[1].ToString()); } else { obj = new GameObject(); } return(Bridge.CreateExternalWithPrototype(obj)); }, out GameObjectPrototype ); JavaScriptValue .GlobalObject .GetProperty("UnityEngine") .SetProperty("GameObject", GameObjectConstructor); // Static Fields // Static Property Accessors // Static Methods GameObjectConstructor.SetProperty( "CreatePrimitive", Bridge.CreateFunction( "CreatePrimitive", (args) => { PrimitiveType type; switch (args[1].ToString().ToLower()) // TODO lowercase? can these collide? { case "capsule": type = PrimitiveType.Capsule; break; case "cube": type = PrimitiveType.Cube; break; case "cylinder": type = PrimitiveType.Cylinder; break; case "plane": type = PrimitiveType.Plane; break; case "quad": type = PrimitiveType.Quad; break; case "sphere": type = PrimitiveType.Sphere; break; default: throw new System.Exception("invalid type"); } return(Bridge.CreateExternalWithPrototype(GameObject.CreatePrimitive(type), GameObjectPrototype)); } ) ); GameObjectConstructor.SetProperty( "FindWithTag", Bridge.CreateFunction( "FindWithTag", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.GameObject.FindWithTag(args[1].ToString())) ) ); GameObjectConstructor.SetProperty( "FindGameObjectWithTag", Bridge.CreateFunction( "FindGameObjectWithTag", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.GameObject.FindGameObjectWithTag(args[1].ToString())) ) ); GameObjectConstructor.SetProperty( "FindGameObjectsWithTag", Bridge.CreateFunction( "FindGameObjectsWithTag", (args) => { // TODO make an array creator helper GameObject[] foundObjects = UnityEngine.GameObject.FindGameObjectsWithTag(args[1].ToString()); JavaScriptValue jsArray = JavaScriptValue.CreateArray(0); for (int i = 0; i < foundObjects.Length; i++) { jsArray.SetIndexedProperty(i, Bridge.CreateExternalWithPrototype(foundObjects[i])); } return(jsArray); } ) ); GameObjectConstructor.SetProperty( "Find", Bridge.CreateFunction( "Find", (args) => Bridge.CreateExternalWithPrototype(UnityEngine.GameObject.Find(args[1].ToString())) ) ); // Instance Fields // Instance Property Accessors Bridge.DefineGetter( GameObjectPrototype, "transform", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => Bridge.CreateExternalWithPrototype(o.transform)) ); Bridge.DefineGetterSetter( GameObjectPrototype, "layer", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromInt32(o.layer)), Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { o.layer = args[1].ToInt32(); }) ); Bridge.DefineGetter( GameObjectPrototype, "activeSelf", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.activeSelf)) ); Bridge.DefineGetter( GameObjectPrototype, "activeInHierarchy", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.activeInHierarchy)) ); Bridge.DefineGetterSetter( GameObjectPrototype, "isStatic", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.isStatic)), Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { o.isStatic = args[1].ToBoolean(); }) ); Bridge.DefineGetterSetter( GameObjectPrototype, "tag", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromString(o.tag)), Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { o.tag = args[1].ToString(); }) ); Bridge.DefineGetter( GameObjectPrototype, "scene", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => Bridge.CreateExternalWithPrototype(o.scene)) ); Bridge.DefineGetter( GameObjectPrototype, "gameObject", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => Bridge.CreateExternalWithPrototype(o.gameObject)) ); // Instance Methods GameObjectPrototype.SetProperty("GetComponent", Bridge.CreateFunction(Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName; if (args[1].ValueType == JavaScriptValueType.Function) { typeName = args[1].GetProperty("name").ToString(); } else if (args[1].ValueType == JavaScriptValueType.String) { typeName = args[1].ToString(); } else { throw new System.Exception("Argument passed to GetComponent must be a class or string"); } System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); return(Bridge.CreateExternalWithPrototype( o.GetComponent(type), prototype )); }))); GameObjectPrototype.SetProperty( "GetComponentInChildren", Bridge.CreateFunction( "GetComponentInChildren", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); if (args.Length == 2) { return(Bridge.CreateExternalWithPrototype( o.GetComponentInChildren(type), prototype )); } else { return(Bridge.CreateExternalWithPrototype( o.GetComponentInChildren(type, args[2].ToBoolean()), prototype )); } }) ) ); GameObjectPrototype.SetProperty( "GetComponentInParent", Bridge.CreateFunction( "GetComponentInParent", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); return(Bridge.CreateExternalWithPrototype( o.GetComponentInParent(type), prototype )); }) ) ); GameObjectPrototype.SetProperty( "GetComponents", Bridge.CreateFunction( "GetComponents", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); JavaScriptValue array = JavaScriptValue.CreateArray(0); foreach (var component in o.GetComponents(type)) { array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype( component, prototype )); } return(array); }) ) ); GameObjectPrototype.SetProperty( "GetComponentsInChildren", Bridge.CreateFunction( "GetComponentsInChildren", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); bool includeInactive = args.Length == 3 ? args[2].ToBoolean() : false; JavaScriptValue array = JavaScriptValue.CreateArray(0); foreach (var component in o.GetComponentsInChildren(type, includeInactive)) { array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype( component, prototype )); } return(array); }) ) ); GameObjectPrototype.SetProperty( "GetComponentsInParent", Bridge.CreateFunction( "GetComponentsInParent", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); bool includeInactive = args.Length == 3 ? args[2].ToBoolean() : false; JavaScriptValue array = JavaScriptValue.CreateArray(0); foreach (var component in o.GetComponentsInParent(type, includeInactive)) { array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype( component, prototype )); } return(array); }) ) ); GameObjectPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2]))) ) ); GameObjectPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2]))) ) ); GameObjectPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2]))) ) ); GameObjectPrototype.SetProperty( "AddComponent", Bridge.CreateFunction( "AddComponent", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); return(Bridge.CreateExternalWithPrototype(o.AddComponent(type), prototype)); }) ) ); /* * GameObject AddComponent * method has generics */ GameObjectPrototype.SetProperty( "SetActive", Bridge.CreateFunction( "SetActive", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SetActive(args[1].ToBoolean())) ) ); GameObjectPrototype.SetProperty( "CompareTag", Bridge.CreateFunction( "CompareTag", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => JavaScriptValue.FromBoolean(o.CompareTag(args[1].ToString()))) ) ); GameObjectPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3]))) ) ); GameObjectPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]))) ) ); GameObjectPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessageUpwards(args[1].ToString())) ) ); GameObjectPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3]))) ) ); GameObjectPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]))) ) ); GameObjectPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.SendMessage(args[1].ToString())) ) ); GameObjectPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3]))) ) ); GameObjectPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]))) ) ); GameObjectPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.GameObject>((o, args) => o.BroadcastMessage(args[1].ToString())) ) ); }
public void GetEmotionForFaces(JavaScriptValue faces, JavaScriptValue callback) { if (RecognitionServer.Inst.IsBusy) { return; } var boundList = new List <BitmapBounds>(); for (int i = 0; i < faces.Length().Value; ++i) { var jsBounds = faces.Get(i).Get("bounds"); var bounds = new BitmapBounds() { X = (uint)jsBounds.Get("x").ToInt32(), Y = (uint)jsBounds.Get("y").ToInt32(), Width = (uint)jsBounds.Get("width").ToInt32(), Height = (uint)jsBounds.Get("height").ToInt32() }; boundList.Add(bounds); } int frameID = faces.Get(0).Get("frame").Get("id").ToInt32(); var frame = SceneCameraManager.Inst.GetFrameFromCache(frameID); callback.AddRef(); faces.AddRef(); server.RecognizeEmotions(frame.bitmap, boundList, (s) => { JsonObject json; if (!JsonObject.TryParse(s, out json)) { ProjectRuntime.Inst.DispatchRuntimeCode(() => { var emotions = JavaScriptValue.CreateArray(0); var pushFunc = emotions.GetProperty(JavaScriptPropertyId.FromString("push")); for (var i = 0; i < faces.Length().Value; ++i) { pushFunc.CallFunction(faces, JavaScriptValue.FromString("Unknown")); } callback.CallFunction(callback, faces); callback.Release(); faces.Release(); }); return; } var responses = json.GetNamedArray("ResponsePerFace"); ProjectRuntime.Inst.DispatchRuntimeCode(() => { var emotions = JavaScriptValue.CreateArray(0); var pushFunc = emotions.GetProperty(JavaScriptPropertyId.FromString("push")); for (int i = 0; i < faces.Length().Value; ++i) { var emotion = responses.GetObjectAt((uint)i).GetNamedString("Emotion"); pushFunc.CallFunction(emotions, JavaScriptValue.FromString(emotion)); } callback.CallFunction(callback, emotions); callback.Release(); faces.Release(); }); }); }
public static void Register(JavaScriptContext context) { JavaScriptValue ComponentPrototype; JavaScriptValue ComponentConstructor = Bridge.CreateConstructor( typeof(UnityEngine.Component), (args) => { throw new System.NotImplementedException(); }, out ComponentPrototype ); JavaScriptValue .GlobalObject .GetProperty("UnityEngine") .SetProperty("Component", ComponentConstructor); // Static Fields // Static Property Accessors // Static Methods // Instance Fields // Instance Property Accessors Bridge.DefineGetter( ComponentPrototype, "transform", Bridge.WithExternal <UnityEngine.Component>((o, args) => Bridge.CreateExternalWithPrototype(o.transform)) ); Bridge.DefineGetter( ComponentPrototype, "gameObject", Bridge.WithExternal <UnityEngine.Component>((o, args) => Bridge.CreateExternalWithPrototype(o.gameObject)) ); Bridge.DefineGetterSetter( ComponentPrototype, "tag", Bridge.WithExternal <UnityEngine.Component>((o, args) => JavaScriptValue.FromString(o.tag)), Bridge.WithExternal <UnityEngine.Component>((o, args) => { o.tag = args[1].ToString(); }) ); // Instance Methods ComponentPrototype.SetProperty("GetComponent", Bridge.CreateFunction(Bridge.WithExternal <UnityEngine.Component>((o, args) => { string typeName; if (args[1].ValueType == JavaScriptValueType.Function) { typeName = args[1].GetProperty("name").ToString(); } else if (args[1].ValueType == JavaScriptValueType.String) { typeName = args[1].ToString(); } else { throw new System.Exception("Argument passed to GetComponent must be a class or string"); } System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); return(Bridge.CreateExternalWithPrototype( o.GetComponent(type), prototype )); }))); ComponentPrototype.SetProperty( "GetComponentInChildren", Bridge.CreateFunction( "GetComponentInChildren", Bridge.WithExternal <UnityEngine.Component>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); if (args.Length == 2) { return(Bridge.CreateExternalWithPrototype( o.GetComponentInChildren(type), prototype )); } else { return(Bridge.CreateExternalWithPrototype( o.GetComponentInChildren(type, args[2].ToBoolean()), prototype )); } }) ) ); ComponentPrototype.SetProperty( "GetComponentInParent", Bridge.CreateFunction( "GetComponentInParent", Bridge.WithExternal <UnityEngine.Component>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); return(Bridge.CreateExternalWithPrototype( o.GetComponentInParent(type), prototype )); }) ) ); ComponentPrototype.SetProperty( "GetComponents", Bridge.CreateFunction( "GetComponents", Bridge.WithExternal <UnityEngine.Component>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); JavaScriptValue array = JavaScriptValue.CreateArray(0); foreach (var component in o.GetComponents(type)) { array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype( component, prototype )); } return(array); }) ) ); ComponentPrototype.SetProperty( "GetComponentsInChildren", Bridge.CreateFunction( "GetComponentsInChildren", Bridge.WithExternal <UnityEngine.Component>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); bool includeInactive = args.Length == 3 ? args[2].ToBoolean() : false; JavaScriptValue array = JavaScriptValue.CreateArray(0); foreach (var component in o.GetComponentsInChildren(type, includeInactive)) { array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype( component, prototype )); } return(array); }) ) ); ComponentPrototype.SetProperty( "GetComponentsInParent", Bridge.CreateFunction( "GetComponentsInParent", Bridge.WithExternal <UnityEngine.Component>((o, args) => { string typeName = args[1].GetProperty("name").ToString(); System.Type type = Bridge.GetType(typeName); JavaScriptValue prototype = Bridge.GetPrototype(typeName); bool includeInactive = args.Length == 3 ? args[2].ToBoolean() : false; JavaScriptValue array = JavaScriptValue.CreateArray(0); foreach (var component in o.GetComponentsInParent(type, includeInactive)) { array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype( component, prototype )); } return(array); }) ) ); ComponentPrototype.SetProperty( "CompareTag", Bridge.CreateFunction( "CompareTag", Bridge.WithExternal <UnityEngine.Component>((o, args) => JavaScriptValue.FromBoolean(o.CompareTag(args[1].ToString()))) ) ); ComponentPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3]))) ) ); ComponentPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]))) ) ); ComponentPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString())) ) ); ComponentPrototype.SetProperty( "SendMessageUpwards", Bridge.CreateFunction( "SendMessageUpwards", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2]))) ) ); ComponentPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]))) ) ); ComponentPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString())) ) ); ComponentPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3]))) ) ); ComponentPrototype.SetProperty( "SendMessage", Bridge.CreateFunction( "SendMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2]))) ) ); ComponentPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3]))) ) ); ComponentPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]))) ) ); ComponentPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString())) ) ); ComponentPrototype.SetProperty( "BroadcastMessage", Bridge.CreateFunction( "BroadcastMessage", Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2]))) ) ); }
private JavaScriptContext CreateHostContext(JavaScriptRuntime runtime, HttpListenerRequest request) { // Create the context. Note that if we had wanted to start debugging from the very // beginning, we would have called JsStartDebugging right after context is created. JavaScriptContext context = runtime.CreateContext(); // Now set the execution context as being the current one on this thread. using (new JavaScriptContext.Scope(context)) { // Create the host object the script will use. JavaScriptValue hostObject = JavaScriptValue.CreateObject(); // Get the global object JavaScriptValue globalObject = JavaScriptValue.GlobalObject; // Get the name of the property ("host") that we're going to set on the global object. JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host"); // Set the property. globalObject.SetProperty(hostPropertyId, hostObject, true); // Now create the host callbacks that we're going to expose to the script. DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "readFile", readFileDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "writeFile", writeFileDelegate, IntPtr.Zero); // Create an object for request. JavaScriptValue requestParams = JavaScriptValue.CreateObject(); if (request.RawUrl != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("rawUrl"), JavaScriptValue.FromString(request.RawUrl), true); } if (request.UserAgent != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("userAgent"), JavaScriptValue.FromString(request.UserAgent), true); } if (request.UserHostName != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("userHostName"), JavaScriptValue.FromString(request.UserHostName), true); } if (request.UserHostAddress != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("userHostAddress"), JavaScriptValue.FromString(request.UserHostAddress), true); } if (request.ServiceName != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("serviceName"), JavaScriptValue.FromString(request.ServiceName), true); } if (request.HttpMethod != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("httpMethod"), JavaScriptValue.FromString(request.HttpMethod), true); } if (request.ContentType != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("contentType"), JavaScriptValue.FromString(request.ContentType), true); } if (request.ContentEncoding != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("contentEncoding"), JavaScriptValue.FromString(request.ContentEncoding.WebName), true); } requestParams.SetProperty(JavaScriptPropertyId.FromString("keepAlive"), JavaScriptValue.FromBoolean(request.KeepAlive), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isWebSocketRequest"), JavaScriptValue.FromBoolean(request.IsWebSocketRequest), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isSecureConnection"), JavaScriptValue.FromBoolean(request.IsSecureConnection), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isLocal"), JavaScriptValue.FromBoolean(request.IsLocal), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("isAuthenticated"), JavaScriptValue.FromBoolean(request.IsAuthenticated), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("hasEntityBody"), JavaScriptValue.FromBoolean(request.HasEntityBody), true); requestParams.SetProperty(JavaScriptPropertyId.FromString("contentLength64"), JavaScriptValue.FromDouble(request.ContentLength64), true); // need to call begingetclientcertificate //requestParams.SetProperty(JavaScriptPropertyId.FromString("clientCertificateError"), JavaScriptValue.FromInt32(request.ClientCertificateError), true); if (request.UrlReferrer != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("urlReferrer"), JavaScriptValue.FromString(request.UrlReferrer.ToString()), true); } if (request.RequestTraceIdentifier != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("requestTraceIdentifier"), JavaScriptValue.FromString(request.RequestTraceIdentifier.ToString()), true); } if (request.RemoteEndPoint != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("remoteEndPoint"), JavaScriptValue.FromString(request.RemoteEndPoint.ToString()), true); } if (request.ProtocolVersion != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("protocolVersion"), JavaScriptValue.FromString(request.ProtocolVersion.ToString()), true); } if (request.LocalEndPoint != null) { requestParams.SetProperty(JavaScriptPropertyId.FromString("localEndPoint"), JavaScriptValue.FromString(request.LocalEndPoint.ToString()), true); } if (request.UserLanguages != null) { JavaScriptValue userLanguages = JavaScriptValue.CreateArray((uint)request.UserLanguages.Length); for (int i = 0; i < request.UserLanguages.Length; i++) { userLanguages.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(request.UserLanguages[i])); } requestParams.SetProperty(JavaScriptPropertyId.FromString("userLanguages"), userLanguages, true); } if (request.AcceptTypes != null) { JavaScriptValue acceptTypes = JavaScriptValue.CreateArray((uint)request.AcceptTypes.Length); for (int i = 0; i < request.AcceptTypes.Length; i++) { acceptTypes.SetIndexedProperty(JavaScriptValue.FromInt32(i), JavaScriptValue.FromString(request.AcceptTypes[i])); } requestParams.SetProperty(JavaScriptPropertyId.FromString("acceptTypes"), acceptTypes, true); } if (request.QueryString != null) { JavaScriptValue queryString = JavaScriptValue.CreateArray((uint)request.QueryString.Count); for (int i = 0; i < request.QueryString.Count; i++) { JavaScriptValue qsItem = JavaScriptValue.CreateObject(); qsItem.SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(request.QueryString.GetKey(i) ?? string.Empty), false); qsItem.SetProperty(JavaScriptPropertyId.FromString("value"), JavaScriptValue.FromString(request.QueryString[i] ?? string.Empty), false); queryString.SetIndexedProperty(JavaScriptValue.FromInt32(i), qsItem); } requestParams.SetProperty(JavaScriptPropertyId.FromString("queryString"), queryString, true); } if (request.Headers != null) { JavaScriptValue headers = JavaScriptValue.CreateArray((uint)request.Headers.Count); for (int i = 0; i < request.Headers.Count; i++) { JavaScriptValue headerItem = JavaScriptValue.CreateObject(); headerItem.SetProperty(JavaScriptPropertyId.FromString("name"), JavaScriptValue.FromString(request.Headers.GetKey(i) ?? string.Empty), false); headerItem.SetProperty(JavaScriptPropertyId.FromString("value"), JavaScriptValue.FromString(request.Headers[i] ?? string.Empty), false); headers.SetIndexedProperty(JavaScriptValue.FromInt32(i), headerItem); } requestParams.SetProperty(JavaScriptPropertyId.FromString("headers"), headers, true); } // #todo // Stream InputStream // CookieCollection Cookies hostObject.SetProperty(JavaScriptPropertyId.FromString("request"), requestParams, true); } return(context); }