Automatically importing UnityEngine in generated MessagePack resolvers - c#

I'm trying to start using MessagePack in my Unity project. I've followed the instructions to generate resolvers using mpc through the provided Unity editor plugin, since my project needs to be AOT.
It works, but if my classes contain members of Unity types e.g. Vector3, the generated code doesn't recognize them unless I manually import UnityEngine in the generated file.
That's fine, but it makes me wonder if I'm missing something or rather, is there a way to tell the generator to add certain imports automatically?

Related

How are protobuf packages used?

I don't understand the first part of the last sentence on packages from google's Python protobuf docs:
The .proto file starts with a package declaration, which helps to prevent naming conflicts between different projects. In Python, packages are normally determined by directory structure, so the package you define in your .proto file will have no effect on the generated code. However, you should still declare one to avoid name collisions in the Protocol Buffers name space as well as in non-Python languages.
It's actually false that "the package you define in your .proto file will have no effect on the generated code" in Python (and presumably therefore in languages which do use namespaces) because the generated ..._pb2.py files contain descriptors which have a package field.
DESCRIPTOR = _descriptor.FileDescriptor(
name='example.proto',
package='example',
syntax='proto3',
...
)
I have checked and you cannot import example into a regular python file after having generated the code. i.e. declaring package example; in your .proto file and then protoc compiling it doesn't automatically make your package available for import in python files in your environment (you must still use the import ..._pb2 to access the classes).
Is anyone able to explain how packages declared in .proto files are used both in Python and say C# as an example of a language which makes explicit use of namespaces?
Quoting from the documentation itself:
In Python, the package directive is ignored, since Python modules are
organized according to their location in the file system.
So what exactly is the use of packages declaration then?
Two things I believe:
To avoid the possibility of naming conflicts in the message objects. If supposedly you have a message which has been declared differently under the same application, then you can consider providing the package specifier at the beginning of the proto file, and then use the package specified when defining fields of your other message types. For example, if Foo is defined under package bar and package baz both; then use bar.Foo and baz.Foo wherever required.
For portability. Package directive is ignored both in Python and Golang. But languages like C++ and C# which create namespaces explicitly, do consider the package specified. So for Foo in the above example, a message type named Foo would be declared both in namespaces of bar and baz. The proto file you created with your Python application can be compiled for your C# application in much the same way. It is to be noted that Protobuf is language agnostic mechanism for serializing and deserializing data.
Having said that, I haven't personally much found the use of the package specifier in my protobuf message with my Python applications.
Ref: https://developers.google.com/protocol-buffers/docs/proto

Importing dll - using add reference doesn't allow me to use the 'using' function in the code

I am using Visual Studio 2017, to create a c# class library (.dll).
I am modding a game and need to import the games .dll files.
I create my project, and add the dll files as a reference, and have checked the local copy box. Once done however I cannot seem to use them?
using System;
using UnityEngine;
What should be happening (as far as I know) is that I should be able to use the classes within the dll files I am 'using', however the second line of code (Unity Engine, a dll I have added as a reference) is greyed out while the system one is not.
I have looked online but all the answers I have found are for C++, and reference a 'linker' file which I do not know how to implement.
directly inherit the class name like this "System.Configuration.ConfigurationManager". If your facing problem by adding namespace.

The imported type `Newtonsoft.Json.Linq.JObject' is defined multiple times

This question is specific to Unity, this question has been asked before but not specifically for Unity.
I have 2 third party packages that have 2 dlls in their respective plugins directory: fastjson.dll and newtonsoft.json.dll
I am getting the following errors:
The imported type Newtonsoft.Json.Linq.JObject' is defined multiple times
The imported typeNewtonsoft.Json.JsonSerializerSettings' is defined multiple times
The reason is obvious, these 2 classes are defined in both dlls. I have tried to delete one of the dlls to fix the problem but since there are other dlls in the package's plugin directories that depend on both of those dlls and so when I run a scene I get a file not found exception.
Short of getting the package creators to do something about it is there anything I can do to fix?
You can bypass the conflict using an extern alias. Here is how to do so.
Create a file mcs.rsp in your Assets folder. Write something of this kind:
-reference:Newtonsoft=Assets/Plugins/newtonsoft.json.dll
Edit the path so it respects your project's architecture. (you can use fastjon.dll if you prefer to)
More about mcs.rsp: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
Then, in the culprit .cs file, add the following:
extern alias Newtonsoft;
using UnityEngine;
using JObject = Newtonsoft::Newtonsoft.Json.Linq.JObject;
// ...
Another solution would be to decompile (recent IDEs like Rider/MVS can do it for you) and rebuild yourself one of the managed DLL and rename the namespace. You may have to modify all scripts using the modified DLL.
Here are some guideline to create DLL for Unity (don't forget to compile with the -sdk:2.0 option, it is not specified in this document but it is required for your dll to work with Unity): https://docs.unity3d.com/Manual/UsingDLL.html
By the way, send a request to the plugin maintainer to warn them about the namespace collision.

ClassInterfaceTypes mixed in one assembly (AutoDual and none)

I have c# project in which I have some classes declared [ClassInterface(ClassInterfaceType.AutoDual)] and other ones [ClassInterface(ClassInterfaceType.None)].
These classes are COM Visible cause I need to import the type library(.tlb) to Delphi.
When I Import the type library to Delphi, if I create the unit without the "Create component wrapper" enabled, the Delphi packages that use the AutoDual types won't recognize them.
However if I create the unit with the wrapper option enabled, the opposite happens, the ClassInterfaceType.None classes don't get recognized properly but passes through the AutoDual ones.
Any idea? Do I need to have these types in different assemblies?
I'm generating the _TLB.pas files on a 32 bit Windows Server 2003 and compiling using FinalBuilder 7.

Extract Attributes from c# source code without Reflection

I want to extract custom attributes from source code from different .CS files. I have list of files .
Scenario is that i have Project A which reads files of Project B. It has to extract all attributes used in Project B.
My question is that how can i do that without using Reflection.
Reflection deals with assemblies but i have source code.
Using Roslyn would be over kill?
what approaches are there?
I had another option to Compile using MSBuild but for large scale projects it would not be feasable due to heavy dependencies. and it seems not a good way to build application and then going for its assemblies
i just want to have some sort of thing that tells me what attribute was on what class and possible a UML diagram of relations of that classes.
Please give your suggestions
You could use NRefactory: https://github.com/icsharpcode/nrefactory (also available as a NuGet package)

Categories

Resources