Skip to content

Get MessagePack working in UWP .NET Native apps - #703

Closed
AArnott wants to merge 4 commits into
MessagePack-CSharp:masterfrom
AArnott:uwptests
Closed

AArnott wants to merge 4 commits into
MessagePack-CSharp:masterfrom
AArnott:uwptests

Conversation

@AArnott

@AArnott AArnott commented Dec 4, 2019 •

Copy link
Copy Markdown
Collaborator

This probably also gets it working on Xamarin.iOS as well.

TODO

  • Should this target 2.0? I don't think 1.8 supported this either, and if so, this can wait till 2.1.
  • I replaced several existing #if conditions in code where there were Unity-related conditions. I'm guessing I didn't get them all right. @neuecc can you confirm? Do we need to set REF_EMIT in certain unity builds?
  • Get Azure Pipelines to build the UWP test project and run the tests defined in it.

Fixes #159

With this change MessagePack builds specially for iOS and UWP applications and omits the Ref.Emit code similar to how we omit it for certain Unity platforms.

A UWP test project is added to verify that the most basic scenario works. We may add more/all of our unit tests to run in the UWP test project eventually.
@AArnott
AArnott marked this pull request as ready for review December 6, 2019 19:52

@neuecc neuecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to target as 2.1.

As your concern, removing UNITY_2018_3_OR_NEWER, NET_STANDARD_2_0 is bad because there definition automaticaly defined by Unity but REF_EMIT is not.

By the way, ideally uap's targetplatforms should not be added.

In my comment of #701

StandardResolver.Resolvers uses DynamicObjectResolver.Instance
that create new DynamicAssembly and it uses AssemblyBuilder.DefineDynamicAssembly immediately.
Well, this is different from what I thought and it is not good behavior.

If this is the cause, can I solve it by initializing DynamicAssembly with Lazy?

public sealed class DynamicObjectResolver : IFormatterResolver
{
    internal static readonly Lazy<DynamicAssembly> DynamicAssembly;

    static DynamicObjectResolver()
    {
        DynamicAssembly = new Lazy<DynamicAssembly>(()=> new DynamicAssembly(ModuleName));
    }

    private static class FormatterCache<T>
    {
        static FormatterCache()
        {
            TypeInfo formatterTypeInfo = DynamicObjectTypeBuilder.BuildType(DynamicAssembly.Value, typeof(T), false, false);
        }
}

I think, this will follow framwork standard( RuntimeFeature.IsDynamicCodeSupported etc).

@AArnott AArnott added this to the v2.1 milestone Dec 12, 2019
@AArnott

AArnott commented Dec 12, 2019

Copy link
Copy Markdown
Collaborator Author

As your concern, removing UNITY_2018_3_OR_NEWER, NET_STANDARD_2_0 is bad because there definition automaticaly defined by Unity but REF_EMIT is not.

Are some unity builds supposed to support REF_EMIT? Which symbols does Unity define that we should use instead of REF_EMIT?

ideally uap's targetplatforms should not be added.

It does add a tax that I'd rather avoid, I agree. In some cases like my P/Invoke project it's absolutely mandatory because the UWP store submission process will reject the app if even an unused p/invoke signature for a banned API is in the DLL. So we literally have to remove the method from the binary that UWP consumes. But I guess for dynamic assembly generation, maybe UWP's submission process won't ban code that tries, and if we avoid invoking it at runtime it shouldn't fail either.

@neuecc: do you have a sense for the general approach you want to take across all these platforms that support different features? I'm thinking especially about StandardResolver. Should it do one thing across all platforms (or throw because it can't be implemented fully on a given platform) so that folks know that if StandardResolver works at all, then it's compatible with what they need? Or should StandardResolver morph into whatever shape (i.e. a different collection of sub-resolvers) that are allowed on the platform? If the first option, we could create other resolvers (e.g. UnityCompatibleStandard) to represent other resolver sets.

@neuecc

neuecc commented Dec 15, 2019

Copy link
Copy Markdown
Member

As your concern, removing UNITY_2018_3_OR_NEWER, NET_STANDARD_2_0 is bad because there definition automaticaly defined by Unity but REF_EMIT is not.

Yes, currently Unity has no pre-declared(library author defined) define symbols.
User can define symbols themself, but it is unacceptable to define it for library use.

The following symbols are currently used.

  • UNITY_2018_3_OR_NEWER
  • NET_STANDARD_2_0
  • ENABLE_IL2CPP

In the case of NET_STANDARD_2_0(not NET4_6), System.Reflection.Emit.ILGeneration is not referenced, so if it is not excluded, compilation itself will not pass.
System.Reflection.Emit.ILGeneration.dll cannot be included in unitypackage because NET4_6 is included in mscorlib and causes a conflict.

This is why it has been excluded from being compiled in the first place.

I think the UWP verification process is very stupid, but if there is still a situation that doesn't pass that way, I don't object to putting in a fix that allows the same path as UNITY to pass.
(Aside from that, I think I should make Lazy)

@AArnott AArnott modified the milestones: v2.1, v2.2 Jan 10, 2020
@AArnott

AArnott commented Jan 20, 2020

Copy link
Copy Markdown
Collaborator Author

I think the UWP verification process is very stupid, but if there is still a situation that doesn't pass that way, I don't object to putting in a fix that allows the same path as UNITY to pass.

Are you saying you object to even encoding UWP tests into the repo? Short of that, I don't know how we'll avoid regressions in this area in the future. Verifying unity is done with every PR/CI build (to a small extent at least, and we should get more tests running there to improve that). I feel we should do the same with UWP since we have users that want to use the library on that platform as well.

As for avoiding a specialized iOS or UAP target in the messagepack library, I'd rather avoid that too. I like your suggestion to use RuntimeFeature.IsDynamicCodeCompiled, although it only works on .NET Core 3.0+, so we'd have to do some of our own detection for UAP.

@AArnott

AArnott commented Jan 20, 2020 •

Copy link
Copy Markdown
Collaborator Author

@neuecc Regarding the 3 compiler symbols defined in Unity that you listed above, can you please provide a grid that indicates all the Unity platforms and these symbols and which symbols are defined in which circumstances? Maybe just modify my grid below to make it easy. Do I even have the right list for the vertical dimension in the table? What are the various modes that Unity can be in? Feel free to edit the table in any way. My goal is to understand when each preprocessor symbol is defined so I can make the necessary edits.

Configuration UNITY_2018_3 _OR_NEWER NET_STANDARD_2_0 NET_4_6 ENABLE_IL2CPP Referrence System.Reflection.Emit Runtime Supports Ref.Emit
net46 [x] [x] [x] [x]
netstandard2.0 [x] [x]
IL2CPP(net46) [x] [x] [x] [x]
IL2CPP(std20) [x] [x] [x]

@neuecc

neuecc commented Jan 21, 2020

Copy link
Copy Markdown
Member

@AArnott I've edited your table.

@Noemata

Noemata commented Mar 27, 2020

Copy link
Copy Markdown

Any word on UWP support?

@AArnott

AArnott commented Mar 28, 2020

Copy link
Copy Markdown
Collaborator Author

@Noemata It's still on my TODO list. Just not prioritized particularly high.

@Noemata

Noemata commented Mar 29, 2020

Copy link
Copy Markdown

No worries @AArnott, turns out this was surprisingly easy to sort out myself from the breadcrumbs left behind by others. It was less than an hours worth of work. It's working well, just doing some perf comparisons to a couple other approaches. Protobuf is looking faster so far.

@waitxd

waitxd commented May 6, 2020

Copy link
Copy Markdown
Contributor

Yes, currently Unity has no pre-declared(library author defined) define symbols.
User can define symbols themself, but it is unacceptable to define it for library use.

For Unity 2018.3+ we can define own define constrains in .asmdef file:
https://github.com/neuecc/MessagePack-CSharp/blob/b0aec2528b21ce7bc12a59e2fd0f90dbff3d3085/src/MessagePack.UnityClient/Assets/Scripts/MessagePack/MessagePack.asmdef#L11

We can make separate .asmdef packages, e.g.:
MessagePack.Core with basic functionality (that will work in strict AOT environment), preferably with strong signed binary release to be able to reference it from non-unity projects
MessagePack.ILEmit with resolvers that are using Reflection.Emit functionality and set constrains to !ENABLE_IL2CPP NET_4_6
MessagePack.Unity with Unity's specialized resolvers (Vector3 etc.)

@AArnott AArnott removed this from the v2.2 milestone Aug 3, 2020
@neuecc

neuecc commented Aug 27, 2020

Copy link
Copy Markdown
Member

this code is almostly same as #996, can close?

@AArnott AArnott closed this Sep 2, 2020
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

Successfully merging this pull request may close these issues.

MessagePack broken at runtime when compiled with .NET Native

4 participants