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);
        }
Exemple #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);
        }