mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Start populating serializer framework
This commit is contained in:
7
SabreTools.Serialization/ArchiveDotOrg.cs
Normal file
7
SabreTools.Serialization/ArchiveDotOrg.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// XML deserializer for archive.org metadata files
|
||||
/// </summary>
|
||||
public class ArchiveDotOrg : Serializer<Models.ArchiveDotOrg.Files> { }
|
||||
}
|
||||
7
SabreTools.Serialization/Listxml.cs
Normal file
7
SabreTools.Serialization/Listxml.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// XML deserializer for MAME listxml files
|
||||
/// </summary>
|
||||
public class Listxml : Serializer<Models.Listxml.Mame> { }
|
||||
}
|
||||
7
SabreTools.Serialization/OfflineList.cs
Normal file
7
SabreTools.Serialization/OfflineList.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// XML deserializer for OfflineList metadata files
|
||||
/// </summary>
|
||||
public class OfflineList : Serializer<Models.OfflineList.Dat> { }
|
||||
}
|
||||
7
SabreTools.Serialization/OpenMSX.cs
Normal file
7
SabreTools.Serialization/OpenMSX.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// XML deserializer for OpenMSX software database files
|
||||
/// </summary>
|
||||
public class OpenMSX : Serializer<Models.OpenMSX.SoftwareDb> { }
|
||||
}
|
||||
50
SabreTools.Serialization/PathProcessor.cs
Normal file
50
SabreTools.Serialization/PathProcessor.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
internal class PathProcessor
|
||||
{
|
||||
/// <summary>
|
||||
/// Opens a path as a stream in a safe manner, decompressing if needed
|
||||
/// </summary>
|
||||
/// <param name="path">Path to open as a stream</param>
|
||||
/// <returns>Stream representing the file, null on error</returns>
|
||||
public static Stream? OpenStream(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
// If we don't have a file
|
||||
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
|
||||
return null;
|
||||
|
||||
// Open the file for deserialization
|
||||
var stream = File.OpenRead(path);
|
||||
|
||||
// Get the extension to determine if additional handling is needed
|
||||
string ext = Path.GetExtension(path).TrimStart('.');
|
||||
|
||||
// Determine what we do based on the extension
|
||||
if (string.Equals(ext, "gz", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return new GZipStream(stream, CompressionMode.Decompress);
|
||||
}
|
||||
else if (string.Equals(ext, "zip", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// TODO: Support zip-compressed files
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// TODO: Handle logging the exception
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
64
SabreTools.Serialization/Serializer.cs
Normal file
64
SabreTools.Serialization/Serializer.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// XML deserializer for nullable types
|
||||
/// </summary>
|
||||
public abstract class Serializer<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Deserializes an XML file to the defined type
|
||||
/// </summary>
|
||||
/// <param name="path">Path to the file to deserialize</param>
|
||||
/// <returns>Deserialized data on success, null on failure</returns>
|
||||
public static T? Deserialize(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = PathProcessor.OpenStream(path);
|
||||
return Deserialize(stream);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// TODO: Handle logging the exception
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes an XML file in a stream to the defined type
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream to deserialize</param>
|
||||
/// <returns>Deserialized data on success, null on failure</returns>
|
||||
public static T? Deserialize(Stream? stream)
|
||||
{
|
||||
try
|
||||
{
|
||||
// If the stream is null
|
||||
if (stream == null)
|
||||
return default;
|
||||
|
||||
// Setup the serializer and the reader
|
||||
var serializer = new XmlSerializer(typeof(T));
|
||||
var settings = new XmlReaderSettings
|
||||
{
|
||||
CheckCharacters = false,
|
||||
DtdProcessing = DtdProcessing.Ignore,
|
||||
};
|
||||
var streamReader = new StreamReader(stream);
|
||||
var xmlReader = XmlReader.Create(streamReader, settings);
|
||||
|
||||
// Perform the deserialization and return
|
||||
return (T?)serializer.Deserialize(xmlReader);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// TODO: Handle logging the exception
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
7
SabreTools.Serialization/SoftwareList.cs
Normal file
7
SabreTools.Serialization/SoftwareList.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// XML deserializer for MAME softwarelist files
|
||||
/// </summary>
|
||||
public class SoftawreList : Serializer<Models.SoftwareList.SoftwareList> { }
|
||||
}
|
||||
Reference in New Issue
Block a user