Simplefied PCKAudioFile.Categories and removed null check when looping over PCKAudioFile.Categories

This commit is contained in:
miku-666
2022-08-06 06:23:41 +02:00
parent 2c2fd9d691
commit 6466dfcf8e
5 changed files with 10 additions and 38 deletions

View File

@@ -16,7 +16,8 @@ namespace PckStudio.Classes.FileTypes
public readonly int type = 1;
public AudioCategory[] Categories { get; } = new AudioCategory[8];
public AudioCategory[] Categories => Array.FindAll(_categories, c => c is not null);
private AudioCategory[] _categories { get; } = new AudioCategory[8];
public Dictionary<string, string> Credits { get; } = new Dictionary<string, string>();
@@ -117,7 +118,7 @@ namespace PckStudio.Classes.FileTypes
if (category < AudioCategory.EAudioType.Overworld ||
category > AudioCategory.EAudioType.Unused)
throw new InvalidCategoryException(nameof(category));
return Categories[(int)category];
return _categories[(int)category];
}
/// <exception cref="InvalidCategoryException"></exception>
@@ -131,13 +132,6 @@ namespace PckStudio.Classes.FileTypes
audioCategory = null;
return false;
}
public int GetCategoryCount()
{
int count = 0;
Array.ForEach(Categories, c => { if (c is not null) count++; });
return count;
}
/// <returns>True when category was created, otherwise false</returns>
/// <exception cref="InvalidCategoryException"></exception>
@@ -147,7 +141,7 @@ namespace PckStudio.Classes.FileTypes
category > AudioCategory.EAudioType.Unused)
throw new InvalidCategoryException(nameof(category));
bool exists = HasCategory(category);
if (!exists) Categories[(int)category] = new AudioCategory(parameterType, category);
if (!exists) _categories[(int)category] = new AudioCategory(parameterType, category);
return !exists;
}
@@ -161,7 +155,7 @@ namespace PckStudio.Classes.FileTypes
public bool RemoveCategory(AudioCategory.EAudioType category)
{
bool exists = HasCategory(category);
if (exists) Categories[(int)category] = null;
if (exists) _categories[(int)category] = null;
return exists;
}

View File

@@ -75,7 +75,6 @@ namespace PckStudio.Classes.IO.PCK
List<string> creditIds = new List<string>();
foreach (var c in _file.Categories)
{
if (c is null) continue;
int audioCount = ReadInt(stream);
for (; 0 < audioCount; audioCount--)
{

View File

@@ -57,10 +57,9 @@ namespace PckStudio.Classes.IO.PCK
private void WriteCategories(Stream stream)
{
WriteInt(stream, _file.GetCategoryCount());
WriteInt(stream, _file.Categories.Length);
foreach (var category in _file.Categories)
{
if (category is null) continue;
WriteInt(stream, (int)category.parameterType);
WriteInt(stream, (int)category.audioType);
WriteString(stream, category.Name);
@@ -72,7 +71,6 @@ namespace PckStudio.Classes.IO.PCK
bool addCredit = true;
foreach (var category in _file.Categories)
{
if (category is null) continue;
WriteInt(stream, category.SongNames.Count + (addCredit ? _file.Credits.Count * 2 : 0));
foreach (var name in category.SongNames)
{

View File

@@ -27,18 +27,6 @@ namespace PckStudio.Forms.Editor
LOCFile loc;
bool _isLittleEndian = false;
public class CategorySorter : IComparer
{
public int Compare(object x, object y)
{
if (x is TreeNode xt && y is TreeNode yt &&
xt.Tag is PCKAudioFile.AudioCategory xcategory &&
yt.Tag is PCKAudioFile.AudioCategory ycategory)
return xcategory.audioType.CompareTo(ycategory.audioType);
return -1;
}
}
public static readonly List<string> Categories = new List<string>
{
"Overworld",
@@ -112,10 +100,9 @@ namespace PckStudio.Forms.Editor
foreach (var category in audioFile.Categories)
{
if (category is null) continue;
if (category.Name == "include_overworld" &&
category.audioType == PCKAudioFile.AudioCategory.EAudioType.Creative &&
audioFile.Categories[0] is PCKAudioFile.AudioCategory overworldCategory)
audioFile.TryGetCategory(PCKAudioFile.AudioCategory.EAudioType.Overworld, out PCKAudioFile.AudioCategory overworldCategory))
{
foreach(var name in category.SongNames.ToList())
{
@@ -125,17 +112,12 @@ namespace PckStudio.Forms.Editor
playOverworldInCreative.Checked = true;
}
TreeNode treeNode = new TreeNode(GetCategoryFromId(category.audioType));
TreeNode treeNode = new TreeNode(GetCategoryFromId(category.audioType), (int)category.audioType, (int)category.audioType);
treeNode.Tag = category;
treeNode.ImageIndex = (int)category.audioType;
treeNode.SelectedImageIndex = (int)category.audioType;
treeView1.Nodes.Add(treeNode);
}
playOverworldInCreative.Enabled = audioFile.HasCategory(PCKAudioFile.AudioCategory.EAudioType.Creative);
treeView1.TreeViewNodeSorter = new CategorySorter();
treeView1.Sort();
}
// https://stackoverflow.com/a/25064568 by Alik Khilazhev -MattNL
@@ -213,7 +195,7 @@ namespace PckStudio.Forms.Editor
string[] avalible = Categories.FindAll(str => !audioFile.HasCategory(GetCategoryId(str)) ).ToArray();
if (avalible.Length > 0)
{
using addCategory add = new addCategory(avalible); //sets category adding dialog
using addCategory add = new addCategory(avalible);
if (add.ShowDialog() == DialogResult.OK)
audioFile.AddCategory(GetCategoryId(add.Category));
}
@@ -369,7 +351,6 @@ namespace PckStudio.Forms.Editor
PCKAudioFile.AudioCategory overworldCategory = audioFile.GetCategory(PCKAudioFile.AudioCategory.EAudioType.Overworld);
foreach (var category in audioFile.Categories)
{
if (category is null) continue;
category.Name = "";
if (playOverworldInCreative.Checked && category.audioType == PCKAudioFile.AudioCategory.EAudioType.Creative)
{

View File

@@ -140,7 +140,7 @@
<ItemGroup>
<Compile Include="Classes\API\PCKCenter\model\PCKCenterJSON.cs" />
<Compile Include="Classes\API\PCKCenter\SaveLocalJSON.cs" />
<Compile Include="Classes\FileTypes\AudioPCKFile.cs" />
<Compile Include="Classes\FileTypes\PCKAudioFile.cs" />
<Compile Include="Classes\FileTypes\Bink.cs" />
<Compile Include="Classes\FileTypes\COLFile.cs" />
<Compile Include="Classes\FileTypes\CSM.cs" />