Skip to content

Commit

Permalink
Merge pull request #1 from Atvaark/master
Browse files Browse the repository at this point in the history
Fixed decoding UTF-8 encoded string literals
  • Loading branch information
NasaNhak authored Jul 28, 2018
2 parents b9e2fac + 790457b commit bab6e0e
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.suo
*.user
*.sln.docstates
/.vs/

# Build results
[Dd]ebug/
Expand Down
9 changes: 9 additions & 0 deletions FoxTool/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text;

namespace FoxTool
{
internal static class Constants
{
public static readonly Encoding StringEncoding = Encoding.UTF8;
}
}
5 changes: 3 additions & 2 deletions FoxTool/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ internal static void AlignWrite(this Stream output, int alignment, byte data)
}
}

internal static string ReadString(this BinaryReader binaryReader, int count)
internal static string ReadString(this BinaryReader binaryReader, int byteCount)
{
return new string(binaryReader.ReadChars(count));
byte[] bytes = binaryReader.ReadBytes(byteCount);
return Constants.StringEncoding.GetString(bytes);
}

internal static bool IsPrintable(this string s)
Expand Down
4 changes: 3 additions & 1 deletion FoxTool/Fox/FoxFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void Read(Stream input)
}
input.AlignRead(16);
reader.Skip(2);
string eof = reader.ReadString(3);
byte[] eof = reader.ReadBytes(3);
input.AlignRead(16);
}

Expand All @@ -232,7 +232,9 @@ private Dictionary<ulong, string> GenerateLocalLookupTable()
if (localLookupTable.ContainsKey(literal.Hash.HashValue) == false)
{
if (literal.IsEncrypted == false)
{
localLookupTable.Add(literal.Hash.HashValue, literal.Literal);
}
}
}
return localLookupTable;
Expand Down
6 changes: 6 additions & 0 deletions FoxTool/Fox/FoxLookupTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ public string Lookup(ulong hash)
{
string result;
if (LocalLookupTable.TryGetValue(hash, out result))
{
return result;
}

if (GlobalLookupTable.TryGetValue(hash, out result))
{
return result;
}

return null;
}
}
Expand Down
5 changes: 3 additions & 2 deletions FoxTool/Fox/FoxStringLiteralBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public override int GetHashCode()

public void CheckForEncryption()
{
if (Hashing.HashString(Literal) != Hash.HashValue)
ulong literalHash = Hashing.HashString(Literal);
if (literalHash != Hash.HashValue)
{
EncryptedLiteral = Encoding.Default.GetBytes(Literal);
EncryptedLiteral = Constants.StringEncoding.GetBytes(Literal);
}
}

Expand Down
2 changes: 1 addition & 1 deletion FoxTool/Fox/FoxStringLookupLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Write(Stream output)
{
BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true);

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

Hash.Write(output);
writer.Write((uint) nameBytes.Length);
Expand Down
2 changes: 1 addition & 1 deletion FoxTool/FoxConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static void DecompileFox(FoxFile foxFile, Stream output)
{
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
Encoding = Constants.StringEncoding,
Indent = true
};

Expand Down
6 changes: 3 additions & 3 deletions FoxTool/FoxTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CityHash, Version=0.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\CityHash.Net.Legacy.0.1.1\lib\net45\CityHash.dll</HintPath>
<Reference Include="CityHash, Version=0.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\CityHash.Net.Legacy.0.1.2.0\lib\net45\CityHash.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -56,6 +55,7 @@
<Compile Include="Fox\Types\Structs\FoxPropertyInfo.cs" />
<Compile Include="Fox\Types\Structs\FoxWideVector3.cs" />
<Compile Include="Fox\Types\Values\FoxStringBase.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Tpp\Enums\TppLensFlareFieldInterpType.cs" />
<Compile Include="Tpp\Enums\TppLensFlareFieldShapeType.cs" />
<Compile Include="Tpp\Enums\TppLensFlareShapeDistanceScalingMode.cs" />
Expand Down
6 changes: 4 additions & 2 deletions FoxTool/Hashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ internal static class Hashing
internal static ulong HashString(string text)
{
if (text == null) throw new ArgumentNullException("text");
byte[] bytes = Constants.StringEncoding.GetBytes(text + "\0");
const ulong seed0 = 0x9ae16a3b2f90404f;
ulong seed1 = text.Length > 0 ? (uint) ((text[0]) << 16) + (uint) text.Length : 0;
return CityHash.CityHash.CityHash64WithSeeds(text + "\0", seed0, seed1) & 0xFFFFFFFFFFFF;
ulong seed1 = bytes.Length > 0 ? (uint) ((bytes[0]) << 16) + (uint) (bytes.Length - 1) : 0;
ulong hash = CityHash.CityHash.CityHash64WithSeeds(bytes, seed0, seed1) & 0xFFFFFFFFFFFF;
return hash;
}
}
}
4 changes: 2 additions & 2 deletions FoxTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("561aab12-c53d-4a0e-bac5-0efc04a54618")]
[assembly: AssemblyVersion("0.2.4.0")]
[assembly: AssemblyFileVersion("0.2.4.0")]
[assembly: AssemblyVersion("0.2.5.0")]
[assembly: AssemblyFileVersion("0.2.5.0")]
2 changes: 1 addition & 1 deletion FoxTool/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CityHash.Net.Legacy" version="0.1.1" targetFramework="net45" />
<package id="CityHash.Net.Legacy" version="0.1.2.0" targetFramework="net45" />
</packages>

0 comments on commit bab6e0e

Please sign in to comment.