Skip to content

Commit

Permalink
Add 'replaceAssetBundleFilePath' option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimjio committed Oct 7, 2022
1 parent 26ee2d4 commit e042a38
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ namespace

Il2CppObject* assets = nullptr;

Il2CppObject* replaceAssets = nullptr;

Il2CppObject* uiManager = nullptr;

vector<string> replaceAssetNames;

Il2CppObject* (*load_from_file)(Il2CppString* path);

Il2CppObject* (*load_assets)(Il2CppObject* _this, Il2CppString* name, Il2CppObject* runtimeType);

Il2CppArraySize* (*get_all_asset_names)(Il2CppObject* _this);

Il2CppString* (*uobject_get_name)(Il2CppObject* uObject);

bool (*uobject_IsNativeObjectAlive)(Il2CppObject* uObject);
Expand Down Expand Up @@ -989,8 +995,8 @@ namespace
}

void* assetbundle_LoadFromFile_orig = nullptr;

Il2CppObject* assetbundle_LoadFromFile_hook(Il2CppString* path) {
Il2CppObject* assetbundle_LoadFromFile_hook(Il2CppString* path)
{
stringstream pathStream(local::wide_u8(path->start_char));
string segment;
vector<string> splited;
Expand All @@ -1008,6 +1014,16 @@ namespace
return reinterpret_cast<decltype(assetbundle_LoadFromFile_hook)*>(assetbundle_LoadFromFile_orig)(path);
}

void* assetbundle_load_asset_orig = nullptr;
Il2CppObject* assetbundle_load_asset_hook(Il2CppObject* _this, Il2CppString* name, Il2CppType* type)
{
string u8Name = local::wide_u8(name->start_char);
if (find(replaceAssetNames.begin(), replaceAssetNames.end(), u8Name) != replaceAssetNames.end())
{
return reinterpret_cast<decltype(assetbundle_load_asset_hook)*>(assetbundle_load_asset_orig)(replaceAssets, name, type);
}
return reinterpret_cast<decltype(assetbundle_load_asset_hook)*>(assetbundle_load_asset_orig)(_this, name, type);
}

void* assetbundle_unload_orig = nullptr;
void assetbundle_unload_hook(Il2CppObject* _this, bool unloadAllLoadedObjects)
Expand Down Expand Up @@ -1216,6 +1232,12 @@ namespace
"AssetBundle", "LoadAsset", 2)
);

get_all_asset_names = reinterpret_cast<Il2CppArraySize * (*)(Il2CppObject * _this)>(
il2cpp_symbols::get_method_pointer(
"UnityEngine.AssetBundleModule.dll", "UnityEngine",
"AssetBundle", "GetAllAssetNames", 0)
);

uobject_get_name = reinterpret_cast<Il2CppString * (*)(Il2CppObject * uObject)>(
il2cpp_symbols::get_method_pointer(
"UnityEngine.CoreModule.dll", "UnityEngine",
Expand Down Expand Up @@ -1580,6 +1602,12 @@ namespace
"UnityEngine.AssetBundleModule.dll", "UnityEngine", "AssetBundle",
"LoadFromFile", 1));

auto assetbundle_load_asset_addr = reinterpret_cast<Il2CppObject * (*)(Il2CppObject * _this, Il2CppString * name, Il2CppObject * runtimeType)>(
il2cpp_symbols::get_method_pointer(
"UnityEngine.AssetBundleModule.dll", "UnityEngine",
"AssetBundle", "LoadAsset", 2)
);

auto assetbundle_unload_addr = reinterpret_cast<Il2CppObject * (*)(Il2CppString * path)>(il2cpp_symbols::get_method_pointer("UnityEngine.AssetBundleModule.dll", "UnityEngine", "AssetBundle", "Unload", 1));

auto load_scene_internal_addr = il2cpp_resolve_icall("UnityEngine.SceneManagement.SceneManager::LoadSceneAsyncNameIndexInternal_Injected(System.String,System.Int32,UnityEngine.SceneManagement.LoadSceneParameters&,System.bool)");
Expand All @@ -1600,9 +1628,36 @@ namespace
}
}

if (assets) {
if (assets)
{
cout << "Asset loaded: " << assets << "\n";
}

if (!replaceAssets && !g_replace_assetbundle_file_path.empty())
{
auto assetbundlePath = local::u8_wide(g_replace_assetbundle_file_path);
if (PathIsRelativeW(assetbundlePath.data()))
{
assetbundlePath.insert(0, ((wstring)std::filesystem::current_path().native()).append(L"/"));
}
replaceAssets = load_from_file(il2cpp_string_new_utf16(assetbundlePath.data(), assetbundlePath.length()));

if (!replaceAssets && filesystem::exists(assetbundlePath))
{
cout << "Replacement AssetBundle founded but not loaded. Maybe Asset BuildTarget is not for Windows\n";
}
}

if (replaceAssets)
{
cout << "Replacement AssetBundle loaded: " << replaceAssets << "\n";
auto names = get_all_asset_names(replaceAssets);
for (int i = 0; i < names->max_length; i++)
{
auto u8Name = local::wide_u8(static_cast<Il2CppString*>(names->vector[i])->start_char);
replaceAssetNames.emplace_back(u8Name);
}
}
#pragma endregion

ADD_HOOK(GetLimitSize, "Gallop.StandaloneWindowResize::GetChangedSize at %p\n");
Expand All @@ -1613,6 +1668,8 @@ namespace

ADD_HOOK(assetbundle_LoadFromFile, "UnityEngine.AssetBundle::LoadFromFile at %p\n");

ADD_HOOK(assetbundle_load_asset, "UnityEngine.AssetBundle::LoadAsset at %p\n");

ADD_HOOK(assetbundle_unload, "UnityEngine.AssetBundle::Unload at %p\n");

ADD_HOOK(get_preferred_width, "UnityEngine.TextGenerator::GetPreferredWidth at %p\n");
Expand Down
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ float g_force_landscape_ui_scale = 0.5;
bool g_ui_loading_show_orientation_guide = true;
std::string g_custom_title_name;
std::unordered_map<std::string, ReplaceAsset> g_replace_assets;
std::string g_replace_assetbundle_file_path;

namespace
{
Expand Down Expand Up @@ -154,6 +155,10 @@ namespace
}
}

if (document.HasMember("replaceAssetBundleFilePath")) {
g_replace_assetbundle_file_path = document["replaceAssetBundleFilePath"].GetString();
}

// Looks like not working for now
// g_aspect_ratio = document["customAspectRatio"].GetFloat();
if (document.HasMember("dicts")) {
Expand Down
1 change: 1 addition & 0 deletions src/stdinclude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern float g_force_landscape_ui_scale;
extern bool g_ui_loading_show_orientation_guide;
extern std::string g_custom_title_name;
extern std::unordered_map<std::string, ReplaceAsset> g_replace_assets;
extern std::string g_replace_assetbundle_file_path;

namespace {
// copy-pasted from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
Expand Down

0 comments on commit e042a38

Please sign in to comment.