support.cpp
author Dr!fter <drifter01620@gmail.com>
Sat Aug 16 17:03:07 2014 -0400 (2014-08-16)
changeset 73 dd05779903ed
parent 48 368cec2fcccf
child 75 e6ddc8400a1c
permissions -rw-r--r--
Fix AMBuildScript.
     1 /** vim: set ts=4 sw=4 et tw=99:
     2  * 
     3  * === Stripper for Metamod:Source ===
     4  * Copyright (C) 2005-2009 David "BAILOPAN" Anderson
     5  * No warranties of any kind.
     6  * Based on the original concept of Stripper2 by botman
     7  *
     8  * License: see LICENSE.TXT
     9  * ===================================
    10  */
    11 #include <new>
    12 #include "support.h"
    13 #include "parser.h"
    14 #if defined WIN32
    15 #include <windows.h>
    16 #include <direct.h>
    17 #else
    18 #include <sys/types.h>
    19 #include <sys/stat.h>
    20 #include <unistd.h>
    21 #endif
    22 
    23 Stripper g_Stripper;
    24 stripper_game_t stripper_game;
    25 
    26 #if defined _WIN32
    27 #define EXPORT extern "C" __declspec(dllexport)
    28 #elif defined __GNUC__
    29 #if __GNUC__ == 4
    30 #define EXPORT extern "C" __attribute__ ((visibility("default")))
    31 #else
    32 #define EXPORT extern "C"
    33 #endif
    34 #endif
    35 
    36 static const char*
    37 parse_map(const char* map, const char* entities)
    38 {
    39     FILE *fp;
    40     char path[256];
    41 
    42     g_Stripper.SetEntityList(entities);
    43 
    44     stripper_game.path_format(path,
    45             sizeof(path),
    46             "%s/%s/global_filters.cfg",
    47             stripper_game.game_path,
    48             stripper_game.stripper_cfg_path);
    49     fp = fopen(path, "rt");
    50     if (fp == NULL)
    51     {
    52         stripper_game.log_message("Could not find global filter file: %s", path);
    53     }
    54     else
    55     {
    56         fclose(fp);
    57         g_Stripper.ApplyFileFilter(path);
    58     }
    59 
    60     stripper_game.path_format(path,
    61             sizeof(path),
    62             "%s/%s/maps/%s.cfg",
    63             stripper_game.game_path,
    64             stripper_game.stripper_cfg_path,
    65             map);
    66     fp = fopen(path, "rt");
    67     if (fp)
    68     {
    69         fclose(fp);
    70         g_Stripper.ApplyFileFilter(path);
    71     }
    72 
    73     return g_Stripper.ToString();
    74 }
    75 
    76 static const char*
    77 ent_string()
    78 {
    79     return g_Stripper.ToString();
    80 }
    81 
    82 static void
    83 command_dump()
    84 {
    85 	char path[255];
    86 
    87 	stripper_game.path_format(path,
    88             sizeof(path),
    89             "%s/%s/dumps",
    90             stripper_game.game_path,
    91             stripper_game.stripper_path);
    92 
    93 #ifdef WIN32
    94 	DWORD attr = GetFileAttributes(path);
    95 	if ( (attr = INVALID_FILE_ATTRIBUTES) || (!(attr & FILE_ATTRIBUTE_DIRECTORY)) )
    96 	{
    97 		unlink(path);
    98 		mkdir(path);
    99 	}
   100 #else
   101 	struct stat s;
   102 	
   103 	if (stat(path, &s) != 0)
   104 	{
   105 		mkdir(path, 0775);
   106 	}
   107     else
   108     {
   109 		if (!(S_ISDIR(s.st_mode)))
   110 		{
   111 			unlink(path);
   112 			mkdir(path, 0775);
   113 		}
   114 	}
   115 #endif
   116 
   117 	int num = 0;
   118 	char file[255];
   119 	do
   120 	{
   121 		stripper_game.path_format(file,
   122                 sizeof(file),
   123                 "%s/%s.%04d.cfg",
   124                 path,
   125                 stripper_game.get_map_name(),
   126                 num);
   127 		FILE *fp = fopen(file, "rt");
   128 		if (!fp)
   129 			break;
   130 		fclose(fp);
   131 	} while (++num);
   132 
   133 	FILE *fp = fopen(file, "wt");
   134 	fprintf(fp, "%s", g_Stripper.ToString());
   135 	fclose(fp);
   136 
   137 	stripper_game.log_message("Logged map %s to file %s", stripper_game.get_map_name(), file);
   138 }
   139 
   140 static void
   141 plugin_unload()
   142 {
   143 }
   144 
   145 static stripper_core_t stripper_core =
   146 {
   147     parse_map,
   148     ent_string,
   149     command_dump,
   150     plugin_unload
   151 };
   152 
   153 EXPORT void
   154 LoadStripper(const stripper_game_t* game, stripper_core_t* core)
   155 {
   156     memcpy(&stripper_game, game, sizeof(stripper_game_t));
   157     memcpy(core, &stripper_core, sizeof(stripper_core_t));
   158 }
   159 
   160 /* Overload a few things to prevent libstdc++ linking */
   161 #if defined __linux__ || defined __APPLE__
   162 extern "C" void __cxa_pure_virtual(void)
   163 {
   164 }
   165 
   166 void *operator new(size_t size)
   167 {
   168 	return malloc(size);
   169 }
   170 
   171 void *operator new[](size_t size) 
   172 {
   173 	return malloc(size);
   174 }
   175 
   176 void operator delete(void *ptr) 
   177 {
   178 	free(ptr);
   179 }
   180 
   181 void operator delete[](void * ptr)
   182 {
   183 	free(ptr);
   184 }
   185 #endif
   186