more refactoring, less warnings, less stuff kept around
authorkoda
Sun, 17 Apr 2011 22:38:24 +0200
changeset 5155 f2165724605c
parent 5154 851f36579ed4
child 5156 641abe679bf0
more refactoring, less warnings, less stuff kept around
project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h
project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m
project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h
project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m
project_files/HedgewarsMobile/Classes/OverlayViewController.h
project_files/HedgewarsMobile/Classes/OverlayViewController.m
--- a/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h	Sun Apr 17 20:52:56 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.h	Sun Apr 17 22:38:24 2011 +0200
@@ -22,25 +22,34 @@
 #import <Foundation/Foundation.h>
 #import "SDL_net.h"
 
+@protocol EngineProtocolDelegate <NSObject>
+
+-(void) gameHasEndedWithStats:(NSArray *)stats;
+
+@end
+
 @interface EngineProtocolNetwork : NSObject {
-    NSMutableArray *statsArray;
-    NSString *savePath;
-    NSDictionary *gameConfig;
+    id<EngineProtocolDelegate> delegate;
 
+    NSOutputStream *stream;
     NSInteger ipcPort;              // Port on which engine will listen
     TCPsocket csd;                  // Client socket descriptor
 }
 
-@property (nonatomic,retain) NSMutableArray *statsArray;
-@property (nonatomic,retain) NSString *savePath;
-@property (nonatomic,retain) NSDictionary *gameConfig;
+@property (nonatomic,assign) id<EngineProtocolDelegate> delegate;
+@property (nonatomic,retain) NSOutputStream *stream;
 @property (assign) NSInteger ipcPort;
 @property (assign) TCPsocket csd;
 
 
 -(id)   init;
--(void) engineProtocol;
--(void) spawnThreadOnPort:(NSInteger) port;
+-(id)   initOnPort:(NSInteger) port;
+
+-(void) spawnThread:(NSString *)onSaveFile;
+-(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary;
+-(void) engineProtocol:(id) object;
+-(void) gameHasEndedWithStats:(NSArray *)stats;
+
 -(int)  sendToEngine:(NSString *)string;
 -(int)  sendToEngineNoSave:(NSString *)string;
 -(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor;
--- a/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Sun Apr 17 20:52:56 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/EngineProtocolNetwork.m	Sun Apr 17 22:38:24 2011 +0200
@@ -27,31 +27,50 @@
 #define BUFFER_SIZE 255     // like in original frontend
 
 @implementation EngineProtocolNetwork
-@synthesize statsArray, savePath, gameConfig, ipcPort, csd;
+@synthesize delegate, stream, ipcPort, csd;
 
 -(id) init {
     if (self = [super init]) {
-        self.savePath = nil;
-        self.statsArray = nil;
-        self.gameConfig = nil;
+        self.delegate = nil;
+
         self.ipcPort = 0;
+        self.csd = NULL;
+        self.stream = nil;
     }
     return self;
 }
 
+-(id) initOnPort:(NSInteger) port {
+    if (self = [self init])
+        self.ipcPort = port;
+    return self;
+}
+
+-(void) gameHasEndedWithStats:(NSArray *)stats {
+    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(gameHasEndedWithStats:)])
+        [self.delegate gameHasEndedWithStats:stats];
+    else
+        DLog(@"Error! delegate == nil");
+}
+
 -(void) dealloc {
-    [statsArray release];
-    [savePath release];
-    [gameConfig release];
+    self.delegate = nil;
+    releaseAndNil(stream);
     [super dealloc];
 }
 
--(void) spawnThreadOnPort:(NSInteger) port {
-    self.ipcPort = port;
+#pragma mark -
+#pragma mark Spawner functions
+-(void) spawnThread:(NSString *)onSaveFile withOptions:(NSDictionary *)dictionary {
+    self.stream = [[NSOutputStream alloc] initToFileAtPath:onSaveFile append:YES];
 
     [NSThread detachNewThreadSelector:@selector(engineProtocol)
                              toTarget:self
-                           withObject:nil];
+                           withObject:dictionary];
+}
+
+-(void) spawnThread:(NSString *)onSaveFile {
+    [self spawnThread:onSaveFile withOptions:nil];
 }
 
 #pragma mark -
@@ -191,13 +210,10 @@
 #pragma mark -
 #pragma mark Network relevant code
 -(void) dumpRawData:(const char *)buffer ofSize:(uint8_t) length {
-    // is it performant to reopen the stream every time?
-    NSOutputStream *os = [[NSOutputStream alloc] initToFileAtPath:self.savePath append:YES];
-    [os open];
-    [os write:&length maxLength:1];
-    [os write:(const uint8_t *)buffer maxLength:length];
-    [os close];
-    [os release];
+    [self.stream open];
+    [self.stream write:&length maxLength:1];
+    [self.stream write:(const uint8_t *)buffer maxLength:length];
+    [self.stream close];
 }
 
 // wrapper that computes the length of the message and then sends the command string, saving the command on a file
@@ -218,8 +234,10 @@
 }
 
 // this is launched as thread and handles all IPC with engine
--(void) engineProtocol {
+-(void) engineProtocol:(id) object {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    NSDictionary *gameConfig = (NSDictionary *)object;
+    NSMutableArray *statsArray = nil;
     TCPsocket sd;
     IPaddress ip;
     int eProto;
@@ -262,7 +280,7 @@
 
         switch (buffer[0]) {
             case 'C':
-                DLog(@"Sending game config...\n%@", self.gameConfig);
+                DLog(@"Sending game config...\n%@", gameConfig);
 
                 /*if (isNetGame == YES)
                     [self sendToEngineNoSave:@"TN"];
@@ -272,32 +290,32 @@
                 [self dumpRawData:[saveHeader UTF8String] ofSize:[saveHeader length]];
 
                 // seed info
-                [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"seed_command"]];
 
                 // dimension of the map
-                [self sendToEngine:[self.gameConfig objectForKey:@"templatefilter_command"]];
-                [self sendToEngine:[self.gameConfig objectForKey:@"mapgen_command"]];
-                [self sendToEngine:[self.gameConfig objectForKey:@"mazesize_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"templatefilter_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"mapgen_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"mazesize_command"]];
 
                 // static land (if set)
-                NSString *staticMap = [self.gameConfig objectForKey:@"staticmap_command"];
+                NSString *staticMap = [gameConfig objectForKey:@"staticmap_command"];
                 if ([staticMap length] != 0)
                     [self sendToEngine:staticMap];
 
                 // lua script (if set)
-                NSString *script = [self.gameConfig objectForKey:@"mission_command"];
+                NSString *script = [gameConfig objectForKey:@"mission_command"];
                 if ([script length] != 0)
                     [self sendToEngine:script];
 
                 // theme info
-                [self sendToEngine:[self.gameConfig objectForKey:@"theme_command"]];
+                [self sendToEngine:[gameConfig objectForKey:@"theme_command"]];
 
                 // scheme (returns initial health)
-                NSInteger health = [self provideScheme:[self.gameConfig objectForKey:@"scheme"]];
+                NSInteger health = [self provideScheme:[gameConfig objectForKey:@"scheme"]];
 
                 // send an ammostore for each team
-                NSArray *teamsConfig = [self.gameConfig objectForKey:@"teams_list"];
-                [self provideAmmoData:[self.gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]];
+                NSArray *teamsConfig = [gameConfig objectForKey:@"teams_list"];
+                [self provideAmmoData:[gameConfig objectForKey:@"weapon"] forPlayingTeams:[teamsConfig count]];
 
                 // finally add hogs
                 for (NSDictionary *teamData in teamsConfig) {
@@ -331,10 +349,10 @@
                 }
                 break;
             case 'i':
-                if (self.statsArray == nil) {
-                    self.statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
+                if (statsArray == nil) {
+                    statsArray = [[NSMutableArray alloc] initWithCapacity:10 - 2];
                     NSMutableArray *ranking = [[NSMutableArray alloc] initWithCapacity:4];
-                    [self.statsArray insertObject:ranking atIndex:0];
+                    [statsArray insertObject:ranking atIndex:0];
                     [ranking release];
                 }
                 NSString *tempStr = [NSString stringWithUTF8String:&buffer[2]];
@@ -343,16 +361,16 @@
                 int index = [arg length] + 3;
                 switch (buffer[1]) {
                     case 'r':           // winning team
-                        [self.statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
+                        [statsArray insertObject:[NSString stringWithUTF8String:&buffer[2]] atIndex:1];
                         break;
                     case 'D':           // best shot
-                        [self.statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
+                        [statsArray addObject:[NSString stringWithFormat:@"The best shot award won by %s (with %@ points)", &buffer[index], arg]];
                         break;
                     case 'k':           // best hedgehog
-                        [self.statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kills in a turn", &buffer[index], arg]];
+                        [statsArray addObject:[NSString stringWithFormat:@"The best killer is %s with %@ kills in a turn", &buffer[index], arg]];
                         break;
                     case 'K':           // number of hogs killed
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
+                        [statsArray addObject:[NSString stringWithFormat:@"%@ hedgehog(s) were killed during this round", arg]];
                         break;
                     case 'H':           // team health/graph
                         break;
@@ -360,16 +378,16 @@
                         // still WIP in statsPage.cpp
                         break;
                     case 'P':           // teams ranking
-                        [[self.statsArray objectAtIndex:0] addObject:tempStr];
+                        [[statsArray objectAtIndex:0] addObject:tempStr];
                         break;
                     case 's':           // self damage
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
+                        [statsArray addObject:[NSString stringWithFormat:@"%s thought it's good to shoot his own hedgehogs with %@ points", &buffer[index], arg]];
                         break;
                     case 'S':           // friendly fire
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
+                        [statsArray addObject:[NSString stringWithFormat:@"%s killed %@ of his own hedgehogs", &buffer[index], arg]];
                         break;
                     case 'B':           // turn skipped
-                        [self.statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
+                        [statsArray addObject:[NSString stringWithFormat:@"%s was scared and skipped turn %@ times", &buffer[index], arg]];
                         break;
                     default:
                         DLog(@"Unhandled stat message, see statsPage.cpp");
@@ -378,9 +396,7 @@
                 break;
             case 'q':
                 // game ended, can remove the savefile and the trailing overlay (when dualhead)
-                [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
-                if (IS_DUALHEAD())
-                    [[NSNotificationCenter defaultCenter] postNotificationName:@"remove overlay" object:nil];
+                [self gameHasEndedWithStats:statsArray];
                 break;
             case 'Q':
                 // game exited but not completed, nothing to do (just don't save the message)
--- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h	Sun Apr 17 20:52:56 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.h	Sun Apr 17 22:38:24 2011 +0200
@@ -20,13 +20,13 @@
 
 
 #import <Foundation/Foundation.h>
+#import "EngineProtocolNetwork.h"
 
 typedef enum {gtNone, gtLocal, gtSave, gtNet} TGameType;
 
 @class OverlayViewController;
-@class EngineProtocolNetwork;
 
-@interface GameInterfaceBridge : NSObject {
+@interface GameInterfaceBridge : NSObject <EngineProtocolDelegate> {
     UIViewController *parentController;
     OverlayViewController *overlayController;
 
@@ -49,25 +49,11 @@
 @property (assign) TGameType gameType;
 
 
--(id) initWithController:(id) viewController;
+-(id)   initWithController:(id) viewController;
 -(void) startLocalGame:(NSDictionary *)withDictionary;
 -(void) startSaveGame:(NSString *)atPath;
--(const char **)gatherGameSettings;
+-(void) prepareEngineLaunch;
 -(void) startGameEngine;
-
-/*
-@property (nonatomic, retain) NSDictionary *systemSettings;
-@property (nonatomic, retain) NSMutableArray *statsArray;
-@property (assign) BOOL menuStyle;
-
--(id) initWithDictionary:(NSDictionary *)gameDictionary;
--(void) engineProtocol;
--(int) sendToEngine:(NSString *)string;
--(int) sendToEngineNoSave:(NSString *)string;
--(void) provideTeamData:(NSString *)teamName forHogs:(NSInteger) numberOfPlayingHogs withHealth:(NSInteger) initialHealth ofColor:(NSNumber *)teamColor;
--(void) provideAmmoData:(NSString *)ammostoreName forPlayingTeams:(NSInteger) numberOfTeams;
--(NSInteger) provideScheme:(NSString *)schemeName;
-
--(const char **)getGameSettings:(NSString *)recordFile;*/
+-(void) gameHasEndedWithStats:(NSArray *)stats;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Sun Apr 17 20:52:56 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m	Sun Apr 17 22:38:24 2011 +0200
@@ -24,23 +24,21 @@
 #import "EngineProtocolNetwork.h"
 #import "OverlayViewController.h"
 
-#define BUFFER_SIZE 255     // like in original frontend
-
 @implementation GameInterfaceBridge
 @synthesize parentController, systemSettings, savePath, overlayController, ipcPort, gameType, engineProtocol;
 
 -(id) initWithController:(id) viewController {
     if (self = [super init]) {
+        self.ipcPort = randomPort();
+        self.gameType = gtNone;
+        self.savePath = nil;
+
         self.parentController = (UIViewController *)viewController;
-        self.engineProtocol = [[EngineProtocolNetwork alloc] init];
-;
-        self.savePath = nil;
+        self.engineProtocol = [[EngineProtocolNetwork alloc] initOnPort:self.ipcPort];
+        self.engineProtocol.delegate = self;
 
         self.systemSettings = [NSDictionary dictionaryWithContentsOfFile:SETTINGS_FILE()];
         self.overlayController = [[OverlayViewController alloc] initWithNibName:@"OverlayViewController" bundle:nil];
-        self.ipcPort = randomPort();
-
-        self.gameType = gtNone;
     }
     return self;
 }
@@ -68,59 +66,6 @@
 
 // main routine for calling the actual game engine
 -(void) startGameEngine {
-    self.parentController.view.opaque = YES;
-    self.parentController.view.backgroundColor = [UIColor blackColor];
-    self.parentController.view.alpha = 0;
-
-    [UIView beginAnimations:@"fade out to black" context:NULL];
-    [UIView setAnimationDuration:1];
-    self.parentController.view.alpha = 1;
-    [UIView commitAnimations];
-
-    self.engineProtocol.savePath = self.savePath;
-    [self.engineProtocol spawnThreadOnPort:self.ipcPort];
-
-    NSDictionary *overlayOptions = [[NSDictionary alloc] initWithObjectsAndKeys:
-                                    [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation",
-                                    [self.systemSettings objectForKey:@"menu"],@"menu",
-                                    nil];
-    [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:1];
-    [overlayOptions release];
-
-    // this is the pascal fuction that starts the game, wrapped around isInGame
-    [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
-    Game([self gatherGameSettings]);
-    [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
-
-    [UIView beginAnimations:@"fade in" context:NULL];
-    [UIView setAnimationDuration:1];
-    self.parentController.view.alpha = 0;
-    [UIView commitAnimations];
-}
-
--(void) startLocalGame:(NSDictionary *)withDictionary {
-    self.gameType = gtLocal;
-    [self.engineProtocol setGameConfig:withDictionary];
-    
-    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
-    [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
-    NSString *newDateString = [outputFormatter stringFromDate:[NSDate date]];
-    self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", newDateString];
-    [outputFormatter release];
-    
-    [self startGameEngine];
-}
-
--(void) startSaveGame:(NSString *)atPath {
-    self.gameType = gtSave;
-    self.savePath = atPath;
-    [self.engineProtocol setGameConfig:nil];
-
-    [self startGameEngine];
-}
-
-#pragma mark -
--(const char **)gatherGameSettings {
     const char *gameArgs[10];
     NSInteger width, height, orientation;
     NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", self.ipcPort];
@@ -180,8 +125,69 @@
     [rotation release];
     [localeString release];
     [ipcString release];
-    return gameArgs;
+
+    // this is the pascal fuction that starts the game, wrapped around isInGame
+    [HedgewarsAppDelegate sharedAppDelegate].isInGame = YES;
+    Game(gameArgs);
+    [HedgewarsAppDelegate sharedAppDelegate].isInGame = NO;
+}
+
+// prepares the controllers for hosting a game
+-(void) prepareEngineLaunch {
+    self.parentController.view.opaque = YES;
+    self.parentController.view.backgroundColor = [UIColor blackColor];
+    self.parentController.view.alpha = 0;
+
+    [UIView beginAnimations:@"fade out to black" context:NULL];
+    [UIView setAnimationDuration:1];
+    self.parentController.view.alpha = 1;
+    [UIView commitAnimations];
+
+    NSDictionary *overlayOptions = [[NSDictionary alloc] initWithObjectsAndKeys:
+                                    [NSNumber numberWithInt:self.parentController.interfaceOrientation],@"orientation",
+                                    [self.systemSettings objectForKey:@"menu"],@"menu",
+                                    nil];
+    [self performSelector:@selector(displayOverlayLater:) withObject:overlayOptions afterDelay:1];
+    [overlayOptions release];
+
+    [self startGameEngine];
+
+    [UIView beginAnimations:@"fade in" context:NULL];
+    [UIView setAnimationDuration:1];
+    self.parentController.view.alpha = 0;
+    [UIView commitAnimations];
 }
 
+// set up variables for a local game
+-(void) startLocalGame:(NSDictionary *)withDictionary {
+    self.gameType = gtLocal;
+
+    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
+    [outputFormatter setDateFormat:@"yyyy-MM-dd '@' HH.mm"];
+    NSString *newDateString = [outputFormatter stringFromDate:[NSDate date]];
+    self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", newDateString];
+    [outputFormatter release];
+
+    [self.engineProtocol spawnThread:self.savePath withOptions:withDictionary];
+    [self prepareEngineLaunch];
+}
+
+// set up variables for a save game
+-(void) startSaveGame:(NSString *)atPath {
+    self.gameType = gtSave;
+    self.savePath = atPath;
+
+    [self.engineProtocol spawnThread:self.savePath];
+    [self prepareEngineLaunch];
+}
+
+-(void) gameHasEndedWithStats:(NSArray *)stats {
+    DLog(@"%@",stats);
+
+    [self.overlayController removeOverlay];
+    // can remove the file if the replay has ended
+    if (self.gameType == gtSave)
+        [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:NULL];
+}
 
 @end
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Sun Apr 17 20:52:56 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.h	Sun Apr 17 22:38:24 2011 +0200
@@ -61,7 +61,6 @@
 @property (nonatomic,retain) InGameMenuViewController *popupMenu;
 @property (nonatomic,retain) HelpPageViewController *helpPage;
 @property (nonatomic,retain) AmmoMenuViewController *amvc;
-@property (assign) BOOL isNetGame;
 @property (assign) BOOL useClassicMenu;
 @property (assign) NSInteger initialOrientation;
 
@@ -77,5 +76,6 @@
 -(void) dismissPopover;
 -(void) dimOverlay;
 -(void) activateOverlay;
+-(void) removeOverlay;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sun Apr 17 20:52:56 2011 +0200
+++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m	Sun Apr 17 22:38:24 2011 +0200
@@ -188,12 +188,6 @@
                                                  name:@"show help ingame"
                                                object:nil];
 
-    // remove the view, required by the dual head version
-    [[NSNotificationCenter defaultCenter] addObserver:self
-                                             selector:@selector(removeOverlay:)
-                                                 name:@"remove overlay"
-                                               object:nil];
-
     // for iOS >= 3.2
     if ([UIScreen respondsToSelector:@selector(screens)]) {
         [[NSNotificationCenter defaultCenter] addObserver:self
@@ -243,12 +237,8 @@
 
 -(void) showHelp:(id) sender {
     if (self.helpPage == nil) {
-        NSString *xib;
-        if (IS_IPAD())
-            xib = @"HelpPageInGameViewController-iPad";
-        else
-            xib = @"HelpPageInGameViewController-iPhone";
-        self.helpPage = [[HelpPageViewController alloc] initWithNibName:xib bundle:nil];
+        NSString *xibName = (IS_IPAD() ? @"HelpPageInGameViewController-iPad" : @"HelpPageInGameViewController-iPhone");
+        self.helpPage = [[HelpPageViewController alloc] initWithNibName:xibName bundle:nil];
     }
     self.helpPage.view.alpha = 0;
     [self.view addSubview:helpPage.view];
@@ -258,7 +248,7 @@
     doNotDim();
 }
 
--(void) removeOverlay:(id) sender {
+-(void) removeOverlay {
     [self.popupMenu performSelectorOnMainThread:@selector(dismiss) withObject:nil waitUntilDone:YES];
     [self.popoverController performSelectorOnMainThread:@selector(dismissPopoverAnimated:) withObject:nil waitUntilDone:YES];
     [self.view performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES];