Esempio n. 1
0
 static INIBlock CreateBlock(string name, INIOptions options, string comments)
 {
     return(new INIBlock
     {
         Name = name,
         Options = options,
         Comments = comments,
     });
 }
        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;
            }
        }
Esempio n. 3
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);
            //}
        }
Esempio n. 4
0
        public bool Read()
        {
            Data = new List <INIBlock>();

            using (FileStream stream = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (BinaryReader reader = new BinaryReader(stream, Encoding.Default))
                {
                    long streamLength = stream.Length;

                    if (streamLength < ByteLen.FILE_TAG + ByteLen.INT ||
                        Encoding.ASCII.GetString(reader.ReadBytes(ByteLen.FILE_TAG)) != FILE_TYPE ||
                        reader.ReadInt32() != FILE_VERSION)
                    {
                        // return false if it is not a bini file
                        return(false);
                    }

                    int  stringBlockOffset = reader.ReadInt32();
                    long dataPosition      = stream.Position;

                    // goto string block
                    stream.Position = stringBlockOffset;

                    // read string block
                    byte[] buffer = new byte[streamLength - stringBlockOffset];
                    reader.Read(buffer, 0, buffer.Length);
                    string stringBlock = Encoding.ASCII.GetString(buffer);

                    // go back to data
                    stream.Position = dataPosition;

                    // read data
                    while (stream.Position < stringBlockOffset && stream.Position < streamLength)
                    {
                        // read section
                        int sectionNameOffset   = reader.ReadInt16();
                        int sectionEntriesCount = reader.ReadInt16();

                        string     sectionName = stringBlock.Substring(sectionNameOffset, stringBlock.IndexOf('\0', sectionNameOffset) - sectionNameOffset);
                        INIOptions block       = new INIOptions();
                        // read each entry
                        for (int i = 0; i < sectionEntriesCount; ++i)
                        {
                            // read entry
                            int    entryNameOffset  = reader.ReadInt16();
                            int    entryValuesCount = reader.ReadByte();
                            string entryName        = stringBlock.Substring(entryNameOffset, stringBlock.IndexOf('\0', entryNameOffset) - entryNameOffset);

                            // read each value
                            List <string> options = new List <string>();
                            for (int j = 0; j < entryValuesCount; ++j)
                            {
                                // read value
                                int valueType = reader.ReadByte();

                                string entryValue;
                                switch (valueType)
                                {
                                case 1:
                                    // read int
                                    entryValue = reader.ReadInt32().ToString("D", CultureInfo.InvariantCulture);
                                    break;

                                case 2:
                                    // read float
                                    entryValue = reader.ReadSingle().ToString("0.000000", CultureInfo.InvariantCulture);
                                    break;

                                default:
                                {
                                    // read string
                                    int valueNameOffset = reader.ReadInt32();
                                    entryValue = stringBlock.Substring(valueNameOffset, stringBlock.IndexOf('\0', valueNameOffset) - valueNameOffset);
                                }
                                break;
                                }
                                options.Add(entryValue);
                            }
                            block.Add(entryName, new INIOption
                            {
                                Value = string.Join(", ", options.ToArray()),
                                Index = i
                            });
                        }
                        Data.Add(new INIBlock
                        {
                            Name    = sectionName,
                            Options = block
                        });
                    }
                }
            return(true);
        }
        public void Read()
        {
            Data = new INIBlocks();
            FileStream   stream       = null;
            BinaryReader binaryReader = null;

            try
            {
                stream       = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read);
                binaryReader = new BinaryReader(stream, Encoding.Default);

                //read header
                if (Encoding.Default.GetString(binaryReader.ReadBytes(4)) != "BINI" ||
                    binaryReader.ReadInt32() != 1)
                {
                    IsBini = false;
                    binaryReader.Close();
                    return;
                }

                IsBini = true;

                int  stringTablePosition = binaryReader.ReadInt32();
                long dataPosition        = stream.Position;

                //goto string table
                stream.Position = stringTablePosition;

                //read string table
                StringTable stringTable = new StringTable(Encoding.Default.GetString(binaryReader.ReadBytes((int)(stream.Length - stream.Position))));

                //go back to data
                stream.Position = dataPosition;

                //us culture for american numbers
                System.Globalization.CultureInfo usCulture = new System.Globalization.CultureInfo("en-us");

                //read data
                while (stream.Position < stringTablePosition && stream.Position < stream.Length)
                {
                    //read section
                    int sectionStringPosition = binaryReader.ReadInt16();
                    int sectionEntriesCount   = binaryReader.ReadInt16();

                    string     sectionName = stringTable.GetString(sectionStringPosition);
                    INIOptions block       = new INIOptions();
                    //read each entry
                    for (int i = 0; i < sectionEntriesCount; i++)
                    {
                        //read entry
                        int    entryStringPosition = binaryReader.ReadInt16();
                        int    entryValuesCount    = binaryReader.ReadByte();
                        string entryName           = stringTable.GetString(entryStringPosition);

                        //read each value
                        List <string> options = new List <string>();
                        for (int j = 0; j < entryValuesCount; j++)
                        {
                            //read value
                            int valueType = binaryReader.ReadByte();

                            string entryValue = null;
                            if (valueType == 1)
                            {
                                entryValue = binaryReader.ReadInt32().ToString("D", usCulture);
                            }
                            else if (valueType == 2)
                            {
                                entryValue = binaryReader.ReadSingle().ToString("0.000000", usCulture);
                            }
                            else //string
                            {
                                int valueStringPosition = binaryReader.ReadInt32();
                                entryValue = stringTable.GetString(valueStringPosition);
                            }
                            options.Add(entryValue);
                        }
                        block.Add(entryName, new INIOption(string.Join(", ", options.ToArray()), i));
                    }
                    Data.Add(sectionName, block);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (binaryReader != null)
            {
                binaryReader.Close();
            }
        }