Esempio n. 1
0
        private static IEnumerable <ConfigurationData> GetConfigurationArray(ExtensionConfigurationData configuration, bool global)
        {
            IEnumerable <ConfigurationData> result = null;

            var items = configuration?.Properties;

            if (items?.Any() ?? false)
            {
                result = items.Select(x => new ConfigurationData(x.Key, x.Value, global));
            }

            return(result);
        }
Esempio n. 2
0
        private static ExtensionConfigurationData GetConfigurationObject(IEnumerable <ConfigurationData> configuration)
        {
            ExtensionConfigurationData result = null;

            var items = configuration?.ToArray();

            if (items?.Any() ?? false)
            {
                result = new ExtensionConfigurationData()
                {
                    Properties = items.ToDictionary(x => x.Name, y => y.Value)
                };
            }

            return(result);
        }
Esempio n. 3
0
        private static ExtensionConfigurationData GetGlobalConfiguration([Required] string extensionId, [Required] string folder)
        {
            ExtensionConfigurationData result = null;

            var pathName = Path.Combine(folder, $"{extensionId}.tmg");

            if (File.Exists(pathName))
            {
                using (var file = File.OpenRead(pathName))
                {
                    using (var ms = new MemoryStream())
                    {
                        file.CopyTo(ms);

                        string jsonText;
                        var    json = ms.ToArray();
                        if (json.Length > 0)
                        {
                            if (json[0] == 0xFF)
                            {
                                jsonText = Encoding.Unicode.GetString(json, 2, json.Length - 2);
                            }
                            else
                            {
                                jsonText = Encoding.Unicode.GetString(json);
                            }

                            using (var textReader = new StringReader(jsonText))
                                using (var reader = new JsonTextReader(textReader))
                                {
                                    var serializer = new JsonSerializer
                                    {
                                        TypeNameHandling      = TypeNameHandling.All,
                                        SerializationBinder   = new KnownTypesBinder(),
                                        MissingMemberHandling = MissingMemberHandling.Ignore
                                    };
                                    result = serializer.Deserialize <ExtensionConfigurationData>(reader);
                                }
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
        private static void SaveGlobalConfiguration([Required] string extensionId, [Required] string folder, ExtensionConfigurationData config)
        {
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            var pathName = Path.Combine(folder, $"{extensionId}.tmg");

            if (File.Exists(pathName))
            {
                File.Delete(pathName);
            }

            using (var file = File.OpenWrite(pathName))
            {
                using (var writer = new BinaryWriter(file))
                {
                    StringBuilder sb = new StringBuilder();
                    StringWriter  sw = new StringWriter(sb);

                    using (JsonWriter jtw = new JsonTextWriter(sw))
                    {
                        var serializer = new JsonSerializer {
                            TypeNameHandling = TypeNameHandling.All, Formatting = Formatting.Indented
                        };
                        serializer.Serialize(jtw, config);
                    }

                    var serialization = Encoding.Unicode.GetBytes(sb.ToString());

                    writer.Write((byte)0xFF);
                    writer.Write((byte)0xFE);
                    writer.Write(serialization);
                }
            }
        }