Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: smiley on Thu 22/07/2010 15:29:20

Title: AGS Plugin API for C#
Post by: smiley on Thu 22/07/2010 15:29:20
Purely proof-of-concept at the moment. Barely anything besides registering new script functions has been tested.

Website (http://bitbucket.org/tbohnen/agspluginsharp)

The 'AGSPlugin' class contains the callback methods as described here (http://www.adventuregamestudio.co.uk/acplugin.htm).

RegisterScriptFunction example:
Declare a method:

private static int AddNumbers(int a, int b)
{
   return a + b;
}

and a matching delegate:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int AddNumbersDel(int a, int b);

The delegate has to use the UnmanagedFunctionPointer attribute with CallingConvention.Cdecl.

Register the script header in EditorStartup:

public int EditorStartup(IAGSEditor editor)
{
   editor.RegisterScriptHeader("import int AddNumbers(int, int);
");
   return 0;
}

Register the script function in EngineStartup:

private AddNumbersDel del;
public void EngineStartup(IAGSEngine engine)
{
   del = new AddNumbersDel(AddNumbers);
   IntPtr address = Marshal.GetFunctionPointerForDelegate(del);
   engine.RegisterScriptFunction("AddNumbers", address);
}



Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Thu 22/07/2010 15:44:59
AGS plugins in C#?

you are a fucking beautiful man and I offer kisses and snuggles in payment.
Title: Re: AGS Plugin API for C#
Post by: tzachs on Thu 22/07/2010 15:46:24
This is brilliant, I wanted to do the same thing but you beat me to it, and I'm happy you did!  :D

As a c# lover and c++ hater, I thank you!
Title: Re: AGS Plugin API for C#
Post by: GarageGothic on Thu 22/07/2010 16:01:19
Wow, that's pretty damn interesting. Still not sure it would be a good idea for me to use XNA because of the iffy licensing, but at least this makes it possible. Would there be any considerable slowdowns in using the wrapper compared to straight C++?
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Thu 22/07/2010 16:51:12
i dont think the XNA library is used.. its just C#
Title: Re: AGS Plugin API for C#
Post by: GarageGothic on Thu 22/07/2010 17:46:01
No, I meant now that C# can be used by plugins, linking to XNA is suddenly an option - one I had previously discarded while searching for appropriate libraries and engines. (Not sure there would be much to gain by using it as an AGS plugin rather than building the entire game in XNA, but I was impressed with some of the 2D capabilities of the framework - including totally neat realtime lighting).
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Thu 22/07/2010 17:52:03
ahh that makes sense.. I didnt think you were that stupid :P
Title: Re: AGS Plugin API for C#
Post by: Wonkyth on Fri 23/07/2010 13:23:11
YAI!
Title: Re: AGS Plugin API for C#
Post by: smiley on Sat 24/07/2010 09:48:09
New version is up.
-Fixed 'AGS_EditorSaveGame' and wrongly marshalled char pointers.
-Most constants are now enums.
-Most properties are now PascalCased.

Quote from: GarageGothic on Thu 22/07/2010 16:01:19
Would there be any considerable slowdowns in using the wrapper compared to straight C++?
There's a significant overhead involved in calling the AGS API methods.
However, I haven't made any performance tests yet.
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Sat 24/07/2010 15:30:42
this doesn't seem to compile in c# express.

its requests ilasm.exe which i believe is part of the vs tools and so is not part of the express edition.

is there any way to make it compile in express?
Title: Re: AGS Plugin API for C#
Post by: smiley on Sat 24/07/2010 16:20:22
Install the .NET SDK:
32 bit:
http://www.microsoft.com/downloads/details.aspx?FamilyID=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en
64 bit:
http://www.microsoft.com/downloads/details.aspx?familyid=1AEF6FCE-6E06-4B66-AFE4-9AAD3C835D3D&displaylang=en
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Sat 24/07/2010 18:03:20
ok now i can get it to compile but AGS throws an unresolved import error and i can't see my error.

I pretty much copied your example verbatim so it must be something pretty basic.
Title: Re: AGS Plugin API for C#
Post by: smiley on Sat 24/07/2010 18:46:27
Please try the updated example.
The error might be caused by the delegate getting GCed.
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Sat 24/07/2010 18:50:15
nope same problem,

heres my code for clarity:


namespace AGSPluginSharp
{
    using System;
    using System.Runtime.InteropServices;

    public class AGSPlugin
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate int AddNumbersDel(int a, int b);

        #region Constructors

        public AGSPlugin()
        {
        }

        #endregion Constructors

        #region Properties

        #region Public Properties

        public string Name
        {
            get
            {
                return "C# AGS Plugin";
            }
        }

        #endregion Public Properties

        #endregion Properties

        #region Methods

        #region Public Methods

        public void EditorLoadGame(byte[] buffer)
        {
        }

        public void EditorProperties(IntPtr parentHandle)
        {
        }

        public byte[] EditorSaveGame(int maxBufferSize)
        {
            return new byte[0];
        }

        public void EditorShutdown()
        {
        }

        public int EditorStartup(IAGSEditor editor)
        {
            editor.RegisterScriptHeader("import int AddNumbers(int, int);\r\n");
            return 0;
        }

        public int EngineDebugHook(string scriptName, int lineNum, int reserved)
        {
            return 0;
        }

        public void EngineInitGfx(string driverID, System.IntPtr data)
        {
        }

        public int EngineOnEvent(EventType evnt, int data)
        {
            return 0;
        }

        public void EngineShutdown()
        {
        }

        private AddNumbersDel del;
        public void EngineStartup(IAGSEngine engine)
        {
            del = new AddNumbersDel(AddNumbers);
            IntPtr address = Marshal.GetFunctionPointerForDelegate(del);
            engine.RegisterScriptFunction("AddNumbers", address);
        }

        private static int AddNumbers(int a, int b)
        {
            return a + b;
        }

        #endregion Public Methods

        #endregion Methods






    }
}
Title: Re: AGS Plugin API for C#
Post by: smiley on Tue 27/07/2010 09:56:36
Do you have the VC++ 2010 redist installed?
http://www.microsoft.com/downloads/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84&displaylang=en

Also copy PluginAPIWrapper.dll to the game's "_Debug" folder, or include the AGS editor folder in PATH.
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Tue 27/07/2010 12:28:19
yep i have the redist package and i'd already copied the wrapper over.
Title: Re: AGS Plugin API for C#
Post by: smiley on Sat 31/07/2010 13:44:50
I've uploaded a new version which might fix the problem.
Title: Re: AGS Plugin API for C#
Post by: Monsieur OUXX on Tue 03/08/2010 15:32:38
Calin, did that eventually work?
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Tue 03/08/2010 15:33:37
I havent got around to testing it yet.

The plugin i'm working on is coded in C++ since i need it to be fast.
Title: Re: AGS Plugin API for C#
Post by: Monsieur OUXX on Wed 04/08/2010 12:25:31
Quote from: Calin Leafshade on Tue 03/08/2010 15:33:37
i need it to be fast.

Show off  ;D
Title: Re: AGS Plugin API for C#
Post by: Pumaman on Tue 10/08/2010 17:30:46
Interesting!

Of course, you need to bear in mind that if you make a game that uses a C# plugin, then the player will need to have the .NET Framework installed in order to play your game.
Title: Re: AGS Plugin API for C#
Post by: Wonkyth on Wed 11/08/2010 04:19:51
Not much of an issue for windows users.
Title: Re: AGS Plugin API for C#
Post by: tzachs on Thu 12/08/2010 19:47:59
Quote from: Monsieur OUXX on Tue 03/08/2010 15:32:38
Calin, did that eventually work?

Works super for me (eventually)   :D
Expect a small demonstration plugin soon...
Title: Re: AGS Plugin API for C#
Post by: Yuri Patrick on Tue 24/08/2010 11:41:50
Alright, back in the original post for this topic is the following:

"Register the script function in EngineStartup:

private AddNumbersDel del;
public void EngineStartup(IAGSEngine engine)
{
   del = new AddNumbersDel(AddNumbers);
  IntPtr address = Marshal.GetFunctionPointerForDelegate(del);
  engine.RegisterScriptFunction("AddNumbers", address);
}



Where in the world is IAGSEngine?! I have AGS Types but IAGSEngine is not in there. Is my AGS broken?
Title: Re: AGS Plugin API for C#
Post by: Calin Leafshade on Tue 24/08/2010 13:06:42
i'm not sure what you mean...

in the code you posted 'engine' is an instance of the IAGSEngine class which is passed from the wrapper.
Title: Re: AGS Plugin API for C#
Post by: Yuri Patrick on Tue 24/08/2010 18:38:28
Yes. I know that, but it is not accepting it. I have VS C# 2008 and am "using AGS.Types;" but when I try to compile with IAGSEngine anywhere in my code, it crashes and says I am missing a directive.
Title: Re: AGS Plugin API for C#
Post by: tzachs on Tue 24/08/2010 18:45:28
Did you use the given template?
It's under Wrapper\Swig\IAGSEngine.cs
Title: Re: AGS Plugin API for C#
Post by: smiley on Thu 26/08/2010 01:52:55
Small update:
The C++ glue code is included in the zip file.
PluginAPIWrapper.dll is compiled with MinGW, so you don't need the VC++ 2010 redist anymore.
Title: Re: AGS Plugin API for C#
Post by: goodohman on Thu 18/11/2010 19:09:12

Hi there,

I'm trying to return a String from a function.

First I just tried to return a C# string - kaboom... it crashed
Then I thought wait.. return a CreateScriptString(mystring) - kaboom...
Then I thought wait... the IAGSEngine and agspluginPINVOKE might be wrong with the return type,
so I've changed them from "string" to "IntPtr" and still - kaboom...
I thought maybe "IntPtr" is not "real" enough as a const char*, so I've changed all to "void*" (in usafe context),
and guess what? yeah... - kaboom

so.. smiley or anybody else.. PLEAAAASSSEEEE does someone knows how to return a string from c# ?

Thanks!!!!!!!!!!!!!!
Title: Re: AGS Plugin API for C#
Post by: tzachs on Thu 18/11/2010 22:03:36
You can take a look at the code I wrote for c# runner (the source code is in that thread), there are some functions there that return a string.
Title: Re: AGS Plugin API for C#
Post by: goodohman on Fri 19/11/2010 02:03:30

Thanks Tzachs!

The C# Runner link appears to be dead, could you repost?
Alternatively, could you give me the short version answer?

Thanks!!

p.s.
any idea on how to access the new ags features from the plugin? (i.e. new audio stuff and attributes)
Title: Re: AGS Plugin API for C#
Post by: tzachs on Sat 20/11/2010 15:22:01
Thanks for the heads up, I re-uploaded it to here (http://shutupload.com/dl/ca89554270b7/).

As for the short version answer, I don't have something special to offer here, cause I don't remember doing anything different for strings that I didn't do for the other types, if was pretty straight forward.
You're probably missing something simple, so I would compare my code to yours and see what's different...

Also, regarding the new audio, my guess is that the API doesn't have that functionality, so no way to access it. However since the source code of the editor is available (and in c#), you can possibly modify it to access the audio somehow, though I'm not sure how much work it will take.
Title: Re: AGS Plugin API for C#
Post by: goodohman on Wed 01/12/2010 22:58:52

tzachs, sorry for the long delay!

Your example is working alright, but the return type is actually derived from using the:

private static TOut CSRun<TIn, TOut>(TIn value, string className)

method (CSRunStringToString...)

Could you PLEASE verify a pure in-code string return type and let me know?

BIG THANKS!
Title: Re: AGS Plugin API for C#
Post by: tzachs on Thu 02/12/2010 22:39:57
The CSRun is only a generic method to run delegates, it's an inner implementation and have no effect of the API, I could have easily replaced that call with a simple:
return "hello world";

This would have worked all the same (guaranteed, this was the first thing I tried when I started playing with smiley's api).
Title: Re: AGS Plugin API for C#
Post by: Yuri Patrick on Mon 26/08/2013 20:52:03
Does anyone still have the C# wrapper? I tried all three of the links that I saw going to the file and they are all dead links. Please someone still have this. :)
Title: Re: AGS Plugin API for C#
Post by: Khris on Tue 27/08/2013 12:33:37
Just send smiley a PM (http://www.adventuregamestudio.co.uk/forums/index.php?action=pm;sa=send;u=6032).
He was last active in July, he's still around it seems.
Title: Re: AGS Plugin API for C#
Post by: smiley on Sat 28/09/2013 10:55:42
Link updated.