Beispiel #1
0
        public void OpenPriFile(string path)
        {
            ClosePriFile();

            try
            {
                PriStream = File.OpenRead(path);

                PriFile = PriFile.Parse(PriStream);
            }
            catch
            {
                ClosePriFile();
                MessageBox.Show("Could not read file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!PriFile.Sections.OfType <ResourceMapSection>().Any())
            {
                ClosePriFile();
                MessageBox.Show("Incompatible PRI file.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            ResourceRootPath = Path.GetDirectoryName(path);

            GetEntries();

            SetResourceRootPathCommand.RaiseCanExecuteChanged();
        }
Beispiel #2
0
        public static string Localize(string priFile, string appId, string packageFullName, string resourceId)
        {
            if (resourceId == null || !resourceId.StartsWith("ms-resource:"))
            {
                return(resourceId);
            }

            if (!File.Exists(priFile))
            {
                return(resourceId);
            }

            var outBuff = new StringBuilder(1024);


            resourceId = resourceId.Remove(0, "ms-resource:".Length);
            if (string.IsNullOrEmpty(resourceId))
            {
                return(resourceId);
            }

            string fullString;

            if (resourceId.StartsWith("//"))
            {
                fullString = "@{" + priFile + "?ms-resource:" + resourceId.TrimEnd('/') + "}";
                if (SHLoadIndirectString(fullString, outBuff, outBuff.Capacity, IntPtr.Zero) == 0)
                {
                    return(outBuff.ToString());
                }
            }

            string msResource;

            if (resourceId[0] == '/')
            {
                var split         = resourceId.Split('/');
                var newResourceId = string.Join('/', split.Take(2)) + "/" + string.Join('.', split.Skip(2));
                msResource = "ms-resource://" + appId + newResourceId.TrimEnd('/');
            }
            else if (resourceId.IndexOf('/') != -1)
            {
                var split         = resourceId.Split('/');
                var newResourceId = string.Join('/', split.Take(1)) + "/" + string.Join('.', split.Skip(1));
                msResource = "ms-resource://" + appId + "/" + newResourceId.TrimEnd('/');
            }
            else
            {
                var split         = resourceId.Split('/');
                var newResourceId = string.Join('/', split.Take(1)) + "/" + string.Join('.', split.Skip(1));
                msResource = "ms-resource://" + appId + "/resources/" + newResourceId.TrimEnd('/');
            }

            fullString = "@{" + priFile + "?" + msResource + "}";

            if (SHLoadIndirectString(fullString, outBuff, outBuff.Capacity, IntPtr.Zero) == 0)
            {
                return(outBuff.ToString());
            }

            fullString = "@{" + packageFullName + "?" + msResource + "}";
            if (SHLoadIndirectString(fullString, outBuff, outBuff.Capacity, IntPtr.Zero) == 0)
            {
                return(outBuff.ToString());
            }

            using (var s = File.OpenRead(priFile))
            {
                var pri  = PriFile.Parse(s);
                var name = pri.Sections.OfType <HierarchicalSchemaSection>().FirstOrDefault(s => s.UniqueName != null);
                if (name != null)
                {
                    msResource = "ms-resource://" + name.UniqueName.Substring(name.UniqueName.IndexOf("://", StringComparison.OrdinalIgnoreCase) + 3) + "Resources/" + resourceId.TrimEnd('/');
                    fullString = "@{" + priFile + "?" + msResource + "}";

                    if (SHLoadIndirectString(fullString, outBuff, outBuff.Capacity, IntPtr.Zero) == 0)
                    {
                        return(outBuff.ToString());
                    }

                    msResource = "ms-resource://" + name.UniqueName.Substring(name.UniqueName.IndexOf("://", StringComparison.OrdinalIgnoreCase) + 3) + resourceId.TrimEnd('/');
                    fullString = "@{" + priFile + "?" + msResource + "}";

                    if (SHLoadIndirectString(fullString, outBuff, outBuff.Capacity, IntPtr.Zero) == 0)
                    {
                        return(outBuff.ToString());
                    }
                }
            }

            return(resourceId);
        }
Beispiel #3
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();
            }
        }
    }
Beispiel #4
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);
                        }
                    }
                }
            }
        }
        public object GetData()
        {
            byte[] data;

            if (Candidate.SourceFile == null)
            {
                ByteSpan byteSpan;

                if (Candidate.DataItem != null)
                {
                    byteSpan = priFile.GetDataItemByRef(Candidate.DataItem.Value);
                }
                else
                {
                    byteSpan = Candidate.Data.Value;
                }

                priStream.Seek(byteSpan.Offset, SeekOrigin.Begin);

                using (BinaryReader binaryReader = new BinaryReader(priStream, Encoding.Default, true))
                    data = binaryReader.ReadBytes((int)byteSpan.Length);
            }
            else
            {
                string sourcePath = priFile.GetReferencedFileByRef(Candidate.SourceFile.Value).FullName;

                if (!File.Exists(sourcePath))
                {
                    return(null);
                }

                using (FileStream fileStream = File.OpenRead(sourcePath))
                {
                    PriFile  sourcePriFile = PriFile.Parse(fileStream);
                    ByteSpan byteSpan      = sourcePriFile.GetDataItemByRef(Candidate.DataItem.Value);

                    fileStream.Seek(byteSpan.Offset, SeekOrigin.Begin);

                    using (BinaryReader binaryReader = new BinaryReader(fileStream, Encoding.Default, true))
                        data = binaryReader.ReadBytes((int)byteSpan.Length);
                }
            }

            switch (Candidate.Type)
            {
            case ResourceValueType.AsciiPath:
            case ResourceValueType.AsciiString:
                return(Encoding.ASCII.GetString(data).TrimEnd('\0'));

            case ResourceValueType.Utf8Path:
            case ResourceValueType.Utf8String:
                return(Encoding.UTF8.GetString(data).TrimEnd('\0'));

            case ResourceValueType.Path:
            case ResourceValueType.String:
                return(Encoding.Unicode.GetString(data).TrimEnd('\0'));

            case ResourceValueType.EmbeddedData:
                return(data);

            default:
                throw new Exception();
            }
        }