-
Notifications
You must be signed in to change notification settings - Fork 7
FileDatabase
public static bool Initialise(string moduleFolderName)
-
moduleFolderName- The name of the folder for the mod which is calling the method.
-
bool- Returnstrueif the operation was successful and all files were loaded. Returnsfalseif an exception occurred.
This method is used to load all files for the given mod. It should be called on startup, for example in the SubModule.OnSubModuleLoad method. Pass the folder name of your module to the method and it will load all files into the FileDatabase. Object instances can then be be retrieved using the Get<T> method.
public static T Get<T>(string id)
-
T- the type of the object to be retrieved from the FileDatabase. -
id- The ID of the object to be retrieved from the FileDatabase.
-
T- Returns the instance of the object of typeTwith the given ID from the FileDatabase. If there is no typeTor the ID doesn't exist in the FileDatabase, returnsnull.
This method is used to retrieve an instance of an object from the FileDatabase. Pass the type T and the id and the method will search the FileDatabase for an instance of type T with the same id. If it cannot find anything, it will return null. The best practice is to check if the retrieved object is null before doing something with it.
SomeClass someClass = FileDatabase.Get<SomeClass>("someID");
if(someClass !=null)
{
//Do something
}
public static bool SaveToFile(string moduleFolderName, ISerialisableFile sf, Location location = Location.Modules)
-
moduleFolderName- The folder name of your module. -
sf- The instance of the object to be saved to file. -
location- Optional: The location to save the file to. It will save it to theModuleData/Loadablesfolder by default, but can be set to save to the Configs folder if desired.
-
bool- Returnstrueif the operation was successful. Returnsfalseif an exception occurred.
Calling the SaveToFile method will save the given instance to an xml file immediately. It will also add the object instance to the FileDatabase for retrieval later using the Get<T> method.
public static bool DeleteFile(string moduleFolderName, string fileName, Location location = Location.Modules)
-
moduleFolderName- The name of the module folder to find the file in. -
fileName- The name of the file to delete. -
location- Optional: The location of the file, either theModuleData/Loadablesfolder or theConfigsfolder.
public static bool DeleteFile(string moduleFolderName, ISerialisableFile sf, Location location = Location.Modules)
-
sf- The instance of the object whose file should be deleted can optionally be passed as an argument. This will create thefileNameargument from the passed instance for the default method.
-
bool- Returnstrueif the file was successfully deleted. Returnsfalseif there was an exception or the file couldn't be found.
If you need to remove an existing file created for an object, you can use the DeleteFile method to remove it. The method only looks in the given moduleFolderName folder. If the directory created from moduleFolderName doesn't exist, the method will throw an exception.
public static string GetFileNameFor(ISerialisableFile sf)
-
sf- The instance of the object to get the file name for.
-
string- Returns the file name for the given object instance. This takes the form"{object's type name}.{object's id}.xml".
Use this method to retrieve the file name for the given object instance in the format that the FileDatabase uses.
public static string GetPathForModule(string moduleFolderName, Location location)
-
moduleFolderName- The name of the folder for the module that is calling the method. -
location- The intended location that the path should point towards. This will either be theModules/ModuleFolderNamefolder or theConfigsfolder.
-
string- Returns the path that the calling module will use.
This method returns either the module's Configs folder or the root folder for that module. For example, calling:
string path = FileDatabase.GetPathForModule("TestMod",Location.Modules);
Will return the following path (BannerlordDirectory)/Modules/TestMod.