public INIBlocks Read() { INIBlocks data = new INIBlocks(); StreamReader streamReader = null; try { streamReader = new StreamReader(File, Encoding.Default); KeyValuePair <string, INIOptions> currentBlock = new KeyValuePair <string, INIOptions>(); int currentOptionIndex = 0; while (!streamReader.EndOfStream) { string line = streamReader.ReadLine().Trim(); //remove comments from data int commentIndex = line.IndexOf(';'); if (commentIndex != -1) { line = line.Substring(0, commentIndex).Trim(); } if (line.Length > 0 && line[0] == '[' && line[line.Length - 1] != ']') { //reset current block if block was commented out if (currentBlock.Key != null) { data.Add(currentBlock.Key, currentBlock.Value); } currentBlock = new KeyValuePair <string, INIOptions>(); } else if (line.Length > 0 && line[0] == '[' && line[line.Length - 1] == ']') { //new block if (currentBlock.Key != null) { data.Add(currentBlock.Key, currentBlock.Value); } string blockName = line.Substring(1, line.Length - 2).Trim(); currentBlock = new KeyValuePair <string, INIOptions>(blockName, new INIOptions()); currentOptionIndex = 0; } else if (currentBlock.Key != null) { //new value for block int valueIndex = line.IndexOf('='); if (valueIndex != -1) { //retrieve name and value from data string optionName = line.Substring(0, valueIndex).Trim(); string optionValue = line.Substring(valueIndex + 1, line.Length - valueIndex - 1).Trim(); currentBlock.Value.Add(optionName, new INIOption(optionValue, currentOptionIndex)); currentOptionIndex++; } } } if (currentBlock.Key != null) { data.Add(currentBlock.Key, currentBlock.Value); } } catch (Exception ex) { throw ex; } if (streamReader != null) { streamReader.Close(); } return(data); }
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; } }
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(); } }