KitchenLib handles preferences via it's own PreferenceManager, each mod will need to register its own PreferenceManager in order to use the preferences. The PreferenceManager contains a number of methods which allow the registration, assignment, and retrieval of each preference.
To setup a PreferenceManager you will need to create a new instance of the PreferenceManager for use. This can be done at any time, although it's suggested to do it as early as possible.
Your PreferenceManager instance will need to take a string for it's argument, this string is the unique identifier for your preferences.
public static PreferenceManager manager;
protected override void OnPostActivate(Mod mod)
{
manager = new PreferenceManager("com.example.modid");
}
Once your PreferenceManager is setup, it's time to give it some use, first thing to do is register all your preferences with the manager so it knows what to expect when loading the file.
In the following example, we will be using the PreferenceBool type. This type along with other included types, require 2 arguments, string key which is the preference's unique identifier, and object defaultValue, where object is respective of the preference type, and assigns the default value for this preference if it doesn't already exist.
manager.RegisterPreference(new PreferenceBool("isEnabled", true));
Other included types are:
PreferenceBoolPreferenceStringPreferenceIntPreferenceFloatPreferenceList<T>PreferenceDictionary<T1, T2>When using preferences, you are going to need to save and load these quite often. Provided are 2 methods within the PreferenceManager which will assist with this:
manager.Load(); and manager.Save();
We highly recommend loading, then saving as soon as you have registered all your preference types to ensure default values are all set, and existing values are loaded.
When saving and loading your preferences, these preferences are not uploaded to the Steam Cloud.
Your preference files are located in %localappdata%low/It's Happening/PlateUp/UserData/Preferences/com.example.modid/ where com.example.modid is the unique ID you set for the PreferenceManager.
PreferenceManager allows for different profiles to be setup within your mod. This is done with manager.SetProfile(string profile);. Using this is as simple as it looks, you'll be able to adjust which preferences you load dependant on existing profiles, or creating new ones.
manager.SetProfile("Profile1");
Main.LogInfo(manager.GetPreference<PreferenceBool>("isEnabled").Value); // Will log TRUE
manager.SetProfile("Profile2");
manager.Load();
Main.LogInfo(manager.GetPreference<PreferenceBool>("isEnabled").Value); // Will log FALSE
Getting and Setting values for your preferences is just as simple as registering the preference. Lets assume we have registered 2 preferences, a PreferenceBool with the key isEnabled and a PreferenceString with the key welcomeMessage.
manager.Get<PreferenceBool>("isEnabled"); // will return TRUE
manager.Get<PreferenceString>("welcomeMessage"); // will return "Hello World!"
manager.Set<PreferenceBool>("isEnabled", false);
manager.Set<PreferenceString>("welcomeMessage", "Welcome!");
When setting a preference, this change is stored in memory, if you want this preference change to persist between sessions (You probably do) you will need to save your preferences after changing them.
As previously exampled in Registering Preferences there are a number of different preference types available to use, although each mod is going to require a different set of preferences, and it may not be available in KitchenLib, so we have designed PreferenceManager to be expandable and adjustable to each mod's requirements.
Each preference type derives from PreferenceBase<T>(string key, T defaultValue) which can be used to build your own preference type.
This class contains 2 abstract methods which are required string Serialize(); and void Deserialize(string json). This is needed as preferences are stored in JSON format.
Lets assume we have a Type known as Person, this is a type which isn't supported by default, but can be easily implemented.
public class Person
{
public string FirstName;
public int Age;
}
To implement the Person type as a preference, we will need to be able to Serialize and Deserialze to and from JSON.
public class PreferencePerson : PreferenceBase<Person>
{
public PreferencePerson(string key, Person defaultValue = null) : base(key, defaultValue) { }
public override string Serialize()
{
return JsonConvert.SerializeObject(Value);
}
public override void Deserialize(string json)
{
Value = JsonConvert.DeserializeObject<Person>(json);
}
}