Important C# Plugin Development Guide

Snake

Moderator
Moderator
Jul 13, 2014
288
171
28
C# Plugin Development
1. Requirements :
  • Microsoft Visual Studio (2013 is used in this guide)
  • Fougerite DLL
  • Extra DLL's : UnityEngine and Assembly-csharp (from a rust legacy server, located in the "Managed" folder)

2. First Steps :

First, we'll need to create a new project of type "Class Library".


Next we will add reference to all the libraries we mentioned before. At the right part of Visual Studio, right click on "References" and click "Add Reference..."



In Visual Studio, you must use .Net Framework 3.5! You can do this by rightclicking on FougeriteTest, then Properties, and setting the target framework to 3.5!
I will add all of them, but for a simple plugin, Fougerite.dll should be enough. That DLL will be used ALWAYS, all plugins require it to work with Fougerite. The next one is UnityEngine.dll, which is used when you work with Vectors and Rotations, and a lot more stuff usefull in Rust. The last one is Assembly-csharp, which contains all the stuff about Rust. That DLL is used in more advanced plugins.


First of all, we'll add a reference to Fougerite on the class by writing "using Fougerite;" to use its types.


Then we will add the main methods of a fougerite module. We make our class inherit from Fougerite.Module, and then we add the main override methods :

Name, Author, Description and Version don't need any explanation. Initialize and DeInitialize will be our focus for now.

In Initialize we will add the code that we want to run when the plugin gets loaded. This is the perfect place to get any file (iniparsers, streamwriters, etc...) and to add your Hooks to Rust.

Hooks are used to execute something on an event. For example, when a player gets killed, or when a player places a structure, when a player writes a command on chat, etc...

3. Example :

The first example will be with the OnCommand hook.This hook gets called everytime someone writes something in chat with a "/" as the first character. For example : "/help" or "/starterkit"

We will add our method "HandleCommand" to the OnCommand hooks on Initialize, and we will delete it on DeInitialize. Notice the difference between them.



Notice that Initialize is += and DeInitialize is -=

On our HandleCommand method, we will take 3 parameters. First one will be the Player that sends the command, second one will be the command string, third one will be an array of strings which stands for whatever comes after the command.

For example : "/spawn battlekit sniper". "spawn" is the command and "battlekit" is the first string on the array and sniper is the second string on the array.


On this example, we check if the command is "test". If so, we send a message to the player.

4. Hooks :

Here we'll cover the basic and most used hooks.


They are quite a lot, and there are more but are almost never used.

Warning : Note that I write Handle + Whatever on all delegates, but this is not necessary. You can call your delegates as you want. However, this is easier to read and understand.




OnBlueprintUse : Called when a Player tries to learn a blueprint. This event will be called both when the Player hasn't learned yet the blueprint and when the Player has already learned the blueprint.

Takes 2 parameters : Fougerite.Player and BPUseEvent

BPUseEvent contains all the information about the event. ItemName (string) property refers to the name of the blueprint item and the Cancel (bool) property cancels the learning of the blueprint when set to true.
The last property is the DataBlock, which contains advanced information about the event which we'll not be using anytime soon.


In this example, we check if the item learned is Explosive Charge. If so, we message the player and cancel the event.


OnChat : Called when a Player writes a message in chat. This event will NOT be called when a player writes a message with a "/" as first character.

Takes 2 parameters : Fougerite.Player and ChatString

ChatString is basically a string with the NewText property. This property will set the content of the entire string in case you give some value to it.


In this example, we check if the message contains the word "noob". If so, we make the message empty, so that way it isn't printed in the chat, and we message the player.

Notice that the ChatString it's the string message with double quotes at the start and the end.


1st : Server.GetServer().Broadcast(chatString)
2nd : Original message




STILL WORKING ON IT
 
Last edited by a moderator:

MasterPeace

Retired Staff
Retired Staff
Feb 2, 2015
269
68
28
44
Poland
Looking good man. I will probably work on it on weekend. I'm wondering if you have more freedom here than in PY. Like some more functions to use?
 
  • Like
Reactions: Snake

Snake

Moderator
Moderator
Jul 13, 2014
288
171
28
Looking good man. I will probably work on it on weekend. I'm wondering if you have more freedom here than in PY. Like some more functions to use?
Well, everything is possible in both languages. I prefer C# that's why I write plugins on it. Back on Magma I used to program in JavaScript, and from there to here it's a really big difference. Never tried Python.
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,066
4,420
113
At your house.
github.com
Py - less code/easy to read/much better cross-platform wise. .................. C# is the main language, which would mean that It does the best performance. Since C#'s point is the performance anyway, It comes with major things like type conversion and casting. It has decorators too,and It is a great language, but the problem is the framework which is pretty much tied to Windows.

Python wins by far in ease of use/syntax; because I think Python has the ideal syntax of a programming language. Forced indentation makes it easier to read. C# wins cause of the performance

Example Python - C# list.

Python:
list = [ ]
C#:
using System.Collections.Generic;

class Blabla
{
    static void main ( )
    {
        List<int> list = new List<int>( );
    }

}
C# list cannot have different types of Lists. What I am saying is that a Python list can contain string/int/float/etc... at a time. C# must have the defined contents. Like above It's an int.


Decorators

Python:
variable = "Hello" # No decorators, python works by the value's given type
Advantage: Easy to do declare, and to work with It
DisAvt: For example you can't always know the type of the variable in a list, unless you use an advanced array import. That would allow you to declare type lists. (Which is available in the default library). So for example you would always have to make sure to use a converter, example str() int() etc...

C#:
string variable = "Hello";
Decorators are there, and if you would like to convert, you will have to cast the object to another one, which can throw an error If It fails.

Adv: Compiler works by the decorator, and speeds up the code
Disav: Decorators are extra code which increases file size but that's not a big deal;sometimes It is annoying for some people, makes the code complicated;Decorator must implement all methods of the wrapped class

Sent from my Samsung Galaxy S4
 
Last edited:

Snake

Moderator
Moderator
Jul 13, 2014
288
171
28
Py - less code/easy to read/much better cross-platform wise. .................. C# is the main language, which would mean that It does the best performance. Since C#'s point is the performance anyway, It comes with major things like type conversion and casting. It has decorators too,and It is a great language, but the problem is the framework which is pretty much tied to Windows.

Python wins by far in ease of use/syntax; because I think Python has the ideal syntax of a programming language. Forced indentation makes it easier to read. C# wins cause of the performance

Example Python - C# list.

Python:
list = [ ]
C#:
using System.Collections.Generic;

class Blabla
{
    static void main ( )
    {
        List<int> list = new List<int>( );
    }

}
C# list cannot have different types of Lists. What I am saying is that a Python list can contain string/int/float/etc... at a time. C# must have the defined contents. Like above It's an int.


Decorators

Python:
variable = "Hello" # No decorators, python works by the value's given type
Advantage: Easy to do declare, and to work with It
DisAvt: For example you can't always know the type of the variable in a list, unless you use an advanced array import. That would allow you to declare type lists. (Which is available in the default library). So for example you would always have to make sure to use a converter, example str() int() etc...

C#:
string variable = "Hello";
Decorators are there, and if you would like to convert, you will have to cast the object to another one, which can throw an error If It fails.

Adv: Compiler works by the decorator, and speeds up the code
Disav: Decorators are extra code which increases file size but that's not a big deal;sometimes It is annoying for some people, makes the code complicated;Decorator must implement all methods of the wrapped class

Sent from my Samsung Galaxy S4
Well, all that stuff on the list code example it's just basic, it's on every class, It could be resumed to :
Python:
list = [ ]
C#:
List<T> list = new List<T>();
And you can use var keyword and let the compiler get the type automatically.
 
  • Like
Reactions: DreTaX

Snake

Moderator
Moderator
Jul 13, 2014
288
171
28
I'll be adding all hooks, an explanation and examples. Here's the full list (common and used hooks) :

 

MasterPeace

Retired Staff
Retired Staff
Feb 2, 2015
269
68
28
44
Poland
@DreTaX - readability is chose of user. When I started to write in PY (just because of fougerite) it wasn't clean for me at all. For me all lines except of opening blocks (functions / instructions) should have ";" at the end. What that mean? That mean that you see where instruction is opening just fastest as possible.

Second: You don't even use TAB in your codes what isn't developer friendly. TABs are to make TABs, not spaces. All your plugins have spaces in web and downloaded version.

Third: No syntax validation. I don't know how is it in your Pycharp (may be wrong name) but in all software what I have on my disc I can't check errors before server run. No matter if it's wrong function name, stupid space somewhere, or no ":" at the end of opening IF.

That's for readability and usage of PY. Before Python I used C++, C#. PHP, JS, HTML, CSS I'm using on regular bases.

From all languages hardest to learn was probably C# because of what you wrote before. But hardest to understand and develop was probably Python, because of those spaces and other stupid errors what was making my job longer and longer.

ANYWAY.

Yea C# can be harder at start. But compilation and usage of Unity preferred language means best performance as possible. Remember that setting type of variable is not for making your job harder, but to make job for PC easier. In the end - fougerite is developed in C# and all functions and classes what you use have set type of variables. and when you send something other you got error.
 
  • Like
Reactions: Snake

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,066
4,420
113
At your house.
github.com
@DreTaX - readability is chose of user. When I started to write in PY (just because of fougerite) it wasn't clean for me at all. For me all lines except of opening blocks (functions / instructions) should have ";" at the end. What that mean? That mean that you see where instruction is opening just fastest as possible.

Second: You don't even use TAB in your codes what isn't developer friendly. TABs are to make TABs, not spaces. All your plugins have spaces in web and downloaded version.

Third: No syntax validation. I don't know how is it in your Pycharp (may be wrong name) but in all software what I have on my disc I can't check errors before server run. No matter if it's wrong function name, stupid space somewhere, or no ":" at the end of opening IF.

That's for readability and usage of PY. Before Python I used C++, C#. PHP, JS, HTML, CSS I'm using on regular bases.

From all languages hardest to learn was probably C# because of what you wrote before. But hardest to understand and develop was probably Python, because of those spaces and other stupid errors what was making my job longer and longer.

ANYWAY.

Yea C# can be harder at start. But compilation and usage of Unity preferred language means best performance as possible. Remember that setting type of variable is not for making your job harder, but to make job for PC easier. In the end - fougerite is developed in C# and all functions and classes what you use have set type of variables. and when you send something other you got error.
One thing that I'm okay with is the readability. But and a C++ or a PHP code infront of me and I will say ugly. I'm totally fine with JS/Java/Py/AHK/C#

1. No. ; is just an ending mark in most of the languages to end a statement. In Py this is also possible. The compiler won't cry about It, but Py's concept is shortening your code in every way. So py compiler checks by () by default.

2. I do you tabs. That's a lie. What you don't really know is that PyCharm has smart tabs. Due to PEP-8 in Py, you use spaces for the code. Tabs are also there but in a format, and PEP-8 takes It as a space. Smart Tabs convert it to tabs in the IDE which makes the code fast. PEP8 provides the basis for the big choices a.k.a line length, tabs vs. spaces for indenting, indent width...


HINT:


3. I don't know what do you mean. If you mean that the Py doesn't show you the places where you miss the parts, PyCharm is there for you. It is really base to use a PyCharm for a good development. Even for fougerite plugins.
 
Last edited:

Snake

Moderator
Moderator
Jul 13, 2014
288
171
28
@DreTaX - readability is chose of user. When I started to write in PY (just because of fougerite) it wasn't clean for me at all. For me all lines except of opening blocks (functions / instructions) should have ";" at the end. What that mean? That mean that you see where instruction is opening just fastest as possible.

Second: You don't even use TAB in your codes what isn't developer friendly. TABs are to make TABs, not spaces. All your plugins have spaces in web and downloaded version.

Third: No syntax validation. I don't know how is it in your Pycharp (may be wrong name) but in all software what I have on my disc I can't check errors before server run. No matter if it's wrong function name, stupid space somewhere, or no ":" at the end of opening IF.

That's for readability and usage of PY. Before Python I used C++, C#. PHP, JS, HTML, CSS I'm using on regular bases.

From all languages hardest to learn was probably C# because of what you wrote before. But hardest to understand and develop was probably Python, because of those spaces and other stupid errors what was making my job longer and longer.

ANYWAY.

Yea C# can be harder at start. But compilation and usage of Unity preferred language means best performance as possible. Remember that setting type of variable is not for making your job harder, but to make job for PC easier. In the end - fougerite is developed in C# and all functions and classes what you use have set type of variables. and when you send something other you got error.
Back in the days when I wrote plugins for Magma in JavaScript I had the same issues. Stupid errors as missing comas, typos, etc... took more to fix than writing the actual plugin.
 

DreTaX

Probably knows the answer...
Administrator
Jun 29, 2014
4,066
4,420
113
At your house.
github.com
Back in the days when I wrote plugins for Magma in JavaScript I had the same issues. Stupid errors as missing comas, typos, etc... took more to fix than writing the actual plugin.
Because you were using Notepad++ to write It. I have to say I never had sort of problems like that, after some years you just get over It. For JS there is Webstorm as an example
 

Snake

Moderator
Moderator
Jul 13, 2014
288
171
28
Because you were using Notepad++ to write It. I have to say I never had sort of problems like that, after some years you just get over It. For JS there is Webstorm as an example
I don't remember well but I think I was using IntellIJ.
 

MasterPeace

Retired Staff
Retired Staff
Feb 2, 2015
269
68
28
44
Poland
@DreTaX

1. I'm just saying that ";" is helping me in readability like "." in end of sentence.

2. Yes, I had that in my mind. Now I at least know that's not your fault. When I was using code examples from you, or when I was using some helping functions from your plugins there was always "What? Error?! Aha.. Spaces......"

3. Yea like I said - maybe you have it in your PyCharm. I have about 3-4 code editors (From basics like Notepad++ to advanced Aptana, Visual) and no one can even make line on red because of no ":" at the end of IF instruction.

PHP code for you:
PHP:
$numeric_variable = 1;
$string_variable = "string";
$array_variable = array("first", 2, "third");
// Comment

// Instruction
if ($numeric_variable == 1) {
  do_Shit_function();
} else {
  // No error if instruction is empty
}

// Negation
if ($numeric_variable != 1) { }

// Bigger than or 1
if ($numeric_variable >= 1) {}

// instruction in other way
if ($numeric_variable == 1):
else:
endif;

if (condition) { bla bla } else if {bla bla}
Python:
numeric_variable = 1
string_variable = "string"
array_variable = ["first", 2, "third"]
# Comment

# Instruction
if numeric_variable == 1:
  self.do_Shit_function()
else:
  #error

# Negation
if numeric_variable != 1:

# Bigger than or equal
if numeric_variable >= 1:

if condition:
  bla bla
elif:
  bla bla
For me with ";", "()", "{}" you adding readability. Also it means that you can do with code what you want - optimize it with no spaces, comments and blocks, or leave it beautiful. It start to matter when you have 100 files from 100 to 1500 lines of code and you need to find yourself there. Almost every editor have syntax checking, some can even check functions from whole projects and PHP documentation to warn you before you run your code and start research :)

But at the end of day - it's up to you bra. For me: Python for Fougerite - and nothing more :)
 
Last edited by a moderator: