gnattu 1 month ago committed by GitHub
commit 79d837522e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -23,6 +23,20 @@ namespace Emby.Server.Implementations.Serialization
static (_, t) => new XmlSerializer(t),
type);
private static void ThrowCollectibleException(NotSupportedException e)
{
var isCollectibleException = string.Equals(e.Message, "A non-collectible assembly may not reference a collectible assembly.", StringComparison.Ordinal);
if (isCollectibleException)
{
throw new NotSupportedException(
$"It seems you are using the server's XML serializer in your plugin.\n" +
$"This serializer is only used by the server to handle the plugin's config file " +
$"and is not intended for the plugin to use directly.\n" +
$"Please create your own XML serializer instance to handle plugin specific files.",
e);
}
}
/// <summary>
/// Serializes to writer.
/// </summary>
@ -41,6 +55,8 @@ namespace Emby.Server.Implementations.Serialization
/// <param name="stream">The stream.</param>
/// <returns>System.Object.</returns>
public object? DeserializeFromStream(Type type, Stream stream)
{
try
{
using (var reader = XmlReader.Create(stream))
{
@ -48,6 +64,12 @@ namespace Emby.Server.Implementations.Serialization
return netSerializer.Deserialize(reader);
}
}
catch (NotSupportedException e)
{
ThrowCollectibleException(e);
throw;
}
}
/// <summary>
/// Serializes to stream.
@ -55,6 +77,8 @@ namespace Emby.Server.Implementations.Serialization
/// <param name="obj">The obj.</param>
/// <param name="stream">The stream.</param>
public void SerializeToStream(object obj, Stream stream)
{
try
{
using (var writer = new StreamWriter(stream, null, IODefaults.StreamWriterBufferSize, true))
using (var textWriter = new XmlTextWriter(writer))
@ -63,6 +87,12 @@ namespace Emby.Server.Implementations.Serialization
SerializeToWriter(obj, textWriter);
}
}
catch (NotSupportedException e)
{
ThrowCollectibleException(e);
throw;
}
}
/// <summary>
/// Serializes to file.
@ -70,12 +100,20 @@ namespace Emby.Server.Implementations.Serialization
/// <param name="obj">The obj.</param>
/// <param name="file">The file.</param>
public void SerializeToFile(object obj, string file)
{
try
{
using (var stream = new FileStream(file, FileMode.Create, FileAccess.Write))
{
SerializeToStream(obj, stream);
}
}
catch (NotSupportedException e)
{
ThrowCollectibleException(e);
throw;
}
}
/// <summary>
/// Deserializes from file.
@ -84,12 +122,20 @@ namespace Emby.Server.Implementations.Serialization
/// <param name="file">The file.</param>
/// <returns>System.Object.</returns>
public object? DeserializeFromFile(Type type, string file)
{
try
{
using (var stream = File.OpenRead(file))
{
return DeserializeFromStream(type, stream);
}
}
catch (NotSupportedException e)
{
ThrowCollectibleException(e);
throw;
}
}
/// <summary>
/// Deserializes from bytes.
@ -98,11 +144,19 @@ namespace Emby.Server.Implementations.Serialization
/// <param name="buffer">The buffer.</param>
/// <returns>System.Object.</returns>
public object? DeserializeFromBytes(Type type, byte[] buffer)
{
try
{
using (var stream = new MemoryStream(buffer, 0, buffer.Length, false, true))
{
return DeserializeFromStream(type, stream);
}
}
catch (NotSupportedException e)
{
ThrowCollectibleException(e);
throw;
}
}
}
}

Loading…
Cancel
Save