Skip to content

Commit

Permalink
Add PriStringDumper
Browse files Browse the repository at this point in the history
  • Loading branch information
WamWooWam committed Aug 12, 2020
1 parent 5c71a57 commit 5666631
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tools/PriStringDumper/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
63 changes: 63 additions & 0 deletions tools/PriStringDumper/PriStringDumper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{410D91D2-A34E-4430-AE2E-A88A179B9D33}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>PriStringDumper</RootNamespace>
<AssemblyName>PriStringDumper</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PriFormat\PriFormat.csproj">
<Project>{A7228EC9-ACD6-4B18-BDCA-65160149B83A}</Project>
<Name>PriFormat</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
108 changes: 108 additions & 0 deletions tools/PriStringDumper/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using PriFormat;

namespace PriInfo
{
class Program
{
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 ?? "generic";
language = language.ToLowerInvariant();

var json = languages.TryGetValue(language, out var la) ? la : languages[language] = 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:
value = string.Format("<{0} bytes>", data.Length);
break;
}

json[item.Name] = value;

Console.WriteLine(" Candidate {0}: {1}", language, value);
}
}
}
}

foreach (var lang in languages)
{
File.WriteAllText($"{lang.Key}.json", lang.Value.ToString());
}
}
}
}
36 changes: 36 additions & 0 deletions tools/PriStringDumper/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PriStringDumper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PriStringDumper")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("410d91d2-a34e-4430-ae2e-a88a179b9d33")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
1 change: 1 addition & 0 deletions tools/PriStringDumper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This tool converts `.pri` resource files into properly formatted `.json` files for `Windows.ApplicationModel.Resources.ResourceLoader` used for localisation and display scaling, based on the wonderful work of @chausner over here: https://github.com/chausner/PriTools
4 changes: 4 additions & 0 deletions tools/PriStringDumper/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
</packages>

0 comments on commit 5666631

Please sign in to comment.