From 388010996b841b1a1394564f21d9629326501511 Mon Sep 17 00:00:00 2001 From: forest93 Date: Sun, 30 Jan 2022 21:41:42 +0800 Subject: [PATCH] Embed conversion dictionaries as resources. Decide to give up on trying Content items. They are difficult to redistribute. --- .../OpenCCSharp.Packages.props | 6 ++-- OpenCCSharp.Presets/ConversionPresetHelper.cs | 36 +++++++++++++------ .../OpenCCSharp.Presets.csproj | 4 +-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/OpenCCSharp.Commons/OpenCCSharp.Packages.props b/OpenCCSharp.Commons/OpenCCSharp.Packages.props index b74e5e5..5d02c35 100644 --- a/OpenCCSharp.Commons/OpenCCSharp.Packages.props +++ b/OpenCCSharp.Commons/OpenCCSharp.Packages.props @@ -4,9 +4,9 @@ net6.0 - 0.0.1-int.1 - 0.0.1.1 - 0.0.1.1 + 0.0.1-int.2 + 0.0.1.2 + 0.0.1.2 Copyright (C) CXuesong 2022 See https://github.com/CXuesong/OpenCCSharp/releases . OpenCC Chinese Text Conversion diff --git a/OpenCCSharp.Presets/ConversionPresetHelper.cs b/OpenCCSharp.Presets/ConversionPresetHelper.cs index 683aa2e..1bd1421 100644 --- a/OpenCCSharp.Presets/ConversionPresetHelper.cs +++ b/OpenCCSharp.Presets/ConversionPresetHelper.cs @@ -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; @@ -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."; // | SortedStringPrefixDictionary> private static readonly ConcurrentDictionary dictCache = new(); @@ -29,17 +30,25 @@ static async Task 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; @@ -56,12 +65,17 @@ static async Task ValueFactory(string fn, Concurrent public static async ValueTask CreateConverterFromAsync(string configFileName) { ConversionDefinitionRoot config; - await using (var fs = File.OpenRead(Path.Join(ConversionDefinitionsDir, configFileName))) - config = JsonSerializer.Deserialize(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(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 diff --git a/OpenCCSharp.Presets/OpenCCSharp.Presets.csproj b/OpenCCSharp.Presets/OpenCCSharp.Presets.csproj index 89d1447..efd297c 100644 --- a/OpenCCSharp.Presets/OpenCCSharp.Presets.csproj +++ b/OpenCCSharp.Presets/OpenCCSharp.Presets.csproj @@ -24,12 +24,12 @@ - + - +