<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2018 urShadow
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"

#include "urmem/urmem.hpp"

#include <unordered_map>
#include <unordered_set>
#include <regex>
#include <string>
#include <vector>
#include <list>
#include <queue>

#include "Pawn.CMD.inc"

#ifdef THISCALL
#undef THISCALL
#endif

#ifdef _WIN32
#define THISCALL __thiscall
#else
#define THISCALL
#endif

extern void *pAMXFunctions;

class Plugin {
public:
    static constexpr char
        *kName = "Pawn.CMD",
        *kVersion = "3.2.0",
        *kPublicVarNameVersion = "_pawncmd_version",
        *kPublicVarNameIsGamemode = "_pawncmd_is_gamemode",
#ifdef _WIN32
        *kOpctPattern =
        "\x83\xEC\x08"          /*sub esp,0x8*/ \
        "\x53"                  /*push ebx*/ \
        "\x8B\x5C\x24\x14"      /*mov ebx,DWORD PTR [esp+0x14]*/ \
        "\x55"                  /*push ebp*/ \
        "\x8B\x6C\x24\x14"      /*mov ebp,DWORD PTR [esp+0x14]*/ \
        "\x56"                  /*push esi*/ \
        "\x33\xF6"              /*xor esi,esi*/ \
        "\x57"                  /*push edi*/ \
        "\x8B\xF9"              /*mov edi,ecx*/ \
        "\x89\x74\x24\x10"      /*mov DWORD PTR [esp+0x10],esi*/ \
        "\x8B\x04\xB7"          /*mov eax,DWORD PTR [edi+esi*4]*/ \
        "\x85\xC0",             /*test eax,eax*/
        *kOpctMask = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#else
        *kOpctPattern =
        "\x55"                          /*push ebp*/ \
        "\x89\xE5"                      /*mov ebp,esp*/ \
        "\x57"                          /*push edi*/ \
        "\x56"                          /*push esi*/ \
        "\x53"                          /*push ebx*/ \
        "\x83\xEC\x2C"                  /*sub esp,0x2c*/ \
        "\x8B\x75\x08"                  /*mov esi,DWORD PTR [ebp+0x8]*/ \
        "\xC7\x45\xE4\x00\x00\x00\x00"  /*mov DWORD PTR [ebp-0x1c],0x0*/ \
        "\x8B\x7D\x10"                  /*mov edi,DWORD PTR [ebp+0x10]*/ \
        "\x89\xF3"                      /*mov ebx,esi*/ \
        "\xEB\x14",                     /*jmp 0x2e*/
        *kOpctMask = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
#endif

    struct Command;
    struct Script;

    using logprintf_t = void(*)(const char *format, ...);
    using m = urmem;

    using CommandMap = std::unordered_map<std::string, Command>;
    using ScriptList = std::list<Script>;
    using CmdArray = std::vector<std::string>;
    using CmdArraySet = std::unordered_set<std::shared_ptr<CmdArray>>;

    struct Command {
        cell addr;
        unsigned int flags;
        bool is_alias;
    };

    struct Script {
        Command &GetCommand(const std::string &name) {
            const auto iter = cmds.find(name);

            if (iter == cmds.end()) {
                throw std::runtime_error{"command '" + name + "' not found"};
            }

            return iter->second;
        }

        AMX *amx;
        cell opct_addr, opcr_addr, opcp_addr, on_init_addr;
        std::deque<cell> init_flags_and_aliases_addresses;
        CommandMap cmds;
    };

    static bool Load(void **ppData) {
        pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];

        _logprintf = reinterpret_cast<logprintf_t>(ppData[PLUGIN_DATA_LOGPRINTF]);

        m::sig_scanner scanner;
        if (scanner.init(reinterpret_cast<void *>(_logprintf))) {
            m::address_t addr{};
            if (scanner.find(kOpctPattern, kOpctMask, addr)) {
                _hook_fs__on_player_command_text = std::make_shared<m::hook>(
                    addr,
                    m::get_func_addr(&HOOK_CFilterScripts__OnPlayerCommandText)
                );

                _logprintf("%s plugin v%s by urShadow has been loaded", kName, kVersion);

                return true;
            }
        }

        _logprintf("[%s] %s: CFilterScripts::OnPlayerCommandText not found", kName, __FUNCTION__);

        return false;
    }

    static void Unload() {
        _hook_fs__on_player_command_text.reset();
        _scripts.clear();
        _cmd_array_set.clear();<commit_msg>Fix argument type mismatch<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2018 urShadow
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"

#include "urmem/urmem.hpp"

#include <unordered_map>
#include <unordered_set>
#include <regex>
#include <string>
#include <vector>
#include <list>
#include <queue>

#include "Pawn.CMD.inc"

#ifdef THISCALL
#undef THISCALL
#endif

#ifdef _WIN32
#define THISCALL __thiscall
#else
#define THISCALL
#endif

extern void *pAMXFunctions;

class Plugin {
public:
    static constexpr char
        *kName = "Pawn.CMD",
        *kVersion = "3.2.0",
        *kPublicVarNameVersion = "_pawncmd_version",
        *kPublicVarNameIsGamemode = "_pawncmd_is_gamemode",
#ifdef _WIN32
        *kOpctPattern =
        "\x83\xEC\x08"          /*sub esp,0x8*/ \
        "\x53"                  /*push ebx*/ \
        "\x8B\x5C\x24\x14"      /*mov ebx,DWORD PTR [esp+0x14]*/ \
        "\x55"                  /*push ebp*/ \
        "\x8B\x6C\x24\x14"      /*mov ebp,DWORD PTR [esp+0x14]*/ \
        "\x56"                  /*push esi*/ \
        "\x33\xF6"              /*xor esi,esi*/ \
        "\x57"                  /*push edi*/ \
        "\x8B\xF9"              /*mov edi,ecx*/ \
        "\x89\x74\x24\x10"      /*mov DWORD PTR [esp+0x10],esi*/ \
        "\x8B\x04\xB7"          /*mov eax,DWORD PTR [edi+esi*4]*/ \
        "\x85\xC0",             /*test eax,eax*/
        *kOpctMask = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#else
        *kOpctPattern =
        "\x55"                          /*push ebp*/ \
        "\x89\xE5"                      /*mov ebp,esp*/ \
        "\x57"                          /*push edi*/ \
        "\x56"                          /*push esi*/ \
        "\x53"                          /*push ebx*/ \
        "\x83\xEC\x2C"                  /*sub esp,0x2c*/ \
        "\x8B\x75\x08"                  /*mov esi,DWORD PTR [ebp+0x8]*/ \
        "\xC7\x45\xE4\x00\x00\x00\x00"  /*mov DWORD PTR [ebp-0x1c],0x0*/ \
        "\x8B\x7D\x10"                  /*mov edi,DWORD PTR [ebp+0x10]*/ \
        "\x89\xF3"                      /*mov ebx,esi*/ \
        "\xEB\x14",                     /*jmp 0x2e*/
        *kOpctMask = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
#endif

    struct Command;
    struct Script;

    using logprintf_t = void(*)(const char *format, ...);
    using m = urmem;

    using CommandMap = std::unordered_map<std::string, Command>;
    using ScriptList = std::list<Script>;
    using CmdArray = std::vector<std::string>;
    using CmdArraySet = std::unordered_set<std::shared_ptr<CmdArray>>;

    struct Command {
        cell addr;
        unsigned int flags;
        bool is_alias;
    };

    struct Script {
        Command &GetCommand(const std::string &name) {
            const auto iter = cmds.find(name);

            if (iter == cmds.end()) {
                throw std::runtime_error{"command '" + name + "' not found"};
            }

            return iter->second;
        }

        AMX *amx;
        cell opct_addr, opcr_addr, opcp_addr, on_init_addr;
        std::deque<cell> init_flags_and_aliases_addresses;
        CommandMap cmds;
    };

    static bool Load(void **ppData) {
        pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];

        _logprintf = reinterpret_cast<logprintf_t>(ppData[PLUGIN_DATA_LOGPRINTF]);

        m::sig_scanner scanner;
        if (scanner.init(reinterpret_cast<urmem::address_t>(_logprintf))) {
            m::address_t addr{};
            if (scanner.find(kOpctPattern, kOpctMask, addr)) {
                _hook_fs__on_player_command_text = std::make_shared<m::hook>(
                    addr,
                    m::get_func_addr(&HOOK_CFilterScripts__OnPlayerCommandText)
                );

                _logprintf("%s plugin v%s by urShadow has been loaded", kName, kVersion);

                return true;
            }
        }

        _logprintf("[%s] %s: CFilterScripts::OnPlayerCommandText not found", kName, __FUNCTION__);

        return false;
    }

    static void Unload() {
        _hook_fs__on_player_command_text.reset();
        _scripts.clear();
        _cmd_array_set.clear();
{'fraction_non_alphanumeric': 0.11970002884338045, 'fraction_numerical': 0.023843861167195463, 'mean_word_length': 1.9968308844713338, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3250094', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3277, 'n_words': 865}