Skip to content

Commit

Permalink
Add locale file viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 15, 2022
1 parent bf4f308 commit e17da4b
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Yura/Formats/Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Yura.Formats
{
public enum Language
{
English,
French,
German,
Italian,
Spanish,
Japanese,
Portugese,
Polish,
British,
Russian
}
}
38 changes: 38 additions & 0 deletions Yura/Formats/LocaleFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using StreamReader = Yura.IO.StreamReader;

namespace Yura.Formats
{
class LocaleFile
{
public Language Language { get; private set; }
public List<string> Entries { get; private set; }

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

Entries = new List<string>();
Language = (Language)reader.ReadUInt32();

// read all strings
var numStrings = reader.ReadUInt32();

for (int i = 0; i < numStrings; i++)
{
// offset of the string
var offset = reader.ReadUInt32();

// preserve current cursor position
var cursor = reader.BaseStream.Position;
reader.BaseStream.Position = offset;

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

reader.BaseStream.Position = cursor;
}
}
}
}
19 changes: 19 additions & 0 deletions Yura/IO/StreamReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Yura.IO
{
Expand Down Expand Up @@ -75,6 +77,23 @@ public uint ReadUInt32()
return BitConverter.ToUInt32(data);
}

/// <summary>
/// Reads a null-terminated string from the stream
/// </summary>
/// <returns>The readed string</returns>
public string ReadString()
{
var chars = new List<byte>();

while(_stream.ReadByte() != 0)
{
_stream.Position--;
chars.Add((byte)_stream.ReadByte());
}

return Encoding.UTF8.GetString(chars.ToArray());
}

public Stream BaseStream => _stream;
}
}
36 changes: 36 additions & 0 deletions Yura/LocaleViewer.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Window x:Class="Yura.LocaleViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Yura"
mc:Ignorable="d"
Icon="Yura.ico"
Title="locals.bin" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="Save" Executed="ExportCommand_Executed"/>
</Window.CommandBindings>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Menu Grid.Row="0">
<MenuItem Header="_File">
<MenuItem Command="Save"></MenuItem>
</MenuItem>
</Menu>

<ListView Grid.Row="1" Name="Entries">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Id" DisplayMemberBinding="{Binding Index}"/>
<GridViewColumn Width="600" Header="Value" DisplayMemberBinding="{Binding Value}"/>
</GridView>
</ListView.View>
</ListView>

</Grid>
</Window>
56 changes: 56 additions & 0 deletions Yura/LocaleViewer.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Microsoft.Win32;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Yura.Formats;

namespace Yura
{
/// <summary>
/// Interaction logic for LocaleViewer.xaml
/// </summary>
public partial class LocaleViewer : Window
{
private LocaleFile _locale;

public LocaleViewer()
{
InitializeComponent();
}

public bool LittleEndian { get; set; }

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

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

// add all locale entries
Entries.ItemsSource = _locale.Entries.Select((x, i) => new LocaleViewerEntry { Index = i, Value = x });
}
}

private void ExportCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
var dialog = new SaveFileDialog();
dialog.Filter = "Text Files (*.txt)|*.txt";

if (dialog.ShowDialog() == true)
{
// write all entries 'index = value' seperated by empty line
File.WriteAllLines(dialog.FileName, _locale.Entries.Select((x, i) => $"{i} = {x}\n"));
}
}

class LocaleViewerEntry
{
public int Index { get; set; }
public string Value { get; set; }
}
}
}
11 changes: 11 additions & 0 deletions Yura/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ private void FileView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
return;
}

if (item.Name == "locals.bin")
{
var viewer = new LocaleViewer();
viewer.LittleEndian = _littleEndian;
viewer.Data = file;

viewer.Show();

return;
}

// no file handler, open file with default Windows file handler
var path = Path.Combine(Path.GetTempPath(), "Yura", item.Name);
Directory.CreateDirectory(Path.GetTempPath() + "\\Yura");
Expand Down

0 comments on commit e17da4b

Please sign in to comment.