vfrz / DotNetGraph

Create GraphViz DOT graph with .NET / C#
MIT License
93 stars 20 forks source link

missing System.Drawing.Color assembly #34

Closed FelixReuthlinger closed 1 year ago

FelixReuthlinger commented 1 year ago

Hey, I am using the latest from DotNetGraph from Nuget and compiling into a dotnet v 4.6.2 project. Getting this error:

[Error  : Unity Log] TypeLoadException: Parent class failed to load, due to: Could not load type of field 'DotNetGraph.Attributes.DotColorAttribute:<Color>k__BackingField' (0) due to: Could not resolve type with token 0100002e (from typeref, class/assembly System.Drawing.Color, netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51) assembly:netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 type:System.Drawing.Color member:(null) signature:<none>
...
Stack trace:
DotNetGraph.Compiler.DotCompilerWorker.CompileNode (DotNetGraph.Node.DotNode node, System.Int32 indentationLevel) (at <aa62545a0724477d9ad05e69e648d004>:0)
DotNetGraph.Compiler.DotCompilerWorker.CompileGraph () (at <aa62545a0724477d9ad05e69e648d004>:0)
DotNetGraph.Compiler.DotCompilerWorker.Compile () (at <aa62545a0724477d9ad05e69e648d004>:0)
DotNetGraph.Compiler.DotCompiler.Compile (System.IO.TextWriter writer, System.Boolean indented, System.Boolean formatStrings) (at <aa62545a0724477d9ad05e69e648d004>:0)
DotNetGraph.Compiler.DotCompiler.Compile (System.Boolean indented, System.Boolean formatStrings) (at <aa62545a0724477d9ad05e69e648d004>:0)
DotNetGraph.Extensions.DotGraphExtensions.Compile (DotNetGraph.DotGraph graph, System.Boolean indented, System.Boolean formatStrings) (at <aa62545a0724477d9ad05e69e648d004>:0)

Any clue what I can do about this?

vfrz commented 1 year ago

What OS are you using? If you are not using Windows this might the problem: https://learn.microsoft.com/en-us/dotnet/api/system.drawing?view=net-6.0#remarks You could try to resolve it by installing this nuget package: https://www.nuget.org/packages/System.Drawing.Common/

FelixReuthlinger commented 1 year ago

I am using Win11 latest versions to compile and run the dll. I also tried referring the System.Drawing.Common and also repacked it into the target dll, but that does not change the program to later fail on missing this dependency

FelixReuthlinger commented 1 year ago

Is this maybe due to using .NETFramework v4.6.2 ? I am not a specialist in dotnet, but the page you linked I also found before and it mentions on .net 5 and .net 6, right?

vfrz commented 1 year ago

Hum, not sure it could be because of the framework version, there is no reason .NET 4.6.2 doesn't include the Color type. Could you try to create a console application with the same framework version please? (but outside unity) so we could determine if the problem is coming from Unity.

vfrz commented 1 year ago

I have just tested on a Windows machine and it works fine outside Unity, so the problem is coming from Unity. Have you tried this already? https://stackoverflow.com/a/70217835

FelixReuthlinger commented 1 year ago

Thanks for the answers. What I did now is to copy the System.Drawing.dll into my project and let it, together with Syste.Drawing.Common.dll get repacked to the target .dll - but this still does not change the issue I am facing...

Maybe bit stupid question: why does the DotNetGraph require colors if it will just write text files that I later will import into GraphViz or similar? The text file would not require things like a color, would it? Can I somehow work around this need for the extra libraries to just assemble the text file?

vfrz commented 1 year ago

There is no stupid question! It requires the Color type because I wanted the users to be able to set the various color properties (node/edge color/background...) using a .NET type (converted to hex format in the compiled dot file). But since it can cause problems in some environments I might create my own color type based on the System.Drawing's one.

FelixReuthlinger commented 1 year ago

for what I am trying to do (drawing graphs of in-game objects) colorless would also be fine. Any ideas on when you might have time to do a change that does not require System.Drawing?

vfrz commented 1 year ago

I've just pushed a beta version with my own color type DotColor, it is available here: https://www.nuget.org/packages/DotNetGraph/3.0.0-beta3 This is version 3.0 so it will require some changes on your side, creating graph/node/edge is a bit different and using fluent extension methods. You can check the readme file on master for some examples, it shouldn't be difficult!

FelixReuthlinger commented 1 year ago

Very nice :) Are you using a special StringWriter lib that supports async? I get this error copying the code from README: Type 'System.IO.StringWriter' used in a 'await using' declaration must be implicitly convertible to 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method. Did you mean to use 'using' instead of 'await using'?

FelixReuthlinger commented 1 year ago

btw I am not sure if you really need to use the async writer, do you? calling like this did also work (at least for a small example

var writer = new StringWriter();
directedGraph.CompileAsync(new CompilationContext(writer, new CompilationOptions()));
var result = writer.GetStringBuilder().ToString();
vfrz commented 1 year ago

You don't need the await using on the string writer, this is a new feature of .net that is not in .net framework. But you still need to await the CompileAsync method.

vfrz commented 1 year ago

@FelixReuthlinger Is it all good? Can I close the issue?

FelixReuthlinger commented 1 year ago

yes, sorry, been hiking the last couple of days ;) works, thx a lot :D