Skip to content

Commit

Permalink
- Fixed TLKs being loaded and saved with wrong encodings
Browse files Browse the repository at this point in the history
- Fixed bug that occured when editing multiple objects of the same type
  • Loading branch information
Cjreek committed Apr 3, 2023
1 parent a18dd24 commit ae1e021
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 17 deletions.
2 changes: 2 additions & 0 deletions eos-edit/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Nwn.Tga;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Eos
{
Expand All @@ -25,6 +26,7 @@ public override void Initialize()

public override void OnFrameworkInitializationCompleted()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.Startup += Desktop_Startup;
Expand Down
2 changes: 1 addition & 1 deletion eos-edit/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Eos.Views.MainWindow" MinHeight="300" MinWidth="500"
Height="700" Width="1100" Loaded="mainWindow_Loaded"
x:Name="mainWindow" Tag="Eos Toolset v0.63c" FontFamily="Segoe UI"
x:Name="mainWindow" Tag="Eos Toolset v0.63d" FontFamily="Segoe UI"
Closing="mainWindow_Closing"
Icon="/Assets/Icons/Eos.ico">
<Window.Title>
Expand Down
1 change: 1 addition & 0 deletions eos-edit/eos-edit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.999-cibuild0031415-beta" />
<PackageReference Include="Avalonia.Themes.Simple" Version="11.0.999-cibuild0031415-beta" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.999-build20230226-01" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.6.1" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions eos/Models/Tables/CustomObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ public CustomObject() : base()

private void InstanceRepository_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if ((e.Action == NotifyCollectionChangedAction.Reset) || (e.Action == NotifyCollectionChangedAction.Move))
NotifyPropertyChanged(nameof(InstanceRepository));
// Suddenly works ??
//if ((e.Action == NotifyCollectionChangedAction.Reset) || (e.Action == NotifyCollectionChangedAction.Move))
// NotifyPropertyChanged(nameof(InstanceRepository));
}

protected override void SetDefaultValues()
Expand Down
4 changes: 2 additions & 2 deletions eos/Nwn/BinaryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public static String ReadString(BinaryReader stream, int length)
return new String(stream.ReadChars(length)).Trim('\0');
}

public static void WriteString(BinaryWriter stream, String data, bool writeNullbyte = true)
public static void WriteString(BinaryWriter stream, String data, Encoding encoding, bool writeNullbyte = true)
{
stream.Write(data.ToCharArray());
stream.Write(encoding.GetBytes(data));
if (writeNullbyte)
stream.Write('\0');
}
Expand Down
18 changes: 16 additions & 2 deletions eos/Nwn/Erf/ErfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ private class ErfResourceKey
private Dictionary<(string, NWNResourceType), ErfResourceKey> resourceKeys = new Dictionary<(string, NWNResourceType), ErfResourceKey>();
private List<ErfResource> resources = new List<ErfResource>();

private Dictionary<TLKLanguage, Encoding> languageEncodings = new Dictionary<TLKLanguage, Encoding>
{
{ TLKLanguage.English, Encoding.GetEncoding(1252) },
{ TLKLanguage.French, Encoding.GetEncoding(1252) },
{ TLKLanguage.German, Encoding.GetEncoding(1252) },
{ TLKLanguage.Italian, Encoding.GetEncoding(1252) },
{ TLKLanguage.Spanish, Encoding.GetEncoding(1252) },
{ TLKLanguage.Polish, Encoding.GetEncoding(1250) },
{ TLKLanguage.Korean, Encoding.GetEncoding(949) },
{ TLKLanguage.ChineseTraditional, Encoding.GetEncoding(950) },
{ TLKLanguage.ChineseSimplified, Encoding.GetEncoding(936) },
{ TLKLanguage.Japanese, Encoding.GetEncoding(932) },
};

public int Count => resources.Count;
public ErfResource this[int index] => resources[index];

Expand Down Expand Up @@ -180,7 +194,7 @@ public void Save(Stream stream, ErfType type = ErfType.HAK)
strElement.LanguageID = ((UInt32)lang) * 2;
strElement.StringSize = (UInt32)Description[lang].Text.Length + 1;
BinaryHelper.Write(writer, strElement);
BinaryHelper.WriteString(writer, Description[lang].Text);
BinaryHelper.WriteString(writer, Description[lang].Text, languageEncodings[lang]);

strSegmentSize += (UInt32)(Marshal.SizeOf<ErfStringElement>() + strElement.StringSize);
langCount++;
Expand All @@ -192,7 +206,7 @@ public void Save(Stream stream, ErfType type = ErfType.HAK)
strElement.LanguageID = ((UInt32)lang) * 2 + 1;
strElement.StringSize = (UInt32)Description[lang].TextF.Length + 1;
BinaryHelper.Write(writer, strElement);
BinaryHelper.WriteString(writer, Description[lang].TextF);
BinaryHelper.WriteString(writer, Description[lang].TextF, languageEncodings[lang]);

strSegmentSize += (UInt32)(Marshal.SizeOf<ErfStringElement>() + strElement.StringSize);
langCount++;
Expand Down
2 changes: 1 addition & 1 deletion eos/Nwn/Ssf/SsfFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Load(String filename)

public void Load(Stream stream)
{
var reader = new BinaryReader(stream, Encoding.Latin1);
var reader = new BinaryReader(stream, Encoding.GetEncoding(1252));
var header = BinaryHelper.Read<SsfHeader>(reader);

var offsetArray = new int[header.EntryCount];
Expand Down
34 changes: 27 additions & 7 deletions eos/Nwn/Tlk/TlkFile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Eos.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -49,6 +50,19 @@ internal class TlkFile

private Dictionary<int, String> cache = new Dictionary<int, string>();
private List<String> strings = new List<string>();
private Dictionary<TLKLanguage, Encoding> languageEncodings = new Dictionary<TLKLanguage, Encoding>
{
{ TLKLanguage.English, Encoding.GetEncoding(1252) },
{ TLKLanguage.French, Encoding.GetEncoding(1252) },
{ TLKLanguage.German, Encoding.GetEncoding(1252) },
{ TLKLanguage.Italian, Encoding.GetEncoding(1252) },
{ TLKLanguage.Spanish, Encoding.GetEncoding(1252) },
{ TLKLanguage.Polish, Encoding.GetEncoding(1250) },
{ TLKLanguage.Korean, Encoding.GetEncoding(949) },
{ TLKLanguage.ChineseTraditional, Encoding.GetEncoding(950) },
{ TLKLanguage.ChineseSimplified, Encoding.GetEncoding(936) },
{ TLKLanguage.Japanese, Encoding.GetEncoding(932) },
};

public void New(TLKLanguage language)
{
Expand All @@ -61,10 +75,14 @@ public void New(TLKLanguage language)

public void Load(String tlkFile, bool writeable = false)
{
fileStream = new FileStream(tlkFile, FileMode.Open, FileAccess.Read);
reader = new BinaryReader(fileStream, Encoding.Latin1);
header = BinaryHelper.Read<TlkHeader>(reader);
using (fileStream = new FileStream(tlkFile, FileMode.Open, FileAccess.Read))
{
reader = new BinaryReader(fileStream, Encoding.Default);
header = BinaryHelper.Read<TlkHeader>(reader);
};

fileStream = new FileStream(tlkFile, FileMode.Open, FileAccess.Read);
reader = new BinaryReader(fileStream, languageEncodings[header.LanguageId]);
if (writeable)
{
int lastNonNullIndex = -1;
Expand All @@ -74,7 +92,7 @@ public void Load(String tlkFile, bool writeable = false)
lastNonNullIndex = i;
}

for (int i=0; i <= lastNonNullIndex; i++)
for (int i = 0; i <= lastNonNullIndex; i++)
{
strings.Add(GetString(i));
}
Expand Down Expand Up @@ -154,15 +172,17 @@ public void Save(Stream stream)
strEntry.VolumeVariance = 0;
strEntry.PitchVariance = 0;
strEntry.OffsetToString = offset;
strEntry.StringSize = (UInt32)strings[i].Length;
strEntry.StringSize = (UInt32)languageEncodings[header.LanguageId].GetByteCount(strings[i]); //(UInt32)strings[i].Length;
strEntry.SoundLength = 0;
BinaryHelper.Write(writer, strEntry);

offset += strEntry.StringSize;
}

for (int i = 0; i < strings.Count; i++)
BinaryHelper.WriteString(writer, strings[i], false);
{
BinaryHelper.WriteString(writer, strings[i], languageEncodings[header.LanguageId], false);
}
}

public void Save(String filename)
Expand Down
3 changes: 1 addition & 2 deletions eos/Repositories/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ protected void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)

private void Item_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, sender, 0, 0)); // 0 -> 0 Dummy. Works
//RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

protected virtual void Changed()
Expand Down

0 comments on commit ae1e021

Please sign in to comment.