Skip to content

Commit 790457b

Browse files
committed
Fixed decoding UTF-8 encoded string literals
1 parent 416bae1 commit 790457b

12 files changed

+37
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*.suo
66
*.user
77
*.sln.docstates
8+
/.vs/
89

910
# Build results
1011
[Dd]ebug/

FoxTool/Constants.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text;
2+
3+
namespace FoxTool
4+
{
5+
internal static class Constants
6+
{
7+
public static readonly Encoding StringEncoding = Encoding.UTF8;
8+
}
9+
}

FoxTool/ExtensionMethods.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ internal static void AlignWrite(this Stream output, int alignment, byte data)
3737
}
3838
}
3939

40-
internal static string ReadString(this BinaryReader binaryReader, int count)
40+
internal static string ReadString(this BinaryReader binaryReader, int byteCount)
4141
{
42-
return new string(binaryReader.ReadChars(count));
42+
byte[] bytes = binaryReader.ReadBytes(byteCount);
43+
return Constants.StringEncoding.GetString(bytes);
4344
}
4445

4546
internal static bool IsPrintable(this string s)

FoxTool/Fox/FoxFile.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private void Read(Stream input)
207207
}
208208
input.AlignRead(16);
209209
reader.Skip(2);
210-
string eof = reader.ReadString(3);
210+
byte[] eof = reader.ReadBytes(3);
211211
input.AlignRead(16);
212212
}
213213

@@ -229,7 +229,9 @@ private Dictionary<ulong, string> GenerateLocalLookupTable()
229229
if (localLookupTable.ContainsKey(literal.Hash.HashValue) == false)
230230
{
231231
if (literal.IsEncrypted == false)
232+
{
232233
localLookupTable.Add(literal.Hash.HashValue, literal.Literal);
234+
}
233235
}
234236
}
235237
return localLookupTable;

FoxTool/Fox/FoxLookupTable.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ public string Lookup(ulong hash)
2727
{
2828
string result;
2929
if (LocalLookupTable.TryGetValue(hash, out result))
30+
{
3031
return result;
32+
}
33+
3134
if (GlobalLookupTable.TryGetValue(hash, out result))
35+
{
3236
return result;
37+
}
38+
3339
return null;
3440
}
3541
}

FoxTool/Fox/FoxStringLiteralBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public override int GetHashCode()
6161

6262
public void CheckForEncryption()
6363
{
64-
if (Hashing.HashString(Literal) != Hash.HashValue)
64+
ulong literalHash = Hashing.HashString(Literal);
65+
if (literalHash != Hash.HashValue)
6566
{
66-
EncryptedLiteral = Encoding.Default.GetBytes(Literal);
67+
EncryptedLiteral = Constants.StringEncoding.GetBytes(Literal);
6768
}
6869
}
6970

FoxTool/Fox/FoxStringLookupLiteral.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override void Write(Stream output)
4242
{
4343
BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true);
4444

45-
byte[] nameBytes = Literal == null ? new byte[0] : Encoding.Default.GetBytes(Literal);
45+
byte[] nameBytes = Literal == null ? new byte[0] : Constants.StringEncoding.GetBytes(Literal);
4646

4747
Hash.Write(output);
4848
writer.Write((uint) nameBytes.Length);

FoxTool/FoxConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static void DecompileFox(FoxFile foxFile, Stream output)
1212
{
1313
var settings = new XmlWriterSettings
1414
{
15-
Encoding = Encoding.UTF8,
15+
Encoding = Constants.StringEncoding,
1616
Indent = true
1717
};
1818

FoxTool/FoxTool.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37-
<Reference Include="CityHash, Version=0.1.1.0, Culture=neutral, processorArchitecture=MSIL">
38-
<SpecificVersion>False</SpecificVersion>
39-
<HintPath>..\packages\CityHash.Net.Legacy.0.1.1\lib\net45\CityHash.dll</HintPath>
37+
<Reference Include="CityHash, Version=0.1.2.0, Culture=neutral, processorArchitecture=MSIL">
38+
<HintPath>..\packages\CityHash.Net.Legacy.0.1.2.0\lib\net45\CityHash.dll</HintPath>
4039
</Reference>
4140
<Reference Include="System" />
4241
<Reference Include="System.Core" />
@@ -56,6 +55,7 @@
5655
<Compile Include="Fox\Types\Structs\FoxPropertyInfo.cs" />
5756
<Compile Include="Fox\Types\Structs\FoxWideVector3.cs" />
5857
<Compile Include="Fox\Types\Values\FoxStringBase.cs" />
58+
<Compile Include="Constants.cs" />
5959
<Compile Include="Tpp\Enums\TppLensFlareFieldInterpType.cs" />
6060
<Compile Include="Tpp\Enums\TppLensFlareFieldShapeType.cs" />
6161
<Compile Include="Tpp\Enums\TppLensFlareShapeDistanceScalingMode.cs" />

FoxTool/Hashing.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ internal static class Hashing
77
internal static ulong HashString(string text)
88
{
99
if (text == null) throw new ArgumentNullException("text");
10+
byte[] bytes = Constants.StringEncoding.GetBytes(text + "\0");
1011
const ulong seed0 = 0x9ae16a3b2f90404f;
11-
ulong seed1 = text.Length > 0 ? (uint) ((text[0]) << 16) + (uint) text.Length : 0;
12-
return CityHash.CityHash.CityHash64WithSeeds(text + "\0", seed0, seed1) & 0xFFFFFFFFFFFF;
12+
ulong seed1 = bytes.Length > 0 ? (uint) ((bytes[0]) << 16) + (uint) (bytes.Length - 1) : 0;
13+
ulong hash = CityHash.CityHash.CityHash64WithSeeds(bytes, seed0, seed1) & 0xFFFFFFFFFFFF;
14+
return hash;
1315
}
1416
}
1517
}

FoxTool/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
[assembly: AssemblyCulture("")]
1212
[assembly: ComVisible(false)]
1313
[assembly: Guid("561aab12-c53d-4a0e-bac5-0efc04a54618")]
14-
[assembly: AssemblyVersion("0.2.4.0")]
15-
[assembly: AssemblyFileVersion("0.2.4.0")]
14+
[assembly: AssemblyVersion("0.2.5.0")]
15+
[assembly: AssemblyFileVersion("0.2.5.0")]

FoxTool/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="CityHash.Net.Legacy" version="0.1.1" targetFramework="net45" />
3+
<package id="CityHash.Net.Legacy" version="0.1.2.0" targetFramework="net45" />
44
</packages>

0 commit comments

Comments
 (0)