public EditorINIData Read(FileEncoding encoding, int templateFileIndex) { #if DEBUG System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch(); st.Start(); #endif INIBlocks iniData = null; try { //read basic file structure if (encoding == FileEncoding.Automatic || encoding == FileEncoding.BINI) { IsBini = true; BINIManager biniManager = new BINIManager(File); biniManager.Read(); if (biniManager.IsBini) { iniData = biniManager.Data; } else { if (encoding == FileEncoding.Automatic) { iniData = ReadINI(); } else { return(null); } } } else { iniData = ReadINI(); } } catch (Exception ex) { throw ex; } #if DEBUG st.Stop(); System.Diagnostics.Debug.WriteLine("load data: " + st.ElapsedMilliseconds + "ms"); #endif return(GetEditorData(iniData, templateFileIndex));; }
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); }
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 Write(INIBlocks data) { StreamWriter streamWriter = null; try { streamWriter = new StreamWriter(File, false, Encoding.Default); //write each block int i = 0; foreach (KeyValuePair <string, List <INIOptions> > block in data) { for (int j = 0; j < block.Value.Count; j++) { if (i + j > 0) { streamWriter.Write(Environment.NewLine + Environment.NewLine); } streamWriter.WriteLine("[" + block.Key + "]"); //write each option int k = 0; foreach (KeyValuePair <string, List <INIOption> > option in block.Value[j]) { for (int h = 0; h < option.Value.Count; h++) { string key; if (option.Value[h].Parent == null) { key = option.Key; } else { key = option.Value[h].Parent; } streamWriter.Write(key + " = " + option.Value[h].Value); if (h < option.Value.Count - 1) { streamWriter.Write(Environment.NewLine); } } if (k < block.Value[j].Count - 1) { streamWriter.Write(Environment.NewLine); } k++; } } i++; } } catch (Exception ex) { throw ex; } if (streamWriter != null) { streamWriter.Close(); } }
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 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(); } }