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
[Dd]ebug/
[Rr]elease/
[Dd]ebugPublic/
[Rr]eleases/
[Xx]64/

View File

@ -1,8 +1,12 @@
# 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.
## Releases
[Here.](https://ci.appveyor.com/project/blueskythlikesclouds/sonicaudiotools)
You can get the binaries from Artifacts tab.
## Building
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.
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)
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
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
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SonicAudioLib", "Source\SonicAudioLib\SonicAudioLib.csproj", "{63138773-1F47-474C-9345-15EB6183ECC6}"
EndProject

View File

@ -26,7 +26,7 @@ namespace AcbEditor
if (args[0].EndsWith(".acb"))
{
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;
Directory.CreateDirectory(outputDirectoryPath);
@ -87,7 +87,7 @@ namespace AcbEditor
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)
{
long oldPosition = source.Position;
bool result = false;
result = EndianStream.ReadCString(source, 4) == "AFS2";
bool result = EndianStream.ReadCString(source, 4) == "AFS2";
source.Seek(oldPosition, SeekOrigin.Begin);
return result;

View File

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

View File

@ -290,39 +290,35 @@ namespace CsbBuilder
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.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));
}
else if (node is BuilderSoundElementNode)
else if (node is BuilderSoundElementNode soundElementNode)
{
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)node;
project.SoundElementNodes.Remove(soundElementNode);
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.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.SynthNodes.Where(synth => synth.VoiceLimitGroupReference == voiceLimitGroupNode.Name).ToList().ForEach(voiceLimitGroup => voiceLimitGroup.VoiceLimitGroupReference = string.Empty);
@ -349,10 +345,8 @@ namespace CsbBuilder
treeNode.Tag = 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)
{
parentSynthNode.Children.Add(synthNode.Name);
@ -385,10 +379,8 @@ namespace CsbBuilder
treeNode.Tag = 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)
{
parentSynthNode.Children.Add(synthNode.Name);
@ -522,9 +514,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == cueTree)
@ -534,7 +525,7 @@ namespace CsbBuilder
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);
}
@ -566,10 +557,8 @@ namespace CsbBuilder
{
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)
{
CreateCueNode(selectedTree.Nodes, null);
@ -601,10 +590,8 @@ namespace CsbBuilder
{
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)
{
CreateSynthFolder(selectedTree.Nodes);
@ -621,9 +608,9 @@ namespace CsbBuilder
{
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);
@ -634,9 +621,9 @@ namespace CsbBuilder
{
saved = false;
if (treeNode.Tag is BuilderBaseNode)
if (treeNode.Tag is BuilderBaseNode baseNode)
{
RemoveNode((BuilderBaseNode)treeNode.Tag);
RemoveNode(baseNode);
}
RemoveNodes(treeNode.Nodes);
@ -649,9 +636,8 @@ namespace CsbBuilder
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;
RemoveNode(selectedNode);
@ -671,9 +657,8 @@ namespace CsbBuilder
{
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;
Clipboard.SetText(selectedNode.FullPath);
@ -684,9 +669,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == synthTree)
@ -708,9 +692,8 @@ namespace CsbBuilder
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;
}
@ -724,10 +707,8 @@ namespace CsbBuilder
{
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;
baseNode.Name = treeNode.FullPath;
@ -1025,7 +1006,7 @@ namespace CsbBuilder
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);
}
@ -1087,15 +1068,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
if (selectedNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (setReferenceForm.SelectedNode == null)
{
synthNode.AisacReference = string.Empty;
@ -1128,15 +1106,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
if (selectedNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (setReferenceForm.SelectedNode == null)
{
synthNode.VoiceLimitGroupReference = string.Empty;
@ -1169,9 +1144,8 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderCueNode)
@ -1200,14 +1174,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderCueNode)
if (selectedNode.Tag is BuilderCueNode cueNode)
{
BuilderCueNode cueNode = (BuilderCueNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(cueNode.SynthReference))
{
TreeNode treeNode = synthTree.FindNodeByFullPath(cueNode.SynthReference);
@ -1224,14 +1196,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
if (selectedNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(synthNode.AisacReference))
{
TreeNode treeNode = aisacTree.FindNodeByFullPath(synthNode.AisacReference);
@ -1249,14 +1219,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
if (selectedNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(synthNode.VoiceLimitGroupReference))
{
TreeNode treeNode = voiceLimitGroupTree.FindNodeByFullPath(synthNode.VoiceLimitGroupReference);
@ -1274,14 +1242,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
if (selectedNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (!string.IsNullOrEmpty(synthNode.SoundElementReference))
{
TreeNode treeNode = soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference);
@ -1308,15 +1274,12 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
if (selectedNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)selectedNode.Tag;
if (setReferenceForm.SelectedNode == null)
{
synthNode.SoundElementReference = string.Empty;
@ -1339,14 +1302,11 @@ namespace CsbBuilder
{
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;
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)selectedNode.Tag;
if (selectedNode.Tag is BuilderSoundElementNode)
if (selectedNode.Tag is BuilderSoundElementNode soundElementNode)
{
string intro = soundElementNode.Intro;
if (!string.IsNullOrEmpty(intro))
@ -1403,9 +1363,8 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag == null)
@ -1419,10 +1378,8 @@ namespace CsbBuilder
{
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);
}
}
@ -1544,8 +1501,7 @@ namespace CsbBuilder
private void AddSoundElementSound(TreeNode soundElementTreeNode, double volume, double pitch, int sampleCount, int delayTime)
{
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)soundElementTreeNode.Tag;
AddSoundElementSound(soundElementNode, volume, pitch, sampleCount, delayTime);
AddSoundElementSound((BuilderSoundElementNode)soundElementTreeNode.Tag, volume, pitch, sampleCount, delayTime);
}
private double GetSecondsFromSampleCount(int sampleCount, int sampleRate)
@ -1555,10 +1511,8 @@ namespace CsbBuilder
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))
{
BuilderSoundElementNode soundElementNode = (BuilderSoundElementNode)soundElementTree.FindNodeByFullPath(synthNode.SoundElementReference).Tag;
@ -1590,9 +1544,8 @@ namespace CsbBuilder
while (synthTreeNode != null)
{
if (synthTreeNode.Tag is BuilderSynthNode)
if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
pitch += synthNode.Pitch;
}
@ -1608,9 +1561,8 @@ namespace CsbBuilder
while (synthTreeNode != null)
{
if (synthTreeNode.Tag is BuilderSynthNode)
if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
volume = (volume * synthNode.Volume) / 1000;
}
@ -1626,9 +1578,8 @@ namespace CsbBuilder
while (synthTreeNode != null)
{
if (synthTreeNode.Tag is BuilderSynthNode)
if (synthTreeNode.Tag is BuilderSynthNode synthNode)
{
BuilderSynthNode synthNode = (BuilderSynthNode)synthTreeNode.Tag;
delayTime += (int)synthNode.DelayTime;
}
@ -1716,10 +1667,8 @@ namespace CsbBuilder
{
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))
{
TreeNode synthNode = synthTree.FindNodeByFullPath(cueNode.SynthReference);
@ -1761,9 +1710,9 @@ namespace CsbBuilder
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)
@ -1782,21 +1731,16 @@ namespace CsbBuilder
{
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;
CopyNode(selectedNode);
CopyNode(selectedTree.SelectedNode);
}
}
private void AddToProject(TreeNode treeNode)
{
if (treeNode.Tag is BuilderCueNode)
if (treeNode.Tag is BuilderCueNode cueNode)
{
BuilderCueNode cueNode = (BuilderCueNode)treeNode.Tag;
// Do checks
if (!string.IsNullOrEmpty(cueNode.SynthReference) && synthTree.FindNodeByFullPath(cueNode.SynthReference) == null)
{
@ -1806,10 +1750,8 @@ namespace CsbBuilder
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)
{
synthNode.SoundElementReference = string.Empty;
@ -1828,10 +1770,8 @@ namespace CsbBuilder
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)))
{
soundElementNode.Intro = string.Empty;
@ -1845,14 +1785,14 @@ namespace CsbBuilder
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);
// 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)
{
// add the new node to track's children
@ -1910,9 +1847,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == treeViewOfCopiedNode)
@ -1935,10 +1871,8 @@ namespace CsbBuilder
TreeNode nodeToPaste = CloneNode(copiedNode);
// 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))
{
cueNode.Identifier++;
@ -2006,10 +1940,8 @@ namespace CsbBuilder
{
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)
{
PasteNode();
@ -2021,9 +1953,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == treeViewOfCopiedNode)
@ -2042,9 +1973,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == synthTree)
@ -2058,9 +1988,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == cueTree)
@ -2070,7 +1999,7 @@ namespace CsbBuilder
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);
}
@ -2102,9 +2031,8 @@ namespace CsbBuilder
{
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;
if (selectedNode.Tag is BuilderSynthNode)
@ -2126,9 +2054,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == aisacTree)
@ -2218,9 +2145,8 @@ namespace CsbBuilder
{
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;
if (selectedTree == aisacTree)

View File

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

View File

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

View File

@ -30,7 +30,7 @@ namespace SonicAudioLib.Archive
{
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())

View File

@ -24,7 +24,7 @@ namespace SonicAudioLib.Archive
{
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);
@ -32,11 +32,11 @@ namespace SonicAudioLib.Archive
uint type = information & 0xFF;
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;
PositionFieldLength = (information & 0x0000FF00) >> 8;
IdFieldLength = (information >> 16) & 0xFF;
PositionFieldLength = (information >> 8) & 0xFF;
ushort entryCount = (ushort)EndianStream.ReadUInt32(source);
Align = EndianStream.ReadUInt32(source);
@ -56,7 +56,7 @@ namespace SonicAudioLib.Archive
break;
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);
@ -73,7 +73,7 @@ namespace SonicAudioLib.Archive
break;
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)
@ -81,7 +81,7 @@ namespace SonicAudioLib.Archive
previousEntry.Length = afs2Entry.Position - previousEntry.Position;
}
afs2Entry.Position = Methods.Align(afs2Entry.Position, Align);
afs2Entry.Position = Helpers.Align(afs2Entry.Position, Align);
if (i == entryCount - 1)
{
@ -123,7 +123,7 @@ namespace SonicAudioLib.Archive
break;
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;
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;

View File

@ -67,7 +67,7 @@ namespace SonicAudioLib.Archive
{
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;
@ -108,9 +108,9 @@ namespace SonicAudioLib.Archive
{
reader.Read();
bool latest = reader.ContainsField("CpkMode");
bool isLatestVersion = reader.ContainsField("CpkMode");
if (latest)
if (isLatestVersion)
{
mode = (CriCpkMode)reader.GetUInt32("CpkMode");
}
@ -166,7 +166,7 @@ namespace SonicAudioLib.Archive
entry.Name = tocReader.GetString("FileName");
entry.Length = tocReader.GetUInt32("FileSize");
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.IsCompressed = entry.Length != tocReader.GetUInt32("ExtractSize");
@ -180,7 +180,7 @@ namespace SonicAudioLib.Archive
entry.Position += tocPosition;
}
entry.Position = Methods.Align(entry.Position, align);
entry.Position = Helpers.Align(entry.Position, align);
etocReader.MoveToRow(tocReader.CurrentRow);
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))
{
@ -244,7 +244,7 @@ namespace SonicAudioLib.Archive
long entryPosition = contentPosition;
foreach (CriCpkEntry entry in entries.OrderBy(entry => entry.Id))
{
entryPosition = Methods.Align(entryPosition, align);
entryPosition = Helpers.Align(entryPosition, align);
entry.Position = entryPosition;
entryPosition += entry.Length;
@ -253,7 +253,7 @@ namespace SonicAudioLib.Archive
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");
@ -262,6 +262,12 @@ namespace SonicAudioLib.Archive
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);
using (CriCpkSection cpkSection = new CriCpkSection(destination, "CPK ", enableMask))
@ -493,7 +499,7 @@ namespace SonicAudioLib.Archive
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();
@ -603,12 +609,6 @@ namespace SonicAudioLib.Archive
((((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 Stream destination;

View File

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

View File

@ -1,8 +1,7 @@
using SonicAudioLib.IO;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
namespace SonicAudioLib.CriMw
@ -88,17 +87,17 @@ namespace SonicAudioLib.CriMw
if (EndianStream.ReadCString(source, 4) != CriTableHeader.Signature)
{
// 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();
Methods.MaskCriTable(source, unmaskedSource, source.Length);
Helpers.MaskCriTable(source, unmaskedSource, source.Length);
// try again
unmaskedSource.Seek(0, SeekOrigin.Begin);
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
@ -116,10 +115,9 @@ namespace SonicAudioLib.CriMw
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)
{
case CriTableHeader.EncodingTypeShiftJis:
@ -131,7 +129,7 @@ namespace SonicAudioLib.CriMw
break;
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);
@ -157,10 +155,7 @@ namespace SonicAudioLib.CriMw
{
if (field.Flag.HasFlag(CriFieldFlag.Data))
{
uint vldPosition;
uint vldLength;
ReadData(out vldPosition, out vldLength);
ReadData(out uint vldPosition, out uint vldLength);
field.Position = vldPosition;
field.Length = vldLength;
@ -175,16 +170,7 @@ namespace SonicAudioLib.CriMw
// 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))
{
if (field.Flag.HasFlag(CriFieldFlag.Data))
{
field.Position = 0;
field.Length = 0;
}
else
{
field.Value = CriField.NullValues[(byte)field.Flag & 0x0F];
}
field.Value = CriField.NullValues[(byte)field.Flag & 0x0F];
}
fields.Add(field);
@ -269,7 +255,7 @@ namespace SonicAudioLib.CriMw
break;
case CriFieldFlag.Int32:
case CriFieldFlag.UInt32:
case CriFieldFlag.Float:
case CriFieldFlag.Single:
case CriFieldFlag.String:
position += 4;
break;
@ -443,12 +429,12 @@ namespace SonicAudioLib.CriMw
return (long)GetValue(fieldName);
}
public float GetFloat(int fieldIndex)
public float GetSingle(int fieldIndex)
{
return (float)GetValue(fieldIndex);
}
public float GetFloat(string fieldName)
public float GetSingle(string fieldName)
{
return (float)GetValue(fieldName);
}
@ -515,11 +501,8 @@ namespace SonicAudioLib.CriMw
return fields[fieldIndex].Length;
}
uint vldPosition;
uint vldLength;
GoToValue(fieldIndex);
ReadData(out vldPosition, out vldLength);
ReadData(out uint vldPosition, out uint vldLength);
return vldLength;
}
@ -540,11 +523,8 @@ namespace SonicAudioLib.CriMw
return fields[fieldIndex].Position;
}
uint vldPosition;
uint vldLength;
GoToValue(fieldIndex);
ReadData(out vldPosition, out vldLength);
ReadData(out uint vldPosition, out uint vldLength);
return (uint)(headerPosition + header.DataPoolPosition + vldPosition);
}
@ -575,9 +555,7 @@ namespace SonicAudioLib.CriMw
private byte[] ReadBytes(int length)
{
byte[] buff = new byte[length];
source.Read(buff, 0, length);
return buff;
return EndianStream.ReadBytes(source, length);
}
private byte ReadByte()
@ -625,9 +603,9 @@ namespace SonicAudioLib.CriMw
return EndianStream.ReadInt64BE(source);
}
private float ReadFloat()
private float ReadSingle()
{
return EndianStream.ReadFloatBE(source);
return EndianStream.ReadSingleBE(source);
}
private double ReadDouble()
@ -638,20 +616,19 @@ namespace SonicAudioLib.CriMw
private string ReadString()
{
uint stringPosition = ReadUInt32();
long previousPosition = source.Position;
source.Position = headerPosition + header.StringPoolPosition + stringPosition;
string strResult = EndianStream.ReadCString(source, encoding);
string readString = EndianStream.ReadCString(source, encoding);
source.Position = previousPosition;
if (strResult == "<NULL>" ||
(strResult == header.TableName && stringPosition == 0))
if (readString == "<NULL>" || (readString == header.TableName && stringPosition == 0))
{
return string.Empty;
}
return strResult;
return readString;
}
private void ReadData(out uint vldPosition, out uint vldLength)
@ -687,25 +664,22 @@ namespace SonicAudioLib.CriMw
return ReadUInt64();
case CriFieldFlag.Int64:
return ReadInt64();
case CriFieldFlag.Float:
return ReadFloat();
case CriFieldFlag.Single:
return ReadSingle();
case CriFieldFlag.Double:
return ReadDouble();
case CriFieldFlag.String:
return ReadString();
case CriFieldFlag.Data:
{
uint vldPosition;
uint vldLength;
ReadData(out vldPosition, out vldLength);
ReadData(out uint vldPosition, out uint vldLength);
// Some ACB files have the length info set to zero for UTF table fields, so find the correct length
if (vldPosition > 0 && vldLength == 0)
{
source.Position = headerPosition + header.DataPoolPosition + vldPosition;
if (EndianStream.ReadCString(source, 4) == "@UTF")
if (EndianStream.ReadCString(source, 4) == CriTableHeader.Signature)
{
vldLength = ReadUInt32() + 8;
}

View File

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

View File

@ -67,7 +67,7 @@ namespace SonicAudioLib.CriMw.Serialization
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) &&
propertyInfo.PropertyType != typeof(Stream) &&
propertyInfo.PropertyType != typeof(bool) &&
@ -161,9 +161,9 @@ namespace SonicAudioLib.CriMw.Serialization
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)
@ -197,9 +197,9 @@ namespace SonicAudioLib.CriMw.Serialization
object value = propertyInfo.GetValue(obj);
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)
@ -299,14 +299,14 @@ namespace SonicAudioLib.CriMw.Serialization
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)

View File

@ -2,23 +2,30 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
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
{
private static byte[] buffer;
/// <summary>
/// Fills <see cref="buffer"/> in the given length.
/// </summary>
private static void FillBuffer(Stream source, int length)
[StructLayout(LayoutKind.Explicit)]
private struct SingleUnion
{
buffer = new byte[length];
source.Read(buffer, 0, length);
[FieldOffset(0)]
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)
@ -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)
{
FillBuffer(source, length);
byte[] buffer = new byte[length];
source.Read(buffer, 0, length);
return buffer;
}
@ -47,10 +79,10 @@ namespace SonicAudioLib.IO
{
long oldPosition = source.Position;
source.Position = position;
FillBuffer(source, length);
var result = ReadBytes(source, length);
source.Position = oldPosition;
return buffer;
return result;
}
public static void WriteBytes(Stream destination, byte[] value)
@ -101,12 +133,12 @@ namespace SonicAudioLib.IO
public static bool ReadBoolean(Stream source)
{
return ReadByte(source) > 0;
return source.ReadByte() > 0;
}
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)
@ -121,226 +153,242 @@ namespace SonicAudioLib.IO
public static ushort ReadUInt16(Stream source)
{
FillBuffer(source, 2);
return BitConverter.ToUInt16(buffer, 0);
return (ushort)(source.ReadByte() | source.ReadByte() << 8);
}
public static ushort ReadUInt16BE(Stream source)
{
FillBuffer(source, 2);
Array.Reverse(buffer);
return BitConverter.ToUInt16(buffer, 0);
return (ushort)(source.ReadByte() << 8 | source.ReadByte());
}
public static void WriteUInt16(Stream destination, ushort value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 2);
destination.WriteByte((byte)(value));
destination.WriteByte((byte)(value >> 8));
}
public static void WriteUInt16BE(Stream destination, ushort value)
{
buffer = BitConverter.GetBytes(value);
Array.Reverse(buffer);
destination.Write(buffer, 0, 2);
destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value));
}
public static short ReadInt16(Stream source)
{
FillBuffer(source, 2);
return BitConverter.ToInt16(buffer, 0);
return (short)(source.ReadByte() | source.ReadByte() << 8);
}
public static short ReadInt16BE(Stream source)
{
FillBuffer(source, 2);
Array.Reverse(buffer);
return BitConverter.ToInt16(buffer, 0);
return (short)(source.ReadByte() << 8 | source.ReadByte());
}
public static void WriteInt16(Stream destination, short value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 2);
destination.WriteByte((byte)(value));
destination.WriteByte((byte)(value >> 8));
}
public static void WriteInt16BE(Stream destination, short value)
{
buffer = BitConverter.GetBytes(value);
Array.Reverse(buffer);
destination.Write(buffer, 0, 2);
destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value));
}
public static uint ReadUInt32(Stream source)
{
FillBuffer(source, 4);
return BitConverter.ToUInt32(buffer, 0);
return (uint)(source.ReadByte() | source.ReadByte() << 8 | source.ReadByte() << 16 | source.ReadByte() << 24);
}
public static uint ReadUInt32BE(Stream source)
{
FillBuffer(source, 4);
Array.Reverse(buffer);
return BitConverter.ToUInt32(buffer, 0);
return (uint)(source.ReadByte() << 24 | source.ReadByte() << 16 | source.ReadByte() << 8 | source.ReadByte());
}
public static void WriteUInt32(Stream destination, uint value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 4);
destination.WriteByte((byte)(value));
destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value >> 16));
destination.WriteByte((byte)(value >> 24));
}
public static void WriteUInt32BE(Stream destination, uint value)
{
buffer = BitConverter.GetBytes(value);
Array.Reverse(buffer);
destination.Write(buffer, 0, 4);
destination.WriteByte((byte)((value >> 24)));
destination.WriteByte((byte)((value >> 16)));
destination.WriteByte((byte)((value >> 8)));
destination.WriteByte((byte)(value));
}
public static int ReadInt32(Stream source)
{
FillBuffer(source, 4);
return BitConverter.ToInt32(buffer, 0);
return source.ReadByte() | source.ReadByte() << 8 | source.ReadByte() << 16 | source.ReadByte() << 24;
}
public static int ReadInt32BE(Stream source)
{
FillBuffer(source, 4);
Array.Reverse(buffer);
return BitConverter.ToInt32(buffer, 0);
return source.ReadByte() << 24 | source.ReadByte() << 16 | source.ReadByte() << 8 | source.ReadByte();
}
public static void WriteInt32(Stream destination, int value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 4);
destination.WriteByte((byte)(value));
destination.WriteByte((byte)(value >> 8));
destination.WriteByte((byte)(value >> 16));
destination.WriteByte((byte)(value >> 24));
}
public static void WriteInt32BE(Stream destination, int value)
{
buffer = BitConverter.GetBytes(value);
Array.Reverse(buffer);
destination.Write(buffer, 0, 4);
destination.WriteByte((byte)((value >> 24)));
destination.WriteByte((byte)((value >> 16)));
destination.WriteByte((byte)((value >> 8)));
destination.WriteByte((byte)(value));
}
public static ulong ReadUInt64(Stream source)
{
FillBuffer(source, 8);
return BitConverter.ToUInt64(buffer, 0);
return ((uint)source.ReadByte() | (ulong)source.ReadByte() << 8 |
(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)
{
FillBuffer(source, 8);
Array.Reverse(buffer);
return BitConverter.ToUInt64(buffer, 0);
return ((ulong)source.ReadByte() << 56 | (ulong)source.ReadByte() << 48 |
(ulong)source.ReadByte() << 40 | (ulong)source.ReadByte() << 32 |
(ulong)source.ReadByte() << 24 | (ulong)source.ReadByte() << 16 |
(ulong)source.ReadByte() << 8 | (uint)source.ReadByte());
}
public static void WriteUInt64(Stream destination, ulong value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 8);
destination.WriteByte((byte)(value));
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)
{
buffer = BitConverter.GetBytes(value);
Array.Reverse(buffer);
destination.Write(buffer, 0, 8);
destination.WriteByte((byte)((value >> 56)));
destination.WriteByte((byte)((value >> 48)));
destination.WriteByte((byte)((value >> 40)));
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)
{
FillBuffer(source, 8);
return BitConverter.ToInt64(buffer, 0);
return source.ReadByte() | source.ReadByte() << 8 |
source.ReadByte() << 16 | source.ReadByte() << 24 |
source.ReadByte() << 32 | source.ReadByte() << 40 |
source.ReadByte() << 48 | source.ReadByte() << 56;
}
public static long ReadInt64BE(Stream source)
{
FillBuffer(source, 8);
Array.Reverse(buffer);
return BitConverter.ToInt64(buffer, 0);
return source.ReadByte() << 56 | source.ReadByte() << 48 |
source.ReadByte() << 40 | source.ReadByte() << 32 |
source.ReadByte() << 24 | source.ReadByte() << 16 |
source.ReadByte() << 8 | source.ReadByte();
}
public static void WriteInt64(Stream destination, long value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 8);
destination.WriteByte((byte)(value));
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)
{
buffer = BitConverter.GetBytes(value);
Array.Reverse(buffer);
destination.Write(buffer, 0, 8);
destination.WriteByte((byte)((value >> 56)));
destination.WriteByte((byte)((value >> 48)));
destination.WriteByte((byte)((value >> 40)));
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);
return BitConverter.ToSingle(buffer, 0);
var union = new SingleUnion();
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 BitConverter.ToSingle(buffer, 0);
return union.Single;
}
public static void WriteFloat(Stream destination, float value)
public static void WriteSingle(Stream destination, float value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 4);
var union = new SingleUnion();
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);
destination.Write(buffer, 0, 4);
WriteUInt32BE(destination, union.UInt);
}
public static double ReadDouble(Stream source)
{
FillBuffer(source, 8);
return BitConverter.ToDouble(buffer, 0);
var union = new DoubleUnion();
union.ULong = ReadUInt64(source);
return union.Double;
}
public static double ReadDoubleBE(Stream source)
{
FillBuffer(source, 8);
var union = new DoubleUnion();
union.ULong = ReadUInt64BE(source);
Array.Reverse(buffer);
return BitConverter.ToDouble(buffer, 0);
return union.Double;
}
public static void WriteDouble(Stream destination, double value)
{
buffer = BitConverter.GetBytes(value);
destination.Write(buffer, 0, 8);
var union = new DoubleUnion();
union.Double = value;
WriteUInt64(destination, union.ULong);
}
public static void WriteDoubleBE(Stream destination, double value)
{
buffer = BitConverter.GetBytes(value);
var union = new DoubleUnion();
union.Double = value;
Array.Reverse(buffer);
destination.Write(buffer, 0, 8);
WriteUInt64BE(destination, union.ULong);
}
public static string ReadCString(Stream source)
@ -350,15 +398,16 @@ namespace SonicAudioLib.IO
public static string ReadCString(Stream source, Encoding encoding)
{
var list = new List<byte>();
var characters = new List<byte>();
byte buff;
while ((buff = ReadByte(source)) != 0)
byte character = (byte)source.ReadByte();
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)
@ -368,14 +417,10 @@ namespace SonicAudioLib.IO
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)
{
WriteByte(destination, _buff);
}
WriteByte(destination, 0);
destination.Write(buffer, 0, buffer.Length);
destination.WriteByte(0);
}
public static string ReadCString(Stream source, int length)

View File

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

View File

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

View File

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