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
@@ -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;
}
@@ -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--)
{
@@ -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)
{