Ludeon Forums

RimWorld => Mods => Help => Topic started by: Eluminate on October 21, 2021, 03:29:50 PM

Title: System.TypeLoadException when using System.Drawing
Post by: Eluminate on October 21, 2021, 03:29:50 PM
Hi all,
I have a question about using external dll's inside my mod (system.drawing in this instance).
The game gives an error when it's trying to call my constructor, saying it can't resolve the type "System.Drawing.Bitmap".
I tested this with a minimal mod, that just builds up a Bitmap and sends a message to the log.

Here is what I have so far (template from the cookiecutter)
Bitmaptest.cs
using System.Drawing;
using Verse;

namespace Bitmaptest
{
    [StaticConstructorOnStartup]
    public static class Bitmaptest
    {
        static Bitmaptest()
        {
            Log.Error("Working");

            var x = new Bitmap(1500, 1500);
        }
    }
}


This is the error that is thrown inside Rimworld as soon as the map is loaded in
Error in static constructor of Bitmaptest.Bitmaptest: System.TypeInitializationException: The type initializer for 'Bitmaptest.Bitmaptest' threw an exception. ---> System.TypeLoadException: Could not resolve type with token 01000013 (from typeref, class/assembly System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
   --- End of inner exception stack trace ---
  at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(intptr)
  at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (System.RuntimeTypeHandle type) [0x0002a] in <eae584ce26bc40229c1b1aa476bfa589>:0
  at Verse.StaticConstructorOnStartupUtility.CallAll () [0x0001a] in <b64badbf3c3d41018b3ca5d3e8c77771>:0
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Verse.Log:Error (string)
Verse.StaticConstructorOnStartupUtility:CallAll ()
Verse.PlayDataLoader/<>c:<DoPlayLoad>b__4_3 ()
Verse.LongEventHandler:ExecuteToExecuteWhenFinished ()
Verse.LongEventHandler:UpdateCurrentAsynchronousEvent ()
Verse.LongEventHandler:LongEventsUpdate (bool&)
Verse.Root:Update ()
Verse.Root_Entry:Update ()


This is my solution, showing the info for System.Drawing
(https://i.imgur.com/dSGRhv1.png)


I have no clue what is causing this error. I tried to copy the dll inside the mod's assembly folder and to copy it inside the rimworld managed folder, but no luck.
It looks like it can't find the System.Drawing library or that there is a mismatch between the found version and the expected one?
Can someone tell me what i'm missing here?
Is it possible to use an external library like System.Drawing?

Thanks you in advance,
Eluminate
Title: Re: System.TypeLoadException when using System.Drawing
Post by: JT on October 24, 2021, 10:32:17 AM
Yep, it's definitely a library mismatch, but RimWorld doesn't even load System.Drawing as far as I can tell.  You'll have to target RimWorld's own API or Unity-based drawing functions instead.  Take a look at CompPowerPlantWind for an example of what's involved in the Unity API for loading textures and drawing.  Dubwise's mods are also pretty good reference, such as his ceiling fan in Dub's Bad Hygiene, as he pulls directly from RimWorld's own drawing functions (keep in mind they're closed source, though, so it's only fair dealing if you learn from them without any copying or modification).

GraphicsDatabase.Get<T>() yields a Verse.Graphic that can then be drawn in various ways with GenDraw or calling Draw on the Graphic itself.
Title: Re: System.TypeLoadException when using System.Drawing
Post by: RawCode on October 27, 2021, 01:30:39 AM
libs can depend on other libs, make sure you have entire chain loaded.