コード例 #1
0
        public bool setCfgValueStatus(string aircraftDirectory, string attrname, string filename, string sectionname = "", bool active = true)
        {
            if (!cfgFileExists(filename))
            {
                filename = "aircraft.cfg";
            }

            CfgFile cfgFile = cfgAircraft.Find(x => x.Name == filename);

            if (cfgFile != null)
            {
                CfgSection ligtsList = cfgFile.Sections.Find(x => x.Name == sectionname);
                if (ligtsList != null)
                {
                    CfgLine line = ligtsList.Lines.Find(x => x.Name == attrname);
                    if (line != null)
                    {
                        line.Active = active;

                        if (DEBUG)
                        {
                            Console.WriteLine("setCfgValueStatus: " + filename + " / " + (sectionname != "" ? sectionname : "ANY") + " / " + attrname + " set to " + active);
                        }
                        return(true);
                    }
                }
            }

            if (DEBUG)
            {
                Console.WriteLine("setCfgValueStatus: " + filename + " / " + (sectionname != "" ? sectionname : "ANY") + " / " + attrname + " NOT FOUND");
            }
            return(false);
        }
コード例 #2
0
        // MISC STUFF

        // IF NO ACTIVE FLAG PROVIDED - ATTRIBUTE WILL BE SET TO 'ACTIVE'
        // SECTION CAN BE EMPTY BUT NEW VALUE WILL BE NOT CREATED IF NOT FOUND

        // FILE NAME OCNVERTED TO LOWERCASE
        // SECTION NAME CONVERTED TO UPPERCASE
        // ATTRIBUTE NAME CONVERTED TO LOWERCASE
        // VALUE AND COMMENT - WHITESPACES TRIM ONLY
        public bool setCfgValue(string aircraftDirectory, string attrname, string value, string filename, string sectionname = "", bool active = true, string comment = "")
        {
            if (!cfgFileExists(filename))
            {
                filename = "aircraft.cfg";
            }

            // TODO: ADD MISSING SECTION
            if (cfgAircraft.Count >= 1)
            {
                foreach (CfgFile cfgFile in cfgAircraft)
                {
                    if (!String.IsNullOrEmpty(filename) && cfgFile.Name != filename.Trim().ToLower())
                    {
                        continue;
                    }

                    foreach (CfgSection cfgSection in cfgFile.Sections)
                    {
                        if (!String.IsNullOrEmpty(sectionname) && cfgSection.Name != sectionname.Trim().ToUpper())
                        {
                            continue;
                        }

                        foreach (CfgLine cfgLine in cfgSection.Lines)
                        {
                            // UPDATE EXISTING ATTR
                            if (cfgLine.Name == attrname.Trim().ToLower())
                            {
                                cfgLine.Active = active;
                                cfgLine.Value  = value.Trim();
                                if (!String.IsNullOrEmpty(comment))
                                {
                                    cfgLine.Comment = comment.Trim();
                                }

                                Console.WriteLine("setCfgExisting: " + cfgFile.Name + "/" + cfgSection.Name + "/" + cfgLine.Name + " = " + cfgLine.Value);
                                //saveCfgFile(aircraftDirectory, cfgFile);

                                return(true);
                            }
                        }

                        // CREATE NEW ATTR
                        if (!String.IsNullOrEmpty(filename) && !String.IsNullOrEmpty(sectionname))
                        {
                            CfgLine cfgLine = new CfgLine(active, attrname.Trim().ToLower(), value, comment);
                            cfgSection.Lines.Add(cfgLine);

                            Console.WriteLine("setCfgNew: " + cfgFile.Name + "/" + cfgSection.Name + "/" + cfgLine.Name + " = " + cfgLine.Value);

                            //saveCfgFile(aircraftDirectory, cfgFile);
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
コード例 #3
0
        // RETURN EMPTY IF NOT FOUND
        public string getCfgValue(string attrname, string filename, string sectionname = "", bool ignoreInactive = false)
        {
            if (!cfgFileExists(filename))
            {
                filename = "aircraft.cfg";
            }

            if (cfgAircraft.Count >= 1)
            {
                foreach (CfgFile cfgFile in cfgAircraft)
                {
                    if (!String.IsNullOrEmpty(filename) && cfgFile.Name != filename.Trim().ToLower())
                    {
                        continue;
                    }

                    foreach (CfgSection section in cfgFile.Sections)
                    {
                        if (!String.IsNullOrEmpty(sectionname) && section.Name != sectionname.Trim().ToUpper())
                        {
                            continue;
                        }

                        CfgLine line = section.Lines.Find(x => x.Name == attrname);
                        if (line != null && (line.Active || ignoreInactive))
                        {
                            if (DEBUG)
                            {
                                Console.WriteLine("getCfgValue: " + filename + " / " + (sectionname != "" ? sectionname : "ANY") + " / " + attrname + " = " + line.Value);
                            }
                            return(line.Value);
                        }
                    }
                }
            }

            if (DEBUG)
            {
                Console.WriteLine("getCfgValue: " + filename + " / " + (sectionname != "" ? sectionname : "ANY") + " / " + attrname + " NOT FOUND");
            }

            return("");
        }
コード例 #4
0
        public void splitCfg(string aircraftDirectory, string singleFile = "", bool addMissing = false)
        {
            List <CfgFile> cfgFiles = new List <CfgFile>();

            processCfgfiles(aircraftDirectory + "\\", true);

            if (cfgTemplates.Count > 0 && cfgFileExists("aircraft.cfg"))
            {
                foreach (var aircraftSection in cfgAircraft[0].Sections)
                {
                    CfgSection cfgTempSection  = null;
                    string     cfgTemplateFile = singleFile == "" ? ".unknown.cfg" : "aircraft.cfg";

                    // FIND SECTION MATCH IN TEMPLATES
                    foreach (CfgFile tplfiles in cfgTemplates)
                    {
                        if (singleFile != "" && tplfiles.Name != singleFile)
                        {
                            continue;
                        }

                        CfgSection cfgTemplateSection = tplfiles.Sections.Find(x => x.Name == aircraftSection.Name);

                        // NUMERUOUS SECTION FIX
                        if (cfgTemplateSection == null)
                        {
                            cfgTemplateSection = tplfiles.Sections.Find(x => x.Name.Contains(".") && aircraftSection.Name.Contains(".") && x.Name.Split('.')[0] == aircraftSection.Name.Split('.')[0]);
                        }

                        if (cfgTemplateSection != null)
                        {
                            cfgTemplateFile = tplfiles.Name;

                            // DEEP COPY FOR COMPARISON PURPOSE
                            cfgTempSection = new CfgSection(cfgTemplateSection.Active, cfgTemplateSection.Name, new List <CfgLine>());
                            foreach (var cfgTempLine in cfgTemplateSection.Lines)
                            {
                                cfgTempSection.Lines.Add(new CfgLine(cfgTempLine.Active, cfgTempLine.Name, cfgTempLine.Value, cfgTempLine.Comment));
                            }

                            break;
                        }
                    }

                    // BUILD MIXED SECTION
                    List <CfgLine> cfgLines = new List <CfgLine>();

                    // ADD LEGACY LINES
                    cfgLines.Add(new CfgLine(true, "", "", ""));
                    foreach (CfgLine aircraftLine in aircraftSection.Lines)
                    {
                        CfgLine cfgTemplateLine = null;

                        if (cfgTempSection != null)
                        {
                            cfgTemplateLine = cfgTempSection.Lines.Find(x => x.Name == aircraftLine.Name ||
                                                                        x.Name.Contains(".") && aircraftLine.Name.Contains(".") && x.Name.Split('.')[0] == aircraftLine.Name.Split('.')[0]);

                            if (cfgTemplateLine != null) // ATTRIBUTE FOUND IN TEMPLATE
                            {
                                cfgTempSection.Lines.Remove(cfgTemplateLine);
                                if (String.IsNullOrEmpty(aircraftLine.Comment))
                                {
                                    aircraftLine.Comment = cfgTemplateLine.Comment;
                                }
                            }
                            // NOT FOUND
                            else if (!String.IsNullOrEmpty(aircraftLine.Name) && !String.IsNullOrEmpty(aircraftLine.Value) && !aircraftLine.Name.Contains("."))
                            {
                                //aircraftLine.Active = false;
                                aircraftLine.Comment += " ### ";
                            }
                        }
                        // SECTION NOT FOUND
                        else if (singleFile == "")
                        {
                            aircraftLine.Active = false;
                        }

                        cfgLines.Add(aircraftLine);
                    }

                    // ADD MODERN LINES
                    if (addMissing && cfgTempSection != null && cfgTempSection.Lines.Count > 0)
                    {
                        cfgLines[0].Comment = "LEGACY";
                        cfgLines.Add(new CfgLine(true, "", "", "MODERN"));
                        foreach (CfgLine cfgTemplateLine in cfgTempSection.Lines)
                        {
                            cfgTemplateLine.Active = false;
                            cfgLines.Add(cfgTemplateLine);
                        }
                    }

                    CfgSection cfgSection = new CfgSection(true, aircraftSection.Name, cfgLines);

                    // ADD SECTION TO FILES LIST
                    if (cfgFiles != null && cfgFiles.Find(x => x.Name == cfgTemplateFile) != null)
                    {
                        cfgFiles.Find(x => x.Name == cfgTemplateFile).Sections.Add(cfgSection);
                    }
                    else
                    {
                        List <CfgSection> cfgSections = new List <CfgSection>();

                        cfgSections.Add(new CfgSection(true, "[VERSION]", new List <CfgLine>()));
                        cfgSections[0].Lines.Add(new CfgLine(true, "major", "1", ""));
                        cfgSections[0].Lines.Add(new CfgLine(true, "minor", "0", ""));

                        cfgSections.Add(cfgSection);
                        cfgFiles.Add(new CfgFile(true, cfgTemplateFile, cfgSections));
                    }
                }

                // RENAME ORIGINAL AIRCRAFT.CFG
                File.Move(aircraftDirectory + "\\aircraft.cfg", aircraftDirectory + "\\.aircraft.cfg");

                // SAVE FILES
                foreach (CfgFile cfgFile in cfgFiles)
                {
                    saveCfgFile(aircraftDirectory, cfgFile);
                }

                //cfgAircraft = cfgFiles;
                processCfgfiles(aircraftDirectory + "\\", true);
            }
        }