main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <windows.h>
  2. #include <bfc/platform/types.h>
  3. #include <bfc/platform/guid.h>
  4. #include <bfc/std_mkncc.h>
  5. #include <rpc.h>
  6. #include <stdio.h>
  7. #include <api/service/api_service.h>
  8. #include "../Agave/Component/ifc_wa5component.h"
  9. #include "WbmSvcMgr.h"
  10. /* layout (binary)
  11. 0xdeadbeef - 32 bits
  12. service guid - 128 bits
  13. service fourcc - 32 bits
  14. length of service name - 16bits
  15. service name - see previous
  16. length of test string - 16 bits
  17. test string - see previous
  18. repeat as necessary
  19. */
  20. static uint32_t magic_word = 0xdeadbeefUL;
  21. int Add(HANDLE manifest, GUID service_guid, uint32_t service_type, const char *service_name, const char *service_test_string)
  22. {
  23. DWORD bytesWritten=0;
  24. WriteFile(manifest, &magic_word, sizeof(magic_word), &bytesWritten, NULL);
  25. WriteFile(manifest, &service_guid, sizeof(service_guid), &bytesWritten, NULL);
  26. WriteFile(manifest, &service_type, sizeof(service_type), &bytesWritten, NULL);
  27. uint16_t service_name_length = 0;
  28. if (service_name)
  29. service_name_length = (uint16_t)strlen(service_name);
  30. WriteFile(manifest, &service_name_length, sizeof(service_name_length), &bytesWritten, NULL);
  31. if (service_name_length)
  32. WriteFile(manifest, service_name, service_name_length, &bytesWritten, NULL);
  33. uint16_t service_test_string_length = 0;
  34. if (service_test_string)
  35. service_test_string_length = (uint16_t)strlen(service_test_string);
  36. WriteFile(manifest, &service_test_string_length, sizeof(service_test_string_length), &bytesWritten, NULL);
  37. if (service_test_string_length)
  38. WriteFile(manifest, service_test_string, service_test_string_length, &bytesWritten, NULL);
  39. return 0;
  40. }
  41. static GUID MakeGUID(const char *source)
  42. {
  43. if (!strchr(source, '{') || !strchr(source, '}'))
  44. return INVALID_GUID;
  45. GUID guid;
  46. int Data1, Data2, Data3;
  47. int Data4[8];
  48. // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  49. int n = sscanf( source, " { %08x - %04x - %04x - %02x%02x - %02x%02x%02x%02x%02x%02x } ",
  50. &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1,
  51. Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  52. if (n != 11) return INVALID_GUID;
  53. // Cross assign all the values
  54. guid.Data1 = Data1;
  55. guid.Data2 = Data2;
  56. guid.Data3 = Data3;
  57. guid.Data4[0] = Data4[0];
  58. guid.Data4[1] = Data4[1];
  59. guid.Data4[2] = Data4[2];
  60. guid.Data4[3] = Data4[3];
  61. guid.Data4[4] = Data4[4];
  62. guid.Data4[5] = Data4[5];
  63. guid.Data4[6] = Data4[6];
  64. guid.Data4[7] = Data4[7];
  65. return guid;
  66. }
  67. static ifc_wa5component *LoadWasabiComponent(const char *filename, api_service *svcmgr)
  68. {
  69. ifc_wa5component * comp = 0;
  70. HMODULE wacLib = LoadLibraryA(filename);
  71. if (wacLib)
  72. {
  73. GETCOMPONENT_FUNC f = (GETCOMPONENT_FUNC)GetProcAddress(wacLib, "GetWinamp5SystemComponent");
  74. if (f)
  75. {
  76. comp = f();
  77. if (comp)
  78. {
  79. comp->hModule = wacLib;
  80. comp->RegisterServices(svcmgr);
  81. }
  82. }
  83. }
  84. return comp;
  85. }
  86. void UnloadWasabiComponent(ifc_wa5component *comp, api_service *svcmgr)
  87. {
  88. comp->DeregisterServices(svcmgr);
  89. FreeLibrary(comp->hModule);
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. if (argc < 3)
  94. return 1;
  95. const char *command=argv[1];
  96. const char *filename=argv[2];
  97. if (!_stricmp(command, "create"))
  98. {
  99. const char *guid = argv[3];
  100. const char *type = argv[4];
  101. const char *name = argv[5];
  102. const char *test_string = (argc>6)?argv[5]:NULL;
  103. GUID service_guid = MakeGUID(guid);
  104. FOURCC service_type = MK4CC(type[0], type[1], type[2], type[3]);
  105. HANDLE manifest = CreateFileA(filename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  106. if (manifest != INVALID_HANDLE_VALUE)
  107. {
  108. Add(manifest, service_guid, service_type, name, test_string);
  109. CloseHandle(manifest);
  110. }
  111. }
  112. else if (!_stricmp(command, "add"))
  113. {
  114. const char *guid = argv[3];
  115. const char *type = argv[4];
  116. const char *name = argv[5];
  117. const char *test_string = (argc>6)?argv[5]:NULL;
  118. GUID service_guid = MakeGUID(guid);
  119. FOURCC service_type = MK4CC(type[0], type[1], type[2], type[3]);
  120. HANDLE manifest = CreateFileA(filename, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  121. if (manifest != INVALID_HANDLE_VALUE)
  122. {
  123. SetFilePointer(manifest, 0, 0, FILE_END);
  124. Add(manifest, service_guid, service_type, name, test_string);
  125. CloseHandle(manifest);
  126. }
  127. }
  128. else if (!_stricmp(command, "auto") && argc >= 4)
  129. {
  130. const char *w5s_filename = argv[3];
  131. HANDLE manifest = CreateFileA(filename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  132. if (manifest != INVALID_HANDLE_VALUE)
  133. {
  134. WbmSvcMgr svcmgr(manifest);
  135. ifc_wa5component *comp = LoadWasabiComponent(w5s_filename, &svcmgr);
  136. if (comp)
  137. {
  138. UnloadWasabiComponent(comp, &svcmgr);
  139. }
  140. else
  141. printf("failed to load %s\n", w5s_filename);
  142. CloseHandle(manifest);
  143. }
  144. else
  145. printf("failed to create %s\n", filename);
  146. }
  147. return 0;
  148. }