Skip to content

Commit

Permalink
Add hack to fix ConfigFile ABI of last hack
Browse files Browse the repository at this point in the history
Because ConfigFile exposed the entire class to plugins, anything that
changes the class breaks ABI, and the last ConfigFile commit did just
that.  So this adds an additional hack to allow additional internal
variables without breaking ABI.  It changes the lpFileData in to a
variable that creates another internal variable structure as a rather
terrible hack for adding new internal variables.

So, what's the lesson?  Never expose anything you don't need to expose
unless you need maximum performance and its structure size will never
change.
  • Loading branch information
jp9000 committed Jan 23, 2015
1 parent 691c251 commit 8b6d936
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
69 changes: 49 additions & 20 deletions OBSApi/Utility/ConfigFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
//
// ...yes, I really need to rewrite this file, I know.

struct InternalUselessStuff
{
TSTR lpActualFileData;
HANDLE hHorribleThreadSafetyMutex;

inline InternalUselessStuff() {hHorribleThreadSafetyMutex = OSCreateMutex();}
inline ~InternalUselessStuff() {OSCloseMutex(hHorribleThreadSafetyMutex);}
};

#define InternalStuff reinterpret_cast<InternalUselessStuff*>(lpFileData)


/*=========================================================
Config
Expand All @@ -36,6 +47,9 @@ BOOL ConfigFile::Create(CTSTR lpConfigFile)
{
strFileName = lpConfigFile;

if (!lpFileData)
lpFileData = (TSTR)new InternalUselessStuff;

if(LoadFile(XFILE_CREATEALWAYS))
LoadData();
else
Expand All @@ -48,14 +62,20 @@ BOOL ConfigFile::Open(CTSTR lpConfigFile, BOOL bOpenAlways)
{
strFileName = lpConfigFile;

if(LoadFile(bOpenAlways ? XFILE_OPENALWAYS : XFILE_OPENEXISTING))
if (!lpFileData)
lpFileData = (TSTR)new InternalUselessStuff;

if (LoadFile(bOpenAlways ? XFILE_OPENALWAYS : XFILE_OPENEXISTING))
LoadData();
else
return 0;

return 1;
}

#define hHorribleThreadSafetyMutex reinterpret_cast<InternalUselessStuff*>(lpFileData)->hHorribleThreadSafetyMutex
#define lpActualFileData reinterpret_cast<InternalUselessStuff*>(lpFileData)->lpActualFileData

BOOL ConfigFile::LoadFile(DWORD dwOpenMode)
{
XFile file;
Expand Down Expand Up @@ -89,8 +109,8 @@ BOOL ConfigFile::LoadFile(DWORD dwOpenMode)
lpTempFileData[dwLength+4] = 0;
file.Close();

lpFileData = utf8_createTstr(lpTempFileData);
dwLength = slen(lpFileData);
lpActualFileData = utf8_createTstr(lpTempFileData);
dwLength = slen(lpActualFileData);
Free(lpTempFileData);

bOpen = 1;
Expand All @@ -103,7 +123,7 @@ BOOL ConfigFile::LoadFile(DWORD dwOpenMode)
void ConfigFile::LoadData()
{
OSEnterMutex(hHorribleThreadSafetyMutex);
TSTR lpCurLine = lpFileData, lpNextLine;
TSTR lpCurLine = lpActualFileData, lpNextLine;
ConfigSection *lpCurSection=NULL;
DWORD i;

Expand Down Expand Up @@ -167,6 +187,9 @@ void ConfigFile::LoadData()

void ConfigFile::Close()
{
if (!bOpen)
return;

OSEnterMutex(hHorribleThreadSafetyMutex);
DWORD i,j,k;

Expand All @@ -189,15 +212,21 @@ void ConfigFile::Close()
}
Sections.Clear();

if(lpFileData)
if(lpActualFileData)
{
Free(lpFileData);
lpFileData = NULL;
Free(lpActualFileData);
lpActualFileData = NULL;
}

bOpen = 0;

OSLeaveMutex(hHorribleThreadSafetyMutex);

if (lpFileData)
{
delete reinterpret_cast<InternalUselessStuff*>(lpFileData);
lpFileData = NULL;
}
}

BOOL ConfigFile::SaveAs(CTSTR lpPath)
Expand All @@ -216,7 +245,7 @@ BOOL ConfigFile::SaveAs(CTSTR lpPath)
return FALSE;
}

if (!newFile.WriteAsUTF8(lpFileData))
if (!newFile.WriteAsUTF8(lpActualFileData))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return FALSE;
Expand Down Expand Up @@ -848,7 +877,7 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
TSTR lpTemp = lpFileData, lpEnd = &lpFileData[dwLength], lpSectionStart;
TSTR lpTemp = lpActualFileData, lpEnd = &lpActualFileData[dwLength], lpSectionStart;
DWORD dwSectionNameSize = slen(lpSection), dwKeyNameSize = slen(lpKey);
BOOL bInSection = 0;

Expand All @@ -873,7 +902,7 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)

XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], dwLength-4);
file.WriteAsUTF8(&lpActualFileData[2], dwLength-4);
file.Write("\r\n[", 3);
file.WriteAsUTF8(lpSection, dwSectionNameSize);
file.Write("]\r\n", 3);
Expand All @@ -896,7 +925,7 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
{
XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], DWORD(lpSectionStart-lpFileData-2));
file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpSectionStart-lpActualFileData-2));
file.WriteAsUTF8(lpKey, dwKeyNameSize);
file.Write("=", 1);
file.WriteAsUTF8(newvalue, slen(newvalue));
Expand Down Expand Up @@ -941,7 +970,7 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
if (!file.WriteAsUTF8(&lpFileData[2], DWORD(lpTemp - lpFileData - 2)))
if (!file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpTemp - lpActualFileData - 2)))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
Expand Down Expand Up @@ -975,7 +1004,7 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)

XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], DWORD(lpSectionStart-lpFileData-2));
file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpSectionStart-lpActualFileData-2));
file.WriteAsUTF8(lpKey, dwKeyNameSize);
file.Write("=", 1);
file.WriteAsUTF8(newvalue, slen(newvalue));
Expand All @@ -994,7 +1023,7 @@ void ConfigFile::Remove(CTSTR lpSection, CTSTR lpKey)
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
TSTR lpTemp = lpFileData, lpEnd = &lpFileData[dwLength], lpSectionStart;
TSTR lpTemp = lpActualFileData, lpEnd = &lpActualFileData[dwLength], lpSectionStart;
DWORD dwSectionNameSize = slen(lpSection), dwKeyNameSize = slen(lpKey);
BOOL bInSection = 0;

Expand Down Expand Up @@ -1033,7 +1062,7 @@ void ConfigFile::Remove(CTSTR lpSection, CTSTR lpKey)
TSTR lpNextLine = schr(lpTemp, '\n')+1;
XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], DWORD(lpTemp-lpFileData-2));
file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpTemp-lpActualFileData-2));
file.WriteAsUTF8(lpNextLine, slen(lpNextLine)-2);
file.Close();

Expand All @@ -1055,7 +1084,7 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
{
assert(lpSection);
assert(lpKey);
TSTR lpTemp = lpFileData, lpEnd = &lpFileData[dwLength], lpSectionStart;
TSTR lpTemp = lpActualFileData, lpEnd = &lpActualFileData[dwLength], lpSectionStart;
DWORD dwSectionNameSize = slen(lpSection), dwKeyNameSize = slen(lpKey);
BOOL bInSection = 0;

Expand All @@ -1078,7 +1107,7 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
{
XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], dwLength-4);
file.WriteAsUTF8(&lpActualFileData[2], dwLength-4);
file.Write("\r\n[", 3);
file.WriteAsUTF8(lpSection, dwSectionNameSize);
file.Write("]\r\n", 3);
Expand All @@ -1103,7 +1132,7 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
{
XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], DWORD(lpSectionStart-lpFileData-2));
file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpSectionStart-lpActualFileData-2));
file.WriteAsUTF8(lpKey, dwKeyNameSize);
file.Write("=", 1);
file.WriteAsUTF8(newvalue, slen(newvalue));
Expand Down Expand Up @@ -1133,7 +1162,7 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
lpTemp = lpLastItem;
XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], DWORD(lpTemp-lpFileData-2));
file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpTemp-lpActualFileData-2));
file.WriteAsUTF8(lpKey, dwKeyNameSize);
file.Write("=", 1);
file.WriteAsUTF8(newvalue, slen(newvalue));
Expand All @@ -1154,7 +1183,7 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)

XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS);
file.Write("\xEF\xBB\xBF", 3);
file.WriteAsUTF8(&lpFileData[2], DWORD(lpSectionStart-lpFileData-2));
file.WriteAsUTF8(&lpActualFileData[2], DWORD(lpSectionStart-lpActualFileData-2));
file.WriteAsUTF8(lpKey, dwKeyNameSize);
file.Write("=", 1);
file.WriteAsUTF8(newvalue, slen(newvalue));
Expand Down
3 changes: 1 addition & 2 deletions OBSApi/Utility/ConfigFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct ConfigSection
class BASE_EXPORT ConfigFile
{
public:
ConfigFile() : bOpen(0), strFileName(), lpFileData(NULL), dwLength(0) { hHorribleThreadSafetyMutex = OSCreateMutex(); }
ConfigFile() : bOpen(0), strFileName(), lpFileData(NULL), dwLength(0) {}
~ConfigFile() {Close();}

BOOL Create(CTSTR lpConfigFile);
Expand Down Expand Up @@ -105,5 +105,4 @@ class BASE_EXPORT ConfigFile
String strFileName;
TSTR lpFileData;
DWORD dwLength;
HANDLE hHorribleThreadSafetyMutex;
};

0 comments on commit 8b6d936

Please sign in to comment.