Extracting specular information from shader

I was looking to code up a way(in the form of a node in the material editor) to create, metallic, specular and Roughness maps form a material in cycles so i can export them into unreal. These can be generated with some effort by messing with the node tree to isolate each value but this takes time and is very difficult for complex shaders. From what i understand of Cycles these values don’t really exist in a way parallel to Unreal due to it being a ray-cast rendered but I would like to create a node that can take a shader as input and create a color output by looking at what the shader is doing at the particular spot and using custom code to generate the metallic, specular and roughness values from that. I haven’t looked into how everything works in depth so is this sort of thing possible? if it is, where should i start looking to learn what i need to make such a thing?

You can extract the color and roughness values from glossy shaders with OSL, so there’s no need to dive into the source code.

Closures are passed as a string array with the name of the shaders, the normal vector, color and any attribute the shader requires. You can then output any of these values and bake them.

I’ve at home a simple parser I can share if you need.