-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix encoding handling for Catherine & CFB
- Loading branch information
1 parent
74914ae
commit 72d3064
Showing
10 changed files
with
223 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Source/AtlusScriptLibrary/Common/Text/Encodings/CatherineEncoding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace AtlusScriptLibrary.Common.Text.Encodings; | ||
|
||
public abstract class CatherineEncodingBase : CustomUnicodeEncoding | ||
{ | ||
protected readonly static Dictionary<ushort, char> _codeToChar = new() | ||
{ | ||
{ 0xFFE3, ' ' } | ||
}; | ||
|
||
protected CatherineEncodingBase(bool isBigEndian) | ||
: base(isBigEndian, _codeToChar) { } | ||
|
||
public override CustomUnicodeEncoding GetEncodingForEndianness(bool isBigEndian) | ||
=> isBigEndian ? CatherineBigEndianEncoding.Instance : CatherineEncoding.Instance; | ||
} | ||
|
||
public class CatherineBigEndianEncoding : CatherineEncodingBase | ||
{ | ||
public static CatherineBigEndianEncoding Instance { get; } = new(); | ||
private CatherineBigEndianEncoding() : base(true) { } | ||
} | ||
|
||
public class CatherineEncoding : CatherineEncodingBase | ||
{ | ||
public static CatherineEncoding Instance { get; } = new(); | ||
private CatherineEncoding() : base(false) { } | ||
} | ||
|
||
public abstract class CatherineFullBodyEncodingBase : CustomUnicodeEncoding | ||
{ | ||
protected readonly static Dictionary<ushort, char> _codeToChar = new() | ||
{ | ||
}; | ||
|
||
protected CatherineFullBodyEncodingBase(bool isBigEndian) | ||
: base(isBigEndian, _codeToChar) { } | ||
|
||
public override CustomUnicodeEncoding GetEncodingForEndianness(bool isBigEndian) | ||
=> isBigEndian ? CatherineFullBodyBigEndianEncoding.Instance : CatherineFullBodyEncoding.Instance; | ||
} | ||
|
||
public class CatherineFullBodyEncoding : CatherineFullBodyEncodingBase | ||
{ | ||
public static CatherineFullBodyEncoding Instance { get; } = new(); | ||
private CatherineFullBodyEncoding() : base(false) { } | ||
} | ||
|
||
public class CatherineFullBodyBigEndianEncoding : CatherineFullBodyEncodingBase | ||
{ | ||
public static CatherineFullBodyBigEndianEncoding Instance { get; } = new(); | ||
private CatherineFullBodyBigEndianEncoding() : base(true) { } | ||
} |
87 changes: 87 additions & 0 deletions
87
Source/AtlusScriptLibrary/Common/Text/Encodings/CustomUnicodeEncoding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace AtlusScriptLibrary.Common.Text.Encodings; | ||
|
||
public abstract class CustomUnicodeEncoding : Encoding | ||
{ | ||
private readonly Dictionary<char, ushort> _charToCode; | ||
private readonly Dictionary<ushort, char> _codeToChar; | ||
private readonly Encoding _baseEncoding; | ||
private readonly bool _isBigEndian; | ||
|
||
protected CustomUnicodeEncoding(bool isBigEndian, Dictionary<ushort, char> codeToCharMap) | ||
{ | ||
_baseEncoding = isBigEndian ? BigEndianUnicode : Unicode; | ||
_isBigEndian = isBigEndian; | ||
_codeToChar = codeToCharMap; | ||
_charToCode = _codeToChar.ToDictionary(x => x.Value, x => x.Key); | ||
} | ||
|
||
public IReadOnlyDictionary<char, ushort> CharToCustomCode => _charToCode; | ||
public IReadOnlyDictionary<ushort, char> CustomCodeToChar => _codeToChar; | ||
public abstract CustomUnicodeEncoding GetEncodingForEndianness(bool isBigEndian); | ||
|
||
public override int GetByteCount(char[] chars, int index, int count) | ||
=> _baseEncoding.GetByteCount(chars, index, count); | ||
|
||
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) | ||
{ | ||
int bytesWritten = 0; | ||
for (int i = 0; i < charCount; i++) | ||
{ | ||
char currentChar = chars[charIndex + i]; | ||
if (_charToCode.TryGetValue(currentChar, out ushort code)) | ||
{ | ||
if (_isBigEndian) | ||
BinaryPrimitives.WriteUInt16BigEndian(bytes.AsSpan(byteIndex + bytesWritten), code); | ||
else | ||
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(byteIndex + bytesWritten), code); | ||
|
||
bytesWritten += 2; | ||
} | ||
else | ||
{ | ||
// Fallback to base encoding for unmapped characters | ||
bytesWritten += _baseEncoding.GetBytes(chars, charIndex + i, 1, bytes, byteIndex + bytesWritten); | ||
} | ||
} | ||
return bytesWritten; | ||
} | ||
|
||
public override int GetCharCount(byte[] bytes, int index, int count) | ||
=> _baseEncoding.GetCharCount(bytes, index, count); | ||
|
||
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) | ||
{ | ||
int charsWritten = 0; | ||
for (int i = 0; i < byteCount; i += 2) | ||
{ | ||
ushort code = _isBigEndian | ||
? BinaryPrimitives.ReadUInt16BigEndian(bytes.AsSpan(byteIndex + i)) | ||
: BinaryPrimitives.ReadUInt16LittleEndian(bytes.AsSpan(byteIndex + i)); | ||
|
||
if (_codeToChar.TryGetValue(code, out char decodedChar)) | ||
{ | ||
chars[charIndex + charsWritten++] = decodedChar; | ||
} | ||
else | ||
{ | ||
// Fallback to base decoding for unmapped codes | ||
char[] fallbackChar = new char[1]; | ||
_baseEncoding.GetChars(bytes, byteIndex + i, 2, fallbackChar, 0); | ||
chars[charIndex + charsWritten++] = fallbackChar[0]; | ||
} | ||
} | ||
return charsWritten; | ||
} | ||
|
||
public override int GetMaxByteCount(int charCount) | ||
=> _baseEncoding.GetMaxByteCount(charCount); | ||
|
||
public override int GetMaxCharCount(int byteCount) | ||
=> _baseEncoding.GetMaxCharCount(byteCount); | ||
} |
22 changes: 22 additions & 0 deletions
22
Source/AtlusScriptLibrary/Common/Text/Encodings/EncodingHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using AtlusScriptLibrary.Common.IO; | ||
using System.Text; | ||
|
||
namespace AtlusScriptLibrary.Common.Text.Encodings; | ||
|
||
public class EncodingHelper | ||
{ | ||
public static Encoding GetEncodingForEndianness(Encoding encoding, bool isBigEndian) | ||
{ | ||
if (encoding == Encoding.Unicode) | ||
{ | ||
if (isBigEndian) | ||
return Encoding.BigEndianUnicode; | ||
} | ||
else if (encoding is CustomUnicodeEncoding cue) | ||
{ | ||
return cue.GetEncodingForEndianness(isBigEndian); | ||
} | ||
|
||
return encoding; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.