Ejemplo n.º 1
0
        EditorINIData GetEditorData(INIBlocks iniData, int templateFileIndex)
        {
#if DEBUG
            System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
            st.Start();
#endif

            EditorINIData editorData = new EditorINIData(templateFileIndex);

            //loop each template block
            for (int i = 0; i < Helper.Template.Data.Files[templateFileIndex].Blocks.Count; i++)
            {
                Template.Block templateBlock = Helper.Template.Data.Files[templateFileIndex].Blocks[i];

                List <INIOptions> iniBlocks;
                if (iniData.TryGetValue(templateBlock.Name, out iniBlocks))
                {
                    //loop each ini block
                    foreach (INIOptions iniBlock in iniBlocks)
                    {
                        EditorINIBlock editorBlock = new EditorINIBlock(templateBlock.Name, i);

                        //loop each template option
                        for (int j = 0; j < templateBlock.Options.Count; j++)
                        {
                            Template.Option childTemplateOption = null;
                            if (j < templateBlock.Options.Count - 1 && templateBlock.Options[j + 1].Parent != null)
                            {
                                //next option is child of current option
                                childTemplateOption = templateBlock.Options[j + 1];
                            }

                            Template.Option templateOption = templateBlock.Options[j];
                            EditorINIOption editorOption   = new EditorINIOption(templateOption.Name, j);

                            List <INIOption> iniOptions;
                            if (iniBlock.TryGetValue(templateOption.Name, out iniOptions))
                            {
                                //h is used to start again at last child option in order to provide better performance
                                int h = 0;

                                if (templateOption.Multiple)
                                {
                                    //loop each ini option
                                    for (int k = 0; k < iniOptions.Count; k++)
                                    {
                                        List <object>    editorChildOptions = null;
                                        List <INIOption> iniChildOptions;
                                        if (childTemplateOption != null && iniBlock.TryGetValue(childTemplateOption.Name, out iniChildOptions))
                                        {
                                            editorOption.ChildTemplateIndex = j + 1;
                                            editorOption.ChildName          = childTemplateOption.Name;
                                            editorChildOptions = new List <object>();

                                            //loop each ini option of child
                                            for (; h < iniChildOptions.Count; h++)
                                            {
                                                INIOption childOption = iniChildOptions[h];
                                                if (k < iniOptions.Count - 1 && childOption.Index > iniOptions[k + 1].Index)
                                                {
                                                    break;
                                                }

                                                if (childOption.Index > iniOptions[k].Index)
                                                {
                                                    editorChildOptions.Add(ConvertToTemplate(childTemplateOption.Type, childOption.Value));
                                                }
                                            }
                                        }

                                        //add entry
                                        editorOption.Values.Add(new EditorINIEntry(ConvertToTemplate(templateOption.Type, iniOptions[k].Value), editorChildOptions));
                                    }
                                }
                                else
                                {
                                    //just add the first option if aviable to prevent multiple options which should be single
                                    if (iniBlock[templateOption.Name].Count > 0)
                                    {
                                        editorOption.Values.Add(new EditorINIEntry(ConvertToTemplate(templateOption.Type, iniOptions[0].Value)));
                                    }
                                }

                                //add option
                                editorBlock.Options.Add(editorOption);
                            }
                            else
                            {
                                //add empty option
                                editorBlock.Options.Add(new EditorINIOption(templateOption.Name, j));
                            }

                            //set index of main option (value displayed in table view)
                            if (templateBlock.Identifier != null && templateBlock.Identifier.ToLower() == editorBlock.Options[editorBlock.Options.Count - 1].Name.ToLower())
                            {
                                editorBlock.MainOptionIndex = editorBlock.Options.Count - 1;
                            }

                            //ignore next option because we already added it as children to the current option
                            if (childTemplateOption != null)
                            {
                                j++;
                            }
                        }

                        //add block
                        editorData.Blocks.Add(editorBlock);
                    }
                }
            }
#if DEBUG
            st.Stop();
            System.Diagnostics.Debug.WriteLine("typecast data: " + st.ElapsedMilliseconds + "ms");
#endif

            return(editorData);
        }
Ejemplo n.º 2
0
        static EditorINIData GetEditorData(List <INIBlock> iniData, int templateFileIndex)
        {
#if DEBUG
            Stopwatch st = new Stopwatch();
            st.Start();
#endif

            EditorINIData editorData   = new EditorINIData(templateFileIndex);
            Template.File templateFile = Helper.Template.Data.Files[templateFileIndex];

            //loop each ini block
            foreach (INIBlock iniBlock in iniData)
            {
                Template.Block templateBlock;
                int            templateBlockIndex;

                //get template block based on ini block name
                if (templateFile.Blocks.TryGetValue(iniBlock.Name, out templateBlock, out templateBlockIndex))
                {
                    EditorINIBlock editorBlock = new EditorINIBlock(templateBlock.Name, templateBlockIndex)
                    {
                        Comments = iniBlock.Comments
                    };

                    //loop each template option
                    for (int j = 0; j < templateBlock.Options.Count; ++j)
                    {
                        //handle nested options
                        Template.Option childTemplateOption = null;
                        if (j < templateBlock.Options.Count - 1 && templateBlock.Options[j + 1].Parent != null)
                        {
                            //next option is child of current option
                            childTemplateOption = templateBlock.Options[j + 1];
                        }

                        Template.Option templateOption = templateBlock.Options[j];
                        EditorINIOption editorOption   = new EditorINIOption(templateOption.Name, j);

                        //get ini options based on all possible template option names
                        List <INIOption> iniOptions;
                        iniBlock.Options.TryGetValue(templateOption.Name, out iniOptions);

                        //search for options with alternative names
                        if (templateOption.RenameFrom != null)
                        {
                            string[] namesFromRename = templateOption.RenameFrom.Split(new[] { ',' });
                            for (int nameIndex = 0; nameIndex < namesFromRename.Length; ++nameIndex)
                            {
                                List <INIOption> alternativeOptions;
                                if (iniBlock.Options.TryGetValue(namesFromRename[nameIndex], out alternativeOptions))
                                {
                                    iniOptions.AddRange(alternativeOptions);

                                    //stop searching after we found the first option group with the correct name
                                    break;
                                }
                            }
                        }

                        if (iniOptions != null)
                        {
                            //h is used to start again at last child option in order to provide better performance
                            int h = 0;

                            if (templateOption.Multiple)
                            {
                                //loop each ini option
                                for (int k = 0; k < iniOptions.Count; ++k)
                                {
                                    List <object>    editorChildOptions = null;
                                    List <INIOption> iniChildOptions;

                                    //get ini options of child based on child's template option name
                                    if (childTemplateOption != null && iniBlock.Options.TryGetValue(childTemplateOption.Name, out iniChildOptions))
                                    {
                                        editorOption.ChildTemplateIndex = j + 1;
                                        editorOption.ChildName          = childTemplateOption.Name;
                                        editorChildOptions = new List <object>();

                                        //loop each ini option of child
                                        for (; h < iniChildOptions.Count; ++h)
                                        {
                                            INIOption childOption = iniChildOptions[h];
                                            if (k < iniOptions.Count - 1 && childOption.Index > iniOptions[k + 1].Index)
                                            {
                                                break;
                                            }

                                            if (childOption.Index > iniOptions[k].Index)
                                            {
                                                editorChildOptions.Add(childOption.Value);
                                            }
                                        }
                                    }

                                    //add entry of multiple option
                                    editorOption.Values.Add(new EditorINIEntry(iniOptions[k].Value, editorChildOptions));
                                }
                            }
                            else //single option
                            {
                                if (iniOptions.Count > 0)
                                {
                                    if (iniOptions[0].Value.Length > 0)
                                    {
                                        //just add the last option (Freelancer like) if aviable to prevent multiple options which should be single
                                        editorOption.Values.Add(new EditorINIEntry(iniOptions[iniOptions.Count - 1].Value));
                                    }
                                    else
                                    {
                                        //use '=' for options which dont have values and are simply defined when using the option key followed by a colon equal
                                        editorOption.Values.Add(new EditorINIEntry("="));
                                    }
                                }
                            }

                            //add option
                            editorBlock.Options.Add(editorOption);
                        }
                        else
                        {
                            //add empty option based on template
                            editorBlock.Options.Add(new EditorINIOption(templateOption.Name, j));
                        }

                        //set index of main option (value displayed in table view)
                        if (templateBlock.Identifier != null && templateBlock.Identifier.Equals(editorBlock.Options[editorBlock.Options.Count - 1].Name, StringComparison.OrdinalIgnoreCase))
                        {
                            editorBlock.MainOptionIndex = editorBlock.Options.Count - 1;
                        }

                        //ignore next option because we already added it as children to the current option
                        if (childTemplateOption != null)
                        {
                            ++j;
                        }
                    }

                    //add block
                    editorData.Blocks.Add(editorBlock);
                }
            }
#if DEBUG
            st.Stop();
            Debug.WriteLine("typecast data: " + st.ElapsedMilliseconds + "ms");
#endif

            return(editorData);
        }
Ejemplo n.º 3
0
        public void Write(EditorINIData data)
        {
            //sort blocks first
            for (int i = 0; i < Helper.Template.Data.Files[data.TemplateIndex].Blocks.Count; i++)
            {
                Template.Block templateBlock = Helper.Template.Data.Files[data.TemplateIndex].Blocks[i];

                for (int j = i; j < data.Blocks.Count; j++)
                {
                    if (data.Blocks[j].Name.ToLower() == templateBlock.Name.ToLower())
                    {
                        //swap blocks
                        EditorINIBlock temporaryBlock = data.Blocks[i];
                        data.Blocks[i] = data.Blocks[j];
                        data.Blocks[j] = temporaryBlock;
                        break;
                    }
                }
            }

            //save data
            INIBlocks newData = new INIBlocks();

            foreach (EditorINIBlock block in data.Blocks)
            {
                INIOptions newBlock = new INIOptions();
                for (int i = 0; i < block.Options.Count; i++)
                {
                    if (block.Options[i].Values.Count > 0)
                    {
                        List <INIOption> newOption = new List <INIOption>();

                        for (int j = 0; j < block.Options[i].Values.Count; j++)
                        {
                            newOption.Add(new INIOption(block.Options[i].Values[j].Value.ToString()));

                            //add suboptions as options with defined parent
                            if (block.Options[i].Values[j].SubOptions != null)
                            {
                                for (int k = 0; k < block.Options[i].Values[j].SubOptions.Count; k++)
                                {
                                    newOption.Add(new INIOption(block.Options[i].Values[j].SubOptions[k].ToString(), block.Options[i].ChildName));
                                }
                            }
                        }

                        newBlock.Add(block.Options[i].Name, newOption);
                    }
                }
                newData.Add(block.Name, newBlock);
            }

            try
            {
                //if (IsBini)
                //{
                //    BINIManager biniManager = new BINIManager(File);
                //    biniManager.Data = newData;
                //    biniManager.Write();
                //}
                //else
                //{
                INIManager iniManager = new INIManager(File);
                iniManager.Write(newData);
                //}
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 4
0
        public void Write(EditorINIData data)
        {
            //save data
            List <INIBlock> newData = new List <INIBlock>();

            foreach (EditorINIBlock block in data.Blocks)
            {
                INIOptions newBlock = new INIOptions();
                foreach (EditorINIOption option in block.Options)
                {
                    if (option.Values.Count > 0)
                    {
                        List <INIOption> newOption = new List <INIOption>();

                        foreach (EditorINIEntry entry in option.Values)
                        {
                            string optionValue = entry.Value.ToString();
                            if (optionValue == "=")
                            {
                                //use an empty value for options which dont have values and are simply defined when using the option key followed by a colon equal
                                newOption.Add(new INIOption
                                {
                                    Value = string.Empty
                                });
                            }
                            else
                            {
                                newOption.Add(new INIOption
                                {
                                    Value = optionValue
                                });
                            }

                            //add suboptions as options with defined parent
                            if (entry.SubOptions != null)
                            {
                                for (int k = 0; k < entry.SubOptions.Count; ++k)
                                {
                                    newOption.Add(new INIOption
                                    {
                                        Value  = entry.SubOptions[k].ToString(),
                                        Parent = option.ChildName
                                    });
                                }
                            }
                        }

                        newBlock.Add(option.Name, newOption);
                    }
                }
                newData.Add(new INIBlock
                {
                    Name     = block.Name,
                    Options  = newBlock,
                    Comments = block.Comments,
                });
            }

            //if (IsBINI)
            //{
            //    BINIManager biniManager = new BINIManager(File);
            //    biniManager.Data = newData;
            //    biniManager.Write();
            //}
            //else
            //{
            INIManager iniManager = new INIManager(File)
            {
                WriteEmptyLine    = WriteEmptyLine,
                WriteSpaces       = WriteSpaces,
                ReadWriteComments = ReadWriteComments,
            };

            iniManager.Write(newData);
            //}
        }