Skip to content

Commit

Permalink
Embed conversion dictionaries as resources.
Browse files Browse the repository at this point in the history
Decide to give up on trying Content items. They are difficult to redistribute.
  • Loading branch information
CXuesong committed Jan 30, 2022
1 parent 48736cf commit 3880109
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions OpenCCSharp.Commons/OpenCCSharp.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<PropertyGroup>
<!-- Here are the default targets. Projects may override on this value with higher requirements. -->
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>0.0.1-int.1</Version>
<AssemblyVersion>0.0.1.1</AssemblyVersion>
<FileVersion>0.0.1.1</FileVersion>
<Version>0.0.1-int.2</Version>
<AssemblyVersion>0.0.1.2</AssemblyVersion>
<FileVersion>0.0.1.2</FileVersion>
<Copyright>Copyright (C) CXuesong 2022</Copyright>
<PackageReleaseNotes>See https://github.com/CXuesong/OpenCCSharp/releases .</PackageReleaseNotes>
<PackageTags>OpenCC Chinese Text Conversion</PackageTags>
Expand Down
36 changes: 25 additions & 11 deletions OpenCCSharp.Presets/ConversionPresetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -12,9 +13,9 @@ namespace OpenCCSharp.Presets;
internal static class ConversionPresetHelper
{

private static readonly string OpenCCDictionaryDir = Path.Join(typeof(ChineseConversionPresets).Assembly.Location, "../vendor/OpenCC/dictionary");
private static readonly string OpenCCDictionariesNamespacePrefix = typeof(ConversionPresetHelper).Namespace + ".ConversionDictionaries.";

private static readonly string ConversionDefinitionsDir = Path.Join(typeof(ChineseConversionPresets).Assembly.Location, "../ConversionDefinitions");
private static readonly string ConversionDefinitionsNamespacePrefix = typeof(ConversionPresetHelper).Namespace + ".ConversionDefinitions.";

// <string, Task<SortedStringPrefixDictionary> | SortedStringPrefixDictionary>
private static readonly ConcurrentDictionary<string, object> dictCache = new();
Expand All @@ -29,17 +30,25 @@ static async Task<TrieStringPrefixDictionary> ValueFactory(string fn, Concurrent

var reverse = segments.Length > 1 && segments[1].Equals("Reverse", StringComparison.OrdinalIgnoreCase);

await foreach (var (k, v) in PlainTextConversionLookupTable.EnumEntriesFromAsync(Path.Join(OpenCCDictionaryDir, segments[0])))
var resourceName = OpenCCDictionariesNamespacePrefix + segments[0];
await using (var rs = typeof(ConversionPresetHelper).Assembly.GetManifestResourceStream(resourceName))
{
if (reverse)
{
foreach (var v1 in v) dict.TryAdd(v1, k.ToArray());
}
else
if (rs == null)
throw new MissingManifestResourceException($"Failed to retrieve the manifest resource: {resourceName}.");

await foreach (var (k, v) in PlainTextConversionLookupTable.EnumEntriesFromAsync(rs))
{
dict.TryAdd(k, v[0].ToArray());
if (reverse)
{
foreach (var v1 in v) dict.TryAdd(v1, k.ToArray());
}
else
{
dict.TryAdd(k, v[0].ToArray());
}
}
}

dict.TrimExcess();
dc[fn] = dict;
return dict;
Expand All @@ -56,12 +65,17 @@ static async Task<TrieStringPrefixDictionary> ValueFactory(string fn, Concurrent
public static async ValueTask<ChainedScriptConverter> CreateConverterFromAsync(string configFileName)
{
ConversionDefinitionRoot config;
await using (var fs = File.OpenRead(Path.Join(ConversionDefinitionsDir, configFileName)))
config = JsonSerializer.Deserialize<ConversionDefinitionRoot>(fs, new JsonSerializerOptions
var resourceName = ConversionDefinitionsNamespacePrefix + configFileName;
await using (var rs = typeof(ConversionPresetHelper).Assembly.GetManifestResourceStream(resourceName))
{
if (rs == null)
throw new MissingManifestResourceException($"Failed to retrieve the manifest resource: {resourceName}.");
config = JsonSerializer.Deserialize<ConversionDefinitionRoot>(rs, new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
})
?? throw new InvalidOperationException("Config JSON resolves to null.");
}
var converters = await config.ConversionSteps.SelectAsync(async step =>
{
var dicts = await step.Dictionaries
Expand Down
4 changes: 2 additions & 2 deletions OpenCCSharp.Presets/OpenCCSharp.Presets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
</ItemGroup>

<ItemGroup>
<Content Include="$(ProjectDir)../vendor/OpenCC/data/dictionary/**/*" Link="vendor/OpenCC/dictionary/%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="Always" />
<EmbeddedResource Include="$(ProjectDir)../vendor/OpenCC/data/dictionary/**/*" Link="ConversionDictionaries/%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
<None Remove="ConversionDefinitions\*" />
<Content Include="ConversionDefinitions\*" CopyToOutputDirectory="Always" />
<EmbeddedResource Include="ConversionDefinitions\*" />
</ItemGroup>

</Project>

0 comments on commit 3880109

Please sign in to comment.