support.cpp
changeset 19 6164205b828d
child 42 9209ac973efe
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/support.cpp	Mon May 11 23:01:38 2009 +0000
     1.3 @@ -0,0 +1,186 @@
     1.4 +/** vim: set ts=4 sw=4 et tw=99:
     1.5 + * 
     1.6 + * === Stripper for Metamod:Source ===
     1.7 + * Copyright (C) 2005-2009 David "BAILOPAN" Anderson
     1.8 + * No warranties of any kind.
     1.9 + * Based on the original concept of Stripper2 by botman
    1.10 + *
    1.11 + * License: see LICENSE.TXT
    1.12 + * ===================================
    1.13 + */
    1.14 +#include <new>
    1.15 +#include "support.h"
    1.16 +#include "parser.h"
    1.17 +#if defined WIN32
    1.18 +#include <windows.h>
    1.19 +#include <direct.h>
    1.20 +#else
    1.21 +#include <sys/types.h>
    1.22 +#include <sys/stat.h>
    1.23 +#include <unistd.h>
    1.24 +#endif
    1.25 +
    1.26 +Stripper g_Stripper;
    1.27 +stripper_game_t stripper_game;
    1.28 +
    1.29 +#if defined _WIN32
    1.30 +#define EXPORT extern "C" __declspec(dllexport)
    1.31 +#elif defined __GNUC__
    1.32 +#if __GNUC__ == 4
    1.33 +#define EXPORT extern "C" __attribute__ ((visibility("default")))
    1.34 +#else
    1.35 +#define EXPORT extern "C"
    1.36 +#endif
    1.37 +#endif
    1.38 +
    1.39 +static const char*
    1.40 +parse_map(const char* map, const char* entities)
    1.41 +{
    1.42 +    FILE *fp;
    1.43 +    char path[256];
    1.44 +
    1.45 +    g_Stripper.SetEntityList(entities);
    1.46 +
    1.47 +    stripper_game.path_format(path,
    1.48 +            sizeof(path),
    1.49 +            "%s/%s/global_filters.cfg",
    1.50 +            stripper_game.game_path,
    1.51 +            stripper_game.stripper_path);
    1.52 +    fp = fopen(path, "rt");
    1.53 +    if (fp == NULL)
    1.54 +    {
    1.55 +        stripper_game.log_message("Could not find global filter file: %s", path);
    1.56 +    }
    1.57 +    else
    1.58 +    {
    1.59 +        fclose(fp);
    1.60 +        g_Stripper.ApplyFileFilter(path);
    1.61 +    }
    1.62 +
    1.63 +    stripper_game.path_format(path,
    1.64 +            sizeof(path),
    1.65 +            "%s/%s/maps/%s.cfg",
    1.66 +            stripper_game.game_path,
    1.67 +            stripper_game.stripper_path,
    1.68 +            map);
    1.69 +    fp = fopen(path, "rt");
    1.70 +    if (fp)
    1.71 +    {
    1.72 +        fclose(fp);
    1.73 +        g_Stripper.ApplyFileFilter(path);
    1.74 +    }
    1.75 +
    1.76 +    return g_Stripper.ToString();
    1.77 +}
    1.78 +
    1.79 +static const char*
    1.80 +ent_string()
    1.81 +{
    1.82 +    return g_Stripper.ToString();
    1.83 +}
    1.84 +
    1.85 +static void
    1.86 +command_dump()
    1.87 +{
    1.88 +	char path[255];
    1.89 +
    1.90 +	stripper_game.path_format(path,
    1.91 +            sizeof(path),
    1.92 +            "%s/%s/dumps",
    1.93 +            stripper_game.game_path,
    1.94 +            stripper_game.stripper_path);
    1.95 +
    1.96 +#ifdef WIN32
    1.97 +	DWORD attr = GetFileAttributes(path);
    1.98 +	if ( (attr = INVALID_FILE_ATTRIBUTES) || (!(attr & FILE_ATTRIBUTE_DIRECTORY)) )
    1.99 +	{
   1.100 +		unlink(path);
   1.101 +		mkdir(path);
   1.102 +	}
   1.103 +#else
   1.104 +	struct stat s;
   1.105 +	
   1.106 +	if (stat(path, &s) != 0)
   1.107 +	{
   1.108 +		mkdir(path, 0775);
   1.109 +	}
   1.110 +    else
   1.111 +    {
   1.112 +		if (!(S_ISDIR(s.st_mode)))
   1.113 +		{
   1.114 +			unlink(path);
   1.115 +			mkdir(path, 0775);
   1.116 +		}
   1.117 +	}
   1.118 +#endif
   1.119 +
   1.120 +	int num = 0;
   1.121 +	char file[255];
   1.122 +	do
   1.123 +	{
   1.124 +		stripper_game.path_format(file,
   1.125 +                sizeof(file),
   1.126 +                "%s/%s.%04d.cfg",
   1.127 +                path,
   1.128 +                stripper_game.get_map_name(),
   1.129 +                num);
   1.130 +		FILE *fp = fopen(file, "rt");
   1.131 +		if (!fp)
   1.132 +			break;
   1.133 +		fclose(fp);
   1.134 +	} while (++num);
   1.135 +
   1.136 +	FILE *fp = fopen(file, "wt");
   1.137 +	fprintf(fp, "%s", g_Stripper.ToString());
   1.138 +	fclose(fp);
   1.139 +
   1.140 +	stripper_game.log_message("Logged map %s to file %s", stripper_game.get_map_name(), file);
   1.141 +}
   1.142 +
   1.143 +static void
   1.144 +plugin_unload()
   1.145 +{
   1.146 +}
   1.147 +
   1.148 +static stripper_core_t stripper_core =
   1.149 +{
   1.150 +    parse_map,
   1.151 +    ent_string,
   1.152 +    command_dump,
   1.153 +    plugin_unload
   1.154 +};
   1.155 +
   1.156 +EXPORT void
   1.157 +LoadStripper(const stripper_game_t* game, stripper_core_t* core)
   1.158 +{
   1.159 +    memcpy(&stripper_game, game, sizeof(stripper_game_t));
   1.160 +    memcpy(core, &stripper_core, sizeof(stripper_core_t));
   1.161 +}
   1.162 +
   1.163 +/* Overload a few things to prevent libstdc++ linking */
   1.164 +#if defined __linux__ || defined __APPLE__
   1.165 +extern "C" void __cxa_pure_virtual(void)
   1.166 +{
   1.167 +}
   1.168 +
   1.169 +void *operator new(size_t size)
   1.170 +{
   1.171 +	return malloc(size);
   1.172 +}
   1.173 +
   1.174 +void *operator new[](size_t size) 
   1.175 +{
   1.176 +	return malloc(size);
   1.177 +}
   1.178 +
   1.179 +void operator delete(void *ptr) 
   1.180 +{
   1.181 +	free(ptr);
   1.182 +}
   1.183 +
   1.184 +void operator delete[](void * ptr)
   1.185 +{
   1.186 +	free(ptr);
   1.187 +}
   1.188 +#endif
   1.189 +