示例#1
0
        public static XElement BuildBackstoryElementTodoWithEnglishComments(Backstory originalBackstory)
        {
            XElement backstoryElement = new XElement(originalBackstory.Id);

            backstoryElement.Add(NewLine);

            backstoryElement.Add(Tab2, new XComment($" EN: {originalBackstory.Title} "), NewLine);
            backstoryElement.Add(Tab2, new XElement("title", originalBackstory.Title), NewLine);

            if (!string.IsNullOrEmpty(originalBackstory.TitleFemale))
            {
                backstoryElement.Add(Tab2, new XComment($" EN: {originalBackstory.TitleFemale} "), NewLine);
                backstoryElement.Add(Tab2, new XElement("titleFemale", originalBackstory.TitleFemale), NewLine);
            }

            backstoryElement.Add(Tab2, new XComment($" EN: {originalBackstory.TitleShort} "), NewLine);
            backstoryElement.Add(Tab2, new XElement("titleShort", originalBackstory.TitleShort), NewLine);

            if (!string.IsNullOrEmpty(originalBackstory.TitleShortFemale))
            {
                backstoryElement.Add(Tab2, new XComment($" EN: {originalBackstory.TitleShortFemale} "), NewLine);
                backstoryElement.Add(Tab2, new XElement("titleShortFemale", originalBackstory.TitleShortFemale), NewLine);
            }

            backstoryElement.Add(Tab2, new XComment($" EN: {originalBackstory.Description} "), NewLine);
            backstoryElement.Add(Tab2, new XElement("desc", originalBackstory.Description), NewLine);
            backstoryElement.Add(Tab);

            return(backstoryElement);
        }
示例#2
0
        public static bool TryReadPawnBioBackstories(XElement bioElem, out Backstory child, out Backstory adult)
        {
            string firstName = bioElem.Element("Name", true).Element("First", true).Value;
            string lastName  = bioElem.Element("Name", true).Element("Last", true).Value;
            string nickName  = bioElem.Element("Name", true).Element("Nick", true)?.Value;
            string gender    = bioElem.Element("Gender", true).Value;

            XElement childhoodElement = bioElem.Element("Childhood", true);
            XElement adulthoodElement = bioElem.Element("Adulthood", true);

            child = adult = null;

            if (childhoodElement == null && adulthoodElement == null)
            {
                return(false);
            }

            child = ReadBackstoryElementResource(childhoodElement);
            adult = ReadBackstoryElementResource(adulthoodElement);

            child.Slot = BackstorySlot.Childhood;
            adult.Slot = BackstorySlot.Adulthood;

            child.FirstName = adult.FirstName = firstName;
            child.LastName  = adult.LastName = lastName;
            child.NickName  = adult.NickName = nickName;
            child.Gender    = adult.Gender = gender;

            return(true);
        }
示例#3
0
        private static string GetHint(Backstory backstory)
        {
            StringBuilder hintBuilder = new StringBuilder();

            if (Backstory.IsSolid(backstory))
            {
                hintBuilder.Append($"{backstory.FirstName} ");

                if (!string.IsNullOrEmpty(backstory.NickName))
                {
                    hintBuilder.Append($"\"{backstory.NickName}\" ");
                }

                hintBuilder.Append($"{backstory.LastName}");

                if (!string.IsNullOrEmpty(backstory.Gender))
                {
                    hintBuilder.Append($", {backstory.Gender}");
                }

                hintBuilder.Append($", {backstory.Slot}");
            }
            else
            {
                if (!string.IsNullOrEmpty(backstory.Category))
                {
                    hintBuilder.Append($"{backstory.Category}");
                }
            }

            return(hintBuilder.ToString());
        }
示例#4
0
        private static Backstory FindBestMatch(HashSet <Backstory> backstories, Backstory backstory)
        {
            string    id        = Backstory.GetIdentifier(backstory);
            Backstory matchById = backstories.FirstOrDefault(bs => Backstory.GetIdentifier(bs) == id);

            if (matchById != null)
            {
                return(matchById);
            }

            if (Backstory.IsSolid(backstory))
            {
                Backstory matchBySolidName = backstories.FirstOrDefault(bs => bs.FirstName == backstory.FirstName && bs.LastName == backstory.LastName && bs.Slot == backstory.Slot);

                if (matchBySolidName != null)
                {
                    return(matchBySolidName);
                }
            }

            Levenshtein backstoryLevenshtein = new Levenshtein(backstory.Description);

            Backstory matchByDescription = backstories.MinValue(bs => backstoryLevenshtein.DistanceFrom(bs.Description), out int minDist);

            if (matchByDescription != null && minDist < 0.5f * backstory.Description.Length)
            {
                return(matchByDescription);
            }

            return(null);
        }
示例#5
0
        public static string GetIdentifier(Backstory backstory)
        {
            if (!string.IsNullOrEmpty(backstory.Id))
            {
                return(backstory.Id);
            }

            //Like in RimWorld.Backstory.PostLoad. Sensitive!
            string descriptionForHash = backstory.Description.TrimEnd().Replace("\\r", "").Replace("\\n", "\n");

            int    num = Math.Abs(VerseGenTextMock.StableStringHash(descriptionForHash) % 100);
            string s   = backstory.Title.Replace('-', ' ');

            s = VerseGenTextMock.CapitalizedNoSpaces(s);
            return(VerseGenTextMock.RemoveNonAlphanumeric(s) + num);
        }
示例#6
0
        private static void FillBackstoryMigrationMap(HashSet <Backstory> prevBackstories, IEnumerable <Backstory> backstories, Dictionary <string, string> oldToNewIds)
        {
            int count = backstories.Count();
            int i     = 0;

            foreach (Backstory backstory in backstories)
            {
                Console.Write($"\rMapping: {++i}/{count}");

                Backstory bestMatchPrevBackstory = FindBestMatch(prevBackstories, backstory);
                if (bestMatchPrevBackstory != null)
                {
                    oldToNewIds[bestMatchPrevBackstory.Id] = backstory.Id;
                    prevBackstories.Remove(bestMatchPrevBackstory);
                }
            }
        }
示例#7
0
        public static Backstory ReadBackstoryElementResource(XElement storyElem)
        {
            Backstory backstory = new Backstory()
            {
                Title            = storyElem.Element("title", true).Value,
                TitleFemale      = storyElem.Element("titleFemale", true)?.Value,
                TitleShort       = storyElem.Element("titleShort", true).Value,
                TitleShortFemale = storyElem.Element("titleShortFemale", true)?.Value,
                Description      = storyElem.Element("baseDesc", true).Value,
                Slot             = ParseSlot(storyElem)
            };

            backstory.Id = Backstory.GetIdentifier(backstory);

            // We don't want to spoil sensitive hash function, but want to make line endings consistent
            backstory.Description = backstory.Description.Replace("\r", "").Replace("\n", "\\n");

            return(backstory);
        }
示例#8
0
        private static IEnumerable <Backstory> LoadResourceBackstoriesRegular(string resourcesDirectory)
        {
            XmlSchemaSet backstoriesSchemaSet = new XmlSchemaSet();

            backstoriesSchemaSet.Add(XmlSchema.Read(new StringReader(Properties.Resources.BackstoriesSchema), ValidateSchema));

            foreach (string resourceFileName in Directory.EnumerateFiles(resourcesDirectory, "*.xml", SearchOption.TopDirectoryOnly))
            {
                XDocument doc = XDocument.Load(resourceFileName, LoadOptions.None);
                if (XmlHelper.IsValid(doc, backstoriesSchemaSet))
                {
                    string category = Path.GetFileNameWithoutExtension(resourceFileName);

                    foreach (XElement element in doc.Root.Elements())
                    {
                        Backstory backstory = XmlHelper.ReadBackstoryElementResource(element);
                        backstory.Category = category;
                        yield return(backstory);
                    }
                }
            }
        }
示例#9
0
        public static XElement BuildBackstoryElementSimple(Backstory backstory)
        {
            XElement backstoryElement = new XElement(backstory.Id);

            backstoryElement.Add(NewLine);
            backstoryElement.Add(Tab2, new XElement("title", backstory.Title), NewLine);

            if (!string.IsNullOrEmpty(backstory.TitleFemale))
            {
                backstoryElement.Add(Tab2, new XElement("titleFemale", backstory.TitleFemale), NewLine);
            }

            backstoryElement.Add(Tab2, new XElement("titleShort", backstory.TitleShort), NewLine);

            if (!string.IsNullOrEmpty(backstory.TitleShortFemale))
            {
                backstoryElement.Add(Tab2, new XElement("titleShortFemale", backstory.TitleShortFemale), NewLine);
            }

            backstoryElement.Add(Tab2, new XElement("desc", backstory.Description), NewLine, Tab);

            return(backstoryElement);
        }
示例#10
0
        static void Main(string[] args)
        {
            ParserResult <Options> parseResult = Parser.Default.ParseArguments <Options>(args);

            if (parseResult.Errors.Any())
            {
                return;
            }

            Options options = parseResult.Value;

            XDocument        translatedBackstoriesDoc = XDocument.Load(options.TranslatedBackstoriesFile, LoadOptions.None);
            List <Backstory> translatedBackstories    = translatedBackstoriesDoc.Root.Elements().Select(XmlHelper.ReadBackstoryElementTranslated).ToList();

            bool migrate = !string.IsNullOrEmpty(options.ResourcesDirectoryPrev);

            Dictionary <string, Backstory> regularBackstories = LoadResourceBackstoriesRegular(options.ResourcesDirectory).OrderBy(bs => bs.Id).ToDictionary(bs => bs.Id);
            Dictionary <string, Backstory> solidBackstories   = LoadResourceBackstoriesSolid(options.ResourcesDirectory).OrderBy(bs => bs.FirstName + bs.LastName).ThenBy(bs => (int)bs.Slot).ToDictionary(bs => bs.Id);

            Dictionary <string, string> oldToNewIdsRegular = null;
            Dictionary <string, string> oldToNewIdsSolid   = null;

            if (migrate)
            {
                HashSet <Backstory> backstoriesPrevRegular = new HashSet <Backstory>(LoadResourceBackstoriesRegular(options.ResourcesDirectoryPrev), new BackstoryEqualityComparer());
                HashSet <Backstory> backstoriesPrevSolid   = new HashSet <Backstory>(LoadResourceBackstoriesSolid(options.ResourcesDirectoryPrev), new BackstoryEqualityComparer());

                oldToNewIdsRegular = new Dictionary <string, string>();
                FillBackstoryMigrationMap(backstoriesPrevRegular, regularBackstories.Values, oldToNewIdsRegular);
                Console.WriteLine();
                oldToNewIdsSolid = new Dictionary <string, string>();
                FillBackstoryMigrationMap(backstoriesPrevSolid, solidBackstories.Values, oldToNewIdsSolid);
                Console.WriteLine();
            }

            List <Backstory> backstoriesTranslatedRegular = new List <Backstory>();
            List <Backstory> backstoriesTranslatedSolid   = new List <Backstory>();
            List <Backstory> backstoriesTranslatedUnused  = new List <Backstory>();

            foreach (Backstory translatedBackstory in translatedBackstories)
            {
                string resourceBackstoryIdRegular = translatedBackstory.Id;
                string resourceBackstoryIdSolid   = translatedBackstory.Id;

                if (migrate)
                {
                    oldToNewIdsRegular.TryGetValue(translatedBackstory.Id, out resourceBackstoryIdRegular);
                    oldToNewIdsSolid.TryGetValue(translatedBackstory.Id, out resourceBackstoryIdSolid);
                }

                if (resourceBackstoryIdRegular != null && regularBackstories.TryGetValue(resourceBackstoryIdRegular, out Backstory regularBackstory))
                {
                    translatedBackstory.Id = resourceBackstoryIdRegular;
                    backstoriesTranslatedRegular.Add(translatedBackstory);
                }
                else if (resourceBackstoryIdSolid != null && solidBackstories.TryGetValue(resourceBackstoryIdSolid, out Backstory solidBackstory))
                {
                    translatedBackstory.Id = resourceBackstoryIdSolid;
                    backstoriesTranslatedSolid.Add(translatedBackstory);
                }
                else
                {
                    backstoriesTranslatedUnused.Add(translatedBackstory);
                }
            }

            XDocument outputDoc = new XDocument();
            XElement  root      = new XElement("BackstoryTranslations");

            outputDoc.Add(root);
            outputDoc.Root.Add(XmlHelper.NewLine);

            foreach (Backstory bs in backstoriesTranslatedRegular)
            {
                Backstory regularBackstory = regularBackstories[bs.Id];
                AddBackstoryElementWithHint(outputDoc, GetHint(regularBackstory), XmlHelper.BuildBackstoryElementTranslatedWithEnglishComments(regularBackstory, bs));
                regularBackstories.Remove(bs.Id);
            }

            foreach (Backstory bs in regularBackstories.Values)
            {
                AddBackstoryElementWithHint(outputDoc, GetHint(bs), XmlHelper.BuildBackstoryElementTodoWithEnglishComments(bs));
            }

            foreach (Backstory bs in backstoriesTranslatedSolid)
            {
                Backstory solidBackstory = solidBackstories[bs.Id];
                AddBackstoryElementWithHint(outputDoc, GetHint(solidBackstory), XmlHelper.BuildBackstoryElementTranslatedWithEnglishComments(solidBackstory, bs));
                solidBackstories.Remove(bs.Id);
            }

            foreach (Backstory bs in solidBackstories.Values)
            {
                AddBackstoryElementWithHint(outputDoc, GetHint(bs), XmlHelper.BuildBackstoryElementTodoWithEnglishComments(bs));
            }

            outputDoc.Root.Add(XmlHelper.NewLine);
            outputDoc.Root.Add(XmlHelper.Tab, new XComment($" UNUSED "), XmlHelper.NewLine);

            foreach (Backstory bs in backstoriesTranslatedUnused)
            {
                AddBackstoryElementWithHint(outputDoc, GetHint(bs), XmlHelper.BuildBackstoryElementSimple(bs));
            }

            outputDoc.Root.Add(XmlHelper.NewLine);
            outputDoc.Save(options.OutputBackstories, SaveOptions.None);
        }
示例#11
0
 public static bool IsSolid(Backstory backstory)
 {
     return(!string.IsNullOrEmpty(backstory.FirstName));
 }