/// <summary> /// Add string to the end of the loc file /// </summary> /// <param name="stringId"></param> /// <param name="content"></param> public void AddString(string stringId, string content) { uint hashId = 0; if (stringId.Length > 2 && stringId[0] == '0' && stringId[1] == 'x') { try { hashId = UInt32.Parse(stringId.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier); } catch (Exception e) { Console.WriteLine("Error! invalid string key:'{0}'", stringId); throw e; } } else { hashId = HashHelper.HashString(stringId); } UInt16 sz = (UInt16)((content.Length * 2) + 2 + 4 + 4); // 2:size info 4:hashId 4 min zero bytes bool sixZeros = false; if (sz % 4 != 0) { sz += (UInt16)2; sixZeros = true; } if (BodyEnd + sz > mData.Count) { AddBytes((int)(BodyEnd + sz) - mData.Count); } int stringLoc = (int)(BodyEnd - 4); //write hashId BinUtils.WriteNumberAtLocation(stringLoc, hashId, mData); // write the size BinUtils.Write2ByteNumberAtLocation(stringLoc + 4, sz, mData); // lay down string stringLoc += 6; /** MODIFYING 'stringLoc' NOW!! **/ for (int i = 0; i < content.Length; i++) { BinUtils.Write2ByteNumberAtLocation(stringLoc, content[i], mData); stringLoc += 2; } BinUtils.WriteNumberAtLocation(stringLoc, 0, mData); if (sixZeros) { BinUtils.Write2ByteNumberAtLocation(stringLoc + 4, 0, mData); } AddBodySize(sz); }
internal static uint GetHashId(string stringId) { uint hash = 0; if (stringId.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase)) { try { hash = UInt32.Parse(stringId.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier); } catch (Exception) { } } else { hash = HashHelper.HashString(stringId); } return(hash); }
/// <summary> /// Adds/replaces the strings in the current loc file. /// </summary> /// <param name="text">The text to apply</param> /// <param name="addOnly">When true, do not modify existing strings; just add the new ones.</param> public void ApplyText(string text, bool addOnly, bool skipEmptyStrings) { GetAllStrings(); int pos = 0; string key = ""; string value = ""; Dictionary <string, string> stringsToAdd = new Dictionary <string, string>(); Dictionary <string, string> stringsToSet = new Dictionary <string, string>(); uint stringId = 0; while (pos < text.Length) { key = GetKey(pos, text); if (String.IsNullOrEmpty(key)) { pos++; continue; } if (key.Length > 2 && key[0] == '0' && key[1] == 'x') { try { stringId = UInt32.Parse(key.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier); } catch (Exception e) { Console.WriteLine("Error! invalid string key:'{0}'", key); throw e; } } else { stringId = HashHelper.HashString(key); } pos += (key.Length); value = ParseStringValue(pos, text); if (value == null) { pos++; Console.WriteLine("Warning! No value found for key:'{0}'", key); continue; } if (mStringSet.ContainsKey(stringId)) { if (mStringSet[stringId] != value) { if (stringsToSet.ContainsKey(key)) { stringsToSet[key] = value; } else { stringsToSet.Add(key, value); } } } else { if (stringsToAdd.ContainsKey(key)) { stringsToAdd[key] = value; } else { stringsToAdd.Add(key, value); } } pos += value.Length; // advance to next line pos = text.IndexOf('\n', pos); if (pos == -1) { break; } pos++;// advance past new line } if (!addOnly) { foreach (string k in stringsToSet.Keys) { SetString(k, stringsToSet[k]); } } List <string> hashThese = new List <string>(); foreach (string k in stringsToAdd.Keys) { if (skipEmptyStrings && stringsToAdd[k] == String.Empty) { // do not include empty strings } else { hashThese.Add(k); AddString(k, stringsToAdd[k]); if (Program.Verbose) { Console.WriteLine("adding string:'[{0}]: '{1}'", k, stringsToAdd[k]); } } } if (hashThese.Count > 0) { HashHelper.AddStringsToDictionary(hashThese); } }
private void mHashMeTextBox_TextChanged(object sender, EventArgs e) { uint result = HashHelper.HashString(txt_hashMe.Text); lbl_hashMe.Text = string.Format("0x{0:x}", result); }