示例#1
0
        private SaveDocument CloneAreaInSection(string sectionName,
                                                SaveDocument saveDocument)
        {
            var section = saveDocument.Sections.Single
                              (s => s.Name == sectionName);

            var offsetX = m_CloneToX - m_CloneFromX;
            var offsetY = m_CloneToY - m_CloneFromY;

            var newInnerSections = section.InnerSections
                                   .Select(GetContentsWithCoordinates)
                                   .Where(r => IsInArea(r, m_CloneFromX, m_CloneFromY))
                                   .Select(r => new SaveSection($"{r.X + offsetX} {r.Y + offsetY}",
                                                                r.InnerSections,
                                                                WithoutExcluded(r.InnerPairs)));

            var allInnerSections = section.InnerSections.Concat(newInnerSections)
                                   .ToArray();
            var newSection = new SaveSection
                                 (sectionName,
                                 allInnerSections,
                                 WithSizeCorrected(section.InnerPairs, allInnerSections.Length));

            return(RepaceSection(newSection, saveDocument));
        }
        private static bool IsObjectContainingType(SaveSection section,
                                                   string objectType)
        {
            var contentsPair = section.InnerPairs.SingleOrDefault
                                   (p => p.Key == "Contents");

            return(contentsPair?.Value == objectType);
        }
示例#3
0
        private static void PersistSection(SaveSection section,
                                           ICollection <string> persistedParts)
        {
            persistedParts.Add($"BEGIN {Quoted(section.Name)}");

            PersistPairs(section.InnerPairs, persistedParts);

            PersistSections(section.InnerSections, persistedParts);

            persistedParts.Add("END");
        }
示例#4
0
        private static SaveDocument RepaceSection(SaveSection newSection,
                                                  SaveDocument saveDocument)
        {
            var sections = saveDocument.Sections.ToArray();

            var sectionIndex = sections.Select((s, i) => new { s, Index = i })
                               .Single(r => r.s.Name == newSection.Name)
                               .Index;

            sections[sectionIndex] = newSection;

            return(new SaveDocument(saveDocument.OuterPairs, sections));
        }
示例#5
0
        GetObjectSectionWithCoordinates(SaveSection section)
        {
            Func <string, double> parseIntPair = key =>
            {
                return(double.Parse
                           (section.InnerPairs.Single(p => p.Key == key).Value));
            };

            return(new ObjectSectionWithCoordinates
            {
                Section = section,
                X = parseIntPair("Pos.x"),
                Y = parseIntPair("Pos.y")
            });
        }
示例#6
0
        private SaveDocument ReplaceAreaWithCloneInSection
            (string sectionName, SaveDocument saveDocument)
        {
            // This ended-up being a mess because I encountered parts of the
            // save file I hadn't spotted until the end, but I didn't feel
            // like refectoring, hence the work-around for other inner sections

            var section = saveDocument.Sections.Single
                              (s => s.Name == sectionName);

            var regex = new Regex("\\d+ \\d+");

            var groupedInnerSections = section.InnerSections
                                       .GroupBy(s => regex.IsMatch(s.Name), s => s)
                                       .ToDictionary(g => g.Key, g => g.ToArray());

            if (!groupedInnerSections.ContainsKey(true))
            {
                groupedInnerSections[true] = new SaveSection[] {};
            }

            if (!groupedInnerSections.ContainsKey(false))
            {
                groupedInnerSections[false] = new SaveSection[] {};
            }

            section = new SaveSection
                          (sectionName, groupedInnerSections[true], section.InnerPairs);

            saveDocument = RepaceSection(section, saveDocument);

            saveDocument = RemoveAreaInSection(sectionName, saveDocument);

            saveDocument = CloneAreaInSection(sectionName, saveDocument);

            section = saveDocument.Sections.Single(s => s.Name == sectionName);

            section = new SaveSection
                          (sectionName,
                          section.InnerSections.Concat(groupedInnerSections[false]),
                          section.InnerPairs);

            return(RepaceSection(section, saveDocument));
        }
示例#7
0
        private static SectionContentsWithCoordinates GetContentsWithCoordinates
            (SaveSection section)
        {
            var nameParts = section.Name.Split(' ');

            if (nameParts.Length != 2)
            {
                throw new Exception
                          ("More than one cell section name part");
            }

            return(new SectionContentsWithCoordinates
            {
                InnerPairs = section.InnerPairs,
                InnerSections = section.InnerSections,
                X = int.Parse(nameParts[0]),
                Y = int.Parse(nameParts[1])
            });
        }
示例#8
0
        private SaveDocument RemoveObjectsInCloneToArea
            (SaveDocument saveDocument)
        {
            var section = saveDocument.Sections.Single
                              (s => s.Name == "Objects");

            var newInnerSections = section.InnerSections
                                   .Select(GetObjectSectionWithCoordinates)
                                   .Where(r => !IsInArea(r, m_CloneToX, m_CloneToY))
                                   .Select(r => r.Section)
                                   .ToArray();

            var newSection = new SaveSection
                                 ("Objects",
                                 newInnerSections,
                                 WithSizeCorrected(section.InnerPairs, newInnerSections.Length));

            return(RepaceSection(newSection, saveDocument));
        }
示例#9
0
        private SaveDocument RemoveAreaInSection(string sectionName,
                                                 SaveDocument saveDocument)
        {
            var section = saveDocument.Sections.Single
                              (s => s.Name == sectionName);

            var newInnerSections = section.InnerSections
                                   .Select(GetContentsWithCoordinates)
                                   .Where(r => !IsInArea(r, m_CloneToX, m_CloneToY))
                                   .Select(r => new SaveSection($"{r.X} {r.Y}",
                                                                r.InnerSections,
                                                                r.InnerPairs))
                                   .ToArray();

            var newSection = new SaveSection
                                 (sectionName,
                                 newInnerSections,
                                 WithSizeCorrected(section.InnerPairs, newInnerSections.Length));

            return(RepaceSection(newSection, saveDocument));
        }
        public SaveDocument Mutate(SaveDocument saveDocument)
        {
            if (saveDocument == null)
            {
                throw new ArgumentNullException(nameof(saveDocument));
            }

            var section = saveDocument.Sections.Single
                              (s => s.Name == "Objects");

            var newInnerSections = section.InnerSections
                                   .Where(s => !IsObjectOfType(s, m_ObjectType) &&
                                          !IsObjectContainingType(s, m_ObjectType))
                                   .ToArray();

            var newSection = new SaveSection
                                 ("Objects",
                                 newInnerSections,
                                 WithSizeCorrected(section.InnerPairs, newInnerSections.Length));

            return(RepaceSection(newSection, saveDocument));
        }
示例#11
0
        private SaveDocument CloneObjects(SaveDocument saveDocument,
                                          int nextObjectId)
        {
            var nextUniqueId = long.Parse
                                   (saveDocument.OuterPairs.Single(p => p.Key == "ObjectId.next")
                                   .Value);

            var section = saveDocument.Sections.Single
                              (s => s.Name == "Objects");

            var offsetX = m_CloneToX - m_CloneFromX;
            var offsetY = m_CloneToY - m_CloneFromY;

            var newInnerSections = section.InnerSections
                                   .Select(GetObjectSectionWithCoordinates)
                                   .Where(r => IsInArea(r, m_CloneFromX, m_CloneFromY))
                                   .Where(r => !m_ExcludedObjectTypes.Contains(r.Section.InnerPairs.Single(p => p.Key == "Type").Value))
                                   .Select(r =>
            {
                var objectId = nextObjectId++;

                var newInnerPairs = r.Section.InnerPairs;

                // ReSharper disable once AccessToModifiedClosure
                Func <string, double> getPairValue = key =>
                                                     double.Parse(newInnerPairs.Single(p => p.Key == key).Value);

                // ReSharper disable SpecifyACultureInStringConversionExplicitly
                newInnerPairs = ReplacePairValue
                                    (newInnerPairs,
                                    "Pos.x",
                                    (getPairValue("Pos.x") + offsetX).ToString());

                newInnerPairs = ReplacePairValue
                                    (newInnerPairs,
                                    "Pos.y",
                                    (getPairValue("Pos.y") + offsetY).ToString());
                // ReSharper restore SpecifyACultureInStringConversionExplicitly

                newInnerPairs = ReplacePairValue
                                    (newInnerPairs, "Id.i", objectId.ToString());

                newInnerPairs = ReplacePairValue
                                    (newInnerPairs, "Id.u", nextUniqueId++.ToString());

                return(new SaveSection
                           ($"[i {objectId}]",
                           r.Section.InnerSections.Where(s => s.Name != "Connections"),
                           WithoutExcluded(newInnerPairs)));
            })
                                   .ToArray();

            saveDocument = RemoveObjectsInCloneToArea(saveDocument);

            section = saveDocument.Sections.Single(s => s.Name == "Objects");

            var newSection = new SaveSection
                                 ("Objects",
                                 section.InnerSections.Concat(newInnerSections).ToArray(),
                                 WithSizeCorrected(section.InnerPairs, nextObjectId));

            saveDocument = RepaceSection(newSection, saveDocument);

            var outerPairs = ReplacePairValue(saveDocument.OuterPairs,
                                              "ObjectId.next",
                                              nextUniqueId.ToString());

            return(new SaveDocument(outerPairs, saveDocument.Sections));
        }
 private static bool IsObjectOfType(SaveSection section,
                                    string objectType)
 {
     return(section.InnerPairs.Single(p => p.Key == "Type").Value
            == objectType);
 }
示例#13
0
 void AddSection(SaveSection section)
 {
     sections.Add(section.identifier, section);
     numSections++;
 }
示例#14
0
 public int GetSectionOffset(SaveSection section)
 {
     return(_saveSectionTable[(int)section]);
 }