Skip to content

C# Programming

Sheep-y edited this page Apr 4, 2020 · 7 revisions

Assuming you are interested in being a Coder type modder, most Unity games' dll mods are coded in C#. C# is a programming language, like English but with very simple grammar and fixed vocabulary, that we use to tell computers what to do.

Here I will try to list the steps you may take to learn C#, in order to read game code and to write dll mods. You will need a computer, preferably Windows.

Table of Contents

Get Started

First, head over to C#'s owner, Microsoft, to get your feet wet:

https://docs.microsoft.com/en-us/dotnet/csharp/

Follow the "Get Started" section until you completed "Create C# apps with Visual Studio", because Visual Studio is the easiest way to write and build mods.

Now you may look at some tiny mods, and perhaps load their source code into Visual Studio and try to compile them. A project type of "Class Library" will compile the code into dll, saved in the bin folder. All mods depends on game dlls, called References, which you can find in the game's Managed folder.

You may check out other official tutorials if you wish to, but keep in mind that .Net Core is different from .Net Framework. Features like C# 8.0 will not work when we targets .Net Framework, like when we make mods. If that make your head spin, I suggest you to move on.

Language Features

After Hello World, you can switch to tutorial point for a crash course on language features:

https://www.tutorialspoint.com/csharp/index.htm

Language features are like prepositions, and is very important for reading real code. At least "finish" the Advanced Tutorial, because you will see those features in game code and in many mods.

You also need to "finish" Linq, which tutorial point does not teach:

https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/working-with-linq

I quoted finish, because it is ok to not master all the new concepts. The important part is have an idea of them and know where to look, what terms to google, when you are lost.

Also check out the "Key Concepts" at the end of this page, which you'll need to know for making real mods.

Practice

After you learned short and long, ?: and ?., interface and class and structs, fields and properties, delegate and event, reflection and collection, and that ()=> is not an ice-cream scope, you may want to practice your .Net skill a bit, to consolidate the learnings.

https://www.w3resource.com/csharp-exercises/

Doing exercise is a tried and proven way to master programming, ever since computer was invented.

Decompiler

At any point, you can read game code to see how much you understand. To see game code, you need a .Net decompiler. There are a few, but I prefer dnSpy.

https://github.com/0xd4d/dnSpy

After you downloaded it, launch it and open "Assembly-CSharp.dll" in the game's PhoenixPointWin64_Data\Managed folder.

Press Ctrl+Shift+K to start searching the code. For example if you search "Hearing" you can find code memebers whose name contains "Hearing", and are likely used by the perception subsystem of tactical combat.

Select any member and press Ctrl+Shift+R to find who use it or overrides it. This is the primary way to walk "up and down" the code to learn its logic.

If the code looks like Pandoranish to you, re-learn C# and do more exercises.

Harmony

When you can understand how a part of the game works, you can start thinking how to modify it.

Most mods modify game logic is with the Harmony patcher.

https://github.com/pardeike/Harmony/wiki

It can adds *your* methods as "Prefix" to run before other (game) methods, and/or as "Postfix" to run after another methods, including property setter and getter (but not fields), to change its input/output or even totally overriding it. (Leave transpiler are for masters. Like when you think you've transcended human and claim to be an AI sheep.)

In the beginning, it is easier to copy than to create, so the easiest way to start is to read existing mods and modify them. There is no shame in that. Mods are open sourced for a reason.

Happy Modding!

Key Concepts

Assuming you've installed visual studio and built your Hello World, here are some .Net programming concepts, that few C# tutorials would explain.

(In alphabetical order)

Assembly
Compiled .Net code, usually a EXE or DLL. Each assembly is its own little country, a little world.
Example: Each mod is an Assembly. They can have same named code without mixing up, like many countries have a Newcastle.
Once an Assembly is loaded into a program, it cannot be unloaded or reloaded until the program exits.
CLR, Common Language Runtime
The name for all "environment" that runs .Net code, such as .Net Framework, .Net Core, Mono, etc.
DLL
A Dynamic Linked Library, with the .dll extension. Users cannot run it, but other Assemblies can refer to the code in it by Referencing it.
Example: The modnix loader is a dll. When user runs a modded game, the game loads it and calls it, which in turn loads and calls the dll mods.
EXE
An EXEcutable file, with the .exe extension. Users can double click it and run it.
Example: The modnix injector is a exe. When user run it, it modifies the game.
To see extensions, open Window Explorer, click View tab > Options > (Folder Options popup) View > Uncheck Hide extensions for known file types.
Mono
An open source version of .Net Framework that can run on Linux.
.Net
Microsoft's attempt to ride the waves of the interNET and eat Delphi and Java for breakfast.
Generally refer to .Net Framework, but can also encompass .Net Core and Mono, and related technologies such as decompilers.
.Net Core
A new cross-platform CLR intended to replace .Net Framework.
Not installed by default. Apps need to bundle their own Core, which also happens if you use Core packages in a Framework project.
.Net Framework
The "default" CLR installed on every Windows, partly because of its long history.
Has Framework-only features like WPF, used by Modnix, not suppored anywhere else.
NuGet
Microsoft's official library manager that let you download and use .Net libraries easily and without mess.
For example, you can add a Lib.Harmony dependency with a few clicks, no need to download or manage reference.
Package
Have many meanings, but here I'll say it means a NuGet Package, a library you can get from NuGet. Example: Harmony.
Project
A set of code and resources to build an Assembly. Also specify which CLR the code "targets", i.e. depends on.
Reference
A Project may reference a library to learn and call its code. Right-click "References" in a Project in Visual Studio to add them.
Example: Add a reference to the game's Assembly-CSharp.dll allows you to call the game's code.
Solution
A set of related Projects in Visual Studio. For example the Modnix solution contains the Injector, Loader, Manager projects.

Clone this wiki locally