initial support for savegames
authorkoda
Wed, 22 Sep 2010 01:10:20 +0200
changeset 3891 f8f0d0ceb19c
parent 3889 f7d6834a54fe
child 3893 568bfd083465
initial support for savegames
project_files/HedgewarsMobile/Classes/CommodityFunctions.h
project_files/HedgewarsMobile/Classes/GameSetup.h
project_files/HedgewarsMobile/Classes/GameSetup.m
project_files/HedgewarsMobile/Classes/MainMenuViewController.m
project_files/HedgewarsMobile/Classes/NSStringExtra.h
project_files/HedgewarsMobile/Classes/NSStringExtra.m
project_files/HedgewarsMobile/Classes/SupportViewController.h
project_files/HedgewarsMobile/Classes/SupportViewController.m
project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj
--- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.h	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.h	Wed Sep 22 01:10:20 2010 +0200
@@ -31,6 +31,7 @@
 #define TEAMS_DIRECTORY()       [DOCUMENTS_FOLDER() stringByAppendingString:@"/Teams/"]
 #define WEAPONS_DIRECTORY()     [DOCUMENTS_FOLDER() stringByAppendingString:@"/Weapons/"]
 #define SCHEMES_DIRECTORY()     [DOCUMENTS_FOLDER() stringByAppendingString:@"/Schemes/"]
+#define SAVES_DIRECTORY()       [DOCUMENTS_FOLDER() stringByAppendingString:@"/Saves/"]
 
 #define GRAPHICS_DIRECTORY()    [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/"]
 #define HATS_DIRECTORY()        [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Graphics/Hats/"]
--- a/project_files/HedgewarsMobile/Classes/GameSetup.h	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.h	Wed Sep 22 01:10:20 2010 +0200
@@ -28,15 +28,19 @@
 
     NSInteger ipcPort;  // Port on which engine will listen
     TCPsocket csd;      // Client socket descriptor
+    
+    NSString *savePath;
 }
 
 @property (nonatomic, retain) NSDictionary *systemSettings;
 @property (nonatomic, retain) NSDictionary *gameConfig;
+@property (nonatomic, retain) NSString *savePath;
 
 -(id) initWithDictionary:(NSDictionary *)gameDictionary;
 -(void) engineProtocol;
 -(void) startThread:(NSString *)selector;
 -(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;
--- a/project_files/HedgewarsMobile/Classes/GameSetup.m	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.m	Wed Sep 22 01:10:20 2010 +0200
@@ -24,12 +24,12 @@
 #import "SDL_net.h"
 #import "PascalImports.h"
 #import "CommodityFunctions.h"
+#import "NSStringExtra.h"
 
-#define BUFFER_SIZE 256
+#define BUFFER_SIZE 128
 
 @implementation GameSetup
-
-@synthesize systemSettings, gameConfig;
+@synthesize systemSettings, gameConfig, savePath;
 
 -(id) initWithDictionary:(NSDictionary *)gameDictionary {
     if (self = [super init]) {
@@ -41,6 +41,7 @@
         [dictSett release];
 
         self.gameConfig = gameDictionary;
+        self.savePath = [SAVES_DIRECTORY() stringByAppendingFormat:@"%@.hws", [[NSDate date] description]];
     }
     return self;
 }
@@ -48,6 +49,7 @@
 -(void) dealloc {
     [gameConfig release];
     [systemSettings release];
+    [savePath release];
     [super dealloc];
 }
 
@@ -238,10 +240,19 @@
     [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
 }
 
-// wrapper that computes the length of the message and then sends the command string
+// wrapper that computes the length of the message and then sends the command string, saving the command on a file
 -(int) sendToEngine: (NSString *)string {
     uint8_t length = [string length];
 
+    [[NSString stringWithFormat:@"%c%@",length,string] appendToFile:savePath];
+    SDLNet_TCP_Send(csd, &length , 1);
+    return SDLNet_TCP_Send(csd, [string UTF8String], length);
+}
+
+// wrapper that computes the length of the message and then sends the command string, skipping file writing
+-(int) sendToEngineNoSave: (NSString *)string {
+    uint8_t length = [string length];
+
     SDLNet_TCP_Send(csd, &length , 1);
     return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
@@ -295,7 +306,9 @@
                 DLog(@"sending game config...\n%@",self.gameConfig);
 
                 // local game
-                [self sendToEngine:@"TL"];
+                [self sendToEngineNoSave:@"TL"];
+                NSString *saveHeader = @"TS";
+                [[NSString stringWithFormat:@"%c%@",[saveHeader length], saveHeader] appendToFile:savePath];
 
                 // seed info
                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
@@ -335,6 +348,8 @@
                 clientQuit = YES;
                 break;
             case 'e':
+                buffer[msgSize] = '\0';
+                [[NSString stringWithUTF8String:buffer] appendToFile:savePath];
                 sscanf(buffer, "%*s %d", &eProto);
                 short int netProto = 0;
                 char *versionStr;
@@ -361,7 +376,9 @@
             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);
+                DLog(@"engineProtocol - %d: received [%s]", gameTicks, buffer);
+                buffer[msgSize] = '\0';
+                [[NSString stringWithUTF8String:buffer] appendToFile:savePath];
                 break;
         }
     }
@@ -422,6 +439,7 @@
     gameArgs[ 8] = [[[self.systemSettings objectForKey:@"alternate"] stringValue] UTF8String];   //cAltDamage
     gameArgs[ 9] = NULL;                                                                         //unused
     gameArgs[10] = NULL;                                                                         //recordFileName
+    //[[SAVES_DIRECTORY() stringByAppendingString:@"/2010-09-21 23/30/10 +0200.hws"] UTF8String];                                                                         //recordFileName
 
     [wSize release];
     [hSize release];
--- a/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Wed Sep 22 01:10:20 2010 +0200
@@ -76,6 +76,11 @@
         NSString *directoryToCheck, *fileToCheck, *fileToUpdate;
         DLog(@"Creating necessary files");
         
+        // create an empty saves directory by deleting the previous one (saves are incompatible between releases)
+        if ([[NSFileManager defaultManager] fileExistsAtPath:SAVES_DIRECTORY()])
+            [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
+        [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
+        
         // if the settings file is already present, we merge current preferences with the update
         directoryToCheck = [NSString stringWithFormat:@"%@/Settings/settings.plist",resDir];
         if ([[NSFileManager defaultManager] fileExistsAtPath:SETTINGS_FILE()]) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/NSStringExtra.h	Wed Sep 22 01:10:20 2010 +0200
@@ -0,0 +1,29 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 21/09/2010.
+ */
+
+
+#import <UIKit/UIKit.h>
+
+@interface NSString (extra)
+
+-(BOOL) appendToFile:(NSString *)path;
+-(BOOL) appendToFile:(NSString *)path usingEncoding:(NSStringEncoding) encoding;
+
+@end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/HedgewarsMobile/Classes/NSStringExtra.m	Wed Sep 22 01:10:20 2010 +0200
@@ -0,0 +1,55 @@
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 21/09/2010.
+ */
+
+
+#import "NSStringExtra.h"
+
+@implementation NSString (extra)
+
+-(BOOL) appendToFile:(NSString *)path {
+    NSOutputStream* os = [[NSOutputStream alloc] initToFileAtPath:path append:YES];
+    NSData *allData = [self dataUsingEncoding:NSUTF8StringEncoding];
+
+    [os open];
+    [os write:[allData bytes] maxLength:[allData length]];
+    [os close];
+    
+    [os release];
+    return YES;
+}
+
+
+// by http://iphonedevelopment.blogspot.com/2010/08/nsstring-appendtofileusingencoding.html
+-(BOOL) appendToFile:(NSString *)path usingEncoding:(NSStringEncoding) encoding {
+    NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path]; 
+    if (fh == nil)
+        return [self writeToFile:path atomically:YES encoding:encoding error:nil];
+    
+    [fh truncateFileAtOffset:[fh seekToEndOfFile]];
+    NSData *encoded = [self dataUsingEncoding:encoding];
+    
+    if (encoded == nil) 
+        return NO;
+    
+    [fh writeData:encoded];
+    return YES;
+}
+
+@end
--- a/project_files/HedgewarsMobile/Classes/SupportViewController.h	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SupportViewController.h	Wed Sep 22 01:10:20 2010 +0200
@@ -1,14 +1,26 @@
-//
-//  SupportViewController.h
-//  Hedgewars
-//
-//  Created by Vittorio on 19/09/10.
-//  Copyright 2010 __MyCompanyName__. All rights reserved.
-//
+/*
+ * Hedgewars-iOS, a Hedgewars port for iOS devices
+ * Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * File created on 19/09/2010.
+ */
+
 
 #import <UIKit/UIKit.h>
 
-
 @interface SupportViewController : UIViewController {
 
 }
--- a/project_files/HedgewarsMobile/Classes/SupportViewController.m	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/SupportViewController.m	Wed Sep 22 01:10:20 2010 +0200
@@ -18,6 +18,7 @@
  * File created on 19/09/2010.
  */
 
+
 #import "SupportViewController.h"
 #import "CommodityFunctions.h"
 
--- a/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Sun Sep 19 22:56:53 2010 +0200
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Wed Sep 22 01:10:20 2010 +0200
@@ -25,6 +25,7 @@
 		1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
 		28FD15000DC6FC520079059D /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 28FD14FF0DC6FC520079059D /* OpenGLES.framework */; };
 		28FD15080DC6FC5B0079059D /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 28FD15070DC6FC5B0079059D /* QuartzCore.framework */; settings = {ATTRIBUTES = (Required, ); }; };
+		611D9B12124949D000008271 /* NSStringExtra.m in Sources */ = {isa = PBXBuildFile; fileRef = 611D9B11124949D000008271 /* NSStringExtra.m */; };
 		611E03E711FA747C0077A41E /* libvorbis.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 611E037C11FA74590077A41E /* libvorbis.a */; };
 		611E0E5111FA92170077A41E /* libfreetype.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 611E0E5011FA92130077A41E /* libfreetype.a */; };
 		611E0EE711FB20610077A41E /* ammoButton.png in Resources */ = {isa = PBXBuildFile; fileRef = 611E0EE511FB20610077A41E /* ammoButton.png */; };
@@ -692,6 +693,8 @@
 		28FD14FF0DC6FC520079059D /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; };
 		28FD15070DC6FC5B0079059D /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
 		32CA4F630368D1EE00C91783 /* Hedgewars_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Hedgewars_Prefix.pch; sourceTree = "<group>"; };
+		611D9B10124949D000008271 /* NSStringExtra.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSStringExtra.h; path = Classes/NSStringExtra.h; sourceTree = "<group>"; };
+		611D9B11124949D000008271 /* NSStringExtra.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSStringExtra.m; path = Classes/NSStringExtra.m; sourceTree = "<group>"; };
 		611E02EC11FA74580077A41E /* cocos2d-iphone.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = "cocos2d-iphone.xcodeproj"; path = "../../../Library/cocos2d/cocos2d-iphone.xcodeproj"; sourceTree = SOURCE_ROOT; };
 		611E0E4B11FA92130077A41E /* freetype.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = freetype.xcodeproj; path = "../../../Library/freetype/Xcode-iPhoneOS/freetype.xcodeproj"; sourceTree = SOURCE_ROOT; };
 		611E0EE511FB20610077A41E /* ammoButton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = ammoButton.png; path = Resources/Overlay/ammoButton.png; sourceTree = "<group>"; };
@@ -972,6 +975,8 @@
 				6165922B11CA9BD500D6E256 /* SquareButtonView.m */,
 				6165922C11CA9BD500D6E256 /* UIImageExtra.h */,
 				6165922D11CA9BD500D6E256 /* UIImageExtra.m */,
+				611D9B10124949D000008271 /* NSStringExtra.h */,
+				611D9B11124949D000008271 /* NSStringExtra.m */,
 			);
 			name = "Other Sources";
 			sourceTree = "<group>";
@@ -2155,6 +2160,7 @@
 				61F2E7CE1205EDE0005734F7 /* AboutViewController.m in Sources */,
 				611EEAEE122B2A4D00DF6938 /* HelpPageViewController.m in Sources */,
 				6199E839124647DE00DADF8C /* SupportViewController.m in Sources */,
+				611D9B12124949D000008271 /* NSStringExtra.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};