Build 0.20.7.0
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
#nullable enable
|
||||
|
||||
using Barotrauma.IO;
|
||||
using Barotrauma.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml.Linq;
|
||||
using System.Globalization;
|
||||
using System.Text.Unicode;
|
||||
@@ -33,54 +33,104 @@ namespace Barotrauma
|
||||
|
||||
public static int LanguageVersion { get; private set; } = 0;
|
||||
|
||||
private static readonly ImmutableArray<Range<int>> CjkRanges = new[]
|
||||
{
|
||||
UnicodeRanges.HangulJamo,
|
||||
UnicodeRanges.Hiragana,
|
||||
UnicodeRanges.Katakana,
|
||||
UnicodeRanges.CjkRadicalsSupplement,
|
||||
UnicodeRanges.CjkSymbolsandPunctuation,
|
||||
UnicodeRanges.EnclosedCjkLettersandMonths,
|
||||
UnicodeRanges.CjkCompatibility,
|
||||
UnicodeRanges.CjkUnifiedIdeographsExtensionA,
|
||||
UnicodeRanges.CjkUnifiedIdeographs,
|
||||
UnicodeRanges.HangulSyllables,
|
||||
UnicodeRanges.CjkCompatibilityForms
|
||||
}.Select(r => new Range<int>(r.FirstCodePoint, r.FirstCodePoint+r.Length-1))
|
||||
.OrderBy(r => r.Start)
|
||||
.ToImmutableArray();
|
||||
private static ImmutableArray<Range<int>> UnicodeToIntRanges(params UnicodeRange[] ranges)
|
||||
=> ranges
|
||||
.Select(r => new Range<int>(r.FirstCodePoint, r.FirstCodePoint + r.Length - 1))
|
||||
.OrderBy(r => r.Start)
|
||||
.ToImmutableArray();
|
||||
|
||||
/// <summary>
|
||||
/// Does the string contain symbols from Chinese, Japanese or Korean languages
|
||||
/// </summary>
|
||||
public static bool IsCJK(LocalizedString text)
|
||||
[Flags]
|
||||
public enum SpeciallyHandledCharCategory
|
||||
{
|
||||
return IsCJK(text.Value);
|
||||
None = 0x0,
|
||||
|
||||
CJK = 0x1,
|
||||
Cyrillic = 0x2,
|
||||
|
||||
All = 0x3
|
||||
}
|
||||
|
||||
public static bool IsCJK(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text)) { return false; }
|
||||
public static readonly ImmutableArray<SpeciallyHandledCharCategory> SpeciallyHandledCharCategories
|
||||
= Enum.GetValues<SpeciallyHandledCharCategory>()
|
||||
.Where(c => c is not (SpeciallyHandledCharCategory.None or SpeciallyHandledCharCategory.All))
|
||||
.ToImmutableArray();
|
||||
|
||||
private static readonly ImmutableDictionary<SpeciallyHandledCharCategory, ImmutableArray<Range<int>>> SpeciallyHandledCharacterRanges
|
||||
= new[]
|
||||
{
|
||||
(SpeciallyHandledCharCategory.CJK, UnicodeToIntRanges(
|
||||
UnicodeRanges.HangulJamo,
|
||||
UnicodeRanges.Hiragana,
|
||||
UnicodeRanges.Katakana,
|
||||
UnicodeRanges.CjkRadicalsSupplement,
|
||||
UnicodeRanges.CjkSymbolsandPunctuation,
|
||||
UnicodeRanges.EnclosedCjkLettersandMonths,
|
||||
UnicodeRanges.CjkCompatibility,
|
||||
UnicodeRanges.CjkUnifiedIdeographsExtensionA,
|
||||
UnicodeRanges.CjkUnifiedIdeographs,
|
||||
UnicodeRanges.HangulSyllables,
|
||||
UnicodeRanges.CjkCompatibilityForms
|
||||
)),
|
||||
(SpeciallyHandledCharCategory.Cyrillic, UnicodeToIntRanges(
|
||||
UnicodeRanges.Cyrillic,
|
||||
UnicodeRanges.CyrillicSupplement,
|
||||
UnicodeRanges.CyrillicExtendedA,
|
||||
UnicodeRanges.CyrillicExtendedB,
|
||||
UnicodeRanges.CyrillicExtendedC
|
||||
))
|
||||
}.ToImmutableDictionary();
|
||||
|
||||
public static SpeciallyHandledCharCategory GetSpeciallyHandledCategories(LocalizedString text)
|
||||
=> GetSpeciallyHandledCategories(text.Value);
|
||||
|
||||
public static SpeciallyHandledCharCategory GetSpeciallyHandledCategories(string text)
|
||||
{
|
||||
if (string.IsNullOrEmpty(text)) { return SpeciallyHandledCharCategory.None; }
|
||||
|
||||
var retVal = SpeciallyHandledCharCategory.None;
|
||||
for (int i = 0; i < text.Length; i++)
|
||||
{
|
||||
char chr = text[i];
|
||||
for (int j = 0; j < CjkRanges.Length; j++)
|
||||
|
||||
foreach (var category in SpeciallyHandledCharCategories)
|
||||
{
|
||||
var range = CjkRanges[j];
|
||||
if (retVal.HasFlag(category)) { continue; }
|
||||
|
||||
for (int j = 0; j < SpeciallyHandledCharacterRanges[category].Length; j++)
|
||||
{
|
||||
var range = SpeciallyHandledCharacterRanges[category][j];
|
||||
|
||||
// If chr < range.Start, we know that it can't
|
||||
// be in any of the following ranges, so let's
|
||||
// not even bother checking them
|
||||
if (chr < range.Start) { break; }
|
||||
// If chr < range.Start, we know that it can't
|
||||
// be in any of the following ranges, so let's
|
||||
// not even bother checking them
|
||||
if (chr < range.Start) { break; }
|
||||
|
||||
// This character is in a range, return true
|
||||
if (range.Contains(chr)) { return true; }
|
||||
// This character is in a range, set the flag
|
||||
if (range.Contains(chr))
|
||||
{
|
||||
retVal |= category;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (retVal == SpeciallyHandledCharCategory.All)
|
||||
{
|
||||
// Input contains characters from all
|
||||
// specially handled categories, there's
|
||||
// no need to inspect the string further
|
||||
return SpeciallyHandledCharCategory.All;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
public static bool IsCJK(LocalizedString text)
|
||||
=> IsCJK(text.Value);
|
||||
|
||||
public static bool IsCJK(string text)
|
||||
=> GetSpeciallyHandledCategories(text).HasFlag(SpeciallyHandledCharCategory.CJK);
|
||||
|
||||
/// <summary>
|
||||
/// Check if the currently selected language is available, and switch to English if not
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user