Skip to content

Instantly share code, notes, and snippets.

@sulix
Created March 21, 2022 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sulix/09f059c468fcb58eefbca8ef46006ccb to your computer and use it in GitHub Desktop.
Save sulix/09f059c468fcb58eefbca8ef46006ccb to your computer and use it in GitHub Desktop.
Loki CivCTP function wrapper
// Step 1: Hex-Edit the civctp.dynamic executable
// - Rename the libSDL import to point to SDL 1.2
// - Rename all the functions below in the import tables:
// - __builtin_new → x_builtin_new (etc.)
// - {mem,str,strn}cpy → {mem,str,strn}cpx
// - Make sure you have libttf and libsmpeg in the current directory
// - Build this with:
// cc -shared -m32 -fPIC -o libcivctp_wrapper.so civctp_wrapper.c
// - Run CivCTP with:
// LD_PRELOAD=libcivctp_wrapper.so LD_LIBRARY_PATH=. ./civctp.dynamic
// - Beware of bugs:
// - It'll probably corrupt memory and crash all the time. :/
// (Changing the malloc(x) → calloc(x, 2) fixes some of these, I think, but…)
// - It doesn't work properly with sdl12-compat yet
// - Multiplayer likely still won't work.
// - Sound does, but CD-audio doesn't without some further hacks.
#include <stdlib.h>
#include <string.h>
void *x_builtin_new(unsigned int size)
{
return malloc(size);
}
void x_builtin_delete(void *ptr)
{
return free(ptr);
}
void *x_builtin_vec_new(unsigned int size)
{
return malloc(size);
}
void x_builtin_vec_delete(void *ptr)
{
return free(ptr);
}
/* Placement new */
void *x_nw__FUiPv(int size, void *p)
{
return p;
}
/* CivCTP overlaps all the time in memcpy, strcpy, and strncpy. */
void *memcpx(void *dest, const void *src, size_t n)
{
return memmove(dest, src, n);
}
char *strcpx(char *dest, const char *src)
{
size_t len = strlen(src);
char *ret = memmove(dest, src, len);
dest[len] = '\0';
return ret;
}
char *strncpx(char *dest, const char *src, size_t n)
{
/* Taken from 'man strncpy' */
size_t i;
for (i = 0; i < n && src[i] != '\0'; i++)
dest[i] = src[i];
for ( ; i < n; i++)
dest[i] = '\0';
return dest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment