-
Notifications
You must be signed in to change notification settings - Fork 7
File Management System
The File Management System is a simple to use system for serialising and deserialising objects to and from xml files. It will enable you to save instance data for use between game sessions.
This is a quick overview of the use of the FileDatabase static class. For a more in-depth look, check out the FileDatabase wiki page.
To use the File Management System, your object needs to implement the ISerialisableFile interface. This is located in the ModLib.Interfaces namespace.
To load files for your mod, you will need to call the public static bool Initialise(string moduleName)
method in the FileDatabase static class.
This should be one of the first things you do when your mod loads. As such, it should be called inside the OnSubModuleLoad()
method of your SubModule class. FileDatabase.Initialise()
takes one argument - the folder name of your module. It is recommended to store your ModuleFolderName in a static variable somewhere, as it is used in other cases as well.
Example:
public const string ModuleFolderName = "TestMod";
protected override void OnSubModuleLoad()
{
try
{
FileDatabase.Initialise(ModuleFolderName);
}
catch(Exception ex)
{
//Handle exceptions here
}
}
This will load all files located in the Modules/TestMod/ModuleData/Loadables
directory, including sub-folders within that directory.
To retrieve an instance of a deserialised object from the FileDatabase, you can call the public static T Get<T>(string id)
method. You will need to know the ID of the instance that you want to retrieve.
SomeClass someClass = FileDatabase.Get<SomeClass>("someID");
If the ID or the type doesn't exist within the FileDatabase, this method will return null.
if(someClass !=null)
{
//Do something
}
To save an instance of an object to file, you can call the public static bool SaveToFile(string moduleFolderName, ISerialisableFile sf, Location location = Location.Modules)
method.
This method will save your instance to file and return a bool value to show whether the operation was successful or not.
Saving an instance will also add it to the FileDatabase if you wish to retrieve it from somewhere else in your mod.
SomeClass someClass = new SomeClass();
FileDatabase.SaveToFile(ModuleFolderName, someClass);
You can optionally choose the location that the file is saved to - the ModuleData/Loadables
or the Configs
folder (which is located in My Documents
.
Only properties marked with the [XmlElement]
attribute from the System.Xml.Serialization
namespace will be serialised.