Skip to content

Commit 72604c2

Browse files
committed
hook AlphaBlendCamera to make FesLIVE lightweight
1 parent 0445511 commit 72604c2

File tree

1 file changed

+67
-28
lines changed

1 file changed

+67
-28
lines changed

Tweak.xm

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,35 @@ static MSHookFunction_t MSHookFunction_p = NULL;
2424
typedef void (*MSHookMessageEx_t)(Class _class, SEL message, IMP hook, IMP *old);
2525
static MSHookMessageEx_t MSHookMessageEx_p = NULL;
2626

27+
static NSMutableDictionary *qualityConfig = nil;
2728
struct Resolution { int width; int height; int refreshRate; };
2829
enum LiveAreaQuality { Low, Medium, High };
2930
enum LiveScreenOrientation { Landscape, Portrait };
30-
static NSMutableDictionary *qualityConfig = nil;
31+
32+
IL2CPP::CClass* get_SaveData() {
33+
void* instance = IL2CPP::Class::Utils::GetStaticField(
34+
IL2CPP::Class::Find("Global"),
35+
"instance"
36+
);
37+
IL2CPP::CClass* pInstance = reinterpret_cast<IL2CPP::CClass*>(instance);
38+
return pInstance->GetPropertyValue<IL2CPP::CClass*>("SaveData");
39+
}
40+
41+
LiveAreaQuality get_RenderTextureQuality() {
42+
IL2CPP::CClass* saveData = get_SaveData();
43+
return saveData->GetPropertyValue<LiveAreaQuality>("RenderTextureQuality");
44+
}
45+
46+
void set_RenderTextureQuality(LiveAreaQuality quality) {
47+
IL2CPP::CClass* saveData = get_SaveData();
48+
saveData->SetPropertyValue<LiveAreaQuality>("RenderTextureQuality", quality);
49+
}
50+
51+
static bool inAlphaBlend = false;
52+
static const float alphaBlendFactor = 2.0f/3.0f;
3153

3254
typedef Resolution (*OriginalGetResolution_t)(LiveAreaQuality quality, LiveScreenOrientation orientation);
3355
static OriginalGetResolution_t Original_GetResolution = nullptr;
34-
3556
Resolution Hooked_GetResolution(LiveAreaQuality quality, LiveScreenOrientation orientation)
3657
{
3758
Resolution customRes;
@@ -49,6 +70,10 @@ Resolution Hooked_GetResolution(LiveAreaQuality quality, LiveScreenOrientation o
4970
shortSide = [qualityConfig[@"LiveStream.Quality.Low.ShortSide"] intValue];
5071
longSide = floor(shortSide / 9.0f * 16.0f);
5172
}
73+
if (inAlphaBlend) {
74+
longSide = floor(longSide * alphaBlendFactor);
75+
shortSide = floor(shortSide * alphaBlendFactor);
76+
}
5277
switch (orientation) {
5378
case Landscape:
5479
customRes.width = longSide;
@@ -63,6 +88,19 @@ Resolution Hooked_GetResolution(LiveAreaQuality quality, LiveScreenOrientation o
6388
// return Original_GetResolution(quality, orientation);
6489
}
6590

91+
// Inspix.AlphaBlendCamera.UpdateAlpha(float newAlpha)
92+
void (*original_AlphaBlendCamera_UpdateAlpha)(void *self, float newAlpha);
93+
void hooked_AlphaBlendCamera_UpdateAlpha(void *self, float newAlpha) {
94+
if (newAlpha < 1.0f && !inAlphaBlend) {
95+
inAlphaBlend = true;
96+
set_RenderTextureQuality(get_RenderTextureQuality());
97+
} else if (newAlpha > 0.99f && inAlphaBlend) {
98+
inAlphaBlend = false;
99+
set_RenderTextureQuality(get_RenderTextureQuality());
100+
}
101+
original_AlphaBlendCamera_UpdateAlpha(self, newAlpha);
102+
}
103+
66104
NSString *getDylibDirectoryPath() {
67105
Dl_info info;
68106

@@ -166,25 +204,6 @@ void Hooked_setFocusArea(void *self) {
166204
pSelf->SetMemberValue<Unity::Vector3>("focusAreaMinValue", focusAreaMinValue);
167205
}
168206

169-
IL2CPP::CClass* get_SaveData() {
170-
void* instance = IL2CPP::Class::Utils::GetStaticField(
171-
IL2CPP::Class::Find("Global"),
172-
"instance"
173-
);
174-
IL2CPP::CClass* pInstance = reinterpret_cast<IL2CPP::CClass*>(instance);
175-
return pInstance->GetPropertyValue<IL2CPP::CClass*>("SaveData");
176-
}
177-
178-
LiveAreaQuality get_RenderTextureQuality() {
179-
IL2CPP::CClass* saveData = get_SaveData();
180-
return saveData->GetPropertyValue<LiveAreaQuality>("RenderTextureQuality");
181-
}
182-
183-
void set_RenderTextureQuality(LiveAreaQuality quality) {
184-
IL2CPP::CClass* saveData = get_SaveData();
185-
saveData->SetPropertyValue<LiveAreaQuality>("RenderTextureQuality", quality);
186-
}
187-
188207
struct RenderTextureDescriptor {
189208
int width;
190209
int height;
@@ -208,7 +227,7 @@ RenderTextureDescriptor Hooked_CreateRenderTextureDescriptor(Unity::CCamera* cam
208227
{
209228
NSString* nsCameraName = camera->GetName()->ToNSString();
210229

211-
if (nsCameraName || [nsCameraName hasPrefix:@"StoryCamera"]) {
230+
if (nsCameraName && [nsCameraName hasPrefix:@"StoryCamera"]) {
212231
IL2CPP::CClass* targetTexture = camera->GetPropertyValue<IL2CPP::CClass*>("targetTexture");
213232
if (targetTexture) {
214233
int width = targetTexture->GetPropertyValue<int>("width");
@@ -343,23 +362,27 @@ void hooked_FesLiveSettingsView_InitButtons(void* self) {
343362
// original_FesLiveSettingsView_InitButtons(self);
344363
IL2CPP::CClass* pSelf = reinterpret_cast<IL2CPP::CClass*>(self);
345364

346-
pSelf->CallMethodSafe<void, IL2CPP::CClass*, int>("RadioButtonToQualitySettings", pSelf->GetMemberValue<IL2CPP::CClass*>("qualityLowRadioButton"), 0);
347-
pSelf->CallMethodSafe<void, IL2CPP::CClass*, int>("RadioButtonToQualitySettings", pSelf->GetMemberValue<IL2CPP::CClass*>("qualityMiddleRadioButton"), 1);
348-
pSelf->CallMethodSafe<void, IL2CPP::CClass*, int>("RadioButtonToQualitySettings", pSelf->GetMemberValue<IL2CPP::CClass*>("qualityHighRadioButton"), 2);
365+
IL2CPP::CClass* qualityLowRadioButton = pSelf->GetMemberValue<IL2CPP::CClass*>("qualityLowRadioButton");
366+
IL2CPP::CClass* qualityMiddleRadioButton = pSelf->GetMemberValue<IL2CPP::CClass*>("qualityMiddleRadioButton");
367+
IL2CPP::CClass* qualityHighRadioButton = pSelf->GetMemberValue<IL2CPP::CClass*>("qualityHighRadioButton");
368+
369+
pSelf->CallMethodSafe<void, IL2CPP::CClass*, int>("RadioButtonToQualitySettings", qualityLowRadioButton, 0);
370+
pSelf->CallMethodSafe<void, IL2CPP::CClass*, int>("RadioButtonToQualitySettings", qualityMiddleRadioButton, 1);
371+
pSelf->CallMethodSafe<void, IL2CPP::CClass*, int>("RadioButtonToQualitySettings", qualityHighRadioButton, 2);
349372

350-
pSelf->GetMemberValue<IL2CPP::CClass*>("qualityLowRadioButton")->GetMemberValue<IL2CPP::CClass*>("label")->SetPropertyValue<Unity::System_String*>("text", IL2CPP::String::New(
373+
qualityLowRadioButton->GetMemberValue<IL2CPP::CClass*>("label")->SetPropertyValue<Unity::System_String*>("text", IL2CPP::String::New(
351374
[[NSString stringWithFormat:@"%dp\n%.2fx",
352375
[qualityConfig[@"LiveStream.Quality.Low.ShortSide"] intValue],
353376
[qualityConfig[@"Story.Quality.Low.Factor"] floatValue]
354377
] UTF8String]
355378
));
356-
pSelf->GetMemberValue<IL2CPP::CClass*>("qualityMiddleRadioButton")->GetMemberValue<IL2CPP::CClass*>("label")->SetPropertyValue<Unity::System_String*>("text", IL2CPP::String::New(
379+
qualityMiddleRadioButton->GetMemberValue<IL2CPP::CClass*>("label")->SetPropertyValue<Unity::System_String*>("text", IL2CPP::String::New(
357380
[[NSString stringWithFormat:@"%dp\n%.2fx",
358381
[qualityConfig[@"LiveStream.Quality.Medium.ShortSide"] intValue],
359382
[qualityConfig[@"Story.Quality.Medium.Factor"] floatValue]
360383
] UTF8String]
361384
));
362-
pSelf->GetMemberValue<IL2CPP::CClass*>("qualityHighRadioButton")->GetMemberValue<IL2CPP::CClass*>("label")->SetPropertyValue<Unity::System_String*>("text", IL2CPP::String::New(
385+
qualityHighRadioButton->GetMemberValue<IL2CPP::CClass*>("label")->SetPropertyValue<Unity::System_String*>("text", IL2CPP::String::New(
363386
[[NSString stringWithFormat:@"%dp\n%.2fx",
364387
[qualityConfig[@"LiveStream.Quality.High.ShortSide"] intValue],
365388
[qualityConfig[@"Story.Quality.High.Factor"] floatValue]
@@ -635,6 +658,22 @@ BOOL hooked_didFinishLaunchingWithOptions(id self, SEL _cmd, UIApplication *appl
635658
);
636659

637660
NSLog(@"[IL2CPP Tweak] Successfully hooked GetResolution!");
661+
662+
// Inspix.AlphaBlendCamera.UpdateAlpha(float newAlpha)
663+
targetAddress = IL2CPP::Class::Utils::GetMethodPointer(
664+
"Inspix.AlphaBlendCamera",
665+
"UpdateAlpha",
666+
1
667+
);
668+
669+
if (targetAddress) {
670+
MSHookFunction_p(
671+
targetAddress,
672+
(void*)hooked_AlphaBlendCamera_UpdateAlpha,
673+
(void**)&original_AlphaBlendCamera_UpdateAlpha
674+
);
675+
NSLog(@"[IL2CPP Tweak] AlphaBlendCamera::UpdateAlpha hooked");
676+
}
638677
}
639678

640679
targetAddress = IL2CPP::Class::Utils::GetMethodPointer(

0 commit comments

Comments
 (0)