ConfigurationHelper.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Model.Serialization;
  4. namespace Emby.Server.Implementations.AppBase
  5. {
  6. /// <summary>
  7. /// Class ConfigurationHelper.
  8. /// </summary>
  9. public static class ConfigurationHelper
  10. {
  11. /// <summary>
  12. /// Reads an xml configuration file from the file system
  13. /// It will immediately re-serialize and save if new serialization data is available due to property changes.
  14. /// </summary>
  15. /// <param name="type">The type.</param>
  16. /// <param name="path">The path.</param>
  17. /// <param name="xmlSerializer">The XML serializer.</param>
  18. /// <returns>System.Object.</returns>
  19. public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
  20. {
  21. object configuration;
  22. byte[]? buffer = null;
  23. // Use try/catch to avoid the extra file system lookup using File.Exists
  24. try
  25. {
  26. buffer = File.ReadAllBytes(path);
  27. configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
  28. }
  29. catch (Exception)
  30. {
  31. // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
  32. configuration = Activator.CreateInstance(type)!;
  33. }
  34. using var stream = new MemoryStream(buffer?.Length ?? 0);
  35. xmlSerializer.SerializeToStream(configuration, stream);
  36. // Take the object we just got and serialize it back to bytes
  37. Span<byte> newBytes = stream.GetBuffer().AsSpan(0, (int)stream.Length);
  38. // If the file didn't exist before, or if something has changed, re-save
  39. if (buffer is null || !newBytes.SequenceEqual(buffer))
  40. {
  41. var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));
  42. Directory.CreateDirectory(directory);
  43. // Save it after load in case we got new items
  44. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
  45. {
  46. fs.Write(newBytes);
  47. }
  48. }
  49. return configuration;
  50. }
  51. }
  52. }