Skip to content

Commit

Permalink
Merge pull request #402 from ToddLa/swift-GameInfo
Browse files Browse the repository at this point in the history
Swift GameInfoCell, and GameInfo class
  • Loading branch information
yoshisuga authored May 29, 2022
2 parents 6216c92 + 2c7c6c0 commit baba40f
Show file tree
Hide file tree
Showing 20 changed files with 1,396 additions and 1,141 deletions.
30 changes: 10 additions & 20 deletions iOS/Bootstrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// check UIApplicationLaunchOptionsURLKey to see if we were launched with a game URL, and set that as the game to restore.
NSURL* url = launchOptions[UIApplicationLaunchOptionsURLKey];
if ([url isKindOfClass:[NSURL class]]) {
// handle our own scheme mame4ios://name
if ([url.scheme isEqualToString:@"mame4ios"] && [url.host length] != 0 && [url.path length] == 0 && [url.query length] == 0) {
[EmulatorController setCurrentGame:@{@"name":url.host}];

// handle our own url scheme mame4ios://
GameInfo* game = [[GameInfo alloc] initWithURL:url];
if (game != nil) {
[EmulatorController setCurrentGame:game];
result = FALSE;
}
}
Expand Down Expand Up @@ -234,23 +236,10 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction
{
NSLog(@"OPEN URL: %@ %@", url, options);

GameInfo* game = [[GameInfo alloc] initWithURL:url];

// handle our own scheme mame4ios://game OR mame4ios://system/game OR mame4ios://system/type:file
if ([url.scheme isEqualToString:@"mame4ios"] && [url.host length] != 0 && [url.query length] == 0) {
NSDictionary* game;

NSString* path = url.path;
if ([path hasPrefix:@"/"])
path = [path substringFromIndex:1];

NSArray* arr = [path componentsSeparatedByString:@":"];

if (arr.count == 2)
game = @{kGameInfoSystem:url.host, kGameInfoMediaType:arr.firstObject, kGameInfoFile:arr.lastObject};
else if ([path length] != 0)
game = @{kGameInfoSystem:url.host, kGameInfoName:path};
else
game = @{kGameInfoName:url.host};

if (game != nil) {
[hrViewController performSelectorOnMainThread:@selector(playGame:) withObject:game waitUntilDone:NO];
return TRUE;
}
Expand Down Expand Up @@ -321,7 +310,8 @@ - (BOOL)performActivity:(NSString*)activityType userInfo:(NSDictionary*)info {

if ([cmd isEqualToString:@"play"])
{
[hrViewController performSelectorOnMainThread:@selector(playGame:) withObject:info waitUntilDone:NO];
GameInfo* game = [[GameInfo alloc] initWithDictionary:info];
[hrViewController performSelectorOnMainThread:@selector(playGame:) withObject:game waitUntilDone:NO];
return TRUE;
}

Expand Down
10 changes: 5 additions & 5 deletions iOS/EmulatorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#import <UIKit/UIKit.h>

#import "Globals.h"
#import "GameInfo.h"
#import "ScreenView.h"
#import "MetalScreenView.h"
#import "GCDWebUploader.h"
Expand Down Expand Up @@ -117,9 +118,8 @@ extern NSArray* g_import_file_types;

@property (class,readonly,nonatomic,strong) EmulatorController* sharedInstance;

+ (NSArray<NSString*>*)romList;
+ (NSDictionary*)getCurrentGame;
+ (void)setCurrentGame:(NSDictionary*)game;
+ (GameInfo*)getCurrentGame;
+ (void)setCurrentGame:(GameInfo*)game;

#if TARGET_OS_IOS
// editing interface used by LayoutView
Expand Down Expand Up @@ -161,8 +161,8 @@ extern NSArray* g_import_file_types;

+ (NSArray<NSString*>*)getROMS;
- (void)moveROMS;
- (void)playGame:(NSDictionary*)game;
- (void)chooseGame:(NSArray*)games;
- (void)playGame:(GameInfo*)game;
- (void)chooseGame:(NSArray<GameInfo*>*)games;
- (void)reload;

#if TARGET_OS_IOS
Expand Down
97 changes: 56 additions & 41 deletions iOS/EmulatorController.m
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ -(void)toggleFullScreen:(id)sender;
#define kHUDPositionPortKey @"hud_rect_port"
#define kHUDScalePortKey @"hud_scale_port"
#define kSelectedGameInfoKey @"selected_game_info"
static GameInfoDictionary* g_mame_game_info;
static GameInfo* g_mame_game_info;
static BOOL g_mame_reset = FALSE; // do a full reset (delete cfg files) before running MAME
static char g_mame_system[64]; // system MAME should run
static char g_mame_type[16]; // game type (-cart, -flop, ...) or empty
Expand Down Expand Up @@ -543,14 +543,16 @@ static void check_pause()
}

// setup the globals the MAME thread uses to run the next game, or pass nil to run without params (aka main menu)
void set_mame_globals(GameInfoDictionary* game)
void set_mame_globals(GameInfo* game)
{
if (game != nil)
{
// please only call this on main-thread
NSCParameterAssert(NSThread.isMainThread);

NSString* name = game[kGameInfoFile] ?: game[kGameInfoName] ?: @"";
NSString* name = game.gameName;
if (game.gameFile.length != 0)
name = game.gameFile;
if ([name isEqualToString:kGameInfoNameMameMenu])
name = @" ";
strncpy(g_mame_game, name.UTF8String, sizeof(g_mame_game));
Expand Down Expand Up @@ -733,29 +735,40 @@ void m4i_game_list(myosd_game_info* game_info, int game_count)
NSString* type = types[game_info[i].type];
if ((game_info[i].flags & MYOSD_GAME_INFO_BIOS) && myosd_get(MYOSD_VERSION) == 139)
type = kGameInfoTypeBIOS;

NSString* parent = @(game_info[i].parent ?: "");
if ([parent isEqualToString:@"0"])
parent = @"";

NSString* year = @(game_info[i].year ?: "");
if ([year isEqualToString:@"0"])
year = @"";

NSDictionary* game = @{
GameInfo* game = [[GameInfo alloc] initWithDictionary:@{
kGameInfoType: type,
kGameInfoName: @(game_info[i].name),
kGameInfoDescription: @(game_info[i].description),
kGameInfoYear: @(game_info[i].year),
kGameInfoParent: @(game_info[i].parent ?: ""),
kGameInfoYear: year,
kGameInfoParent: parent,
kGameInfoManufacturer:@(game_info[i].manufacturer),
kGameInfoCategory: find_category(@(game_info[i].name), @(game_info[i].parent ?: "")),
kGameInfoCategory: find_category(@(game_info[i].name), parent),
kGameInfoDriver: [@(game_info[i].source_file ?: "").lastPathComponent stringByDeletingPathExtension],
kGameInfoSoftwareMedia:software_list,
kGameInfoScreen: screens[(game_info[i].flags & MYOSD_GAME_INFO_VERTICAL) ? 1 : 0 +
(game_info[i].flags & MYOSD_GAME_INFO_VECTOR) ? 2 : 0 +
(game_info[i].flags & MYOSD_GAME_INFO_LCD) ? 4 : 0 ]
};

}];

#ifdef DEBUG
XGameInfo* xgame = [[XGameInfo alloc] initWithDictionary:game.gameDictionary];
#endif
NSArray* software = @[];
if (software_list.length != 0)
{
software = [g_softlist getGamesForSystem:game] ?: @[];

if (g_pref_filter_clones)
software = [software filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K == ''", kGameInfoParent]];
software = [software filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"gameParent == ''"]];
}

[games addObject:game];
Expand All @@ -765,14 +778,14 @@ void m4i_game_list(myosd_game_info* game_info, int game_count)
NSString* mame_version = [@((const char *)myosd_get(MYOSD_VERSION_STRING) ?: "") componentsSeparatedByString:@" ("].firstObject;

// add a *special* system game that will run the DOS MAME menu.
[games addObject:@{
[games addObject:[[GameInfo alloc] initWithDictionary:@{
kGameInfoType:kGameInfoTypeComputer,
kGameInfoName:kGameInfoNameMameMenu,
kGameInfoParent:@"",
kGameInfoDescription:[NSString stringWithFormat:@"MAME %@", mame_version],
kGameInfoYear:@"1996",
kGameInfoManufacturer:@"MAMEDev and contributors",
}];
}]];

// give the list to the main thread to display to user
[sharedInstance performSelectorOnMainThread:@selector(chooseGame:) withObject:games waitUntilDone:FALSE];
Expand Down Expand Up @@ -865,16 +878,13 @@ - (void)setButtonRect:(int)i rect:(CGRect)rect {

#endif

+ (NSArray*)romList {
return [g_category_dict allKeys];
}

+ (void)setCurrentGame:(NSDictionary*)game {
[[NSUserDefaults standardUserDefaults] setObject:(game ?: @{}) forKey:kSelectedGameInfoKey];
+ (void)setCurrentGame:(GameInfo*)game {
[[NSUserDefaults standardUserDefaults] setObject:(game.gameDictionary ?: @{}) forKey:kSelectedGameInfoKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (NSDictionary*)getCurrentGame {
return [[NSUserDefaults standardUserDefaults] objectForKey:kSelectedGameInfoKey] ?: @{};
+ (GameInfo*)getCurrentGame {
NSDictionary* info = [[NSUserDefaults standardUserDefaults] objectForKey:kSelectedGameInfoKey] ?: @{};
return [[GameInfo alloc] initWithDictionary:info];
}

+ (EmulatorController*)sharedInstance {
Expand Down Expand Up @@ -1441,7 +1451,7 @@ - (void)endMenu{
[self updatePointerLocked];
}

- (void)runAddROMS {
- (void)runAddROMS:(id)from {
NSString* title = g_no_roms_found ? @"Welcome to " PRODUCT_NAME_LONG : @"Add ROMs";
#if TARGET_OS_TV
NSString* message = @"To transfer ROMs from your computer, Start Web Server or Import ROMs.";
Expand All @@ -1450,9 +1460,6 @@ - (void)runAddROMS {
#endif

UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Start Web Server" symbol:@"arrow.up.arrow.down.circle" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) {
[self runServer];
}]];
#if TARGET_OS_IOS
[alert addAction:[UIAlertAction actionWithTitle:@"Import ROMs" symbol:@"square.and.arrow.down" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) {
[self runImport];
Expand All @@ -1469,6 +1476,9 @@ - (void)runAddROMS {
[self runShowFiles];
}]];
#endif
[alert addAction:[UIAlertAction actionWithTitle:@"Start Web Server" symbol:@"arrow.up.arrow.down.circle" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) {
[self runServer];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Reload ROMs" symbol:@"arrow.2.circlepath.circle" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self reload]; /* exit mame menu and re-scan ROMs*/
}]];
Expand All @@ -1477,6 +1487,14 @@ - (void)runAddROMS {
if (self.presentedViewController == nil)
[self reload]; /* exit mame menu and re-scan ROMs*/
}]];
#if TARGET_OS_IOS
if ([from isKindOfClass:[UIView class]]) {
UIView* view = from;
alert.modalPresentationStyle = UIModalPresentationPopover;
alert.popoverPresentationController.sourceView = view;
alert.popoverPresentationController.sourceRect = view.bounds;
}
#endif
[self.topViewController presentViewController:alert animated:YES completion:nil];
}

Expand Down Expand Up @@ -1911,7 +1929,7 @@ - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

if (g_mame_game_info.gameName.length != 0 && !g_mame_game_info.gameIsFake)
if (g_mame_game_info.gameName.length != 0)
[self updateUserActivity:g_mame_game_info];

[self scanForDevices];
Expand Down Expand Up @@ -2324,7 +2342,7 @@ -(void)buildHUD {

if (g_pref_showHUD == HudSizeInfo) {
// add game info
if (g_mame_game_info != nil && g_mame_game_info[kGameInfoName] != nil)
if (g_mame_game_info != nil && g_mame_game_info.gameName.length != 0)
[hudViewController addAttributedText:[ChooseGameController getGameText:g_mame_game_info]];

// add FPS display
Expand Down Expand Up @@ -4863,7 +4881,7 @@ -(void)moveROMS {
// TODO: 7z for artwork and samples?
// TODO: we specificaly *DONT* save CHDs
// "hi" is the 139 dir, and "hiscore" is the 2xx dir
NSArray* paths = @[@"roms/%@.zip", @"roms/%@.7z", @"artwork/%@.zip", @"samples/%@.zip", @"titles/%@.png", @"cfg/%@.cfg", @"ini/%@.ini", @"sta/%@/1.sta", @"sta/%@/2.sta", @"hi/%@.hi", @"hiscore/%@.hi"];
NSArray* paths = @[@"roms/%@.zip", @"roms/%@.7z", @"roms/%@.json", @"artwork/%@.zip", @"samples/%@.zip", @"titles/%@.png", @"cfg/%@.cfg", @"ini/%@.ini", @"sta/%@/1.sta", @"sta/%@/2.sta", @"hi/%@.hi", @"hiscore/%@.hi"];

for (NSString* path in paths) {
NSString* file = [NSString stringWithFormat:path, rom.stringByDeletingPathExtension];
Expand Down Expand Up @@ -5887,7 +5905,7 @@ - (void)webServerShowAlert:(GCDWebServer*)server {
// NOTE we cant run a game in all situations, for example if the user is deep
// into the Settings dialog, we just give up, to complex to try to back out.
//
-(void)playGame:(NSDictionary*)game {
-(void)playGame:(GameInfo*)game {
NSLog(@"PLAY: %@", game);

// if we are not presenting anything, we can just "run" the game
Expand Down Expand Up @@ -6022,7 +6040,7 @@ -(void)chooseGame:(NSArray*)games {
}

change_pause(PAUSE_INPUT);
[self runAddROMS];
[self runAddROMS:nil];
return;
}
if (g_mame_game_error[0] != 0) {
Expand Down Expand Up @@ -6069,17 +6087,7 @@ -(void)chooseGame:(NSArray*)games {
choose.hideConsoles = g_pref_filter_bios;
[choose setGameList:games];
change_pause(PAUSE_INPUT);
choose.selectGameCallback = ^(NSDictionary* game) {
if ([game[kGameInfoName] isEqualToString:kGameInfoNameSettings]) {
[self runSettings];
return;
}

if ([game[kGameInfoName] isEqualToString:kGameInfoNameAddROMS]) {
[self runAddROMS];
return;
}

choose.selectGameCallback = ^(GameInfo* game) {
if (self.presentedViewController.isBeingDismissed)
return;

Expand All @@ -6089,6 +6097,13 @@ -(void)chooseGame:(NSArray*)games {
[self performSelectorOnMainThread:@selector(playGame:) withObject:game waitUntilDone:FALSE];
}];
};
choose.settingsCallback = ^(id from) {
[self runSettings];
};
choose.romsCallback = ^(id from) {
[self runAddROMS:from];
};

UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:choose];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
if (@available(iOS 13.0, tvOS 13.0, *)) {
Expand Down Expand Up @@ -6155,7 +6170,7 @@ - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)eve

#pragma mark NSUserActivty

-(void)updateUserActivity:(NSDictionary*)game
-(void)updateUserActivity:(GameInfo*)game
{
#if TARGET_OS_IOS
if (game != nil)
Expand Down
2 changes: 1 addition & 1 deletion iOS/MetalScreenView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ - (void)drawScreenDebug:(void*)prim_list {
if (orient == MYOSD_ORIENTATION_SWAP_XY)
assert(TRUE);
if (orient == (MYOSD_ORIENTATION_SWAP_XY | MYOSD_ORIENTATION_FLIP_X | MYOSD_ORIENTATION_FLIP_Y))
assert(FALSE);
assert(TRUE);

if (wrap == 0)
assert(TRUE);
Expand Down
15 changes: 11 additions & 4 deletions xcode/MAME4iOS/ChooseGameController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@
#ifndef ChooseGameController_h
#define ChooseGameController_h

NS_ASSUME_NONNULL_BEGIN

@interface ChooseGameController : UICollectionViewController

- (void)setGameList:(NSArray<GameInfoDictionary*>*)games;
- (void)setGameList:(NSArray<GameInfo*>*)games;
+ (void)reset;

#if TARGET_OS_IOS
+ (NSUserActivity*)userActivityForGame:(GameInfoDictionary*)game;
+ (NSUserActivity*)userActivityForGame:(GameInfo*)game;
#endif

@property(nonatomic, strong) void (^selectGameCallback)(GameInfoDictionary* info);
@property(nonatomic, strong) void (^selectGameCallback)(GameInfo* info);
@property(nonatomic, strong) void (^settingsCallback)(id from);
@property(nonatomic, strong) void (^romsCallback)(id from);

@property(nonatomic, strong) UIImage* backgroundImage;
@property(nonatomic, assign) BOOL hideConsoles;

+(NSAttributedString*)getGameText:(GameInfoDictionary*)game;
+(NSAttributedString*)getGameText:(GameInfo*)game;
+(UIImage*)getGameIcon:(GameInfo*)game;

@end

NS_ASSUME_NONNULL_END

#endif /* ChooseGameController_h */
Loading

0 comments on commit baba40f

Please sign in to comment.