Esempio n. 1
0
        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);
                }
            }
        }
Esempio n. 2
0
        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;
                }
            }
        }
Esempio n. 3
0
        private void GetCandidates(ResourceMapItem resourceMapItem)
        {
            Candidates.Clear();

            foreach (Candidate candidate in EnumerateCandidates(resourceMapItem))
            {
                CandidateViewModel candidateViewModel = new CandidateViewModel(PriFile, PriStream, ResourceRootPath, resourceMapItem, candidate);

                Candidates.Add(candidateViewModel);
            }
        }
Esempio n. 4
0
        private IEnumerable <Candidate> EnumerateCandidates(ResourceMapItem resourceMapItem)
        {
            ResourceMapSection primaryResourceMapSection =
                PriFile.GetSectionByRef(PriFile.PriDescriptorSection.PrimaryResourceMapSection.Value);

            CandidateSet candidateSet;

            if (primaryResourceMapSection.CandidateSets.TryGetValue(resourceMapItem.Index, out candidateSet))
            {
                foreach (Candidate candidate in candidateSet.Candidates)
                {
                    if (candidate != null)
                    {
                        yield return(candidate);
                    }
                }
            }
        }
Esempio n. 5
0
        public void SelectedEntryChanged()
        {
            if (selectedEntry == null)
            {
                PreviewContent = null;
                return;
            }

            //if (selectedEntry.ResourceMapEntry is ResourceMapScope)
            //{
            //    ResourceMapScope selectedScope = (ResourceMapScope)selectedEntry.ResourceMapEntry;

            //    scopeDetailFrame.Navigate(new ScopeDetailPage(PriFile, PriStream, selectedScope));

            //    scopeDetailFrame.Visibility = Visibility.Visible;
            //}
            //else
            //{
            //    scopeDetailFrame.Visibility = Visibility.Hidden;
            //    scopeDetailFrame.Navigate(null);
            //}

            if (selectedEntry.ResourceMapEntry is ResourceMapItem)
            {
                ResourceMapItem resourceMapItem = (ResourceMapItem)selectedEntry.ResourceMapEntry;

                GetCandidates(resourceMapItem);

                if (Candidates.Count > 0)
                {
                    SelectedCandidate = Candidates.First();
                }
                else
                {
                    PreviewContent = null;
                }
            }
        }
Esempio n. 6
0
        private void GetEntryType(EntryViewModel entry)
        {
            if (entry.ResourceMapEntry is ResourceMapScope)
            {
                entry.Icon = "/Assets/folder-horizontal.png";
            }
            else
            {
                entry.Icon = "/Assets/blue-document.png";

                ResourceMapItem resourceMapItem = (ResourceMapItem)entry.ResourceMapEntry;

                CandidateViewModel[] candidates = EnumerateCandidates(resourceMapItem)
                                                  .Select(candidate => new CandidateViewModel(PriFile, PriStream, ResourceRootPath, resourceMapItem, candidate)).ToArray();

                if (candidates.Length == 0)
                {
                    entry.Icon = "/Assets/document.png";
                }
                else if (candidates.All(c => c.SourceNotFound || c.LocationNotFound))
                {
                    entry.Icon = "/Assets/blue-document-attribute-x.png";
                }
                else if (candidates.All(
                             c => c.Candidate.Type == ResourceValueType.String ||
                             c.Candidate.Type == ResourceValueType.AsciiString ||
                             c.Candidate.Type == ResourceValueType.Utf8String))
                {
                    entry.Icon     = "/Assets/blue-document-attribute-s.png";
                    entry.IsString = true;
                }
                else if (resourceMapItem.Name.EndsWith(".xbf", StringComparison.OrdinalIgnoreCase) ||
                         resourceMapItem.Name.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase))
                {
                    entry.Icon = "/Assets/blue-document-xaml.png";
                }
            }
        }
Esempio n. 7
0
        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);
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        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();
            }
        }
    }