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