13 |
13 |
14 #define BUFFER_SIZE 256 |
14 #define BUFFER_SIZE 256 |
15 |
15 |
16 @implementation GameSetup |
16 @implementation GameSetup |
17 |
17 |
18 @synthesize localeString, systemSettings; |
18 @synthesize systemSettings; |
19 |
19 |
20 -(id) init { |
20 -(id) init { |
21 self = [super init]; |
21 self = [super init]; |
22 self.localeString = [[[NSLocale currentLocale] localeIdentifier] stringByAppendingString:@".txt"]; |
22 srandom(time(NULL)); |
23 self.systemSettings = nil; |
23 ipcPort = (random() % 64541) + 1024 ;//(arc4random() % ((unsigned)64541)) + 1024; |
24 engineProtocolStarted = NO; |
24 |
25 ipcPort = 51432; |
25 NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:@"settings.plist"]; |
|
26 self.systemSettings = [[NSDictionary alloc] initWithContentsOfFile:filePath]; //should check it exists |
26 return self; |
27 return self; |
27 } |
28 } |
28 |
29 |
29 -(void) dealloc { |
30 -(void) dealloc { |
30 if (systemSettings) [self.systemSettings release]; |
|
31 [self.localeString autorelease]; |
|
32 [super dealloc]; |
31 [super dealloc]; |
33 } |
32 } |
34 |
33 |
35 #pragma mark - |
34 #pragma mark - |
36 #pragma mark Thread/Network relevant code |
35 #pragma mark Thread/Network relevant code |
37 -(void) startThread: (NSString *) selector { |
36 -(void) startThread: (NSString *) selector { |
38 SEL usage = NSSelectorFromString(selector); |
37 SEL usage = NSSelectorFromString(selector); |
39 |
38 [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
40 // do not start the server thread because the port is already bound |
|
41 if (NO == engineProtocolStarted) { |
|
42 engineProtocolStarted = YES; |
|
43 [NSThread detachNewThreadSelector:usage toTarget:self withObject:nil]; |
|
44 } |
|
45 } |
39 } |
46 |
40 |
47 -(int) sendToEngine: (NSString *)string { |
41 -(int) sendToEngine: (NSString *)string { |
48 Uint8 length = [string length]; |
42 Uint8 length = [string length]; |
49 |
43 |
50 SDLNet_TCP_Send(csd, &length , 1); |
44 SDLNet_TCP_Send(csd, &length , 1); |
51 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
45 return SDLNet_TCP_Send(csd, [string UTF8String], length); |
52 } |
46 } |
53 |
47 |
54 -(void) engineProtocol { |
48 -(void) engineProtocol { |
|
49 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
55 IPaddress ip; |
50 IPaddress ip; |
56 int idx, eProto; |
51 int idx, eProto; |
57 BOOL serverQuit, clientQuit; |
52 BOOL clientQuit, serverQuit; |
58 char buffer[BUFFER_SIZE], string[BUFFER_SIZE]; |
53 char buffer[BUFFER_SIZE], string[BUFFER_SIZE]; |
59 Uint8 msgSize; |
54 Uint8 msgSize; |
60 Uint16 gameTicks; |
55 Uint16 gameTicks; |
61 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
62 |
56 |
63 if (SDLNet_Init() < 0) { |
57 if (SDLNet_Init() < 0) { |
64 fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError()); |
58 NSLog(@"SDLNet_Init: %s", SDLNet_GetError()); |
65 exit(EXIT_FAILURE); |
59 exit(EXIT_FAILURE); |
66 } |
60 } |
67 |
61 |
68 /* Resolving the host using NULL make network interface to listen */ |
62 /* Resolving the host using NULL make network interface to listen */ |
69 if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) { |
63 if (SDLNet_ResolveHost(&ip, NULL, ipcPort) < 0) { |
70 fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
64 NSLog(@"SDLNet_ResolveHost: %s\n", SDLNet_GetError()); |
71 exit(EXIT_FAILURE); |
65 exit(EXIT_FAILURE); |
72 } |
66 } |
73 |
67 |
74 /* Open a connection with the IP provided (listen on the host's port) */ |
68 /* Open a connection with the IP provided (listen on the host's port) */ |
75 if (!(sd = SDLNet_TCP_Open(&ip))) { |
69 if (!(sd = SDLNet_TCP_Open(&ip))) { |
76 fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError()); |
70 NSLog(@"SDLNet_TCP_Open: %s\n", SDLNet_GetError()); |
77 exit(EXIT_FAILURE); |
71 exit(EXIT_FAILURE); |
78 } |
72 } |
79 |
73 |
80 NSLog(@"engineProtocol - Waiting for a client"); |
74 NSLog(@"engineProtocol - Waiting for a client on port %d", ipcPort); |
81 |
|
82 serverQuit = NO; |
75 serverQuit = NO; |
83 while (!serverQuit) { |
76 while (!serverQuit) { |
84 |
77 |
85 /* This check the sd if there is a pending connection. |
78 /* This check the sd if there is a pending connection. |
86 * If there is one, accept that, and open a new socket for communicating */ |
79 * If there is one, accept that, and open a new socket for communicating */ |
98 |
91 |
99 if ('C' == buffer[0]) { |
92 if ('C' == buffer[0]) { |
100 NSLog(@"engineProtocol - sending game config"); |
93 NSLog(@"engineProtocol - sending game config"); |
101 |
94 |
102 // send config data data |
95 // send config data data |
103 /*seed is arbitrary string |
96 /* |
104 [16:12] unC0Rr: |
97 seed is arbitrary string |
105 addteam <color> <team name> |
98 addteam <color> <team name> |
106 [16:13] unC0Rr: |
|
107 addhh <level> <health> <hedgehog name> |
99 addhh <level> <health> <hedgehog name> |
108 [16:13] unC0Rr: |
100 <level> is 0 for human, 1-5 for bots (5 is the most stupid) |
109 <level> is 0 for human, 1-5 for bots (5 is the most stupid)*/ // local game |
101 */ |
|
102 // local game |
110 [self sendToEngine:@"TL"]; |
103 [self sendToEngine:@"TL"]; |
111 |
104 |
112 // seed info |
105 // seed info |
113 [self sendToEngine:@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}"]; |
106 [self sendToEngine:@"eseed {232c1b42-7d39-4ee6-adf8-4240e1f1efb8}"]; |
114 |
107 |
232 // empty packet or just statistics |
225 // empty packet or just statistics |
233 break; |
226 break; |
234 // missing case for exiting right away |
227 // missing case for exiting right away |
235 } |
228 } |
236 } |
229 } |
237 NSLog(@"Client Exited"); |
230 NSLog(@"Engine exited, closing server"); |
238 // wait a little to let the client close cleanly |
231 // wait a little to let the client close cleanly |
239 sleep(2); |
232 [NSThread sleepForTimeInterval:2]; |
240 // Close the client socket |
233 // Close the client socket |
241 SDLNet_TCP_Close(csd); |
234 SDLNet_TCP_Close(csd); |
|
235 serverQuit = YES; |
242 } |
236 } |
243 |
237 } |
244 } |
238 |
245 |
|
246 SDLNet_TCP_Close(sd); |
239 SDLNet_TCP_Close(sd); |
247 SDLNet_Quit(); |
240 SDLNet_Quit(); |
248 |
241 |
249 [pool release]; |
242 [pool release]; |
250 [NSThread exit]; |
243 [NSThread exit]; |
251 } |
244 } |
252 |
245 |
253 #pragma mark - |
246 #pragma mark - |
254 #pragma mark Settings setup methods |
247 #pragma mark Setting methods |
255 -(void) loadSettingsFromFile:(NSString *)fileName forKey:(NSString *)objName { |
248 -(const char **)getSettings { |
256 NSString *filePath = [[SDLUIKitDelegate sharedAppDelegate] dataFilePath:fileName]; |
249 const char **gameArgs = (const char**) malloc(sizeof(char*) * 7); |
257 |
250 NSString *ipcString = [[NSString alloc] initWithFormat:@"%d", ipcPort]; |
258 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { |
251 NSString *localeString = [[NSString alloc] initWithFormat:@"%@.txt", [[NSLocale currentLocale] localeIdentifier]]; |
259 NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; |
252 |
260 systemSettings = dict; |
253 gameArgs[0] = [[systemSettings objectForKey:@"username"] UTF8String]; //UserNick |
261 //[self setValue:dict forKey:objName]; |
254 gameArgs[1] = [ipcString UTF8String]; //ipcPort |
262 [dict release]; |
255 gameArgs[2] = [[systemSettings objectForKey:@"sounds"] UTF8String]; //isSoundEnabled |
263 } else { |
256 gameArgs[3] = [[systemSettings objectForKey:@"music"] UTF8String]; //isMusicEnabled |
264 //TODO create it |
257 gameArgs[4] = [localeString UTF8String]; //cLocaleFName |
265 [NSException raise:@"File NOT found" format:@"The file %@ was not found at %@", fileName, filePath]; |
258 gameArgs[5] = [[systemSettings objectForKey:@"volume"] UTF8String]; //cInitVolume |
266 } |
259 gameArgs[6] = [[systemSettings objectForKey:@"alternate"] UTF8String]; //cAltDamage |
267 |
260 |
268 } |
261 [localeString release]; |
269 |
262 [ipcString release]; |
270 -(void) unloadSettings { |
263 return gameArgs; |
271 systemSettings = nil; |
|
272 } |
264 } |
273 |
265 |
274 |
266 |
275 @end |
267 @end |