have Saves restart net when loaded, also resumes music
authorkoda
Sat, 25 Sep 2010 03:04:22 +0200
changeset 3902 3aac7ca07b0e
parent 3900 5fe24180fc72
child 3903 db01c37494af
have Saves restart net when loaded, also resumes music fix saving on hwmobile
hedgewars/uGame.pas
project_files/HedgewarsMobile/Classes/GameSetup.m
project_files/HedgewarsMobile/Classes/NSStringExtra.m
--- a/hedgewars/uGame.pas	Fri Sep 24 15:14:40 2010 -0400
+++ b/hedgewars/uGame.pas	Sat Sep 25 03:04:22 2010 +0200
@@ -26,7 +26,7 @@
 ////////////////////
    implementation
 ////////////////////
-uses uMisc, uConsts, uKeys, uTeams, uIO, uAI, uGears, uScript;
+uses uMisc, uConsts, uKeys, uTeams, uIO, uAI, uGears, uScript, uSound;
 
 procedure DoGameTick(Lag: LongInt);
 var i: LongInt;
@@ -65,7 +65,9 @@
                         SetBinds(CurrentTeam^.Binds);
                         //CurrentHedgehog^.Gear^.Message:= 0; <- produces bugs with further save restoring and demos
                         isSoundEnabled:= isSEBackup;
-                        GameType:= gmtLocal
+                        if isSoundEnabled then playMusic;
+                        GameType:= gmtLocal;
+                        InitIPC;
                         end;
                end
           else ProcessGears
--- a/project_files/HedgewarsMobile/Classes/GameSetup.m	Fri Sep 24 15:14:40 2010 -0400
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.m	Sat Sep 25 03:04:22 2010 +0200
@@ -26,7 +26,7 @@
 #import "CommodityFunctions.h"
 #import "NSStringExtra.h"
 
-#define BUFFER_SIZE 64
+#define BUFFER_SIZE 255     // like in original frontend
 
 @implementation GameSetup
 @synthesize systemSettings, gameConfig, savePath;
@@ -255,7 +255,7 @@
     uint8_t length = [string length];
 
     [[NSString stringWithFormat:@"%c%@",length,string] appendToFile:savePath];
-    SDLNet_TCP_Send(csd, &length , 1);
+    SDLNet_TCP_Send(csd, &length, 1);
     return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
 
@@ -263,7 +263,7 @@
 -(int) sendToEngineNoSave: (NSString *)string {
     uint8_t length = [string length];
 
-    SDLNet_TCP_Send(csd, &length , 1);
+    SDLNet_TCP_Send(csd, &length, 1);
     return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
 
@@ -274,9 +274,8 @@
     IPaddress ip;
     int eProto;
     BOOL clientQuit;
-    char buffer[BUFFER_SIZE];
+    uint8_t buffer[BUFFER_SIZE];
     uint8_t msgSize;
-    uint16_t gameTicks;
 
     clientQuit = NO;
     csd = NULL;
@@ -304,12 +303,14 @@
     SDLNet_TCP_Close(sd);
 
     while (!clientQuit) {
+        NSString *msgToSave = nil;
+        NSOutputStream *os = nil;
         msgSize = 0;
-        memset(buffer, 0, BUFFER_SIZE);
+        memset(buffer, '\0', BUFFER_SIZE);
         if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
-            clientQuit = YES;
+            break;
         if (SDLNet_TCP_Recv(csd, buffer, msgSize) <=0)
-            clientQuit = YES;
+            break;
 
         switch (buffer[0]) {
             case 'C':
@@ -360,9 +361,10 @@
                 clientQuit = YES;
                 break;
             case 'e':
-                buffer[msgSize] = '\0';
-                [[NSString stringWithUTF8String:buffer] appendToFile:savePath];
-                sscanf(buffer, "%*s %d", &eProto);
+                msgToSave = [NSString stringWithFormat:@"%c%s",msgSize,buffer];                
+                [msgToSave appendToFile:self.savePath];
+                
+                sscanf((char *)buffer, "%*s %d", &eProto);
                 short int netProto = 0;
                 char *versionStr;
 
@@ -378,19 +380,28 @@
             case 'i':
                 switch (buffer[1]) {
                     case 'r':
-                        NSLog(@"Winning team: %s", &buffer[2]);
+                        DLog(@"Winning team: %s", &buffer[2]);
                         break;
                     case 'k':
-                        NSLog(@"Best Hedgehog: %s", &buffer[2]);
+                        DLog(@"Best Hedgehog: %s", &buffer[2]);
+                        break;
+                    default:
+                        // TODO: losta stats stuff
                         break;
                 }
                 break;
+            case 'q':
+                // game ended, can remove the savefile
+                [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil];
+                break;
             default:
-                // empty packet or just statistics -- in either cases gameTicks is sent
-                gameTicks = SDLNet_Read16 (&buffer[msgSize - 2]);
-                DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
-                buffer[msgSize] = '\0';
-                [[NSString stringWithUTF8String:buffer] appendToFile:savePath];
+                // is it performant to reopen the stream every time? 
+                os = [[NSOutputStream alloc] initToFileAtPath:self.savePath append:YES];
+                [os open];
+                [os write:&msgSize maxLength:1];
+                [os write:buffer maxLength:msgSize];
+                [os close];
+                [os release];
                 break;
         }
     }
--- a/project_files/HedgewarsMobile/Classes/NSStringExtra.m	Fri Sep 24 15:14:40 2010 -0400
+++ b/project_files/HedgewarsMobile/Classes/NSStringExtra.m	Sat Sep 25 03:04:22 2010 +0200
@@ -35,6 +35,11 @@
     return YES;
 }
 
+-(BOOL) appendToFile:(NSString *)path usingStream:(NSOutputStream *)os {
+    NSData *allData = [self dataUsingEncoding:NSUTF8StringEncoding];
+    [os write:[allData bytes] maxLength:[allData length]];
+    return YES;
+}
 
 // by http://iphonedevelopment.blogspot.com/2010/08/nsstring-appendtofileusingencoding.html
 -(BOOL) appendToFile:(NSString *)path usingEncoding:(NSStringEncoding) encoding {