project_files/HedgewarsMobile/Classes/GameSetup.m
changeset 3891 f8f0d0ceb19c
parent 3829 81db3c85784b
child 3893 568bfd083465
equal deleted inserted replaced
3889:f7d6834a54fe 3891:f8f0d0ceb19c
    22 #import "GameSetup.h"
    22 #import "GameSetup.h"
    23 #import "SDL_uikitappdelegate.h"
    23 #import "SDL_uikitappdelegate.h"
    24 #import "SDL_net.h"
    24 #import "SDL_net.h"
    25 #import "PascalImports.h"
    25 #import "PascalImports.h"
    26 #import "CommodityFunctions.h"
    26 #import "CommodityFunctions.h"
    27 
    27 #import "NSStringExtra.h"
    28 #define BUFFER_SIZE 256
    28 
       
    29 #define BUFFER_SIZE 128
    29 
    30 
    30 @implementation GameSetup
    31 @implementation GameSetup
    31 
    32 @synthesize systemSettings, gameConfig, savePath;
    32 @synthesize systemSettings, gameConfig;
       
    33 
    33 
    34 -(id) initWithDictionary:(NSDictionary *)gameDictionary {
    34 -(id) initWithDictionary:(NSDictionary *)gameDictionary {
    35     if (self = [super init]) {
    35     if (self = [super init]) {
    36         ipcPort = randomPort();
    36         ipcPort = randomPort();
    37 
    37 
    39         NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    39         NSDictionary *dictSett = [[NSDictionary alloc] initWithContentsOfFile:SETTINGS_FILE()];
    40         self.systemSettings = dictSett;
    40         self.systemSettings = dictSett;
    41         [dictSett release];
    41         [dictSett release];
    42 
    42 
    43         self.gameConfig = gameDictionary;
    43         self.gameConfig = gameDictionary;
       
    44         self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", [[NSDate date] description]];
    44     }
    45     }
    45     return self;
    46     return self;
    46 }
    47 }
    47 
    48 
    48 -(void) dealloc {
    49 -(void) dealloc {
    49     [gameConfig release];
    50     [gameConfig release];
    50     [systemSettings release];
    51     [systemSettings release];
       
    52     [savePath release];
    51     [super dealloc];
    53     [super dealloc];
    52 }
    54 }
    53 
    55 
    54 #pragma mark -
    56 #pragma mark -
    55 #pragma mark Provider functions
    57 #pragma mark Provider functions
   236 -(void) startThread: (NSString *) selector {
   238 -(void) startThread: (NSString *) selector {
   237     SEL usage = NSSelectorFromString(selector);
   239     SEL usage = NSSelectorFromString(selector);
   238     [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
   240     [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
   239 }
   241 }
   240 
   242 
   241 // wrapper that computes the length of the message and then sends the command string
   243 // wrapper that computes the length of the message and then sends the command string, saving the command on a file
   242 -(int) sendToEngine: (NSString *)string {
   244 -(int) sendToEngine: (NSString *)string {
       
   245     uint8_t length = [string length];
       
   246 
       
   247     [[NSString stringWithFormat:@"%c%@",length,string] appendToFile:savePath];
       
   248     SDLNet_TCP_Send(csd, &length , 1);
       
   249     return SDLNet_TCP_Send(csd, [string UTF8String], length);
       
   250 }
       
   251 
       
   252 // wrapper that computes the length of the message and then sends the command string, skipping file writing
       
   253 -(int) sendToEngineNoSave: (NSString *)string {
   243     uint8_t length = [string length];
   254     uint8_t length = [string length];
   244 
   255 
   245     SDLNet_TCP_Send(csd, &length , 1);
   256     SDLNet_TCP_Send(csd, &length , 1);
   246     return SDLNet_TCP_Send(csd, [string UTF8String], length);
   257     return SDLNet_TCP_Send(csd, [string UTF8String], length);
   247 }
   258 }
   293         switch (buffer[0]) {
   304         switch (buffer[0]) {
   294             case 'C':
   305             case 'C':
   295                 DLog(@"sending game config...\n%@",self.gameConfig);
   306                 DLog(@"sending game config...\n%@",self.gameConfig);
   296 
   307 
   297                 // local game
   308                 // local game
   298                 [self sendToEngine:@"TL"];
   309                 [self sendToEngineNoSave:@"TL"];
       
   310                 NSString *saveHeader = @"TS";
       
   311                 [[NSString stringWithFormat:@"%c%@",[saveHeader length], saveHeader] appendToFile:savePath];
   299 
   312 
   300                 // seed info
   313                 // seed info
   301                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   314                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
   302 
   315 
   303                 // dimension of the map
   316                 // dimension of the map
   333             case 'E':
   346             case 'E':
   334                 DLog(@"ERROR - last console line: [%s]", &buffer[1]);
   347                 DLog(@"ERROR - last console line: [%s]", &buffer[1]);
   335                 clientQuit = YES;
   348                 clientQuit = YES;
   336                 break;
   349                 break;
   337             case 'e':
   350             case 'e':
       
   351                 buffer[msgSize] = '\0';
       
   352                 [[NSString stringWithUTF8String:buffer] appendToFile:savePath];
   338                 sscanf(buffer, "%*s %d", &eProto);
   353                 sscanf(buffer, "%*s %d", &eProto);
   339                 short int netProto = 0;
   354                 short int netProto = 0;
   340                 char *versionStr;
   355                 char *versionStr;
   341 
   356 
   342                 HW_versionInfo(&netProto, &versionStr);
   357                 HW_versionInfo(&netProto, &versionStr);
   359                 }
   374                 }
   360                 break;
   375                 break;
   361             default:
   376             default:
   362                 // empty packet or just statistics -- in either cases gameTicks is sent
   377                 // empty packet or just statistics -- in either cases gameTicks is sent
   363                 gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
   378                 gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
   364                 //DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
   379                 DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
       
   380                 buffer[msgSize] = '\0';
       
   381                 [[NSString stringWithUTF8String:buffer] appendToFile:savePath];
   365                 break;
   382                 break;
   366         }
   383         }
   367     }
   384     }
   368     DLog(@"Engine exited, closing server");
   385     DLog(@"Engine exited, closing server");
   369     // wait a little to let the client close cleanly
   386     // wait a little to let the client close cleanly
   420     gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
   437     gameArgs[ 6] = [[[self.systemSettings objectForKey:@"sound"] stringValue] UTF8String];       //isSoundEnabled
   421     gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
   438     gameArgs[ 7] = [[[self.systemSettings objectForKey:@"music"] stringValue] UTF8String];       //isMusicEnabled
   422     gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
   439     gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
   423     gameArgs[ 9] = NULL;                                                                         //unused
   440     gameArgs[ 9] = NULL;                                                                         //unused
   424     gameArgs[10] = NULL;                                                                         //recordFileName
   441     gameArgs[10] = NULL;                                                                         //recordFileName
       
   442     //[[SAVES_DIRECTORY() stringByAppendingString:@"/2010-09-21 23/30/10 +0200.hws"] UTF8String];                                                                         //recordFileName
   425 
   443 
   426     [wSize release];
   444     [wSize release];
   427     [hSize release];
   445     [hSize release];
   428     [localeString release];
   446     [localeString release];
   429     [ipcString release];
   447     [ipcString release];