diff --git a/OpenKh.Kh2/Places.cs b/OpenKh.Kh2/Places.cs index 6308fa0a7..dce5b99d6 100644 --- a/OpenKh.Kh2/Places.cs +++ b/OpenKh.Kh2/Places.cs @@ -7,11 +7,17 @@ namespace OpenKh.Kh2 { public class Place { + /// + /// Refers to sys.bar + /// public ushort MessageId { get; set; } - public List SubPlaces { get; set; } + /// + /// Place name to be decoded as Shift-JIS. + /// + public byte[] Name { get; set; } - private static IEnumerable ReadPlaceSubEntries(Stream stream) + private static IEnumerable ReadPlaceSubEntries(Stream stream) { while (true) { @@ -19,7 +25,7 @@ private static IEnumerable ReadPlaceSubEntries(Stream stream) if (ch <= 0) yield break; - yield return (ushort)(ch | (stream.ReadByte() << 8)); + yield return (byte)ch; } } @@ -41,22 +47,22 @@ private static IEnumerable 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(); + 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() }; } @@ -69,17 +75,15 @@ private static void Write(Stream stream, List 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); } } diff --git a/OpenKh.Tests/kh2/PlaceTests.cs b/OpenKh.Tests/kh2/PlaceTests.cs index cc3315b8c..698e1cefd 100644 --- a/OpenKh.Tests/kh2/PlaceTests.cs +++ b/OpenKh.Tests/kh2/PlaceTests.cs @@ -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] @@ -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; }); }); diff --git a/OpenKh.Tools.Kh2PlaceEditor/ViewModels/PlaceViewModel.cs b/OpenKh.Tools.Kh2PlaceEditor/ViewModels/PlaceViewModel.cs index 653380991..6ca0f89c1 100644 --- a/OpenKh.Tools.Kh2PlaceEditor/ViewModels/PlaceViewModel.cs +++ b/OpenKh.Tools.Kh2PlaceEditor/ViewModels/PlaceViewModel.cs @@ -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; @@ -22,6 +26,8 @@ public PlaceViewModel(IMessageProvider messageProvider, public Place Place { get; } + public string Map => $"{World}{_index:D02}"; + public short MessageId { get => (short)(Place.MessageId & 0x7fff); @@ -29,12 +35,22 @@ public short MessageId { 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)); + } } } diff --git a/OpenKh.Tools.Kh2PlaceEditor/Views/PlaceEditorWindow.xaml b/OpenKh.Tools.Kh2PlaceEditor/Views/PlaceEditorWindow.xaml index 4f09a36b6..ed397aaed 100644 --- a/OpenKh.Tools.Kh2PlaceEditor/Views/PlaceEditorWindow.xaml +++ b/OpenKh.Tools.Kh2PlaceEditor/Views/PlaceEditorWindow.xaml @@ -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"> diff --git a/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml b/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml index b8a1ecead..819debd4a 100644 --- a/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml +++ b/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml @@ -6,31 +6,16 @@ xmlns:local="clr-namespace:OpenKh.Tools.Kh2PlaceEditor.Views" mc:Ignorable="d" d:DesignHeight="350" d:DesignWidth="400"> - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + diff --git a/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml.cs b/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml.cs index 20f012d95..ac7157622 100644 --- a/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml.cs +++ b/OpenKh.Tools.Kh2PlaceEditor/Views/PlacesView.xaml.cs @@ -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 {