have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path)
authorkoda
Mon, 27 Sep 2010 04:58:15 +0200
changeset 3912 e11df2de6af2
parent 3911 46d7a5cf8ac6
child 3913 f9cb69eecefe
have engine try for a second position when map loading fails (in this way it's possible to move Missions data to any path) remove nsstringextra and implement raw dump when needed
hedgewars/uConsts.pas
hedgewars/uLand.pas
project_files/HedgewarsMobile/Classes/GameSetup.m
project_files/HedgewarsMobile/Classes/MapConfigViewController.m
project_files/HedgewarsMobile/Classes/NSStringExtra.h
project_files/HedgewarsMobile/Classes/NSStringExtra.m
project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj
--- a/hedgewars/uConsts.pas	Mon Sep 27 01:51:12 2010 +0200
+++ b/hedgewars/uConsts.pas	Mon Sep 27 04:58:15 2010 +0200
@@ -40,7 +40,7 @@
 
     TPathType = (ptNone, ptData, ptGraphics, ptThemes, ptCurrTheme, ptTeams, ptMaps,
             ptMapCurrent, ptDemos, ptSounds, ptGraves, ptFonts, ptForts,
-            ptLocale, ptAmmoMenu, ptHedgehog, ptVoices, ptHats, ptFlags);
+            ptLocale, ptAmmoMenu, ptHedgehog, ptVoices, ptHats, ptFlags, ptMissionMaps);
 
     TSprite = (sprWater, sprCloud, sprBomb, sprBigDigit, sprFrame,
             sprLag, sprArrow, sprGrenade, sprTargetP, sprBee,
@@ -460,7 +460,8 @@
         'Graphics/Hedgehog',             // ptHedgehog
         'Sounds/voices',                 // ptVoices
         'Graphics/Hats',                 // ptHats
-        'Graphics/Flags'                 // ptFlags
+        'Graphics/Flags',                // ptFlags
+        'Missions/Maps'                  // ptMissionMaps
     );
 
     cTagsMasks : array[0..15] of byte = (7, 0, 0, 0, 15, 6, 4, 5, 0, 0, 0, 0, 0, 14, 12, 13);
--- a/hedgewars/uLand.pas	Mon Sep 27 01:51:12 2010 +0200
+++ b/hedgewars/uLand.pas	Mon Sep 27 04:58:15 2010 +0200
@@ -52,7 +52,7 @@
 function  LandBackPixel(x, y: LongInt): LongWord;
 
 implementation
-uses uConsole, uStore, uMisc, uRandom, uTeams, uLandObjects, Adler32, uIO, uLandTexture;
+uses uConsole, uStore, uMisc, uRandom, uTeams, uLandObjects, Adler32, uIO, uLandTexture, sysutils;
 
 operator=(const a, b: direction) c: Boolean;
 begin
@@ -1165,22 +1165,35 @@
 var tmpsurf: PSDL_Surface;
     s: shortstring;
     f: textfile;
+    mapName: shortstring;
 begin
 isMap:= true;
 WriteLnToConsole('Loading land from file...');
 AddProgress;
-tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/map', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
+tmpsurf:= LoadImage(Pathz[ptMapCurrent] + '/map', ifAlpha or ifTransparent or ifIgnoreCaps);
+if tmpsurf = nil then
+begin
+    mapName:= ExtractFileName(Pathz[ptMapCurrent]);
+    tmpsurf:= LoadImage(Pathz[ptMissionMaps] + '/' + mapName + '/map', ifAlpha or ifCritical or ifTransparent or ifIgnoreCaps);
+end;
 TryDo((tmpsurf^.w <= LAND_WIDTH) and (tmpsurf^.h <= LAND_HEIGHT), 'Map dimensions too big!', true);
 
 // unC0Rr - should this be passed from the GUI? I am not sure which layer does what
 s:= Pathz[ptMapCurrent] + '/map.cfg';
 WriteLnToConsole('Fetching map HH limit');
+{$I-}
 Assign(f, s);
 filemode:= 0; // readonly
 Reset(f);
+if IOResult <> 0 then
+begin
+    s:= Pathz[ptMissionMaps] + '/' + mapName + '/map.cfg';
+    Assign(f, s);
+    Reset(f);
+end;
 Readln(f);
 if not eof(f) then Readln(f, MaxHedgehogs);
-
+{$I+}
 if (MaxHedgehogs = 0) then MaxHedgehogs:= 18;
 
 playHeight:= tmpsurf^.h;
--- a/project_files/HedgewarsMobile/Classes/GameSetup.m	Mon Sep 27 01:51:12 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/GameSetup.m	Mon Sep 27 04:58:15 2010 +0200
@@ -251,11 +251,21 @@
     [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil];
 }
 
+-(void) dumpRawData:(const uint8_t*)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:buffer maxLength:length];
+    [os close];
+    [os release];
+}
+
 // 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];
+    [self dumpRawData:(const uint8_t *)[string UTF8String] ofSize:length];
     SDLNet_TCP_Send(csd, &length, 1);
     return SDLNet_TCP_Send(csd, [string UTF8String], length);
 }
@@ -304,8 +314,6 @@
     SDLNet_TCP_Close(sd);
 
     while (!clientQuit) {
-        NSString *msgToSave = nil;
-        NSOutputStream *os = nil;
         msgSize = 0;
         memset(buffer, '\0', BUFFER_SIZE);
         if (SDLNet_TCP_Recv(csd, &msgSize, sizeof(uint8_t)) <= 0)
@@ -322,7 +330,7 @@
                 else
                     [self sendToEngineNoSave:@"TL"];
                 NSString *saveHeader = @"TS";
-                [[NSString stringWithFormat:@"%c%@",[saveHeader length], saveHeader] appendToFile:savePath];
+                [self dumpRawData:(const uint8_t *)[saveHeader UTF8String] ofSize:[saveHeader length]];
 
                 // seed info
                 [self sendToEngine:[self.gameConfig objectForKey:@"seed_command"]];
@@ -367,9 +375,8 @@
                 clientQuit = YES;
                 break;
             case 'e':
-                msgToSave = [NSString stringWithFormat:@"%c%s",msgSize,buffer];                
-                [msgToSave appendToFile:self.savePath];
-                
+                [self dumpRawData:buffer ofSize:msgSize];
+
                 sscanf((char *)buffer, "%*s %d", &eProto);
                 short int netProto = 0;
                 char *versionStr;
@@ -404,13 +411,7 @@
                 setGameRunning(NO);
                 break;
             default:
-                // 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];
+                [self dumpRawData:buffer ofSize:msgSize];
                 break;
         }
     }
--- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Mon Sep 27 01:51:12 2010 +0200
+++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Mon Sep 27 04:58:15 2010 +0200
@@ -171,6 +171,7 @@
     
     self.themeCommand = [NSString stringWithFormat:@"etheme %@", [split objectAtIndex:0]];
     self.staticMapCommand = [NSString stringWithFormat:@"emap %@", [source objectAtIndex:index]];
+
     if (scIndex != 3)
         self.missionCommand = @"";
     else
--- a/project_files/HedgewarsMobile/Classes/NSStringExtra.h	Mon Sep 27 01:51:12 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-/*
- * 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
--- a/project_files/HedgewarsMobile/Classes/NSStringExtra.m	Mon Sep 27 01:51:12 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-/*
- * 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;
-}
-
--(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 {
-    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/Hedgewars.xcodeproj/project.pbxproj	Mon Sep 27 01:51:12 2010 +0200
+++ b/project_files/HedgewarsMobile/Hedgewars.xcodeproj/project.pbxproj	Mon Sep 27 04:58:15 2010 +0200
@@ -25,7 +25,6 @@
 		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 */; };
 		611D9BFB12497E9800008271 /* SavedGamesViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 611D9BF912497E9800008271 /* SavedGamesViewController.m */; };
 		611D9BFC12497E9800008271 /* SavedGamesViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 611D9BFA12497E9800008271 /* SavedGamesViewController.xib */; };
 		611DA031124E2BC500008271 /* background_med.png in Resources */ = {isa = PBXBuildFile; fileRef = 611DA030124E2BC500008271 /* background_med.png */; };
@@ -721,8 +720,6 @@
 		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>"; };
 		611D9BF812497E9800008271 /* SavedGamesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SavedGamesViewController.h; sourceTree = "<group>"; };
 		611D9BF912497E9800008271 /* SavedGamesViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SavedGamesViewController.m; sourceTree = "<group>"; };
 		611D9BFA12497E9800008271 /* SavedGamesViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SavedGamesViewController.xib; sourceTree = "<group>"; };
@@ -1014,8 +1011,6 @@
 				6165922B11CA9BD500D6E256 /* SquareButtonView.m */,
 				6165922C11CA9BD500D6E256 /* UIImageExtra.h */,
 				6165922D11CA9BD500D6E256 /* UIImageExtra.m */,
-				611D9B10124949D000008271 /* NSStringExtra.h */,
-				611D9B11124949D000008271 /* NSStringExtra.m */,
 				619C5BA0124FA59000D041AE /* MapPreviewButtonView.h */,
 				619C5BA1124FA59000D041AE /* MapPreviewButtonView.m */,
 			);
@@ -2253,7 +2248,6 @@
 				61F2E7CE1205EDE0005734F7 /* AboutViewController.m in Sources */,
 				611EEAEE122B2A4D00DF6938 /* HelpPageViewController.m in Sources */,
 				6199E839124647DE00DADF8C /* SupportViewController.m in Sources */,
-				611D9B12124949D000008271 /* NSStringExtra.m in Sources */,
 				611D9BFB12497E9800008271 /* SavedGamesViewController.m in Sources */,
 				619C5AF4124F7E3100D041AE /* LuaPas.pas in Sources */,
 				619C5BA2124FA59000D041AE /* MapPreviewButtonView.m in Sources */,