Skip to content

Instantly share code, notes, and snippets.

@olilarkin
Last active January 12, 2024 07:01
Show Gist options
  • Save olilarkin/4c9b5c9697b5c5720017e7b5c8f030c9 to your computer and use it in GitHub Desktop.
Save olilarkin/4c9b5c9697b5c5720017e7b5c8f030c9 to your computer and use it in GitHub Desktop.
In plugin CTOR:
if (sInstances == 0 && sSettings == nullptr)
{
WDL_String settingsFolder;
AppSupportPath(settingsFolder, false);
#ifdef OS_MAC
settingsFolder.AppendFormatted(MAX_PATH, "/%s", BUNDLE_NAME);
#elif defined(OS_WIN)
settingsFolder.AppendFormatted(MAX_PATH, "\\%s", BUNDLE_NAME);
#endif
sSettings = new NAMPersistance(settingsFolder.Get());
}
sInstances++;
#pragma once
#include "IPlugPlatform.h"
#if defined OS_MAC || defined OS_IOS || defined OS_LINUX
#include "IPlugSWELL.h"
#endif
#include "wdlstring.h"
#include "mutex.h"
class NAMPersistance
{
public:
enum EThingsToPersist
{
kModelDirectory,
kIRDirectory,
kNumThingsToPersist,
};
const char* tags[2] = {"ModelDirectory", "kIRDirectory"};
NAMPersistance(const char* settingsFolder);
void Init();
void UpdateINI();
WDL_String* Get(EThingsToPersist thing);
void Set(EThingsToPersist thing, const char* str);
private:
WDL_String mDefaults[kNumThingsToPersist];
WDL_String mItems[kNumThingsToPersist];
WDL_String mINIPath;
WDL_Mutex mMutex;
};
#include "IPlugLogger.h"
#include "NeuralAmpModelerPersistance.h"
#include <sys/stat.h>
static const int BUFFER_SIZE = 256;
NAMPersistance::NAMPersistance(const char* settingsFolder)
{
WDL_String defaultModelPath, defaultIRPath;
#if defined OS_MAC
defaultModelPath.Set(settingsFolder);
defaultModelPath.AppendFormatted(MAX_PATH, "/Models");
// defaultIRPath = ...
#elif defined OS_IOS
defaultModelPath.Set(settingsFolder);
defaultModelPath.AppendFormatted(MAX_PATH, "/Models");
#elif defined OS_WIN
char strPath[MAX_PATH];
ExpandEnvironmentStrings("%ProgramFiles%", strPath, ARRAYSIZE(strPath));
defaultModelPath.AppendFormatted(MAX_PATH, "%s\\NeuralAmpModeler\\Models", strPath);
#endif
auto PREF_FILE_NAME = "NeuralAmpModeler-settings.ini";
mDefaults[kModelDirectory].Set(&defaultModelPath);
mItems[kModelDirectory].Set(mDefaults[kModelDirectory].Get(), BUFFER_SIZE);
mDefaults[kIRDirectory].Set(&defaultIRPath);
mItems[kIRDirectory].Set(mDefaults[kIRDirectory].Get(), BUFFER_SIZE);
mINIPath.Set(settingsFolder);
#if defined OS_MAC || defined OS_IOS
mINIPath.AppendFormatted(MAX_PATH, "/%s", PREF_FILE_NAME);
#elif defined OS_WIN
mINIPath.AppendFormatted(MAX_PATH, "\\%s", PREF_FILE_NAME);
#endif
struct stat st;
if (stat(settingsFolder, &st) == 0) // if NAM directory exists
{
Init();
}
else // folder doesn't exist - make folder
{
if (CreateDirectory(settingsFolder, NULL) == 0)
{
DBGMSG("error - couldn't create settings folder %s\n", settingsFolder);
return;
}
}
UpdateINI(); // will write file if doesn't exist
}
void NAMPersistance::Init()
{
WDL_MutexLock lock(&mMutex);
char buf[BUFFER_SIZE]; // temp buffer for reading profile strings
struct stat st;
if (stat(mINIPath.Get(), &st) == 0) // if settings file exists read values into state
{
//CHECK PATHS
for (int i = 0; i < kNumThingsToPersist; i++)
{
GetPrivateProfileString("paths", tags[i], mDefaults[i].Get(), buf, BUFFER_SIZE, mINIPath.Get());
//take off trailing slash
#if defined OS_MAC
if (buf[strlen(buf)-1] == '/')
{
buf[strlen(buf)-1] = 0;
}
#elif defined OS_WIN
if (buf[strlen(buf)-1] == '\\')
{
buf[strlen(buf)-1] = 0;
}
#endif
if (stat(buf, &st) == 0)
{
mItems[i].Set(buf);
}
else
{
DBGMSG("invalid %s value in ini, setting to default\n", tags[i]);
mItems[i].Set(mDefaults[i].Get());
}
}
}
UpdateINI();
}
void NAMPersistance::UpdateINI()
{
WDL_MutexLock lock(&mMutex);
for (int i = 0; i< kNumThingsToPersist; i++)
{
WritePrivateProfileString("paths", tags[i], mItems[i].Get(), mINIPath.Get());
}
}
WDL_String* NAMPersistance::Get(EThingsToPersist thing)
{
WDL_MutexLock lock(&mMutex);
char buf[BUFFER_SIZE]; // temp buffer for reading profile strings
GetPrivateProfileString("paths", tags[thing], mDefaults[thing].Get(), buf, BUFFER_SIZE, mINIPath.Get());
mItems[thing].Set(buf);
return &mItems[thing];
}
void NAMPersistance::Set(EThingsToPersist thing, const char* str)
{
WDL_MutexLock lock(&mMutex);
mItems[thing].Set(str);
WritePrivateProfileString("paths", tags[thing], mItems[thing].Get(), mINIPath.Get());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment