Esempio n. 1
0
        private void Parse(string data)
        {
            List <CfgSaveInfo> currentSection     = null;
            string             currentSectionName = null;

            int currentIndex = 0;
            int nextBlockEnd;

            for (;;)
            {
                int nextQuotes = data.IndexOf('"', currentIndex);
                if (nextQuotes == -1)
                {
                    break;
                }

                int delta = nextQuotes - currentIndex;

                if (currentSection == null)
                {
                    if (delta > 1)
                    {
                        currentSectionName = data.Substring(currentIndex, nextQuotes - currentIndex);
                        currentSection     = new List <CfgSaveInfo>();
                        sections.Add(currentSectionName, currentSection);

                        nextBlockEnd = data.IndexOf('}', nextQuotes);
                    }
                }
                else
                {
                    if (delta > 1)
                    {
                        string propertyName = data.Substring(currentIndex, nextQuotes - currentIndex);
                        if (!string.IsNullOrWhiteSpace(propertyName) && !propertyName.Contains("{"))
                        {
                            // read to the right
                            int start = data.IndexOf('"', nextQuotes + 1);
                            int end   = data.IndexOf('"', start + 1);

                            string      propertyValue = data.Substring(start + 1, end - start - 1);
                            CfgSaveInfo info          = new CfgSaveInfo(currentSectionName, propertyName, propertyValue);
                            currentSection.Add(info);

                            nextQuotes = end;
                        }
                        else if (propertyName.Contains("}"))
                        {
                            break;
                        }
                    }
                }

                currentIndex = nextQuotes + 1;
            }
        }
Esempio n. 2
0
        public void ChangeProperty(string section, string propertyName, string value)
        {
            List <CfgSaveInfo> infos;

            if (!sections.TryGetValue(section, out infos))
            {
                infos = new List <CfgSaveInfo>();
                sections.Add(section, infos);
            }

            CfgSaveInfo info = infos.FirstOrDefault(c => c.Key == propertyName);

            if (info == null)
            {
                infos.Add(new CfgSaveInfo(section, propertyName, value));
            }
            else
            {
                info.Value = value;
            }
            //int start;
            //int end;
            //if (GetPosition(rawData, propertyName, out start, out end))
            //{
            //    rawData = rawData.Remove(start, end - start);
            //    rawData = rawData.Insert(start, value);

            //    return true;
            //}
            //else
            //{
            //    // search for the section name
            //    if (GetPosition(rawData, section, out start, out end))
            //    {
            //        // write the property

            //    }
            //    else
            //    {
            //        // write section
            //        rawData += section + "{ }";

            //        // now write the property
            //        ChangeProperty(section, propertyName, value);
            //    }
            //}
            //return false;
        }
Esempio n. 3
0
        public void ModifySaveFile(string installSavePath, string saveFullPath, SaveType type, params SaveInfo[] info)
        {
            // this needs to be dynamic someday
            switch (type)
            {
            case SaveType.CFG:
            {
                SourceCfgFile cfg = new SourceCfgFile(installSavePath);
                for (int j = 0; j < info.Length; j++)
                {
                    SaveInfo save = info[j];
                    if (save is CfgSaveInfo)
                    {
                        CfgSaveInfo option = (CfgSaveInfo)save;
                        cfg.ChangeProperty(option.Section, option.Key, option.Value);
                    }
                }
                cfg.Save(saveFullPath);
            }
            break;

            case SaveType.INI:
            {
                if (!installSavePath.Equals(saveFullPath))
                {
                    File.Copy(installSavePath, saveFullPath);
                }
                IniFile file = new IniFile(saveFullPath);
                for (int j = 0; j < info.Length; j++)
                {
                    SaveInfo save = info[j];
                    if (save is IniSaveInfo)
                    {
                        IniSaveInfo ini = (IniSaveInfo)save;
                        file.IniWriteValue(ini.Section, ini.Key, ini.Value);
                    }
                }
            }
            break;

            default:
                throw new NotImplementedException();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Flushes all changes to disk
        /// </summary>
        public void Save(string newPath = "")
        {
            if (string.IsNullOrEmpty(newPath))
            {
                newPath = path;
            }

            if (File.Exists(newPath))
            {
                File.Delete(newPath);
            }

            using (Stream str = File.OpenWrite(newPath))
            {
                using (StreamWriter writer = new StreamWriter(str))
                {
                    foreach (var pair in sections)
                    {
                        writer.WriteLine($"\"{pair.Key}\"");
                        writer.WriteLine("{");
                        var list = pair.Value;

                        for (int i = 0; i < list.Count; i++)
                        {
                            CfgSaveInfo info = list[i];
                            writer.WriteLine($"\r\"{info.Key}\"  \"{info.Value}\"");
                        }

                        writer.WriteLine("}");
                    }


                    writer.Flush();
                    writer.Close();
                }
            }
        }