Skip to content

Commit

Permalink
Uppercases escaped Unicode.
Browse files Browse the repository at this point in the history
Matches ModEditor's default Escaped Unicode formatting.
  • Loading branch information
NBKRedSpy committed Jan 7, 2024
1 parent 7173561 commit d2fd9de
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 38 deletions.
10 changes: 5 additions & 5 deletions CardSurvival-Localization/CardSurvival-Localization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>CardSurvival_Localization</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<FileVersion>3.2.0</FileVersion>
<AssemblyVersion>3.2.0</AssemblyVersion>
<FileVersion>3.3.0</FileVersion>
<AssemblyVersion>3.3.0</AssemblyVersion>
<Description></Description>
<Title></Title>
<Product>$(AssemblyName)</Product>
Expand All @@ -22,8 +22,8 @@
<PackageReference Include="Cocona" Version="2.2.0" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="19.2.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="19.2.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions" Version="20.0.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="20.0.4" />
</ItemGroup>

</Project>
54 changes: 41 additions & 13 deletions CardSurvival-Localization/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Text;
using System.Text.RegularExpressions;
Expand All @@ -10,6 +11,7 @@
using CsvHelper.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;


namespace CardSurvival_Localization
Expand All @@ -20,14 +22,18 @@ internal class Program
public void Run([Argument(Description = "The directory of the mod to translate. Must contain the ModInfo.json file in the folder")]
string sourceDirectory,

[Option('e', Description = "How to escape unicode characters. Defaults to retaining the file's format.")]
[Option('e', Description = "How to escape unicode characters. Defaults to retaining the file's format. Ex: For the letter A, escaped is \u0041, unescaped is A")]
[EnumDataType(typeof(UnicodeEscapeMode))]
UnicodeEscapeMode? unicodeEscapeMode)
UnicodeEscapeMode? unicodeEscapeMode,

[Option('u', Description = "If set, uses lowercase for any Unicode escaped characters. Ex: \u98CE instead of \u98ce")]
bool useLowerCaseUnicodeEncoding)

{
try
{
unicodeEscapeMode ??= UnicodeEscapeMode.AutoDetect;
ProcessMod(sourceDirectory, new FileSystem(), unicodeEscapeMode.Value);
ProcessMod(sourceDirectory, new FileSystem(), unicodeEscapeMode.Value, useLowerCaseUnicodeEncoding);

ReturnCode = 0;
}
Expand All @@ -53,8 +59,11 @@ static int Main(string[] args)
}


public static string ProcessMod(string sourceDirectory, IFileSystem fileSystem, UnicodeEscapeMode escapeMode)
public static string ProcessMod(string sourceDirectory, IFileSystem fileSystem, UnicodeEscapeMode escapeMode,
bool useLowercaseUnicodeEncoding)
{
Regex unicodeReplaceRegEx = new Regex(@"(\\u)([a-f0-9]{4})", RegexOptions.Compiled);

if (!fileSystem.Directory.Exists(sourceDirectory))
{
throw new ArgumentException($"Mod Directory does not exist: {sourceDirectory}");
Expand Down Expand Up @@ -91,17 +100,11 @@ public static string ProcessMod(string sourceDirectory, IFileSystem fileSystem,
|| escapeMode == UnicodeEscapeMode.AlwaysEscapeNonAscii
|| escapeMode == UnicodeEscapeMode.NoEncode)
{
////A new localization key was set, write the changes.
fileSystem.File.WriteAllText(file, jsonDoc.ToString(Newtonsoft.Json.Formatting.Indented));

//using (var streamWriter = new StreamWriter(file))
using (var fileStreamWriter = fileSystem.FileStream.New(file, FileMode.Create))
using (StreamWriter streamWriter = new StreamWriter(fileStreamWriter))
using (MemoryStream resultWriterStream = new())
using (StreamWriter streamWriter = new StreamWriter(resultWriterStream))
using (JsonWriter writer = new JsonTextWriter(streamWriter))
{
JsonWriter writer = new JsonTextWriter(streamWriter);

writer.Formatting = Formatting.Indented;

writer.AutoCompleteOnClose = true;

switch (escapeMode)
Expand All @@ -121,6 +124,31 @@ public static string ProcessMod(string sourceDirectory, IFileSystem fileSystem,
}

jsonDoc.WriteTo(writer);
writer.Flush();

byte[] jsonResultArray = resultWriterStream.ToArray();

if (useLowercaseUnicodeEncoding)
{
fileSystem.File.WriteAllBytes(file, jsonResultArray);
}
else
{
//Dev Note: This is a bit inefficient, but is fine for the performance target of this utility.
// Unfortunately was not able to intercept JsonWriter since it encodes Unicode after the
// converters are executed. The encoding methods are also private.
//
// A custom stream would require intercepting the bytes as they stream in, so not worth the
// work since this accomplishes the need.

string result = Encoding.UTF8.GetString(jsonResultArray);

result = unicodeReplaceRegEx.Replace(result, (Match match) =>
match.Groups[1].Value + match.Groups[2].Value.ToUpper());

fileSystem.File.WriteAllText(file, result);

}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>CardSurvival_LocalizationTests</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
Loading

0 comments on commit d2fd9de

Please sign in to comment.