Пример #1
0
        private static ISubFolder FindSubItem(ISubFolder parent, string[] paths, int begin)
        {
            ISubFolder subItem;
            ISubFolder current = parent;

            for (int i = begin; i < paths.Length; i++)
            {
                subItem = FindItem(current.SubItems, paths[i]);
                if (subItem == null)
                {
                    current = CreateFullPath(current, paths, i);
                    break;
                }
                current = subItem;
            }
            return(current);
        }
Пример #2
0
        private static ISubFolder CreateFullPath(ISubFolder parent, string[] paths, int begin)
        {
            ISubFolder top = parent;

            if (top == null)
            {
                top = new SmartFolder(paths[begin]);
                begin++;
            }
            for (int i = begin; i < paths.Length; i++)
            {
                ISubFolder current = new SubFolder(paths[i]);
                top.Add(current);
                top = current;
            }
            return(top);
        }
Пример #3
0
        /// <summary>
        /// Saves the given object instance which inherits ISerialisableFile to an xml file.
        /// </summary>
        /// <param name="moduleName">The folder name of the module to save to.</param>
        /// <param name="sf">Instance of the object to save to file.</param>
        /// <param name="location">Indicates whether to save the file to the ModuleData/Loadables folder or to the mod's Config folder in Bannerlord's 'My Documents' directory.</param>
        public static bool SaveToFile(string moduleName, ISerialisableFile sf, Location location = Location.Modules)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(sf.ID))
                {
                    throw new Exception($"FileDatabase tried to save an object of type {sf.GetType().FullName} but the ID value was null.");
                }
                if (string.IsNullOrWhiteSpace(moduleName))
                {
                    throw new Exception($"FileDatabase tried to save an object of type {sf.GetType().FullName} with ID {sf.ID} but the module folder name given was null or empty.");
                }

                //Gets the intended path for the file.
                string path = GetPathForModule(moduleName, location);

                if (location == Location.Modules && !Directory.Exists(path))
                {
                    throw new Exception($"FileDatabase cannot find the module named {moduleName}");
                }
                else if (location == Location.Configs && !Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                if (location == Location.Modules)
                {
                    path = Path.Combine(path, "ModuleData", LoadablesFolderName);
                }

                if (sf is ISubFolder)
                {
                    ISubFolder subFolder = sf as ISubFolder;
                    if (!string.IsNullOrWhiteSpace(subFolder.SubFolder))
                    {
                        path = Path.Combine(path, subFolder.SubFolder);
                    }
                }

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                path = Path.Combine(path, GetFileNameFor(sf));

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

                using (XmlWriter writer = XmlWriter.Create(path, new XmlWriterSettings()
                {
                    Indent = true, OmitXmlDeclaration = true
                }))
                {
                    XmlRootAttribute rootNode = new XmlRootAttribute();
                    rootNode.ElementName = $"{sf.GetType().Assembly.GetName().Name}-{sf.GetType().FullName}";
                    XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
                    var serializer = new XmlSerializer(sf.GetType(), rootNode);
                    serializer.Serialize(writer, sf, xmlns);
                }
                return(true);
            }
            catch (Exception ex)
            {
                ModDebug.ShowError($"Cannot create the file for type {sf.GetType().FullName} with ID {sf.ID} for module {moduleName}:", "Error saving to file", ex);
                return(false);
            }
        }