AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   SharpSource (https://forums.alliedmods.net/showthread.php?t=139156)

ToXedVirus 09-27-2010 03:37

SharpSource
 
SharpSource

Short description:
SharpSource is a SourceMod extension which enables the developer to create extensions in C#. Like adding new native functions while using C#.

Status report:
This extension is currently in an early development stage, while the code is already available in order to encourage other people to participate in the development, binaries will be shipped when the extension will provide more functionality.

Currently the plugin loads managed (C#) code and loads managed assemblies, note how the arguments are all marshalled.

TODO:
  • Adjust and compliment (with helper functions) the exposed C++ interfaces for easy access
  • Create a safe subplugin system
  • Add IronRuby support
  • Add IronPython support

DONE:
  • Access C++ interfaces from within C#
  • Generate C++ wrapper classes with gccxml/a C# generator
  • Support native function creation

Credits:
The CXXI Project - an awesome C++ bindings generator for C#
Bailoplan for such a great project

Code is available at github

Some of the interfaces available in the C++ world are now available in the C# world.
I am continuing to add more and more, but I have to adjust them to make the look as native as possible.
A lot of the code is autogenerated, but the generated code needs a lot of adjustments.

Adding native functions from the C# world looks already quite awesome and easy to do:

PHP Code:

using System;
using AlliedMods;
using AlliedMods.SourceMod;
using AlliedMods.SourcePawn;

namespace 
TestSuite
{
    public class 
InteropTests IExt
    
{
        static 
int tests 0;

        public 
InteropTests()
        {
            
Core.ShareSys.AddNatives("test_addnative1", (IntPtr contextIntPtr arguments) => {
                
tests++;
                return 
1;
            });

            
Core.ShareSys.AddNatives("test_addnative2", (IPluginContext contextint[] arguments) => {
                
tests++;
                return 
2;
            });


            
Core.ShareSys.AddNatives(Core.Extensiontypeof(InteropTests));
        }

        [
SPMethod]
        public static 
void test_addnative0()
        {
        }

        [
SPMethod("test_addnative3")]
        public static 
int test_addnative3()
        {
            
tests++;
            return 
3;
        }

        [
SPMethod]
        public static 
int test_addnative4()
        {
            
tests++;
            return 
4;
        }

        [
SPMethod]
        public static 
int test_addnative5(int argument)
        {
            
tests++;
            if (
argument != 42) {
                
Console.WriteLine("failed test_addnative5");
            }
            return 
5;
        }

        [
SPMethod]
        public static 
int test_addnative6()
        {
            
tests++;
            return 
42;
        }

        [
SPMethod]
        public static 
int test_addnative7(int i)
        {
            
tests++;
            return 
i;
        }

        [
SPMethod]
        public static 
void test_end(int count)
        {
            
Console.WriteLine();
            
Console.WriteLine("tests executed {0}/{1}"testscount);
            
Console.WriteLine();
        }

        [
SPMethod]
        public static 
void test_callback(Action<SPBuffercallbackSPBuffer bufferuint maxlength)
        {
            if (
buffer.Get() != "HELLO WORLD") {
                
Console.WriteLine("FAILED");
            }

            
buffer.Set(buffer.Get().ToLower(), maxlength);
            
callback(buffer);
        }
    }


PHP Code:

#include <sourcemod>

public Plugin:myinfo =
{
  
name "SharpSource example plugin",
  
author "Andrius Bentkus",
  
description "SharpSource testing suite",
  
version "0.0.1.0",
  
url "no site available"
};

native test_addnative0();
native test_addnative1();
native test_addnative2();
native test_addnative3();
native test_addnative4();
native test_addnative5(i);
native test_addnative6();
native test_addnative7(i);
native test_end(i);

native test_callback(Function:funcString:buffer[], maxlength);

// new Function:callback = GetFunctionByName(GetMyHandle(), "callback_func");
public callback_func(String:str[])
{
  
assert(strcmp(str"hello world") == 0);
}


public 
OnPluginStart()
{
  
assert(test_addnative0() == 0);
  
assert(test_addnative1() == 1);
  
assert(test_addnative2() == 2);
  
assert(test_addnative3() == 3);
  
assert(test_addnative4() == 4);
  
assert(test_addnative5(42) == 5);
  
assert(test_addnative6() == 42);
  new 
1234;
  
assert(test_addnative7(i) == i);
  
test_end(7);
  new 
String:buffer[] = "HELLO WORLD";
  
test_callback(callback_funcbuffersizeof(buffer));



rhelgeby 09-27-2010 06:37

Re: SharpSource
 
Interesting concept. :)

Matthias Vance 10-05-2011 12:09

Re: SharpSource
 
I know this is a very old thread, and only for software that will never be developed further, but I still felt the need to note this.

NOTE

Because of the nature of .NET (and Mono), loaded assemblies (plugins) cannot be unloaded, and therefore this extension will leak memory, unless they are seperated into multiple AppDomains. This is also not doable because of the huge performance hit for cross-AppDomain communication.

Quote:

Originally Posted by ToXedVirus (Post 1309738)
Some people suggested that C# interoping would be very costly, therefore I created a benchmark where an empty native C++, a native C# and a simple pawn function would be called 100000000 times.

Interop is also about converting types, it gets really slow with strings.

Thraka 10-12-2011 18:13

Re: SharpSource
 
Damnit I got really excited too....

Matthias Vance 10-13-2011 06:25

Re: SharpSource
 
You never know what the future brings. ;)


All times are GMT -4. The time now is 18:23.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.