| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/lib/module.h $ |
| 21 | * $Revision: 12 $ |
| 22 | * $Date: 7/14/99 6:43p $ |
| 23 | * $Author: Jeff $ |
| 24 | * |
| 25 | * Header for Dynamic Loadable Modules |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/module.h $ |
| 28 | * |
| 29 | * 12 7/14/99 6:43p Jeff |
| 30 | * don't load modules globally by default |
| 31 | * |
| 32 | * 11 7/13/99 5:40p Jeff |
| 33 | * fixes for Linux compile |
| 34 | * |
| 35 | * 10 6/25/99 4:58p Jeff |
| 36 | * default module load as global |
| 37 | * |
| 38 | * 9 5/10/99 10:53p Ardussi |
| 39 | * changes to compile on Mac |
| 40 | * |
| 41 | * 8 4/19/99 3:57a Jeff |
| 42 | * changed define for Linux in order for multiplayer games to compile |
| 43 | * |
| 44 | * 7 4/15/99 2:57a Jeff |
| 45 | * added missing defines for linux |
| 46 | * |
| 47 | * 6 1/11/99 12:53p Jeff |
| 48 | * added a function that given a module name it will make sure it has an |
| 49 | * extension. Made Osiris friendly with modules with no extension |
| 50 | * |
| 51 | * 5 1/04/99 12:24p Jeff |
| 52 | * updated...added compiler friendly define for dll functions that return |
| 53 | * a pointer |
| 54 | * |
| 55 | * 4 11/13/98 6:36p Jeff |
| 56 | * created dmfc_dll (a DLL version of DMFC) and converted current mods to |
| 57 | * use it |
| 58 | * |
| 59 | * 3 7/06/98 10:45a Jeff |
| 60 | * Made Linux friendly |
| 61 | * |
| 62 | * 2 6/05/98 2:15p Jeff |
| 63 | * Initial creation |
| 64 | * |
| 65 | * 1 6/05/98 2:14p Jeff |
| 66 | * |
| 67 | * $NoKeywords: $ |
| 68 | */ |
| 69 | |
| 70 | #ifndef __DLMODULE_H_ |
| 71 | #define __DLMODULE_H_ |
| 72 | |
| 73 | #include <cstdint> |
| 74 | |
| 75 | #ifdef __cplusplus |
| 76 | #define CPPEXTERN extern "C" |
| 77 | #else |
| 78 | #define CPPEXTERN extern |
| 79 | #endif |
| 80 | |
| 81 | #ifdef _MSC_VER // MSVC Build |
| 82 | # define STDCALL __stdcall |
| 83 | # define STDCALLPTR *STDCALL |
| 84 | #else // Non-MS compilers |
| 85 | # if defined(__i386__) || defined(_X86_) || defined(__THW_INTEL__) |
| 86 | # define STDCALL __attribute__((stdcall)) |
| 87 | # else |
| 88 | # define STDCALL |
| 89 | # endif |
| 90 | # define STDCALLPTR STDCALL * |
| 91 | #endif |
| 92 | |
| 93 | # define DLLFUNCCALL STDCALL |
| 94 | # define DLLFUNCCALLPTR STDCALLPTR |
| 95 | |
| 96 | #ifdef WIN32 |
| 97 | //=========================Windows Definition============================ |
| 98 | #include "windows.h" |
| 99 | |
| 100 | #define MODPROCADDRESS FARPROC |
| 101 | #define DLLFUNCEXPORT __declspec(dllexport) |
| 102 | #define DLLFUNCIMPORT __declspec(dllimport) |
| 103 | #define DLLEXPORT CPPEXTERN DLLFUNCEXPORT |
| 104 | struct module { |
| 105 | HINSTANCE handle; // handle to the DLL |
| 106 | }; |
| 107 | //======================================================================= |
| 108 | #elif defined(POSIX) |
| 109 | //==========================Linux Definitions============================ |
| 110 | |
| 111 | #define MODPROCADDRESS void * |
| 112 | #define DLLFUNCEXPORT __attribute__((visibility("default"))) |
| 113 | #define DLLFUNCIMPORT |
| 114 | #define DLLEXPORT CPPEXTERN DLLFUNCEXPORT |
| 115 | |
| 116 | struct module { |
| 117 | void *handle; // handle to the DLL |
| 118 | }; |
| 119 | //======================================================================= |
| 120 | #endif |
| 121 | |
| 122 | // Mod error codes |
| 123 | #define MODERR_NOERROR 0 // There was no error |
| 124 | #define MODERR_INVALIDMOD 1 // This is not a valid module |
| 125 | #define MODERR_MODNOTFOUND 2 // The module couldn't be found |
| 126 | #define MODERR_MODINITFAIL 3 // The module initialization routine failed |
| 127 | #define MODERR_NOMOD 4 // The value you past in for the module isn't a module, or nothing has been loaded |
| 128 | #define MODERR_INVALIDHANDLE 5 // The module handle passed in is NULL |
| 129 | #define MODERR_OTHER 255 // Some other error occured |
| 130 | |
| 131 | // Flags |
| 132 | #define MODF_LAZY 0x001 // Symbol resolution on demand |
| 133 | #define MODF_NOW 0x002 // Resolve all symbols before returning |
| 134 | #define MODF_GLOBAL 0x200 // |
| 135 | |
| 136 | // Returns the real name of the module. If a given file has an extension, it will |
| 137 | // just return that filename. If the given file has no given extension, the |
| 138 | // system specific extension is concatted and returned. |
| 139 | void mod_GetRealModuleName(const char *modfilename, char *realmodfilename); |
| 140 | |
| 141 | // Loads a dynamic module into memory for use. If no extension is given, the default |
| 142 | // system specific extension is used. |
| 143 | // Returns true on success, false otherwise |
| 144 | bool mod_LoadModule(module *handle, const char *modfilename, int flags = MODF_LAZY); |
| 145 | |
| 146 | // Frees a previously loaded module from memory, it can no longer be used |
| 147 | // Returns true on success, false otherwise |
| 148 | bool mod_FreeModule(module *handle); |
| 149 | |
| 150 | // Returns a pointer to a function within a loaded module. If it returns NULL there was an error. Check |
| 151 | // mod_GetLastError to see if there was an error symstr is the name of the function you want to get the symbol for (Do |
| 152 | // NOT give any pre/suffix to this name) parmbytes is the size (in bytes) of the parameter list the function should have |
| 153 | MODPROCADDRESS mod_GetSymbol(module *handle, const char *symstr, uint8_t parmbytes); |
| 154 | |
| 155 | // Returns an error code to what the last error was. When this function is called the last error is cleared, so by |
| 156 | // calling this function it not only returns the last error, but it removes it, so if you were to call this function |
| 157 | // again, it would return no error |
| 158 | int mod_GetLastError(void); |
| 159 | |
| 160 | #endif |