Hot Door CORE Forum

Full Version: implementing a preferences file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Could someone give a brief overview and simple sample code of how to create a Preferences file, read, write, and close it? Studying the Preferences and *Pref classes hasn't proven as intuitively obvious as I'd hoped. Previously my plugins preferences were simply embedded in the AI preferences file, so I'm mostly interested in where the CORE method differs. I'm sure much of it is JSON specific, and can research that portion elsewhere. I'd be most grateful to get started in the right direction. -- Rick
We moved away from using Illustrator's preference file many years ago, due to the file becoming corrupted for no apparent reason whatsoever. This generated lots of tech support calls where the solution was inevitably telling the user to delete the Illustrator prefs file and relaunching. Since moving to our own prefs file, we have seen basically no data corruption at all (so the phone calls have been drastically reduced).

CORE's prefs data is indeed stored on disk JSON-encoded. As such, you can nest data in whatever hierarchy you'd like (as JSON objects can contain other JSON objects, etc.) We have our own classes to handle this nesting, namely ArrayPref and DictionaryPref (arrays storing indexed elements and dictionaries storing named members, as you'd expect). All other classes are named pretty obviously, and all of them have the *Pref suffix.

Here is a little sample for loading the prefs file and checking if the current plugin run is its first, or if the user has run it before.

Code:
// Assume this is not the first plugin launch
bool firstLaunch = false;

// Load prefs object from file on disk
hdi::core::Preferences prefs;
bool loaded = hdi::core::Preferences::load("/path/to/prefs/file", prefs);
if(loaded)
{
    // Get the first launch pref if it exists, otherwise it will be created with a default value of "true"
    hdi::core::BoolPref firstLaunchPref;
    prefs.getOrCreateBoolPref("FirstLaunch", true, firstLaunchPref);

    // The first launch pref should only be true if it was just created, but always set to false after reading the value
    firstLaunch = firstLaunchPref.boolValue();
    firstLaunchPref.setBoolValue(false);

    // Save the prefs object to its file on disk
    prefs.save("/path/to/prefs/file");
}
else
{
    // TODO show some error dialog to the user
}

Lastly, if you'd like to store your prefs file in the user's platform preferences folder, have a look at the hdi::core::files::platformPrefsPath() function.
Thanks, Garrett! This is very helpful. I'm eager to move to this preferences model since AI Pref file issues have been in my top three support categories, also. -- rj