Path: blob/master/RSDKv5/RSDK/Core/ModAPI.hpp
1163 views
#ifndef MOD_API_H1#define MOD_API_H23#if RETRO_USE_MOD_LOADER4#include <vector>5#include <string>6#include <sstream>7#include <map>8#include <regex>9#include "tinyxml2.h"1011#include <functional>12#endif1314namespace RSDK15{1617#if RETRO_USE_MOD_LOADER1819#define LEGACY_PLAYERNAME_COUNT (0x10)2021extern std::map<uint32, uint32> superLevels;22extern int32 inheritLevel;2324enum ModCallbackEvents {25MODCB_ONGAMESTARTUP,26MODCB_ONSTATICLOAD,27MODCB_ONSTAGELOAD,28MODCB_ONUPDATE,29MODCB_ONLATEUPDATE,30MODCB_ONSTATICUPDATE,31MODCB_ONDRAW,32MODCB_ONSTAGEUNLOAD,33MODCB_ONSHADERLOAD,34MODCB_ONVIDEOSKIPCB,35MODCB_ONSCANLINECB,36MODCB_MAX,37};3839enum ModSuper {40SUPER_UPDATE,41SUPER_LATEUPDATE,42SUPER_STATICUPDATE,43SUPER_DRAW,44SUPER_CREATE,45SUPER_STAGELOAD,46SUPER_EDITORLOAD,47SUPER_EDITORDRAW,48SUPER_SERIALIZE49};5051enum ModFunctionTableIDs {52ModTable_RegisterGlobals,53ModTable_RegisterObject,54ModTable_RegisterObjectSTD,55ModTable_RegisterObjectHook,56ModTable_FindObject,57ModTable_GetGlobals,58ModTable_Super,59ModTable_LoadModInfo,60ModTable_GetModPath,61ModTable_GetModCount,62ModTable_GetModIDByIndex,63ModTable_ForeachModID,64ModTable_AddModCallback,65ModTable_AddModCallbackSTD,66ModTable_AddPublicFunction,67ModTable_GetPublicFunction,68ModTable_GetSettingsBool,69ModTable_GetSettingsInt,70ModTable_GetSettingsFloat,71ModTable_GetSettingsString,72ModTable_SetSettingsBool,73ModTable_SetSettingsInt,74ModTable_SetSettingsFloat,75ModTable_SetSettingsString,76ModTable_SaveSettings,77ModTable_GetConfigBool,78ModTable_GetConfigInt,79ModTable_GetConfigFloat,80ModTable_GetConfigString,81ModTable_ForeachConfig,82ModTable_ForeachConfigCategory,83ModTable_RegisterAchievement,84ModTable_GetAchievementInfo,85ModTable_GetAchievementIndexByID,86ModTable_GetAchievementCount,87ModTable_LoadShader,88ModTable_StateMachineRun,89ModTable_RegisterStateHook,90ModTable_HandleRunState_HighPriority,91ModTable_HandleRunState_LowPriority,9293#if RETRO_MOD_LOADER_VER >= 294// Mod Settings (Part 2)95ModTable_ForeachSetting,96ModTable_ForeachSettingCategory,9798// Files99ModTable_ExcludeFile,100ModTable_ExcludeAllFiles,101ModTable_ReloadFile,102ModTable_ReloadAllFiles,103104// Graphics105ModTable_GetSpriteAnimation,106ModTable_GetSpriteSurface,107ModTable_GetPaletteBank,108ModTable_GetActivePaletteBuffer,109ModTable_GetRGB32To16Buffer,110ModTable_GetBlendLookupTable,111ModTable_GetSubtractLookupTable,112ModTable_GetTintLookupTable,113ModTable_GetMaskColor,114ModTable_GetScanEdgeBuffer,115ModTable_GetCamera,116ModTable_GetShader,117ModTable_GetModel,118ModTable_GetScene3D,119ModTable_DrawDynamicAniTile,120121// Audio122ModTable_GetSfx,123ModTable_GetChannel,124125// Objects/Entities126ModTable_GetGroupEntities,127128// Collision129ModTable_SetPathGripSensors,130ModTable_FloorCollision,131ModTable_LWallCollision,132ModTable_RoofCollision,133ModTable_RWallCollision,134ModTable_FindFloorPosition,135ModTable_FindLWallPosition,136ModTable_FindRoofPosition,137ModTable_FindRWallPosition,138ModTable_CopyCollisionMask,139ModTable_GetCollisionInfo,140#endif141142ModTable_Count143};144145typedef void (*ModCallback)(void *data);146typedef std::function<void(void *data)> ModCallbackSTD;147148typedef bool (*modLink)(EngineInfo *, const char *);149typedef std::function<bool(EngineInfo *, const char *)> modLinkSTD;150151struct ModPublicFunctionInfo {152std::string name;153void *ptr;154};155156struct ObjectHook {157RETRO_HASH_MD5(hash);158Object **staticVars;159};160161struct ModVersionInfo {162uint8 engineVer;163uint8 gameVer;164uint8 modLoaderVer;165// uint8 engineVer; // 5, 4 or 3166};167168struct ModSVInfo {169std::string name;170Object **staticVars;171uint32 size;172};173174struct ModInfo {175std::string path;176std::string id;177std::string name;178std::string desc;179std::string author;180std::string version;181std::string folderName;182bool active;183bool redirectSaveRAM;184bool disableGameLogic;185bool forceScripts;186int32 targetVersion;187int32 forceVersion;188std::map<std::string, std::string> fileMap;189std::vector<std::string> excludedFiles;190std::vector<ModPublicFunctionInfo> functionList;191std::vector<Link::Handle> modLogicHandles;192std::vector<modLinkSTD> linkModLogic;193void (*unloadMod)();194std::map<std::string, std::map<std::string, std::string>> settings;195std::map<std::string, std::map<std::string, std::string>> config;196197// mapped to base hash198std::map<uint32 *, ModSVInfo> staticVars;199};200201struct StateHook {202void (*state)();203bool32 (*hook)(bool32 skippedState);204bool32 priority;205};206207struct ModSettings {208int32 activeMod = -1;209bool32 redirectSaveRAM = false;210bool32 disableGameLogic = false;211212#if RETRO_REV0U213int32 versionOverride = 0;214bool32 forceScripts = false;215216char playerNames[LEGACY_PLAYERNAME_COUNT][0x20];217int32 playerCount = 0;218#endif219};220221extern ModSettings modSettings;222extern std::vector<ModInfo> modList;223extern std::vector<ModCallbackSTD> modCallbackList[MODCB_MAX];224extern std::vector<StateHook> stateHookList;225extern std::vector<ObjectHook> objectHookList;226extern ModVersionInfo targetModVersion;227228extern char customUserFileDir[0x100];229230extern void *modFunctionTable[ModTable_Count];231extern int32 currentObjectID;232233extern ModInfo *currentMod;234235inline void SetActiveMod(int32 id) { modSettings.activeMod = id; }236237void InitModAPI(bool32 getVersion = false);238void UnloadMods();239void LoadMods(bool newOnly = false, bool32 getVersion = false);240bool32 LoadMod(ModInfo *info, const std::string& modsPath, const std::string& folder, bool32 active, bool32 getVersion);241void SaveMods();242void SortMods();243void LoadModSettings();244void ApplyModChanges();245246bool32 ScanModFolder(ModInfo *info, const char *targetFile = nullptr, bool32 fromLoadMod = false, bool32 loadingBar = true);247inline void RefreshModFolders(bool32 versionOnly = false, bool32 loadingBar = true)248{249SortMods();250for (int32 m = 0; m < modList.size(); ++m) {251if (!modList[m].active)252break;253ScanModFolder(&modList[m], versionOnly ? "Data/Game/GameConfig.bin" : nullptr, true, loadingBar);254}255}256257void RunModCallbacks(int32 callbackID, void *data);258259// Mod API260#if RETRO_REV0U261void ModRegisterGlobalVariables(const char *globalsPath, void **globals, uint32 size);262263void ModRegisterObject(Object **staticVars, Object **modStaticVars, const char *name, uint32 entityClassSize, uint32 staticClassSize,264uint32 modClassSize, void (*update)(), void (*lateUpdate)(), void (*staticUpdate)(), void (*draw)(), void (*create)(void *),265void (*stageLoad)(), void (*editorLoad)(), void (*editorDraw)(), void (*serialize)(), void (*staticLoad)(Object *),266const char *inherited);267268void ModRegisterObject_STD(Object **staticVars, Object **modStaticVars, const char *name, uint32 entityClassSize, uint32 staticClassSize,269uint32 modClassSize, std::function<void()> update, std::function<void()> lateUpdate, std::function<void()> staticUpdate,270std::function<void()> draw, std::function<void(void *)> create, std::function<void()> stageLoad,271std::function<void()> editorLoad, std::function<void()> editorDraw, std::function<void()> serialize,272std::function<void(Object *)> staticLoad, const char *inherited);273#else274void ModRegisterGlobalVariables(const char *globalsPath, void **globals, uint32 size);275276void ModRegisterObject(Object **staticVars, Object **modStaticVars, const char *name, uint32 entityClassSize, uint32 staticClassSize,277uint32 modClassSize, void (*update)(), void (*lateUpdate)(), void (*staticUpdate)(), void (*draw)(), void (*create)(void *),278void (*stageLoad)(), void (*editorLoad)(), void (*editorDraw)(), void (*serialize)(), const char *inherited);279280void ModRegisterObject_STD(Object **staticVars, Object **modStaticVars, const char *name, uint32 entityClassSize, uint32 staticClassSize,281uint32 modClassSize, std::function<void()> update, std::function<void()> lateUpdate, std::function<void()> staticUpdate,282std::function<void()> draw, std::function<void(void *)> create, std::function<void()> stageLoad,283std::function<void()> editorLoad, std::function<void()> editorDraw, std::function<void()> serialize,284const char *inherited);285#endif286287void ModRegisterObjectHook(Object **staticVars, const char *staticName);288Object *ModFindObject(const char *name);289290void *GetGlobals();291292bool32 LoadModInfo(const char *id, String *name, String *description, String *version, bool32 *active);293void GetModPath(const char *id, String *result);294int32 GetModCount(bool32 active);295const char *GetModIDByIndex(uint32 index);296bool32 ForeachModID(String *id);297298void AddModCallback(int32 callbackID, ModCallback callback);299void AddModCallback_STD(int32 callbackID, ModCallbackSTD callback);300void AddPublicFunction(const char *functionName, void *functionPtr);301void *GetPublicFunction(const char *id, const char *functionName);302303bool32 GetSettingsBool(const char *id, const char *key, bool32 fallback);304int32 GetSettingsInteger(const char *id, const char *key, int32 fallback);305float GetSettingsFloat(const char *id, const char *key, float fallback);306void GetSettingsString(const char *id, const char *key, String *result, const char *fallback);307308void SetSettingsBool(const char *key, bool32 val);309void SetSettingsInteger(const char *key, int32 val);310void SetSettingsFloat(const char *key, float val);311void SetSettingsString(const char *key, String *val);312313void SaveSettings();314315bool32 GetConfigBool(const char *key, bool32 fallback);316int32 GetConfigInteger(const char *key, int32 fallback);317float GetConfigFloat(const char *key, float fallback);318void GetConfigString(const char *key, String *result, const char *fallback);319bool32 ForeachConfig(String *config);320bool32 ForeachConfigCategory(String *category);321#if RETRO_MOD_LOADER_VER >= 2322bool32 ForeachSetting(const char *id, String *setting);323bool32 ForeachSettingCategory(const char *id, String *category);324#endif325326void Super(int32 objectID, ModSuper callback, void *data);327328void GetAchievementInfo(uint32 id, String *name, String *description, String *identifer, bool32 *achieved);329int32 GetAchievementIndexByID(const char *id);330int32 GetAchievementCount();331332void StateMachineRun(void (*state)(void));333bool32 HandleRunState_HighPriority(void (*state)(void));334void HandleRunState_LowPriority(void (*state)(void), bool32 skipState);335void RegisterStateHook(void (*state)(void), bool32 (*hook)(bool32 skippedState), bool32 priority);336337#if RETRO_MOD_LOADER_VER >= 2338339// Files340bool32 ExcludeFile(const char *id, const char *path);341bool32 ExcludeAllFiles(const char *id);342bool32 ReloadFile(const char *id, const char *path);343bool32 ReloadAllFiles(const char *id);344345// Graphics346inline void *GetSpriteAnimation(uint16 id)347{348if (id >= SPRFILE_COUNT)349return NULL;350return &spriteAnimationList[id];351}352inline void *GetSpriteSurface(uint16 id)353{354if (id >= SURFACE_COUNT)355return NULL;356return &gfxSurface[id];357}358inline uint16 *GetPaletteBank(uint8 id)359{360if (id >= PALETTE_BANK_COUNT)361return NULL;362return fullPalette[id];363}364inline uint8 *GetActivePaletteBuffer() { return gfxLineBuffer; }365inline void GetRGB32To16Buffer(uint16 **rgb32To16_R, uint16 **rgb32To16_G, uint16 **rgb32To16_B)366{367if (rgb32To16_R)368*rgb32To16_R = RSDK::rgb32To16_R;369370if (rgb32To16_G)371*rgb32To16_G = RSDK::rgb32To16_G;372373if (rgb32To16_B)374*rgb32To16_B = RSDK::rgb32To16_B;375}376inline uint16 *GetBlendLookupTable() { return blendLookupTable; }377inline uint16 *GetSubtractLookupTable() { return subtractLookupTable; }378inline color GetMaskColor() { return maskColor; }379inline void *GetScanEdgeBuffer() { return scanEdgeBuffer; }380inline void *GetCamera(uint8 id)381{382if (id >= CAMERA_COUNT)383return NULL;384return &cameras[id];385}386inline void *GetShader(uint8 id)387{388if (id >= SHADER_COUNT)389return NULL;390return &shaderList[id];391}392inline void *GetModel(uint16 id)393{394if (id >= MODEL_COUNT)395return NULL;396return &modelList[id];397}398inline void *GetScene3D(uint16 id)399{400if (id >= SCENE3D_COUNT)401return NULL;402return &scene3DList[id];403}404405// Audio406inline void *GetSfxEntry(uint16 id)407{408if (id >= SFX_COUNT)409return NULL;410return &sfxList[id];411}412inline void *GetChannel(uint8 id)413{414if (id >= CHANNEL_COUNT)415return NULL;416return &channels[id];417}418419// Objects/Entities420bool32 GetGroupEntities(uint16 group, void **entity);421#endif422423#endif424425#if RETRO_REV0U426#include "Legacy/ModAPILegacy.hpp"427#endif428429} // namespace RSDK430431#if RETRO_USE_MOD_LOADER && RETRO_PLATFORM == RETRO_ANDROID432#if _INTELLISENSE_ANDROID433#include "RetroEngine.hpp"434#endif435436#include <iterator>437#include <cstddef>438#include <forward_list>439440extern jmethodID fsExists;441extern jmethodID fsIsDir;442extern jmethodID fsDirIter;443extern jmethodID fsRecurseIter;444445namespace fs446{447struct filesystem_error {448const char *what() { return err.c_str(); }449450private:451std::string err;452};453454struct path {455path() = default;456path(std::string str) : pathStr(str){};457const std::string &string() const { return pathStr; }458path filename() { return pathStr.substr(pathStr.find_last_of('/') + 1); }459460private:461std::string pathStr = "";462};463464bool exists(path path);465466bool is_directory(path path);467468struct directory_entry {469directory_entry() = default;470directory_entry(fs::path p) : m_path(p){};471472bool exists() { return fs::exists(m_path); }473474bool is_directory() { return fs::is_directory(m_path); }475476bool is_regular_file() { return !fs::is_directory(m_path); }477478const path &path() { return m_path; }479480private:481fs::path m_path;482};483484class path_list : public std::vector<directory_entry>485{486public:487path_list(jobjectArray array)488{489vector();490auto *jni = GetJNISetup();491int len = jni->env->GetArrayLength(array);492for (int i = 0; i < len; ++i) {493jstring jstr = (jstring)jni->env->GetObjectArrayElement(array, i);494const char *str = jni->env->GetStringUTFChars(jstr, NULL);495this->push_back(directory_entry(std::string(str)));496jni->env->ReleaseStringUTFChars(jstr, str);497}498}499};500501enum class directory_options { follow_directory_symlink = 0 };502503path_list directory_iterator(path path);504505class recursive_directory_iterator506{507struct JNISetup *jni = nullptr;508directory_entry current;509510jstring jstr;511const char *str = nullptr;512513jbyteArray jpath;514515public:516using iterator_category = std::input_iterator_tag;517using value_type = directory_entry;518using difference_type = ptrdiff_t;519using pointer = const directory_entry *;520using reference = const directory_entry &;521522recursive_directory_iterator() = default;523recursive_directory_iterator(path path, directory_options _)524{525(void)_;526jni = GetJNISetup();527jpath = jni->env->NewByteArray(path.string().length());528jni->env->SetByteArrayRegion(jpath, 0, path.string().length(), (jbyte *)path.string().c_str());529operator++();530};531532// this class is modified from the MSVC headers LMAO533534bool operator==(const recursive_directory_iterator &rhs) const noexcept { return jni == rhs.jni; }535bool operator!=(const recursive_directory_iterator &rhs) const noexcept { return jni != rhs.jni; }536const directory_entry &operator*() const noexcept { return current; }537538const directory_entry *operator->() const noexcept { return &**this; }539540recursive_directory_iterator &operator++()541{542if (str) {543jni->env->ReleaseStringUTFChars(jstr, str);544}545jstr = (jstring)jni->env->CallObjectMethod(jni->thiz, fsRecurseIter, jpath);546if (jstr == NULL) {547*this = {};548}549else {550str = jni->env->GetStringUTFChars(jstr, NULL);551current = directory_entry(fs::path(str));552}553return *this;554}555556inline recursive_directory_iterator begin() noexcept { return *this; }557558inline recursive_directory_iterator end() noexcept { return {}; }559};560561}; // namespace fs562#endif563564#endif // !MOD_API_H565566567