|
| 1 | +package("bnm-android") |
| 2 | + set_homepage("https://github.com/ByNameModding/BNM-Android") |
| 3 | + set_description("Modding il2cpp games by classes, methods, fields names on Android.") |
| 4 | + |
| 5 | + add_urls("https://github.com/ByNameModding/BNM-Android.git") |
| 6 | + add_versions("2025.10.14", "502928771983d29e37f28c78f28823dfb775a3aa") |
| 7 | + |
| 8 | + add_configs("link_log", {description = "Link against liblog.so", default = false, type = "boolean"}) |
| 9 | + add_configs("hook_lib", { |
| 10 | + description = "Choose the hooking library used (dobby or shadowhook).", |
| 11 | + default = "shadowhook", |
| 12 | + type = "string", |
| 13 | + values = {"shadowhook", "dobby"}, |
| 14 | + }) |
| 15 | + add_configs("unity_version", { |
| 16 | + description = "Unity version (e.g., 5.6.4, 2017.1.0, 2022.2.1)", |
| 17 | + default = "2022.2.0", |
| 18 | + type = "string", |
| 19 | + }) |
| 20 | + |
| 21 | + on_load(function (package) |
| 22 | + package:add("deps", package:config("hook_lib")) |
| 23 | + end) |
| 24 | + |
| 25 | + local parse_unity_version = function (version) |
| 26 | + local major, minor, patch = version:match("^(%d+)%.(%d+)%.(%d+)") |
| 27 | + if not major then |
| 28 | + major, minor = version:match("^(%d+)%.(%d+)%.%w+") |
| 29 | + if not major then |
| 30 | + return 222, 32 |
| 31 | + end |
| 32 | + patch = nil |
| 33 | + end |
| 34 | + |
| 35 | + major = tonumber(major) |
| 36 | + minor = tonumber(minor) |
| 37 | + patch = patch and tonumber(patch) or nil |
| 38 | + |
| 39 | + local unity_ver |
| 40 | + local unity_patch_ver |
| 41 | + |
| 42 | + if major == 5 then |
| 43 | + if minor == 6 then |
| 44 | + unity_ver = 56 |
| 45 | + end |
| 46 | + elseif major == 2017 then |
| 47 | + if minor == 1 then |
| 48 | + unity_ver = 171 |
| 49 | + elseif minor >= 2 and minor <= 4 then |
| 50 | + unity_ver = 172 |
| 51 | + end |
| 52 | + elseif major == 2018 then |
| 53 | + if minor == 1 then |
| 54 | + unity_ver = 181 |
| 55 | + elseif minor == 2 then |
| 56 | + unity_ver = 182 |
| 57 | + elseif minor >= 3 then |
| 58 | + unity_ver = 183 |
| 59 | + end |
| 60 | + elseif major == 2019 then |
| 61 | + if minor <= 2 then |
| 62 | + unity_ver = 191 |
| 63 | + elseif minor == 3 then |
| 64 | + unity_ver = 193 |
| 65 | + elseif minor == 4 then |
| 66 | + unity_ver = 194 |
| 67 | + end |
| 68 | + elseif major == 2020 then |
| 69 | + if minor == 1 then |
| 70 | + unity_ver = 201 |
| 71 | + elseif minor == 2 then |
| 72 | + unity_ver = 202 |
| 73 | + elseif minor == 3 then |
| 74 | + if patch and patch >= 20 then |
| 75 | + unity_ver = 203 |
| 76 | + else |
| 77 | + unity_ver = 202 |
| 78 | + end |
| 79 | + end |
| 80 | + elseif major == 2021 then |
| 81 | + if minor == 1 then |
| 82 | + unity_ver = 211 |
| 83 | + -- Need to set UNITY_PATCH_VER to 24 if x (2021.1.x) >= 24 |
| 84 | + if patch and patch >= 24 then |
| 85 | + unity_patch_ver = patch |
| 86 | + end |
| 87 | + elseif minor == 2 then |
| 88 | + unity_ver = 212 |
| 89 | + elseif minor == 3 then |
| 90 | + unity_ver = 213 |
| 91 | + end |
| 92 | + elseif major == 2022 then |
| 93 | + if minor == 1 then |
| 94 | + unity_ver = 221 |
| 95 | + elseif minor >= 2 then |
| 96 | + unity_ver = 222 |
| 97 | + unity_patch_ver = patch or 32 |
| 98 | + end |
| 99 | + elseif major == 2023 then |
| 100 | + if minor == 1 then |
| 101 | + unity_ver = 231 |
| 102 | + elseif minor >= 2 then |
| 103 | + unity_ver = 232 |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + -- Fallback to default if no match found |
| 108 | + if not unity_ver then |
| 109 | + unity_ver = 222 |
| 110 | + unity_patch_ver = 32 |
| 111 | + end |
| 112 | + |
| 113 | + return unity_ver, unity_patch_ver |
| 114 | + end |
| 115 | + |
| 116 | + on_install("android", function (package) |
| 117 | + local unity_version = package:config("unity_version") |
| 118 | + local unity_ver, unity_patch_ver = parse_unity_version(unity_version) |
| 119 | + |
| 120 | + local dummy_impl = [[// Dummy |
| 121 | +#include <cassert> |
| 122 | +
|
| 123 | +static_assert(false, "No hooking software!"); |
| 124 | +
|
| 125 | +template<typename PTR_T, typename NEW_T, typename T_OLD> |
| 126 | +inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &oldBytes) { |
| 127 | + if ((void *) ptr != nullptr) ((void)0); |
| 128 | + return nullptr; |
| 129 | +} |
| 130 | +
|
| 131 | +template<typename PTR_T, typename NEW_T, typename T_OLD> |
| 132 | +inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &&oldBytes) { |
| 133 | + if ((void *) ptr != nullptr) ((void)0); |
| 134 | + return nullptr; |
| 135 | +} |
| 136 | +
|
| 137 | +template<typename PTR_T> |
| 138 | +inline void Unhook(PTR_T ptr) { |
| 139 | + if ((void *) ptr != nullptr) ((void)0); |
| 140 | +} |
| 141 | +]] |
| 142 | + |
| 143 | + io.replace("include/BNM/UserSettings/GlobalSettings.hpp", "#define UNITY_VER 222 // 2022.2.x", "#define UNITY_VER " .. unity_ver .. " // " .. unity_version, {plain = true}) |
| 144 | + if unity_patch_ver then |
| 145 | + io.replace("include/BNM/UserSettings/GlobalSettings.hpp", "#define UNITY_PATCH_VER 32", "#define UNITY_PATCH_VER " .. unity_patch_ver, {plain = true}) |
| 146 | + end |
| 147 | + if package:config("hook_lib") == "dobby" then |
| 148 | + io.replace("include/BNM/UserSettings/GlobalSettings.hpp", dummy_impl, [[ |
| 149 | +#include <dobby.h> |
| 150 | +
|
| 151 | +template<typename PTR_T, typename NEW_T, typename T_OLD> |
| 152 | +inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &oldBytes) { |
| 153 | + if ((void *) ptr != nullptr) DobbyHook((void *)ptr, (void *) newMethod, (void **) &oldBytes); |
| 154 | + return (void *) ptr; |
| 155 | +} |
| 156 | +
|
| 157 | +template<typename PTR_T, typename NEW_T, typename T_OLD> |
| 158 | +inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &&oldBytes) { |
| 159 | + if ((void *) ptr != nullptr) DobbyHook((void *)ptr, (void *) newMethod, (void **) &oldBytes); |
| 160 | + return (void *) ptr; |
| 161 | +} |
| 162 | +
|
| 163 | +template<typename PTR_T> |
| 164 | +inline void Unhook(PTR_T ptr) { |
| 165 | + if ((void *) ptr != nullptr) DobbyDestroy((void *)ptr); |
| 166 | +} |
| 167 | +]], {plain = true}) |
| 168 | + elseif package:config("hook_lib") == "shadowhook" then |
| 169 | + io.replace("include/BNM/UserSettings/GlobalSettings.hpp", dummy_impl, [[ |
| 170 | +#include "shadowhook.h" |
| 171 | +template<typename PTR_T, typename NEW_T, typename T_OLD> |
| 172 | +inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &oldBytes) { |
| 173 | + if ((void *) ptr != nullptr) return shadowhook_hook_func_addr((void *)ptr, (void *) newMethod, (void **) &oldBytes); |
| 174 | + return nullptr; |
| 175 | +} |
| 176 | +
|
| 177 | +template<typename PTR_T, typename NEW_T, typename T_OLD> |
| 178 | +inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &&oldBytes) { |
| 179 | + if ((void *) ptr != nullptr) return shadowhook_hook_func_addr((void *)ptr, (void *) newMethod, (void **) &oldBytes); |
| 180 | + return nullptr; |
| 181 | +} |
| 182 | +
|
| 183 | +template<typename PTR_T> |
| 184 | +inline void Unhook(PTR_T ptr) { |
| 185 | + if ((void *) ptr != nullptr) shadowhook_unhook((void *)ptr); |
| 186 | +} |
| 187 | + ]], {plain = true}) |
| 188 | + else |
| 189 | + raise("Unknown hooking library: " .. package:config("hook_lib")) |
| 190 | + end |
| 191 | + |
| 192 | + io.replace("include/BNM/UnityStructures/Matrix4x4.hpp", [[#include "Matrix3x3.hpp"]], [[#include "Matrix3x3.hpp" |
| 193 | +#include <cmath>]], {plain = true}) |
| 194 | + |
| 195 | + os.cp(path.join(package:scriptdir(), "port", "xmake.lua"), "xmake.lua") |
| 196 | + import("package.tools.xmake").install(package, { |
| 197 | + link_log = package:config("link_log"), |
| 198 | + hook_lib = package:config("hook_lib"), |
| 199 | + unity_version = package:config("unity_version"), |
| 200 | + version = package:version_str() |
| 201 | + }) |
| 202 | + end) |
| 203 | + |
| 204 | + on_test(function (package) |
| 205 | + package:check_cxxsnippets({test = [[ |
| 206 | + void test() { |
| 207 | + BNM::Loading::TryLoadByUsersFinder(); |
| 208 | + } |
| 209 | + ]]}, {configs = {languages = "c++20"}, includes = {"BNM/Loading.hpp"}}) |
| 210 | + end) |
0 commit comments