Пример #1
0
        private void SaveToDynamicXml(string path)
        {
            // To reduce size of XML files
            const int maxEntries = 10000;

            var tlkFile = new TlkFile
            {
                Id     = GetId(Path.GetFileNameWithoutExtension(_path)),
                Name   = Path.GetFileNameWithoutExtension(_path),
                Source = Path.GetFileName(_path)
            };

            dynamic tlkXml = new DynamicXml();

            var maleIncludeCount   = MaleStringRefs.Count / maxEntries;
            var femaleIncludeCount = FemaleStringRefs.Count / maxEntries;
            var includeCount       = Math.Max(maleIncludeCount, femaleIncludeCount);

            if ((MaleStringRefs.Count % maxEntries > 0) ||
                (FemaleStringRefs.Count % maxEntries > 0))
            {
                includeCount++;
            }

            MaleStringRefs.Sort((s1, s2) => (s1.Id & Int32.MaxValue).CompareTo(s2.Id & Int32.MaxValue));
            FemaleStringRefs.Sort((s1, s2) => (s1.Id & Int32.MaxValue).CompareTo(s2.Id & Int32.MaxValue));

            /*for (var i = 0; i < MaleStringRefs.Count; i++)
             * {
             *      var s = MaleStringRefs[i];
             *      s.Position = i;
             *
             *      tlkFile.MaleStrings.Add(s);
             * }*/

            /*for (var i = 0; i < FemaleStringRefs.Count; i++)
             * {
             *      var s = FemaleStringRefs[i];
             *      s.Position = i;
             *
             *      tlkFile.FemaleStrings.Add(s);
             * }*/

            tlkXml.TlkFile(DynamicXml.CreateElement(tlkfile =>
            {
                tlkfile["id"]     = tlkFile.Id;
                tlkfile["name"]   = tlkFile.Name;
                tlkfile["source"] = tlkFile.Source;

                tlkfile.Includes(DynamicXml.CreateElement(includes =>
                {
                    for (var i = 0; i < includeCount; i++)
                    {
                        var item = string.Format("{0}/{0}{1}.xml", tlkFile.Name, i);

                        includes.Include(DynamicXml.CreateElement(include =>
                        {
                            include["source"] = item;
                        }));

                        tlkFile.Includes.Add(item);
                    }
                }));
            }));

            tlkXml.Save(path);

            var index = 0;

            foreach (var include in tlkFile.Includes)
            {
                dynamic includeXml = new DynamicXml();

                var include1 = include;

                includeXml.TlkFile(DynamicXml.CreateElement(tlkfile =>
                {
                    tlkfile["name"] = include1;

                    tlkfile.MaleStrings(DynamicXml.CreateElement(strings =>
                    {
                        var count = MaleStringRefs.Count;

                        for (var x = 0; x < maxEntries && index < count; x++, index++)
                        {
                            var s = MaleStringRefs[index];

                            strings.String(DynamicXml.CreateElement(str =>
                            {
                                str["id"] = s.Id;
                                str.Value = s.Value;
                            }));
                        }
                    }));

                    tlkfile.FemaleStrings(DynamicXml.CreateElement(strings =>
                    {
                        var count = FemaleStringRefs.Count;

                        for (var x = 0; x < maxEntries && index < count; x++, index++)
                        {
                            var s = FemaleStringRefs[index];

                            strings.String(DynamicXml.CreateElement(str =>
                            {
                                str["id"] = s.Id;
                                str.Value = s.Value;
                            }));
                        }
                    }));
                }));

                var destPath = Path.GetDirectoryName(path);

                if (destPath != null)
                {
                    destPath = Path.Combine(destPath, include1);

                    if (!Directory.Exists(Path.GetDirectoryName(destPath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(destPath));
                    }

                    includeXml.Save(destPath);
                }
            }
        }