Example #1
0
        public static int TryUnpackAsString(MemoryStream ms, out string cmd, out string dataStr, out string error)
        {
            MessagePackObject obj;
            int ret = 1;

            dataStr = string.Empty;
            error   = string.Empty;
            cmd     = string.Empty;

            try
            {
                if (MessagePack.Unpack(ms, out obj))
                {
                    var dict = obj.AsDictionary();

                    // <= 0都是錯誤
                    if (dict != null)
                    {
                        if (dict.TryGetStringValue("cmd", out cmd))
                        {
                            dataStr = obj.ToString();
                        }
                        else if (dict.TryGetStringValue("err", out error))
                        {
                            ret = 0;
                        }
                    }
                }
            }
            catch
            {
                ret = 0;
            }

            return(ret);
        }
Example #2
0
        async void UpdateShipAvatars()
        {
            try
            {
                var rRequest = WebRequest.CreateHttp("http://heavenlywind.cc/api/ship_avatars");
                rRequest.UserAgent = ProductInfo.UserAgent;

                var rPath      = Path.Combine(ProductInfo.RootDirectory, "Resources", "Avatars", "Ships") + "\\";
                var rDirectory = new DirectoryInfo(rPath);
                if (!rDirectory.Exists)
                {
                    rDirectory.Create();
                }

                var rFiles = Directory.EnumerateFiles(rPath, "*.png").ToArray();
                if (rFiles.Length == 0)
                {
                    rRequest.Method = "GET";
                }
                else
                {
                    rRequest.Method = "POST";

                    var rData = new SortedList <int, int>(rFiles.Length);

                    foreach (var rFile in rFiles)
                    {
                        var rMatch = r_ShipAvatarRegex.Match(Path.GetFileName(rFile));
                        if (!rMatch.Success)
                        {
                            continue;
                        }

                        var rShip = int.Parse(rMatch.Groups[1].Value);

                        int rType;
                        rData.TryGetValue(rShip, out rType);

                        rType += rMatch.Groups[2].Value == "n" ? 1 : 2;

                        rData[rShip] = rType;
                    }

                    using (var rStream = await rRequest.GetRequestStreamAsync())
                    {
                        var rSerializer = new JsonSerializer();
                        var rWriter     = new JsonTextWriter(new StreamWriter(rStream));

                        rSerializer.Serialize(rWriter, rData);
                        await rWriter.FlushAsync();
                    }
                }

                using (var rResponse = await rRequest.GetResponseAsync())
                    using (var rStream = rResponse.GetResponseStream())
                    {
                        var rResult = (IDictionary <object, object>)MessagePack.Unpack(new MessagePackReader(rStream));

                        foreach (var rInfo in rResult)
                        {
                            var rData = rInfo.Value as object[];
                            if (rData == null || rData.Length == 0)
                            {
                                continue;
                            }

                            var rShip = rInfo.Key.ToString();

                            if (rData[0] != null)
                            {
                                File.WriteAllBytes(rPath + rShip + "_n.png", (byte[])rData[0]);
                            }

                            if (rData.Length > 1 && rData[1] != null)
                            {
                                File.WriteAllBytes(rPath + rShip + "_d.png", (byte[])rData[1]);
                            }
                        }
                    }
            }
            catch (Exception e)
            {
                Logger.Write(LoggingLevel.Error, string.Format(StringResources.Instance.Main.Log_CheckForUpdate_Exception, e.Message));
            }
        }