// // LangParser.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Text; using NosSmooth.Data.Abstractions; using NosSmooth.Data.Abstractions.Language; using NosSmooth.Data.NOSFiles.Files; using Remora.Results; namespace NosSmooth.Data.NOSFiles.Parsers; /// /// Language txt file parser. /// public class LangParser { /// /// Parse the given language. /// /// The NosTale files. /// The language to parse. /// Translations or an error. public Result>> Parse(NostaleFiles files, Language language) { if (!files.LanguageFiles.ContainsKey(language)) { return new NotFoundError($"Could not find the language file for {language}."); } var archive = files.LanguageFiles[language]; var encoding = LanguageEncoding.GetEncoding(language); var dictionary = new Dictionary>(); var itemParsedResult = ParseFile(archive, encoding, $"_code_{language.ToString().ToLower()}_Item.txt"); if (!itemParsedResult.IsSuccess) { return Result>>.FromError (itemParsedResult); } dictionary.Add(TranslationRoot.Item, itemParsedResult.Entity); var monsterParsedResult = ParseFile(archive, encoding, $"_code_{language.ToString().ToLower()}_monster.txt"); if (!monsterParsedResult.IsSuccess) { return Result>>.FromError (monsterParsedResult); } dictionary.Add(TranslationRoot.Monster, itemParsedResult.Entity); var skillParsedResult = ParseFile(archive, encoding, $"_code_{language.ToString().ToLower()}_Skill.txt"); if (!skillParsedResult.IsSuccess) { return Result>>.FromError (skillParsedResult); } dictionary.Add(TranslationRoot.Skill, itemParsedResult.Entity); var mapParsedResult = ParseFile(archive, encoding, $"_code_{language.ToString().ToLower()}_MapIDData.txt"); if (!mapParsedResult.IsSuccess) { return Result>>.FromError (mapParsedResult); } dictionary.Add(TranslationRoot.Map, itemParsedResult.Entity); return dictionary; } private Result> ParseFile(FileArchive files, Encoding encoding, string name) { var fileResult = files.FindFile(name); if (!fileResult.IsSuccess) { return Result>.FromError(fileResult); } var fileContent = encoding.GetString(fileResult.Entity.Content); var dictionary = new Dictionary(); var lines = fileContent.Split('\r', '\n'); foreach (var line in lines) { var splitted = line.Split('\t'); if (splitted.Length != 2) { continue; } dictionary.Add(splitted[0], splitted[1]); } return dictionary; } }