Commit 3733817b authored by Canvas123's avatar Canvas123
Browse files

Initial commit

parents
*.so
*.out
*.lk
*.int
#ifndef __MAIN_H__
#define __MAIN_H__
/* To use this exported function of dll, include this header
* in your project.
*/
char init_;
#ifdef _WIN32
#include <Windows.h>
#define DLL_EXPORT __declspec(dllexport)
#define DLL_IMPORT __declspec(dllimport)
#elif defined __unix__
#include <link.h>
#include <memory>
#include <sys/mman.h>
#include <vector>
#define DLL_EXPORT __attribute__((visibility("default")))
#define DLL_IMPORT
#else
#define DLL_EXPORT
#define DLL_IMPORT
#pragma warning Unknown dynamic link import / export semantics.
#endif
#if defined(_WIN32)
#define GET_EDX(a) __asm__ __volatile__("mov %%edx, %0" : "=r"(a));
#elif defined(__unix__)
#define GET_EDX(a) asm volatile("mov %%edx, %0" : "=r"(a));
#else
#pragma warning Unknown asm / __asm semantics.
#endif
#if defined(_WIN32)
#define BYONDLIB "byondcore.dll"
#define GET_BYONDCORE_PTR() (uintptr_t) GetModuleHandle(BYONDLIB)
#elif defined(__unix__)
#define BYONDLIB "libbyond.so"
#include <string.h>
struct dlinfo_t {
const char *library = nullptr;
uintptr_t address = 0;
size_t size = 0;
};
std::vector<dlinfo_t> libraries;
bool GetLibraryInformation(const char *library, uintptr_t *address,
size_t *size) {
if (libraries.size() == 0) {
dl_iterate_phdr(
[](struct dl_phdr_info *info, size_t, void *) {
dlinfo_t library_info = {};
library_info.library = info->dlpi_name;
library_info.address = info->dlpi_addr + info->dlpi_phdr[0].p_vaddr;
library_info.size = info->dlpi_phdr[0].p_memsz;
libraries.push_back(library_info);
return 0;
},
nullptr);
}
for (const dlinfo_t &current : libraries) {
if (!strcasestr(current.library, library))
continue;
if (address)
*address = current.address;
if (size)
*size = current.size;
return true;
}
return false;
}
uintptr_t GET_BYONDCORE_PTR() {
uintptr_t address;
size_t size;
GetLibraryInformation(BYONDLIB, &address, &size);
return address;
}
#else
#pragma warning Unknown module info semantics.
#endif
#if defined(_WIN32)
#include <Psapi.h>
#include <tlhelp32.h>
MODULEINFO GetModuleInfo(char *szModule) {
MODULEINFO modinfo = {0};
HMODULE hModule = GetModuleHandle(szModule);
if (hModule == 0)
return modinfo;
GetModuleInformation(GetCurrentProcess(), hModule, &modinfo,
sizeof(MODULEINFO));
return modinfo;
}
DWORD FindPattern(const char *module, unsigned char *pattern,
const char *mask) {
MODULEINFO mInfo = GetModuleInfo(module);
DWORD base = (DWORD)mInfo.lpBaseOfDll;
DWORD size = (DWORD)mInfo.SizeOfImage;
DWORD patternLength = (DWORD)strlen(mask);
for (DWORD i = 0; i < size - patternLength; i++) {
bool found = true;
for (DWORD j = 0; j < patternLength; j++) {
found &= mask[j] == '?' || pattern[j] == *(char *)(base + i + j);
}
if (found) {
return base + i;
}
}
return NULL;
}
#elif defined(__unix__)
inline bool Compare(const unsigned char *pData, const unsigned char *bMask,
const char *szMask) {
for (; *szMask; ++szMask, ++pData, ++bMask)
if (*szMask == 'x' && *pData != *bMask)
return false;
return (*szMask) == 0;
}
uintptr_t _FindPattern(uintptr_t dwAddress, uintptr_t dwLen,
unsigned char *bMask, const char *szMask) {
for (uintptr_t i = 0; i < dwLen; i++)
if (Compare((unsigned char *)(dwAddress + i), bMask, szMask))
return (uintptr_t)(dwAddress + i);
return 0;
}
uintptr_t FindPattern(const char *moduleName, unsigned char *bMask,
const char *szMask) {
uintptr_t baseAddress;
size_t memSize;
if (!GetLibraryInformation(moduleName, &baseAddress, &memSize))
return 0;
return _FindPattern(baseAddress, baseAddress + memSize, bMask, szMask);
}
#else
#pragma warning Unknown find pattern semantics.
#endif
#define BYOND_FUNCTION(NAME, RET_TYPE, ARG_TYPES) RET_TYPE(*NAME) ARG_TYPES;
#define IMPORT_BYOND_FUNCTION(NAME, OFFSET, RET_TYPE, ARG_TYPES) \
calc_ptr = OFFSET; \
NAME = (RET_TYPE(*) ARG_TYPES)calc_ptr
uintptr_t byondcore_base;
unsigned int usr_type, usr_id = 0;
BYOND_FUNCTION(WorldStringFindOrAdd, unsigned int, (char *, char, char))
BYOND_FUNCTION(_GetVariableFrom, unsigned int,
(unsigned int, unsigned int, unsigned int))
BYOND_FUNCTION(SetVariableTo, void,
(unsigned int, unsigned int, unsigned int, unsigned int,
unsigned int))
BYOND_FUNCTION(_GetVariableFromListIndex, unsigned int,
(char, unsigned int, unsigned int))
BYOND_FUNCTION(SetVariableToListIndex, void,
(char, unsigned int, unsigned int, unsigned int, unsigned int))
#ifdef _WIN32
#define WORLDSTRINGFINDORADD_SIGNATURE \
"\x55\x8B\xEC\x8B\x45\x00\x83\xEC\x00\x53\x8B\x1D\x00\x00\x00\x00"
#define WORLDSTRINGFINDORADD_MASK "xxxxx?xx?xxx????"
#define GETVARIABLEFROM_SIGNATURE \
"\x55\x8B\xEC\x6A\x00\x68\x00\x00\x00\x00\x64\xA1\x00\x00\x00\x00\x50\x83" \
"\xEC\x00\xA1\x00\x00\x00\x00\x33\xC5\x50\x8D\x45\x00\x64\xA3\x00\x00\x00" \
"\x00\x8B\x4D\x00\x0F\xB6\xC1"
#define GETVARIABLEFROM_MASK "xxxx?x????xx????xxx?x????xxxxx?xx????xx?xxx"
#define SETVARIABLETO_SIGNATURE \
"\x55\x8B\xEC\x8B\x4D\x00\x0F\xB6\xC1\x48\x83\xF8\x00\x0F\x87\x00\x00\x00" \
"\x00\x0F\xB6\x80\x00\x00\x00\x00\xFF\x24\x85\x00\x00\x00\x00\xFF\x75\x00" \
"\xFF\x75\x00\xFF\x75\x00\xFF\x75\x00\xE8\x00\x00\x00\x00"
#define SETVARIABLETO_MASK "xxxxx?xxxxxx?xx????xxx????xxx????xx?xx?xx?xx?x????"
#define GETVARIABLEFROMLISTINDEX_SIGNATURE \
"\x55\x8B\xEC\x8B\x4D\x00\x83\xEC\x00\x0F\xB6\xC1\x48\x53\x56\x57\x83\xF8" \
"\x00\x0F\x87\x00\x00\x00\x00\x0F\xB6\x80\x00\x00\x00\x00\xFF\x24\x85\x00" \
"\x00\x00\x00\x6A\x00\xFF\x75\x00\x51\xE8\x00\x00\x00\x00\x50\xE8\x00\x00" \
"\x00\x00\x8B\x4D\x00"
#define GETVARIABLEFROMLISTINDEX_MASK \
"xxxxx?xx?xxxxxxxxx?xx????xxx????xxx????x?xx?xx????xx????xx?"
#define SETVARIABLETOLISTINDEX_SIGNATURE \
"\x55\x8B\xEC\x8B\x4D\x00\x0F\xB6\xC1\x83\xC0\x00\x56"
#define SETVARIABLETOLISTINDEX_MASK "xxxx?xxxxx?x"
#elif defined(__unix__)
#define WORLDSTRINGFINDORADD_SIGNATURE \
"\x55\x89\xE5\x57\x56\x53\x89\xD3\x83\xEC\x00\x85\xC0"
#define WORLDSTRINGFINDORADD_MASK "xxxxxxxxxx?xx"
#define GETVARIABLEFROM_SIGNATURE \
"\x55\x89\xE5\x81\xEC\x00\x00\x00\x00\x8B\x55\x00\x89\x5D\x00\x8B\x5D\x00"
#define GETVARIABLEFROM_MASK "xxxxx????xx?xx?xx?"
#define SETVARIABLETO_SIGNATURE \
"\x55\x89\xE5\x81\xEC\x00\x00\x00\x00\x89\x75\x00\x8B\x55\x00\x8B\x75\x00"
#define SETVARIABLETO_MASK "xxxxx????xx?xx?xx?"
#define GETVARIABLEFROMLISTINDEX_SIGNATURE \
"\x55\x89\xE5\x57\x56\x53\x81\xEC\x00\x00\x00\x00\x8B\x55\x00\x8B\x75\x00" \
"\x8B\x7D\x00"
#define GETVARIABLEFROMLISTINDEX_MASK "xxxxxxxx????xx?xx?xx?"
#define SETVARIABLETOLISTINDEX_SIGNATURE \
"\x55\x89\xE5\x83\xEC\x00\x89\x75\x00\x8B\x75\x00\x89\x5D\x00\x89\xC3"
#define SETVARIABLETOLISTINDEX_MASK "xxxxx?xx?xx?xx?xx"
#else
#pragma warning Unknown signature / mask semantics.
#endif
void get_byondcore_functions() {
uintptr_t calc_ptr;
IMPORT_BYOND_FUNCTION(
WorldStringFindOrAdd,
FindPattern(BYONDLIB, (unsigned char *)WORLDSTRINGFINDORADD_SIGNATURE,
WORLDSTRINGFINDORADD_MASK),
unsigned int, (char *, char, char));
IMPORT_BYOND_FUNCTION(
_GetVariableFrom,
FindPattern(BYONDLIB, (unsigned char *)GETVARIABLEFROM_SIGNATURE,
GETVARIABLEFROM_MASK),
unsigned int, (unsigned int, unsigned int, unsigned int));
IMPORT_BYOND_FUNCTION(
SetVariableTo,
FindPattern(BYONDLIB, (unsigned char *)SETVARIABLETO_SIGNATURE,
SETVARIABLETO_MASK),
void,
(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int));
IMPORT_BYOND_FUNCTION(
_GetVariableFromListIndex,
FindPattern(BYONDLIB, (unsigned char *)GETVARIABLEFROMLISTINDEX_SIGNATURE,
GETVARIABLEFROMLISTINDEX_MASK),
unsigned int, (char, unsigned int, unsigned int));
IMPORT_BYOND_FUNCTION(
SetVariableToListIndex,
FindPattern(BYONDLIB, (unsigned char *)SETVARIABLETOLISTINDEX_SIGNATURE,
SETVARIABLETOLISTINDEX_SIGNATURE),
void, (char, unsigned int, unsigned int, unsigned int, unsigned int));
if (&WorldStringFindOrAdd == NULL || &_GetVariableFrom == NULL ||
&SetVariableTo == NULL || &_GetVariableFromListIndex == NULL ||
&SetVariableToListIndex == NULL)
init_ = 0;
else
init_ = 1;
}
// "Get" function wrappers.
// Get functions in BYOND return the value in EDX register, so we can't use them
// directly.
unsigned int GetVariableFrom(unsigned int type, unsigned int id,
unsigned int name_str_id, unsigned int *get_id) {
unsigned int get_type = _GetVariableFrom(type, id, name_str_id);
GET_EDX(*get_id);
return get_type;
}
unsigned int GetVariableFromListIndex(unsigned int type, unsigned int id,
unsigned int index,
unsigned int *get_id) {
unsigned int get_type = _GetVariableFromListIndex(type, id, index);
GET_EDX(*get_id);
return get_type;
}
unsigned int resolve_const_string(const char *res) {
return WorldStringFindOrAdd((char *)res, 0, 0);
}
unsigned int resolve_string(char *res) {
return WorldStringFindOrAdd(res, 0, 1);
}
// I couldn't track down where BYOND is resolving ref strings, so this would
// have to do
unsigned int decode_ref_string(char *str_ref, unsigned int *id) {
unsigned int uint_ref;
sscanf(str_ref, "[%x]", &uint_ref);
*id = uint_ref & 0xFFFFFF;
return uint_ref >> 24;
}
char initialized() { return init_; }
#ifdef _WIN32
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpvReserved) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
byondcore_base = GET_BYONDCORE_PTR();
get_byondcore_functions();
break;
case DLL_PROCESS_DETACH:
break;
}
return FALSE;
}
#elif defined(__unix__)
extern "C" DLL_EXPORT __attribute__((constructor)) int main() {
byondcore_base = GET_BYONDCORE_PTR();
get_byondcore_functions();
return 0;
}
#else
#pragma error Unknown main body semantics.
#endif
#endif // __MAIN_H__
#include "byondgate.h"
#include <stdint.h>
#include <stdio.h>
extern "C" DLL_EXPORT char *test(int argc, char *argv[]) {
unsigned int name_str_id, repl_str_id, type, id, get_type, get_id;
type = decode_ref_string(argv[0], &id);
char *buf = new char[1000];
name_str_id = resolve_const_string("hand");
repl_str_id = resolve_const_string("TEST NAME");
#ifdef DEBUG
return (char *)(std::to_string(name_str_id) + std::to_string).c_str();
#endif
get_type = GetVariableFrom(type, id, name_str_id, &get_id);
snprintf(buf, sizeof(buf) * 100, "%#x %#x [%#04x %#08x] [%#04x %#08x]",
name_str_id, repl_str_id, type, id, get_type, get_id);
SetVariableTo(type, id, name_str_id, 0x06, repl_str_id);
return buf;
}
/*
These are simple defaults for your project.
*/
#define LIBPATH "/root/MyStuff/Projects/BYOND/BYONDGate/tests/TestServer/sharedLib.so"
world
fps = 25 // 25 frames per second
icon_size = 32 // 32x32 icon size by default
view = 6 // show up to 6 tiles outward from center (13x13 view)
// Make objects move 8 pixels per tick when walking
mob
step_size = 8
var/hand = "left"
var/some_var = "lol"
obj
step_size = 8
/mob/verb/test()
src << "[hand] \ref[src]"
src << call(LIBPATH, "test")("\ref[src]")
src << hand
// DM Environment file for test.dme.
// All manual changes should be made outside the BEGIN_ and END_ blocks.
// New source code should be placed in .dm files: choose File/New --> Code File.
// BEGIN_INTERNALS
// END_INTERNALS
// BEGIN_FILE_DIR
#define FILE_DIR .
// END_FILE_DIR
// BEGIN_PREFERENCES
// END_PREFERENCES
// BEGIN_INCLUDE
#include "test.dm"
// END_INCLUDE
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment