Skip to content

Commit

Permalink
Make BAR tools aware of the motionset type
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Dec 8, 2020
1 parent 2918f55 commit 4d8ce3d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 85 deletions.
23 changes: 15 additions & 8 deletions OpenKh.Command.Bar/Core.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -11,7 +11,7 @@ public static class Core
internal class BarRoot
{
[JsonProperty] public string OriginalFileName { get; set; }

[JsonProperty] public Kh2.Bar.MotionsetType Motionset { get; set; }
[JsonProperty] public List<BarDesc> Entries { get; set; }
}

Expand All @@ -29,7 +29,7 @@ internal class BarDesc
private const string InvalidBarText = "The specified file is not a BAR file.";
internal static readonly Exception InvalidBarFileException = new InvalidDataException(InvalidBarText);

public static List<Kh2.Bar.Entry> ReadEntries(string fileName)
public static Kh2.Bar ReadEntries(string fileName)
{
using var stream = File.OpenRead(fileName);
if (!Kh2.Bar.IsValid(stream))
Expand All @@ -41,11 +41,12 @@ internal class BarDesc

public static void ExportProject(string inputFileName, string outputFolder, bool suppress = false)
{
var barEntries = ReadEntries(inputFileName);
var binarc = ReadEntries(inputFileName);
var project = new BarRoot
{
OriginalFileName = Path.GetFileName(inputFileName),
Entries = barEntries
Motionset = binarc.Motionset,
Entries = binarc
.Select(x => new BarDesc
{
FileName = $"{x.Name}.{Helpers.GetSuggestedExtension(x.Type)}",
Expand Down Expand Up @@ -92,7 +93,7 @@ public static void ExportProject(string inputFileName, string outputFolder, bool
}
}

public static IEnumerable<Kh2.Bar.Entry> ImportProject(string inputProjectName, out string originalFileName)
public static Kh2.Bar ImportProject(string inputProjectName, out string originalFileName)
{
var baseDirectory = Path.GetDirectoryName(inputProjectName);
var project = JsonConvert.DeserializeObject<BarRoot>(File.ReadAllText(inputProjectName));
Expand All @@ -103,14 +104,20 @@ public static void ExportProject(string inputFileName, string outputFolder, bool
.ToDictionary(x => x.FileName,
x => File.OpenRead(Path.Combine(baseDirectory, x.FileName)));

return project.Entries
var binarc = new Kh2.Bar
{
Motionset = project.Motionset
};
binarc.AddRange(project.Entries
.Select(x => new Kh2.Bar.Entry
{
Name = x.InternalName,
Type = (Kh2.Bar.EntryType)x.TypeId,
Index = x.LinkIndex,
Stream = x.LinkIndex == 0 ? streams[x.FileName] : null
});
}));

return binarc;
}
}
}
20 changes: 4 additions & 16 deletions OpenKh.Command.Bar/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Reflection;

namespace OpenKh.Command.Bar
Expand Down Expand Up @@ -50,16 +48,6 @@ protected int OnExecute(CommandLineApplication app)
private static string GetVersion()
=> typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

private static List<Kh2.Bar.Entry> ReadEntries(string fileName)
{
using var stream = File.OpenRead(fileName);
if (!Kh2.Bar.IsValid(stream))
throw Core.InvalidBarFileException;

stream.Position = 0;
return Kh2.Bar.Read(stream);
}

[Command(Description = "Unpack the content of a BAR file and generate a project")]
private class UnpackCommand
{
Expand Down Expand Up @@ -102,7 +90,7 @@ private class PackCommand
protected int OnExecute(CommandLineApplication app)
{
var baseDirectory = Path.GetDirectoryName(InputProject);
var bar = Core.ImportProject(InputProject, out var originalFileName);
var binarc = Core.ImportProject(InputProject, out var originalFileName);

if (string.IsNullOrEmpty(OutputFile))
OutputFile = Path.Combine(baseDirectory, originalFileName);
Expand All @@ -112,9 +100,9 @@ protected int OnExecute(CommandLineApplication app)
OutputFile = Path.Combine(OutputFile, originalFileName);

using var outputStream = File.Create(OutputFile);
Kh2.Bar.Write(outputStream, bar);
Kh2.Bar.Write(outputStream, binarc);

foreach (var entry in bar)
foreach (var entry in binarc)
entry.Stream?.Dispose();

return 0;
Expand Down
58 changes: 27 additions & 31 deletions OpenKh.Kh2/Bar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace OpenKh.Kh2
{
public static class Bar
public class Bar : List<Bar.Entry>
{
private const uint MagicCode = 0x01524142U;
private const int HeaderSize = 0x10;
Expand Down Expand Up @@ -94,6 +94,13 @@ public enum EntryType
Vag = 48,
}

public enum MotionsetType
{
Default,
Player,
Raw
}

public class Entry
{
public EntryType Type { get; set; }
Expand All @@ -107,6 +114,8 @@ public class Entry
public Stream Stream { get; set; }
}

public MotionsetType Motionset { get; set; }

public class BarContainer
{
/// <summary>
Expand All @@ -117,7 +126,7 @@ public class BarContainer
public List<Entry> Entries { get; set; } = new List<Entry>();
}

public static BarContainer ReadBarContainer(Stream stream, Func<string, EntryType, bool> filter)
public static Bar Read(Stream stream, Func<string, EntryType, bool> predicate)
{
if (!stream.CanRead || !stream.CanSeek)
throw new InvalidDataException($"Read or seek must be supported.");
Expand All @@ -126,14 +135,16 @@ public static BarContainer ReadBarContainer(Stream stream, Func<string, EntryTyp
if (stream.Length < 16L || reader.ReadUInt32() != MagicCode)
throw new InvalidDataException("Invalid header");

int filesCount = reader.ReadInt32();
int entryCount = reader.ReadInt32();
reader.ReadInt32(); // always zero
int flags = reader.ReadInt32(); // used by P_EX mset

return new BarContainer

var motionsetType = (MotionsetType)reader.ReadInt32();
var binarc = new Bar()
{
Flags = flags,
Entries = Enumerable.Range(0, filesCount)
Motionset = motionsetType
};

binarc.AddRange(Enumerable.Range(0, entryCount)
.Select(x => new
{
Type = (EntryType)reader.ReadUInt16(),
Expand All @@ -143,7 +154,7 @@ public static BarContainer ReadBarContainer(Stream stream, Func<string, EntryTyp
Size = reader.ReadInt32()
})
.ToList() // Needs to be consumed
.Where(x => filter(x.Name, x.Type))
.Where(x => predicate(x.Name, x.Type))
.Select(x =>
{
reader.BaseStream.Position = x.Offset;
Expand All @@ -163,32 +174,17 @@ public static BarContainer ReadBarContainer(Stream stream, Func<string, EntryTyp
Offset = x.Offset,
Stream = fileStream
};
})
.ToList()
};
}
}));

public static BarContainer ReadBarContainer(Stream stream)
{
return ReadBarContainer(stream, (name, type) => true);
return binarc;
}

public static List<Entry> Read(Stream stream, Func<string, EntryType, bool> filter)
{
return ReadBarContainer(stream, filter).Entries;
}
public static Bar Read(Stream stream) => Read(stream, (name, type) => true);

public static List<Entry> Read(Stream stream)
{
return Read(stream, (name, type) => true);
}

public static int Count(Stream stream, Func<string, EntryType, bool> filter)
{
return Read(stream, filter).Count;
}
public static void Write(Stream stream, Bar binarc) =>
Write(stream, binarc, binarc.Motionset);

public static void Write(Stream stream, IEnumerable<Entry> entries, int flags = 0)
public static void Write(Stream stream, IEnumerable<Entry> entries, MotionsetType motionset = MotionsetType.Default)
{
if (!stream.CanWrite || !stream.CanSeek)
throw new InvalidDataException($"Write or seek must be supported.");
Expand All @@ -199,7 +195,7 @@ public static void Write(Stream stream, IEnumerable<Entry> entries, int flags =
writer.Write(MagicCode);
writer.Write(entriesCount);
writer.Write(0);
writer.Write(flags);
writer.Write((int)motionset);

var offset = HeaderSize + entriesCount * EntrySize;
var dicLink = new Dictionary<(string name, EntryType type), (int offset, int length)>();
Expand Down
50 changes: 26 additions & 24 deletions OpenKh.Tools.BarEditor/ViewModels/BarViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpenKh.Kh2;
using OpenKh.Kh2;
using OpenKh.Tools.BarEditor.Models;
using OpenKh.Tools.BarEditor.Services;
using OpenKh.Tools.Common;
Expand All @@ -21,7 +21,9 @@ public class BarViewModel : GenericListModel<BarEntryModel>
private string _fileName;
private static readonly List<FileDialogFilter> Filters = FileDialogFilterComposer.Compose().AddAllFiles();
private readonly ToolInvokeDesc _toolInvokeDesc;
private int _barFlags;
private Bar.MotionsetType _motionsetType;

public static EnumModel<Bar.MotionsetType> MotionsetTypes { get; } = new EnumModel<Bar.MotionsetType>();

public string Title => $"{FileName ?? "untitled"} | {ApplicationName}";

Expand All @@ -35,10 +37,10 @@ public BarViewModel(ToolInvokeDesc desc) :
NewCommand = new RelayCommand(x => { }, x => false);
OpenCommand = new RelayCommand(x => { }, x => false);
SaveCommand = new RelayCommand(x =>
{
var memoryStream = new MemoryStream();

Bar.Write(memoryStream, Items.Select(item => item.Entry), BarFlags);
{
var memoryStream = new MemoryStream();

Bar.Write(memoryStream, Items.Select(item => item.Entry), MotionsetType);

var stream = _toolInvokeDesc.SelectedEntry.Stream;

Expand All @@ -63,7 +65,7 @@ public BarViewModel(IEnumerable<BarEntryModel> list) :
{
FileName = "untitled.bar";
Items.Clear();
BarFlags = 0;
MotionsetType = 0;
}, x => true);

OpenCommand = new RelayCommand(x =>
Expand Down Expand Up @@ -175,9 +177,9 @@ public void OpenFileName(string fileName)
using (var stream = File.Open(fileName, FileMode.Open))
{
Items.Clear();
var barContainer = Bar.ReadBarContainer(stream);
BarFlags = barContainer.Flags;
foreach (var item in barContainer.Entries)
var binarc = Bar.Read(stream);
MotionsetType = binarc.Motionset;
foreach (var item in binarc)
{
Items.Add(new BarEntryModel(item));
}
Expand All @@ -186,13 +188,13 @@ public void OpenFileName(string fileName)

private void SaveToFile(string fileName)
{
var memoryStream = new MemoryStream();
Bar.Write(memoryStream, Items.Select(item => item.Entry), BarFlags);
var memoryStream = new MemoryStream();
Bar.Write(memoryStream, Items.Select(item => item.Entry), MotionsetType);

using (var stream = File.Open(fileName, FileMode.Create))
{
memoryStream.Position = 0;
memoryStream.CopyTo(stream);
using (var stream = File.Open(fileName, FileMode.Create))
{
memoryStream.Position = 0;
memoryStream.CopyTo(stream);
}
}

Expand Down Expand Up @@ -229,14 +231,14 @@ public string FileName
}
}

public int BarFlags
{
get => _barFlags;
set
{
_barFlags = value;
OnPropertyChanged(nameof(BarFlags));
}
public Bar.MotionsetType MotionsetType
{
get => _motionsetType;
set
{
_motionsetType = value;
OnPropertyChanged(nameof(MotionsetType));
}
}

public RelayCommand NewCommand { get; set; }
Expand Down
15 changes: 9 additions & 6 deletions OpenKh.Tools.BarEditor/Views/BarView.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="OpenKh.Tools.BarEditor.Views.BarView"
<Window x:Class="OpenKh.Tools.BarEditor.Views.BarView"
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"
Expand Down Expand Up @@ -82,8 +82,8 @@

<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="8*"/>
</Grid.ColumnDefinitions>

<DockPanel Grid.Column="0">
Expand All @@ -105,9 +105,12 @@
</Grid>

<DockPanel DockPanel.Dock="Bottom">
<Label>Bar flags</Label>
<TextBox Text="{Binding BarFlags,UpdateSourceTrigger=PropertyChanged}"
Margin="5,5,0,5" />
<TextBlock VerticalAlignment="Center">Motionset</TextBlock>
<ComboBox Margin="5,5,0,5"
ItemsSource="{Binding MotionsetTypes, Mode=OneTime}"
SelectedValue="{Binding MotionsetType, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"
SelectedValuePath="Value"/>
</DockPanel>

<ListBox
Expand Down

0 comments on commit 4d8ce3d

Please sign in to comment.