2023-08-10 11:35:32 -04:00
|
|
|
using System;
|
2023-08-10 01:20:42 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SabreTools.Models.Internal;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Serialization
|
|
|
|
|
{
|
|
|
|
|
public class Internal
|
|
|
|
|
{
|
2023-08-10 11:35:32 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Extract nested items from a Dump
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static DatItem[]? ExtractItems(Dump? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var datItems = new List<DatItem>();
|
|
|
|
|
|
|
|
|
|
var rom = item.Read<Rom>(Dump.RomKey);
|
|
|
|
|
if (rom != null)
|
|
|
|
|
datItems.Add(rom);
|
|
|
|
|
|
|
|
|
|
var megaRom = item.Read<Rom>(Dump.MegaRomKey);
|
|
|
|
|
if (megaRom != null)
|
|
|
|
|
datItems.Add(megaRom);
|
|
|
|
|
|
|
|
|
|
var sccPlusCart = item.Read<Rom>(Dump.SCCPlusCartKey);
|
|
|
|
|
if (sccPlusCart != null)
|
|
|
|
|
datItems.Add(sccPlusCart);
|
|
|
|
|
|
|
|
|
|
return datItems.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 01:20:42 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Extract nested items from a Part
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static DatItem[]? ExtractItems(Part? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var datItems = new List<DatItem>();
|
|
|
|
|
|
|
|
|
|
var features = item.Read<Feature[]>(Part.FeatureKey);
|
|
|
|
|
if (features != null && features.Any())
|
|
|
|
|
datItems.AddRange(features);
|
|
|
|
|
|
|
|
|
|
var dataAreas = item.Read<DataArea[]>(Part.DataAreaKey);
|
|
|
|
|
if (dataAreas != null && dataAreas.Any())
|
2023-08-10 11:35:32 -04:00
|
|
|
{
|
|
|
|
|
datItems.AddRange(dataAreas
|
|
|
|
|
.Where(d => d != null)
|
|
|
|
|
.SelectMany(ExtractItems));
|
|
|
|
|
}
|
2023-08-10 01:20:42 -04:00
|
|
|
|
|
|
|
|
var diskAreas = item.Read<DiskArea[]>(Part.DiskAreaKey);
|
|
|
|
|
if (diskAreas != null && diskAreas.Any())
|
2023-08-10 11:35:32 -04:00
|
|
|
{
|
|
|
|
|
datItems.AddRange(diskAreas
|
|
|
|
|
.Where(d => d != null)
|
|
|
|
|
.SelectMany(ExtractItems));
|
|
|
|
|
}
|
2023-08-10 01:20:42 -04:00
|
|
|
|
|
|
|
|
var dipSwitches = item.Read<DipSwitch[]>(Part.DipSwitchKey);
|
|
|
|
|
if (dipSwitches != null && dipSwitches.Any())
|
2023-08-10 11:35:32 -04:00
|
|
|
{
|
|
|
|
|
datItems.AddRange(dipSwitches
|
|
|
|
|
.Where(d => d != null));
|
|
|
|
|
}
|
2023-08-10 01:20:42 -04:00
|
|
|
|
|
|
|
|
return datItems.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extract nested items from a DataArea
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static Rom[] ExtractItems(DataArea item)
|
2023-08-10 01:20:42 -04:00
|
|
|
{
|
2023-08-10 11:35:32 -04:00
|
|
|
var roms = item.Read<Rom[]>(DataArea.RomKey);
|
|
|
|
|
if (roms == null || !roms.Any())
|
|
|
|
|
return Array.Empty<Rom>();
|
2023-08-10 01:20:42 -04:00
|
|
|
|
2023-08-10 11:35:32 -04:00
|
|
|
return roms.ToArray();
|
2023-08-10 01:20:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extract nested items from a DiskArea
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static Disk[] ExtractItems(DiskArea item)
|
2023-08-10 01:20:42 -04:00
|
|
|
{
|
2023-08-10 11:35:32 -04:00
|
|
|
var roms = item.Read<Disk[]>(DiskArea.DiskKey);
|
|
|
|
|
if (roms == null || !roms.Any())
|
|
|
|
|
return Array.Empty<Disk>();
|
2023-08-10 01:20:42 -04:00
|
|
|
|
2023-08-10 11:35:32 -04:00
|
|
|
return roms.ToArray();
|
2023-08-10 01:20:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|