Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix settings sharing conflict #21

Merged
merged 3 commits into from
Oct 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32 -D_WINDOWS -D_WINDLL -D_USRDL /MP2 -
set(FLAGS_DEBUG "-D_DEBUG /Gm- /MTd /GS"
)

set(FLAGS_RELEASE "-U_DEBUG -DNDEBUG /GL /Gm- /MT -Ox -Ob1 -Oi -Os -GF -GS- -Gy /fp:fast /GA"
set(FLAGS_RELEASE "-U_DEBUG -DNDEBUG /GL /Gm- /MT -Ox -Ob1 -Oi -Os -GF -GS- -Gy /fp:fast"
)

else()
Expand Down
3 changes: 1 addition & 2 deletions src/FarPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ void FarPlugin::Create()
exit(0);
}
InitOptions();
settings.create();
LoadOptions();

descs.AddString(L"Descript.ion");
Expand Down Expand Up @@ -113,7 +112,7 @@ void beep(int b, bool useBASS, const String &file)
L"LuaBASS.Init(1,44100,0);"
L"local Handle = LuaBASS.StreamCreateFile('";
start += file.replace(L"\\", L"\\\\");
start += L"',0,0,LuaBASS.Flags(LuaBASS.STREAM.BASS_STREAM_PRESCAN,LuaBASS.SAMPLE.BASS_SAMPLE_LOOP));"
start += L"',0,0,LuaBASS.Flags(LuaBASS.STREAM.BASS_STREAM_PRESCAN));"
L"LuaBASS.ChannelPlay(Handle,false) "
L"end";
MacroExecuteString mes = { sizeof(mes) };
Expand Down
143 changes: 77 additions & 66 deletions src/FarSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,132 +5,143 @@
#include "SDK/plugin.hpp"

FarSettings::FarSettings() :
handle(INVALID_HANDLE_VALUE), dirId(0)
handle(INVALID_HANDLE_VALUE), dirId(0), nAttachments(0)
{
}

FarSettings::~FarSettings()
{
clean();
while (nAttachments-- != 0)
detach();
}

intptr_t FarSettings::control(FAR_SETTINGS_CONTROL_COMMANDS cmd, void * param)
{
return Info.SettingsControl(handle, cmd, 0, param);
}

void FarSettings::clean()
void FarSettings::detach()
{
if (handle != INVALID_HANDLE_VALUE)
{
control(SCTL_FREE);
handle = INVALID_HANDLE_VALUE;
dirId = 0;
if (--nAttachments == 0) {
if (handle != INVALID_HANDLE_VALUE)
{
control(SCTL_FREE);
handle = INVALID_HANDLE_VALUE;
dirId = 0;
}
}
}

bool FarSettings::create()
bool FarSettings::attach()
{
clean();
FarSettingsCreate fsc = { sizeof(FarSettingsCreate) };
fsc.Guid = MainGuid;
if (!control(SCTL_CREATE, &fsc))
{
return false;
}
handle = fsc.Handle;
FarSettingsValue fsv = { sizeof(FarSettingsValue) };
fsv.Root = dirId;
fsv.Value = L"FileCopyEx3Settings";
intptr_t dir_id = control(SCTL_CREATESUBKEY, &fsv);
if (dir_id == 0)
{
return false;
}
else
if (nAttachments++ == 0)
{
dirId = dir_id;
FarSettingsCreate fsc = { sizeof(FarSettingsCreate) };
fsc.Guid = MainGuid;
if (!control(SCTL_CREATE, &fsc))
{
nAttachments--;
return false;
}
handle = fsc.Handle;
FarSettingsValue fsv = { sizeof(FarSettingsValue) };
fsv.Root = dirId;
fsv.Value = L"FileCopyEx3Settings";
intptr_t dir_id = control(SCTL_CREATESUBKEY, &fsv);
if (dir_id != 0)
{
dirId = dir_id;
}
}
return true;
}

bool FarSettings::get(const String & name, String & value)
{
if (!attach())
return false;
FarSettingsItem fsi = { sizeof(FarSettingsItem) };
fsi.Root = dirId;
fsi.Name = name.c_str();
fsi.Type = FST_STRING;
if (!control(SCTL_GET, &fsi))
{
return false;
}
value = fsi.String;
return true;
bool ok = control(SCTL_GET, &fsi) != 0;
if (ok)
value = fsi.String;
detach();
return ok;
}

bool FarSettings::set(const String & name, const String & value)
{
if (!attach())
return false;
FarSettingsItem fsi = { sizeof(FarSettingsItem) };
fsi.Root = dirId;
fsi.Name = name.c_str();
fsi.Type = FST_STRING;
fsi.String = value.c_str();
return control(SCTL_SET, &fsi) != 0;
bool ok = control(SCTL_SET, &fsi) != 0;
detach();
return ok;
}

bool FarSettings::list(ParamInfoVector & res)
{
if (!attach())
return false;
FarSettingsEnum fse = { sizeof(FarSettingsEnum) };
fse.Root = dirId;
if (!control(SCTL_ENUM, &fse))
{
return false;
}
res.clear();
for (size_t Index = 0; Index < fse.Count; Index++)
{
if (fse.Items[Index].Type == FST_STRING)
bool ok = control(SCTL_ENUM, &fse) != 0;
if (ok) {
res.clear();
for (size_t Index = 0; Index < fse.Count; Index++)
{
res.push_back(ParamInfo(fse.Items[Index].Name, fse.Items[Index].Type));
if (fse.Items[Index].Type == FST_STRING)
{
res.push_back(ParamInfo(fse.Items[Index].Name, fse.Items[Index].Type));
}
}
}
return true;
detach();
return ok;
}

bool saveOptions(const PropertyMap & options, FarSettings & settings)
{
bool ok = true;
for (PropertyMap::const_iterator it = options.begin(); it != options.end(); ++it)
{
if (!settings.set(it->first, it->second.operator const String()))
bool ok = settings.attach();
if (ok) {
for (PropertyMap::const_iterator it = options.begin(); it != options.end(); ++it)
{
ok = false;
if (!settings.set(it->first, it->second.operator const String()))
{
ok = false;
}
}
settings.detach();
}

return ok;
}

bool loadOptions(PropertyMap & options, FarSettings & settings)
{
FarSettings::ParamInfoVector v;

if (!settings.list(v))
{
return false;
}

for (FarSettings::ParamInfoVector::iterator it = v.begin(); it != v.end(); ++it)
{
String & name = it->name;
String v;
if (settings.get(name, v))
{
options[name] = v;
bool ok = settings.attach();
if (ok) {
FarSettings::ParamInfoVector v;
ok = settings.list(v);
if (ok) {
for (FarSettings::ParamInfoVector::iterator it = v.begin(); it != v.end(); ++it)
{
String & name = it->name;
String v;
if (settings.get(name, v))
{
options[name] = v;
}
}
}
settings.detach();
}

return true;
return ok;
}

/*
Expand Down
7 changes: 4 additions & 3 deletions src/FarSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class FarSettings
FarSettings();
~FarSettings();

bool create();
bool attach();
void detach();
bool get(const String & name, String & value);
bool set(const String & name, const String & value);

Expand All @@ -34,10 +35,10 @@ class FarSettings
bool list(ParamInfoVector & res);
private:
HANDLE handle;
size_t dirId;
intptr_t dirId;
int nAttachments;

intptr_t control(FAR_SETTINGS_CONTROL_COMMANDS cmd, void * param = nullptr);
void clean();
};

bool saveOptions(const PropertyMap & options, FarSettings & settings);
Expand Down