vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
482 stars 77 forks source link

Use generated C# files from CodeGen #225

Closed windowslucker1121 closed 8 months ago

windowslucker1121 commented 8 months ago

Hey folks, i cant seem to get my generated files working with SharpGLTF and i am taking the last straw now and searching help here... :-)

What i have done so far:

  1. Writing a PoC custom extension to handle all my data
  2. Extending SharpGLTF to copy over all schema files
  3. Generating C# files (Generated/ext.xxxx.g.cs) with CodeGen for this custom schema
  4. Now i am clueless :D

i tried registering it in the CodeExtensionFactory like in this file https://github.com/vpenades/SharpGLTF/blob/5a33d5452528d827ac55727f302d8a91a75c4186/src/SharpGLTF.Ext.Agi/Schema2/AgiExtensions.cs#L19 but then i need to call the function explicitly at startup in my other program(?) (so i cant use it in my IDE) - furthermore i dont want to mess with the base sharpGLTF repo to add it to the "base ExtensionsFactory".

and i tried using it directly, like gltfRoot.UseExtension<vmde_machine>() but the generated files are internal/private so i cant use them in another project.

Do i need to write seperate classes to access the gernerated files and handle all the information, so whats the sense of the CodeGen project then? (no offense, just curious!) And what is the ExtensionFactory explicitly used for? Whats its purpose?

How do i proceed here? How i can "register" it to SharpGLTF to be able to use for example code completion for this schema and use it for gltf models that have this extension in their file as "extensionsUsed"?

Maybe a lightweight documentation on what files do and how to use them would be helpful for the following ones - because im already messing around with this a lot of time (just to find out how to use it :-P )

I would appreciate any guidance on how to go on in my specific case and thanks in advance! :-)

vpenades commented 8 months ago

So far, there is no official way of writing an extension, as everything is still in the works, and to be clear, there's some stuff I am trying to figure out which is the right approach.

Having said that, the whole process of adding a custom extension is almost done, so to clarify some of the questions:

https://github.com/vpenades/SharpGLTF/blob/5a33d5452528d827ac55727f302d8a91a75c4186/src/SharpGLTF.Ext.Agi/Schema2/AgiExtensions.cs#L19-L29

Notice that you have to pass every extension type object, AND the parent object to which it is attached.

So if your extension called "NodeCustomDataExtension" is attached to nodes, then you registed it as <Node,NodeCustomDataExtension> and finally you pass a lambda that's used to construct the object, receiving the parent object as the only parameter. This is required to prevent issues in AOT scenarios.

So if you want to use Agi extensions, you call AgiExtensions.RegisterExtensions at the beginning of your application. Afterwards, the library will recognize the extension.

windowslucker1121 commented 8 months ago

So to double check my assumption: The CodeGen Tool only produces files to De-/Serialize my schema files - it is on me to write and maintain code (eg. creating classes which are mapping onto my schema) that i can "work" with them in combination with sharpGLTF in my IDE (eg. code completion)?

Because currently the CodeGen Tool generates only these "schema2/Generated/ext.xxxx.g.cs" files and i can see in other extension classes there are more .cs files in their respective schema2 root directory.

Thanks for your detailed and fast answer, awesome!

vpenades commented 8 months ago

Well, strictly speaking, the CodeGen tool is only needed if you have your extension defined as json schema files (which is usually the case)

If you're not writing your own schema json files, and you're going to write your extension directly in C#, then you can forget avout the CodeGen tool, and write the de-/serialization code by hand

windowslucker1121 commented 8 months ago

Thanks again for your fast answer!

I am indeed defining my schema in a json schema file - but to give an example:

You wrote that i can register my extension here:

https://github.com/vpenades/SharpGLTF/blob/5a33d5452528d827ac55727f302d8a91a75c4186/src/SharpGLTF.Ext.Agi/Schema2/AgiExtensions.cs#L19-L29

and now looking in your code -

https://github.com/vpenades/SharpGLTF/blob/5a33d5452528d827ac55727f302d8a91a75c4186/src/SharpGLTF.Ext.Agi/Schema2/AgiExtensions.cs#L25

your are creating a new instance of new AgiRootArticulations(p) which referenced class is NOT(?) generated by the CodeGen Tool (because the class/file is not in the generated folder?)

Currently my generated files are looking like this: image

Now to ease out the question, do i need to do either:

Edit: i just want my schema defined values (strings, integers, enums) to be exposed in the IDE that i can easily work with them.

vpenades commented 8 months ago

Not exactly: notice the classes are decorated with PARTIAL modifier

A partial class can have its body distributed across multiple files.

So the generated files contain the serialization code

And the files written by you contain the constructor, properties and functions you may require for your public API

In other words:

// SomeExt.g.cs  <- generated by CodeGen

partial class SomeExt
{
  // fields and serialization code
}
// SomeExt.cs  <- manually written by you

partial class SomeExt
{
  // constructor, properties and methods
}
windowslucker1121 commented 8 months ago

That was the answer i was looking for, thanks for your fast and handy response!