Skip to content

startGameWithFilePath:options:didGameHandleCreated:completion:

启动游戏(通过本地文件路径)

该方法允许开发者通过指定的本地资源包(如 .zip 文件)正式启动游戏。SDK 会处理文件的校验、解压(如果尚未解压)、环境初始化,并最终交付游戏视图。这在离线分发游戏资源或使用自定义下载器的场景中非常有用。


方法声明

objectivec
/**
 Start the game using a local file path.
 @param pkgPath the path.
 @param options Configuration object containing the path, appGameID, and verification metadata.
 @param onCreated Called when the game handle is created. Use this to mount the view and register delegates.
 @param completion Called when the game has finished loading and is ready for interaction.
 @return An instance of id<SUDOPGameTask> to track the startup process.
 */
+ (id<SUDOPGameTask>)startGameWithPkgPath:(NSString *)pkgPath
                                  options:(SUDOPGamePackageOptions *)options
                     didGameHandleCreated:(nullable SUDOPDidGameHandleCreated)didGameHandleCreated
                               completion:(nullable SUDOPGameOperationCompletion)completion;

参数说明

参数名类型必传描述
pkgPathNSString *pkgPath (绝对路径)
optionsSUDOPGameFilePathOptions *配置对象。必须包含 appGameIDversion
didGameHandleCreatedBlock关键回调。在此处获取 gameHandle,并将其视图挂载到宿主 App 的 UI 层级。
completionBlock最终回调。当游戏首屏逻辑执行完毕,进入可交互状态时触发。

运行行为

  1. 资源校验:根据 options.fid 校验路径下的文件。若校验失败,则直接触发 completion 并携带错误信息。
  2. 解压与加载:将资源包解压至 SDK 内部的工作目录,并初始化 JavaScript 运行时(Runtime)。
  3. 视图激活:不同于 preStart,此接口会自动触发 UI 渲染流。开发者在 didGameHandleCreated 拿到的 gameHandle 可以直接调用 getGameView 获得可见的 UIView。

代码示例

objc
SUDOPGameFilePathOptions *options = [[SUDOPGameFilePathOptions alloc] init];
NSString * pkgPath = [[NSBundle mainBundle] pathForResource:@"game_bundle" ofType:@"zip"];
options.appGameID = @"custom_game_001";
options.version = @"1.0.2";
options.fid = @"md5_hash_of_the_file";
options.fidType = SUDOPFIDTypeMD5;

[SUDOP startGameWithPkgPath:pkgPath options:options 
    didGameHandleCreated:^(id<SUDRTGameHandle> gameHandle) {
        // 绑定业务代理
        [SUDOP registerWrappedClientWithGameHandle:gameHandle clientDelegate:self];
        
        // 获取并展示游戏视图
        UIView *gameView = [gameHandle getGameView];
        gameView.frame = self.view.bounds;
        [self.view addSubview:gameView];
    } 
    completion:^(id<SUDOPGameHandleProvider> _Nullable provider, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"本地资源包启动成功");
        } else {
            NSLog(@"启动失败: %@", error.localizedDescription);
        }
    }];

注意事项

  • 路径权限:iOS 环境下请确保该路径在沙盒可读范围内(Documents, Caches, 或 Bundle)。
  • 重复启动:如果已经有一个相同 appGameID 的游戏在运行,调用此接口的行为取决于 SDK 的实例管理策略(通常会先销毁旧实例)。
  • 线程安全didGameHandleCreatedcompletion 均在主线程回调,可直接进行 UI 操作。