[help] Understading Mt2 function calls

02/18/2024 00:34 Nawky#1
Hello pvpers,


I've been investigating this for a while now and I feel that I've come to a brick wall therefore I'd like to request your help.

I've done a cpp DLL that calls a python script as soon as it is injected into the metin2 client.

I've successfully printed some messages in the chat with using the chat import.

But I've not been successful with any other function.

This is a basic example that print in the chat a message but blows up afterwards:

Code:
chat = __import__("XXsRHxy")
chr = __import__("chr")

chat.AppendChat(7, "test")

for i in xrange(100000):
    if(chr.HasInstance(i) != 0):
        if(chr.GetInstanceType(i) == 6):
            chr.SelectInstance(i);
            chat.AppendChat(7, str(i));
I get a "test" written to the chat, but then it blows up, before anyone asks, there are no printed logs I've tried to catch the error on the cpp side and check for

Code:
    
if (PyErr_Occurred())
        PyErr_Print();
but I have nothing printed out in the logs, if there is a python error usually it prints out something.

I wanted to do a simple scan for other players and in the future interact with them. Any suggestion is appreciated

I think it's not relevant but here is the cpp code:

Code:
#include "pch.h"
#include <Python.h>
#include <thread>
#include <iostream>
#include <vector>


bool executePythonFile(const char* file) {
    int result = 0;
    char path[256] = { 0 };
    strcat_s(path, file);
    printf("Executing Python file: %s\n", path);

    PyObject* PyFileObject = PyFile_FromString(path, (char*)"r");
    if (PyFileObject == NULL) {
        printf("%s  is not a File!\n", path);
        goto error_code;
    }
    result = PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "MyFile", 1);
    if (result == -1) {
        printf("Error executing python script!\n");
        goto error_code;
    }
    else {
        printf("Python script execution complete!\n");
        Py_DECREF(PyFileObject);
        return true;
    }

    if (PyErr_Occurred())
        PyErr_Print();

error_code:
    PyErr_Print();
    Py_DECREF(PyFileObject);
    return 0;
}


void init() {

    AllocConsole();
    freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
    std::cout << "### START ###" << std::endl;
    
    executePythonFile("C:\\code\\testing.py");
 
    FreeConsole();
}

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH: {
        // sdk::utilities::h_module = (HMODULE)hDllHandle;
        init();
        FreeLibraryAndExitThread(hModule, 0);
    }break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
02/18/2024 23:42 MrCrisp#2
Hey Nawky,

it could be a failing function call from the chr module. I don't remember the function signature of those functions, but I would check them individually.

Maybe a tip: Get a pointer to the VID map in memory instead of running over a million VIDs and check each individually.
02/21/2024 23:17 dave_m#3
What @[Only registered and activated users can see links. Click Here To Register...] says and in addition, you can call python functions directly using c++ with certain python commands like "PyObject_CallObject"
03/01/2024 11:46 Godalike#4
try dave's, it can work