SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 11/02/2016, 03:11 PM   #1
Seregamil1
Big Clucker
 
Join Date: Nov 2012
Location: Russia, Altay
Posts: 132
Reputation: 45
Default .NET Plugin - Write PLUGINS in C#

So. This is a experimental project that allows you to write plugins at C#.


The functions on the server side:
callDotnetMethod - it calls the C# method
Code:
Parameter's:
methodName - name of the method split[] - key for parameters
i - int32 f - float s - string c - char
{Float,_}:... - arguments
Return's:
int32, boolean, float
Quote:
Due to the fact that AMX can not return a text value, it was necessary to create an alternative function based on the previous
callDotnetMethodStr - An alternative to the previous function, but it records the text in variable
Code:
Parameter's:
methodName - name of the method split[] - key for parameters
i - int32 f - float s - string c - char
str[] - array for string length - array length {Float,_}:... - arguments
Always returns 1!
The functions on the plugin side:

cpp.callRemoteCallback - It calls a function by name and passes parameters.
Code:
Parameter's:
string callback - callback name params object[] args - arguments
cpp.logwrite - write message to console
An example of using these functions

Pawn:
PHP Code:
#include <a_samp>

native callDotnetMethod(methodName[], split[], {Float,_}:...);
native callDotnetMethodStr(methodName[], str[], lensplit[], {Float,_}:...);

main(){

}

public 
OnGameModeInit() {
    
callDotnetMethod("onDotnetLoaded""cf"'v'1.0);

    new 
temp_int callDotnetMethod("kernel.testINT""ii"1024);
    
printf("testINT returned: %i"temp_int);
    
    new 
temp_bool callDotnetMethod("kernel.testBOOL""isi"1"boolean=)"0);
    
printf("testBOOL returned: %i"temp_bool);
    
    new 
temp_float callDotnetMethod("kernel.testFLOAT""ifcf"101.432'c'242);
    
printf("testFLOAT returned: %f"temp_float);
    
    new 
temp_str16 ] ;
    
callDotnetMethodStr("kernel.testSTRING"temp_strsizeof temp_str"sss""by""Seregamil""dotNET v1.0");
    
printf("testSTRING returned: %s"temp_str);
    return 
true ;
}

forward onDotnetWasLoaded(str[]);
public 
onDotnetWasLoaded(str[]){
    print(
str);

C#:
PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace 
c_sharp
{
    public class 
kernel
    
{
        public static 
object onDotnetLoaded(params object[] args)
        {
            
cpp.logwrite("dotnet-> onDotnetLoaded was called. ARGS:");
            foreach (
object arg in args)
            {
                
cpp.logwrite(arg.ToString());
            }

            
cpp.callRemoteCallback("onDotnetWasLoaded""Hello! =)");
            return 
true;
        }

        public static 
object testINT(params object[] args)
        {
            
cpp.logwrite("dotnet-> testINT was called");
            
int a Convert.ToInt32(args[0]);
            
int b Convert.ToInt32(args[1]);
            return 
;
        }

        public static 
object testBOOL(params object[] args)
        {
            
cpp.logwrite("dotnet-> testBOOL was called.");
            return 
false;
        }

        public static 
object testSTRING(params object[] args)
        {
            
cpp.logwrite("dotnet-> testSTRING was called." );

            foreach (
object arg in args)
            {
                
cpp.logwrite(string.Format("{0}: {1}",arg.GetType(), arg.ToString()));
            }

            return 
"blackJack prod.";
        }

        public static 
object testFLOAT(params object[] args)
        {
            
cpp.logwrite("dotnet-> testFLOAT was called. ARGS:");
            
float result 13.228f;
            return 
result;
        }
    }

Result:


The solution contains 2 project. First at C #, the second at C ++. It's a COM assembly.
When compiling formed a * .dll file in the right directory. In my case it S:\gta-o\plugins\.
I'm using VS 2012 Express.

ATTENTION. COMPILATION OF PROJECT MADE IN ADMINISTATOR MODE

I have not tested the project in a Linux environment.

Link to project with examples: https://github.com/Seregamil/.NET-plugin
Seregamil1 is offline   Reply With Quote
Old 11/02/2016, 03:16 PM   #2
Seregamil1
Big Clucker
 
Join Date: Nov 2012
Location: Russia, Altay
Posts: 132
Reputation: 45
Default Re: .NET Plugin - Write PLUGINS in C#

Added class for working with regular expressions

regex.IsMatch - Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.
regex.Match - Searches the specified input string for the first occurrence of the specified regular expression.
regex.Replace -

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

The names of the functions are taken from here ( http://forum.sa-mp.com/showthread.php?t=247893 )

macro:
PHP Code:
#define regex_match(%0,%1) callDotnetMethod("regex.Match", "ss", %0, %1)
#define regex_is_match(%0,%1) callDotnetMethod("regex.IsMatch", "ss", %0, %1)
#define regex_replace(%0,%1,%2) callDotnetMethodStr("regex.Replace", %0, sizeof(%0), "sss", %0, %1, %2) 
class regex: https://github.com/Seregamil/.NET-pl...sharp/regex.cs
example script: https://github.com/Seregamil/.NET-pl...egex/regex.pwn
Seregamil1 is offline   Reply With Quote
Old 11/02/2016, 03:35 PM   #3
Ahmad45123
Gangsta
 
Ahmad45123's Avatar
 
Join Date: Oct 2013
Location: Egypt
Posts: 826
Reputation: 187
Default Re: .NET Plugin - Write PLUGINS in C#

Nice.. Could be useful to make a c# backend and a pawn frontend.
__________________

ExtremeStudio

The Best SAMP IDE.
Ahmad45123 is offline   Reply With Quote
Old 11/02/2016, 04:48 PM   #4
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,324
Reputation: 652
Default Re: .NET Plugin - Write PLUGINS in C#

This could be great!
Crayder is offline   Reply With Quote
Old 12/02/2016, 04:29 PM   #5
erorcun
Big Clucker
 
Join Date: Aug 2009
Posts: 161
Reputation: 28
Default Re: .NET Plugin - Write PLUGINS in C#

Is this allows us to use multithread?
erorcun is offline   Reply With Quote
Old 13/02/2016, 03:03 AM   #6
Seregamil1
Big Clucker
 
Join Date: Nov 2012
Location: Russia, Altay
Posts: 132
Reputation: 45
Default Re: .NET Plugin - Write PLUGINS in C#

Quote:
Originally Posted by erorcun View Post
Is this allows us to use multithread?
Theoretically - yes.
In practice - I have not yet experienced.

In the evening I try to run the multi-threading.
Seregamil1 is offline   Reply With Quote
Old 13/02/2016, 06:55 AM   #7
DevinPatino
Big Clucker
 
DevinPatino's Avatar
 
Join Date: Dec 2013
Location: Compton
Posts: 74
Reputation: 1
Default Re: .NET Plugin - Write PLUGINS in C#

I actually think this might be useful pat yourself on the back young pawadan +rep.
__________________
Herro
DevinPatino is offline   Reply With Quote
Old 13/02/2016, 07:31 AM   #8
kristo
Banned
 
Join Date: Jun 2012
Location: Estonia
Posts: 352
Reputation: 179
Default Re: .NET Plugin - Write PLUGINS in C#

Well... I know what it is and I can see why it's useful but could someone explain me, how is it different from creating a plugin in C++ and calling the natives in PAWN?
kristo is offline   Reply With Quote
Old 13/02/2016, 08:43 AM   #9
Amunra
Gangsta
 
Amunra's Avatar
 
Join Date: Nov 2015
Location: Indonesian
Posts: 502
Reputation: 12
Default Re: .NET Plugin - Write PLUGINS in C#

Good Job,Keep it Up !
__________________
Legendary Criminals


Amunra is offline   Reply With Quote
Old 13/02/2016, 07:07 PM   #10
Crayder
Banned
 
Join Date: Sep 2013
Location: Flames of Hell
Posts: 3,324
Reputation: 652
Default Re: .NET Plugin - Write PLUGINS in C#

Quote:
Originally Posted by kvann View Post
Well... I know what it is and I can see why it's useful but could someone explain me, how is it different from creating a plugin in C++ and calling the natives in PAWN?
Sometimes it's too much work to compile an entire plugin just for something simple. Some people don't even know C++. Some people don't even know how to make a SA-MP plugin. Sometimes C# can do a job easier than C++. Sometimes C++ can't do something C# can (and vice-versa). And many more...
Crayder is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[14:42:20] [14:42:20] Server Plugins [14:42:20] -------------- [14:42:20] Loading plugin: mysql.so [14:42:20] Failed Sn4ke2 Scripting Help 6 25/06/2015 01:44 PM
[Tutorial] Using y_xml to write/read xml files. [Another way to write CFG data but better] Ahmad45123 Tutorials 8 13/03/2015 02:41 AM
what is plugin plugins.so betta Scripting Help 7 17/02/2014 10:17 PM
[Volt-Host] Whirlpool plugin & Other plugins not loading Keisuke Server Support 5 09/09/2012 02:07 AM
Write plugin in C? Double-O-Seven Scripting Help 3 28/10/2011 03:06 PM


All times are GMT. The time now is 09:39 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.