Ejemplo n.º 1
0
        private async Task WriteSoundSettingsToCache(string filename, Dictionary <string, ISoundSettings> cache)
        {
            var data = await FileAsync.ReadAllTextAsync(filename);

            var settings = new JsonSerializerSettings();

            settings.Converters.Add(new SoundSettingsConverter(filename));

            cache[filename] = JsonConvert.DeserializeObject <ISoundSettings>(data, settings);
        }
        /// <summary>
        /// Loads the bot token from disk.
        /// </summary>
        /// <exception cref="FileNotFoundException">Thrown if the bot token file can't be found.</exception>
        /// <exception cref="InvalidDataException">Thrown if no token exists in the file.</exception>
        private async Task LoadBotTokenAsync()
        {
            var tokenPath = Path.Combine(this.BaseContentPath, "bot.token");

            if (!File.Exists(tokenPath))
            {
                throw new FileNotFoundException("The bot token file could not be found.", tokenPath);
            }

            var token = await FileAsync.ReadAllTextAsync(tokenPath);

            if (string.IsNullOrEmpty(token))
            {
                throw new InvalidDataException("Missing bot token.");
            }

            this.BotToken = token;
        }
        public async Task <RetrieveEntityResult <IReadOnlyList <Transformation> > > DiscoverBundledTransformationsAsync
        (
            [NotNull] GlobalInfoContext db,
            [NotNull] TransformationService transformation,
            [NotNull] Species species
        )
        {
            const string speciesFilename = "Species.yml";

            var speciesDir          = GetSpeciesDirectory(species);
            var transformationFiles = Directory.EnumerateFiles(speciesDir).Where(p => !p.EndsWith(speciesFilename));

            var transformations = new List <Transformation>();
            var deser           = new DeserializerBuilder()
                                  .WithTypeConverter(new ColourYamlConverter())
                                  .WithTypeConverter(new SpeciesYamlConverter(db, transformation))
                                  .WithNodeDeserializer(i => new ValidatingNodeDeserializer(i), s => s.InsteadOf <ObjectNodeDeserializer>())
                                  .WithNamingConvention(new UnderscoredNamingConvention())
                                  .Build();

            foreach (var transformationFile in transformationFiles)
            {
                string content = await FileAsync.ReadAllTextAsync(transformationFile);

                try
                {
                    transformations.Add(deser.Deserialize <Transformation>(content));
                }
                catch (YamlException yex)
                {
                    if (yex.InnerException is SerializationException sex)
                    {
                        return(RetrieveEntityResult <IReadOnlyList <Transformation> > .FromError(sex));
                    }

                    return(RetrieveEntityResult <IReadOnlyList <Transformation> > .FromError(yex));
                }
            }

            return(RetrieveEntityResult <IReadOnlyList <Transformation> > .FromSuccess(transformations));
        }
        public async Task <RetrieveEntityResult <IReadOnlyList <Species> > > DiscoverBundledSpeciesAsync()
        {
            const string speciesFilename = "Species.yml";

            var deser = new DeserializerBuilder()
                        .WithNodeDeserializer(i => new ValidatingNodeDeserializer(i), s => s.InsteadOf <ObjectNodeDeserializer>())
                        .WithNamingConvention(new UnderscoredNamingConvention())
                        .Build();

            var species        = new List <Species>();
            var speciesFolders = Directory.EnumerateDirectories(this.BaseTransformationSpeciesPath);

            foreach (string directory in speciesFolders)
            {
                string speciesFilePath = Path.Combine(directory, speciesFilename);
                if (!File.Exists(speciesFilePath))
                {
                    continue;
                }

                string content = await FileAsync.ReadAllTextAsync(speciesFilePath, Encoding.UTF8);

                try
                {
                    species.Add(deser.Deserialize <Species>(content));
                }
                catch (YamlException yex)
                {
                    if (yex.InnerException is SerializationException sex)
                    {
                        return(RetrieveEntityResult <IReadOnlyList <Species> > .FromError(sex));
                    }

                    return(RetrieveEntityResult <IReadOnlyList <Species> > .FromError(yex));
                }
            }

            return(RetrieveEntityResult <IReadOnlyList <Species> > .FromSuccess(species));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Load JSON object from a file.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static async Task <object> LoadJSONFromFile(string filePath)
        {
            string value = await FileAsync.ReadAllTextAsync(filePath).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject(value));
        }