Skip to content

FileDatabase

Liam edited this page Apr 15, 2020 · 7 revisions

Methods

 

Initialise

public static bool Initialise(string moduleFolderName)

Arguments

  • moduleFolderName - The name of the folder for the mod which is calling the method.

Returns

  • bool - Returns true if the operation was successful and all files were loaded. Returns false if an exception occurred.

Usage

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.

 

Get<T>

public static T Get<T>(string id)

Arguments

  • T - the type of the object to be retrieved from the FileDatabase.
  • id - The ID of the object to be retrieved from the FileDatabase.

Returns

  • T - Returns the instance of the object of type T with the given ID from the FileDatabase. If there is no type T or the ID doesn't exist in the FileDatabase, returns null.

Usage

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
}

 

SaveToFile

public static bool SaveToFile(string moduleFolderName, ISerialisableFile sf, Location location = Location.Modules)

Arguments

  • 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 the ModuleData/Loadables folder by default, but can be set to save to the Configs folder if desired.

Returns

  • bool - Returns true if the operation was successful. Returns false if an exception occurred.

Usage

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.

 

DeleteFile

public static bool DeleteFile(string moduleFolderName, string fileName, Location location = Location.Modules)

Arguments

  • 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 the ModuleData/Loadables folder or the Configs folder.

Overrides

public static bool DeleteFile(string moduleFolderName, ISerialisableFile sf, Location location = Location.Modules)

Different Arguments

  • sf - The instance of the object whose file should be deleted can optionally be passed as an argument. This will create the fileName argument from the passed instance for the default method.

Returns

  • bool - Returns true if the file was successfully deleted. Returns false if there was an exception or the file couldn't be found.

Usage

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.

 

GetFileNameFor

public static string GetFileNameFor(ISerialisableFile sf)

Arguments

  • sf - The instance of the object to get the file name for.

Returns

  • string - Returns the file name for the given object instance. This takes the form "{object's type name}.{object's id}.xml".

Usage

Use this method to retrieve the file name for the given object instance in the format that the FileDatabase uses.

 

GetPathForModule

public static string GetPathForModule(string moduleFolderName, Location location)

Arguments

  • 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 the Modules/ModuleFolderName folder or the Configs folder.

Returns

  • string - Returns the path that the calling module will use.

Usage

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.