xpnteam / xpnet

Develop X-Plane plugins in .NET.
MIT License
50 stars 15 forks source link

Can not get code to work with X-11 #1

Closed sognatore99 closed 6 years ago

sognatore99 commented 6 years ago

I am attempting to use your code to interface with Xplane X-11. I have compiled it using Core 2.0 and started with the Log example. When attempting to run, Xplane starts then exits without an error at the time it is loading the log example. As I do not know C++ very well I used the XPLMDebug statements to figure out that it was failing at ClrPluginStart. I attempted to put a try/catch around but it exited without hitting. Any helps or pointers would be appreciated (otherwise I will have to learn Lua :)

jaurenq commented 6 years ago

I had either the same, or a similar, problem back in December and it turned out to be that I did not have all of the necessary DLLs in the plugin folder, next to the logger plugin DLL. In particular, the required DLLs from the nuget dependencies. I am not near a computer where I can check right now; I'll check when I can get home and give you an example of the names. If you do a Release build from Visual Studio, the other .dlls should be in the build output folder, and likely have names like System.dll and Microsoft.dll, and I think Newtonsoft.Json.dll is in there too. Medium-term, I'd like to work up a packager build step that will pull all the necessary dependencies together to create the correct folder tree for dropping into X-Plane, with all the dependencies. Side note: .NET Core's behavior when there are missing dependency DLLs is somewhat infuriating; I spent some time trying to find how to correctly handle and report that fact in the log instead of just crashing, but Core seems to want to simply fail with no useful information. Hopefully they'll either improve that in the future or we'll find the correct way to find that info, so we can log "Hey, you're missing System.BlahBlah.dll" instead of failing silently.

jaurenq commented 6 years ago

I didn't do any work on xpnet last month, and as it turns out the only uncommitted change I've got is this comment that I had forgotten I'd written, right above the call to ClrPluginStart.

// TODO: If the exception calling ClrPluginStart turns out to be missing dependencies, note
// that the try/catch I added here did not do the trick.  Try wrapping in __try/__catch
// instead, to catch NT exceptions.  If that works, then we can log a good message in the
// X-Plane log pointing in the direction of something wrong with the assembly, like I tried
// to do with C++ exceptions below.

Spoiler: the problem I was hitting was missing dependencies. My intent was/is to recreate the problem and do some more poking around to try to find a way to handle and properly report the error, instead of crash. (It seems kind of crazy that trying to load a library using .NET Core with missing dependencies results in a hard crash instead of an error code of some kind).

To resolve the problem, I had to copy the Microsoft.Extensions.*.dll files and the Newtonsoft.Json.dll file into the plugin directory alongside XPNet.LoggerPlugin.dll. My comment earlier about those being in the output directory of the XPNet.LoggerPlugin project was outdated; that's not how VS 2017 does things for .NET Core. Instead, look in XPNet\x64\Release after building the solution. Everything should end up there when you build, and you can pick it up from there and put it into your plugin directory at XPlane11\Resources\plugins\YourPluginName\64.

Please let me know if that helps.

sognatore99 commented 6 years ago

Thank you very much for the quick reply. When I get back to work tomorrow I will make sure all the dll's are there. BTW excellent work on the plugin, this will save me a lot of headache by not having to learn Lua!

sognatore99 commented 6 years ago

I still can not get it to work. When I compile the dll's where not in the directory and I searched for a solution and found the following:

https://stackoverflow.com/questions/44763082/dependencies-not-copied-to-bin-folder

Once I put the

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
in to the project settings all the dlls where now copied into the bin. However, the program still hard crashes at the same location. I have attached some screen shots of how my system is configured. I was worried about the core 2.0 setting and the fact that when I type in dotnet.exe --version it shows 2.1.4 (which is the sdk that I installed, I believe the binaries are 2.05). I found older versions and tried those in the dotnet folder but still no luck. I have not used netcore before so I am a little behind the power curve as I mainly do framework. Anyway if there is any thing here that might look wrong I hope you can help. I am also running on Windows 10 and targeted the 10.0.16299.0 sdk. I would think that those should not be an issue. Thanks again for your time.

directory dotnetpropertypage propertypage

jaurenq commented 6 years ago

I looked into this some more last night. Notes:

  1. I figured out how to handle the problem of a missing dependency in a way that lets me log a good error message instead of crashing. I have checked that change in.

  2. The way I had set up to get the dependencies into place for running the test harness is incorrect and needs fixed still, but that wouldn't help with deploying into X-Plane anyway so it's probably not what you're seeing. I think my next goal on this project had better be to get a Package target in place.

Your plugin folder looks the same as mine, including the exact same set of DLLs. The things I know to try:

  1. Right click on those DLLs and see what version they are. They should be version 2.*. If they are version 1.*, then they won't work (but with the latest build, you should get a good error message about it now).

  2. Make sure that your dotnet folder contains 'host' and 'shared' subfolders. If it contains something else, you might have extracted the zip to the wrong location.

  3. Build with the latest from git, make sure the new XPNet.CLR.dll gets copied into your plugins folder, and try running with it. I wouldn't expect it to fix the problem, but hopefully now you'll have an experience like the following:

To test the change, I intentionally deleted Microsoft.Extensions.Configuration.dll from my plugins folder and tried starting X-Plane.

In <X-Plane Root>\Log.txt, I got:
XPNet: Loading plugins from D:\Games\X-Plane\X-Plane 11\Resources\plugins\XPNetDev\64
XPNet: ClrPluginStart Result = 0.
Failed: D:\Games/X-Plane/X-Plane 11/Resources/plugins/XPNetDev/64/win.xpl. (The plugin refused to start by returning 0 from XPluginStart.)

That's as expected. And in xpnet.log in the plugin folder, I got:

XPNet CLR: Start
----------
Exception: FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Stack: 
   at XPNet.PluginBridge.GetConfig(String configFilePath)
   at XPNet.PluginBridge.StartInternal(StartParams& startParams, ApiFunctionPointers& apiFunctionPointers, String rootDir)
   at XPNet.PluginBridge.Start(StartParams& startParams, ApiFunctionPointers& apiFunctionPointers)
----------

Better than a crash! I also tried downgrading one of the DLLs to the wrong version, and I got a different error about a manifest version match on that DLL.

sognatore99 commented 6 years ago

Ok I will take a look at it sometime today. Thank you for your effort. I hope this points me to the problem.

sognatore99 commented 6 years ago

SUCCESS! Thank you for your help. I really do not know what I did different this time. There was one error in that I did not have the config file but I know in yesterday's test I had it there and just forgot this time. I tried both release and debug and they both work. Thank you for your time and software.