Creating Rule Tiles in Code?

Hi!
I am currently trying to make my game moddable through xml and wanted to be able to make custom tiles.
I managed to get simple tiles using textures working, but now I want to make rule tiles aswell.
Anyone know a place where I can read up on how to do this?
I know it is possible, as there are many tileset editors out there that do it.

Something where I only have an imput texture, and the code automagically translates it into a rule tile, so I only need to save the location of the png in xml.

Any help is appreciated

nothing is automagical until you code it, you have to code 1 ruletile first and configure all rules, only after that can you use createinstance() to duplicate that tile and apply the new texture

Yeah I get that, but how do I code a rule Tile? Thats what I am asking haha

This could be quite complicated. The steps should be as follows: