<commit_before>#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIApplication.h>

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include "iphone.h"
#include "input.h"
#include "base/log.h"
#include "base/thread.h"

#import "game/iphone/wiviewcontroller.h"
#import "game/iphone/chatviewcontroller.h"
#include "game/ht.h"

@interface IPhoneAppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *m_window;
    WiViewController *m_vcwi;
    ChatViewController *m_vcchat;
    wi::IChatController *m_pchat;
    id m_view;
    base::Thread *m_game_thread;
    bool m_fExiting;
    char *m_pszMissionPacksDir;
    char *m_pszMissionPackInfosDir;
    char *m_pszSaveGamesDir;
    char *m_pszPrefsFilename;
    char *m_pszMainDataDir;
    char *m_pszTempDir;
    char *m_pszCompletesDir;
    char *m_pszUUID;
}
@end

namespace wi {
IPhoneAppDelegate *g_appDelegate;
}

@implementation IPhoneAppDelegate

+ (void)initialize
{
}

- (void)allocPath:(char **)ppsz baseDir:(const char *)baseDir
        subDir:(const char *)subDir
{
    int cb = (int)(strlen(baseDir) + strlen(subDir) + 1);
    char *psz = (char *)malloc(cb);
    strcpy(psz, baseDir);
    strcat(psz, subDir);
    *ppsz = psz;
}

- (void)allocPaths
{
    // .app -> main app directory
    // .app/htdata832.pdb, htsfx.pdb -> main data files
    // Library/MissionPacks/ -> downloaded mission packs
    // Library/SaveGames/ -> saved games
    // Library/prefs.bin -> game prefs
    // NSTemporaryDirectory() -> temp dir

    // This returns the home directory; .app, Library, and Documents are in it
    NSString *homeDir = NSHomeDirectory();
    const char *pszHomeDir = [homeDir cStringUsingEncoding:
            [NSString defaultCStringEncoding]];
    NSString *tempDir = NSTemporaryDirectory();
    const char *pszTempDir = [tempDir cStringUsingEncoding:
            [NSString defaultCStringEncoding]];

    [self allocPath:&m_pszMissionPacksDir baseDir:pszHomeDir
            subDir:"/Library/MissionPacks"];
    [self allocPath:&m_pszMissionPackInfosDir baseDir:pszHomeDir
            subDir:"/Library/MissionPackInfos"];
    [self allocPath:&m_pszSaveGamesDir baseDir:pszHomeDir
            subDir:"/Library/SaveGames"];
    [self allocPath:&m_pszPrefsFilename baseDir:pszHomeDir
            subDir:"/Library/prefs.bin"];
    [self allocPath:&m_pszTempDir baseDir:pszTempDir subDir:""];
    [self allocPath:&m_pszCompletesDir baseDir:pszHomeDir
            subDir:"/Library/Completes"];

    // Make the directories under Library
    mkdir(m_pszMissionPacksDir, 0755);
    mkdir(m_pszMissionPackInfosDir, 0755);
    mkdir(m_pszSaveGamesDir, 0755);
    mkdir(m_pszCompletesDir, 0755);

    // Get directories off .app
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *appDir = [bundle bundlePath];
    const char *pszAppDir = [appDir cStringUsingEncoding:
            [NSString defaultCStringEncoding]];

    [self allocPath:&m_pszMainDataDir baseDir:pszAppDir
            subDir:""];

#if 0
    NSArray *pathsLibrary = NSSearchPathForDirectoriesInDomains(
            NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [pathsLibray objectAtIndex:0];
#endif
}

- (void)initiateAsk:(const char *)title max:(int)max default:(const char *)def
        keyboard:(int)keyboard secure:(BOOL)secure
{
    return [m_vcwi initiateAsk:title max:max default:def keyboard:keyboard
            secure:secure];
}

- (void)getAskString:(char *)psz size:(int)cb
{
    return [m_vcwi getAskString:psz size:cb];
}

- (wi::IChatController *)getChatController
{
    if (m_pchat == NULL) {
        m_pchat = new wi::ChatController(m_vcwi, m_vcchat);
    }
    return m_pchat;
}

- (void)initiateWebView:(const char *)title withUrl:(const char *)url
{
    [m_vcwi initiateWebView:title withUrl:url];
}

#if 0
- (void)application:(UIApplication *)app
        willChangeStatusBarOrientation:(UIInterfaceOrientation)orientation
        duration:(NSTimeInterval)duration
{
    // This prevents the view from autorotating to portrait in the simulator
    if ((orientation == UIInterfaceOrientationPortrait) ||
            (orientation== UIInterfaceOrientationPortraitUpsideDown)) {
        [app setStatusBarOrientation:
                UIInterfaceOrientationLandscapeRight animated:NO];
    }
}
#endif

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    // Create the window and view
   
    wi::g_appDelegate = self;
    m_fExiting = false;

    // Tell the application object to turn off the screen dimming idle
    // timer.
    application.idleTimerDisabled = YES; 
    
    CGRect frame = [[UIScreen mainScreen] bounds];
    m_window = [[UIWindow alloc] initWithFrame: frame];

    // Create the Wi view controller, remember the view for shortcut purposes
    m_vcwi = [[WiViewController alloc] initWithNibName:nil bundle:nil];
	m_window.rootViewController = m_vcwi;
    m_view = [[m_vcwi view] retain];

    m_vcchat = [[ChatViewController alloc] init:nil parent:m_view];
    m_pchat = NULL;
        
    // Show the window with table view
	[m_window addSubview:m_view];
    [m_window makeKeyAndVisible];

    // Must do this after makeKeyAndVisible, in order for it all to rotate
    [application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight
            animated:NO];

    // Alloc the key directories
    [self allocPaths];

    // Generate a UUID and keep around for later query
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    NSString *strUUID = (NSString *)CFUUIDCreateString(NULL, uuid);
    CFRelease(uuid);
    const char *pszUUID = [strUUID cStringUsingEncoding:
            [NSString defaultCStringEncoding]];
    m_pszUUID = (char *)malloc(strlen(pszUUID) + 1);
    strcpy(m_pszUUID, pszUUID);
    CFRelease(strUUID);

    // Spin off a thread to run the game
    m_game_thread = new base::Thread();
    m_game_thread->Start(wi::IPhone::GameThreadStart, NULL);
    [m_vcwi setGameThread: m_game_thread];
}

- (wi::SpriteManager *)getSpriteManager
{
    return [m_view getSpriteManager];
}<commit_msg>Remove setStatusBarOrientation due to deprecation<commit_after>#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIApplication.h>

#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include "iphone.h"
#include "input.h"
#include "base/log.h"
#include "base/thread.h"

#import "game/iphone/wiviewcontroller.h"
#import "game/iphone/chatviewcontroller.h"
#include "game/ht.h"

@interface IPhoneAppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *m_window;
    WiViewController *m_vcwi;
    ChatViewController *m_vcchat;
    wi::IChatController *m_pchat;
    id m_view;
    base::Thread *m_game_thread;
    bool m_fExiting;
    char *m_pszMissionPacksDir;
    char *m_pszMissionPackInfosDir;
    char *m_pszSaveGamesDir;
    char *m_pszPrefsFilename;
    char *m_pszMainDataDir;
    char *m_pszTempDir;
    char *m_pszCompletesDir;
    char *m_pszUUID;
}
@end

namespace wi {
IPhoneAppDelegate *g_appDelegate;
}

@implementation IPhoneAppDelegate

+ (void)initialize
{
}

- (void)allocPath:(char **)ppsz baseDir:(const char *)baseDir
        subDir:(const char *)subDir
{
    int cb = (int)(strlen(baseDir) + strlen(subDir) + 1);
    char *psz = (char *)malloc(cb);
    strcpy(psz, baseDir);
    strcat(psz, subDir);
    *ppsz = psz;
}

- (void)allocPaths
{
    // .app -> main app directory
    // .app/htdata832.pdb, htsfx.pdb -> main data files
    // Library/MissionPacks/ -> downloaded mission packs
    // Library/SaveGames/ -> saved games
    // Library/prefs.bin -> game prefs
    // NSTemporaryDirectory() -> temp dir

    // This returns the home directory; .app, Library, and Documents are in it
    NSString *homeDir = NSHomeDirectory();
    const char *pszHomeDir = [homeDir cStringUsingEncoding:
            [NSString defaultCStringEncoding]];
    NSString *tempDir = NSTemporaryDirectory();
    const char *pszTempDir = [tempDir cStringUsingEncoding:
            [NSString defaultCStringEncoding]];

    [self allocPath:&m_pszMissionPacksDir baseDir:pszHomeDir
            subDir:"/Library/MissionPacks"];
    [self allocPath:&m_pszMissionPackInfosDir baseDir:pszHomeDir
            subDir:"/Library/MissionPackInfos"];
    [self allocPath:&m_pszSaveGamesDir baseDir:pszHomeDir
            subDir:"/Library/SaveGames"];
    [self allocPath:&m_pszPrefsFilename baseDir:pszHomeDir
            subDir:"/Library/prefs.bin"];
    [self allocPath:&m_pszTempDir baseDir:pszTempDir subDir:""];
    [self allocPath:&m_pszCompletesDir baseDir:pszHomeDir
            subDir:"/Library/Completes"];

    // Make the directories under Library
    mkdir(m_pszMissionPacksDir, 0755);
    mkdir(m_pszMissionPackInfosDir, 0755);
    mkdir(m_pszSaveGamesDir, 0755);
    mkdir(m_pszCompletesDir, 0755);

    // Get directories off .app
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *appDir = [bundle bundlePath];
    const char *pszAppDir = [appDir cStringUsingEncoding:
            [NSString defaultCStringEncoding]];

    [self allocPath:&m_pszMainDataDir baseDir:pszAppDir
            subDir:""];

#if 0
    NSArray *pathsLibrary = NSSearchPathForDirectoriesInDomains(
            NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDirectory = [pathsLibray objectAtIndex:0];
#endif
}

- (void)initiateAsk:(const char *)title max:(int)max default:(const char *)def
        keyboard:(int)keyboard secure:(BOOL)secure
{
    return [m_vcwi initiateAsk:title max:max default:def keyboard:keyboard
            secure:secure];
}

- (void)getAskString:(char *)psz size:(int)cb
{
    return [m_vcwi getAskString:psz size:cb];
}

- (wi::IChatController *)getChatController
{
    if (m_pchat == NULL) {
        m_pchat = new wi::ChatController(m_vcwi, m_vcchat);
    }
    return m_pchat;
}

- (void)initiateWebView:(const char *)title withUrl:(const char *)url
{
    [m_vcwi initiateWebView:title withUrl:url];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    // Create the window and view
   
    wi::g_appDelegate = self;
    m_fExiting = false;

    // Tell the application object to turn off the screen dimming idle
    // timer.
    application.idleTimerDisabled = YES; 
    
    CGRect frame = [[UIScreen mainScreen] bounds];
    m_window = [[UIWindow alloc] initWithFrame: frame];

    // Create the Wi view controller, remember the view for shortcut purposes
    m_vcwi = [[WiViewController alloc] initWithNibName:nil bundle:nil];
	m_window.rootViewController = m_vcwi;
    m_view = [[m_vcwi view] retain];

    m_vcchat = [[ChatViewController alloc] init:nil parent:m_view];
    m_pchat = NULL;
        
    // Show the window with table view
	[m_window addSubview:m_view];
    [m_window makeKeyAndVisible];

    // Alloc the key directories
    [self allocPaths];

    // Generate a UUID and keep around for later query
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    NSString *strUUID = (NSString *)CFUUIDCreateString(NULL, uuid);
    CFRelease(uuid);
    const char *pszUUID = [strUUID cStringUsingEncoding:
            [NSString defaultCStringEncoding]];
    m_pszUUID = (char *)malloc(strlen(pszUUID) + 1);
    strcpy(m_pszUUID, pszUUID);
    CFRelease(strUUID);

    // Spin off a thread to run the game
    m_game_thread = new base::Thread();
    m_game_thread->Start(wi::IPhone::GameThreadStart, NULL);
    [m_vcwi setGameThread: m_game_thread];
}

- (wi::SpriteManager *)getSpriteManager
{
    return [m_view getSpriteManager];
}
{'fraction_non_alphanumeric': 0.11100610018042788, 'fraction_numerical': 0.004038147607182747, 'mean_word_length': 3.7685374846374438, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2809854', 'n_tokens_mistral': 3937, 'n_tokens_neox': 3819, 'n_words': 844}