Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having trouble getting FlatSharp to work in Unity #43

Closed
julienkay opened this issue Jun 10, 2020 · 3 comments
Closed

Having trouble getting FlatSharp to work in Unity #43

julienkay opened this issue Jun 10, 2020 · 3 comments

Comments

@julienkay
Copy link

Hi there, FlatSharp seems to be pretty neat. I've worked with protobuf-net in Unity before and I'm looking for a similar library for Flatbuffers, so I thought I'll check it out.

Support for Unity is mentioned in the readme, so maybe you can help me with this issue I'm having. So far I've imported FlatSharp and FlatSharp.Runtime into my Unity Project including all of the dependencies. That means, I also include System.Memory.dll which creates some issues as we'll see later.

When FlatSharp generates a Serializer at runtime it's throwing a FlatSharpCompilationException. Those are the 3 CompilerErrors that it reports:

(17,54): error CS0433: The type 'Span<T>' exists in both 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
UnityEngine.Debug:LogError(Object)
FlatBufferTestClass:Start() (at Assets/FlatBufferTestClass.cs:21)

(38,105): error CS0433: The type 'Span<T>' exists in both 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
UnityEngine.Debug:LogError(Object)
FlatBufferTestClass:Start() (at Assets/FlatBufferTestClass.cs:21)

(13,59): error CS0535: 'GeneratedSerializer' does not implement interface member 'IGeneratedSerializer<Test>.Write(SpanWriter, Span<byte>, Test, int, SerializationContext)'
UnityEngine.Debug:LogError(Object)
FlatBufferTestClass:Start() (at Assets/FlatBufferTestClass.cs:21)

So Span is already defined in the Unity-provided mscorlib. A google search brought up some information on why that might be the case:
dotnet/runtime#26144
dotnet/corefx#28034

The base issue seems clear, but I'm not sure what to make of that information, so any help would be appreciated.

Btw, I'm on Unity 2019.3.14.f1, .Net Standard 2.0, Visual Studio 2017 (v15.9.12)

@jamescourtney
Copy link
Owner

jamescourtney commented Jun 10, 2020

Hey -- thanks for letting me know about this. My understanding was that Unity supported .NET Standard 2.0, so it should just "work", assuming you are using .fbs files to generate your schema at build time. I can't imagine runtime generation will work with Unity since that's all AOT compiled code, right?

Can you share a sample project with me that exhibits this behavior and I can take a look?

@julienkay
Copy link
Author

Hey James, 👋
thanks for the quick reply.

Yeah, Unity supports .NET Standard 2.0, and in my experience works well with most libraries that target that version. But it seems for whatever reason, they/the Mono version they use, bundle System.Memory, which causes this unfortunate clash with any library that is compiled with System.Memory as a dependency.

Regarding compilation: When using Unity's Mono Scripting backend, code is JIT compiled. Compiling at runtime is probably not the most straight-forward thing in Unity, but certainly possible (at least In-Editor). You're completely right though, generating the classes at build time is probably the sane thing to do in this case. And now after I generated my classes with the FlatSharpCompiler from .fbs schemas, things seem to work fine.

I'm still a bit confused, because FlatSharp.Runtime also has dependencies on System.Memory, but if things work I won't complain. So thanks for the nudge in the right direction. 👍

@jamescourtney
Copy link
Owner

Glad that helped! Please reach out if there's anything else I can do to make FlatSharp a better experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants