Skip to content

Commit

Permalink
Add recent files list and ability to annotate streams
Browse files Browse the repository at this point in the history
  • Loading branch information
segabl committed Apr 30, 2021
1 parent ff5c6b7 commit 2ecf194
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
1 change: 1 addition & 0 deletions ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace PD2SoundBankEditor {
public class ApplicationSettings {
public bool checkForUpdates = true;
public DateTime lastUpdateCheck = new DateTime();
public List<string> recentlyOpenedFiles = new List<string>();
}
}
21 changes: 21 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@
<MenuItem Header="_Save" Command="Save"/>
<MenuItem Header="Save _as..." Command="SaveAs"/>
<Separator />
<MenuItem Header="Recent _Files" Name="recentFilesList">
<MenuItem.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding}"/>
<EventSetter Event="Click" Handler="OnRecentFileClick"/>
</Style>
</MenuItem.ItemContainerStyle>
<!--<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding}" Click="OnRecentFileClick"></MenuItem>
</DataTemplate>
</MenuItem.ItemTemplate>-->
</MenuItem>
<Separator />
<MenuItem Header="_Exit" Click="OnExitButtonClick"/>
</MenuItem>
<!--<MenuItem Header="_Tools">
Expand Down Expand Up @@ -72,6 +86,13 @@
</GridViewColumn>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Header="Size" DisplayMemberBinding="{Binding Size, StringFormat={}{0:0.0} KB}" />
<GridViewColumn Header="Note">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="96" Height="20" Padding="0,0,0,0" Margin="0, 0, 0, 0" Background="{x:Null}" BorderThickness="0" Text="{Binding Note}"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Replacement" DisplayMemberBinding="{Binding ReplacementFile}" />
</GridView>
</ListView.View>
Expand Down
32 changes: 32 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public MainWindow() {
}
}

recentFilesList.ItemsSource = appSettings.recentlyOpenedFiles;
recentFilesList.IsEnabled = appSettings.recentlyOpenedFiles.Count > 0;

mediaPlayer.MediaEnded += SetPlayButtonState;
}

Expand Down Expand Up @@ -128,6 +131,8 @@ private void OnOpenButtonClick(object sender, RoutedEventArgs e) {
if (diag.ShowDialog() != true) {
return;
}
soundBank?.SaveNotes();

DoGenericProcessing(false, LoadSoundBank, OnSoundBankLoaded, diag.FileName);
}

Expand Down Expand Up @@ -203,6 +208,21 @@ private void OnPlayButtonClick(object sender, RoutedEventArgs e) {
SetPlayButtonState(button, null);
}

private void OnRecentFileClick(object sender, RoutedEventArgs e) {
var menuItem = (MenuItem)sender;
var file = (string)menuItem.DataContext;

soundBank?.SaveNotes();

DoGenericProcessing(false, LoadSoundBank, OnSoundBankLoaded, file);

if (!File.Exists(file)) {
appSettings.recentlyOpenedFiles.Remove(file);
recentFilesList.ItemsSource = null; //too lazy for proper notify
recentFilesList.ItemsSource = appSettings.recentlyOpenedFiles;
}
}

private void OnListViewSelectionChanged(object sender, SelectionChangedEventArgs e) {
replaceSelectedButton.IsEnabled = converterAvailable && listView.SelectedItems.Count > 0;
extractSelectedButton.IsEnabled = converterAvailable && listView.SelectedItems.Count > 0;
Expand All @@ -220,6 +240,7 @@ private void OnListViewSizeChanged(object sender, SizeChangedEventArgs e) {
}

private void OnWindowClosed(object sender, EventArgs e) {
soundBank?.SaveNotes();
if (Directory.Exists(TEMPORARY_PATH)) {
Directory.Delete(TEMPORARY_PATH, true);
}
Expand Down Expand Up @@ -275,6 +296,7 @@ public void LoadSoundBank(object sender, DoWorkEventArgs e) {
} catch (Exception ex) {
e.Result = ex.Message;
}

}

public void OnSoundBankLoaded(object sender, RunWorkerCompletedEventArgs e) {
Expand All @@ -283,6 +305,16 @@ public void OnSoundBankLoaded(object sender, RunWorkerCompletedEventArgs e) {
return;
}

if (appSettings.recentlyOpenedFiles.Contains(soundBank.FilePath)) {
appSettings.recentlyOpenedFiles.Remove(soundBank.FilePath);
}
appSettings.recentlyOpenedFiles.Insert(0, soundBank.FilePath);
if (appSettings.recentlyOpenedFiles.Count > 10) {
appSettings.recentlyOpenedFiles.RemoveRange(10, appSettings.recentlyOpenedFiles.Count - 10);
}
recentFilesList.ItemsSource = null; //too lazy for proper notify
recentFilesList.ItemsSource = appSettings.recentlyOpenedFiles;

Title = $"PD2 Soundbank Editor - {Path.GetFileName(soundBank.FilePath)}";

var containsEmedded = soundBank.StreamInfos.Count > 0;
Expand Down
6 changes: 3 additions & 3 deletions PD2SoundBankEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<UseWPF>true</UseWPF>
<StartupObject></StartupObject>
<AssemblyName>PD2SoundBankEditor</AssemblyName>
<Version>0.8.5</Version>
<Version>0.9.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AdonisUI.ClassicTheme" Version="1.16.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="AdonisUI.ClassicTheme" Version="1.17.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>
51 changes: 45 additions & 6 deletions SoundBank.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
Expand All @@ -14,6 +15,7 @@ public class StreamInfo : INotifyPropertyChanged {

public event PropertyChangedEventHandler PropertyChanged;

public SoundBank SoundBank { get; protected set; }
public uint Id { get; private set; }
public int Offset { get; set; }
public byte[] Data {
Expand All @@ -34,7 +36,19 @@ public string ReplacementFile {
}
}

public StreamInfo(uint id, int offset, int length) {
public string Note {
get => SoundBank.StreamNotes.TryGetValue(Id, out var n) ? n : "";
set {
if (value == "")
SoundBank.StreamNotes.Remove(Id);
else
SoundBank.StreamNotes[Id] = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Note"));
}
}

public StreamInfo(SoundBank soundBank, uint id, int offset, int length) {
SoundBank = soundBank;
Id = id;
Offset = offset;
Data = new byte[length];
Expand Down Expand Up @@ -98,7 +112,7 @@ protected override void Read(BinaryReader reader, int amount) {
var id = reader.ReadUInt32();
var offset = reader.ReadInt32();
var length = reader.ReadInt32();
SoundBank.StreamInfos.Add(new StreamInfo(id, offset, length));
SoundBank.StreamInfos.Add(new StreamInfo(SoundBank, id, offset, length));
}
if (reader.BaseStream.Position != DataOffset + amount) {
throw new FileFormatException("Soundbank data is malformed.");
Expand Down Expand Up @@ -195,11 +209,13 @@ public virtual void Write(BinaryWriter writer) {
// Sound object
public class ObjectSound : ObjectBase {
public StreamInfo StreamInfo { get; protected set; }
public bool IsEmbedded { get; protected set; }
public uint AudioId { get; protected set; }

public ObjectSound(SectionHIRC section, byte type, BinaryReader reader) : base(section, type, reader) {
var embed = BitConverter.ToUInt32(Data, 8);
var audioId = BitConverter.ToUInt32(Data, 12);
StreamInfo = embed == 0 ? Section.SoundBank.StreamInfos.Find(x => x.Id == audioId) : null;
IsEmbedded = BitConverter.ToUInt32(Data, 8) == 0;
AudioId = BitConverter.ToUInt32(Data, 12);
StreamInfo = IsEmbedded ? Section.SoundBank.StreamInfos.Find(x => x.Id == AudioId) : null;
}

public override void Write(BinaryWriter writer) {
Expand Down Expand Up @@ -247,10 +263,13 @@ public override void Write(BinaryWriter writer) {
public bool IsDirty { get; set; }
public string FilePath { get; private set; }
public List<StreamInfo> StreamInfos { get; private set; } = new List<StreamInfo>();
public Dictionary<uint, string> StreamNotes { get; private set; } = new Dictionary<uint, string>();

public SoundBank(string file) {
FilePath = file;

LoadNotes();

// Read all sections
using var reader = new BinaryReader(new FileStream(FilePath, FileMode.Open));
while (reader.BaseStream.Position < reader.BaseStream.Length) {
Expand All @@ -269,5 +288,25 @@ public void Save(string file) {
FilePath = file;
IsDirty = false;
}

public void LoadNotes() {
var notesDir = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "notes");
if (!Directory.Exists(notesDir))
return;
var notesFile = Path.Join(notesDir, Path.GetFileName(FilePath) + ".json");
if (!File.Exists(notesFile))
return;
StreamNotes = JsonConvert.DeserializeObject<Dictionary<uint, string>>(File.ReadAllText(notesFile));
}

public void SaveNotes() {
if (StreamNotes.Count == 0)
return;
var notesDir = Path.Join(AppDomain.CurrentDomain.BaseDirectory, "notes");
if (!Directory.Exists(notesDir))
Directory.CreateDirectory(notesDir);
var notesFile = Path.Join(notesDir, Path.GetFileName(FilePath) + ".json");
File.WriteAllText(notesFile, JsonConvert.SerializeObject(StreamNotes));
}
}
}

0 comments on commit 2ecf194

Please sign in to comment.