예제 #1
0
        private IEnumerable <DefinitionUpdater> MatchVariant2Rows(DefinitionUpdater[] defUpdaters, ColumnComparer[] comparers)
        {
            Variant2.SubRow[] prevRows    = _PreviousSheet.Cast <Variant2.RelationalDataRow>().SelectMany(r => r.SubRows).ToArray();
            Variant2.SubRow[] updatedRows = _UpdatedSheet.Cast <Variant2.RelationalDataRow>().SelectMany(r => r.SubRows).ToArray();
            Dictionary <string, Variant2.SubRow> updatedRowIndex = updatedRows.ToDictionary(r => r.FullKey);

            foreach (Variant2.SubRow prevRow in prevRows)
            {
                if (!updatedRowIndex.TryGetValue(prevRow.FullKey, out Variant2.SubRow updatedRow))
                {
                    continue;
                }

                object[] prevRowFields =
                    _PreviousSheet.Header.Columns.OrderBy(_ => _.Index).Select(_ => prevRow[_.Index]).ToArray();
                object[] updatedRowFields =
                    _UpdatedSheet.Header.Columns.OrderBy(_ => _.Index).Select(_ => updatedRow[_.Index]).ToArray();

                foreach (DefinitionUpdater def in defUpdaters)
                {
                    def.MatchRow(prevRowFields, updatedRowFields, comparers);
                }
            }

            return(defUpdaters);
        }
예제 #2
0
        static void SaveAsCsv(IRelationalSheet sheet, string path)
        {
            using (var s = new StreamWriter(path, false, Encoding.UTF8)) {
                var indexLine = new StringBuilder("key");
                var nameLine  = new StringBuilder("#");
                var typeLine  = new StringBuilder("int32");

                var colIndices = new List <int>();
                foreach (var col in sheet.Header.Columns)
                {
                    indexLine.AppendFormat(",{0}", col.Index);
                    nameLine.AppendFormat(",{0}", col.Name);
                    typeLine.AppendFormat(",{0}", col.ValueType);

                    colIndices.Add(col.Index);
                }

                s.WriteLine(indexLine);
                s.WriteLine(nameLine);
                s.WriteLine(typeLine);

                foreach (var row in sheet.Cast <SaintCoinach.Ex.IRow>().OrderBy(_ => _.Key))
                {
                    s.Write(row.Key);
                    foreach (var col in colIndices)
                    {
                        var v = row[col];

                        if (v is SaintCoinach.Xiv.XivRow xivRow && Controls.ColumnFactory.ForceRaw)
                        {
                            v = xivRow.Key;
                        }

                        if (v == null)
                        {
                            s.Write(",");
                        }
                        else if (v is IDictionary <int, object> )
                        {
                            WriteDict(s, v as IDictionary <int, object>);
                        }
                        else if (IsUnescaped(v))
                        {
                            s.Write(",{0}", v);
                        }
                        else
                        {
                            s.Write(",\"{0}\"", v.ToString().Replace("\"", "\"\""));
                        }
                    }
                    s.WriteLine();
                }
            }
        }
예제 #3
0
        static void SaveAsCsv(IRelationalSheet sheet, string path)
        {
            using (var s = new StreamWriter(path, false, Encoding.UTF8)) {
                var indexLine = new StringBuilder("key");
                var nameLine = new StringBuilder("#");
                var typeLine = new StringBuilder("int32");

                var colIndices = new List<int>();
                foreach (var col in sheet.Header.Columns) {
                    indexLine.AppendFormat(",{0}", col.Index);
                    nameLine.AppendFormat(",{0}", col.Name);
                    typeLine.AppendFormat(",{0}", col.ValueType);

                    colIndices.Add(col.Index);
                }

                s.WriteLine(indexLine);
                s.WriteLine(nameLine);
                s.WriteLine(typeLine);

                foreach (var row in sheet.Cast<SaintCoinach.Ex.IRow>().OrderBy(_ => _.Key)) {
                    s.Write(row.Key);
                    foreach (var col in colIndices) {
                        var v = row[col];

                        if (v == null)
                            s.Write(",");
                        else if (v is IDictionary<int, object>)
                            WriteDict(s, v as IDictionary<int, object>);
                        else if (IsUnescaped(v))
                            s.Write(",{0}", v);
                        else
                            s.Write(",\"{0}\"", v.ToString().Replace("\"", "\"\""));
                    }
                    s.WriteLine();
                }
            }
        }
예제 #4
0
        public IEnumerable <IChange> Compare()
        {
            var changes = new List <IChange>();


            var prevKeys    = _PreviousSheet.Cast <IRow>().Select(_ => _.Key).ToArray();
            var updatedKeys = _UpdatedSheet.Cast <IRow>().Select(_ => _.Key).ToArray();

            changes.AddRange(updatedKeys.Except(prevKeys).Select(_ => new RowAdded(_UpdatedDefinition.Name, _)));
            changes.AddRange(prevKeys.Except(updatedKeys).Select(_ => new RowRemoved(_PreviousDefinition.Name, _)));

            var columns = _UpdatedDefinition.GetAllColumnNames().Select(_ => {
                var previousColumn = _PreviousDefinition.FindColumn(_);
                var newColumn      = _UpdatedDefinition.FindColumn(_);

                if (!previousColumn.HasValue || !newColumn.HasValue)
                {
                    throw new InvalidDataException();
                }

                return(new ColumnMap {
                    Name = _,
                    PreviousIndex = previousColumn.Value,
                    NewIndex = newColumn.Value
                });
            }).ToArray();

            var prevIsMulti = _PreviousSheet is IMultiSheet;
            var upIsMulti   = _UpdatedSheet is IMultiSheet;

            if (prevIsMulti == upIsMulti)
            {
                if (prevIsMulti)
                {
                    var prevMulti = (IMultiSheet)_PreviousSheet;
                    var prevLang  = _PreviousSheet.Header.AvailableLanguages;

                    var upMulti = (IMultiSheet)_UpdatedSheet;
                    var upLang  = _UpdatedSheet.Header.AvailableLanguages;

                    changes.AddRange(upLang.Except(prevLang).Select(_ => new SheetLanguageAdded(_UpdatedSheet.Name, _)));
                    changes.AddRange(
                        prevLang.Except(upLang)
                        .Select(_ => new SheetLanguageRemoved(_PreviousDefinition.Name, _)));

                    foreach (var lang in prevLang.Intersect(upLang))
                    {
                        var prevSheet = prevMulti.GetLocalisedSheet(lang);
                        var upSheet   = upMulti.GetLocalisedSheet(lang);

                        changes.AddRange(Compare(prevSheet, upSheet, lang, columns));
                    }
                }
                else
                {
                    changes.AddRange(Compare(_PreviousSheet, _UpdatedSheet, Language.None, columns));
                }
            }
            else
            {
                changes.Add(new SheetTypeChanged(_UpdatedDefinition.Name));
                System.Console.Error.WriteLine("Type of sheet {0} has changed, unable to detect changes.",
                                               _UpdatedDefinition.Name);
            }

            return(changes);
        }
        public IEnumerable <IChange> Compare()
        {
            List <IChange> changes = new List <IChange>();


            int[] prevKeys    = _PreviousSheet.Cast <IRow>().Select(_ => _.Key).ToArray();
            int[] updatedKeys = _UpdatedSheet.Cast <IRow>().Select(_ => _.Key).ToArray();

            changes.AddRange(updatedKeys.Except(prevKeys).Select(_ => new RowAdded(_UpdatedDefinition.Name, _)));
            changes.AddRange(prevKeys.Except(updatedKeys).Select(_ => new RowRemoved(_PreviousDefinition.Name, _)));

            ColumnMap[] columns = _UpdatedDefinition.GetAllColumnNames().Select(_ => {
                int?previousColumn = _PreviousDefinition.FindColumn(_);
                int?newColumn      = _UpdatedDefinition.FindColumn(_);

                if (!previousColumn.HasValue || !newColumn.HasValue)
                {
                    throw new InvalidDataException();
                }

                return(new ColumnMap {
                    Name = _,
                    PreviousIndex = previousColumn.Value,
                    NewIndex = newColumn.Value
                });
            }).ToArray();

            bool prevIsMulti = _PreviousSheet is IMultiSheet;
            bool upIsMulti   = _UpdatedSheet is IMultiSheet;

            if (prevIsMulti == upIsMulti)
            {
                if (prevIsMulti)
                {
                    IMultiSheet            prevMulti = (IMultiSheet)_PreviousSheet;
                    IEnumerable <Language> prevLang  = _PreviousSheet.Header.AvailableLanguages;

                    IMultiSheet            upMulti = (IMultiSheet)_UpdatedSheet;
                    IEnumerable <Language> upLang  = _UpdatedSheet.Header.AvailableLanguages;

                    changes.AddRange(upLang.Except(prevLang).Select(_ => new SheetLanguageAdded(_UpdatedSheet.Name, _)));
                    changes.AddRange(
                        prevLang.Except(upLang)
                        .Select(_ => new SheetLanguageRemoved(_PreviousDefinition.Name, _)));

                    foreach (Language lang in prevLang.Intersect(upLang))
                    {
                        // Do not compare languages marked unavailable elsewhere.
                        if (_unavailableLanguages.ContainsKey(lang))
                        {
                            continue;
                        }

                        ISheet prevSheet = prevMulti.GetLocalisedSheet(lang);
                        ISheet upSheet   = upMulti.GetLocalisedSheet(lang);

                        try {
                            changes.AddRange(Compare(prevSheet, upSheet, lang, columns));
                        } catch (System.IO.FileNotFoundException) {
                            // Usually caused by one language ahead of another
                            // in patches, or that language data is not found.
                            // Skip it and mark unavailable for other comparisons.
                            _unavailableLanguages.TryAdd(lang, true);
                            continue;
                        }
                    }
                }
                else
                {
                    changes.AddRange(Compare(_PreviousSheet, _UpdatedSheet, Language.None, columns));
                }
            }
            else
            {
                changes.Add(new SheetTypeChanged(_UpdatedDefinition.Name));
                System.Console.Error.WriteLine("Type of sheet {0} has changed, unable to detect changes.",
                                               _UpdatedDefinition.Name);
            }

            return(changes);
        }
예제 #6
0
        private static void SaveAsJson(IRelationalSheet sheet, string path)
        {
            using (var s = new StreamWriter(path, false, Encoding.UTF8)) {
                s.WriteLine("[");

                var colIndices = new List <int>();
                var colNames   = new List <String>();
                foreach (var col in sheet.Header.Columns)
                {
                    colIndices.Add(col.Index);
                    colNames.Add(col.Name);
                }

                bool isFirst = true;
                foreach (var row in sheet.Cast <SaintCoinach.Ex.IRow>().OrderBy(_ => _.Key))
                {
                    if (isFirst)
                    {
                        isFirst = false;
                        s.Write("\t");
                    }
                    else
                    {
                        s.Write(",");
                    }
                    s.WriteLine("{");
                    string sKey = row.Key.ToString();
                    foreach (var col in colIndices)
                    {
                        var    content  = row[col];
                        string sContent = "";
                        if (content == null)
                        {
                            sContent = "";
                        }
                        else if (content is IDictionary <int, object> )
                        {
                            var dContent = content;
                            foreach (var kvp in dContent as IDictionary <int, object> )
                            {
                                sKey += "[" + kvp.Key + "]";
                                if (kvp.Value != null)
                                {
                                    string result = kvp.Value.ToString().Replace("\"", "\"\"");
                                    result    = Regex.Replace(result, @"\t|\n|\r", " ");
                                    sContent += result;
                                }
                            }
                        }
                        else if (IsUnescaped(content))
                        {
                            sContent = content.ToString();
                        }
                        else
                        {
                            string result = content.ToString().Replace("\"", "\"\"");
                            result   = Regex.Replace(result, @"\t|\n|\r", " ");
                            sContent = result;
                        }
                        s.WriteLine("\t\t\"{0}\":\"{1}\",", colNames[col], sContent);
                    }
                    s.WriteLine("\t\t\"{0}\":\"{1}\"", "Key", sKey);
                    s.Write("\t}");
                }
                s.WriteLine();
                s.WriteLine("]");
            }
        }