Skip to content

Commit 421e5cd

Browse files
committed
Use the GameBox GTA mode picker with the iPadOS 16 engine
1 parent 6f8bec5 commit 421e5cd

3 files changed

Lines changed: 200 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--- a/dm/ios/Engines/GTAV/TonTonGTAV.m
2+
+++ b/dm/ios/Engines/GTAV/TonTonGTAV.m
3+
@@ -6,6 +6,8 @@
4+
#import "../../LibraryApp/Sources/LibraryRoot.h"
5+
#import "../../LibraryApp/Sources/GameSettings.h"
6+
#include "gpu_host.h"
7+
+#import <objc/message.h>
8+
+#include <dlfcn.h>
9+
static NSString *gtavSlot, *gtavData, *gtavShaders, *gtavUser;
10+
static BOOL gtavActive, gtavPickerOpen;
11+
static BOOL gtavFindResources(NSString *slot, NSString **data, NSString **shaders) {
12+
@@ -18,11 +20,38 @@
13+
![fm fileExistsAtPath:s isDirectory:&isDir] || !isDir)return NO;
14+
if(data)*data=d;if(shaders)*shaders=s;return YES;
15+
}
16+
+static BOOL gtavLegacyActive;
17+
+static NSString *gtavLegacyBinary(void) {
18+
+ return [NSBundle.mainBundle.privateFrameworksPath stringByAppendingPathComponent:@"TonTonGTAEmbedded.framework/TonTonGTAEmbedded"];
19+
+}
20+
+static BOOL gtavLegacyAvailable(NSString *slot) {
21+
+ if (!slot.length) return NO;
22+
+ NSString *manifest=[slot stringByAppendingPathComponent:@"UserData/GTA V/data/manifest.json"];
23+
+ NSFileManager *fm=NSFileManager.defaultManager;
24+
+ return [fm isReadableFileAtPath:manifest] && [fm isReadableFileAtPath:gtavLegacyBinary()];
25+
+}
26+
+static UIViewController *gtavLegacyController(NSString *slot, NSString *mode, NSDictionary *options,
27+
+ void (^returnToLibrary)(void)) {
28+
+ if (!gtavLegacyAvailable(slot)) return nil;
29+
+ static void *runtime;
30+
+ if (!runtime) runtime=dlopen(gtavLegacyBinary().fileSystemRepresentation, RTLD_NOW | RTLD_LOCAL);
31+
+ if (!runtime) { NSLog(@"TonTon GTA V compatibility engine failed to load: %s", dlerror()); return nil; }
32+
+ Class entry=NSClassFromString(@"TonTonGTAEmbeddedEntry");
33+
+ SEL action=NSSelectorFromString(@"controllerForSlot:mode:options:returnToLibrary:");
34+
+ if (![entry respondsToSelector:action]) { NSLog(@"TonTon GTA V compatibility entry missing"); return nil; }
35+
+ void (^back)(void)=^{
36+
+ gtavLegacyActive=NO;
37+
+ if(returnToLibrary) returnToLibrary();
38+
+ };
39+
+ UIViewController *controller=((UIViewController *(*)(id, SEL, NSString *, NSString *, NSDictionary *, void (^)(void)))objc_msgSend)(entry,action,slot,mode,options,back);
40+
+ gtavLegacyActive=controller!=nil;
41+
+ return controller;
42+
+}
43+
BOOL TonTonGTAVAvailable(NSString *slot) {
44+
if (@available(iOS 17.0,*))return gtavFindResources(slot,NULL,NULL);
45+
- return NO;
46+
-}
47+
-BOOL TonTonGTAVIsActive(void) { return gtavActive || gtavPickerOpen; }
48+
+ return gtavLegacyAvailable(slot);
49+
+}
50+
+BOOL TonTonGTAVIsActive(void) { return gtavActive || gtavPickerOpen || gtavLegacyActive; }
51+
void TonTonGTAVFocus(int focus, int suspend) { if (gtavActive) { host_input_focus(focus);gpu_suspend(suspend); } }
52+
// The iPhone app: a Metal-backed game view, touch controls that feed the engine's keyboard-and-mouse input block, and startup.
53+
// The engine, renderer and audio are in libgta.a (host/).
54+
@@ -293,6 +322,7 @@
55+
56+
@interface TonTonGTAVController : UIViewController
57+
@property(nonatomic,copy) void (^returnToLibraryHandler)(void);
58+
+@property(nonatomic,copy) NSString *compatibilitySlot;
59+
@end
60+
@implementation TonTonGTAVController {
61+
BOOL _started;
62+
@@ -431,6 +461,32 @@
63+
}
64+
65+
- (void)startGameWithViewSize:(CGSize)size {
66+
+ if (self.compatibilitySlot) {
67+
+ NSUserDefaults *prefs=NSUserDefaults.standardUserDefaults;
68+
+ NSString *global=[prefs stringForKey:@"tonton.global.DM_FRAME_LIMIT"];
69+
+ NSString *fps=[global isEqual:@"game"]?@"60":@"0";
70+
+ NSMutableDictionary *options=[NSMutableDictionary dictionary];
71+
+ for (NSDictionary *spec in TTGTASettingSpecs()) {
72+
+ NSString *key=spec[@"env"];
73+
+ options[key]=TTGTAValue(key,[key isEqual:@"GTA_FPS"]?fps:spec[@"default"]);
74+
+ }
75+
+ __weak typeof(self) weakSelf=self;
76+
+ UIViewController *game=gtavLegacyController(self.compatibilitySlot,_selectedMode,options,^{
77+
+ TonTonGTAVController *owner=weakSelf;
78+
+ void (^back)(void)=owner.returnToLibraryHandler;
79+
+ owner.returnToLibraryHandler=nil;
80+
+ if (back) back();
81+
+ });
82+
+ if (!game) { [self showMessage:@"无法打开 GTA5,请返回游戏匣后重试。" ]; return; }
83+
+ [self addChildViewController:game];
84+
+ game.view.frame=self.view.bounds;
85+
+ game.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
86+
+ [self.view addSubview:game.view];
87+
+ [game didMoveToParentViewController:self];
88+
+ [_modePanel removeFromSuperview];
89+
+ gtavPickerOpen=NO;
90+
+ return;
91+
+ }
92+
NSString* docs = [gtavSlot stringByAppendingPathComponent:@"UserData"];
93+
NSString* data = gtavData, *shaders = gtavShaders;
94+
NSString* user = gtavUser;
95+
@@ -533,7 +589,16 @@
96+
}
97+
98+
UIViewController *TonTonGTAVCreateControllerWithReturn(NSString *slot, void (^returnToLibrary)(void)) {
99+
- if(!TonTonGTAVAvailable(slot)||gtavActive||gtavPickerOpen)return nil;
100+
+ if(!TonTonGTAVAvailable(slot)||gtavActive||gtavPickerOpen||gtavLegacyActive)return nil;
101+
+ if (@available(iOS 17.0,*)) {
102+
+ // Keep the same launch path as Air on supported iPadOS versions.
103+
+ } else {
104+
+ TonTonGTAVController *controller=[TonTonGTAVController new];
105+
+ controller.compatibilitySlot=slot;
106+
+ controller.returnToLibraryHandler=returnToLibrary;
107+
+ gtavPickerOpen=YES;
108+
+ return controller;
109+
+ }
110+
NSString *data=nil,*shaders=nil;
111+
if(!gtavFindResources(slot,&data,&shaders))return nil;
112+
gtavSlot=[slot copy];gtavData=data;gtavShaders=shaders;

‎dm/ios/Engines/GTAV/EMBEDDING.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ The release-only GameBox touch-layout editor can be updated with
1919
`contrib/tonton/apply_per_title_touch_layout.py RELEASE_SOURCE_ROOT`
2020
against the version of the release source with `TouchLayoutEditor.h`.
2121
The library host must compile using the generated multi-title profile.
22+
23+
24+
For the release GameBox source with the existing Chinese mode picker,
25+
also apply `gta-v1.5.2-gamebox-picker.patch` after the embedded patch.
26+
Apply `contrib/tonton/gtav-release-shared-picker.patch` in the release
27+
GameBox source snapshot that already has `TonTonGTAVModeStorage.h` and
28+
the Air mode picker. Use these two optional overlays together; the
29+
older checked-in host still expects the base embedded entry point.
30+
The shared picker passes story or sandbox and the seven existing
31+
per-game graphics values to the embedded iPadOS 16 engine. It uses
32+
the same game directory and does not migrate or duplicate saves.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
--- a/app/App/TonTonEmbeddedEntry.swift
2+
+++ b/app/App/TonTonEmbeddedEntry.swift
3+
@@ -11,21 +11,24 @@
4+
final class TonTonGTAEmbeddedEntry: NSObject {
5+
private static var controller: NativePortViewController?
6+
7+
- @objc(controllerForSlot:returnToLibrary:)
8+
- static func controller(forSlot slot: String, returnToLibrary: @escaping () -> Void) -> UIViewController? {
9+
- guard Thread.isMainThread else { return nil }
10+
+ @objc(controllerForSlot:mode:options:returnToLibrary:)
11+
+ static func controller(forSlot slot: String, mode: String, options: [String: String],
12+
+ returnToLibrary: @escaping () -> Void) -> UIViewController? {
13+
+ guard Thread.isMainThread,
14+
+ let selected: GameMode = ["story": .story, "sandbox": .sandbox][mode] else { return nil }
15+
let documents = URL(fileURLWithPath: slot, isDirectory: true).appendingPathComponent("UserData", isDirectory: true)
16+
let manifest = documents.appendingPathComponent("GTA V/data/manifest.json")
17+
guard FileManager.default.isReadableFile(atPath: manifest.path) else { return nil }
18+
if let previous = controller {
19+
- previous.returnToGameBox = returnToLibrary
20+
- return previous
21+
+ if previous.startedInGameBox { return nil }
22+
+ previous.returnToGameBox = nil
23+
}
24+
TTGTASetDocuments(documents.path)
25+
GameSettings.applyExperimentalSwitches()
26+
ModKeyRemap.apply()
27+
let game = NativePortViewController()
28+
game.returnToGameBox = returnToLibrary
29+
+ game.configureGameBoxLaunch(mode: selected, options: options)
30+
controller = game
31+
return game
32+
}
33+
--- a/app/App/NativePortViewController.swift
34+
+++ b/app/App/NativePortViewController.swift
35+
@@ -190,6 +190,42 @@
36+
private let gyro = GyroInput()
37+
private var engineStarted = false
38+
var returnToGameBox: (() -> Void)?
39+
+ var startedInGameBox: Bool { engineStarted }
40+
+ private var launchFromGameBox = false
41+
+
42+
+ func configureGameBoxLaunch(mode: GameMode, options: [String: String]) {
43+
+ precondition(!isViewLoaded)
44+
+ let settings = GameSettings.shared
45+
+ settings.gameMode = mode
46+
+ settings.sandboxMap = 0
47+
+ if let value = options["GTA_FPS"].flatMap(Int.init), [0, 30, 60].contains(value) {
48+
+ settings.frameRateCap = value
49+
+ }
50+
+ if let value = options["GTA_HEIGHT"].flatMap(Int.init), [480, 540, 640, 720, 900, 1080].contains(value) {
51+
+ settings.renderHeight = value
52+
+ }
53+
+ let supported = ["GTA_TEXTURE_QUALITY": "textureQuality", "GTA_SHADOW_QUALITY": "shadowQuality",
54+
+ "GTA_REFLECTION_QUALITY": "reflectionQuality", "GTA_PARTICLE_QUALITY": "particleQuality",
55+
+ "GTA_GRASS_QUALITY": "grassQuality"]
56+
+ var overrides = settings.graphicsOverrides
57+
+ for (key, name) in supported {
58+
+ if let value = options[key], let spec = GraphicsDetail.all.first(where: { $0.option == name }),
59+
+ spec.values.contains(value) { overrides[name] = value }
60+
+ }
61+
+ settings.graphicsOverrides = overrides
62+
+ launchFromGameBox = true
63+
+ }
64+
+
65+
+ override func didMove(toParent parent: UIViewController?) {
66+
+ super.didMove(toParent: parent)
67+
+ guard parent != nil, launchFromGameBox else { return }
68+
+ DispatchQueue.main.async { [weak self] in
69+
+ guard let self, self.parent != nil, self.launchFromGameBox else { return }
70+
+ self.launchFromGameBox = false
71+
+ self.view.layoutIfNeeded()
72+
+ self.startEngine()
73+
+ }
74+
+ }
75+
/// The app was in the background at least once since it opened (the log's "App back in the foreground.").
76+
private var wasInBackground = false
77+
/// leftApp ran for the current time away (reset when the app is active again).

0 commit comments

Comments
 (0)