Adam's Lair Forum

game development and casual madness
It is currently 2018/04/25, 07:11

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: 2017/08/28, 04:28 
Junior Member
Junior Member

Joined: 2016/01/09, 22:52
Posts: 35
Role: Hobbyist
I am trying to do some networking with a Duality plugin. I am sending messages with this library https://github.com/RevenantX/LiteNetLib and serializing/deserializing with this one https://github.com/rogeralsing/Wire. When I send a message from one instance to another I get the following error message.

Code:
System.InvalidCastException occurred
  HResult=0x80004002
  Message=[A]DualitySync.Synchronization.SceneChangeInfo cannot be cast to [B]DualitySync.Synchronization.SceneChangeInfo. Type A originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Development\GameDevelopment\Library\DualitySync\Plugins\GamePlugin.core.dll'. Type B originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array.
  Source=Wire
  StackTrace:
   at Wire.Serializer.Deserialize[T](Stream stream)
   at DualitySync.Synchronization.ClientSceneSynchronizer.Process(Stream stream) in C:\Development\GameDevelopment\Library\DualitySync\Source\Code\CorePlugin\Synchronization\ClientSceneSynchronizer.cs:line 26
   at DualitySync.Synchronization.SyncClient.BeforeUpdate() in C:\Development\GameDevelopment\Library\DualitySync\Source\Code\CorePlugin\Synchronization\SyncClient.cs:line 40
   at DualitySync.DualitySyncCorePlugin.OnBeforeUpdate() in C:\Development\GameDevelopment\Library\DualitySync\Source\Code\CorePlugin\CorePlugin.cs:line 41
   at Duality.CorePluginManager.InvokeBeforeUpdate()
   at Duality.DualityApp.Update()
   at Duality.Backend.DefaultOpenTK.NativeWindow.OnUpdateFrame(FrameEventArgs e) in c:\projects\duality\Backend\DefaultOpenTK\Backend\Graphics\NativeWindow.cs:line 232
   at OpenTK.GameWindow.RaiseUpdateFrame(Double elapsed, Double& timestamp)
   at OpenTK.GameWindow.DispatchUpdateAndRenderFrame(Object sender, EventArgs e)
   at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second)
   at Duality.Backend.DefaultOpenTK.NativeWindow.Duality.Backend.INativeWindow.Run() in c:\projects\duality\Backend\DefaultOpenTK\Backend\Graphics\NativeWindow.cs:line 142
   at Duality.Launcher.Program.Main(String[] args)


I am testing by running two instances of Duality with my plugin. The receiving instance crashes whether it is running from Visual Studio, Dualitor, or the Duality Launcher.

I believe this is related to the way .dll's are loaded with Duality? I have changed my project to .NET Standard 1.6, could that be related? Any advise/help would be appreciated.


Top
 Profile  
 
PostPosted: 2017/08/28, 07:34 
Junior Member
Junior Member

Joined: 2016/01/09, 22:52
Posts: 35
Role: Hobbyist
I did not think to try it earlier but I will set up a non-duality test to see if I get the same problems without Duality's .dll loading.


Top
 Profile  
 
PostPosted: 2017/08/28, 09:06 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 859
Location: Italy
Role: Hobbyist
Try with a separate project. From your error it seems that somehow, somewhere, someone loaded the same assembly twice, and it's giving you the error (in .net, if you load the same assembly more than once, the types contained within result to be different) - see the different 'context' in your error message.

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2017/08/28, 10:28 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2042
Location: Germany
Role: Professional
Since .Net does not have any facilities to remove, replace or update a loaded assembly with new code (for good reasons), Duality has to do a workaround when reloading plugins: It will load the updated version of an Assembly in addition to the original one and reload all object instances, resolving them with their updated equivalent type in the process. The old Assembly is still around, it's just not used anymore.

When you use an external serialization library, you might need to consider how they cache type metadata: Say they store reflection data on how to serialize a type, or an id-to-type mapping in some static variable somewhere, this information will still refer to the old, outdated Assembly after a plugin reload in the editor. (Note: I have no idea about the specific library you're using, please double-check if that could be an issue.)

To handle this properly, you would need to purge their cache (like Duality does with its own) when a reload occurs in the editor / when your plugin is unloaded, or disable their caching techniques altogether in editor mode. Both is something they would need to provide in their API - if they don't, consider opening an issue in their repository to investigate potential support. Duality might not the only application with a chance to run into this.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/08/28, 16:27 
Junior Member
Junior Member

Joined: 2016/01/09, 22:52
Posts: 35
Role: Hobbyist
Sire Pi, I have tried copying my project and running the two instances, I receive the same error. Adam, the problem still exists when running from visual studio or even when launching two instances with Duality Launcher, does the plugin reloading sill occur in those contexts? I haven't had time yet but I will set up a plain windows cmd line project to test it out in a "regular" context. Thanks for the input, any other ideas?


Top
 Profile  
 
PostPosted: 2017/08/28, 17:04 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2042
Location: Germany
Role: Professional
zakila wrote:
Adam, the problem still exists when running from visual studio or even when launching two instances with Duality Launcher, does the plugin reloading sill occur in those contexts?

No, only the editor reloads plugins. In the launcher, there is only one instance of each plugin Assembly loaded.

When you take a look at the logfile of the launcher, there should be one log item for every time a new Assembly is loaded. Check for duplicates, especially for occurrences of your game plugin being loaded. If it shows up more than once, the context of that log could be a hint of what's happening.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/08/28, 17:20 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2042
Location: Germany
Role: Professional
It appears that Wire uses lots of Type.GetType resolves, which can cause loading an Assembly. I'm not sure what exactly the behavior behind that is, but it might have something to do with duplicate / accidentally loaded Assemblies, especially since Duality loads its plugins anonymously. Theoretically, that shouldn't be an issue, since Duality also does manual Assembly resolve.. but again, not sure about the exact Type.GetType or additional Wire-specific load behavior. Anyway, a look at the logfile might tell us more.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/08/29, 06:44 
Junior Member
Junior Member

Joined: 2016/01/09, 22:52
Posts: 35
Role: Hobbyist
I looked at the log file. Looks like my GamePlugin, which contains the Type that is being serialized/deserialized, is loaded more than once. The second time it is loaded is during the deserialization. I you might be right about the GetType calls Adam, any thoughts on how to get around this problem? Does Duality have a safe way of getting Type info for its serialization system? I can open up Wire and tinker with it to get it working with Duality, already did that once for PCL. Also I put together a test console app, I did not run into any problems Deserializing.

Code:
[Core] Msg: Running DualityLauncher with flags:
[Core] Msg: Using 'DefaultPluginLoader' to load plugins.
[Core] Msg: Environment Info:
              Current Directory: C:\Development\GameDevelopment\Library\DualitySync - Copy
              Command Line: "C:\Development\GameDevelopment\Library\DualitySync - Copy\DualityLauncher.exe"
              Operating System: Microsoft Windows NT 6.2.9200.0 (Windows 8)
              64 Bit OS: True
              64 Bit Process: True
              CLR Version: 4.0.30319.42000
              Processor Count: 8
[Core] Msg: Currently Loaded Assemblies:
              mscorlib 4.0.0.0
              DualityLauncher 2.2.0.0
              System.Core 4.0.0.0
              System 4.0.0.0
              Duality 2.7.5.0
              System.Runtime 4.0.0.0
              System.IO 4.0.0.0
              System.Reflection 4.0.0.0
              DualityPrimitives 2.0.0.0
              System.Collections 4.0.0.0
              System.Linq 4.0.0.0
              System.Runtime.Extensions 4.0.0.0
              System.Globalization 4.0.0.0
              System.Diagnostics.Debug 4.0.0.0
[Core] Msg: Plugin Base Directories:
              Plugins
[Core] Msg: Available Assembly Paths:
              Plugins\CamView.editor.dll
              Plugins\Compatibility.core.dll
              Plugins\DefaultOpenTKBackend.core.dll
              Plugins\DefaultOpenTKBackend.editor.dll
              Plugins\DotNetFrameworkBackend.core.dll
              Plugins\Duality.dll
              Plugins\DualityPrimitives.dll
              Plugins\EditorBase.editor.dll
              Plugins\FarseerDuality.dll
              Plugins\GamePlugin.core.dll
              Plugins\GamePlugin.editor.dll
              Plugins\HelpAdvisor.editor.dll
              Plugins\LiteNetLibStandard.dll
              Plugins\LogView.editor.dll
              Plugins\MoreLinq.Portable.dll
              Plugins\NVorbis.dll
              Plugins\ObjectInspector.editor.dll
              Plugins\OpenALSoft32.dll
              Plugins\OpenALSoft64.dll
              Plugins\PackageManagerFrontend.editor.dll
              Plugins\ProjectView.editor.dll
              Plugins\QuickGraph.dll
              Plugins\QuickGraph.Graphviz.dll
              Plugins\SceneView.editor.dll
              Plugins\Sockets.Plugin.Abstractions.dll
              Plugins\Sockets.Plugin.dll
              Plugins\System.IO.dll
              Plugins\System.Linq.dll
              Plugins\System.Linq.Expressions.dll
              Plugins\System.Net.Sockets.dll
              Plugins\System.Reflection.dll
              Plugins\System.Reflection.TypeExtensions.dll
              Plugins\System.Runtime.dll
              Plugins\System.Runtime.Extensions.dll
              Plugins\System.Threading.Thread.dll
              Plugins\Wire.dll
[Core] Msg: Assembly loaded: System.Threading 4.0.0.0
[Core] Msg: Scanning for core plugins...
[Core] Msg:   Plugins\Compatibility.core.dll...
[Core] Msg:     Assembly loaded: Compatibility.core 2.0.1.0
[Core] Msg:     Assembly loaded: System.Linq.Expressions 4.0.0.0
[Core] Msg:     Assembly loaded: Anonymously Hosted DynamicMethods Assembly 0.0.0.0
[Core] Msg:   Plugins\DefaultOpenTKBackend.core.dll...
[Core] Msg:     Assembly loaded: DefaultOpenTKBackend.core 2.0.9.0
[Core] Msg:     Assembly loaded: OpenTK 1.1.6.0
[Core] Msg:   Plugins\DotNetFrameworkBackend.core.dll...
[Core] Msg:     Assembly loaded: DotNetFrameworkBackend.core 2.1.0.0
[Core] Msg:   Plugins\GamePlugin.core.dll...
[Core] Msg:     Assembly loaded: GamePlugin.core 1.0.0.0
[Core] Msg:     Assembly loaded: System.Runtime 4.1.0.0
[Core] Msg: Loading auxiliary libraries...
[Core] Msg:   Assembly loaded: Duality 2.2.0.0
[Core] Msg:   Assembly loaded: DualityPrimitives 2.0.0.0
[Core] Msg:   Assembly loaded: FarseerDuality 4.0.3.0
[Core] Msg:   Assembly loaded: LiteNetLibStandard 1.0.0.0
[Core] Msg:   Assembly loaded: MoreLinq.Portable 1.4.18916.0
[Core] Msg:   Assembly loaded: NVorbis 0.7.5.0
[Core] Msg:   Assembly loaded: QuickGraph 3.6.61114.0
[Core] Msg:   Assembly loaded: QuickGraph.Graphviz 3.6.61114.0
[Core] Msg:   Assembly loaded: Sockets.Plugin.Abstractions 2.0.2.0
[Core] Msg:   Assembly loaded: Sockets.Plugin 2.0.2.0
[Core] Msg:   Assembly loaded: System.IO 4.1.0.0
[Core] Msg:   Assembly loaded: System.Linq 4.1.0.0
[Core] Msg:   Assembly loaded: System.Linq.Expressions 4.1.0.0
[Core] Msg:   Assembly loaded: System.Net.Sockets 4.1.0.0
[Core] Msg:   Assembly loaded: System.Reflection 4.1.0.0
[Core] Msg:   Assembly loaded: System.Reflection.TypeExtensions 4.0.0.0
[Core] Msg:   Assembly loaded: System.Runtime.Extensions 4.1.0.0
[Core] Msg:   Assembly loaded: System.Threading.Thread 4.0.0.0
[Core] Msg:   Assembly loaded: Wire 1.0.0.0
[Core] Msg: Initializing ISystemBackend...
[Core] Msg:   .Net Framework...
[Core] Msg: Assembly loaded: System.Xml.ReaderWriter 4.0.0.0
[Core] Msg: Assembly loaded: System.Xml 4.0.0.0
[Core] Msg: Assembly loaded: System.Xml.XDocument 4.0.0.0
[Core] Msg: Assembly loaded: System.Xml.Linq 4.0.0.0
[Core] Msg: Assembly loaded: System.Text.Encoding 4.0.0.0
[Core] Msg: Assembly loaded: System.Text.RegularExpressions 4.0.0.0
[Core] Msg: Assembly loaded: System.Reflection.Extensions 4.0.0.0
[Core] Msg: Initializing IGraphicsBackend...
[Core] Msg:   OpenGL 2.1 (OpenTK)...
[Core] Msg:     Initializing OpenTK...
[Core] Msg:       Platform Backend: PreferNative
                  EnableHighResolution: True
[Core] Msg: Initializing IAudioBackend...
[Core] Msg:   OpenAL (OpenTK)...
[Core] Msg:     OpenAL Drivers installed in C:\WINDOWS\system32\OpenAL32.dll
[Core] Msg:     Available devices:
                  Generic Software on Speakers (Realtek High Definition Audio) (Default)
                  Generic Software on Realtek Digital Output (Realtek High Definition Audio)
[Core] Msg:     Current device: Generic Software
[Core] Msg:     OpenAL Version: 1.1
                Vendor: Creative Labs Inc.
                Renderer: Software
                Effects: True
[Core] Msg:     256 sources available
[Core] Msg: Initializing core plugins...
[Core] Msg:   Compatibility.core...
[Core] Msg:   DefaultOpenTKBackend.core...
[Core] Msg:     Assembly loaded: System.Drawing 4.0.0.0
[Core] Msg:   DotNetFrameworkBackend.core...
[Core] Msg:   GamePlugin.core...
[Core] Msg: DualityApp initialized
            Debug Mode: False
            Command line arguments:
[Core] Msg: Opening Window...
[Core] Msg:   Window Specification:
                Buffers: 2
                Samples: 8
                ColorFormat: 32 (8888)
                AccumFormat: 64 (16161616)
                Depth: 24
                Stencil: 8
                VSync: On
                SwapInterval: 1
[Core] Msg:   OpenGL Version: 4.3.0 - Build 20.19.15.4531
              Vendor: Intel
              Renderer: Intel(R) HD Graphics 4600
              Shader Version: 4.30 - Build 20.19.15.4531
[Core] Msg: Initializing default content...
[Core] Msg:   Assembly loaded: System.Runtime.InteropServices 4.0.0.0
[Core] Msg:   Assembly loaded: System.Collections.Concurrent 4.0.0.0
[Core] Msg:   Assembly loaded: System.Threading.Tasks.Parallel 4.0.0.0
[Core] Msg:   Assembly loaded: NVorbis 0.7.5.0
[Core] Msg:   ...done!
[Core] Msg: Assembly loaded: FarseerDuality 4.1.1.0
[Core] Msg: Loading Resource 'Data\Scene.Scene.res'
[Core] Msg: Assembly loaded: System.Reflection.Emit.ILGeneration 4.0.0.0
[Core] Msg: Assembly loaded: System.Reflection.Primitives 4.0.0.0
[Core] Msg: Assembly loaded: System.Reflection.Emit.Lightweight 4.0.0.0
[Core] Msg: Assembly loaded: System.ObjectModel 4.0.0.0
[Core] Msg: Setting up client server
[Core] Msg: Assembly loaded: System.Net.Primitives 4.0.0.0
[Core] Msg: Assembly loaded: System.Configuration 4.0.0.0
[Core] Msg: Finished setting up client server
[Core] Msg: Assembly loaded: System.Text.Encoding.Extensions 4.0.0.0
[Core] Msg: Assembly loaded: GamePlugin.core 1.0.0.0
[Core] Msg: Assembly loaded: System.Dynamic.Runtime 4.0.0.0
[Core] Msg: Assembly loaded: System.Reflection.Emit 4.0.0.0
[Core] ERR: InvalidCastException: [A]DualitySync.Synchronization.SceneChangeInfo cannot be cast to [B]DualitySync.Synchronization.SceneChangeInfo. Type A originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadFrom' at location 'C:\Development\GameDevelopment\Library\DualitySync - Copy\Plugins\GamePlugin.core.dll'. Type B originates from 'GamePlugin.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' in a byte array.
            CallStack:
               at Wire.Serializer.Deserialize[T](Stream stream)
               at DualitySync.Synchronization.ClientSceneSynchronizer.Process(Stream stream) in C:\Development\GameDevelopment\Library\DualitySync\Source\Code\CorePlugin\Synchronization\ClientSceneSynchronizer.cs:line 26
               at DualitySync.Synchronization.SyncClient.BeforeUpdate() in C:\Development\GameDevelopment\Library\DualitySync\Source\Code\CorePlugin\Synchronization\SyncClient.cs:line 38
               at DualitySync.DualitySyncCorePlugin.OnBeforeUpdate() in C:\Development\GameDevelopment\Library\DualitySync\Source\Code\CorePlugin\CorePlugin.cs:line 42
               at Duality.CorePluginManager.InvokeBeforeUpdate() in c:\projects\duality\Duality\CorePluginManager.cs:line 114
               at Duality.DualityApp.Update() in c:\projects\duality\Duality\DualityApp.cs:line 520
               at Duality.Backend.DefaultOpenTK.NativeWindow.OnUpdateFrame(FrameEventArgs e) in c:\projects\duality\Backend\DefaultOpenTK\Backend\Graphics\NativeWindow.cs:line 232
               at OpenTK.GameWindow.RaiseUpdateFrame(Double elapsed, Double& timestamp) in c:\Stuff\svn\Projects\AdamsLair.OpenTK\Source\OpenTK\GameWindow.cs:line 506
               at OpenTK.GameWindow.DispatchUpdateAndRenderFrame(Object sender, EventArgs e) in c:\Stuff\svn\Projects\AdamsLair.OpenTK\Source\OpenTK\GameWindow.cs:line 469
               at OpenTK.GameWindow.Run(Double updates_per_second, Double frames_per_second) in c:\Stuff\svn\Projects\AdamsLair.OpenTK\Source\OpenTK\GameWindow.cs:line 431
               at Duality.Backend.DefaultOpenTK.NativeWindow.Duality.Backend.INativeWindow.Run() in c:\projects\duality\Backend\DefaultOpenTK\Backend\Graphics\NativeWindow.cs:line 142
               at Duality.Launcher.Program.Main(String[] args) in c:\projects\duality\DualityLauncher\Program.cs:line 76


Top
 Profile  
 
PostPosted: 2017/08/29, 09:58 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2042
Location: Germany
Role: Professional
Instead of using Type.GetType, which may or may not load additional Assemblies, iterate over all already loaded Assemblies, retrieve their Types and check if the Type you're searching for is there. This would be completely independent of Duality.

The way Duality itself resolves types is using ReflectionHelper.ResolveType, which is built around the above approach, combined with a bit of Duality-specific code and serialization fallbacks. Also note that Duality doesn't use the .Net way of representing types as strings, i.e. not the one used by Wire when it relied on Type.GetType. If you're modifying the Wire library, you'll probably prefer the first approach, not the Duality-specific one though.

However, looking at your logfile there's something weird going on: Duality itself is loaded twice, in different versions, and so are various .Net libraries:

Code:
[Core] Msg: Currently Loaded Assemblies:
              mscorlib 4.0.0.0
              DualityLauncher 2.2.0.0
              System.Core 4.0.0.0
              System 4.0.0.0
              Duality 2.7.5.0
              System.Runtime 4.0.0.0
              System.IO 4.0.0.0
              System.Reflection 4.0.0.0
              DualityPrimitives 2.0.0.0
              System.Collections 4.0.0.0
              System.Linq 4.0.0.0
              System.Runtime.Extensions 4.0.0.0
              System.Globalization 4.0.0.0
              System.Diagnostics.Debug 4.0.0.0


Code:
[Core] Msg: Loading auxiliary libraries...
[Core] Msg:   Assembly loaded: Duality 2.2.0.0
[Core] Msg:   Assembly loaded: DualityPrimitives 2.0.0.0
[Core] Msg:   Assembly loaded: FarseerDuality 4.0.3.0
[Core] Msg:   Assembly loaded: LiteNetLibStandard 1.0.0.0
[Core] Msg:   Assembly loaded: MoreLinq.Portable 1.4.18916.0
[Core] Msg:   Assembly loaded: NVorbis 0.7.5.0
[Core] Msg:   Assembly loaded: QuickGraph 3.6.61114.0
[Core] Msg:   Assembly loaded: QuickGraph.Graphviz 3.6.61114.0
[Core] Msg:   Assembly loaded: Sockets.Plugin.Abstractions 2.0.2.0
[Core] Msg:   Assembly loaded: Sockets.Plugin 2.0.2.0
[Core] Msg:   Assembly loaded: System.IO 4.1.0.0
[Core] Msg:   Assembly loaded: System.Linq 4.1.0.0
[Core] Msg:   Assembly loaded: System.Linq.Expressions 4.1.0.0
[Core] Msg:   Assembly loaded: System.Net.Sockets 4.1.0.0
[Core] Msg:   Assembly loaded: System.Reflection 4.1.0.0
[Core] Msg:   Assembly loaded: System.Reflection.TypeExtensions 4.0.0.0
[Core] Msg:   Assembly loaded: System.Runtime.Extensions 4.1.0.0
[Core] Msg:   Assembly loaded: System.Threading.Thread 4.0.0.0
[Core] Msg:   Assembly loaded: Wire 1.0.0.0


You may need to take a look at your plugins directory and check if there are any files in there that shouldn't be there.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/08/29, 10:06 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2042
Location: Germany
Role: Professional
The type resolve problem actually seemed to come up before in a Wire repo issue. Could be a good spot to chime in, so the devs know this isn't just that one developer having an issue similar to this.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group