/// <summary> /// Creates a new table for storing plugin metadata. /// </summary> private void CreatePluginsTable() { try { using (LockedFile file = new LockedFile(pluginsFileName, pluginsFileMutex)) { // If the file contains content, it doesn't need initialising if (!file.IsEmpty) { return; } XDocument doc = new XDocument(); if (doc.Root == null) { doc.Add(new XElement("plugins", new XAttribute("nextID", 0))); } file.Save(doc); } } 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; } }
/// <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; } }
/// <summary> /// Deletes a record from the plugin table. /// </summary> /// <param name="name">The plugin to delete.</param> internal void DeletePluginRecord(string name) { try { using (LockedFile file = new LockedFile(pluginsFileName, pluginsFileMutex)) { XDocument doc = file.Load(); doc.Root .Elements() .Single(e => e.Attribute("name").Value == name) .Remove(); file.Save(doc); } } 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; } }