aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/AppBase/ConfigurationHelper.cs
blob: f923e59efb9292ae9d077f8e164a55e356c01781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.IO;
using MediaBrowser.Model.Serialization;

namespace Emby.Server.Implementations.AppBase
{
    /// <summary>
    /// Class ConfigurationHelper.
    /// </summary>
    public static class ConfigurationHelper
    {
        /// <summary>
        /// Reads an xml configuration file from the file system
        /// It will immediately re-serialize and save if new serialization data is available due to property changes.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="path">The path.</param>
        /// <param name="xmlSerializer">The XML serializer.</param>
        /// <returns>System.Object.</returns>
        public static object GetXmlConfiguration(Type type, string path, IXmlSerializer xmlSerializer)
        {
            object configuration;

            byte[]? buffer = null;

            // Use try/catch to avoid the extra file system lookup using File.Exists
            try
            {
                buffer = File.ReadAllBytes(path);

                configuration = xmlSerializer.DeserializeFromBytes(type, buffer);
            }
            catch (Exception)
            {
                // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
                configuration = Activator.CreateInstance(type)!;
            }

            using var stream = new MemoryStream(buffer?.Length ?? 0);
            xmlSerializer.SerializeToStream(configuration, stream);

            // Take the object we just got and serialize it back to bytes
            Span<byte> newBytes = stream.GetBuffer().AsSpan(0, (int)stream.Length);

            // If the file didn't exist before, or if something has changed, re-save
            if (buffer == null || !newBytes.SequenceEqual(buffer))
            {
                var directory = Path.GetDirectoryName(path) ?? throw new ArgumentException($"Provided path ({path}) is not valid.", nameof(path));

                Directory.CreateDirectory(directory);

                // Save it after load in case we got new items
                using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    fs.Write(newBytes);
                }
            }

            return configuration;
        }
    }
}