/// <summary>
        /// 拡張編集オブジェクトファイルのセクションコレクションから
        /// 拡張編集オブジェクトを作成する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <returns>拡張編集オブジェクト。</returns>
        public static ExEditObject FromExoFileSections(
            IniFileSectionCollection sections)
        {
            if (sections == null)
            {
                throw new ArgumentNullException(nameof(sections));
            }

            var result = new ExEditObject();

            // [exedit] セクションのプロパティ値設定
            FromSectionsToFileProperties(sections, result);

            // レイヤーアイテムのベースセクション群([0] ~ [N])取得
            var itemBaseSections = GetLayerItemBaseSections(sections);

            // レイヤーアイテム群作成
            var items =
                itemBaseSections
                .AsParallel()
                .AsOrdered()
                .Select(s => FromSectionsToLayerItem(sections, s));

            // レイヤーアイテム群設定
            foreach (var item in items)
            {
                result.LayerItems.Add(item);
            }

            return(result);
        }
        /// <summary>
        /// セクションコレクションからレイヤーアイテムを作成する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <param name="baseSection">ベースセクション。</param>
        /// <returns>レイヤーアイテム。</returns>
        private static LayerItem FromSectionsToLayerItem(
            IniFileSectionCollection sections,
            IniFileSection baseSection)
        {
            Debug.Assert(sections != null);
            Debug.Assert(baseSection != null);

            var result = new LayerItem();

            // ベースセクションのプロパティ値設定
            ExoFileItemsConverter.ToProperties(baseSection.Items, ref result);

            // コンポーネント群追加
            var componentSections =
                Enumerable
                .Range(0, int.MaxValue)
                .Select(
                    i =>
                    sections.FirstOrDefault(
                        s => s.Name == baseSection.Name + @"." + i))
                .TakeWhile(s => s != null);

            foreach (var cs in componentSections)
            {
                result.Components.Add(ComponentMaker.FromExoFileItems(cs.Items));
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// 拡張編集オブジェクトファイルのセクションコレクションから
        /// 拡張編集オブジェクトを作成する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <returns>拡張編集オブジェクト。</returns>
        public static ExEditObject FromExoFileSections(
            IniFileSectionCollection sections)
        {
            if (sections == null)
            {
                throw new ArgumentNullException(nameof(sections));
            }

            var result = new ExEditObject();

            // [exedit] セクションのプロパティ値設定
            FromSectionsToFileProperties(sections, result);

            // レイヤーアイテムのベースセクション群([0] ~ [N])取得
            var itemBaseSections = GetLayerItemBaseSections(sections);

            // レイヤーアイテム群作成
            var items =
                itemBaseSections
                    .AsParallel()
                    .AsOrdered()
                    .Select(s => FromSectionsToLayerItem(sections, s));

            // レイヤーアイテム群設定
            foreach (var item in items)
            {
                result.LayerItems.Add(item);
            }

            return result;
        }
        /// <summary>
        /// INIファイル形式文字列値を IniFileSectionCollection オブジェクトへパースする。
        /// </summary>
        /// <param name="iniString">INIファイル形式文字列値。</param>
        /// <param name="strict">厳格な形式チェックを行うならば true 。</param>
        /// <returns>IniFileSectionCollection オブジェクト。</returns>
        public static IniFileSectionCollection FromString(
            string iniString,
            bool strict = false)
        {
            if (iniString == null)
            {
                throw new ArgumentNullException(nameof(iniString));
            }

            var ini = new IniFileSectionCollection();

            foreach (var line in ReadLines(iniString))
            {
                var trimmedLine = line.Trim();

                if (trimmedLine.Length == 0 || trimmedLine[0] == ';')
                {
                    continue;
                }

                if (trimmedLine[0] == '[')
                {
                    if (trimmedLine[trimmedLine.Length - 1] == ']')
                    {
                        AddSection(ini, trimmedLine.Substring(1, trimmedLine.Length - 2));
                    }
                }

                var eqIndex = line.IndexOf('=');
                if (eqIndex < 0)
                {
                    if (strict)
                    {
                        throw new FormatException(@"Invalid line.");
                    }
                    continue;
                }

                var name = line.Substring(0, eqIndex).Trim();
                var value = line.Substring(eqIndex + 1).TrimStart();

                if (ini.Count <= 0)
                {
                    if (strict)
                    {
                        throw new FormatException(
                            "The item (\"" + name + "\") is found before a section.");
                    }
                    continue;
                }

                AddItemToLastSection(ini, name, value);
            }

            return ini;
        }
        /// <summary>
        /// レイヤーアイテムのベースセクション配列を取得する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <returns>ベースセクション配列。</returns>
        private static IniFileSection[] GetLayerItemBaseSections(
            IniFileSectionCollection sections)
        {
            Debug.Assert(sections != null);

            return
                (Enumerable
                 .Range(0, int.MaxValue)
                 .Select(i => sections.FirstOrDefault(s => s.Name == i.ToString()))
                 .TakeWhile(s => s != null)
                 .ToArray());
        }
        /// <summary>
        /// レイヤーアイテムからセクション群を作成して追加する。
        /// </summary>
        /// <param name="layerItem">レイヤーアイテム。</param>
        /// <param name="index">レイヤーアイテムインデックス。</param>
        /// <param name="target">追加先のセクションコレクション。</param>
        private static void FromLayerItemToSections(
            LayerItem layerItem,
            int index,
            IniFileSectionCollection target)
        {
            Debug.Assert(layerItem != null);
            Debug.Assert(index >= 0);
            Debug.Assert(target != null);

            // ベースセクションアイテム群取得
            var items = ExoFileItemsConverter.ToItems(layerItem);

            // ベースセクションアイテム群を整理
            if (layerItem.GroupId <= 0)
            {
                items.Remove(LayerItem.ExoFileItemNameOfGroupId);
            }
            if (layerItem.IsAudio)
            {
                items.Remove(LayerItem.ExoFileItemNameOfIsClipping);
                items.Remove(LayerItem.ExoFileItemNameOfIsCameraTarget);
            }
            else
            {
                items.Remove(LayerItem.ExoFileItemNameOfIsAudio);
                if (!layerItem.IsClipping)
                {
                    items.Remove(LayerItem.ExoFileItemNameOfIsClipping);
                }
            }
            if (layerItem.ChainIndex < 0)
            {
                items.Remove(LayerItem.ExoFileItemNameOfChainIndex);
            }

            // ベースセクション追加
            var baseName = index.ToString();

            target.Add(baseName, items);

            // コンポーネントセクション群追加
            baseName += '.';
            foreach (var v in layerItem.Components.Select((c, i) => new { c, i }))
            {
                target.Add(baseName + v.i, v.c.ToExoFileItems());
            }
        }
        /// <summary>
        /// このオブジェクトを拡張編集オブジェクトファイルのセクション形式に変換する。
        /// </summary>
        /// <returns>セクションコレクションデータ。</returns>
        public IniFileSectionCollection ToExoFileSections()
        {
            // ファイルセクション追加しつつ作成
            var sections =
                new IniFileSectionCollection
            {
                { SectionNameOfFile, ExoFileItemsConverter.ToItems(this) },
            };

            // レイヤーアイテムセクション群追加
            foreach (var v in this.LayerItems.Select((item, i) => new { item, i }))
            {
                FromLayerItemToSections(v.item, v.i, sections);
            }

            return(sections);
        }
        /// <summary>
        /// セクションコレクションからファイルセクションを取得、変換し、
        /// 拡張編集オブジェクトのプロパティ値を設定する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <param name="target">設定先の拡張編集オブジェクト。</param>
        private static void FromSectionsToFileProperties(
            IniFileSectionCollection sections,
            ExEditObject target)
        {
            Debug.Assert(sections != null);
            Debug.Assert(target != null);

            // ファイルセクション取得
            var section = sections.FirstOrDefault(s => s.Name == SectionNameOfFile);

            if (section == null)
            {
                throw new FormatException(
                          @"The [" + SectionNameOfFile + @"] section is not found.");
            }

            // プロパティ値設定
            ExoFileItemsConverter.ToProperties(section.Items, ref target);
        }
Exemple #9
0
        /// <summary>
        /// このオブジェクトを拡張編集オブジェクトファイルのセクション形式に変換する。
        /// </summary>
        /// <returns>セクションコレクションデータ。</returns>
        public IniFileSectionCollection ToExoFileSections()
        {
            var sections = new IniFileSectionCollection();

            // ファイルセクション追加
            sections.Add(
                SectionNameOfFile,
                ExoFileItemsConverter.ToItems(this));

            // レイヤーアイテムセクション群追加
            foreach (var v in this.LayerItems.Select((item, i) => new { item, i }))
            {
                FromLayerItemToSections(v.item, v.i, sections);
            }

            return sections;
        }
Exemple #10
0
        /// <summary>
        /// レイヤーアイテムからセクション群を作成して追加する。
        /// </summary>
        /// <param name="layerItem">レイヤーアイテム。</param>
        /// <param name="index">レイヤーアイテムインデックス。</param>
        /// <param name="target">追加先のセクションコレクション。</param>
        private static void FromLayerItemToSections(
            LayerItem layerItem,
            int index,
            IniFileSectionCollection target)
        {
            Debug.Assert(layerItem != null);
            Debug.Assert(index >= 0);
            Debug.Assert(target != null);

            // ベースセクションアイテム群取得
            var items = ExoFileItemsConverter.ToItems(layerItem);

            // ベースセクションアイテム群を整理
            if (layerItem.GroupId <= 0)
            {
                items.Remove(LayerItem.ExoFileItemNameOfGroupId);
            }
            if (layerItem.IsAudio)
            {
                items.Remove(LayerItem.ExoFileItemNameOfIsClipping);
                items.Remove(LayerItem.ExoFileItemNameOfIsCameraTarget);
            }
            else
            {
                items.Remove(LayerItem.ExoFileItemNameOfIsAudio);
                if (!layerItem.IsClipping)
                {
                    items.Remove(LayerItem.ExoFileItemNameOfIsClipping);
                }
            }
            if (layerItem.ChainIndex < 0)
            {
                items.Remove(LayerItem.ExoFileItemNameOfChainIndex);
            }

            // ベースセクション追加
            var baseName = index.ToString();
            target.Add(baseName, items);

            // コンポーネントセクション群追加
            baseName += '.';
            foreach (var v in layerItem.Components.Select((c, i) => new { c, i }))
            {
                target.Add(baseName + v.i, v.c.ToExoFileItems());
            }
        }
Exemple #11
0
        /// <summary>
        /// セクションコレクションからレイヤーアイテムを作成する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <param name="baseSection">ベースセクション。</param>
        /// <returns>レイヤーアイテム。</returns>
        private static LayerItem FromSectionsToLayerItem(
            IniFileSectionCollection sections,
            IniFileSection baseSection)
        {
            Debug.Assert(sections != null);
            Debug.Assert(baseSection != null);

            var result = new LayerItem();

            // ベースセクションのプロパティ値設定
            ExoFileItemsConverter.ToProperties(baseSection.Items, ref result);

            // コンポーネント群追加
            var componentSections =
                Enumerable
                    .Range(0, int.MaxValue)
                    .Select(
                        i =>
                            sections.FirstOrDefault(
                                s => s.Name == baseSection.Name + @"." + i))
                    .TakeWhile(s => s != null);
            foreach (var cs in componentSections)
            {
                result.Components.Add(ComponentMaker.FromExoFileItems(cs.Items));
            }

            return result;
        }
Exemple #12
0
        /// <summary>
        /// レイヤーアイテムのベースセクション配列を取得する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <returns>ベースセクション配列。</returns>
        private static IniFileSection[] GetLayerItemBaseSections(
            IniFileSectionCollection sections)
        {
            Debug.Assert(sections != null);

            return
                Enumerable
                    .Range(0, int.MaxValue)
                    .Select(i => sections.FirstOrDefault(s => s.Name == i.ToString()))
                    .TakeWhile(s => s != null)
                    .ToArray();
        }
Exemple #13
0
        /// <summary>
        /// セクションコレクションからファイルセクションを取得、変換し、
        /// 拡張編集オブジェクトのプロパティ値を設定する。
        /// </summary>
        /// <param name="sections">セクションコレクション。</param>
        /// <param name="target">設定先の拡張編集オブジェクト。</param>
        private static void FromSectionsToFileProperties(
            IniFileSectionCollection sections,
            ExEditObject target)
        {
            Debug.Assert(sections != null);
            Debug.Assert(target != null);

            // ファイルセクション取得
            var section = sections.FirstOrDefault(s => s.Name == SectionNameOfFile);
            if (section == null)
            {
                throw new FormatException(
                    @"The [" + SectionNameOfFile + @"] section is not found.");
            }

            // プロパティ値設定
            ExoFileItemsConverter.ToProperties(section.Items, ref target);
        }
 internal IniFileSection(IniFileSectionCollection parent, String name, IDictionary<String, String> properties) : this(parent, name)
 {
     this.Properties.Add(properties);
 }
 internal IniFileSection(IniFileSectionCollection parent, String name)
 {
     this.Name = ExceptionHelpers.ThrowIfNullOrEmpty(name, nameof(name));
     this.Properties = new Dictionary<String, String>();
 }
Exemple #16
0
        /// <summary>
        /// IniFileSectionCollection オブジェクトの末尾セクションへアイテムを追加する。
        /// </summary>
        /// <param name="dest">IniFileSectionCollection オブジェクト。</param>
        /// <param name="name">名前。</param>
        /// <param name="value">値。</param>
        private static void AddItemToLastSection(
            IniFileSectionCollection dest,
            string name,
            string value)
        {
            Debug.Assert(dest != null && dest.Count > 0);

            var section = dest[dest.Count - 1];
            IniFileItem item = null;

            try
            {
                item = new IniFileItem(name);
            }
            catch (Exception ex)
            {
                throw new FormatException(
                    "The name of the item (in the section \"" +
                    section.Name +
                    "\") is invalid.",
                    ex);
            }

            try
            {
                item.Value = value;
            }
            catch (Exception ex)
            {
                throw new FormatException(
                    "The value of the item (\"" +
                    name +
                    "\" in the section \"" +
                    section.Name +
                    "\") is invalid.",
                    ex);
            }

            try
            {
                dest[dest.Count - 1].Items.Add(item);
            }
            catch (Exception ex)
            {
                throw new FormatException(
                    "The item (\"" +
                    name +
                    "\" in the section \"" +
                    section.Name +
                    "\") is duplicated.",
                    ex);
            }
        }
Exemple #17
0
        /// <summary>
        /// IniFileSectionCollection オブジェクトへセクションを追加する。
        /// </summary>
        /// <param name="dest">IniFileSectionCollection オブジェクト。</param>
        /// <param name="sectionName">セクション名。</param>
        private static void AddSection(IniFileSectionCollection dest, string sectionName)
        {
            Debug.Assert(dest != null);

            IniFileSection section = null;

            try
            {
                section = new IniFileSection(sectionName);
            }
            catch (Exception ex)
            {
                throw new FormatException(@"Invalid section name.", ex);
            }

            try
            {
                dest.Add(section);
            }
            catch (Exception ex)
            {
                throw new FormatException(
                    "The section (\"" + sectionName + "\") is duplicated.",
                    ex);
            }
        }