userlibs.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "std.h"
  2. #include "bbsys.h"
  3. #include "userlibs.h"
  4. #include <windows.h>
  5. static vector<HMODULE> _mods;
  6. struct Str{
  7. char *p;
  8. int size;
  9. };
  10. static Str _strs[256];
  11. static int _nextStr;
  12. static void libNotFound(){
  13. RTEX( "User lib not found" );
  14. }
  15. static void procNotFound(){
  16. RTEX( "User lib function not found" );
  17. }
  18. void _bbLoadLibs( char *p ){
  19. string home;
  20. if( const char *t=getenv( "blitzpath" ) ) home=t;
  21. while( *p ){
  22. HMODULE mod=LoadLibrary( p );
  23. if( !mod && home.size() ){
  24. mod=LoadLibrary( (home+"/userlibs/"+p).c_str() );
  25. }
  26. p+=strlen(p)+1;
  27. if( mod ){
  28. _mods.push_back( mod );
  29. while( *p ){
  30. void *proc=GetProcAddress( mod,p );
  31. p+=strlen(p)+1;
  32. void *ptr=*(void**)p;
  33. p+=4;
  34. *(void**)ptr=proc ? proc : procNotFound;
  35. }
  36. }else{
  37. while( *p ){
  38. p+=strlen(p)+1;
  39. void *ptr=*(void**)p;
  40. p+=4;
  41. *(void**)ptr=libNotFound;
  42. }
  43. }
  44. ++p;
  45. }
  46. }
  47. const char* _bbStrToCStr( BBStr *str ){
  48. Str &t=_strs[_nextStr++ & 255];
  49. int size=str->size();
  50. if( !t.p || t.size<size ){
  51. delete[] t.p;
  52. t.p=new char[size+1];
  53. t.size=size;
  54. }
  55. memcpy( t.p,str->data(),size );
  56. t.p[size]=0;
  57. delete str;
  58. return t.p;
  59. }
  60. BBStr* _bbCStrToStr( const char *str ){
  61. return new BBStr( str );
  62. }
  63. bool userlibs_create(){
  64. return true;
  65. }
  66. void userlibs_destroy(){
  67. for( ;_mods.size();_mods.pop_back() ) FreeLibrary( _mods.back() );
  68. }
  69. void userlibs_link( void(*rtSym)(const char*,void*) ){
  70. rtSym( "_bbLoadLibs",_bbLoadLibs );
  71. rtSym( "_bbStrToCStr",_bbStrToCStr );
  72. rtSym( "_bbCStrToStr",_bbCStrToStr );
  73. }