Skip to content

Commit

Permalink
Read place name information correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed May 13, 2020
1 parent b6e73b0 commit b62b403
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 71 deletions.
34 changes: 19 additions & 15 deletions OpenKh.Kh2/Places.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ namespace OpenKh.Kh2
{
public class Place
{
/// <summary>
/// Refers to sys.bar
/// </summary>
public ushort MessageId { get; set; }

public List<ushort> SubPlaces { get; set; }
/// <summary>
/// Place name to be decoded as Shift-JIS.
/// </summary>
public byte[] Name { get; set; }

private static IEnumerable<ushort> ReadPlaceSubEntries(Stream stream)
private static IEnumerable<byte> ReadPlaceSubEntries(Stream stream)
{
while (true)
{
var ch = stream.ReadByte();
if (ch <= 0)
yield break;

yield return (ushort)(ch | (stream.ReadByte() << 8));
yield return (byte)ch;
}
}

Expand All @@ -41,22 +47,22 @@ private static IEnumerable<Place> ReadPlaceEntry(Stream stream)

for (var i = 0; i < placeCount - 1; i++)
{
var hasSubPlaces = offsetIds[i] != offsetIds[i + 1];
var subPlaces = hasSubPlaces ?
ReadPlaceSubEntries(stream.SetPosition(offsetIds[i])).ToList() :
new List<ushort>();
var hasName = offsetIds[i] != offsetIds[i + 1];
var name = hasName ?
ReadPlaceSubEntries(stream.SetPosition(offsetIds[i])).ToArray() :
new byte[0];

yield return new Place
{
MessageId = placeIds[i],
SubPlaces = subPlaces
Name = name
};
}

yield return new Place
{
MessageId = placeIds[placeCount - 1],
SubPlaces = ReadPlaceSubEntries(stream.SetPosition(offsetIds[placeCount - 1])).ToList()
Name = ReadPlaceSubEntries(stream.SetPosition(offsetIds[placeCount - 1])).ToArray()
};
}

Expand All @@ -69,17 +75,15 @@ private static void Write(Stream stream, List<Place> places)
stream.Write((ushort)endOfFile);

endOfFile -= 4;
if (place.SubPlaces.Count > 0)
endOfFile += place.SubPlaces.Count * 2 + 1;
if (place.Name.Length > 0)
endOfFile += place.Name.Length + 1;
}

foreach (var place in places)
{
// If no subplaces are found, then it's a place.bin and not a 00place.bin
if (place.SubPlaces.Count > 0)
if (place.Name.Length > 0)
{
foreach (var subplace in place.SubPlaces)
stream.Write(subplace);
stream.Write(place.Name);
stream.Write((byte)0);
}
}
Expand Down
15 changes: 4 additions & 11 deletions OpenKh.Tests/kh2/PlaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ public void HasWorldTheRightAmountOfPlaces(string fileName) => Common.FileOpenRe
});

[Fact]
public void ReadSubPlaces() => Common.FileOpenRead(OldPlaceFileName, stream =>
public void ReadPlaceName() => Common.FileOpenRead(OldPlaceFileName, stream =>
{
var places = Place.Read(stream)["bb"];
Assert.Equal(9, places[0].SubPlaces.Count);
Assert.Equal(0x4783, places[0].SubPlaces[0]);
Assert.Equal(0x8B83, places[0].SubPlaces[8]);
Assert.Single(places[15].SubPlaces);
Assert.Equal(0xB48B, places[15].SubPlaces[0]);
Assert.Equal(18, places[0].Name.Length);
Assert.Equal(0x83, places[0].Name[0]);
Assert.Equal(0x8B, places[0].Name[17]);
});

[Theory]
Expand All @@ -59,10 +56,6 @@ public void WriteBackTheSameFile(string fileName) => Common.FileOpenRead(fileNam
var outStream = new MemoryStream();
Place.Write(outStream, Place.Read(inStream));
outStream.Position = 0;
using var tmp = File.Create("D:/place.bin");
outStream.CopyTo(tmp);
return outStream;
});
});
Expand Down
22 changes: 19 additions & 3 deletions OpenKh.Tools.Kh2PlaceEditor/ViewModels/PlaceViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using OpenKh.Engine;
using OpenKh.Kh2;
using System.Text;
using Xe.Tools;

namespace OpenKh.Tools.Kh2PlaceEditor.ViewModels
{
public class PlaceViewModel : BaseNotifyPropertyChanged
{
private const int ShiftJisCodepage = 932;
private readonly Encoding Encoding = Encoding.GetEncoding(ShiftJisCodepage);

private readonly IMessageProvider _messageProvider;
private readonly int _index;

Expand All @@ -22,19 +26,31 @@ public PlaceViewModel(IMessageProvider messageProvider,

public Place Place { get; }

public string Map => $"{World}{_index:D02}";

public short MessageId
{
get => (short)(Place.MessageId & 0x7fff);
set
{
Place.MessageId = (ushort)(value | 0x8000);
OnPropertyChanged(nameof(Name));
OnPropertyChanged(nameof(Message));
}
}

public string Name
{
get => Encoding.GetString(Place.Name);
set => Place.Name = Encoding.GetBytes(value);
}

public string Name =>
$"{World}{_index:D02} | {MessageId:D5} | {_messageProvider.GetMessage(Place.MessageId)}";
public string Message => _messageProvider.GetMessage(Place.MessageId);

public void RefreshMessages() => OnPropertyChanged(nameof(Name));
public void RefreshMessages()
{
OnPropertyChanged(nameof(Name));
OnPropertyChanged(nameof(Message));
}
}
}
2 changes: 1 addition & 1 deletion OpenKh.Tools.Kh2PlaceEditor/Views/PlaceEditorWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:OpenKh.Tools.Kh2PlaceEditor.Views"
xmlns:converters="clr-namespace:OpenKh.Tools.Kh2PlaceEditor.Converters"
mc:Ignorable="d"
Title="{Binding Title}" Height="570" Width="800">
Title="{Binding Title}" Height="500" Width="800">
<Window.Resources>
<converters:EnumMatchToBooleanConverter x:Key="enumConverter" />
</Window.Resources>
Expand Down
39 changes: 12 additions & 27 deletions OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,16 @@
xmlns:local="clr-namespace:OpenKh.Tools.Kh2PlaceEditor.Views"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>

<ListBox
Grid.Column="0"
ItemsSource="{Binding Items}"
DisplayMemberPath="Name"/>

<StackPanel Grid.Column="2">
<TextBlock Text="Map name"/>
<Grid Margin="0 0 0 5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="3"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<TextBlock Grid.Column="0" Text="{Binding MessageId}"/>
<TextBlock Grid.Column="2" Text="{Binding Name}"/>
</Grid>
</StackPanel>

</Grid>
<DataGrid ItemsSource="{Binding Items.Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Map" Width="Auto"
Binding="{Binding Map, Mode=OneWay}" IsReadOnly="True"/>
<DataGridTextColumn Header="Msg ID" Width="Auto"
Binding="{Binding MessageId}"/>
<DataGridTextColumn Header="Name" Width="*"
Binding="{Binding Name, Mode=TwoWay}"/>
<DataGridTextColumn Header="Message name" Width="*"
Binding="{Binding Message, Mode=OneWay}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</UserControl>
15 changes: 1 addition & 14 deletions OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls;

namespace OpenKh.Tools.Kh2PlaceEditor.Views
{
Expand Down

0 comments on commit b62b403

Please sign in to comment.