Beispiel #1
0
        private void processHandshakeData(JsonNode msg)
        {
            //Handshake error
            if (!msg.ContainsKey("code") || !msg.ContainsKey("sys") || msg["code"].AsInt != 200)
            {
                UnityEngine.Debug.LogError("Handshake error! Please check your handshake config.");
            }

            //Set compress data
            JsonNode sys = msg["sys"];

            JsonNode dict = new JsonClass();

            if (sys.ContainsKey("dict"))
            {
                dict = sys["dict"];
            }

            JsonNode protos       = new JsonClass();
            JsonNode serverProtos = new JsonClass();
            JsonNode clientProtos = new JsonClass();

            if (sys.ContainsKey("protos"))
            {
                protos       = sys["protos"];
                serverProtos = protos["server"];
                clientProtos = protos["client"];
            }

            messageProtocol = new MessageProtocol(dict, serverProtos, clientProtos);

            //Init heartbeat service
            int interval = 0;

            if (sys.ContainsKey("heartbeat"))
            {
                interval = sys["heartbeat"].AsInt;
            }
            heartBeatService = new HeartBeatService(interval, this);

            if (interval > 0)
            {
                heartBeatService.start();
            }

            //send ack and change protocol state
            handshake.ack();
            this.state = ProtocolState.working;

            //Invoke handshake callback
            JsonNode user = new JsonClass();

            if (msg.ContainsKey("user"))
            {
                user = msg["user"];
            }
            handshake.invokeCallback(user);
        }
Beispiel #2
0
 public static float[] Migrate(JsonNode parent, string key)
 {
     if (!parent.ContainsKey(key))
     {
         return(new float[] { 0, 0, 0 });
     }
     return(Migrate(parent[key]));
 }
Beispiel #3
0
    private void ApplySettings()
    {
        // set background image(s)
        if (settings.ContainsKey("background") && settings["background"].ContainsKey("images"))
        {
            float rotateBackgroundMinutes = settings["background"]["rotate-minutes"];
            backgroundWaitTime = rotateBackgroundMinutes * 60f;
            Debug.Log("Rotating background pictures every " + backgroundWaitTime + " seconds.");

            backgroundSprites = new List <Sprite>();
            JsonArray backgroundSettings = settings["background"]["images"] as JsonArray;
            for (int i = 0; i < backgroundSettings.Count; i++)
            {
                backgroundSprites.Add(ExhibitUtilities.LoadSpriteFromFile(backgroundSettings[i]));
            }

            backgroundSprites.ShuffleList();
            backgroundImage.sprite = backgroundSprites[0];
            backgroundImage.color  = Color.white;

            StartCoroutine(NextBackgroundImage());
        }
        else
        {
            backgroundImage.color = defaultBackgroundColor;
        }

        // set highlight color
        Color highlightColor = defaultHightlightColor;

        ColorUtility.TryParseHtmlString(settings["highlight-color"], out highlightColor);
        highlight.color  = highlightColor;
        exitButton.color = highlightColor;


        // set app icons
        JsonArray appSettings = settings["apps"] as JsonArray;

        for (int i = 0; i < appSettings.Count; i++)
        {
            GameObject iconObject = Instantiate(iconPrefab);
            iconObject.transform.SetParent(this.transform);

            Icon iconSettings = iconObject.GetComponent <Icon>();
            iconSettings.ChangeIcon(appSettings[i]["icon-image"]);
            iconSettings.SetLaunchPath(appSettings[i]["launch-path"]);
            iconSettings.highlight  = highlight.GetComponent <RectTransform>();
            iconSettings.exitButton = exitButton;

            if (i == 0)
            {
                iconObject.GetComponent <Button>().Select();
            }
        }
    }
Beispiel #4
0
    public Page(JsonNode pageConfig)
    {
        pageName = pageConfig["page-name"];
        pageText = pageConfig["page-text"];

        options = new Dictionary <string, string>();

        if (pageConfig.ContainsKey("options"))
        {
            JsonArray optionsConfig = pageConfig["options"] as JsonArray;
            foreach (JsonNode optionConfig in optionsConfig)
            {
                string optionText         = optionConfig["text"];
                string optionGoToPageName = optionConfig["go-to-page"];
                options.Add(optionText, optionGoToPageName);
            }
        }
    }
Beispiel #5
0
            public SpringBoneGroupMigrator(UniGLTF.glTF gltf, JsonNode vrm0BoneGroup)
            {
                _gltf = gltf;

                _comment        = vrm0BoneGroup.GetObjectValueOrDefault("comment", "");
                _dragForce      = vrm0BoneGroup["dragForce"].GetSingle();
                _gravityDir     = MigrateVector3.Migrate(vrm0BoneGroup["gravityDir"]);
                _gravityPower   = vrm0BoneGroup["gravityPower"].GetSingle();
                _hitRadius      = vrm0BoneGroup["hitRadius"].GetSingle();
                _stiffness      = vrm0BoneGroup["stiffiness"].GetSingle();
                _colliderGroups = vrm0BoneGroup["colliderGroups"].ArrayItems().Select(z => z.GetInt32()).ToArray();
                if (vrm0BoneGroup.ContainsKey("bones"))
                {
                    foreach (var vrm0Bone in vrm0BoneGroup["bones"].ArrayItems())
                    {
                        MigrateRootBone(vrm0Bone.GetInt32());
                    }
                }
            }
Beispiel #6
0
        /// <summary>
        /// Check the message.
        /// </summary>
        private bool checkMsg(JsonNode msg, JsonNode proto)
        {
            ICollection <string> protoKeys = proto.Keys;

            foreach (string key in protoKeys)
            {
                JsonNode value        = proto[key];
                JsonNode proto_option = null;
                if (value.TryGetValue("option", out proto_option))
                {
                    switch (proto_option.AsString)
                    {
                    case "required":
                        if (!msg.ContainsKey(key))
                        {
                            return(false);
                        }
                        else
                        {
                        }
                        break;

                    case "optional":
                        JsonNode value_type = null;

                        JsonNode messages = proto["__messages"];

                        value_type = value["type"];

                        if (msg.ContainsKey(key))
                        {
                            JsonNode value_proto = null;

                            if (messages.TryGetValue(value_type.AsString, out value_proto) || protos.TryGetValue("message " + value_type.AsString, out value_proto))
                            {
                                checkMsg(msg[key], value_proto);
                            }
                        }
                        break;

                    case "repeated":
                        JsonNode msg_name = null;
                        JsonNode msg_type = null;
                        if (value.TryGetValue("type", out value_type) && msg.TryGetValue(key, out msg_name))
                        {
                            if ((proto["__messages"]).TryGetValue(value_type.AsString, out msg_type) || protos.TryGetValue("message " + value_type.AsString, out msg_type))
                            {
                                JsonArray o = msg_name as JsonArray;
                                for (int i = 0; i < o.Count; i++)
                                {
                                    if (!checkMsg(o[i], msg_type))
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
            return(true);
        }
Beispiel #7
0
        public byte[] encode(string route, uint id, JsonNode msg)
        {
            int routeLength = byteLength(route);

            if (routeLength > MSG_Route_Limit)
            {
                throw new Exception("Route is too long!");
            }

            //Encode head
            //The maximus length of head is 1 byte flag + 4 bytes message id + route string length + 1byte
            byte[] head   = new byte[routeLength + 6];
            int    offset = 1;
            byte   flag   = 0;

            if (id > 0)
            {
                byte[] bytes = Protobuf.Encoder.encodeUInt32(id);

                writeBytes(bytes, offset, head);
                flag   |= ((byte)MessageType.MSG_REQUEST) << 1;
                offset += bytes.Length;
            }
            else
            {
                flag |= ((byte)MessageType.MSG_NOTIFY) << 1;
            }

            //Compress head
            if (dict.ContainsKey(route))
            {
                ushort cmpRoute = dict[route];
                writeShort(offset, cmpRoute, head);
                flag   |= MSG_Route_Mask;
                offset += 2;
            }
            else
            {
                //Write route length
                head[offset++] = (byte)routeLength;

                //Write route
                writeBytes(Encoding.UTF8.GetBytes(route), offset, head);
                offset += routeLength;
            }

            head[0] = flag;

            //Encode body
            byte[] body;
            if (encodeProtos.ContainsKey(route))
            {
                body = protobuf.encode(route, msg);
            }
            else
            {
                body = Encoding.UTF8.GetBytes(msg.ToString());
            }

            //Construct the result
            byte[] result = new byte[offset + body.Length];
            for (int i = 0; i < offset; i++)
            {
                result[i] = head[i];
            }

            for (int i = 0; i < body.Length; i++)
            {
                result[offset + i] = body[i];
            }

            //Add id to route map
            if (id > 0)
            {
                reqMap.Add(id, route);
            }

            return(result);
        }
Beispiel #8
0
        public Message decode(byte[] buffer)
        {
            //Decode head
            //Get flag
            byte flag = buffer[0];
            //Set offset to 1, for the 1st byte will always be the flag
            int offset = 1;

            //Get type from flag;
            MessageType type = (MessageType)((flag >> 1) & MSG_Type_Mask);
            uint        id   = 0;
            string      route;

            if (type == MessageType.MSG_RESPONSE)
            {
                int length;
                id = (uint)Protobuf.Decoder.decodeUInt32(offset, buffer, out length);
                if (id <= 0 || !reqMap.ContainsKey(id))
                {
                    return(null);
                }
                else
                {
                    route = reqMap[id];
                    reqMap.Remove(id);
                }

                offset += length;
            }
            else if (type == MessageType.MSG_PUSH)
            {
                //Get route
                if ((flag & 0x01) == 1)
                {
                    ushort routeId = readShort(offset, buffer);
                    route = abbrs[routeId];

                    offset += 2;
                }
                else
                {
                    byte length = buffer[offset];
                    offset += 1;

                    route   = Encoding.UTF8.GetString(buffer, offset, length);
                    offset += length;
                }
            }
            else
            {
                return(null);
            }

            //Decode body
            byte[] body = new byte[buffer.Length - offset];
            for (int i = 0; i < body.Length; i++)
            {
                body[i] = buffer[i + offset];
            }

            JsonNode msg = null;

            if (decodeProtos.ContainsKey(route))
            {
                msg = protobuf.decode(route, body);
            }
            else
            {
                msg = JsonParser.Parse(Encoding.UTF8.GetString(body));
            }

            //Construct the message
            return(new Message(type, id, route, msg));
        }