Exemplo n.º 1
0
        public async Task <T> LoadAsync <T>(string key) where T : class
        {
            var configuration = GetConfiguration(key);

            if (configuration == null)
            {
                _logger.LogInformation($"Loading data file locally: {_componentId}:{key}");
                return(await _baseDataStore.LoadAsync <T>(key));
            }

            var url      = GetUrl(key);
            var response = await _client.GetAsync(url);

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception($"Server responded with {(int)response.StatusCode} - {response.StatusCode} for URL: GET {url}");
            }

            var serializedYaml = await response.Content.ReadAsStringAsync();

            return(m_Deserializer.Deserialize <T>(serializedYaml));
        }
Exemplo n.º 2
0
		private IRestResponse<T> Deserialize<T>(IRestRequest request, IRestResponse raw)
		{
			request.OnBeforeDeserialization(raw);
			IDeserializer handler = GetHandler(raw.ContentType);
			handler.RootElement = request.RootElement;
			handler.DateFormat = request.DateFormat;
			handler.Namespace = request.XmlNamespace;
			IRestResponse<T> response = new RestResponse<T>();
			try
			{
			    response = raw.toAsyncResponse<T>();
				response.Data = handler.Deserialize<T>(raw);
				response.Request = request;
			}
			catch (Exception ex)
			{
				response.ResponseStatus = ResponseStatus.Error;
				response.ErrorMessage = ex.Message;
				response.ErrorException = ex;
			}
			return response;
		}
Exemplo n.º 3
0
        public static News DownloadNews()
        {
            string rawData;

            try
            {
                rawData = RemoteObjectManager.Instance.DownloadObjectAsString("news.yaml");
            }
            catch (Exception ex)
            {
                throw new NewsParsingException($"Failed to download update news data: {ex.Message}", ex);
            }

            try
            {
                return(deserializer.Deserialize <News>(rawData));
            }
            catch (Exception ex)
            {
                throw new NewsParsingException($"Failed to parse update news data: {ex.Message}", ex);
            }
        }
Exemplo n.º 4
0
 public ReadYamlFileResult ReadYamlFile(string yamlFile)
 {
     // TODO: Firetrust.MailWasherPro -> copyright sign works with Encoding.Default, but other packages have wrong signs then
     using (StreamReader streamReader = new StreamReader(yamlFile, true))
     {
         try
         {
             ManifestPackage_1_0_0 package = _deserializer.Deserialize <ManifestPackage_1_0_0>(streamReader);
             return(new ReadYamlFileResult()
             {
                 Manifest = package, FilePath = yamlFile
             });
         }
         catch (YamlException ex)
         {
             return(new ReadYamlFileResult()
             {
                 ErrorMessage = GetMessage(ex), FilePath = yamlFile
             });
         }
     }
 }
        // Register and validate a conversation in real-time
        public void RegisterConversation(string name, TextAsset file)
        {
            var tempObject = _deserializer.Deserialize <Conversation>(file.text);

            if (tempObject == null)
            {
                DialogueLogger.LogError($"There was an error deserializing file {file.name}");
                return;
            }

            tempObject.PreValidation();
            if (valdateConversation(tempObject, name))
            {
                if (_conversations.ContainsKey(name))
                {
                    DialogueLogger.LogWarning($"Conversation {name} already registered, overwritting");
                }

                _conversations[name] = tempObject;
                tempObject.FinishedParsing();
            }
        }
Exemplo n.º 6
0
        private void DeserializeMetrics(YamlMappingNode node, MetricDefinitionV1 metricDefinition, IErrorReporter errorReporter)
        {
            if (metricDefinition.ResourceType == null)
            {
                return;
            }

            var resourceTypeNode = node.Children["resourceType"];

            if (metricDefinition.ResourceType == ResourceType.NotSpecified)
            {
                errorReporter.ReportError(resourceTypeNode, "'resourceType' must not be set to 'NotSpecified'.");
                return;
            }

            if (node.Children.TryGetValue(ResourceCollectionsTag, out var resourceCollectionNode))
            {
                metricDefinition.ResourceCollections = _azureResourceCollectionDeserializer.Deserialize((YamlSequenceNode)resourceCollectionNode, errorReporter);
            }

            if (node.Children.TryGetValue(ResourcesTag, out var metricsNode))
            {
                var resourceDeserializer = _azureResourceDeserializerFactory.GetDeserializerFor(metricDefinition.ResourceType.Value);
                if (resourceDeserializer != null)
                {
                    metricDefinition.Resources = resourceDeserializer.Deserialize((YamlSequenceNode)metricsNode, errorReporter);
                }
                else
                {
                    errorReporter.ReportError(resourceTypeNode, $"Could not find a deserializer for resource type '{metricDefinition.ResourceType}'.");
                }
            }

            if ((metricDefinition.Resources == null || !metricDefinition.Resources.Any()) &&
                (metricDefinition.ResourceCollections == null || !metricDefinition.ResourceCollections.Any()))
            {
                errorReporter.ReportError(node, "Either 'resources' or 'resourceCollections' must be specified.");
            }
        }
Exemplo n.º 7
0
        private IRestResponse <T> Deserialize <T>(IRestRequest request, IRestResponse raw)
        {
            request.OnBeforeDeserialization(raw);

            IRestResponse <T> response = new RestResponse <T>();

            try
            {
                response         = raw.ToAsyncResponse <T>();
                response.Request = request;

                // Only attempt to deserialize if the request has not errored due
                // to a transport or framework exception.  HTTP errors should attempt to
                // be deserialized
                if (response.ErrorException == null)
                {
                    IDeserializer handler = this.GetHandler(raw.ContentType);

                    // Only continue if there is a handler defined else there is no way to deserialize the data.
                    // This can happen when a request returns for example a 404 page instead of the requested JSON/XML resource
                    if (handler != null)
                    {
                        handler.RootElement = request.RootElement;
                        handler.DateFormat  = request.DateFormat;
                        handler.Namespace   = request.XmlNamespace;

                        response.Data = handler.Deserialize <T>(raw);
                    }
                }
            }
            catch (Exception ex)
            {
                response.ResponseStatus = ResponseStatus.Error;
                response.ErrorMessage   = ex.Message;
                response.ErrorException = ex;
            }

            return(response);
        }
Exemplo n.º 8
0
    /// <summary>
    /// Deserializes and adds message from stream.
    /// </summary>
    /// <param name="stream">Stream from which to Deserialize</param>
    /// <param name="deserializer">Deserializer which is to be used</param>
    /// <returns>Reference to added message, null if unsuccessful.</returns>
    public Message AddMessage(Stream stream, IDeserializer deserializer)
    {
        var mess = deserializer.Deserialize(stream) as Message;

        if (mess == null)
        {
            return(null);
        }
        if (!messages.ContainsKey(mess.ID) && (messages.ContainsKey(mess.TargetId) || mess.TargetId == Guid.Empty))
        {
            //as in previous method, these conditions had to be checked
            messages.Add(mess.ID, mess);
            mess.Conversation = this;
            OnPropertyChanged(this, new(nameof(ObservableMessages)));
        }
        else
        {
            mess = null;
        }

        return(mess);
    }
Exemplo n.º 9
0
        protected override AggregatableMessage <ICommand> Process(KafkaMessageBatch batch)
        {
            var filtered = batch.Results
                           .Where(x => _appropriateTopics.Contains(x.Topic));

            using (var transaction = new TransactionScope(TransactionScopeOption.Required, _transactionOptions))
            {
                var dtos = _deserializer.Deserialize(filtered).ToList();
                if (dtos.Count != 0)
                {
                    Process(dtos);
                }

                transaction.Complete();
            }

            return(new AggregatableMessage <ICommand>
            {
                TargetFlow = MessageFlow,
                Commands = Array.Empty <ICommand>(),
            });
        }
        private OfferingSaleCancellationResponse ProcessUnsuccessfulRequest(HttpResponseMessage response)
        {
            var body = response.Content.ReadAsStringAsync().Result;

            ErrorResponse errorResponse = null;
            string        message;

            if (string.IsNullOrWhiteSpace(body))
            {
                message = $"HTTP Status: '{(int)response.StatusCode} - {response.StatusCode}' Reason: '{response.ReasonPhrase}'";

                errorResponse = new ErrorResponse
                {
                    Message        = message,
                    HttpStatusCode = response.StatusCode
                };
            }
            else
            {
                errorResponse = _deserializer.Deserialize <ErrorResponse>(body);
                errorResponse.HttpStatusCode = response.StatusCode;

                message = errorResponse.ValidationMessages.Count > 0 ?
                          $"Invalid Request : {string.Join(" - ", errorResponse.ValidationMessages)}" :
                          $"Unable to cancel offering sale : {errorResponse.Message}";
            }

            if (_configuration.ThrowExceptions)
            {
                // use WebException to maintain backwards compatibility with old versions which used HttpWebRequest
                throw new WebException(message, null, WebExceptionStatus.ProtocolError, new SimpleWebResponse(response.StatusCode, response.Content));
            }

            return(new OfferingSaleCancellationResponse
            {
                Success = false,
                ErrorResponse = errorResponse
            });
        }
Exemplo n.º 11
0
        public void GetTradesExpected()
        {
            IDeserializer deserializer = null;

            var restMock = new Mock <IRestClient>(MockBehavior.Strict);

            restMock.SetupAllProperties();
            restMock.Setup(x => x.AddHandler("application/json", It.IsAny <IDeserializer>())).Callback <string, IDeserializer>((ct, d) => deserializer = d);

            restMock.Setup(x => x.Execute <ResponseCollection <TradeInfo> >(It.IsAny <IRestRequest>())).Returns <IRestRequest>(request =>
            {
                var responseMock = new Mock <IRestResponse <ResponseCollection <TradeInfo> > >(MockBehavior.Strict);
                responseMock.SetupAllProperties();

                responseMock.Object.Content = Properties.Resources.TradesInfoResponse;
                responseMock.Object.Request = request;
                responseMock.Object.Data    = deserializer.Deserialize <ResponseCollection <TradeInfo> >(responseMock.Object);

                return(responseMock.Object);
            });

            var client = new BitsoClient(restMock.Object, ServiceUrlMock, ApiKeyMock, ApiSecretMock);

            var res = client.GetTrades("btc_mxn");

            Assert.AreNotEqual <int>(0, res.Count);
            Assert.AreNotEqual <decimal>(0, res[0].Amount);
            Assert.IsFalse(string.IsNullOrEmpty(res[0].BookName));

            Assert.IsTrue(res[0].MakerSide != MarketSide.None);
            Assert.AreNotEqual <decimal>(0M, res[0].Price);
            Assert.AreNotEqual <decimal>(0M, res[0].Amount);
            Assert.AreNotEqual <long>(0, res[0].TradeId);

            var diff = res[0].CreatedAt - default(System.DateTime);

            Assert.IsTrue(diff > System.TimeSpan.Zero);
        }
Exemplo n.º 12
0
        public static async Task <ResultT> DecodeAsync <ResultT, M>(this AbstractStreamSource source, IDeserializer <ResultT, M> deserializer, IProgress prog = null)
            where M : MediaType
        {
            if (source is null)
            {
                throw new System.ArgumentNullException(nameof(source));
            }

            if (deserializer is null)
            {
                throw new System.ArgumentNullException(nameof(deserializer));
            }

            prog.Report(0);
            using var stream = await source
                               .GetStreamAsync(prog)
                               .ConfigureAwait(false);

            var value = deserializer.Deserialize(stream);

            prog.Report(1);
            return(value);
        }
Exemplo n.º 13
0
        public async Task <FrontMatter?> ReadFrontMatterAsync(string postPath)
        {
            var frontMatterYaml = await File.ReadAllTextAsync(postPath);

            if (frontMatterYaml == null)
            {
                return(null);
            }

            var temp = frontMatterYaml.Split("---");

            if (temp.Length < 2)
            {
                return(null);
            }

            var frontMatter = _yamlDeserializer
                              .Deserialize <FrontMatter>(temp[1]);

            // Cleanup front matter
            frontMatter.Title = frontMatter.Title.Replace("&amp;", "&");
            return(frontMatter);
        }
Exemplo n.º 14
0
        public static RemotePackageConfig DownloadPackageConfig(Config config)
        {
            string packageConfigName = config.Debug.AlternatePackageConfigFile ?? "packageconfig.yaml";
            string rawData;

            try
            {
                rawData = RemoteObjectManager.Instance.DownloadObjectAsString(packageConfigName);
            }
            catch (Exception ex)
            {
                throw new PackageException($"Failed to download package config: {ex.Message}", ex);
            }

            try
            {
                return(deserializer.Deserialize <RemotePackageConfig>(rawData));
            }
            catch (Exception ex)
            {
                throw new PackageException($"Failed to parse package config: {ex.Message}", ex);
            }
        }
Exemplo n.º 15
0
        public void Process()
        {
            try
            {
                Stream = client.GetStream();

                string state = GetState();
                SendState(state);

                while (true)
                {
                    try
                    {
                        string   activityString = GetMessage();
                        Activity activity       = deserializer.Deserialize <Activity>(activityString);
                        clientModel.Move(activity);

                        server.SendAll(GetState());
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e.Message);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
            }
            finally
            {
                server.RemoveConnection(Id);
                Close();
            }
        }
Exemplo n.º 16
0
        internal static T CustomField <T>(IDeserializer context, IDeserializer <T> custom, string name, ICfgNode node, bool required)
        {
            try
            {
                var field = node.NestedByName(name).FirstOrDefault();
                if (field == null)
                {
                    if (required)
                    {
                        throw new FormatException("field not found");
                    }
                    else
                    {
                        return(default(T));
                    }
                }

                return(custom.Deserialize(context, field));
            }
            catch (Exception ex)
            {
                throw new DeserializeChildException(name, ex);
            }
        }
Exemplo n.º 17
0
    public IEnumerable <T> LoadFromStream(TextReader stream, string configSection)
    {
        var parser = new Parser(stream);

        parser.Consume <StreamStart>();
        parser.Consume <DocumentStart>();
        parser.Consume <MappingStart>();

        var validConfigs = new List <T>();

        while (parser.TryConsume <Scalar>(out var key))
        {
            if (key.Value != configSection)
            {
                parser.SkipThisAndNestedEvents();
                continue;
            }

            var configs = _deserializer.Deserialize <List <T>?>(parser);
            if (configs == null)
            {
                parser.SkipThisAndNestedEvents();
                continue;
            }

            ValidateConfigs(configSection, configs, validConfigs);
            parser.SkipThisAndNestedEvents();
        }

        if (validConfigs.Count == 0)
        {
            throw new ConfigurationException(configSection, typeof(T), "There are no configured instances defined");
        }

        return(validConfigs);
    }
Exemplo n.º 18
0
        public async Task Load(ILogger logger, CancellationToken token)
        {
            var init = this.Root.File(InitFilename);

            if (!init.Exists)
            {
                logger.LogWarning($"Settings file {InitFilename} not found. Will use default settings");
                return;
            }

            try
            {
                logger.LogInformation("Loading settings from {0}", init.FullName);
                using (var stream = init.OpenRead())
                    using (var reader = new StreamReader(stream))
                    {
                        _values = GetValues(await Task.Run(() => YamlDeserializer.Deserialize <Dictionary <string, object> >(reader)));
                    }
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to load settings from {init.FullName}: {ex.Message}", ex);
            }
        }
        static public async Task Handle(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                ErrorResult errorResult = null;
                try
                {
                    IDeserializer <HttpResponseMessage, Task <ErrorResult> > d = DeserializerFactory.GetFactory().CreateErrorResultDeserializer();

                    errorResult = await d.Deserialize(response);
                }
                catch (XmlException ex)
                {
                    throw ex;
                    //response.EnsureSuccessful();
                }
                catch (InvalidOperationException invalidEx)
                {
                    throw invalidEx;
                    //response.EnsureSuccessful();
                }
                throw ExceptionFactory.CreateException(errorResult.Code, errorResult.Message, errorResult.RequestId, errorResult.HostId);
            }
        }
Exemplo n.º 20
0
        public void GetAccountFeesExpected()
        {
            IDeserializer deserializer = null;

            var restMock = new Mock <IRestClient>(MockBehavior.Strict);

            restMock.SetupAllProperties();
            restMock.Setup(x => x.AddHandler("application/json", It.IsAny <IDeserializer>())).Callback <string, IDeserializer>((ct, d) => deserializer = d);

            restMock.Setup(x => x.Execute <ResponseSingle <AccountFees> >(It.IsAny <IRestRequest>())).Returns <IRestRequest>(request =>
            {
                var responseMock = new Mock <IRestResponse <ResponseSingle <AccountFees> > >(MockBehavior.Strict);
                responseMock.SetupAllProperties();

                responseMock.Object.Content = Properties.Resources.AccountFeesResponse;
                responseMock.Object.Request = request;
                responseMock.Object.Data    = deserializer.Deserialize <ResponseSingle <AccountFees> >(responseMock.Object);

                return(responseMock.Object);
            });

            var client = new BitsoClient(restMock.Object, ServiceUrlMock, ApiKeyMock, ApiSecretMock);

            var res = client.GetAccountFees();

            Assert.IsNotNull(res);
            Assert.AreNotEqual <int>(0, res.TradeFees.Count);

            Assert.IsFalse(string.IsNullOrEmpty(res.TradeFees[0].BookName));
            Assert.AreNotEqual <decimal>(0M, res.TradeFees[0].TakerPercent);
            Assert.AreNotEqual <decimal>(0M, res.TradeFees[0].TakerValue);
            Assert.AreNotEqual <decimal>(0M, res.TradeFees[0].MakerPercent);
            Assert.AreNotEqual <decimal>(0M, res.TradeFees[0].MakerValue);
            Assert.AreNotEqual <int>(0, res.WithdrawalFees.Count);
            Assert.AreNotEqual <decimal>(0M, res.WithdrawalFees.First().Value);
        }
Exemplo n.º 21
0
        public async Task GetSportEventSummaryAsync(URN id, CultureInfo culture, ISportEventCI requester)
        {
            RecordCall("GetSportEventSummaryAsync");
            var strId    = id?.ToString().Replace(":", "_") ?? string.Empty;
            var filePath = GetFile($"summary_{strId}.{culture.TwoLetterISOLanguageName}.xml", culture);

            if (string.IsNullOrEmpty(filePath) && id?.TypeGroup != ResourceTypeGroup.BASIC_TOURNAMENT && id?.TypeGroup != ResourceTypeGroup.TOURNAMENT && id?.TypeGroup != ResourceTypeGroup.SEASON)
            {
                filePath = GetFile(MatchDetailsXml, culture);
            }

            if (string.IsNullOrEmpty(filePath))
            {
                return;
            }
            var mapper = new SportEventSummaryMapperFactory();
            var stream = FileHelper.OpenFile(filePath);
            var result = mapper.CreateMapper(_restDeserializer.Deserialize(stream)).Map();

            if (result != null)
            {
                await _cacheManager.SaveDtoAsync(id, result, culture, DtoType.SportEventSummary, requester).ConfigureAwait(false);
            }
        }
 public void Deserialize(IDeserializer deserializer)
 {
     deserializer.Deserialize("PlantShadows", shadowFB);
     deserializer.Deserialize("Buttons", buttonFB);
 }
Exemplo n.º 23
0
        private static void PatchList(Context context, List <AssetFile> sources, Stream stream)
        {
            foreach (var source in sources)
            {
                string sourceText = File.ReadAllText(context.GetSourceModAssetPath(source.Name));
                switch (source.Type)
                {
                case "trsr":
                    var trsrList   = Kh2.SystemData.Trsr.Read(stream).ToDictionary(x => x.Id, x => x);
                    var moddedTrsr = deserializer.Deserialize <Dictionary <ushort, Kh2.SystemData.Trsr> >(sourceText);
                    foreach (var treasure in moddedTrsr)
                    {
                        if (trsrList.ContainsKey(treasure.Key))
                        {
                            if (treasure.Value.World == 0)
                            {
                                trsrList[treasure.Key].ItemId = treasure.Value.ItemId;
                            }
                            else
                            {
                                trsrList[treasure.Key] = treasure.Value;
                            }
                        }
                        else
                        {
                            trsrList.Add(treasure.Key, treasure.Value);
                        }
                    }
                    Kh2.SystemData.Trsr.Write(stream.SetPosition(0), trsrList.Values);
                    break;

                case "item":
                    var itemList   = Kh2.SystemData.Item.Read(stream);
                    var moddedItem = deserializer.Deserialize <Kh2.SystemData.Item>(sourceText);
                    if (moddedItem.Items != null)
                    {
                        foreach (var item in moddedItem.Items)
                        {
                            var itemToUpdate = itemList.Items.FirstOrDefault(x => x.Id == item.Id);
                            if (itemToUpdate != null)
                            {
                                itemList.Items[itemList.Items.IndexOf(itemToUpdate)] = item;
                            }
                            else
                            {
                                itemList.Items.Add(item);
                            }
                        }
                    }
                    if (moddedItem.Stats != null)
                    {
                        foreach (var item in moddedItem.Stats)
                        {
                            var itemToUpdate = itemList.Stats.FirstOrDefault(x => x.Id == item.Id);
                            if (itemToUpdate != null)
                            {
                                itemList.Stats[itemList.Stats.IndexOf(itemToUpdate)] = item;
                            }
                            else
                            {
                                itemList.Stats.Add(item);
                            }
                        }
                    }
                    itemList.Write(stream.SetPosition(0));
                    break;

                case "fmlv":
                    var formRaw  = Kh2.Battle.Fmlv.Read(stream).ToList();
                    var formList = new Dictionary <Kh2.Battle.Fmlv.FormFm, List <Kh2.Battle.Fmlv> >();
                    foreach (var form in formRaw)
                    {
                        if (!formList.ContainsKey(form.FinalMixForm))
                        {
                            formList.Add(form.FinalMixForm, new List <Kh2.Battle.Fmlv>());
                        }
                        formList[form.FinalMixForm].Add(form);
                    }
                    var moddedForms = deserializer.Deserialize <Dictionary <Kh2.Battle.Fmlv.FormFm, List <FmlvDTO> > >(sourceText);
                    foreach (var form in moddedForms)
                    {
                        foreach (var level in form.Value)
                        {
                            formList[form.Key][level.FormLevel - 1].Ability = (ushort)level.Ability;
                            formList[form.Key][level.FormLevel - 1].Exp     = level.Experience;
                            formList[form.Key][level.FormLevel - 1].Unk1    = level.GrowthAbilityLevel;
                        }
                    }
                    Kh2.Battle.Fmlv.Write(stream.SetPosition(0), formList.Values.SelectMany(x => x).ToList());
                    break;

                case "lvup":
                    var levelList    = Kh2.Battle.Lvup.Read(stream);
                    var moddedLevels = deserializer.Deserialize <Dictionary <string, Dictionary <int, Kh2.Battle.Lvup.PlayableCharacter.Level> > > (sourceText);

                    foreach (var character in moddedLevels)
                    {
                        foreach (var level in moddedLevels[character.Key])
                        {
                            levelList.Characters[characterMap[character.Key] - 1].Levels[level.Key - 1] = moddedLevels[character.Key][level.Key];
                        }
                    }

                    levelList.Write(stream.SetPosition(0));
                    break;

                case "bons":
                    var bonusRaw  = Kh2.Battle.Bons.Read(stream);
                    var bonusList = new Dictionary <byte, Dictionary <string, Kh2.Battle.Bons> >();
                    foreach (var bonus in bonusRaw)
                    {
                        if (!bonusList.ContainsKey(bonus.RewardId))
                        {
                            bonusList.Add(bonus.RewardId, new Dictionary <string, Kh2.Battle.Bons>());
                        }
                        var character = characterMap.First(x => x.Value == bonus.CharacterId).Key;
                        if (!bonusList[bonus.RewardId].ContainsKey(character))
                        {
                            bonusList[bonus.RewardId].Add(character, bonus);
                        }
                    }
                    var moddedBonus = deserializer.Deserialize <Dictionary <byte, Dictionary <string, Kh2.Battle.Bons> > >(sourceText);
                    foreach (var bonus in moddedBonus)
                    {
                        if (!bonusList.ContainsKey(bonus.Key))
                        {
                            bonusList.Add(bonus.Key, new Dictionary <string, Kh2.Battle.Bons>());
                        }
                        foreach (var character in moddedBonus[bonus.Key])
                        {
                            if (!bonusList[bonus.Key].ContainsKey(character.Key))
                            {
                                bonusList[bonus.Key].Add(character.Key, new Kh2.Battle.Bons());
                            }
                            bonusList[bonus.Key][character.Key] = character.Value;
                        }
                    }

                    Kh2.Battle.Bons.Write(stream.SetPosition(0), bonusList.Values.SelectMany(x => x.Values));
                    break;

                case "objentry":
                    var objEntryList   = Kh2.Objentry.Read(stream).ToDictionary(x => x.ObjectId, x => x);
                    var moddedObjEntry = deserializer.Deserialize <Dictionary <uint, Kh2.Objentry> >(sourceText);
                    foreach (var objEntry in moddedObjEntry)
                    {
                        if (objEntryList.ContainsKey(objEntry.Key))
                        {
                            objEntryList[objEntry.Key] = objEntry.Value;
                        }
                        else
                        {
                            objEntryList.Add(objEntry.Key, objEntry.Value);
                        }
                    }
                    Kh2.Objentry.Write(stream.SetPosition(0), objEntryList.Values);
                    break;

                case "plrp":
                    var plrpList   = Kh2.Battle.Plrp.Read(stream);
                    var moddedPlrp = deserializer.Deserialize <List <Kh2.Battle.Plrp> >(sourceText);
                    foreach (var plrp in moddedPlrp)
                    {
                        var oldPlrp = plrpList.First(x => x.Character == plrp.Character && x.Id == plrp.Id);
                        plrpList[plrpList.IndexOf(oldPlrp)] = plrp;
                    }
                    Kh2.Battle.Plrp.Write(stream.SetPosition(0), plrpList);
                    break;

                default:
                    break;
                }
            }
        }
        public ConfigurationDefinition ReadDefinition(Stream input)
        {
            var yaml = deserializer.Deserialize <YamlTemplate>(new StreamReader(input));

            var guid            = Guid.Parse(yaml.Metadata.Guid);
            var descriptionGuid = Guid.Parse(yaml.Template.Guid);

            var description = componentDescriptionLookup.GetDescription(new TypeDescriptionComponentType(descriptionGuid, ComponentType.Unknown(yaml.Template.Name)));

            if (description == null)
            {
                throw new InvalidOperationException("Base description for template not found.");
            }

            var metadata = new DefinitionMetadata
            {
                Guid        = guid,
                Description = yaml.Metadata.Description,
                Name        = yaml.Metadata.Name,
                Version     = yaml.Metadata.Version,
            };

            var properties = new Dictionary <PropertyName, PropertyValue>();

            foreach (var setter in yaml.Properties)
            {
                var componentProperty = description.Properties.FirstOrDefault(x => x.Name == setter.Name);
                if (componentProperty == null)
                {
                    throw new InvalidOperationException($"Property '{setter.Name}' does not exist on component type '{description.ComponentName}'");
                }

                PropertyValue value;
                switch (componentProperty.Type)
                {
                case PropertyType.Boolean:
                {
                    value = new PropertyValue((bool)setter.Value);
                    break;
                }

                case PropertyType.Decimal:
                {
                    value = new PropertyValue((double)setter.Value);
                    break;
                }

                case PropertyType.Integer:
                {
                    value = new PropertyValue((int)setter.Value);
                    break;
                }

                case PropertyType.Enum:
                case PropertyType.String:
                {
                    value = new PropertyValue((string)setter.Value);
                    break;
                }

                default:
                {
                    throw new NotSupportedException("Unknown property type.");
                }
                }

                properties.Add(componentProperty.SerializedName, value);
            }

            var configuration = new ComponentConfiguration(null, yaml.Metadata.Name, properties);

            return(new ConfigurationDefinition(metadata, description, configuration));
        }
Exemplo n.º 25
0
 /// <summary>
 /// Parse the string into dotnet object.
 /// </summary>
 /// <param name="input">String representation of YAML file.</param>
 /// <returns>The application configuration object.</returns>
 public static ImageScannerConfiguration Parse(string input)
 {
     return(Deserializer.Deserialize <ImageScannerConfiguration>(input));
 }
Exemplo n.º 26
0
        private async Task Fetch(ChannelWriter <Message <TKey, TValue> > messageWriter, string topic, PartitionMetadata partition, BrokerConnection broker, CancellationToken cancellationToken)
        {
            long offset = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                var fetch = await broker.Fetch(new FetchRequest
                {
                    ReplicaId      = -1,
                    MaxWaitTime    = 100,
                    MinBytes       = 1,
                    MaxBytes       = 64 * 1024,
                    IsolationLevel = 0,
                    SessionId      = 0,
                    SessionEpoc    = -1,
                    Topics         = new []
                    {
                        new FetchRequest.Topic
                        {
                            TopicName  = topic,
                            Partitions = new []
                            {
                                new FetchRequest.Topic.Partition
                                {
                                    PartitionId       = partition.PartitionId,
                                    CurrentLeaderEpoc = -1,
                                    FetchOffset       = offset,
                                    LogStartOffset    = -1,
                                    PartitionMaxBytes = 32 * 1024
                                }
                            }
                        }
                    }
                });

                long maxoffset = 0;
                long high      = 0;
                foreach (var r in fetch.Responses)
                {
                    foreach (var pr in r.PartitionResponses)
                    {
                        high = pr.HighWaterMark;
                        foreach (var batch in pr.RecordBatches)
                        {
                            foreach (var rec in batch.Records)
                            {
                                if (rec.Offset > maxoffset)
                                {
                                    maxoffset = rec.Offset;
                                }

                                var msg = new Message <TKey, TValue>
                                {
                                    Timestamp   = Timestamp.UnixTimestampMsToDateTime(batch.FirstTimeStamp + rec.TimeStampDelta),
                                    Topic       = r.TopicName,
                                    PartitionId = pr.PartitionId,
                                    Offset      = rec.Offset,
                                    Key         = await keyDeserializer.Deserialize(rec.Key),
                                    Value       = await valueDeserializer.Deserialize(rec.Value)
                                };

                                if (rec.Headers.Count > 0)
                                {
                                    foreach (var h in rec.Headers)
                                    {
                                        msg.AddHeader(h.Key, h.Value);
                                    }
                                }

                                await messageWriter.WriteAsync(msg, cancellationToken);
                            }
                        }
                    }
                }

                if (maxoffset > 0)
                {
                    offset = maxoffset + 1;
                }
                if (offset >= high)
                {
                    await Task.Delay(200); // linger
                }
            }
        }
Exemplo n.º 27
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer message, List <object> output)
        {
            if (context == null || message == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var continueToDecode = true;
            var temp             = new List <IPacket>();
            var encryptedString  = "";
            var mapper           = SessionFactory.Instance.Sessions[context.Channel.Id.AsLongText()];

            _region    = mapper.RegionType;
            _sessionId = mapper.SessionId;
            var str = ((Span <byte>)message.Array).Slice(message.ArrayOffset, message.ReadableBytes).ToArray();

            if (_sessionId == 0)
            {
                if (!(_deserializer.Deserialize(DecryptCustomParameter(str, out var endofPacket)) is UnresolvedPacket pack))
                {
                    throw new ArgumentNullException(nameof(context));
                }

                if (!int.TryParse(pack.Header, out _sessionId))
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.ERROR_SESSIONID));
                    return;
                }

                SessionFactory.Instance.Sessions[context.Channel.Id.AsLongText()].SessionId = _sessionId;
                _logger.Information(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CLIENT_CONNECTED),
                                    mapper.SessionId);
                temp.Add(pack);
                if (endofPacket.Length == 0)
                {
                    continueToDecode = false;
                }

                str = endofPacket;
            }

            if (continueToDecode)
            {
                var sessionKey    = _sessionId & 0xFF;
                var sessionNumber = unchecked ((byte)(_sessionId >> 6));
                sessionNumber &= 0xFF;
                sessionNumber &= unchecked ((byte)0x80000003);

                switch (sessionNumber)
                {
                case 0:
                    encryptedString =
                        (from character in str
                         let firstbyte = unchecked ((byte)(sessionKey + 0x40))
                                         select unchecked ((byte)(character - firstbyte))).Aggregate(encryptedString,
                                                                                                     (current, highbyte) => current + (char)highbyte);
                    break;

                case 1:
                    encryptedString =
                        (from character in str
                         let firstbyte = unchecked ((byte)(sessionKey + 0x40))
                                         select unchecked ((byte)(character + firstbyte))).Aggregate(encryptedString,
                                                                                                     (current, highbyte) => current + (char)highbyte);
                    break;

                case 2:
                    encryptedString =
                        (from character in str
                         let firstbyte = unchecked ((byte)(sessionKey + 0x40))
                                         select unchecked ((byte)((character - firstbyte) ^ 0xC3))).Aggregate(encryptedString,
                                                                                                              (current, highbyte) => current + (char)highbyte);
                    break;

                case 3:
                    encryptedString =
                        (from character in str
                         let firstbyte = unchecked ((byte)(sessionKey + 0x40))
                                         select unchecked ((byte)((character + firstbyte) ^ 0xC3))).Aggregate(encryptedString,
                                                                                                              (current, highbyte) => current + (char)highbyte);
                    break;

                default:
                    encryptedString += (char)0xF;
                    break;
                }

                temp.AddRange(encryptedString.Split((char)0xFF, StringSplitOptions.RemoveEmptyEntries).Select(p =>
                {
                    try
                    {
                        var packet = _deserializer.Deserialize(DecryptPrivate(p));
                        if (!packet.IsValid)
                        {
                            _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CORRUPT_PACKET),
                                          packet);
                        }

                        return(packet);
                    }
#pragma warning disable CA1031 // Do not catch general exception types
                    catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                    {
                        _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.ERROR_DECODING),
                                      ex.Data["Packet"]);
                        ushort?keepalive = null;
                        if (ushort.TryParse(ex.Data["Packet"]?.ToString()?.Split(" ")[0], out var kpalive))
                        {
                            keepalive = kpalive;
                        }
                        return(new UnresolvedPacket
                        {
                            KeepAliveId = keepalive, Header = "0"
                        });
                    }
                }));
            }

            if (temp.Count > 0)
            {
                output?.Add(temp);
            }
        }
Exemplo n.º 28
0
 public object Deserialize(string input, IDeserializer deserializer)
 {
     var obj = deserializer.Deserialize(input);
     return obj;
 }
Exemplo n.º 29
0
 public Task <object> Execute(object data, object state = null)
 {
     return(Task.FromResult(_deserializer.Deserialize((DataObject)data, _type, _args)));
 }
Exemplo n.º 30
0
        /// <summary>Deserializes the given file into the specified object using the specified factory.</summary>
        /// <typeparam name="T">The object to return to.</typeparam>
        /// <param name="FactoryName">The name of the factory, <see cref="Serialization.GetFactoryNames"/>.</param>
        /// <param name="stream">The stream to read from.</param>
        /// <returns>Deserializes the given file into the specified object using the specified factory.</returns>
        public static T Deserialize <T>(String FactoryName, Stream stream)
        {
            IDeserializer <T, Stream> deserializer = GetDeserializer <T>(FactoryName);

            return(deserializer.Deserialize(stream));
        }
Exemplo n.º 31
0
 public T Deserialize <T>(string data)
 => _deserializer.Deserialize <T>(data);
Exemplo n.º 32
0
        public static void ParseInput(string input, IDeserializer deserializer)
        {
            var result = TomlGrammar.Document.TryParse(input);
            if (!result.WasSuccessful)
                throw new GrammarException(result.Message);

            var tables = result.Value;
            var tableValueMap = tables.ToDictionary(x => x, x => new TableValue(x.Title, x.KeyValuePairs));
            // The dictionary holds all non-array tables, and most recently processed array table element
            var tableLookup = new Dictionary<string[], TableAndValue>(new TableKeyComparer());
            foreach (var item in tableValueMap.Where(x => !x.Key.IsArrayTable))
            {
                if (tableLookup.ContainsKey(item.Key.Title))
                    throw new DuplicateTableKeyException(item.Key.ParentTitle, item.Key.ActualTitle);
                tableLookup.Add(item.Key.Title, new TableAndValue(item.Key, item.Value));
            }
            // A list of all array tables
            var arrayTableTitles = new HashSet<string[]>(tables.Where(x => x.IsArrayTable).Select(x => x.Title), new TableKeyComparer());

            // This could probably be refactored out into its own method... But I want to keep the parser stateless for now,
            // and I could create an inner class... but the compiler will do that for me this way
            Action<Table> ensureTableDeclaredAndWiredInLookup = null;
            ensureTableDeclaredAndWiredInLookup = table =>
            {
                TableAndValue parent;
                // The case for tables that imply the existence of their parent, eifi [foo.bar] where [foo] isn't defined
                if (!tableLookup.TryGetValue(table.ParentTitle, out parent))
                {
                    if (arrayTableTitles.Contains(table.ParentTitle))
                        throw new ParseException("Whoops"); // TODO better message

                    var parentTable = new Table(table.ParentTitle, Enumerable.Empty<KeyValuePair>(), false);
                    parent = new TableAndValue(parentTable, new TableValue(parentTable.Title));
                    tableValueMap.Add(parentTable, parent.TableValue);
                    ensureTableDeclaredAndWiredInLookup(parentTable);
                }

                var tableValue = tableValueMap[table];
                if (table.IsArrayTable)
                    parent.TableValue.AddChildArrayTable(table.ActualTitle, tableValue);
                else
                    parent.TableValue.AddKeyValuePair(table.ActualTitle, tableValue);
            };

            Table mostRecentlyDefinedTableElement = null;

            // Assign child tables to their parents
            foreach (var table in tables)
            {
                if (table.IsArrayTable)
                    tableLookup[table.Title] = new TableAndValue(table, tableValueMap[table]);

                if (table.ParentTitle != null)
                    ensureTableDeclaredAndWiredInLookup(table);

                mostRecentlyDefinedTableElement = table;
            }

            // Tables doesn't contain the newly-created intermediate tables
            foreach (var table in tableValueMap.Values)
            {
                table.FlattenChildArrayTables();
            }

            deserializer.Deserialize(tableLookup[new string[0]].TableValue);
        }
Exemplo n.º 33
0
 public Collection(string text, string base_dir)
     : this(_Deserializer.Deserialize <YAML.Collection>(text), base_dir)
 {
 }
Exemplo n.º 34
0
 public static Database Load(string text, IDeserializer serializer)
 {
     return new Database(serializer.Deserialize(text));
 }