Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/b/bnm-android/port/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
set_project("BNM")

set_languages("c++20")

add_rules("mode.debug", "mode.release")

if is_config("hook_lib", "dobby") then
add_requires("dobby")
elseif is_config("hook_lib", "shadowhook") then
add_requires("shadowhook")
end

option("link_log", {
description = "Link against liblog.so",
default = false,
type = "boolean"
})
option("hook_lib", {
description = "Choose the hooking library used (dobby or shadowhook).",
default = "shadowhook",
type = "string",
values = {"shadowhook", "dobby"}
})
option("unity_version", {
description = "Unity version (e.g., 5.6.4, 2017.1.0, 2022.2.1)",
default = "2022.2.0",
type = "string"
})
option("version", {
description = "Set the version",
type = "string"
})

set_version(get_config("version"))

target("BNM")
set_kind("static")
if is_config("hook_lib", "dobby") then
add_packages("dobby", {public = true})
elseif is_config("hook_lib", "shadowhook") then
add_packages("shadowhook", {public = true})
end

add_files("src/*.cpp")

add_headerfiles("include/(**.h)", "include/(**.hpp)")
add_headerfiles("external/include/(**.h)", "external/include/(**.hpp)")
add_includedirs("include", "external/include", "external", "external/utf8", "src/private")

if has_config("link_log") then
add_syslinks("log")
end
Comment on lines +50 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of has_config("link_log") is incorrect for checking a boolean option. has_config checks if the option was set by the user, not its value. This means that if a user explicitly sets link_log=false, this condition would be true, which is not the intended behavior.

You should use get_config("link_log") to correctly check the boolean value of the option.

    if get_config("link_log") then
        add_syslinks("log")
    end


set_configvar("BNM_INCLUDE_DIRECTORIES", "include;external/include")
210 changes: 210 additions & 0 deletions packages/b/bnm-android/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package("bnm-android")
set_homepage("https://github.com/ByNameModding/BNM-Android")
set_description("Modding il2cpp games by classes, methods, fields names on Android.")

add_urls("https://github.com/ByNameModding/BNM-Android.git")
add_versions("2025.10.14", "502928771983d29e37f28c78f28823dfb775a3aa")

add_configs("link_log", {description = "Link against liblog.so", default = false, type = "boolean"})
add_configs("hook_lib", {
description = "Choose the hooking library used (dobby or shadowhook).",
default = "shadowhook",
type = "string",
values = {"shadowhook", "dobby"},
})
add_configs("unity_version", {
description = "Unity version (e.g., 5.6.4, 2017.1.0, 2022.2.1)",
default = "2022.2.0",
type = "string",
})

on_load(function (package)
package:add("deps", package:config("hook_lib"))
end)

local parse_unity_version = function (version)
local major, minor, patch = version:match("^(%d+)%.(%d+)%.(%d+)")
if not major then
major, minor = version:match("^(%d+)%.(%d+)%.%w+")
if not major then
return 222, 32
end
patch = nil
end

major = tonumber(major)
minor = tonumber(minor)
patch = patch and tonumber(patch) or nil

local unity_ver
local unity_patch_ver

if major == 5 then
if minor == 6 then
unity_ver = 56
end
elseif major == 2017 then
if minor == 1 then
unity_ver = 171
elseif minor >= 2 and minor <= 4 then
unity_ver = 172
end
elseif major == 2018 then
if minor == 1 then
unity_ver = 181
elseif minor == 2 then
unity_ver = 182
elseif minor >= 3 then
unity_ver = 183
end
elseif major == 2019 then
if minor <= 2 then
unity_ver = 191
elseif minor == 3 then
unity_ver = 193
elseif minor == 4 then
unity_ver = 194
end
elseif major == 2020 then
if minor == 1 then
unity_ver = 201
elseif minor == 2 then
unity_ver = 202
elseif minor == 3 then
if patch and patch >= 20 then
unity_ver = 203
else
unity_ver = 202
end
end
elseif major == 2021 then
if minor == 1 then
unity_ver = 211
-- Need to set UNITY_PATCH_VER to 24 if x (2021.1.x) >= 24
if patch and patch >= 24 then
unity_patch_ver = patch
end
elseif minor == 2 then
unity_ver = 212
elseif minor == 3 then
unity_ver = 213
end
elseif major == 2022 then
if minor == 1 then
unity_ver = 221
elseif minor >= 2 then
unity_ver = 222
unity_patch_ver = patch or 32
end
elseif major == 2023 then
if minor == 1 then
unity_ver = 231
elseif minor >= 2 then
unity_ver = 232
end
end

-- Fallback to default if no match found
if not unity_ver then
unity_ver = 222
unity_patch_ver = 32
end

return unity_ver, unity_patch_ver
end
Comment on lines +25 to +114
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function is quite long and contains a large chain of if/elseif statements, which can be difficult to read and maintain. Consider refactoring this logic to be more data-driven. You could use a table of rules to define the version mappings, or break the logic into smaller helper functions. This would make the function shorter, and adding support for new Unity versions in the future would be simpler.


on_install("android", function (package)
local unity_version = package:config("unity_version")
local unity_ver, unity_patch_ver = parse_unity_version(unity_version)

local dummy_impl = [[// Dummy
#include <cassert>

static_assert(false, "No hooking software!");

template<typename PTR_T, typename NEW_T, typename T_OLD>
inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &oldBytes) {
if ((void *) ptr != nullptr) ((void)0);
return nullptr;
}

template<typename PTR_T, typename NEW_T, typename T_OLD>
inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &&oldBytes) {
if ((void *) ptr != nullptr) ((void)0);
return nullptr;
}

template<typename PTR_T>
inline void Unhook(PTR_T ptr) {
if ((void *) ptr != nullptr) ((void)0);
}
]]

io.replace("include/BNM/UserSettings/GlobalSettings.hpp", "#define UNITY_VER 222 // 2022.2.x", "#define UNITY_VER " .. unity_ver .. " // " .. unity_version, {plain = true})
if unity_patch_ver then
io.replace("include/BNM/UserSettings/GlobalSettings.hpp", "#define UNITY_PATCH_VER 32", "#define UNITY_PATCH_VER " .. unity_patch_ver, {plain = true})
end
if package:config("hook_lib") == "dobby" then
io.replace("include/BNM/UserSettings/GlobalSettings.hpp", dummy_impl, [[
#include <dobby.h>

template<typename PTR_T, typename NEW_T, typename T_OLD>
inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &oldBytes) {
if ((void *) ptr != nullptr) DobbyHook((void *)ptr, (void *) newMethod, (void **) &oldBytes);
return (void *) ptr;
}

template<typename PTR_T, typename NEW_T, typename T_OLD>
inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &&oldBytes) {
if ((void *) ptr != nullptr) DobbyHook((void *)ptr, (void *) newMethod, (void **) &oldBytes);
return (void *) ptr;
}

template<typename PTR_T>
inline void Unhook(PTR_T ptr) {
if ((void *) ptr != nullptr) DobbyDestroy((void *)ptr);
}
]], {plain = true})
elseif package:config("hook_lib") == "shadowhook" then
io.replace("include/BNM/UserSettings/GlobalSettings.hpp", dummy_impl, [[
#include "shadowhook.h"
template<typename PTR_T, typename NEW_T, typename T_OLD>
inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &oldBytes) {
if ((void *) ptr != nullptr) return shadowhook_hook_func_addr((void *)ptr, (void *) newMethod, (void **) &oldBytes);
return nullptr;
}

template<typename PTR_T, typename NEW_T, typename T_OLD>
inline void *BasicHook(PTR_T ptr, NEW_T newMethod, T_OLD &&oldBytes) {
if ((void *) ptr != nullptr) return shadowhook_hook_func_addr((void *)ptr, (void *) newMethod, (void **) &oldBytes);
return nullptr;
}

template<typename PTR_T>
inline void Unhook(PTR_T ptr) {
if ((void *) ptr != nullptr) shadowhook_unhook((void *)ptr);
}
]], {plain = true})
else
raise("Unknown hooking library: " .. package:config("hook_lib"))
Copy link
Contributor

@luadebug luadebug Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-code-assist Is this redundant block of code, as it is already limited to two options there would not be third options selected anyway nothing to worry about?

end

io.replace("include/BNM/UnityStructures/Matrix4x4.hpp", [[#include "Matrix3x3.hpp"]], [[#include "Matrix3x3.hpp"
#include <cmath>]], {plain = true})

os.cp(path.join(package:scriptdir(), "port", "xmake.lua"), "xmake.lua")
import("package.tools.xmake").install(package, {
link_log = package:config("link_log"),
hook_lib = package:config("hook_lib"),
unity_version = package:config("unity_version"),
version = package:version_str()
})
end)

on_test(function (package)
package:check_cxxsnippets({test = [[
void test() {
BNM::Loading::TryLoadByUsersFinder();
}
]]}, {configs = {languages = "c++20"}, includes = {"BNM/Loading.hpp"}})
end)
Loading