public void LoadLocresFile(string path) { Locres = new LocresFile(); using (var fileStream = File.Open(path, FileMode.Open)) { using (var reader = new LocBinaryReader(fileStream)) { var signature = reader.ReadBytes(16); if (LocresFile.Version4Signature.SequenceEqual(signature)) { Locres.Flags = reader.ReadByte(); Locres.Version = Locres.Flags > 1 ? EngineVersion.Version4_2 : EngineVersion.Version4; Locres.DataSectionStart = reader.ReadInt64(); } else { Locres.Version = EngineVersion.Version3; fileStream.Seek(0, SeekOrigin.Begin); } if (Locres.Version == EngineVersion.Version4_2) { var numberOfEntries = reader.ReadInt32(); } var numberOfSections = reader.ReadUInt32(); for (var i = 0; i < numberOfSections; i++) { var currentSection = new LocSection(); if (Locres.Version == EngineVersion.Version4_2) { currentSection.NamespaceHash = reader.ReadUInt32(); } currentSection.Name = reader.ReadString(); var numberOfEntries = reader.ReadInt32(); for (var y = 0; y < numberOfEntries; y++) { uint hash2 = 0; if (Locres.Version == EngineVersion.Version4_2) { hash2 = reader.ReadUInt32(); } var entryId = reader.ReadString(); var hash = reader.ReadUInt32(); string text = null; int numberOfLine = -1; if (Locres.Version == EngineVersion.Version3) { text = reader.ReadString(); } else { numberOfLine = reader.ReadInt32(); } var entry = new TextEntry { EntryId = entryId, Hash = hash, Hash2 = hash2, Entry = text, NumberOfLine = numberOfLine }; currentSection.Entries.Add(entry); } Locres.LocSections.Add(currentSection); } if (Locres.Version == EngineVersion.Version4 || Locres.Version == EngineVersion.Version4_2) { var numberOfTextEntries = reader.ReadInt32(); var entries = Locres.LocSections.SelectMany(section => section.Entries).ToList(); for (var i = 0; i < numberOfTextEntries; i++) { var localClosureI = i; var entriesToModify = entries.Where(entry => entry.NumberOfLine == localClosureI); var text = reader.ReadString(); if (Locres.Version == EngineVersion.Version4_2) { var numberOfReferences = reader.ReadInt32(); } foreach (var entryToModify in entriesToModify) { entryToModify.Entry = text; } } } } } }
public void LoadTextFile(string path) { Locres = new LocresFile(); using (var fileStream = File.Open(path, FileMode.Open)) { var reader = new StreamReader(fileStream, Encoding.Unicode); LocSection currentSection = null; string line; var isFirstLine = true; var listOfStrings = new List<string>(); while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (isFirstLine) { isFirstLine = false; if (line.Equals("v4")) { Locres.Version = EngineVersion.Version4; Locres.Flags = 1; continue; } if (line.Equals("v4.2")) { Locres.Version = EngineVersion.Version4_2; Locres.Flags = 2; continue; } } if (Locres.Version != EngineVersion.Version4_2) { // new section if (line.StartsWith("[") && line.EndsWith("]")) { currentSection = new LocSection {Name = line.Equals("[]") ? string.Empty : line.Split('[', ']')[1]}; Locres.LocSections.Add(currentSection); } else if (currentSection != null) { string[] tokens = line.Split(new[] { '\t', '=' }, 3); if (tokens.Length == 3) { var text = tokens[2]; var lineIndex = listOfStrings.IndexOf(text); if (lineIndex == -1) { lineIndex = listOfStrings.Count; listOfStrings.Add(text); } var entryIds = tokens[1].Split(','); foreach (var entryId in entryIds) { var entry = new TextEntry { Hash = Convert.ToUInt32(tokens[0], 16), EntryId = entryId, Entry = tokens[2], NumberOfLine = lineIndex }; currentSection.Entries.Add(entry); } } } } else { // new section if (line.StartsWith("[") && line.EndsWith("]")) { var insideBrackets = line.Split('[', ']')[1]; var split = insideBrackets.Split('|'); currentSection = new LocSection { Name = split[0], NamespaceHash = Convert.ToUInt32(split[1], 16) }; Locres.LocSections.Add(currentSection); } else if (currentSection != null) { string[] tokens = line.Split(new[] { '\t' }, 4); if (tokens.Length == 4) { var text = tokens[3]; var lineIndex = listOfStrings.IndexOf(text); if (lineIndex == -1) { lineIndex = listOfStrings.Count; listOfStrings.Add(text); } var entryIds = tokens[2].Split(','); foreach (var entryId in entryIds) { var entry = new TextEntry { Hash = Convert.ToUInt32(tokens[0], 16), Hash2 = Convert.ToUInt32(tokens[1], 16), EntryId = entryId, Entry = text, NumberOfLine = lineIndex }; currentSection.Entries.Add(entry); } } } } } } }