Skip to content

Commit 4f10102

Browse files
committed
Replace binary reader with shared implementation
1 parent f098fc1 commit 4f10102

File tree

10 files changed

+72
-138
lines changed

10 files changed

+72
-138
lines changed

Yura.Shared/IO/DataReader.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
5+
using System.Text;
46

57
namespace Yura.Shared.IO
68
{
@@ -13,7 +15,7 @@ public class DataReader
1315
private readonly Endianness _endianness;
1416

1517
/// <summary>
16-
/// Initializes a new data reader with the the specified stream and endianness
18+
/// Initializes a new data reader with the specified stream and endianness
1719
/// </summary>
1820
/// <param name="stream">The input stream</param>
1921
/// <param name="endianness">The endianness of the data</param>
@@ -25,6 +27,15 @@ public DataReader(Stream stream, Endianness endianness)
2527
_endianness = endianness;
2628
}
2729

30+
/// <summary>
31+
/// Initializes a new data reader from a buffer
32+
/// </summary>
33+
/// <param name="data">The input buffer</param>
34+
/// <param name="endianness">The endianness of the data</param>
35+
public DataReader(byte[] data, Endianness endianness) : this(new MemoryStream(data), endianness)
36+
{
37+
}
38+
2839
/// <summary>
2940
/// Gets the underlying stream
3041
/// </summary>
@@ -62,6 +73,22 @@ private ReadOnlySpan<byte> InternalRead(Span<byte> data)
6273
return data;
6374
}
6475

76+
/// <summary>
77+
/// Reads a byte from the stream
78+
/// </summary>
79+
/// <returns>The read byte</returns>
80+
public byte ReadByte()
81+
{
82+
var value = _stream.ReadByte();
83+
84+
if (value == -1)
85+
{
86+
throw new EndOfStreamException();
87+
}
88+
89+
return (byte)value;
90+
}
91+
6592
/// <summary>
6693
/// Reads a signed 16-bit integer from the stream
6794
/// </summary>
@@ -124,5 +151,23 @@ public float ReadSingle()
124151
{
125152
return BitConverter.ToSingle(InternalRead(stackalloc byte[4]));
126153
}
154+
155+
/// <summary>
156+
/// Reads a null-terminated string from the stream
157+
/// </summary>
158+
/// <param name="encoding">The encoding of the string</param>
159+
/// <returns>The read string</returns>
160+
public string ReadString(Encoding encoding)
161+
{
162+
List<byte> data = [];
163+
164+
byte value;
165+
while ((value = ReadByte()) != 0)
166+
{
167+
data.Add(value);
168+
}
169+
170+
return encoding.GetString(data.ToArray());
171+
}
127172
}
128173
}

Yura.Shared/IO/DataWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DataWriter
1313
private readonly Endianness _endianness;
1414

1515
/// <summary>
16-
/// Initializes a new data writer with the the specified stream and endianness
16+
/// Initializes a new data writer with the specified stream and endianness
1717
/// </summary>
1818
/// <param name="stream">The output stream</param>
1919
/// <param name="endianness">The endianness of the data</param>

Yura/Formats/CMPRTexture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Windows;
33
using System.Windows.Media;
44
using System.Windows.Media.Imaging;
5-
using Yura.IO;
5+
using Yura.Shared.IO;
66

77
namespace Yura.Formats
88
{
@@ -29,7 +29,7 @@ protected override Freezable CreateInstanceCore()
2929
// most of the code below for CMPR algo is copied from there
3030
public override void CopyPixels(Int32Rect sourceRect, Array pixels, int stride, int _)
3131
{
32-
var reader = new StreamReader(_buffer, false);
32+
var reader = new DataReader(_buffer, Endianness.LittleEndian);
3333

3434
for (int y = 0; y < _height; y += 8)
3535
{

Yura/Formats/DrmFile.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Yura.IO;
3+
using Yura.Shared.IO;
74

85
namespace Yura.Formats
96
{
@@ -18,11 +15,11 @@ class DrmFile
1815
/// <param name="data">The data of the file</param>
1916
/// <param name="litteEndian">Whether the file should be read as little endian</param>
2017
/// <param name="relocate">Whether to relocate relocations, currently unsupported</param>
21-
public DrmFile(byte[] data, bool litteEndian, bool relocate = false)
18+
public DrmFile(byte[] data, Endianness endianness, bool relocate = false)
2219
{
2320
_sections = new List<Section>();
2421

25-
var reader = new StreamReader(data, litteEndian);
22+
var reader = new DataReader(data, endianness);
2623

2724
// read file version
2825
if (reader.ReadInt32() != 14)

Yura/Formats/LocaleFile.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
2-
using StreamReader = Yura.IO.StreamReader;
2+
using System.Text;
3+
using Yura.Shared.IO;
34

45
namespace Yura.Formats
56
{
@@ -8,9 +9,9 @@ class LocaleFile
89
public Language Language { get; private set; }
910
public List<string> Entries { get; private set; }
1011

11-
public LocaleFile(byte[] data, bool litteEndian)
12+
public LocaleFile(byte[] data, Endianness endianness)
1213
{
13-
var reader = new StreamReader(data, litteEndian);
14+
var reader = new DataReader(data, endianness);
1415

1516
Entries = new List<string>();
1617
Language = (Language)reader.ReadUInt32();
@@ -28,7 +29,7 @@ public LocaleFile(byte[] data, bool litteEndian)
2829
reader.BaseStream.Position = offset;
2930

3031
// read the string
31-
var str = reader.ReadString();
32+
var str = reader.ReadString(Encoding.UTF8);
3233
Entries.Add(str);
3334

3435
reader.BaseStream.Position = cursor;

Yura/IO/StreamReader.cs

Lines changed: 0 additions & 111 deletions
This file was deleted.

Yura/MainWindow.xaml.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public partial class MainWindow : Window
3636
// dictionary of ext,file type for names and icons
3737
private Dictionary<string, Tuple<string, BitmapImage>> _fileTypes;
3838

39-
private bool _littleEndian;
39+
private Endianness _endianness;
4040
private TextureFormat _textureFormat;
4141
private Game _currentGame;
4242

@@ -135,7 +135,7 @@ public void OpenBigfile(string bigfile, IFileSettings settings)
135135
{
136136
var list = (settings.FileList == null) ? null : new FileList(settings.FileList, settings.Game != Game.Tiger);
137137

138-
_littleEndian = settings.Endianness == Endianness.LittleEndian;
138+
_endianness = settings.Endianness;
139139
_textureFormat = settings.TextureFormat;
140140
_currentGame = settings.Game;
141141

@@ -385,7 +385,7 @@ private void FileView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
385385
{
386386
var viewer = new TextureViewer();
387387
viewer.TextureFormat = _textureFormat;
388-
viewer.LittleEndian = _littleEndian;
388+
viewer.Endianness = _endianness;
389389

390390
viewer.Texture = file;
391391
viewer.Title = item.Name;
@@ -398,7 +398,7 @@ private void FileView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
398398
if (item.Name == "locals.bin")
399399
{
400400
var viewer = new LocaleViewer();
401-
viewer.LittleEndian = _littleEndian;
401+
viewer.Endianness = _endianness;
402402
viewer.Data = file;
403403

404404
viewer.Show();
@@ -552,7 +552,7 @@ private void SearchCommand_Executed(object sender, ExecutedRoutedEventArgs e)
552552
{
553553
var searchWindow = new SearchWindow() { Owner = this };
554554
searchWindow.Archive = _bigfile;
555-
searchWindow.LittleEndian = _littleEndian;
555+
searchWindow.Endianness = _endianness;
556556

557557
searchWindow.Show();
558558
}

Yura/Windows/LocaleViewer.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows;
55
using System.Windows.Input;
66
using Yura.Formats;
7+
using Yura.Shared.IO;
78

89
namespace Yura
910
{
@@ -19,13 +20,13 @@ public LocaleViewer()
1920
InitializeComponent();
2021
}
2122

22-
public bool LittleEndian { get; set; }
23+
public Endianness Endianness { get; set; }
2324

2425
public byte[] Data
2526
{
2627
set
2728
{
28-
_locale = new LocaleFile(value, LittleEndian);
29+
_locale = new LocaleFile(value, Endianness);
2930

3031
// append current language to title
3132
Title += " - " + _locale.Language.ToString();

Yura/Windows/SearchWindow.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Windows;
77
using Yura.Formats;
88
using Yura.Shared.Archive;
9+
using Yura.Shared.IO;
910

1011
namespace Yura
1112
{
@@ -16,7 +17,7 @@ public partial class SearchWindow : Window
1617
{
1718
public ArchiveFile Archive { get; set; }
1819

19-
public bool LittleEndian { get; set; }
20+
public Endianness Endianness { get; set; }
2021

2122
public SearchWindow()
2223
{
@@ -98,7 +99,7 @@ private void SearchTask(IEnumerable<ArchiveRecord> files, int id, SectionType ty
9899
}
99100

100101
// read drm file
101-
var drm = new DrmFile(content, LittleEndian);
102+
var drm = new DrmFile(content, Endianness);
102103

103104
// check sections
104105
foreach (var section in drm.Sections)

0 commit comments

Comments
 (0)