Vice City: Multiplayer

VC:MP Discussion => Support => Tutorials => Topic started by: Xmair on Dec 26, 2019, 03:17 PM

Title: [Windows] Writing a C++ plugin
Post by: Xmair on Dec 26, 2019, 03:17 PM
No this isn't the best guide you can get. I wrote this tutorial reaaaaaaaallllllllly long ago for someone in Discord.

Firstly, you need to choose a good IDE for writing C++, what I personally use is Visual Studio.
So here the second step becomes loading up and creating a project in Visual Studio
(https://i.imgur.com/DYQWzEy.png)
Click the Create a new project button to start
Then you choose Dynamic-Link Library as that's what we're gonna be needing
(https://i.imgur.com/5LNexPg.png)
Next you'd want to select a name for your project, choose whatever name you want and name the solution to your desire aswell
(https://i.imgur.com/6yIFxav.png)
Click on Create, this may take some time
(https://i.imgur.com/7BTq4De.png)
We won't be needing dllmain.cpp and framework.h so you can delete them
Now firstly we need to include the VCMP header file, you can find the updated one here:
https://bitbucket.org/stormeus/0.4-squirrel/src/
Search for the VCMP.h file and click on it
(https://i.imgur.com/ReXkTD7.png)
Now copy all the file and come back to your Visual Studio project
Right click on Header Files
Go to Add -> New Item or simply press CTRL + SHIFT + A
Set the file type to Header File (.h)
Set the name to VCMP.h
(https://i.imgur.com/hhc7MLe.png)
Click on Add
Paste your clipboard (VCMP.h copied from BitBucket) into the new opened file
Now your file should look like this
(https://i.imgur.com/sB3HErn.png)
Create a new file in the Source Files folder named Main.cpp
Now before we move on to code, we need to select the compiling platform
In my case I needed x64 builds as Release and not Debug (it doesn't really matter at this point but anyway) so I changed my configuration to Release x64
(https://i.imgur.com/kMNo3a8.png)
Now we create another file in Header Files named Main.h which will serve as a header file for our main file
In the Main.h file, we do:
#pragma once // Only load this file once (#include)
#ifndef __MAIN_H // Include guard; makes sure that this file is only included once and subsequent #includes are ignored
#define __MAIN_H // Include guard

#include "VCMP.h"
#include <cstdio>

#ifdef _WIN32 // If we're on windows
#include "Windows.h"

#define DLL_EXPORT __declspec( dllexport ) // This is required in windows so that other programs can access the function
#else // We're not on windows
#define DLL_EXPORT // Nothing like that is required in linux so we leave it blank
#define MAX_PATH 250
#endif

#ifdef __cplusplus // We don't need to (and can not) extern "C" if we're already in C. so we make sure that we are in C++
extern "C" { // We need to extern "C" the function because windows expects a C-style function
#endif
    DLL_EXPORT    unsigned int            VcmpPluginInit(PluginFuncs* pluginFuncs, PluginCallbacks* pluginCalls, PluginInfo* pluginInfo); // This is a blank function and it's exported (check the #define tags above). VCMP requires it
#ifdef __cplusplus // We opened a brace in C++, so we need to close it in C++ only as well
}
#endif

#endif // Include guard
Now we come back to the Main.cpp file and,
#include "pch.h" // Pre-compiled header
#include "VCMP.h" // We need to include the VCMP header file first so we can access it's functons
#include "Main.h" // Including the main header file we created

uint8_t serverInitialise() {
    printf("Plugin initialised.\n"); // Sending a message to make sure our plugin has loaded
    return 1;
}

// This event is called when the plugin is loaded
// We can also make a global variable to store the plugin functions, callbacks and info
unsigned int VcmpPluginInit(PluginFuncs* pluginFuncs, PluginCallbacks* pluginCalls, PluginInfo* pluginInfo)
{
    pluginInfo -> apiMinorVersion = PLUGIN_API_MINOR; // This step is very important, in this step
    pluginInfo -> apiMajorVersion = PLUGIN_API_MAJOR; // we set the version of the API, if you skip this step, your server will not load this plugin

    pluginCalls -> OnServerInitialise = serverInitialise; // uint8_t(*OnServerInitialise) (void) - Line 915 @ VCMP.h
    return 1;
}
(https://i.imgur.com/RmDft52.png)
Now right click on your project name, in my case, VCMP
Click on Build
(https://i.imgur.com/AnUPWdr.png)
Your output should show something similar to this
(https://i.imgur.com/heZJYFr.png)
Now go to the compiled directory, in my case, C:\Users\Xmair\source\repos\VCMP\x64\Release
(https://i.imgur.com/ZQK8aDM.png)
Copy the VCMP.dll to a VC:MP server
Paste it in the plugins folder
(https://i.imgur.com/Ui0qWUx.png)
Now edit your server.cfg file to include your plugin
(https://i.imgur.com/jUNUETO.png)
Now load up your server.
(https://i.imgur.com/nWvHlhl.png)
Credits: Xmair & Fleka (for the comments in VCMP.h file)
Title: Re: [Windows] Writing a C++ plugin
Post by: D4rkR420R on Dec 27, 2019, 12:18 AM
Excellent tutorial! :D #C++Squad
Title: Re: [Windows] Writing a C++ plugin
Post by: habi on Apr 29, 2020, 05:55 AM
thanks. it worked for me just now.
In the new version of visual studio, you need to comment this line in pch.h
//#include "framework.h"
Title: Re: [Windows] Writing a C++ plugin
Post by: UncleRus on Apr 29, 2020, 07:37 AM
Xmair,thank you
Title: Re: [Windows] Writing a C++ plugin
Post by: Alpays on Dec 07, 2020, 10:44 AM
Plugin works but i cant use functions in vcmp.h i try calling from pluginFuncs->but server crashes
Title: Re: [Windows] Writing a C++ plugin
Post by: Xmair on Dec 07, 2020, 12:15 PM
Quote from: Alpays on Dec 07, 2020, 10:44 AMPlugin works but i cant use functions in vcmp.h i try calling from pluginFuncs->but server crashes
What function? Where? How? Any code?
Title: Re: [Windows] Writing a C++ plugin
Post by: Alpays on Dec 07, 2020, 12:58 PM
I build a plugin it works
#include "VCMP.h" // plugin api
#include "Main.h" // main.h same as the tutorial

PluginFuncs* globalFuncs;


uint8_t serverInitialise() {
    printf("Plugin initialised.\n");
   
    return 1;
}

void playerJoin(int32_t playerid) {
    printf("Player joined with id %d", playerid);
}

unsigned int VcmpPluginInit(PluginFuncs* pluginFuncs, PluginCallbacks* pluginCalls, PluginInfo* pluginInfo)
{
    pluginInfo->apiMinorVersion = PLUGIN_API_MINOR;
    pluginInfo->apiMajorVersion = PLUGIN_API_MAJOR;

    pluginCalls->OnServerInitialise = serverInitialise;
    pluginCalls->OnPlayerConnect = playerJoin;

    pluginFuncs = globalFuncs;

    return 1;
}

but when i add a function from plugin.h like
globalFuncs->SetPlayerWeapon(playerid, 26, 999); to onPlayerConnect callback


it builds fine but crashes when a player join (or if i add pluginFuncs->SetServerName("blabla"); in on server init it crashes on server start) this happens at every function without anything simply server closes with no error
Title: Re: [Windows] Writing a C++ plugin
Post by: habi on Dec 07, 2020, 01:49 PM
Now it is clear.

pluginFuncs = globalFuncs;

Put it reverse like
globalFuncs =pluginFuncs
Title: Re: [Windows] Writing a C++ plugin
Post by: Alpays on Dec 07, 2020, 02:12 PM
Quote from: habi on Dec 07, 2020, 01:49 PMNow it is clear.

pluginFuncs = globalFuncs;

Put it reverse like
globalFuncs =pluginFuncs

How did i missed this xD  thank you for your help
also xmair for this tutorial