private void AddStringResources(ResourceMapItem item, string path, List <StringResource> stringResources) { ResourceMapSection primaryResourceMapSection = priFile.GetSectionByRef(priFile.PriDescriptorSection.PrimaryResourceMapSection.Value); DecisionInfoSection decisionInfoSection = priFile.GetSectionByRef(priFile.PriDescriptorSection.DecisionInfoSections.First()); CandidateSet candidateSet; if (!primaryResourceMapSection.CandidateSets.TryGetValue(item.Index, out candidateSet)) { return; } foreach (Candidate candidate in candidateSet.Candidates) { if (candidate != null) { string value; if (candidate.SourceFile != null) { value = "external at " + priFile.GetReferencedFileByRef(candidate.SourceFile.Value).FullName; } else { ByteSpan byteSpan; if (candidate.DataItem != null) { byteSpan = priFile.GetDataItemByRef(candidate.DataItem.Value); } else { byteSpan = candidate.Data.Value; } byte[] data; priStream.Seek(byteSpan.Offset, SeekOrigin.Begin); using (BinaryReader binaryReader = new BinaryReader(priStream, Encoding.Default, true)) data = binaryReader.ReadBytes((int)byteSpan.Length); value = GetCandidateDataAsString(candidate, data); } IReadOnlyList <Qualifier> qualifiers = decisionInfoSection.QualifierSets[candidate.QualifierSet].Qualifiers; string qualifiersDescription = string.Join(", ", qualifiers.Select(q => q.Type + "=" + q.Value)); StringResource stringResource = new StringResource(path, value, qualifiersDescription); stringResources.Add(stringResource); } } }
public CandidateViewModel(PriFile priFile, Stream priStream, string resourceRootPath, ResourceMapItem resourceMapItem, Candidate candidate) { GoToLocationCommand = new RelayCommand(GoToLocationCommand_CanExecute, GoToLocationCommand_Execute); SaveAsCommand = new RelayCommand(SaveAsCommand_CanExecute, SaveAsCommand_Execute); this.priFile = priFile; this.priStream = priStream; this.resourceRootPath = resourceRootPath; this.resourceMapItem = resourceMapItem; Candidate = candidate; DecisionInfoSection decisionInfoSection = priFile.GetSectionByRef(priFile.PriDescriptorSection.DecisionInfoSections.First()); qualifiers = decisionInfoSection.QualifierSets[candidate.QualifierSet].Qualifiers; if (candidate.Type == ResourceValueType.AsciiPath || candidate.Type == ResourceValueType.Utf8Path || candidate.Type == ResourceValueType.Path) { string path = (string)GetData(); if (path != null) { locationPath = path; Location = path; if (!File.Exists(FullLocationPath)) { LocationNotFound = true; } } else { Location = ""; } } else { Location = "(embedded)"; } if (candidate.SourceFile != null) { string sourcePath = priFile.GetReferencedFileByRef(candidate.SourceFile.Value).FullName; if (File.Exists(sourcePath)) { Location = sourcePath + ": " + Location; } else { Location = sourcePath + " (not found)"; SourceNotFound = true; } } }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: PriInfo <path to PRI file>"); return; } using (FileStream stream = File.OpenRead(args[0])) { PriFile priFile = PriFile.Parse(stream); Console.WriteLine("Sections:"); foreach (Section section in priFile.Sections) { Console.WriteLine(" {0}", section); } Console.WriteLine(); Console.WriteLine("Candidates:"); foreach (var resourceMapSectionRef in priFile.PriDescriptorSection.ResourceMapSections) { ResourceMapSection resourceMapSection = priFile.GetSectionByRef(resourceMapSectionRef); if (resourceMapSection.HierarchicalSchemaReference != null) { continue; } DecisionInfoSection decisionInfoSection = priFile.GetSectionByRef(resourceMapSection.DecisionInfoSection); foreach (var candidateSet in resourceMapSection.CandidateSets.Values) { ResourceMapItem item = priFile.GetResourceMapItemByRef(candidateSet.ResourceMapItem); Console.WriteLine(" {0}:", item.FullName); foreach (var candidate in candidateSet.Candidates) { string value = null; if (candidate.SourceFile != null) { value = string.Format("<external in {0}>", priFile.GetReferencedFileByRef(candidate.SourceFile.Value).FullName); } else { ByteSpan byteSpan; if (candidate.DataItem != null) { byteSpan = priFile.GetDataItemByRef(candidate.DataItem.Value); } else { byteSpan = candidate.Data.Value; } stream.Seek(byteSpan.Offset, SeekOrigin.Begin); byte[] data; using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.Default, true)) data = binaryReader.ReadBytes((int)byteSpan.Length); switch (candidate.Type) { case ResourceValueType.AsciiPath: case ResourceValueType.AsciiString: value = Encoding.ASCII.GetString(data).TrimEnd('\0'); break; case ResourceValueType.Utf8Path: case ResourceValueType.Utf8String: value = Encoding.UTF8.GetString(data).TrimEnd('\0'); break; case ResourceValueType.Path: case ResourceValueType.String: value = Encoding.Unicode.GetString(data).TrimEnd('\0'); break; case ResourceValueType.EmbeddedData: value = string.Format("<{0} bytes>", data.Length); break; } } QualifierSet qualifierSet = decisionInfoSection.QualifierSets[candidate.QualifierSet]; string qualifiers = string.Join(", ", qualifierSet.Qualifiers.Select(q => string.Format("{0}={1}", q.Type, q.Value))); Console.WriteLine(" Candidate {0}: {1}", qualifiers, value); } } } } }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: PriInfo <path to PRI file>"); return; } var languages = new Dictionary <string, JObject>(); using (FileStream stream = File.OpenRead(args[0])) { PriFile priFile = PriFile.Parse(stream); foreach (var resourceMapSectionRef in priFile.PriDescriptorSection.ResourceMapSections) { ResourceMapSection resourceMapSection = priFile.GetSectionByRef(resourceMapSectionRef); if (resourceMapSection.HierarchicalSchemaReference != null) { continue; } DecisionInfoSection decisionInfoSection = priFile.GetSectionByRef(resourceMapSection.DecisionInfoSection); foreach (var candidateSet in resourceMapSection.CandidateSets.Values) { ResourceMapItem item = priFile.GetResourceMapItemByRef(candidateSet.ResourceMapItem); Console.WriteLine(" {0}:", item.FullName); foreach (var candidate in candidateSet.Candidates) { if (candidate.SourceFile != null) { continue; } string value = null; var qualifierSet = decisionInfoSection.QualifierSets[candidate.QualifierSet]; //var language = qualifierSet.Qualifiers.FirstOrDefault(l => l.Type == QualifierType.Language)?.Value ?? qualifierSet.Qualifiers.FirstOrDefault(l => l.Type == QualifierType.Scale)?.Value ?? "generic"; //language = language.ToLowerInvariant(); var set = "generic"; var language = qualifierSet.Qualifiers.FirstOrDefault(l => l.Type == QualifierType.Language); if (language != null) { set = language.Value.ToLowerInvariant(); } else { var scale = qualifierSet.Qualifiers.FirstOrDefault(l => l.Type == QualifierType.Scale); if (scale != null) { set = $"scale-{scale.Value}"; } } var json = languages.TryGetValue(set, out var la) ? la : languages[set] = new JObject(); var keysArray = item.FullName.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries); var keys = keysArray.Take(keysArray.Length - 1); if (!keys.Any()) { keys = new[] { "generic" } } ; foreach (var key in keys) { json = json.TryGetValue(key, out var j) ? (JObject)j : (JObject)(json[key] = new JObject()); } ByteSpan byteSpan; if (candidate.DataItem != null) { byteSpan = priFile.GetDataItemByRef(candidate.DataItem.Value); } else { byteSpan = candidate.Data.Value; } stream.Seek(byteSpan.Offset, SeekOrigin.Begin); byte[] data = new byte[byteSpan.Length]; stream.Read(data, 0, (int)byteSpan.Length); switch (candidate.Type) { case ResourceValueType.AsciiPath: case ResourceValueType.AsciiString: value = Encoding.ASCII.GetString(data).TrimEnd('\0'); break; case ResourceValueType.Utf8Path: case ResourceValueType.Utf8String: value = Encoding.UTF8.GetString(data).TrimEnd('\0'); break; case ResourceValueType.Path: case ResourceValueType.String: value = Encoding.Unicode.GetString(data).TrimEnd('\0'); break; case ResourceValueType.EmbeddedData: continue; } var name = item.Name; //if (scale != null && scale.Value != "100") //{ // name = Path.ChangeExtension(name, $".scale-{scale.Value}" + Path.GetExtension(name)); //} json[item.Name] = value; //var name = item.FullName.Substring(11).Replace('\\', '.'); //json.WriteLine($"<data name=\"{name}\" xml:space=\"preserve\">"); //json.WriteLine($" <value><![CDATA[{value}]]></value>"); //json.WriteLine($"</data>"); Console.WriteLine(" Candidate {0}: {1}", language, value); } } } } foreach (var lang in languages) { File.WriteAllText($"{lang.Key}.json", lang.Value.ToString()); //lang.Value.Flush(); } } }