Skip to content

Commit

Permalink
Replace binary reader with shared implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 25, 2025
1 parent f098fc1 commit 4f10102
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 138 deletions.
47 changes: 46 additions & 1 deletion Yura.Shared/IO/DataReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Yura.Shared.IO
{
Expand All @@ -13,7 +15,7 @@ public class DataReader
private readonly Endianness _endianness;

/// <summary>
/// Initializes a new data reader with the the specified stream and endianness
/// Initializes a new data reader with the specified stream and endianness
/// </summary>
/// <param name="stream">The input stream</param>
/// <param name="endianness">The endianness of the data</param>
Expand All @@ -25,6 +27,15 @@ public DataReader(Stream stream, Endianness endianness)
_endianness = endianness;
}

/// <summary>
/// Initializes a new data reader from a buffer
/// </summary>
/// <param name="data">The input buffer</param>
/// <param name="endianness">The endianness of the data</param>
public DataReader(byte[] data, Endianness endianness) : this(new MemoryStream(data), endianness)
{
}

/// <summary>
/// Gets the underlying stream
/// </summary>
Expand Down Expand Up @@ -62,6 +73,22 @@ private ReadOnlySpan<byte> InternalRead(Span<byte> data)
return data;
}

/// <summary>
/// Reads a byte from the stream
/// </summary>
/// <returns>The read byte</returns>
public byte ReadByte()
{
var value = _stream.ReadByte();

if (value == -1)
{
throw new EndOfStreamException();
}

return (byte)value;
}

/// <summary>
/// Reads a signed 16-bit integer from the stream
/// </summary>
Expand Down Expand Up @@ -124,5 +151,23 @@ public float ReadSingle()
{
return BitConverter.ToSingle(InternalRead(stackalloc byte[4]));
}

/// <summary>
/// Reads a null-terminated string from the stream
/// </summary>
/// <param name="encoding">The encoding of the string</param>
/// <returns>The read string</returns>
public string ReadString(Encoding encoding)
{
List<byte> data = [];

byte value;
while ((value = ReadByte()) != 0)
{
data.Add(value);
}

return encoding.GetString(data.ToArray());
}
}
}
2 changes: 1 addition & 1 deletion Yura.Shared/IO/DataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DataWriter
private readonly Endianness _endianness;

/// <summary>
/// Initializes a new data writer with the the specified stream and endianness
/// Initializes a new data writer with the specified stream and endianness
/// </summary>
/// <param name="stream">The output stream</param>
/// <param name="endianness">The endianness of the data</param>
Expand Down
4 changes: 2 additions & 2 deletions Yura/Formats/CMPRTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Yura.IO;
using Yura.Shared.IO;

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

for (int y = 0; y < _height; y += 8)
{
Expand Down
9 changes: 3 additions & 6 deletions Yura/Formats/DrmFile.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yura.IO;
using Yura.Shared.IO;

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

var reader = new StreamReader(data, litteEndian);
var reader = new DataReader(data, endianness);

// read file version
if (reader.ReadInt32() != 14)
Expand Down
9 changes: 5 additions & 4 deletions Yura/Formats/LocaleFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using StreamReader = Yura.IO.StreamReader;
using System.Text;
using Yura.Shared.IO;

namespace Yura.Formats
{
Expand All @@ -8,9 +9,9 @@ class LocaleFile
public Language Language { get; private set; }
public List<string> Entries { get; private set; }

public LocaleFile(byte[] data, bool litteEndian)
public LocaleFile(byte[] data, Endianness endianness)
{
var reader = new StreamReader(data, litteEndian);
var reader = new DataReader(data, endianness);

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

// read the string
var str = reader.ReadString();
var str = reader.ReadString(Encoding.UTF8);
Entries.Add(str);

reader.BaseStream.Position = cursor;
Expand Down
111 changes: 0 additions & 111 deletions Yura/IO/StreamReader.cs

This file was deleted.

10 changes: 5 additions & 5 deletions Yura/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public partial class MainWindow : Window
// dictionary of ext,file type for names and icons
private Dictionary<string, Tuple<string, BitmapImage>> _fileTypes;

private bool _littleEndian;
private Endianness _endianness;
private TextureFormat _textureFormat;
private Game _currentGame;

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

_littleEndian = settings.Endianness == Endianness.LittleEndian;
_endianness = settings.Endianness;
_textureFormat = settings.TextureFormat;
_currentGame = settings.Game;

Expand Down Expand Up @@ -385,7 +385,7 @@ private void FileView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var viewer = new TextureViewer();
viewer.TextureFormat = _textureFormat;
viewer.LittleEndian = _littleEndian;
viewer.Endianness = _endianness;

viewer.Texture = file;
viewer.Title = item.Name;
Expand All @@ -398,7 +398,7 @@ private void FileView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
if (item.Name == "locals.bin")
{
var viewer = new LocaleViewer();
viewer.LittleEndian = _littleEndian;
viewer.Endianness = _endianness;
viewer.Data = file;

viewer.Show();
Expand Down Expand Up @@ -552,7 +552,7 @@ private void SearchCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
var searchWindow = new SearchWindow() { Owner = this };
searchWindow.Archive = _bigfile;
searchWindow.LittleEndian = _littleEndian;
searchWindow.Endianness = _endianness;

searchWindow.Show();
}
Expand Down
5 changes: 3 additions & 2 deletions Yura/Windows/LocaleViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows;
using System.Windows.Input;
using Yura.Formats;
using Yura.Shared.IO;

namespace Yura
{
Expand All @@ -19,13 +20,13 @@ public LocaleViewer()
InitializeComponent();
}

public bool LittleEndian { get; set; }
public Endianness Endianness { get; set; }

public byte[] Data
{
set
{
_locale = new LocaleFile(value, LittleEndian);
_locale = new LocaleFile(value, Endianness);

// append current language to title
Title += " - " + _locale.Language.ToString();
Expand Down
5 changes: 3 additions & 2 deletions Yura/Windows/SearchWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows;
using Yura.Formats;
using Yura.Shared.Archive;
using Yura.Shared.IO;

namespace Yura
{
Expand All @@ -16,7 +17,7 @@ public partial class SearchWindow : Window
{
public ArchiveFile Archive { get; set; }

public bool LittleEndian { get; set; }
public Endianness Endianness { get; set; }

public SearchWindow()
{
Expand Down Expand Up @@ -98,7 +99,7 @@ private void SearchTask(IEnumerable<ArchiveRecord> files, int id, SectionType ty
}

// read drm file
var drm = new DrmFile(content, LittleEndian);
var drm = new DrmFile(content, Endianness);

// check sections
foreach (var section in drm.Sections)
Expand Down
Loading

0 comments on commit 4f10102

Please sign in to comment.