/// <summary> /// Gets the value between the parantheses and quotation marks in the given string. /// Ex: Passing `VarScope("build")` would return `build`. /// </summary> /// <param name="line">String to parse.</param> /// <returns>Parsed value.</returns> public static string ParseValue(string line) { string parsedValue; // Replace quotation marks with '|' string formattedLine = line.Replace("\"", "|"); bool useQuotations = false; if (line.Contains("|")) { useQuotations = true; } // Get the value of the string from within the parantheses/quotation marks char openChar, closeChar; int openIdx, closeIdx; if (useQuotations) { openChar = '|'; closeChar = '|'; openIdx = formattedLine.IndexOf(openChar) + 1; closeIdx = formattedLine.LastIndexOf(closeChar) - 1; } else { openChar = '('; closeChar = ')'; openIdx = formattedLine.IndexOf(openChar) + 1; closeIdx = formattedLine.LastIndexOf(closeChar); } // Remove any quotation marks and such from the final string if (openIdx != closeIdx) { parsedValue = StringExt.SubstringIdx(formattedLine, openIdx, closeIdx); parsedValue = parsedValue.Replace("|", ""); } else { parsedValue = formattedLine[openIdx].ToString(); } return(parsedValue); }
/// <summary> /// Sets a new value for this Key. Automatically handles setting the binary Values and the Size. /// </summary> /// <param name="str">Readable string to save to the key.</param> public void SetValue(string str) { // TODO: need to figure out the behaviour of the first two unicode characters and add logic to make sure the Value always starts with the right ones /* NOTES: * - Closing MLTool, reopening, and modifying the key seems to increment the first digit * - Closing MLTool, reopening, and NOT modifying the key (but still saving) does NOT increment the first digit * - Creating a new, blank key and saving: 0 * - Creating a new, blank key, adding a character, and saving: 1 * - It appears that several chars are cut off from the end of the string when these two unicode chars are left out */ // Set the readable value Value = "\u0000" + "\u0000" + str; // Calculate and set the new size Size = (Value.Length * 2).ToString(); // Convert readable string to binary unicode BinaryValues.Clear(); BinaryValues = StringExt.ConvertStringToUnicodeList(Value); }
/// <summary> /// Constructs a DataBase from the specified localization file. /// </summary> /// <param name="filePath">Path of localization file to parse.</param> /// <returns>DataBase containing the localization file's parsed contents.</returns> /// <exception cref="System.Security.SecurityException"></exception> /// <exception cref="NotSupportedException"></exception> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="UnauthorizedAccessException"></exception> /// <exception cref="IOException"></exception> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> public static DataBase ParseDataBase(string filePath) { DataBase db = new DataBase(); try { // StreamReader vars //StreamReader file = new StreamReader(filePath); string[] file = File.ReadAllLines(filePath); // Iteration vars Chunk curParentChunk = Chunk.DataBase; List <Chunk> parentChunks = new List <Chunk>(); Scope curScope = new Scope(); List <Scope> scopes = new List <Scope>(); Key curKey = new Key(); int curIndex = 0; // Go through each line of the file for (int i = 0; i < file.Length; i++) { string line = file[i]; // Set the current chunk header if (line == "DataBase()") { curParentChunk = Chunk.DataBase; } else if (line.Contains("VarScope(")) { curParentChunk = Chunk.VarScope; } else if (line.Contains("VarBinary(")) { curParentChunk = Chunk.VarBinary; } // Are we opening a new chunk? else if (line.Contains("{")) { if (curParentChunk == Chunk.VarScope) { // Initialize the new Scope Scope scope = new Scope(); scope.Name = ParseValue(file[i - 1]); // Add the new Scope to the DataBase container if (parentChunks.Last() == Chunk.DataBase) { db.Scopes.Add(scope); // Get a ref to the most recent Scope curIndex = db.Scopes.Count - 1; curScope = db.Scopes[curIndex]; } // Add the new Scope to the Scope container else if (parentChunks.Last() == Chunk.VarScope) { if (curScope != null) { // Add the new Scope to the DataBase container curScope.Scopes.Add(scope); // <-- might be what's causing problems // Get a ref to the most recent Scope curIndex = curScope.Scopes.Count - 1; curScope = curScope.Scopes[curIndex]; } } scopes.Add(curScope); //curLastScope = new Scope(); } else if (curParentChunk == Chunk.VarBinary) { // Initialize the new Key Key key = new Key(); key.Name = ParseValue(file[i - 1]); if (parentChunks.Last() == Chunk.DataBase) { // Add the new Key to the DataBase container db.Keys.Add(key); // Get a ref to the most recent Key curIndex = db.Keys.Count - 1; curKey = db.Keys[curIndex]; } else if (parentChunks.Last() == Chunk.VarScope) { if (curKey != null) { // Add the new Key to the Scope container curScope.Keys.Add(key); // Get a ref to the most recent Key curIndex = curScope.Keys.Count - 1; curKey = curScope.Keys[curIndex]; } } //if (parentChunks.Last() == Chunk.DataBase || parentChunks.Last() == Chunk.VarScope) //{ // parentChunkParent = parentChunk; //} } parentChunks.Add(curParentChunk); } // Are we closing the opened chunk? else if (line.Contains("}")) { if (curParentChunk == Chunk.VarScope) { // Close out of the Scope scopes.RemoveAt(scopes.Count - 1); if (scopes.Count != 0) { curScope = scopes.Last(); } } else if (curParentChunk == Chunk.VarBinary) { curKey.Value = StringExt.ConvertUnicodeListToString(curKey.BinaryValues); } parentChunks.RemoveAt(parentChunks.Count - 1); if (parentChunks.Count != 0) { curParentChunk = parentChunks.Last(); } } // If we're in a VarBinary chunk, parse the Key properties else if (parentChunks.Last() == Chunk.VarBinary) { if (line.Contains("Size(")) { curKey.Size = ParseValue(line); } else if (line.Contains("Value(")) { curKey.BinaryValues.Add(ParseValue(line)); } //else if (curLine.Contains("}")) //{ // curParentChunk = parentChunkParent; //} } else if (parentChunks.Last() == Chunk.VarScope) { //if (curLine.Contains("}")) //{ // curParentChunk = parentChunkParent; //} } } } catch (System.Security.SecurityException ex) { throw new System.Security.SecurityException(ex.Message, ex); } catch (NotSupportedException ex) { throw new NotSupportedException(ex.Message, ex); } catch (FileNotFoundException ex) { throw new FileNotFoundException(ex.Message, filePath, ex); } catch (UnauthorizedAccessException ex) { throw new UnauthorizedAccessException(ex.Message, ex); } catch (IOException ex) { throw new IOException(ex.Message, ex); } catch (ArgumentNullException ex) { throw new ArgumentNullException(ex.Message, ex); } catch (ArgumentException ex) { throw new ArgumentException(ex.Message, ex); } return(db); }