Skip to content

Instantly share code, notes, and snippets.

@X39
Created August 1, 2015 20:41
Show Gist options
  • Save X39/a3dfb1150687722a924d to your computer and use it in GitHub Desktop.
Save X39/a3dfb1150687722a924d to your computer and use it in GitHub Desktop.
ArmA3 Mutex extension
#pragma once
#include <Windows.h>
#include <vector>
#include <mutex>
extern "C"
{
__declspec(dllexport) void __stdcall RVExtension(char *output, int outputSize, const char *function);
};
std::vector<std::mutex*> mutexList;
bool lockMutex(size_t index)
{
return mutexList[index]->try_lock();
}
bool unlockMutex(size_t index)
{
mutexList[index]->unlock();
return true;
}
bool toIndex(char c, size_t* out)
{
(*out) = c - '0';
return c < 10;
}
void __stdcall RVExtension(char *output, int outputSize, const char *function)
{
/*
* Function:
* c1 --> l | u
* c2 --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
*
* Returns:
* c1 --> t | f | e
* t ==> true
* f ==> false
* e ==> error
*/
if (outputSize == 0)
return;
if (function[0] == '\0' || function[1] == '\0')
{
output[0] = 'e';
output[1] = '\0';
}
switch (function[0])
{
case 'l':
size_t index;
if (!toIndex(function[1], &index))
{
output[0] = 'e';
output[1] = '\0';
}
else
{
output[0] = (lockMutex(index) ? 't' : 'f');
output[1] = '\0';
}
break;
case 'u':
size_t index;
if (!toIndex(function[1], &index))
{
output[0] = 'e';
output[1] = '\0';
}
else
{
output[0] = (unlockMutex(index) ? 't' : 'f');
output[1] = '\0';
}
break;
default:
output[0] = 'e';
output[1] = '\0';
break;
}
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
mutexList.resize(10);
for (int i = 0; i < mutexList.size(); i++)
{
mutexList[i] = new std::mutex();
}
break;
//case DLL_THREAD_ATTACH:
//case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
for (int i = 0; i < mutexList.size(); i++)
{
delete mutexList[i];
}
mutexList.clear();
break;
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment