How to use external libraries while developing mod?

Started by dpainhahn, July 23, 2021, 09:28:36 PM

Previous topic - Next topic

dpainhahn

Hi, I'm new in Rimworld modding. I'm going through the Modding tutorial: https://rimworldwiki.com/wiki/Modding_Tutorials
And it's been pretty helpful for learning the basic mod structure as well as getting the workspace set up.

I intend to make a mod that would be making http calls and tried to install Newtonsoft's Json.net library. I am using VS19, so I installed the library via Nuget package manager. My IDE is able to detect the newly added library, but when I build the mod to test basic features, I would get weird reflection errors.

Is there a specific way to add external libraries? Whenever I use a method like "JsonConvert.SerializeObject()" I would get an error like the following.
Code with Error message: https://pastebin.com/8N52apMj

Besides that, are there any thing I should know regarding trying to use external libraries within a Rimworld mod?

Should I be using .NET framework 4.5 or 4.7.2?

Thanks in advance!

RawCode

how rimworld is expected to access your nuget package cache?

dpainhahn

That I'm not sure. When I build the dll file, it puts the newtonsoft dll in the assemblies folder as well. Is that not enough??

RawCode

newtonsoft.dll that you provide with mod have strong reference to other version of mscorlib

System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

start from reading this article:
https://docs.microsoft.com/en-us/dotnet/standard/assembly/resolve-loads

obviously modding wiki do not have tutorials on c# itself.

dpainhahn

Quote from: RawCode on July 23, 2021, 10:50:50 PM
newtonsoft.dll that you provide with mod have strong reference to other version of mscorlib

System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

start from reading this article:
https://docs.microsoft.com/en-us/dotnet/standard/assembly/resolve-loads

obviously modding wiki do not have tutorials on c# itself.

Thanks!
I'll look into it :)
I had experience with visual c# and I usually don't have to care about dlls haha

dpainhahn

#5
Quote from: RawCode on July 23, 2021, 10:50:50 PM
newtonsoft.dll that you provide with mod have strong reference to other version of mscorlib

System.Numerics.BigInteger, System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

start from reading this article:
https://docs.microsoft.com/en-us/dotnet/standard/assembly/resolve-loads

obviously modding wiki do not have tutorials on c# itself.

As per another post: https://ludeon.com/forums/index.php?topic=20987.0
I was able to get the JSON part working by swapping to JSONfx. I guess which .net runtime the library is built isn't automatically resolved. Regarding the assembly resolve loads, I had trouble trying to get it work, since I wasn't familiar with how dlls work.

I noticed I was getting the same issue when even using built-in references like System.Net.Http.
Error message:

Could not resolve type with token 01000011 (from typeref, class/assembly System.Net.Http.HttpClient, System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) assembly:System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:System.Net.Http.HttpClient member:(null) signature:<none>

Like previously mentioned, I suspect that I'm developing with the wrong mscorlib version that Rimworld has been developed on. Any tips on making sure I'm developing on the correct environment?

RawCode

kay, i will ask you question, what version of mscorlib your version of game have?


dpainhahn

Quote from: RawCode on July 24, 2021, 07:10:33 PM
kay, i will ask you question, what version of mscorlib your version of game have?
Rimworld is using mscorlib version: 4.6.57.0
I think by default my VS19 setup uses an mscorlib verison of 4.7.3062.0.
I made vs19 use Rimworld's included mscorlib instead of the one I'm using, but I think I was having the same error.
I ended up using a different library to get the JSON part working.
Someone said assemblies were loaded alphabetically. Do you think that could be why I was having the issue?
Newtonsoft is alphabetically before my mod name (Starts with "R"), but System.* might get loaded after my mod.

RawCode

1) Exact version does not matter, only token and version does.
Game have version 400 with token "b77a5c561934e089" and this is exactly same token, that listed in original error message.
You do not need exactly same version of mscorlib, you need one with same token and version.

2) Game do load files one by one in alphabetical order, this is exactly reason, why libs normally called "000_name" and similar to ensure them loading before assemblies, that have references to said libraries.

3) Rename your json library into something like 000000JSON.dll, this will make game load your library first and references to library won't be invalid.

4) b03f5f7f11d50a3a is different token, ever if version of mscorlib exactly same, due to different token, it won't be recognized.

5) There are workarounds to force runtime to accept references ignoring version and token, read about side effects before using them.