Advertisement
Vearie

BZCC/BZ2 DLL Code

Dec 17th, 2022
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.13 KB | Source Code | 0 0
  1. // This is a simple functioning DLL without any of the mission exports
  2. // that can be compiled with GCC with the command:
  3. // gcc.exe simpledll.c -o simpledll.dll -shared
  4.  
  5. // BZ2 Loads DLL module with LoadLibrary(..., "DLL Name From BZN")
  6. // BZ2 looks for "GetMisnAPI" export in DLL, using GetProcAddress("GetMisnAPI")
  7. // BZ2 fills a MisnImport struct with a call to FillMisnImport(MisnImport*)
  8. // BZ2 calls GetMisnAPI(MisnImport*) passing a pointer to the filled struct
  9. // In GetMisnAPI, DLL fills its global MisnExport struct inside GetMisnAPI, and returns a pointer to it for BZ2.
  10. // BZ2 starts calling stuff in MisnExport
  11.  
  12. #define TARGET_BZCC
  13. #ifdef TARGET_BZCC
  14. // Nathan = "~GSH"
  15. // Brad = "BRAD"
  16. // Ken = "KEN3"
  17. // Brain = "BAL1"
  18. #define LATEST_DLL_VERSION 0X4C89322E
  19. #define LATEST_DLL_VERSION_MODIFIER "BAL1"
  20. #else
  21. #define LATEST_DLL_VERSION 0x4C89322D
  22. #define LATEST_DLL_VERSION_MODIFIER "~GSH"
  23. #endif
  24.  
  25. #include <windows.h>
  26. #include <stdio.h> // _snprintf
  27.  
  28. #define DLLAPI __cdecl
  29.  
  30. typedef char bool;
  31. typedef char* Name;
  32. typedef int Handle;
  33. typedef int TeamNum;
  34. typedef float Time;
  35. typedef float Dist;
  36. typedef int ScrapValue;
  37. typedef unsigned long DWORD;
  38. typedef DWORD DPID;
  39. #define DPID_UNKNOWN 0xFFFFFFFF
  40.  
  41. // Set of return codes from the PlayerEjected/PlayerKilled call to DLL
  42. enum EjectKillRetCodes {
  43.     DoEjectPilot, // Do 'standard' eject
  44.     DoRespawnSafest, // Respawn a 'PLAYER' at safest spawnpoint
  45.     DLLHandled, // DLL handled actions. Do nothing ingame
  46.     DoGameOver // Game over, man.
  47. };
  48.  
  49. typedef void GameObject;
  50. typedef void AiPath;
  51.  
  52. bool Save(bool missionSave) {
  53.     char msg[256];
  54.     _snprintf(msg, 256, "missionSave=%d", missionSave);
  55.     MessageBox(0, msg, "Save", MB_OK);
  56.     return missionSave;
  57. }
  58.  
  59. bool Load(bool missionSave) {
  60.     char msg[256];
  61.     _snprintf(msg, 256, "missionSave=%d", missionSave);
  62.     MessageBox(0, msg, "Load", MB_OK);
  63.     return missionSave;
  64. }
  65.  
  66. bool PostLoad(bool missionSave) {
  67.     char msg[256];
  68.     _snprintf(msg, 256, "missionSave=%d", missionSave);
  69.     MessageBox(0, msg, "PostLoad", MB_OK);
  70.     return missionSave;
  71. }
  72.  
  73. void AddObject(Handle h) {
  74.     char msg[256];
  75.     _snprintf(msg, 256, "Handle: 0x%x", h);
  76.     MessageBox(0, msg, "AddObject", MB_OK);
  77. }
  78.  
  79. // wingdi.h (included in windows.h) has a DeleteObject so I put an underscore
  80. void _DeleteObject(Handle h) {
  81.     char msg[256];
  82.     _snprintf(msg, 256, "Handle: 0x%x", h);
  83.     MessageBox(0, msg, "DeleteObject", MB_OK);
  84. }
  85.  
  86. void PostRun(void) {
  87.     MessageBox(0, "PostRun", "PostRun", MB_OK);
  88. }
  89.  
  90. bool AddPlayer(DPID id, int Team, bool ShouldCreateThem) {
  91.     char msg[256];
  92.     _snprintf(msg, 256, "DPID=%d\nTeam=%d\nShouldCreate=%d", id, Team, ShouldCreateThem);
  93.     MessageBox(0, msg, "AddPlayer", MB_OK);
  94.     return 0;
  95. }
  96.  
  97. void DeletePlayer(DPID id) {
  98.     char msg[256];
  99.     _snprintf(msg, 256, "DPID=%d", id);
  100.     MessageBox(0, msg, "DeletePlayer", MB_OK);
  101. };
  102.  
  103. int PlayerEjected(Handle DeadObjectHandle) {
  104.     char msg[256];
  105.     _snprintf(msg, 256, "DeadObjectHandle=0x%x", DeadObjectHandle);
  106.     MessageBox(0, msg, "PlayerEjected", MB_OK);
  107.     return DoGameOver;
  108. }
  109.  
  110. int ObjectKilled(Handle DeadObjectHandle, Handle KillersHandle) {
  111.     char msg[256];
  112.     _snprintf(msg, 256, "DeadObjectHandle=0x%x\nKillersHandle=0x%x", DeadObjectHandle, KillersHandle);
  113.     MessageBox(0, msg, "PlayerEjected", MB_OK);
  114.     return DoEjectPilot;
  115. }
  116.  
  117. int ObjectSniped(Handle DeadObjectHandle, Handle SnipersHandle) {
  118.     char msg[256];
  119.     _snprintf(msg, 256, "DeadObjectHandle=0x%x\nSnipersHandle=0x%x", DeadObjectHandle, SnipersHandle);
  120.     MessageBox(0, msg, "ObjectSniped", MB_OK);
  121.     return DoEjectPilot;
  122. }
  123.  
  124. char* GetNextRandomVehicleODF(int ForTeam) {
  125.     char msg[256];
  126.     _snprintf(msg, 256, "ForTeam=%d", ForTeam);
  127.     MessageBox(0, msg, "GetNextRandomVehicleODF", MB_OK);
  128.     return "player";
  129. }
  130.  
  131. void SetWorld(int nextWorld) {
  132.     char msg[256];
  133.     _snprintf(msg, 256, "nextWorld=%d", nextWorld);
  134.     MessageBox(0, msg, "SetWorld", MB_OK);
  135. }
  136.  
  137. void ProcessCommand(unsigned long crc) {
  138.     char msg[256];
  139.     _snprintf(msg, 256, "crc=%lu", crc);
  140.     MessageBox(0, msg, "ProcessCommand", MB_OK);
  141. }
  142.  
  143. void SetRandomSeed(unsigned long seed) {
  144.     // Parameter is not pointer type, so not sure what we would do here?
  145. };
  146.  
  147. void InitialSetup() {
  148.     MessageBox(0, "InitialSetup", "InitialSetup", MB_OK);
  149. }
  150.  
  151. void Update(void) {
  152.     // Called every game tick
  153. }
  154.  
  155. typedef struct MisnImport {
  156.     float time;
  157.     void (DLLAPI *FailMission)(Time t, char* fileName);
  158.     void (DLLAPI *SucceedMission)(Time t, char* fileName);
  159.     void (DLLAPI *ChangeSide)(void);
  160.     ScrapValue (DLLAPI *AddScrap)(TeamNum t, ScrapValue v);
  161.     ScrapValue (DLLAPI *SetScrap)(TeamNum t, ScrapValue v);
  162.     ScrapValue (DLLAPI *GetScrap)(TeamNum t);
  163.     ScrapValue (DLLAPI *GetMaxScrap)(TeamNum t);
  164.     Handle (DLLAPI *GetTug)(Handle h);
  165.     bool (DLLAPI *HasCargo)(Handle h);
  166.     Dist (DLLAPI *GetDistanceObject)(Handle *h1, Handle *h2);
  167.     Dist (DLLAPI *GetDistancePath)(Handle *h1, Name path, int point);
  168.     Dist (DLLAPI *GetDistancePathPtr)(Handle *h1, AiPath *path, int point);
  169.     Handle (DLLAPI *GetNearestObject)(Handle h);
  170.     Handle (DLLAPI *GetNearestVehicleObject)(Handle h);
  171.     Handle (DLLAPI *GetNearestVehiclePath)(Name path, int point);
  172.     Handle (DLLAPI *GetNearestBuilding)(Handle h);
  173.     Handle (DLLAPI *GetNearestEnemy)(Handle h);
  174. } MisnImport;
  175.  
  176. typedef struct MisnExport {
  177.     MisnImport *misnImport;
  178.     unsigned long version;
  179.     unsigned long VersionModifier;
  180.     void (DLLAPI *InitialSetup)(void);
  181.     bool (DLLAPI *Save)(bool missionSave);
  182.     bool (DLLAPI *Load)(bool missionSave);
  183.     bool (DLLAPI *PostLoad)(bool missionSave);
  184.     void (DLLAPI *AddObject)(Handle h);
  185.     void (DLLAPI *DeleteObject)(Handle h);
  186.     void (DLLAPI *Update)(void);
  187.     void (DLLAPI *PostRun)(void);
  188.     bool (DLLAPI *AddPlayer)(DPID id, int Team, bool ShouldCreateThem);
  189.     void (DLLAPI *DeletePlayer)(DPID id);
  190.     int (DLLAPI *PlayerEjected)(Handle DeadObjectHandle); // return type is EjectKillRetCodes
  191.     int (DLLAPI *ObjectKilled)(Handle DeadObjectHandle, Handle KillersHandle); // return type is EjectKillRetCodes
  192.     int (DLLAPI *ObjectSniped)(Handle DeadObjectHandle, Handle KillersHandle); // return type is EjectKillRetCodes
  193.     char* (DLLAPI *GetNextRandomVehicleODF)(int ForTeam);
  194.     void (DLLAPI *SetWorld)(int nextWorld);
  195.     void (DLLAPI *ProcessCommand)(unsigned long crc);
  196.     void (DLLAPI *SetRandomSeed)(unsigned long seed);
  197. } MisnExport;
  198.  
  199. MisnExport global_misnexport;
  200.  
  201. #define SIGNATURE_TO_UINT32(s) _byteswap_ulong(s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24)
  202.  
  203. // Function must be in export table of DLL (default in gcc I think)
  204. MisnExport* GetMisnAPI(MisnImport *misnimport) {
  205.     MessageBox(0, "DLL has been called by game.", "GetMisnAPI", MB_OK);
  206.     MisnExport *misnexport = &global_misnexport;
  207.     misnexport->misnImport = (void*)misnimport;
  208.     misnexport->version = LATEST_DLL_VERSION;
  209.     misnexport->VersionModifier = SIGNATURE_TO_UINT32(LATEST_DLL_VERSION_MODIFIER);
  210.     misnexport->Save = Save;
  211.     misnexport->Load = Load;
  212.     misnexport->PostLoad = PostLoad;
  213.     misnexport->AddObject = AddObject;
  214.     misnexport->DeleteObject = _DeleteObject;
  215.     misnexport->Update = Update;
  216.     misnexport->PostRun = PostRun;
  217.     misnexport->AddPlayer = AddPlayer;
  218.     misnexport->DeletePlayer = DeletePlayer;
  219.     misnexport->PlayerEjected = PlayerEjected;
  220.     misnexport->ObjectKilled = ObjectKilled;
  221.     misnexport->ObjectSniped = ObjectSniped;
  222.     misnexport->GetNextRandomVehicleODF = GetNextRandomVehicleODF;
  223.     misnexport->SetWorld = SetWorld;
  224.     misnexport->ProcessCommand = ProcessCommand;
  225.     misnexport->SetRandomSeed = SetRandomSeed;
  226.     misnexport->InitialSetup = InitialSetup;
  227.     return misnexport;
  228. }
  229.  
  230. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
  231.     // Perform actions based on the reason for calling.
  232.     switch( fdwReason )
  233.     {
  234.         case DLL_PROCESS_ATTACH:
  235.             MessageBox(NULL, "DLL_PROCESS_ATTACH", "DLL_PROCESS_ATTACH", MB_OK);
  236.         break;
  237.  
  238.         case DLL_THREAD_ATTACH:
  239.             // Do thread-specific initialization.
  240.         break;
  241.  
  242.         case DLL_THREAD_DETACH:
  243.             // Do thread-specific cleanup.
  244.         break;
  245.  
  246.         case DLL_PROCESS_DETACH:
  247.             MessageBox(NULL, "DLL_PROCESS_DETACH", "DLL_PROCESS_DETACH", MB_OK);
  248.             if (lpvReserved != NULL) {
  249.                 break; // do not do cleanup if process termination scenario
  250.             }
  251.  
  252.             // Perform any necessary cleanup.
  253.         break;
  254.     }
  255.     return TRUE;
  256. }
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement