MIDI: Fix Sound Canvas VA duplication function to support the 32 signed instances current versions ship

CQTexperiment
Christopher Snowhill 2021-08-10 17:34:08 -07:00
parent 8875a36aba
commit 59a5a7687e
2 changed files with 13 additions and 68 deletions

View File

@ -6,9 +6,11 @@
#include "SCCore.h" #include "SCCore.h"
static unsigned long g_serial = 0;
SCCore::SCCore() SCCore::SCCore()
{ {
duped = false; serial = g_serial++;
path = 0; path = 0;
handle = 0; handle = 0;
@ -44,11 +46,6 @@ void SCCore::Unload()
dlclose(handle); dlclose(handle);
handle = 0; handle = 0;
} }
if (duped && path)
{
unlink(path);
duped = false;
}
if (path) if (path)
{ {
free(path); free(path);
@ -56,71 +53,19 @@ void SCCore::Unload()
} }
} }
static const char name_template[] = "/tmp/SCCore.dylib.XXXXXXXX";
bool SCCore::Load(const char * _path, bool dupe) bool SCCore::Load(const char * _path, bool dupe)
{ {
uintptr_t size = 0; path = (char *) malloc(strlen(_path) + 1);
strcpy(path, _path);
if (dupe) if (dupe)
{ {
path = (char *) malloc(strlen(name_template) + 1); char * name = strstr(path, "SCCore00");
strcpy(path, name_template); if (name) {
mktemp(path); unsigned long serial_wrapped = serial % 32;
name[6] = '0' + (serial_wrapped / 10);
const char * uniq = path + strlen(path) - 8; name[7] = '0' + (serial_wrapped % 10);
}
FILE * f = fopen(_path, "rb");
if (!f) return false;
fseek(f, 0, SEEK_END);
size_t fs = ftell(f);
fseek(f, 0, SEEK_SET);
size = fs;
unsigned char * buffer = (unsigned char *) malloc(fs);
if (!fs)
{
fclose(f);
return false;
}
fread(buffer, 1, fs, f);
fclose(f);
for (size_t i = 0; i < fs - 14; ++i)
{
if (memcmp(buffer + i, "SCCore00.dylib", 14) == 0)
{
memcpy(buffer + i, uniq, 8);
i += 13;
}
}
duped = true;
f = fopen(path, "wb");
if (!f)
{
free(buffer);
return false;
}
fwrite(buffer, 1, fs, f);
fclose(f);
free(buffer);
}
else
{
path = (char *) malloc(strlen(_path) + 1);
strcpy(path, _path);
FILE * f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
size = ftell(f);
fclose(f);
} }
handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL); handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL);

View File

@ -5,7 +5,7 @@
class SCCore class SCCore
{ {
bool duped; unsigned long serial;
char * path; char * path;
void * handle; void * handle;