mirror of
https://github.com/FWGS/xash3d-fwgs.git
synced 2026-08-05 03:24:56 +08:00
platform: ios: Remake launch dialog using UIAlertController
This commit is contained in:
@@ -13,41 +13,22 @@
|
|||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "SDL_syswm.h"
|
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
#import <AVFoundation/AVFoundation.h>
|
#import <AVFoundation/AVFoundation.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include "dlfcn.h"
|
|
||||||
|
|
||||||
#define XASHLIB "@rpath/libxash.dylib"
|
#define XASHLIB "@rpath/libxash.dylib"
|
||||||
|
|
||||||
|
|
||||||
@interface XashPromptAlertViewDelegate : NSObject <UIAlertViewDelegate>
|
|
||||||
|
|
||||||
@property (nonatomic, assign) int *button;
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation XashPromptAlertViewDelegate
|
|
||||||
|
|
||||||
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
|
|
||||||
*_button = buttonIndex;
|
|
||||||
}
|
|
||||||
@end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int szArgc;
|
int szArgc;
|
||||||
char **szArgv;
|
char **szArgv;
|
||||||
char *g_szLibrarySuffix;
|
|
||||||
float g_iOSVer;
|
float g_iOSVer;
|
||||||
bool isdark;
|
|
||||||
|
|
||||||
const char *IOS_GetDocsDir(void)
|
//UI vars
|
||||||
|
UIAlertController *alert;
|
||||||
|
UIViewController *controller;
|
||||||
|
bool finishedDialog = false;
|
||||||
|
|
||||||
|
const char *IOS_GetDocsDir( void )
|
||||||
{
|
{
|
||||||
if( g_iOSVer >= 8.0 )
|
|
||||||
{
|
|
||||||
static const char *dir = NULL;
|
static const char *dir = NULL;
|
||||||
|
|
||||||
if( dir )
|
if( dir )
|
||||||
@@ -61,23 +42,9 @@ const char *IOS_GetDocsDir(void)
|
|||||||
NSLog(@"IOS_GetDocsDir: %s", dir);
|
NSLog(@"IOS_GetDocsDir: %s", dir);
|
||||||
|
|
||||||
return dir;
|
return dir;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
static char dir[1024];
|
|
||||||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
||||||
NSString *basePath = paths.firstObject;
|
|
||||||
[[NSFileManager defaultManager] createDirectoryAtPath:basePath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
||||||
strcpy(dir,[basePath UTF8String]);
|
|
||||||
mkdir(dir,777);
|
|
||||||
|
|
||||||
NSLog(@"IOS_GetDocsDir: %s", dir);
|
|
||||||
|
|
||||||
return dir;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *IOS_GetExecDir(void)
|
const char *IOS_GetExecDir( void )
|
||||||
{
|
{
|
||||||
static const char *dir = NULL;
|
static const char *dir = NULL;
|
||||||
|
|
||||||
@@ -87,7 +54,7 @@ const char *IOS_GetExecDir(void)
|
|||||||
NSString *executableDirctory = [[NSBundle mainBundle] bundlePath];
|
NSString *executableDirctory = [[NSBundle mainBundle] bundlePath];
|
||||||
|
|
||||||
dir = [executableDirctory fileSystemRepresentation];
|
dir = [executableDirctory fileSystemRepresentation];
|
||||||
NSLog(@"IOS_GetDocsDir: %s", dir);
|
NSLog(@"IOS_GetExecDir: %s", dir);
|
||||||
|
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
@@ -99,43 +66,44 @@ typedef struct settings_s
|
|||||||
{
|
{
|
||||||
unsigned char magic;
|
unsigned char magic;
|
||||||
char args[1024];
|
char args[1024];
|
||||||
unsigned int port;
|
|
||||||
char suffix[32];
|
|
||||||
unsigned int ftpserver;
|
|
||||||
} settings_t;
|
} settings_t;
|
||||||
@interface ButtonHandler :NSObject
|
|
||||||
@property (nonatomic, assign) int *button1;
|
|
||||||
@end
|
|
||||||
@implementation ButtonHandler
|
|
||||||
|
|
||||||
|
settings_t settings;
|
||||||
|
FILE *settingsfile;
|
||||||
|
char settingspath[256];
|
||||||
|
|
||||||
-(void) buttonClicked:(UIButton*)sender
|
static void SaveSettings( void )
|
||||||
{
|
{
|
||||||
*_button1 = 0;
|
if( (settingsfile = fopen( settingspath, "wb" )) )
|
||||||
|
{
|
||||||
|
strlcpy( settings.args, [alert.textFields.firstObject.text UTF8String], 1024 );
|
||||||
|
settings.magic = 111;
|
||||||
|
|
||||||
|
fwrite( &settings, sizeof(settings), 1, settingsfile );
|
||||||
|
fclose( settingsfile );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@end
|
|
||||||
void IOS_PrepareView(void)
|
static void IOS_PrepareView( void )
|
||||||
{
|
{
|
||||||
ButtonHandler *handler = [[ButtonHandler alloc] init];
|
|
||||||
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
||||||
UIViewController *controller = [[UIViewController alloc] init];
|
controller = [[UIViewController alloc] init];
|
||||||
[[controller view] setBackgroundColor:[UIColor grayColor]];
|
[controller.view setBackgroundColor:[UIColor grayColor]];
|
||||||
[window setRootViewController:controller];
|
[window setRootViewController:controller];
|
||||||
[window makeKeyAndVisible];
|
[window makeKeyAndVisible];
|
||||||
if([[controller traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark && g_iOSVer >= 13.0) isdark = true; else isdark = false;
|
alert = [UIAlertController alertControllerWithTitle:@"Xash3D FWGS"
|
||||||
#if 0
|
message:nil
|
||||||
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
|
preferredStyle:UIAlertControllerStyleAlert];
|
||||||
int button1 = -1;
|
|
||||||
handler.button1 = &button1;
|
|
||||||
|
|
||||||
[button addTarget:handler action:@selector(buttonClicked:) forControlEvents:UIControlEventValueChanged];
|
UIAlertAction* startAction = [UIAlertAction actionWithTitle:@"Start" style:UIAlertActionStyleDefault
|
||||||
@autoreleasepool {
|
handler:^(UIAlertAction *action) { finishedDialog = true; }];
|
||||||
while( button1 == -1 ) {
|
UIAlertAction* exitAction = [UIAlertAction actionWithTitle:@"Exit" style:UIAlertActionStyleDefault
|
||||||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
handler:^(UIAlertAction *action) {
|
||||||
}
|
SaveSettings();
|
||||||
}
|
NSLog(@"Exit Selected\n");
|
||||||
[[controller view] addSubview:button];
|
exit(0); }];
|
||||||
#endif
|
[alert addAction:startAction];
|
||||||
|
[alert addAction:exitAction];
|
||||||
}
|
}
|
||||||
|
|
||||||
void IOS_LaunchDialog( void )
|
void IOS_LaunchDialog( void )
|
||||||
@@ -148,91 +116,48 @@ void IOS_LaunchDialog( void )
|
|||||||
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){}];
|
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){}];
|
||||||
|
|
||||||
IOS_PrepareView();
|
IOS_PrepareView();
|
||||||
int button = -1, bExit, bStart;
|
|
||||||
UIAlertView * alert = [[UIAlertView alloc] init];
|
|
||||||
bExit = [alert addButtonWithTitle:@"Exit"];
|
|
||||||
bStart = [alert addButtonWithTitle:@"Start"];
|
|
||||||
XashPromptAlertViewDelegate *delegate = [[XashPromptAlertViewDelegate alloc] init];
|
|
||||||
delegate.button = &button;
|
|
||||||
|
|
||||||
alert.delegate = delegate;
|
|
||||||
|
|
||||||
const char *docsDir = IOS_GetDocsDir();
|
const char *docsDir = IOS_GetDocsDir();
|
||||||
|
|
||||||
//set working directory to documents so logs can be generated there
|
//set working directory to documents so logs can be generated there
|
||||||
NSString *workingDir = [NSString stringWithUTF8String:IOS_GetDocsDir()];
|
[[NSFileManager defaultManager]
|
||||||
[[NSFileManager defaultManager] changeCurrentDirectoryPath:workingDir];
|
changeCurrentDirectoryPath:[NSString stringWithUTF8String:IOS_GetDocsDir()]];
|
||||||
|
|
||||||
FILE *settingsfile;
|
snprintf( settingspath, sizeof( settingspath ), "%s/settings.bin", docsDir );
|
||||||
char settingspath[256];
|
|
||||||
snprintf(settingspath, sizeof(settingspath), "%s/settings.bin", docsDir );
|
|
||||||
settingspath[255] = 0;
|
settingspath[255] = 0;
|
||||||
settings_t settings;
|
|
||||||
|
|
||||||
[alert setTransform:CGAffineTransformMakeTranslation(0,109)];
|
|
||||||
|
|
||||||
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
|
|
||||||
|
|
||||||
UILabel *argstitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
|
|
||||||
[argstitle setText:@"Command-line arguments:"];
|
|
||||||
|
|
||||||
UITextField *args = [[UITextField alloc] initWithFrame:CGRectMake(0, 30, 300, 30)];
|
|
||||||
[args setBackgroundColor:[[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1]];
|
|
||||||
if(isdark) [args setBackgroundColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1]];
|
|
||||||
|
|
||||||
|
|
||||||
UITextField *suffix = [[UITextField alloc] initWithFrame:CGRectMake(140, 90, 160, 30 )];
|
|
||||||
[suffix setBackgroundColor:[[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1]];
|
|
||||||
if(isdark) [suffix setBackgroundColor:[[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:1]];
|
|
||||||
|
|
||||||
UILabel *suffixtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 140, 30)];
|
|
||||||
[suffixtitle setText:@"Library suffix"];
|
|
||||||
|
|
||||||
[scroll addSubview:argstitle];
|
|
||||||
[scroll addSubview:args];
|
|
||||||
[scroll addSubview:suffix];
|
|
||||||
[scroll addSubview:suffixtitle];
|
|
||||||
|
|
||||||
settingsfile = fopen( settingspath, "rb" );
|
settingsfile = fopen( settingspath, "rb" );
|
||||||
if( settingsfile && ( fread(&settings, sizeof( settings ), 1, settingsfile ) == 1 ) && ( settings.magic == SETTINGS_MAGIC ) )
|
bool ret = false;
|
||||||
|
if ( settingsfile )
|
||||||
{
|
{
|
||||||
settings.args[1023] = 0;
|
ret = fread( &settings, sizeof( settings ), 1, settingsfile ) == 1;
|
||||||
settings.suffix[31] = 0;
|
|
||||||
[args setText:@(settings.args)];
|
|
||||||
[suffix setText:@(settings.suffix)];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
[args setText:@"-dev 2 -log"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scroll.contentSize=CGSizeMake(250, 200);
|
|
||||||
[alert setValue:scroll forKey:@"accessoryView"];
|
|
||||||
|
|
||||||
[alert show];
|
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
||||||
|
[textField setPlaceholder:@"Launch Options"];
|
||||||
|
if (ret && ( settings.magic == SETTINGS_MAGIC ))
|
||||||
|
{
|
||||||
|
[textField setText:@(settings.args)];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
[textField setText:@"-dev 2 -log"];
|
||||||
|
}
|
||||||
|
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
|
||||||
|
}];
|
||||||
|
|
||||||
|
[controller presentViewController:alert animated:YES completion:nil];
|
||||||
|
|
||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
while( button == -1 ) {
|
while( !finishedDialog ) {
|
||||||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( (settingsfile = fopen( settingspath, "wb" )) )
|
NSArray *argv = [alert.textFields.firstObject.text componentsSeparatedByString:@" "];
|
||||||
{
|
|
||||||
strlcpy(settings.args, [args.text UTF8String], 1024);
|
|
||||||
strlcpy(settings.suffix, [suffix.text UTF8String], 32 );
|
|
||||||
settings.magic = 111;
|
|
||||||
|
|
||||||
fwrite(&settings, sizeof(settings), 1, settingsfile);
|
|
||||||
fclose(settingsfile);
|
|
||||||
}
|
|
||||||
if( button == bExit )
|
|
||||||
{
|
|
||||||
printf("Exit selected\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
NSArray *argv = [ args.text componentsSeparatedByString:@" " ];
|
SaveSettings();
|
||||||
|
|
||||||
int count = [argv count];
|
int count = [argv count];
|
||||||
szArgv = calloc( count + 2, sizeof( char* ) );
|
szArgv = calloc( count + 2, sizeof( char* ) );
|
||||||
@@ -244,16 +169,6 @@ void IOS_LaunchDialog( void )
|
|||||||
szArgc = count + 1;
|
szArgc = count + 1;
|
||||||
szArgv[count + 1] = 0;
|
szArgv[count + 1] = 0;
|
||||||
|
|
||||||
if( [suffix.text length] )
|
|
||||||
g_szLibrarySuffix = strdup([suffix.text UTF8String]);
|
|
||||||
|
|
||||||
alert.delegate = nil;
|
|
||||||
|
|
||||||
[args release];
|
|
||||||
[argstitle release];
|
|
||||||
[suffix release];
|
|
||||||
[suffixtitle release];
|
|
||||||
|
|
||||||
[alert release];
|
[alert release];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,7 +181,7 @@ char *IOS_GetUDID( void )
|
|||||||
return udid;
|
return udid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IOS_Log(const char *text)
|
void IOS_Log( const char *text )
|
||||||
{
|
{
|
||||||
NSLog(@"Xash: %@", [NSString stringWithUTF8String:text]);
|
NSLog(@"Xash: %@", [NSString stringWithUTF8String:text]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user