Пример #1
0
        /// <summary>
        ///     Atomically reads a plugin record and updates the fields as specified or
        ///     creates a new record if not present.
        /// </summary>
        /// <param name="name">The plugin to read and set.</param>
        /// <param name="version">The version to update the record to.</param>
        internal PluginRecord ReadAndSetPluginRecord(string name, Version version)
        {
            try
            {
                using (LockedFile file = new LockedFile(pluginsFileName, pluginsFileMutex))
                {
                    XDocument doc = file.Load();

                    XElement element = doc.Root
                                       .Elements()
                                       .FirstOrDefault(x => x.Attribute("name").Value == name);

                    //If not there, build it
                    PluginRecord record;
                    if (element == null)
                    {
                        element = new XElement("plugin");

                        var  idAttribute = doc.Root.Attribute("nextID");
                        uint id          = uint.Parse(idAttribute.Value);
                        idAttribute.SetValue(id + 1);

                        element.SetAttributeValue("id", id);
                        element.SetAttributeValue("name", name);

                        doc.Root.Add(element);

                        record = null;
                    }
                    else
                    {
                        record = new PluginRecord(
                            uint.Parse(element.Attribute("id").Value),
                            name,
                            new Version(element.Attribute("version").Value)
                            );
                    }

                    element.SetAttributeValue("version", version);

                    file.Save(doc);

                    return(record);
                }
            }
            catch (XmlException)
            {
                logger.Error($"The plugins index file ({pluginsFileName}) was corrupt and could not be loaded. It may be possible to fix this manually by inspecting the file; otherwise, it is likely that you will need to delete the file to force DarkRift to regenerate it, however doing so will cause all plugins to reinstall.");
                throw;
            }
        }