sarbian/ModuleManager

Wrong version can run if all dlls aren't located in the same folder

Opened this issue · 3 comments

The version election code doesn't work as-is because KSP only instantiates the last type loaded. A simple example:

[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class VersionTester : MonoBehaviour
{
    void Start()
    {
        print(string.Format("Hello, world from {0}", Assembly.GetExecutingAssembly().GetName().Version));
    }
}

If you build three different versions of this, you'll end up with the output of only the last version loaded. Normally this will be the correct version assuming a sane naming scheme. If you name them out of order, you break the versioning.

What makes this subtle is that KSP appears to select the correct version regardless of name if the assemblies have same-name but differing version KSPAssembly attributes. But this only works for same-folder assemblies; otherwise it defaults to the breadth-first last-loaded assembly when resolving types.

A concrete example: let's say some mod distributes an outdated version of MM inside its own subfolder:

GameData/ModuleManager_1_5.dll
GameData/ModuleManager_1_6.dll
GameData/ModDistributingMM/ModuleManager_1_4.dll

In the above example, the wrong really old version will be loaded. The exact same version of any KSPAddon-carrying MonoBehaviours will then be run, once for each dll found. Even if you specify the exact type using reflection and try to instantiate a GameObject from a particular version, Unity will end up resolving to the latest-loaded type matching its name. The only way I could find around this was to put each version of the DLL in its own namespace.

Something may have changed in the loader because it worked when I designed it.
No mod should distributes MM in a folder. But since some people can't read...

I find it odd that we can never seem to simply say "dont do that" with any facet of any KSP mod :P

Just thought you might want to be aware if you weren't already ;) Wouldn't be fun trying to figure out why a user with a newer version of MM is having older version problems