parse.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. open CS,">generated/Object.Events.cs" || die;
  2. open CPP,">generated/events.cpp" || die;
  3. print CS "using System;\n";
  4. print CS "using System.Runtime.InteropServices;\n";
  5. print CS "namespace Urho {\n\n";
  6. print CS "\tpublic delegate void ObjectCallbackSignature (IntPtr data, int stringhash, IntPtr variantMap);\n";
  7. print CPP "#define URHO3D_OPENGL\n";
  8. print CPP "#include \"../AllUrho.h\"\n";
  9. print CPP "#include \"../src/glue.h\"\n";
  10. print CPP "extern \"C\" {\n";
  11. print CPP "void urho_unsubscribe (NotificationProxy *proxy);\n";
  12. sub mapType {
  13. my ($pt) = @_;
  14. ($t,$st) = split (/ /, $pt);
  15. if ($st eq "pointer"){
  16. return "$t";
  17. }
  18. if ($st eq "ptr"){
  19. return "$t";
  20. }
  21. return "uint" if $t eq "unsigned";
  22. return "float" if $t eq "Float";
  23. return "IntPtr" if $t eq "User-defined";
  24. return "CollisionData []" if $pt eq "Buffer containing";
  25. return "byte []" if $t eq "Buffer";
  26. return $t;
  27. }
  28. open EVENTS, "events.txt" || die "Need the file events.txt to figure out where to generate events";
  29. while (<EVENTS>){
  30. ($event, $classes) = split;
  31. @events{$event} = $classes;
  32. }
  33. RESTART:
  34. while (<>){
  35. chop;
  36. next if (/#define/);
  37. if (/EVENT\(/){
  38. ($ec,$en) = $_ =~ /EVENT\((\w+), ?(\w+)/;
  39. if ($en ne "DbCursor"){
  40. print CS " public partial struct ${en}EventArgs {\n";
  41. print CS " internal IntPtr handle;\n";
  42. print CPP "DllExport void *urho_subscribe_$en (void *_receiver, HandlerFunctionPtr callback, void *data)\n";
  43. print CPP "{\n";
  44. print CPP "\tUrho3D::Object *receiver = (Urho3D::Object *) _receiver;\n";
  45. print CPP "\tNotificationProxy *proxy = new NotificationProxy (receiver, callback, data, Urho3D::$ec);\n";
  46. print CPP "\treceiver->SubscribeToEvent (receiver, Urho3D::$ec, proxy);\n";
  47. print CPP "\treturn proxy;\n";
  48. print CPP "}\n\n";
  49. while (<>){
  50. chop;
  51. $cast = "";
  52. if (/PARAM/){
  53. ($pc,$pn,$pt) = $_ =~ /PARAM\((\w+), ?(\w+).*\/\/\W*(\w+(\W+\w+)?)/;
  54. $cspt = $plain = &mapType ($pt);
  55. if (/P_KEY.*Key/){
  56. $cspt = "Key";
  57. $cast = "(Key)";
  58. }
  59. $plain =~ s/ .*//;
  60. if ($plain eq "byte"){
  61. $plain = "Buffer";
  62. }
  63. $hashgetters{$pc} = $en;
  64. print CS " public $cspt $pn =>$cast UrhoMap.get_$plain (handle, UrhoHash.$pc);\n";
  65. }
  66. if (/}/){
  67. print CS " }\n\n";
  68. if ($ec eq "E_UNHANDLEDKEY"){
  69. print "GOING TO DO @events{$ec}\n";
  70. }
  71. for $type (split /,/,$events{$ec}){
  72. print CS " public partial class $type {\n";
  73. print CS " ObjectCallbackSignature callback${en};\n";
  74. print CS " [DllImport(\"mono-urho\", CallingConvention=CallingConvention.Cdecl)]\n";
  75. print CS " extern static IntPtr urho_subscribe_$en (IntPtr target, ObjectCallbackSignature act, IntPtr data);\n";
  76. print CS " internal Subscription SubscribeTo$en (Action<${en}EventArgs> handler)\n";
  77. print CS " {\n";
  78. print CS " Action<IntPtr> proxy = (x)=> { var d = new ${en}EventArgs () { handle = x }; handler (d); };\n";
  79. print CS " var s = new Subscription (proxy);\n";
  80. print CS " callback${en} = ObjectCallback;\n";
  81. print CS " s.UnmanagedProxy = urho_subscribe_$en (handle, callback${en}, GCHandle.ToIntPtr (s.gch));\n";
  82. print CS " return s;\n";
  83. print CS " }\n\n";
  84. print CS " static UrhoEventAdapter<${en}EventArgs> eventAdapterFor${en};\n";
  85. print CS " public event Action<${en}EventArgs> On${en}\n";
  86. print CS " {\n";
  87. print CS " add\n";
  88. print CS " {\n";
  89. print CS " if (eventAdapterFor${en} == null)\n";
  90. print CS " eventAdapterFor${en} = new UrhoEventAdapter<${en}EventArgs>();\n";
  91. print CS " eventAdapterFor${en}.AddManagedSubscriber(handle, value, SubscribeTo${en});\n";
  92. print CS " }\n";
  93. print CS " remove { eventAdapterFor${en}.RemoveManagedSubscriber(handle, value); }\n";
  94. print CS " }\n";
  95. print CS " }\n\n";
  96. }
  97. }
  98. next RESTART if (/}/);
  99. }
  100. }
  101. }
  102. }
  103. print CPP "// Hash Getters\n";
  104. print CS "// Hash Getters\n";
  105. print CS "internal class UrhoHash {\n";
  106. foreach $pc (keys %hashgetters){
  107. $en = $hashgetters{$pc};
  108. print CPP "DllExport int urho_hash_get_$pc ()\n{\n";
  109. print CPP "\treturn Urho3D::$en::$pc.Value ();\n}\n\n";
  110. print CS " [DllImport(\"mono-urho\", CallingConvention=CallingConvention.Cdecl)]\n";
  111. print CS " extern static int urho_hash_get_$pc ();\n";
  112. print CS " static int _$pc;\n";
  113. print CS " internal static int $pc { get { if (_$pc == 0){ _$pc = urho_hash_get_$pc (); } return _$pc; }}\n\n";
  114. }
  115. print CPP "}\n";
  116. print CS " }\n}";
  117. close CS;
  118. close CPP;