Code cleanup and making use of C# 7.0 features

This commit is contained in:
Skyth 2017-03-09 20:48:17 +03:00
parent 96c65d2d33
commit c7069bff58
24 changed files with 410 additions and 498 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
[Rr]elease/
[Dd]ebugPublic/ [Dd]ebugPublic/
[Rr]eleases/ [Rr]eleases/
[Xx]64/ [Xx]64/

View File

@ -1,8 +1,12 @@
# Sonic Audio Tools # Sonic Audio Tools
Sonic Audio Tools is a set of tools for editing the audio formats seen in Sonic the Hedgehog games. Currently, it's mostly focused on CRIWARE's audio formats, this means that these tools can be used to edit files from any game, as long as they are CSB, CPK, ACB or AWB. Sonic Audio Tools is a set of tools for editing the audio formats seen in Sonic the Hedgehog games. Currently, it's mostly focused on CRIWARE's audio formats, this means that these tools can be used to edit files from any game, as long as they are CSB, CPK, ACB or AWB.
## Releases
[Here.](https://ci.appveyor.com/project/blueskythlikesclouds/sonicaudiotools)
You can get the binaries from Artifacts tab.
## Building ## Building
1. Clone from [GitHub](https://github.com/blueskythlikesclouds/SonicAudioTools.git) `git clone https://github.com/blueskythlikesclouds/SonicAudioTools.git` 1. Clone from [GitHub](https://github.com/blueskythlikesclouds/SonicAudioTools.git) `git clone https://github.com/blueskythlikesclouds/SonicAudioTools.git`
2. Open the solution in Visual Studio. (Visual Studio 2015 or later is required.) 2. Open the solution in Visual Studio. (Visual Studio 2017 or later is required.)
3. Install the missing NuGet packages. 3. Install the missing NuGet packages.
4. Build the solution. 4. Build the solution.
@ -17,8 +21,5 @@ A fully functional CSB creator. It's currently able to load any CSB file as long
### [CsbEditor](https://github.com/blueskythlikesclouds/SonicAudioTools/tree/master/Source/CsbEditor) ### [CsbEditor](https://github.com/blueskythlikesclouds/SonicAudioTools/tree/master/Source/CsbEditor)
A tool to replace audio data in CSB files. Handles both CSBs and CPKs. A tool to replace audio data in CSB files. Handles both CSBs and CPKs.
## Releases
The compiled executables can be found in the [Release](https://github.com/blueskythlikesclouds/SonicAudioTools/tree/master/Release) directory. This directory will be removed when the projects get released under the [Releases](https://github.com/blueskythlikesclouds/SonicAudioTools/releases) page.
## License ## License
See [LICENSE.txt](https://github.com/blueskythlikesclouds/SonicAudioTools/blob/master/LICENSE.txt) See [LICENSE.txt](https://github.com/blueskythlikesclouds/SonicAudioTools/blob/master/LICENSE.txt)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,7 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14 # Visual Studio 15
VisualStudioVersion = 14.0.25420.1 VisualStudioVersion = 15.0.26228.4
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SonicAudioLib", "Source\SonicAudioLib\SonicAudioLib.csproj", "{63138773-1F47-474C-9345-15EB6183ECC6}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SonicAudioLib", "Source\SonicAudioLib\SonicAudioLib.csproj", "{63138773-1F47-474C-9345-15EB6183ECC6}"
EndProject EndProject

View File

@ -26,7 +26,7 @@ namespace AcbEditor
if (args[0].EndsWith(".acb")) if (args[0].EndsWith(".acb"))
{ {
string baseDirectory = Path.GetDirectoryName(args[0]); string baseDirectory = Path.GetDirectoryName(args[0]);
string outputDirectoryPath = Path.Combine(baseDirectory, Path.GetFileNameWithoutExtension(args[0])); string outputDirectoryPath = Path.ChangeExtension(args[0], null);
string extAfs2ArchivePath = string.Empty; string extAfs2ArchivePath = string.Empty;
Directory.CreateDirectory(outputDirectoryPath); Directory.CreateDirectory(outputDirectoryPath);
@ -87,7 +87,7 @@ namespace AcbEditor
if (!found) if (!found)
{ {
throw new Exception("Cannot find the external .AWB file for this .ACB file. Please ensure that the external .AWB file is stored in the directory where the .ACB file is."); throw new FileNotFoundException("Cannot find the external .AWB file for this .ACB file. Please ensure that the external .AWB file is stored in the directory where the .ACB file is.");
} }
} }
@ -358,9 +358,7 @@ namespace AcbEditor
static bool CheckIfAfs2(Stream source) static bool CheckIfAfs2(Stream source)
{ {
long oldPosition = source.Position; long oldPosition = source.Position;
bool result = false; bool result = EndianStream.ReadCString(source, 4) == "AFS2";
result = EndianStream.ReadCString(source, 4) == "AFS2";
source.Seek(oldPosition, SeekOrigin.Begin); source.Seek(oldPosition, SeekOrigin.Begin);
return result; return result;

View File

@ -120,10 +120,7 @@ namespace CsbBuilder.Audio
short secondHistory1 = 0; short secondHistory1 = 0;
short secondHistory2 = 0; short secondHistory2 = 0;
short coef1 = 0; CalculateCoefficients(header.CutoffFrequency, header.SampleRate, out short coef1, out short coef2);
short coef2 = 0;
CalculateCoefficients(header.CutoffFrequency, header.SampleRate, out coef1, out coef2);
source.Seek(header.DataPosition + 4, SeekOrigin.Begin); source.Seek(header.DataPosition + 4, SeekOrigin.Begin);

View File

@ -290,39 +290,35 @@ namespace CsbBuilder
private void RemoveNode(BuilderBaseNode node) private void RemoveNode(BuilderBaseNode node)
{ {
if (node is BuilderCueNode) if (node is BuilderCueNode cueNode)
{ {
project.CueNodes.Remove((BuilderCueNode)node); project.CueNodes.Remove(cueNode);
} }
else if (node is BuilderSynthNode) else if (node is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)node;
project.SynthNodes.Remove(synthNode); project.SynthNodes.Remove(synthNode);
project.CueNodes.Where(cue => cue.SynthReference == synthNode.Name).ToList().ForEach(cue => cue.SynthReference = string.Empty); project.CueNodes.Where(cue => cue.SynthReference == synthNode.Name).ToList().ForEach(cue => cue.SynthReference = string.Empty);
project.SynthNodes.Where(synth => synth.Children.Contains(synthNode.Name)).ToList().ForEach(synth => synth.Children.Remove(synthNode.Name)); project.SynthNodes.Where(synth => synth.Children.Contains(synthNode.Name)).ToList().ForEach(synth => synth.Children.Remove(synthNode.Name));
} }
else if (node is BuilderSoundElementNode) else if (node is BuilderSoundElementNode soundElementNode)
{ {
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)node;
project.SoundElementNodes.Remove(soundElementNode); project.SoundElementNodes.Remove(soundElementNode);
project.SynthNodes.Where(soundElement => soundElement.SoundElementReference == soundElementNode.Name).ToList().ForEach(synth => synth.SoundElementReference = string.Empty); project.SynthNodes.Where(soundElement => soundElement.SoundElementReference == soundElementNode.Name).ToList().ForEach(synth => synth.SoundElementReference = string.Empty);
} }
else if (node is BuilderAisacNode) else if (node is BuilderAisacNode aisacNode)
{ {
BuilderAisacNode aisacNode = (BuilderAisacNode)node;
project.AisacNodes.Remove(aisacNode); project.AisacNodes.Remove(aisacNode);
project.SynthNodes.Where(synth => synth.AisacReference == aisacNode.Name).ToList().ForEach(synth => synth.AisacReference = string.Empty); project.SynthNodes.Where(synth => synth.AisacReference == aisacNode.Name).ToList().ForEach(synth => synth.AisacReference = string.Empty);
} }
else if (node is BuilderVoiceLimitGroupNode) else if (node is BuilderVoiceLimitGroupNode voiceLimitGroupNode)
{ {
BuilderVoiceLimitGroupNode voiceLimitGroupNode = (BuilderVoiceLimitGroupNode)node;
project.VoiceLimitGroupNodes.Remove(voiceLimitGroupNode); project.VoiceLimitGroupNodes.Remove(voiceLimitGroupNode);
project.SynthNodes.Where(synth => synth.VoiceLimitGroupReference == voiceLimitGroupNode.Name).ToList().ForEach(voiceLimitGroup => voiceLimitGroup.VoiceLimitGroupReference = string.Empty); project.SynthNodes.Where(synth => synth.VoiceLimitGroupReference == voiceLimitGroupNode.Name).ToList().ForEach(voiceLimitGroup => voiceLimitGroup.VoiceLimitGroupReference = string.Empty);
@ -349,10 +345,8 @@ namespace CsbBuilder
treeNode.Tag = synthNode; treeNode.Tag = synthNode;
project.SynthNodes.Add(synthNode); project.SynthNodes.Add(synthNode);
if (parent != null && parent.Tag is BuilderSynthNode) if (parent != null && parent.Tag is BuilderSynthNode parentSynthNode)
{ {
BuilderSynthNode parentSynthNode = (BuilderSynthNode)parent.Tag;
if (parentSynthNode.Type == BuilderSynthType.WithChildren) if (parentSynthNode.Type == BuilderSynthType.WithChildren)
{ {
parentSynthNode.Children.Add(synthNode.Name); parentSynthNode.Children.Add(synthNode.Name);
@ -385,10 +379,8 @@ namespace CsbBuilder
treeNode.Tag = synthNode; treeNode.Tag = synthNode;
project.SynthNodes.Add(synthNode); project.SynthNodes.Add(synthNode);
if (parent != null && parent.Tag is BuilderSynthNode) if (parent != null && parent.Tag is BuilderSynthNode parentSynthNode)
{ {
BuilderSynthNode parentSynthNode = (BuilderSynthNode)parent.Tag;
if (parentSynthNode.Type == BuilderSynthType.WithChildren) if (parentSynthNode.Type == BuilderSynthType.WithChildren)
{ {
parentSynthNode.Children.Add(synthNode.Name); parentSynthNode.Children.Add(synthNode.Name);
@ -522,9 +514,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == cueTree) if (selectedTree == cueTree)
@ -534,7 +525,7 @@ namespace CsbBuilder
else if (selectedTree == synthTree) else if (selectedTree == synthTree)
{ {
if (selectedNode.Tag is BuilderSynthNode && ((BuilderSynthNode)selectedNode.Tag).Type == BuilderSynthType.WithChildren) if (selectedNode.Tag is BuilderSynthNode synthNode && synthNode.Type == BuilderSynthType.WithChildren)
{ {
CreateSoundNode(selectedNode.Nodes, selectedNode); CreateSoundNode(selectedNode.Nodes, selectedNode);
} }
@ -566,10 +557,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
if (selectedTree == cueTree) if (selectedTree == cueTree)
{ {
CreateCueNode(selectedTree.Nodes, null); CreateCueNode(selectedTree.Nodes, null);
@ -601,10 +590,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
if (selectedTree == synthTree) if (selectedTree == synthTree)
{ {
CreateSynthFolder(selectedTree.Nodes); CreateSynthFolder(selectedTree.Nodes);
@ -621,9 +608,9 @@ namespace CsbBuilder
{ {
foreach (TreeNode node in collection) foreach (TreeNode node in collection)
{ {
if (node.Tag is BuilderBaseNode) if (node.Tag is BuilderBaseNode baseNode)
{ {
RemoveNode((BuilderBaseNode)node.Tag); RemoveNode(baseNode);
} }
RemoveNodes(node.Nodes); RemoveNodes(node.Nodes);
@ -634,9 +621,9 @@ namespace CsbBuilder
{ {
saved = false; saved = false;
if (treeNode.Tag is BuilderBaseNode) if (treeNode.Tag is BuilderBaseNode baseNode)
{ {
RemoveNode((BuilderBaseNode)treeNode.Tag); RemoveNode(baseNode);
} }
RemoveNodes(treeNode.Nodes); RemoveNodes(treeNode.Nodes);
@ -649,9 +636,8 @@ namespace CsbBuilder
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
RemoveNode(selectedNode); RemoveNode(selectedNode);
@ -671,9 +657,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
Clipboard.SetText(selectedNode.FullPath); Clipboard.SetText(selectedNode.FullPath);
@ -684,9 +669,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == synthTree) if (selectedTree == synthTree)
@ -708,9 +692,8 @@ namespace CsbBuilder
private void UpdateNodeNoRename(TreeNode treeNode) private void UpdateNodeNoRename(TreeNode treeNode)
{ {
if (treeNode.Tag is BuilderBaseNode) if (treeNode.Tag is BuilderBaseNode baseNode)
{ {
BuilderBaseNode baseNode = (BuilderBaseNode)treeNode.Tag;
baseNode.Name = treeNode.FullPath; baseNode.Name = treeNode.FullPath;
} }
@ -724,10 +707,8 @@ namespace CsbBuilder
{ {
foreach (TreeNode treeNode in collection) foreach (TreeNode treeNode in collection)
{ {
if (treeNode.Tag is BuilderBaseNode) if (treeNode.Tag is BuilderBaseNode baseNode)
{ {
BuilderBaseNode baseNode = (BuilderBaseNode)treeNode.Tag;
string previousName = baseNode.Name; string previousName = baseNode.Name;
baseNode.Name = treeNode.FullPath; baseNode.Name = treeNode.FullPath;
@ -1025,7 +1006,7 @@ namespace CsbBuilder
PasteNode(); PasteNode();
} }
else if (treeView.SelectedNode != null && (treeView.SelectedNode.Tag == null || (treeView.SelectedNode.Tag is BuilderSynthNode && ((BuilderSynthNode)treeView.SelectedNode.Tag).Type == BuilderSynthType.WithChildren))) else if (treeView.SelectedNode != null && (treeView.SelectedNode.Tag == null || (treeView.SelectedNode.Tag is BuilderSynthNode synthNode && synthNode.Type == BuilderSynthType.WithChildren)))
{ {
PasteAsChildNode(treeView.SelectedNode); PasteAsChildNode(treeView.SelectedNode);
} }
@ -1087,15 +1068,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (setReferenceForm.SelectedNode == null) if (setReferenceForm.SelectedNode == null)
{ {
synthNode.AisacReference = string.Empty; synthNode.AisacReference = string.Empty;
@ -1128,15 +1106,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (setReferenceForm.SelectedNode == null) if (setReferenceForm.SelectedNode == null)
{ {
synthNode.VoiceLimitGroupReference = string.Empty; synthNode.VoiceLimitGroupReference = string.Empty;
@ -1169,9 +1144,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderCueNode) if (selectedNode.Tag is BuilderCueNode)
@ -1200,14 +1174,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderCueNode) if (selectedNode.Tag is BuilderCueNode cueNode)
{ {
BuilderCueNode cueNode = (BuilderCueNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(cueNode.SynthReference)) if (!string.IsNullOrEmpty(cueNode.SynthReference))
{ {
TreeNode treeNode = synthTree.FindNodeByFullPath(cueNode.SynthReference); TreeNode treeNode = synthTree.FindNodeByFullPath(cueNode.SynthReference);
@ -1224,14 +1196,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(synthNode.AisacReference)) if (!string.IsNullOrEmpty(synthNode.AisacReference))
{ {
TreeNode treeNode = aisacTree.FindNodeByFullPath(synthNode.AisacReference); TreeNode treeNode = aisacTree.FindNodeByFullPath(synthNode.AisacReference);
@ -1249,14 +1219,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(synthNode.VoiceLimitGroupReference)) if (!string.IsNullOrEmpty(synthNode.VoiceLimitGroupReference))
{ {
TreeNode treeNode = voiceLimitGroupTree.FindNodeByFullPath(synthNode.VoiceLimitGroupReference); TreeNode treeNode = voiceLimitGroupTree.FindNodeByFullPath(synthNode.VoiceLimitGroupReference);
@ -1274,14 +1242,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(synthNode.SoundElementReference)) if (!string.IsNullOrEmpty(synthNode.SoundElementReference))
{ {
TreeNode treeNode = soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference); TreeNode treeNode = soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference);
@ -1308,15 +1274,12 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (setReferenceForm.SelectedNode == null) if (setReferenceForm.SelectedNode == null)
{ {
synthNode.SoundElementReference = string.Empty; synthNode.SoundElementReference = string.Empty;
@ -1339,14 +1302,11 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)selectedNode.Tag; if (selectedNode.Tag is BuilderSoundElementNode soundElementNode)
if (selectedNode.Tag is BuilderSoundElementNode)
{ {
string intro = soundElementNode.Intro; string intro = soundElementNode.Intro;
if (!string.IsNullOrEmpty(intro)) if (!string.IsNullOrEmpty(intro))
@ -1403,9 +1363,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag == null) if (selectedNode.Tag == null)
@ -1419,10 +1378,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
CreateSoundNode(selectedTree.Nodes, null); CreateSoundNode(selectedTree.Nodes, null);
} }
} }
@ -1544,8 +1501,7 @@ namespace CsbBuilder
private void AddSoundElementSound(TreeNode soundElementTreeNode, double volume, double pitch, int sampleCount, int delayTime) private void AddSoundElementSound(TreeNode soundElementTreeNode, double volume, double pitch, int sampleCount, int delayTime)
{ {
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)soundElementTreeNode.Tag; AddSoundElementSound((BuilderSoundElementNode)soundElementTreeNode.Tag, volume, pitch, sampleCount, delayTime);
AddSoundElementSound(soundElementNode, volume, pitch, sampleCount, delayTime);
} }
private double GetSecondsFromSampleCount(int sampleCount, int sampleRate) private double GetSecondsFromSampleCount(int sampleCount, int sampleRate)
@ -1555,10 +1511,8 @@ namespace CsbBuilder
private void GetTheBiggestSampleCount(TreeNode synthTreeNode, ref int sampleCount) private void GetTheBiggestSampleCount(TreeNode synthTreeNode, ref int sampleCount)
{ {
if (synthTreeNode.Tag is BuilderSynthNode) if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
if (synthNode.Type == BuilderSynthType.Single && !string.IsNullOrEmpty(synthNode.SoundElementReference)) if (synthNode.Type == BuilderSynthType.Single && !string.IsNullOrEmpty(synthNode.SoundElementReference))
{ {
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference).Tag; BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference).Tag;
@ -1590,9 +1544,8 @@ namespace CsbBuilder
while (synthTreeNode != null) while (synthTreeNode != null)
{ {
if (synthTreeNode.Tag is BuilderSynthNode) if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
pitch += synthNode.Pitch; pitch += synthNode.Pitch;
} }
@ -1608,9 +1561,8 @@ namespace CsbBuilder
while (synthTreeNode != null) while (synthTreeNode != null)
{ {
if (synthTreeNode.Tag is BuilderSynthNode) if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
volume = (volume * synthNode.Volume) / 1000; volume = (volume * synthNode.Volume) / 1000;
} }
@ -1626,9 +1578,8 @@ namespace CsbBuilder
while (synthTreeNode != null) while (synthTreeNode != null)
{ {
if (synthTreeNode.Tag is BuilderSynthNode) if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
delayTime += (int)synthNode.DelayTime; delayTime += (int)synthNode.DelayTime;
} }
@ -1716,10 +1667,8 @@ namespace CsbBuilder
{ {
StopSound(sender, e); StopSound(sender, e);
if (cueTree.Focused && cueTree.SelectedNode != null && cueTree.SelectedNode.Tag is BuilderCueNode) if (cueTree.Focused && cueTree.SelectedNode != null && cueTree.SelectedNode.Tag is BuilderCueNode cueNode)
{ {
BuilderCueNode cueNode = (BuilderCueNode)cueTree.SelectedNode.Tag;
if (!string.IsNullOrEmpty(cueNode.SynthReference)) if (!string.IsNullOrEmpty(cueNode.SynthReference))
{ {
TreeNode synthNode = synthTree.FindNodeByFullPath(cueNode.SynthReference); TreeNode synthNode = synthTree.FindNodeByFullPath(cueNode.SynthReference);
@ -1761,9 +1710,9 @@ namespace CsbBuilder
private void CloneTag(TreeNode treeNode) private void CloneTag(TreeNode treeNode)
{ {
if (treeNode.Tag != null && treeNode.Tag is ICloneable) if (treeNode.Tag != null && treeNode.Tag is ICloneable clonable)
{ {
treeNode.Tag = ((ICloneable)treeNode.Tag).Clone(); treeNode.Tag = clonable.Clone();
} }
foreach (TreeNode childNode in treeNode.Nodes) foreach (TreeNode childNode in treeNode.Nodes)
@ -1782,21 +1731,16 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl; CopyNode(selectedTree.SelectedNode);
TreeNode selectedNode = selectedTree.SelectedNode;
CopyNode(selectedNode);
} }
} }
private void AddToProject(TreeNode treeNode) private void AddToProject(TreeNode treeNode)
{ {
if (treeNode.Tag is BuilderCueNode) if (treeNode.Tag is BuilderCueNode cueNode)
{ {
BuilderCueNode cueNode = (BuilderCueNode)treeNode.Tag;
// Do checks // Do checks
if (!string.IsNullOrEmpty(cueNode.SynthReference) && synthTree.FindNodeByFullPath(cueNode.SynthReference) == null) if (!string.IsNullOrEmpty(cueNode.SynthReference) && synthTree.FindNodeByFullPath(cueNode.SynthReference) == null)
{ {
@ -1806,10 +1750,8 @@ namespace CsbBuilder
project.CueNodes.Add(cueNode); project.CueNodes.Add(cueNode);
} }
else if (treeNode.Tag is BuilderSynthNode) else if (treeNode.Tag is BuilderSynthNode synthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)treeNode.Tag;
if (!string.IsNullOrEmpty(synthNode.SoundElementReference) && soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference) == null) if (!string.IsNullOrEmpty(synthNode.SoundElementReference) && soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference) == null)
{ {
synthNode.SoundElementReference = string.Empty; synthNode.SoundElementReference = string.Empty;
@ -1828,10 +1770,8 @@ namespace CsbBuilder
project.SynthNodes.Add(synthNode); project.SynthNodes.Add(synthNode);
} }
if (treeNode.Tag is BuilderSoundElementNode) if (treeNode.Tag is BuilderSoundElementNode soundElementNode)
{ {
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)treeNode.Tag;
if (!string.IsNullOrEmpty(soundElementNode.Intro) && !File.Exists(project.GetFullAudioPath(soundElementNode.Intro))) if (!string.IsNullOrEmpty(soundElementNode.Intro) && !File.Exists(project.GetFullAudioPath(soundElementNode.Intro)))
{ {
soundElementNode.Intro = string.Empty; soundElementNode.Intro = string.Empty;
@ -1845,14 +1785,14 @@ namespace CsbBuilder
project.SoundElementNodes.Add(soundElementNode); project.SoundElementNodes.Add(soundElementNode);
} }
if (treeNode.Tag is BuilderAisacNode) if (treeNode.Tag is BuilderAisacNode aisacNode)
{ {
project.AisacNodes.Add((BuilderAisacNode)treeNode.Tag); project.AisacNodes.Add(aisacNode);
} }
if (treeNode.Tag is BuilderVoiceLimitGroupNode) if (treeNode.Tag is BuilderVoiceLimitGroupNode voiceLimitGroupNode)
{ {
project.VoiceLimitGroupNodes.Add((BuilderVoiceLimitGroupNode)treeNode.Tag); project.VoiceLimitGroupNodes.Add(voiceLimitGroupNode);
} }
} }
@ -1888,11 +1828,8 @@ namespace CsbBuilder
UpdateNodeNoRename(nodeToPaste); UpdateNodeNoRename(nodeToPaste);
// fix node if the tree is synthTree, and the nodes are synth nodes // fix node if the tree is synthTree, and the nodes are synth nodes
if (parent.TreeView == synthTree && parent.Tag is BuilderSynthNode && nodeToPaste.Tag is BuilderSynthNode) if (parent.TreeView == synthTree && parent.Tag is BuilderSynthNode synthNode && nodeToPaste.Tag is BuilderSynthNode copiedSynthNode)
{ {
BuilderSynthNode synthNode = (BuilderSynthNode)parent.Tag;
BuilderSynthNode copiedSynthNode = (BuilderSynthNode)nodeToPaste.Tag;
if (synthNode.Type == BuilderSynthType.WithChildren) if (synthNode.Type == BuilderSynthType.WithChildren)
{ {
// add the new node to track's children // add the new node to track's children
@ -1910,9 +1847,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == treeViewOfCopiedNode) if (selectedTree == treeViewOfCopiedNode)
@ -1935,10 +1871,8 @@ namespace CsbBuilder
TreeNode nodeToPaste = CloneNode(copiedNode); TreeNode nodeToPaste = CloneNode(copiedNode);
// check if it's cue and fix duplicate identifier if needed // check if it's cue and fix duplicate identifier if needed
if (nodeToPaste.Tag is BuilderCueNode) if (nodeToPaste.Tag is BuilderCueNode cueNode)
{ {
BuilderCueNode cueNode = (BuilderCueNode)nodeToPaste.Tag;
while (project.CueNodes.Exists(cue => cue.Identifier == cueNode.Identifier)) while (project.CueNodes.Exists(cue => cue.Identifier == cueNode.Identifier))
{ {
cueNode.Identifier++; cueNode.Identifier++;
@ -2006,10 +1940,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
if (selectedTree == treeViewOfCopiedNode) if (selectedTree == treeViewOfCopiedNode)
{ {
PasteNode(); PasteNode();
@ -2021,9 +1953,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == treeViewOfCopiedNode) if (selectedTree == treeViewOfCopiedNode)
@ -2042,9 +1973,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == synthTree) if (selectedTree == synthTree)
@ -2058,9 +1988,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == cueTree) if (selectedTree == cueTree)
@ -2070,7 +1999,7 @@ namespace CsbBuilder
else if (selectedTree == synthTree) else if (selectedTree == synthTree)
{ {
if (selectedNode.Tag is BuilderSynthNode && ((BuilderSynthNode)selectedNode.Tag).Type == BuilderSynthType.WithChildren) if (selectedNode.Tag is BuilderSynthNode synthNode && synthNode.Type == BuilderSynthType.WithChildren)
{ {
CreateSoundNode(selectedNode.Nodes, selectedNode, selectedNode.Index + 1); CreateSoundNode(selectedNode.Nodes, selectedNode, selectedNode.Index + 1);
} }
@ -2102,9 +2031,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedNode.Tag is BuilderSynthNode) if (selectedNode.Tag is BuilderSynthNode)
@ -2126,9 +2054,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == aisacTree) if (selectedTree == aisacTree)
@ -2218,9 +2145,8 @@ namespace CsbBuilder
{ {
ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner; ContextMenuStrip menuStrip = (ContextMenuStrip)((ToolStripItem)sender).Owner;
if (menuStrip.SourceControl is TreeView) if (menuStrip.SourceControl is TreeView selectedTree)
{ {
TreeView selectedTree = (TreeView)menuStrip.SourceControl;
TreeNode selectedNode = selectedTree.SelectedNode; TreeNode selectedNode = selectedTree.SelectedNode;
if (selectedTree == aisacTree) if (selectedTree == aisacTree)

View File

@ -132,7 +132,6 @@ namespace CsbEditor
} }
CriCpkArchive cpkArchive = new CriCpkArchive(); CriCpkArchive cpkArchive = new CriCpkArchive();
//cpkArchive.EnableMask = true;
CriTable csbFile = new CriTable(); CriTable csbFile = new CriTable();
csbFile.Load(csbPath); csbFile.Load(csbPath);
@ -187,19 +186,8 @@ namespace CsbEditor
if (streaming) if (streaming)
{ {
CriCpkEntry entry = new CriCpkEntry(); CriCpkEntry entry = new CriCpkEntry();
entry.Name = Path.GetFileName(sdlName);
int lastSlash = sdlName.LastIndexOf('/'); entry.DirectoryName = Path.GetDirectoryName(sdlName);
if (lastSlash != -1)
{
entry.Name = sdlName.Substring(lastSlash + 1);
entry.DirectoryName = sdlName.Substring(0, lastSlash);
}
else
{
entry.Name = sdlName;
}
entry.Id = (uint)cpkArchive.Count; entry.Id = (uint)cpkArchive.Count;
entry.FilePath = new FileInfo(Path.GetTempFileName()); entry.FilePath = new FileInfo(Path.GetTempFileName());
junks.Add(entry.FilePath); junks.Add(entry.FilePath);

View File

@ -20,11 +20,11 @@ namespace SonicAudioCmd
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
using (Stream source = File.OpenRead(args[0])) CriTable table = new CriTable();
using (Stream destination = File.Create(args[0] + "-unmask")) table.Load("test.utf");
{
Methods.MaskCriTable(source, destination, source.Length); Console.WriteLine(table.Rows[0]["testField"]);
} Console.ReadLine();
} }
} }
} }

View File

@ -30,7 +30,7 @@ namespace SonicAudioLib.Archive
{ {
if (reader.TableName != "AAX") if (reader.TableName != "AAX")
{ {
throw new Exception("Unknown AAX type! Please report the error with the file."); throw new Exception("Unknown AAX type. Please report the error with the file.");
} }
while (reader.Read()) while (reader.Read())

View File

@ -24,7 +24,7 @@ namespace SonicAudioLib.Archive
{ {
if (EndianStream.ReadCString(source, 4) != "AFS2") if (EndianStream.ReadCString(source, 4) != "AFS2")
{ {
throw new Exception("No AFS2 signature found."); throw new InvalidDataException("'AFS2' signature could not be found.");
} }
uint information = EndianStream.ReadUInt32(source); uint information = EndianStream.ReadUInt32(source);
@ -32,11 +32,11 @@ namespace SonicAudioLib.Archive
uint type = information & 0xFF; uint type = information & 0xFF;
if (type != 1) if (type != 1)
{ {
throw new Exception($"Invalid AFS2 type ({type}). Please report the error with the AWB file."); throw new InvalidDataException($"Unknown AFS2 type ({type}). Please report this error with the file(s).");
} }
IdFieldLength = (information & 0x00FF0000) >> 16; IdFieldLength = (information >> 16) & 0xFF;
PositionFieldLength = (information & 0x0000FF00) >> 8; PositionFieldLength = (information >> 8) & 0xFF;
ushort entryCount = (ushort)EndianStream.ReadUInt32(source); ushort entryCount = (ushort)EndianStream.ReadUInt32(source);
Align = EndianStream.ReadUInt32(source); Align = EndianStream.ReadUInt32(source);
@ -56,7 +56,7 @@ namespace SonicAudioLib.Archive
break; break;
default: default:
throw new Exception($"Unknown CueIndexFieldLength ({IdFieldLength}). Please report the error with the AWB file."); throw new InvalidDataException($"Unknown IdFieldLength ({IdFieldLength}). Please report this error with the file(s).");
} }
long positionPosition = 16 + (entryCount * IdFieldLength) + (i * PositionFieldLength); long positionPosition = 16 + (entryCount * IdFieldLength) + (i * PositionFieldLength);
@ -73,7 +73,7 @@ namespace SonicAudioLib.Archive
break; break;
default: default:
throw new Exception($"Unknown PositionFieldLength ({PositionFieldLength}). Please report the error with the AWB file."); throw new InvalidDataException($"Unknown PositionFieldLength ({PositionFieldLength}). Please report this error with the file(s).");
} }
if (previousEntry != null) if (previousEntry != null)
@ -81,7 +81,7 @@ namespace SonicAudioLib.Archive
previousEntry.Length = afs2Entry.Position - previousEntry.Position; previousEntry.Length = afs2Entry.Position - previousEntry.Position;
} }
afs2Entry.Position = Methods.Align(afs2Entry.Position, Align); afs2Entry.Position = Helpers.Align(afs2Entry.Position, Align);
if (i == entryCount - 1) if (i == entryCount - 1)
{ {
@ -123,7 +123,7 @@ namespace SonicAudioLib.Archive
break; break;
default: default:
throw new Exception($"Unknown CueIndexFieldLength ({IdFieldLength}). Please set a valid length."); throw new NotImplementedException($"Unimplemented IdFieldLength ({IdFieldLength}). Implemented length: (2)");
} }
} }
@ -143,7 +143,7 @@ namespace SonicAudioLib.Archive
break; break;
default: default:
throw new Exception($"Unknown PositionFieldLength ({PositionFieldLength}). Please set a valid length."); throw new InvalidDataException($"Unimplemented PositionFieldLength ({PositionFieldLength}). Implemented lengths: (2, 4)");
} }
afs2Entry.Position = entryPosition; afs2Entry.Position = entryPosition;

View File

@ -67,7 +67,7 @@ namespace SonicAudioLib.Archive
{ {
if (align != 1) if (align != 1)
{ {
new NotImplementedException("Currently, alignment handling in .CPK files is broken. Always use 1."); new NotImplementedException("Alignment is currently not implemented.");
} }
align = value; align = value;
@ -108,9 +108,9 @@ namespace SonicAudioLib.Archive
{ {
reader.Read(); reader.Read();
bool latest = reader.ContainsField("CpkMode"); bool isLatestVersion = reader.ContainsField("CpkMode");
if (latest) if (isLatestVersion)
{ {
mode = (CriCpkMode)reader.GetUInt32("CpkMode"); mode = (CriCpkMode)reader.GetUInt32("CpkMode");
} }
@ -166,7 +166,7 @@ namespace SonicAudioLib.Archive
entry.Name = tocReader.GetString("FileName"); entry.Name = tocReader.GetString("FileName");
entry.Length = tocReader.GetUInt32("FileSize"); entry.Length = tocReader.GetUInt32("FileSize");
entry.Position = (long)tocReader.GetUInt64("FileOffset"); entry.Position = (long)tocReader.GetUInt64("FileOffset");
entry.Id = latest ? tocReader.GetUInt32("ID") : tocReader.GetUInt32("Info"); entry.Id = isLatestVersion ? tocReader.GetUInt32("ID") : tocReader.GetUInt32("Info");
entry.Comment = tocReader.GetString("UserString"); entry.Comment = tocReader.GetString("UserString");
entry.IsCompressed = entry.Length != tocReader.GetUInt32("ExtractSize"); entry.IsCompressed = entry.Length != tocReader.GetUInt32("ExtractSize");
@ -180,7 +180,7 @@ namespace SonicAudioLib.Archive
entry.Position += tocPosition; entry.Position += tocPosition;
} }
entry.Position = Methods.Align(entry.Position, align); entry.Position = Helpers.Align(entry.Position, align);
etocReader.MoveToRow(tocReader.CurrentRow); etocReader.MoveToRow(tocReader.CurrentRow);
entry.UpdateDateTime = DateTimeFromCpkDateTime(etocReader.GetUInt64("UpdateDateTime")); entry.UpdateDateTime = DateTimeFromCpkDateTime(etocReader.GetUInt64("UpdateDateTime"));
@ -189,7 +189,7 @@ namespace SonicAudioLib.Archive
} }
} }
if (mode == CriCpkMode.FileNameAndId && latest) if (mode == CriCpkMode.FileNameAndId && isLatestVersion)
{ {
using (CriTableReader itocReader = CriCpkSection.Open(source, itocPosition)) using (CriTableReader itocReader = CriCpkSection.Open(source, itocPosition))
{ {
@ -244,7 +244,7 @@ namespace SonicAudioLib.Archive
long entryPosition = contentPosition; long entryPosition = contentPosition;
foreach (CriCpkEntry entry in entries.OrderBy(entry => entry.Id)) foreach (CriCpkEntry entry in entries.OrderBy(entry => entry.Id))
{ {
entryPosition = Methods.Align(entryPosition, align); entryPosition = Helpers.Align(entryPosition, align);
entry.Position = entryPosition; entry.Position = entryPosition;
entryPosition += entry.Length; entryPosition += entry.Length;
@ -253,7 +253,7 @@ namespace SonicAudioLib.Archive
else else
{ {
throw new Exception($"This CPK mode ({mode}) needs to be implemented. Please report this error with the file."); throw new NotImplementedException($"Unimplemented CPK mode ({mode})");
} }
Comment = reader.GetString("Comment"); Comment = reader.GetString("Comment");
@ -262,6 +262,12 @@ namespace SonicAudioLib.Archive
public override void Write(Stream destination) public override void Write(Stream destination)
{ {
string GetToolVersion()
{
AssemblyName assemblyName = Assembly.GetEntryAssembly().GetName();
return $"{assemblyName.Name}, {assemblyName.Version.ToString()}";
}
VldPool vldPool = new VldPool(Align, 2048); VldPool vldPool = new VldPool(Align, 2048);
using (CriCpkSection cpkSection = new CriCpkSection(destination, "CPK ", enableMask)) using (CriCpkSection cpkSection = new CriCpkSection(destination, "CPK ", enableMask))
@ -493,7 +499,7 @@ namespace SonicAudioLib.Archive
else else
{ {
throw new Exception($"This CPK mode ({mode}) isn't implemented yet! Please choose another mode."); throw new NotImplementedException($"Unimplemented CPK mode ({mode})");
} }
cpkSection.Writer.WriteStartRow(); cpkSection.Writer.WriteStartRow();
@ -603,12 +609,6 @@ namespace SonicAudioLib.Archive
((((ulong)dateTime.Hour * 0x100 + (uint)dateTime.Minute) * 0x100 + (uint)dateTime.Second) * 0x100); ((((ulong)dateTime.Hour * 0x100 + (uint)dateTime.Minute) * 0x100 + (uint)dateTime.Second) * 0x100);
} }
private string GetToolVersion()
{
AssemblyName assemblyName = Assembly.GetEntryAssembly().GetName();
return $"{assemblyName.Name}, {assemblyName.Version.ToString()}";
}
private class CriCpkSection : IDisposable private class CriCpkSection : IDisposable
{ {
private Stream destination; private Stream destination;

View File

@ -8,16 +8,16 @@ namespace SonicAudioLib.CriMw
public const byte EncodingTypeShiftJis = 0; public const byte EncodingTypeShiftJis = 0;
public const byte EncodingTypeUtf8 = 1; public const byte EncodingTypeUtf8 = 1;
public uint Length { get; set; } public uint Length;
public byte UnknownByte { get; set; } public byte UnknownByte;
public byte EncodingType { get; set; } public byte EncodingType;
public ushort RowsPosition { get; set; } public ushort RowsPosition;
public uint StringPoolPosition { get; set; } public uint StringPoolPosition;
public uint DataPoolPosition { get; set; } public uint DataPoolPosition;
public string TableName { get; set; } public string TableName;
public ushort NumberOfFields { get; set; } public ushort NumberOfFields;
public ushort RowLength { get; set; } public ushort RowLength;
public uint NumberOfRows { get; set; } public uint NumberOfRows;
} }
[Flags] [Flags]
@ -35,7 +35,7 @@ namespace SonicAudioLib.CriMw
Int32 = 5, Int32 = 5,
UInt64 = 6, UInt64 = 6,
Int64 = 7, Int64 = 7,
Float = 8, Single = 8,
Double = 9, Double = 9,
String = 10, String = 10,
Data = 11, Data = 11,
@ -46,10 +46,10 @@ namespace SonicAudioLib.CriMw
struct CriTableField struct CriTableField
{ {
public CriFieldFlag Flag { get; set; } public CriFieldFlag Flag;
public string Name { get; set; } public string Name;
public uint Position { get; set; } public uint Position;
public uint Length { get; set; } public uint Length;
public object Value { get; set; } public object Value;
} }
} }

View File

@ -1,8 +1,7 @@
using SonicAudioLib.IO; using SonicAudioLib.IO;
using System; using System;
using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.IO;
using System.Text; using System.Text;
namespace SonicAudioLib.CriMw namespace SonicAudioLib.CriMw
@ -88,17 +87,17 @@ namespace SonicAudioLib.CriMw
if (EndianStream.ReadCString(source, 4) != CriTableHeader.Signature) if (EndianStream.ReadCString(source, 4) != CriTableHeader.Signature)
{ {
// try to decrypt (currently only for CPK files since those are the only examples I have) // try to decrypt (currently only for CPK files since those are the only examples I have)
source.Seek(-4, SeekOrigin.Current); source.Seek(headerPosition, SeekOrigin.Begin);
MemoryStream unmaskedSource = new MemoryStream(); MemoryStream unmaskedSource = new MemoryStream();
Methods.MaskCriTable(source, unmaskedSource, source.Length); Helpers.MaskCriTable(source, unmaskedSource, source.Length);
// try again // try again
unmaskedSource.Seek(0, SeekOrigin.Begin); unmaskedSource.Seek(0, SeekOrigin.Begin);
if (EndianStream.ReadCString(unmaskedSource, 4) != CriTableHeader.Signature) if (EndianStream.ReadCString(unmaskedSource, 4) != CriTableHeader.Signature)
{ {
throw new Exception("No @UTF signature found."); throw new InvalidDataException("'@UTF' signature could not be found.");
} }
// Close the old stream // Close the old stream
@ -116,10 +115,9 @@ namespace SonicAudioLib.CriMw
if (header.UnknownByte != 0) if (header.UnknownByte != 0)
{ {
throw new Exception($"Invalid byte ({header.UnknownByte}. Please report the error with the file."); throw new InvalidDataException($"Invalid byte ({header.UnknownByte}. Please report this error with the file(s).");
} }
// This actually might be a boolean telling the reader that it's using UTF-8 encoding, but idk
switch (header.EncodingType) switch (header.EncodingType)
{ {
case CriTableHeader.EncodingTypeShiftJis: case CriTableHeader.EncodingTypeShiftJis:
@ -131,7 +129,7 @@ namespace SonicAudioLib.CriMw
break; break;
default: default:
throw new Exception($"Unknown encoding type ({header.EncodingType}). Please report the error with the file."); throw new InvalidDataException($"Unknown encoding type ({header.EncodingType}). Please report this error with the file(s).");
} }
header.RowsPosition = (ushort)(ReadUInt16() + 0x8); header.RowsPosition = (ushort)(ReadUInt16() + 0x8);
@ -157,10 +155,7 @@ namespace SonicAudioLib.CriMw
{ {
if (field.Flag.HasFlag(CriFieldFlag.Data)) if (field.Flag.HasFlag(CriFieldFlag.Data))
{ {
uint vldPosition; ReadData(out uint vldPosition, out uint vldLength);
uint vldLength;
ReadData(out vldPosition, out vldLength);
field.Position = vldPosition; field.Position = vldPosition;
field.Length = vldLength; field.Length = vldLength;
@ -175,16 +170,7 @@ namespace SonicAudioLib.CriMw
// Not even per row, and not even constant value? Then there's no storage. // Not even per row, and not even constant value? Then there's no storage.
else if (!field.Flag.HasFlag(CriFieldFlag.RowStorage) && !field.Flag.HasFlag(CriFieldFlag.DefaultValue)) else if (!field.Flag.HasFlag(CriFieldFlag.RowStorage) && !field.Flag.HasFlag(CriFieldFlag.DefaultValue))
{ {
if (field.Flag.HasFlag(CriFieldFlag.Data)) field.Value = CriField.NullValues[(byte)field.Flag & 0x0F];
{
field.Position = 0;
field.Length = 0;
}
else
{
field.Value = CriField.NullValues[(byte)field.Flag & 0x0F];
}
} }
fields.Add(field); fields.Add(field);
@ -269,7 +255,7 @@ namespace SonicAudioLib.CriMw
break; break;
case CriFieldFlag.Int32: case CriFieldFlag.Int32:
case CriFieldFlag.UInt32: case CriFieldFlag.UInt32:
case CriFieldFlag.Float: case CriFieldFlag.Single:
case CriFieldFlag.String: case CriFieldFlag.String:
position += 4; position += 4;
break; break;
@ -443,12 +429,12 @@ namespace SonicAudioLib.CriMw
return (long)GetValue(fieldName); return (long)GetValue(fieldName);
} }
public float GetFloat(int fieldIndex) public float GetSingle(int fieldIndex)
{ {
return (float)GetValue(fieldIndex); return (float)GetValue(fieldIndex);
} }
public float GetFloat(string fieldName) public float GetSingle(string fieldName)
{ {
return (float)GetValue(fieldName); return (float)GetValue(fieldName);
} }
@ -515,11 +501,8 @@ namespace SonicAudioLib.CriMw
return fields[fieldIndex].Length; return fields[fieldIndex].Length;
} }
uint vldPosition;
uint vldLength;
GoToValue(fieldIndex); GoToValue(fieldIndex);
ReadData(out vldPosition, out vldLength); ReadData(out uint vldPosition, out uint vldLength);
return vldLength; return vldLength;
} }
@ -540,11 +523,8 @@ namespace SonicAudioLib.CriMw
return fields[fieldIndex].Position; return fields[fieldIndex].Position;
} }
uint vldPosition;
uint vldLength;
GoToValue(fieldIndex); GoToValue(fieldIndex);
ReadData(out vldPosition, out vldLength); ReadData(out uint vldPosition, out uint vldLength);
return (uint)(headerPosition + header.DataPoolPosition + vldPosition); return (uint)(headerPosition + header.DataPoolPosition + vldPosition);
} }
@ -575,9 +555,7 @@ namespace SonicAudioLib.CriMw
private byte[] ReadBytes(int length) private byte[] ReadBytes(int length)
{ {
byte[] buff = new byte[length]; return EndianStream.ReadBytes(source, length);
source.Read(buff, 0, length);
return buff;
} }
private byte ReadByte() private byte ReadByte()
@ -625,9 +603,9 @@ namespace SonicAudioLib.CriMw
return EndianStream.ReadInt64BE(source); return EndianStream.ReadInt64BE(source);
} }
private float ReadFloat() private float ReadSingle()
{ {
return EndianStream.ReadFloatBE(source); return EndianStream.ReadSingleBE(source);
} }
private double ReadDouble() private double ReadDouble()
@ -638,20 +616,19 @@ namespace SonicAudioLib.CriMw
private string ReadString() private string ReadString()
{ {
uint stringPosition = ReadUInt32(); uint stringPosition = ReadUInt32();
long previousPosition = source.Position; long previousPosition = source.Position;
source.Position = headerPosition + header.StringPoolPosition + stringPosition; source.Position = headerPosition + header.StringPoolPosition + stringPosition;
string strResult = EndianStream.ReadCString(source, encoding); string readString = EndianStream.ReadCString(source, encoding);
source.Position = previousPosition; source.Position = previousPosition;
if (strResult == "<NULL>" || if (readString == "<NULL>" || (readString == header.TableName && stringPosition == 0))
(strResult == header.TableName && stringPosition == 0))
{ {
return string.Empty; return string.Empty;
} }
return strResult; return readString;
} }
private void ReadData(out uint vldPosition, out uint vldLength) private void ReadData(out uint vldPosition, out uint vldLength)
@ -687,25 +664,22 @@ namespace SonicAudioLib.CriMw
return ReadUInt64(); return ReadUInt64();
case CriFieldFlag.Int64: case CriFieldFlag.Int64:
return ReadInt64(); return ReadInt64();
case CriFieldFlag.Float: case CriFieldFlag.Single:
return ReadFloat(); return ReadSingle();
case CriFieldFlag.Double: case CriFieldFlag.Double:
return ReadDouble(); return ReadDouble();
case CriFieldFlag.String: case CriFieldFlag.String:
return ReadString(); return ReadString();
case CriFieldFlag.Data: case CriFieldFlag.Data:
{ {
uint vldPosition; ReadData(out uint vldPosition, out uint vldLength);
uint vldLength;
ReadData(out vldPosition, out vldLength);
// Some ACB files have the length info set to zero for UTF table fields, so find the correct length // Some ACB files have the length info set to zero for UTF table fields, so find the correct length
if (vldPosition > 0 && vldLength == 0) if (vldPosition > 0 && vldLength == 0)
{ {
source.Position = headerPosition + header.DataPoolPosition + vldPosition; source.Position = headerPosition + header.DataPoolPosition + vldPosition;
if (EndianStream.ReadCString(source, 4) == "@UTF") if (EndianStream.ReadCString(source, 4) == CriTableHeader.Signature)
{ {
vldLength = ReadUInt32() + 8; vldLength = ReadUInt32() + 8;
} }

View File

@ -137,7 +137,7 @@ namespace SonicAudioLib.CriMw
if (settings.EnableMask) if (settings.EnableMask)
{ {
destination.Position = headerPosition; destination.Position = headerPosition;
Methods.MaskCriTable(destination, header.Length); Helpers.MaskCriTable(destination, header.Length);
} }
destination.Seek(previousPosition, SeekOrigin.Begin); destination.Seek(previousPosition, SeekOrigin.Begin);
@ -267,7 +267,7 @@ namespace SonicAudioLib.CriMw
public void WriteValue(int fieldIndex, object rowValue) public void WriteValue(int fieldIndex, object rowValue)
{ {
if (!fields[fieldIndex].Flag.HasFlag(CriFieldFlag.RowStorage) || rowValue == null) if (fieldIndex >= fields.Count || fieldIndex < 0 || !fields[fieldIndex].Flag.HasFlag(CriFieldFlag.RowStorage) || rowValue == null)
{ {
return; return;
} }
@ -304,7 +304,7 @@ namespace SonicAudioLib.CriMw
break; break;
case CriFieldFlag.Int32: case CriFieldFlag.Int32:
case CriFieldFlag.UInt32: case CriFieldFlag.UInt32:
case CriFieldFlag.Float: case CriFieldFlag.Single:
case CriFieldFlag.String: case CriFieldFlag.String:
position += 4; position += 4;
break; break;
@ -343,7 +343,7 @@ namespace SonicAudioLib.CriMw
break; break;
case CriFieldFlag.Int32: case CriFieldFlag.Int32:
case CriFieldFlag.UInt32: case CriFieldFlag.UInt32:
case CriFieldFlag.Float: case CriFieldFlag.Single:
case CriFieldFlag.String: case CriFieldFlag.String:
length += 4; length += 4;
break; break;
@ -429,9 +429,9 @@ namespace SonicAudioLib.CriMw
EndianStream.WriteInt64BE(destination, value); EndianStream.WriteInt64BE(destination, value);
} }
private void WriteFloat(float value) private void WriteSingle(float value)
{ {
EndianStream.WriteFloatBE(destination, value); EndianStream.WriteSingleBE(destination, value);
} }
private void WriteDouble(double value) private void WriteDouble(double value)
@ -482,96 +482,83 @@ namespace SonicAudioLib.CriMw
destination.Write(buffer, 0, buffer.Length); destination.Write(buffer, 0, buffer.Length);
} }
private void WriteValue(object value) private void WriteValue(object val)
{ {
if (value == null) switch (val)
{ {
return; case byte value:
} WriteByte(value);
break;
if (value is byte) case sbyte value:
{ WriteSByte(value);
WriteByte((byte)value); break;
}
else if (value is sbyte) case ushort value:
{ WriteUInt16(value);
WriteSByte((sbyte)value); break;
}
else if (value is ushort) case short value:
{ WriteInt16(value);
WriteUInt16((ushort)value); break;
}
else if (value is short) case uint value:
{ WriteUInt32(value);
WriteInt16((short)value); break;
}
else if (value is uint) case int value:
{ WriteInt32(value);
WriteUInt32((uint)value); break;
}
else if (value is int) case ulong value:
{ WriteUInt64(value);
WriteInt32((int)value); break;
}
else if (value is ulong) case long value:
{ WriteInt64(value);
WriteUInt64((ulong)value); break;
}
else if (value is long) case float value:
{ WriteSingle(value);
WriteInt64((long)value); break;
}
else if (value is float) case double value:
{ WriteDouble(value);
WriteFloat((float)value); break;
}
else if (value is double) case string value:
{ WriteString(value);
WriteDouble((double)value); break;
}
else if (value is string) case byte[] value:
{ WriteData(value);
WriteString((string)value); break;
}
else if (value is byte[]) case Guid value:
{ WriteGuid(value);
WriteData((byte[])value); break;
}
else if (value is Stream) case Stream stream:
{ WriteStream(stream);
WriteStream((Stream)value); break;
}
else if (value is FileInfo) case ModuleBase module:
{ WriteModule(module);
WriteFile((FileInfo)value); break;
}
else if (value is ModuleBase) case FileInfo fileInfo:
{ WriteFile(fileInfo);
WriteModule((ModuleBase)value); break;
}
else if (value is Guid)
{
WriteGuid((Guid)value);
} }
} }
public void Dispose() public void Dispose()
{ {
if (status != Status.End)
{
WriteEndTable();
}
fields.Clear(); fields.Clear();
stringPool.Clear(); stringPool.Clear();
vldPool.Clear(); vldPool.Clear();

View File

@ -67,7 +67,7 @@ namespace SonicAudioLib.CriMw.Serialization
continue; continue;
} }
// Also ignore the properties that are not supportable (except FileInfo and Stream) // Ignore the properties that are not supportable
if (propertyInfo.PropertyType != typeof(FileInfo) && if (propertyInfo.PropertyType != typeof(FileInfo) &&
propertyInfo.PropertyType != typeof(Stream) && propertyInfo.PropertyType != typeof(Stream) &&
propertyInfo.PropertyType != typeof(bool) && propertyInfo.PropertyType != typeof(bool) &&
@ -161,9 +161,9 @@ namespace SonicAudioLib.CriMw.Serialization
defaultValue = null; defaultValue = null;
} }
if (defaultValue is bool) if (defaultValue is bool boolean)
{ {
defaultValue = ((bool)defaultValue == true) ? (byte)1 : (byte)0; defaultValue = boolean ? (byte)1 : (byte)0;
} }
else if (defaultValue is Enum) else if (defaultValue is Enum)
@ -197,9 +197,9 @@ namespace SonicAudioLib.CriMw.Serialization
object value = propertyInfo.GetValue(obj); object value = propertyInfo.GetValue(obj);
Type propertyType = propertyInfo.PropertyType; Type propertyType = propertyInfo.PropertyType;
if (value is bool) if (value is bool boolean)
{ {
value = ((bool)value == true) ? (byte)1 : (byte)0; value = boolean ? (byte)1 : (byte)0;
} }
else if (value is Enum) else if (value is Enum)
@ -299,14 +299,14 @@ namespace SonicAudioLib.CriMw.Serialization
object value = tableReader.GetValue(fieldName); object value = tableReader.GetValue(fieldName);
if (value is Substream) if (value is Substream substream)
{ {
value = ((Substream)value).ToArray(); value = substream.ToArray();
} }
else if (value is byte && propertyInfo.PropertyType == typeof(bool)) else if (value is byte boolean && propertyInfo.PropertyType == typeof(bool))
{ {
value = (byte)value == 1; value = boolean == 1;
} }
else if (propertyInfo.PropertyType.IsEnum) else if (propertyInfo.PropertyType.IsEnum)

View File

@ -2,23 +2,30 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Runtime.InteropServices;
namespace SonicAudioLib.IO namespace SonicAudioLib.IO
{ {
/// <summary>
/// Represents a static class for reading various data types in any endian format from a <see cref="Stream"/>.
/// </summary>
public static class EndianStream public static class EndianStream
{ {
private static byte[] buffer; [StructLayout(LayoutKind.Explicit)]
private struct SingleUnion
/// <summary>
/// Fills <see cref="buffer"/> in the given length.
/// </summary>
private static void FillBuffer(Stream source, int length)
{ {
buffer = new byte[length]; [FieldOffset(0)]
source.Read(buffer, 0, length); public float Single;
[FieldOffset(0)]
public uint UInt;
}
[StructLayout(LayoutKind.Explicit)]
private struct DoubleUnion
{
[FieldOffset(0)]
public double Double;
[FieldOffset(0)]
public ulong ULong;
} }
public static void CopyTo(Stream source, Stream destination) public static void CopyTo(Stream source, Stream destination)
@ -37,9 +44,34 @@ namespace SonicAudioLib.IO
} }
} }
public static void CopyPartTo(Stream source, Stream destination, long position, long length, int bufferSize)
{
source.Position = position;
byte[] buffer = new byte[bufferSize];
int num;
long totalWrittenBytes = 0;
while ((num = source.Read(buffer, 0, bufferSize)) != 0)
{
totalWrittenBytes += num;
if (totalWrittenBytes > length)
{
num -= (int)(totalWrittenBytes - length);
destination.Write(buffer, 0, num);
break;
}
destination.Write(buffer, 0, num);
}
}
public static byte[] ReadBytes(Stream source, int length) public static byte[] ReadBytes(Stream source, int length)
{ {
FillBuffer(source, length); byte[] buffer = new byte[length];
source.Read(buffer, 0, length);
return buffer; return buffer;
} }
@ -47,10 +79,10 @@ namespace SonicAudioLib.IO
{ {
long oldPosition = source.Position; long oldPosition = source.Position;
source.Position = position; source.Position = position;
FillBuffer(source, length); var result = ReadBytes(source, length);
source.Position = oldPosition; source.Position = oldPosition;
return buffer; return result;
} }
public static void WriteBytes(Stream destination, byte[] value) public static void WriteBytes(Stream destination, byte[] value)
@ -101,12 +133,12 @@ namespace SonicAudioLib.IO
public static bool ReadBoolean(Stream source) public static bool ReadBoolean(Stream source)
{ {
return ReadByte(source) > 0; return source.ReadByte() > 0;
} }
public static void WriteBoolean(Stream destination, bool value) public static void WriteBoolean(Stream destination, bool value)
{ {
WriteByte(destination, (byte)(value == true ? 1 : 0)); WriteByte(destination, (byte)(value ? 1 : 0));
} }
public static sbyte ReadSByte(Stream source) public static sbyte ReadSByte(Stream source)
@ -121,226 +153,242 @@ namespace SonicAudioLib.IO
public static ushort ReadUInt16(Stream source) public static ushort ReadUInt16(Stream source)
{ {
FillBuffer(source, 2); return (ushort)(source.ReadByte() | source.ReadByte() << 8);
return BitConverter.ToUInt16(buffer, 0);
} }
public static ushort ReadUInt16BE(Stream source) public static ushort ReadUInt16BE(Stream source)
{ {
FillBuffer(source, 2); return (ushort)(source.ReadByte() << 8 | source.ReadByte());
Array.Reverse(buffer);
return BitConverter.ToUInt16(buffer, 0);
} }
public static void WriteUInt16(Stream destination, ushort value) public static void WriteUInt16(Stream destination, ushort value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value));
destination.Write(buffer, 0, 2); destination.WriteByte((byte)(value >> 8));
} }
public static void WriteUInt16BE(Stream destination, ushort value) public static void WriteUInt16BE(Stream destination, ushort value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value));
Array.Reverse(buffer);
destination.Write(buffer, 0, 2);
} }
public static short ReadInt16(Stream source) public static short ReadInt16(Stream source)
{ {
FillBuffer(source, 2); return (short)(source.ReadByte() | source.ReadByte() << 8);
return BitConverter.ToInt16(buffer, 0);
} }
public static short ReadInt16BE(Stream source) public static short ReadInt16BE(Stream source)
{ {
FillBuffer(source, 2); return (short)(source.ReadByte() << 8 | source.ReadByte());
Array.Reverse(buffer);
return BitConverter.ToInt16(buffer, 0);
} }
public static void WriteInt16(Stream destination, short value) public static void WriteInt16(Stream destination, short value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value));
destination.Write(buffer, 0, 2); destination.WriteByte((byte)(value >> 8));
} }
public static void WriteInt16BE(Stream destination, short value) public static void WriteInt16BE(Stream destination, short value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value));
Array.Reverse(buffer);
destination.Write(buffer, 0, 2);
} }
public static uint ReadUInt32(Stream source) public static uint ReadUInt32(Stream source)
{ {
FillBuffer(source, 4); return (uint)(source.ReadByte() | source.ReadByte() << 8 | source.ReadByte() << 16 | source.ReadByte() << 24);
return BitConverter.ToUInt32(buffer, 0);
} }
public static uint ReadUInt32BE(Stream source) public static uint ReadUInt32BE(Stream source)
{ {
FillBuffer(source, 4); return (uint)(source.ReadByte() << 24 | source.ReadByte() << 16 | source.ReadByte() << 8 | source.ReadByte());
Array.Reverse(buffer);
return BitConverter.ToUInt32(buffer, 0);
} }
public static void WriteUInt32(Stream destination, uint value) public static void WriteUInt32(Stream destination, uint value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value));
destination.Write(buffer, 0, 4); destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value >> 16));
destination.WriteByte((byte)(value >> 24));
} }
public static void WriteUInt32BE(Stream destination, uint value) public static void WriteUInt32BE(Stream destination, uint value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)((value >> 24)));
destination.WriteByte((byte)((value >> 16)));
Array.Reverse(buffer); destination.WriteByte((byte)((value >> 8)));
destination.Write(buffer, 0, 4); destination.WriteByte((byte)(value));
} }
public static int ReadInt32(Stream source) public static int ReadInt32(Stream source)
{ {
FillBuffer(source, 4); return source.ReadByte() | source.ReadByte() << 8 | source.ReadByte() << 16 | source.ReadByte() << 24;
return BitConverter.ToInt32(buffer, 0);
} }
public static int ReadInt32BE(Stream source) public static int ReadInt32BE(Stream source)
{ {
FillBuffer(source, 4); return source.ReadByte() << 24 | source.ReadByte() << 16 | source.ReadByte() << 8 | source.ReadByte();
Array.Reverse(buffer);
return BitConverter.ToInt32(buffer, 0);
} }
public static void WriteInt32(Stream destination, int value) public static void WriteInt32(Stream destination, int value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value));
destination.Write(buffer, 0, 4); destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value >> 16));
destination.WriteByte((byte)(value >> 24));
} }
public static void WriteInt32BE(Stream destination, int value) public static void WriteInt32BE(Stream destination, int value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)((value >> 24)));
destination.WriteByte((byte)((value >> 16)));
Array.Reverse(buffer); destination.WriteByte((byte)((value >> 8)));
destination.Write(buffer, 0, 4); destination.WriteByte((byte)(value));
} }
public static ulong ReadUInt64(Stream source) public static ulong ReadUInt64(Stream source)
{ {
FillBuffer(source, 8); return ((uint)source.ReadByte() | (ulong)source.ReadByte() << 8 |
return BitConverter.ToUInt64(buffer, 0); (ulong)source.ReadByte() << 16 | (ulong)source.ReadByte() << 24 |
(ulong)source.ReadByte() << 32 | (ulong)source.ReadByte() << 40 |
(ulong)source.ReadByte() << 48 | (ulong)source.ReadByte() << 56);
} }
public static ulong ReadUInt64BE(Stream source) public static ulong ReadUInt64BE(Stream source)
{ {
FillBuffer(source, 8); return ((ulong)source.ReadByte() << 56 | (ulong)source.ReadByte() << 48 |
(ulong)source.ReadByte() << 40 | (ulong)source.ReadByte() << 32 |
Array.Reverse(buffer); (ulong)source.ReadByte() << 24 | (ulong)source.ReadByte() << 16 |
return BitConverter.ToUInt64(buffer, 0); (ulong)source.ReadByte() << 8 | (uint)source.ReadByte());
} }
public static void WriteUInt64(Stream destination, ulong value) public static void WriteUInt64(Stream destination, ulong value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value));
destination.Write(buffer, 0, 8); destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value >> 16));
destination.WriteByte((byte)(value >> 24));
destination.WriteByte((byte)(value >> 32));
destination.WriteByte((byte)(value >> 40));
destination.WriteByte((byte)(value >> 48));
destination.WriteByte((byte)(value >> 56));
} }
public static void WriteUInt64BE(Stream destination, ulong value) public static void WriteUInt64BE(Stream destination, ulong value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)((value >> 56)));
destination.WriteByte((byte)((value >> 48)));
Array.Reverse(buffer); destination.WriteByte((byte)((value >> 40)));
destination.Write(buffer, 0, 8); destination.WriteByte((byte)((value >> 32)));
destination.WriteByte((byte)((value >> 24)));
destination.WriteByte((byte)((value >> 16)));
destination.WriteByte((byte)((value >> 8)));
destination.WriteByte((byte)(value));
} }
public static long ReadInt64(Stream source) public static long ReadInt64(Stream source)
{ {
FillBuffer(source, 8); return source.ReadByte() | source.ReadByte() << 8 |
return BitConverter.ToInt64(buffer, 0); source.ReadByte() << 16 | source.ReadByte() << 24 |
source.ReadByte() << 32 | source.ReadByte() << 40 |
source.ReadByte() << 48 | source.ReadByte() << 56;
} }
public static long ReadInt64BE(Stream source) public static long ReadInt64BE(Stream source)
{ {
FillBuffer(source, 8); return source.ReadByte() << 56 | source.ReadByte() << 48 |
source.ReadByte() << 40 | source.ReadByte() << 32 |
Array.Reverse(buffer); source.ReadByte() << 24 | source.ReadByte() << 16 |
return BitConverter.ToInt64(buffer, 0); source.ReadByte() << 8 | source.ReadByte();
} }
public static void WriteInt64(Stream destination, long value) public static void WriteInt64(Stream destination, long value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)(value));
destination.Write(buffer, 0, 8); destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value >> 16));
destination.WriteByte((byte)(value >> 24));
destination.WriteByte((byte)(value >> 32));
destination.WriteByte((byte)(value >> 40));
destination.WriteByte((byte)(value >> 48));
destination.WriteByte((byte)(value >> 56));
} }
public static void WriteInt64BE(Stream destination, long value) public static void WriteInt64BE(Stream destination, long value)
{ {
buffer = BitConverter.GetBytes(value); destination.WriteByte((byte)((value >> 56)));
destination.WriteByte((byte)((value >> 48)));
Array.Reverse(buffer); destination.WriteByte((byte)((value >> 40)));
destination.Write(buffer, 0, 8); destination.WriteByte((byte)((value >> 32)));
destination.WriteByte((byte)((value >> 24)));
destination.WriteByte((byte)((value >> 16)));
destination.WriteByte((byte)((value >> 8)));
destination.WriteByte((byte)(value));
} }
public static float ReadFloat(Stream source) public static float ReadSingle(Stream source)
{ {
FillBuffer(source, 4); var union = new SingleUnion();
return BitConverter.ToSingle(buffer, 0); union.UInt = ReadUInt32(source);
return union.Single;
} }
public static float ReadFloatBE(Stream source) public static float ReadSingleBE(Stream source)
{ {
FillBuffer(source, 4); var union = new SingleUnion();
union.UInt = ReadUInt32BE(source);
Array.Reverse(buffer); return union.Single;
return BitConverter.ToSingle(buffer, 0);
} }
public static void WriteFloat(Stream destination, float value) public static void WriteSingle(Stream destination, float value)
{ {
buffer = BitConverter.GetBytes(value); var union = new SingleUnion();
destination.Write(buffer, 0, 4); union.Single = value;
WriteUInt32(destination, union.UInt);
} }
public static void WriteFloatBE(Stream destination, float value) public static void WriteSingleBE(Stream destination, float value)
{ {
buffer = BitConverter.GetBytes(value); var union = new SingleUnion();
union.Single = value;
Array.Reverse(buffer); WriteUInt32BE(destination, union.UInt);
destination.Write(buffer, 0, 4);
} }
public static double ReadDouble(Stream source) public static double ReadDouble(Stream source)
{ {
FillBuffer(source, 8); var union = new DoubleUnion();
return BitConverter.ToDouble(buffer, 0); union.ULong = ReadUInt64(source);
return union.Double;
} }
public static double ReadDoubleBE(Stream source) public static double ReadDoubleBE(Stream source)
{ {
FillBuffer(source, 8); var union = new DoubleUnion();
union.ULong = ReadUInt64BE(source);
Array.Reverse(buffer); return union.Double;
return BitConverter.ToDouble(buffer, 0);
} }
public static void WriteDouble(Stream destination, double value) public static void WriteDouble(Stream destination, double value)
{ {
buffer = BitConverter.GetBytes(value); var union = new DoubleUnion();
destination.Write(buffer, 0, 8); union.Double = value;
WriteUInt64(destination, union.ULong);
} }
public static void WriteDoubleBE(Stream destination, double value) public static void WriteDoubleBE(Stream destination, double value)
{ {
buffer = BitConverter.GetBytes(value); var union = new DoubleUnion();
union.Double = value;
Array.Reverse(buffer); WriteUInt64BE(destination, union.ULong);
destination.Write(buffer, 0, 8);
} }
public static string ReadCString(Stream source) public static string ReadCString(Stream source)
@ -350,15 +398,16 @@ namespace SonicAudioLib.IO
public static string ReadCString(Stream source, Encoding encoding) public static string ReadCString(Stream source, Encoding encoding)
{ {
var list = new List<byte>(); var characters = new List<byte>();
byte buff; byte character = (byte)source.ReadByte();
while ((buff = ReadByte(source)) != 0) while (character != 0)
{ {
list.Add(buff); characters.Add(character);
character = (byte)source.ReadByte();
} }
return encoding.GetString(list.ToArray()); return encoding.GetString(characters.ToArray());
} }
public static void WriteCString(Stream destination, string value) public static void WriteCString(Stream destination, string value)
@ -368,14 +417,10 @@ namespace SonicAudioLib.IO
public static void WriteCString(Stream destination, string value, Encoding encoding) public static void WriteCString(Stream destination, string value, Encoding encoding)
{ {
var buff = encoding.GetBytes(value); byte[] buffer = encoding.GetBytes(value);
foreach (byte _buff in buff) destination.Write(buffer, 0, buffer.Length);
{ destination.WriteByte(0);
WriteByte(destination, _buff);
}
WriteByte(destination, 0);
} }
public static string ReadCString(Stream source, int length) public static string ReadCString(Stream source, int length)

View File

@ -6,7 +6,7 @@ using System.IO;
namespace SonicAudioLib.IO namespace SonicAudioLib.IO
{ {
public class Methods public class Helpers
{ {
public static long Align(long value, long alignment) public static long Align(long value, long alignment)
{ {

View File

@ -44,7 +44,7 @@ namespace SonicAudioLib.IO
return 0; return 0;
} }
length = Methods.Align(length, align); length = Helpers.Align(length, align);
long position = length; long position = length;
length += data.Length; length += data.Length;
@ -60,7 +60,7 @@ namespace SonicAudioLib.IO
return 0; return 0;
} }
length = Methods.Align(length, align); length = Helpers.Align(length, align);
long position = length; long position = length;
length += stream.Length; length += stream.Length;
@ -76,7 +76,7 @@ namespace SonicAudioLib.IO
return 0; return 0;
} }
length = Methods.Align(length, align); length = Helpers.Align(length, align);
long position = length; long position = length;
length += fileInfo.Length; length += fileInfo.Length;
@ -92,7 +92,7 @@ namespace SonicAudioLib.IO
return 0; return 0;
} }
length = Methods.Align(length, align); length = Helpers.Align(length, align);
long position = length; long position = length;
length += module.CalculateLength(); length += module.CalculateLength();
@ -109,32 +109,27 @@ namespace SonicAudioLib.IO
{ {
EndianStream.Pad(destination, align); EndianStream.Pad(destination, align);
if (item is byte[]) if (item is byte[] bytes)
{ {
byte[] output = (byte[])item; destination.Write(bytes, 0, bytes.Length);
destination.Write(output, 0, output.Length);
} }
else if (item is Stream) else if (item is Stream stream)
{ {
Stream output = (Stream)item; stream.Seek(0, SeekOrigin.Begin);
output.Seek(0, SeekOrigin.Begin); stream.CopyTo(destination);
output.CopyTo(destination);
} }
else if (item is FileInfo) else if (item is FileInfo fileInfo)
{ {
FileInfo fileInfo = (FileInfo)item; using (Stream source = fileInfo.OpenRead())
{
Stream output = fileInfo.OpenRead(); source.CopyTo(destination);
output.CopyTo(destination); }
output.Close();
} }
else if (item is ModuleBase) else if (item is ModuleBase module)
{ {
ModuleBase module = (ModuleBase)item;
module.Write(destination); module.Write(destination);
} }
} }

View File

@ -60,7 +60,7 @@
<Compile Include="IO\EndianStream.cs" /> <Compile Include="IO\EndianStream.cs" />
<Compile Include="IO\StringPool.cs" /> <Compile Include="IO\StringPool.cs" />
<Compile Include="IO\Substream.cs" /> <Compile Include="IO\Substream.cs" />
<Compile Include="IO\Methods.cs" /> <Compile Include="IO\Helpers.cs" />
<Compile Include="IO\VldPool.cs" /> <Compile Include="IO\VldPool.cs" />
<Compile Include="Module\ModuleBase.cs" /> <Compile Include="Module\ModuleBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />