-
Notifications
You must be signed in to change notification settings - Fork 9
/
sparrowNet.c
2790 lines (2646 loc) · 72.8 KB
/
sparrowNet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of sparrow3d.
* Sparrow3d 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, either version 2 of the License, or
* (at your option) any later version.
*
* Sparrow3d 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 Foobar. If not, see <http://www.gnu.org/licenses/>
*
* For feedback and questions about my Files and Projects please mail me,
* Alexander Matthes (Ziz) , zizsdl_at_googlemail.com */
#include "sparrowNet.h"
#include <stdio.h>
#include <errno.h>
#include <math.h>
#ifdef __GNUC__
#include <sys/stat.h>
#endif
#ifndef _WIN32
#include <unistd.h>
#endif
//#define DEBUG_MESSAGES
//This is a copy of spReadOneLine sparrowFile. However, I don't want the
//extra dependency of libSparrow3d or linking sparrowFile twice.
int internal_spNet_spReadOneLine( SDL_RWops* file , char* buffer, int buffer_len)
{
int pos = 0;
buffer[pos] = 0;
while (pos < buffer_len)
{
if (SDL_RWread( file, &(buffer[pos]), 1, 1 ) <= 0)
return 1; //EOF
if ( buffer[pos] == '\n' )
break;
if (buffer[pos] != '\r') //fucking windows line break
pos++;
}
buffer[pos] = 0;
return 0; //not EOF
}
#define DIGIT_CHECK(v) \
((v) == '0' || (v) == '1' || \
(v) == '2' || (v) == '3' || \
(v) == '4' || (v) == '5' || \
(v) == '6' || (v) == '7' || \
(v) == '8' || (v) == '9')
PREFIX double internal_spNet_spAtoFloat( char* buffer )
{
int i = 0;
while (buffer[i] == ' ' && buffer[i] != 0)
i++;
if (buffer[i] == 0)
return 0.0f;
double result = 0.0f;
double sign = 1.0f;
Sint64 left = 0;
Sint64 middle = 0;
Sint64 divisor = 1;
Sint64 right = 0;
//sign
if (buffer[i] == '-')
{
sign = -1.0f;
i++;
}
//left part
while ( DIGIT_CHECK(buffer[i]) )
{
left *= 10;
int digit = (int)buffer[i] - (int)'0';
left += digit;
i++;
}
//middle part
if (buffer[i] == '.')
{
i++;
while ( DIGIT_CHECK(buffer[i]) )
{
middle *= 10;
int digit = (int)buffer[i] - (int)'0';
middle += digit;
divisor *= 10;
i++;
}
}
//right part
if (buffer[i] == 'e' || buffer[i] == 'E')
{
i++;
while ( DIGIT_CHECK(buffer[i]) )
{
right *= 10;
int digit = (int)buffer[i] - (int)'0';
right += digit;
i++;
}
}
if (right)
return sign*((double)left + (double)middle/(double)divisor)*pow(10.0f,(double)right);
else
return sign*((double)left + (double)middle/(double)divisor);
}
spNetC4ATaskPointer spGlobalC4ATask = NULL;
SDL_mutex* spCacheMutex = NULL;
spNetC4ATaskPointer createNewC4ATask( void )
{
spNetC4ATaskPointer task = (spNetC4ATaskPointer)malloc(sizeof(spNetC4ATask));
task->statusMutex = SDL_CreateMutex();
task->status = 0;
task->dataPointer = NULL;
task->timeOut = 0;
task->thread = NULL;
task->result = 0;
task->threadStatus = 0;
task->message = 0;
return task;
}
void spNetC4ADeleteTask(spNetC4ATaskPointer task)
{
SDL_DestroyMutex(task->statusMutex);
free(task);
}
PREFIX void spInitNet( void )
{
if(SDLNet_Init()==-1)
printf("SDLNet_Init: %s\n", SDLNet_GetError());
spGlobalC4ATask = createNewC4ATask();
spCacheMutex = SDL_CreateMutex();
}
void fill_ip_struct(spNetIPPointer ip)
{
ip->type = IPV4;
ip->address.ipv4 = ip->sdl_address.host; //bytes are set automaticly, yeah!
ip->port = ip->sdl_address.port;
}
PREFIX spNetIP spNetResolve(char* host,Uint16 port)
{
spNetIP result;
SDLNet_ResolveHost(&(result.sdl_address), host, port);
fill_ip_struct(&result);
return result;
}
PREFIX char* spNetResolveHost(spNetIP ip,char* host,int host_len)
{
const char* sdlHost = SDLNet_ResolveIP(&(ip.sdl_address));
if (strlen(sdlHost) >= host_len)
{
host = NULL;
return NULL;
}
sprintf(host,"%s",sdlHost);
return host;
}
PREFIX spNetTCPConnection spNetOpenClientTCP(spNetIP ip)
{
spNetTCPConnection result;
result=SDLNet_TCP_Open(&(ip.sdl_address));
if(!result) {
printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
return NULL;
}
return result;
}
PREFIX spNetTCPServer spNetOpenServerTCP(Uint16 port)
{
IPaddress ip;
spNetTCPServer result;
if(SDLNet_ResolveHost(&ip,NULL,port)==-1) {
printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
return NULL;
}
result=SDLNet_TCP_Open(&ip);
if(!result) {
printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
return NULL;
}
return result;
}
PREFIX spNetTCPConnection spNetAcceptTCP(spNetTCPServer server)
{
return SDLNet_TCP_Accept(server);
}
PREFIX spNetIP spNetGetConnectionIP(spNetTCPConnection connection)
{
IPaddress *temp_ip;
temp_ip=SDLNet_TCP_GetPeerAddress(connection);
spNetIP result;
if(!temp_ip) {
printf("SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());
printf("This may be a server socket.\n");
printf("However, the ip may not be valid!\n");
}
result.sdl_address = *temp_ip;
fill_ip_struct(&result);
return result;
}
PREFIX int spNetSendTCP(spNetTCPConnection connection,void* data,int length)
{
return SDLNet_TCP_Send(connection,data,length);
}
PREFIX int spNetSendHTTP(spNetTCPConnection connection,char* data)
{
return spNetSendTCP(connection,data,strlen(data)+1);
}
typedef struct receivingStruct *receivingPointer;
typedef struct receivingStruct {
spNetTCPConnection connection;
void* data;
int length;
SDL_mutex* mutex;
int done;
SDL_Thread* thread;
int result;
receivingPointer next;
} receivingType;
int tcpReceiveThread(void* data)
{
receivingPointer tcpData = (receivingPointer)data;
int res=spNetReceiveTCP(tcpData->connection,tcpData->data,tcpData->length);
SDL_mutexP(tcpData->mutex);
tcpData->done = 1;
tcpData->result = res;
SDL_mutexV(tcpData->mutex);
return res;
}
int tcpReceiveThread_http(void* data)
{
receivingPointer tcpData = (receivingPointer)data;
int res=spNetReceiveHTTP(tcpData->connection,(char*)tcpData->data,tcpData->length);
SDL_mutexP(tcpData->mutex);
tcpData->done = 1;
tcpData->result = res;
SDL_mutexV(tcpData->mutex);
return res;
}
receivingPointer firstReceiving = NULL;
SDL_Thread* allreadyReceiving(spNetTCPConnection connection)
{
receivingPointer before = NULL;
receivingPointer mom = firstReceiving;
while (mom)
{
if (mom->connection == connection)
{
SDL_mutexP(mom->mutex);
if (mom->done)
{
SDL_mutexV(mom->mutex); //The Thread lost the interest on this struct
//Removing mom
if (before)
{
SDL_mutexP(before->mutex);
before->next = mom->next;
SDL_mutexV(before->mutex);
}
else
firstReceiving = mom->next;
SDL_DestroyMutex(mom->mutex);
if (mom->result<=0) //connection destroyed!
{
free(mom);
return (SDL_Thread*)(-1);
}
free(mom);
return NULL;
}
SDL_mutexV(mom->mutex);
return mom->thread;
}
before = mom;
mom = mom->next;
}
return NULL;
}
PREFIX int spNetReceiveTCP(spNetTCPConnection connection,void* data,int length)
{
char* data_pointer = (char*)data;
return SDLNet_TCP_Recv(connection,&(data_pointer[0]),length);
}
PREFIX SDL_Thread* spNetReceiveTCPUnblocked(spNetTCPConnection connection,void* data,int length)
{
SDL_Thread* thread;
if (thread = allreadyReceiving(connection))
return thread;
receivingPointer tcpData = (receivingPointer)malloc(sizeof(receivingType));
tcpData->connection = connection;
tcpData->data = data;
tcpData->length = length;
tcpData->connection = connection;
tcpData->done = 0;
tcpData->mutex = SDL_CreateMutex();
tcpData->next = firstReceiving;
firstReceiving = tcpData;
tcpData->thread = SDL_CreateThread(tcpReceiveThread,tcpData);
return tcpData->thread;
}
PREFIX int spNetReceiveHTTP(spNetTCPConnection connection,char* data,int length)
{
int received = 0;
while (length > 0)
{
int new_received = spNetReceiveTCP(connection,&(data[received]),length);
received+=new_received;
length-=new_received;
if (new_received == 0)
return received;
}
return received;
}
PREFIX SDL_Thread* spNetReceiveHTTPUnblocked(spNetTCPConnection connection,char* data,int length)
{
SDL_Thread* thread;
if (thread = allreadyReceiving(connection))
return thread;
receivingPointer tcpData = (receivingPointer)malloc(sizeof(receivingType));
tcpData->connection = connection;
tcpData->data = data;
tcpData->length = length;
tcpData->connection = connection;
tcpData->done = 0;
tcpData->mutex = SDL_CreateMutex();
tcpData->next = firstReceiving;
firstReceiving = tcpData;
tcpData->thread = SDL_CreateThread(tcpReceiveThread_http,tcpData);
return tcpData->thread;
}
PREFIX int spNetReceiveStillWaiting(SDL_Thread* thread)
{
receivingPointer before = NULL;
receivingPointer mom = firstReceiving;
while (mom)
{
if (mom->thread == thread)
{
SDL_mutexP(mom->mutex);
if (mom->done)
{
SDL_mutexV(mom->mutex); //The Thread lost the interest on this struct
//Removing mom
if (before)
{
SDL_mutexP(before->mutex);
before->next = mom->next;
SDL_mutexV(before->mutex);
}
else
firstReceiving = mom->next;
SDL_DestroyMutex(mom->mutex);
free(mom);
return 0;
}
SDL_mutexV(mom->mutex);
return 1;
}
before = mom;
mom = mom->next;
}
return 0;
}
PREFIX int spNetReceiveFinished(SDL_Thread* thread)
{
receivingPointer before = NULL;
receivingPointer mom = firstReceiving;
while (mom)
{
if (mom->thread == thread)
{
SDL_mutexP(mom->mutex);
if (mom->done)
{
SDL_mutexV(mom->mutex); //The Thread lost the interest on this struct
//Removing mom
if (before)
{
SDL_mutexP(before->mutex);
before->next = mom->next;
SDL_mutexV(before->mutex);
}
else
firstReceiving = mom->next;
SDL_DestroyMutex(mom->mutex);
int res = mom->result;
free(mom);
return res;
}
SDL_mutexV(mom->mutex);
return 0;
}
before = mom;
mom = mom->next;
}
return -1;
}
PREFIX void spNetCloseTCP(spNetTCPConnection connection)
{
SDLNet_TCP_Close(connection);
}
PREFIX void spQuitNet( void )
{
spNetC4ADeleteTask(spGlobalC4ATask);
spGlobalC4ATask = NULL;
SDL_mutexP(spCacheMutex);
SDL_DestroyMutex(spCacheMutex);
SDLNet_Quit();
}
#ifdef PANDORA
#include "pnd_locate.h"
#endif
typedef struct getgenericStruct *getgenericPointer;
typedef struct getgenericStruct {
spNetC4ATaskPointer task;
int ( *function )( void* data );
} getgenericType;
typedef struct getgameStruct *getgamePointer;
typedef struct getgameStruct {
spNetC4ATaskPointer task;
int ( *function )( void* data );
spNetC4AGamePointer* game;
} getgameType;
typedef struct getscoreStruct *getscorePointer;
typedef struct getscoreStruct {
spNetC4ATaskPointer task;
int ( *function )( void* data );
spNetC4AScorePointer* score;
spNetC4AProfilePointer profile;
int year;
int month;
char game[256];
} getscoreType;
typedef struct commitStruct *commitPointer;
typedef struct commitStruct {
spNetC4ATaskPointer task;
int ( *function )( void* data );
spNetC4AProfilePointer profile;
char game[256];
int score;
char system[256];
spNetC4AScorePointer* scoreList;
} commitType;
typedef struct createStruct *createPointer;
typedef struct createStruct {
spNetC4ATaskPointer task;
int ( *function )( void* data );
spNetC4AProfilePointer* profile;
char longname[256];
char shortname[256];
char password[256];
char email[256];
int deleteFile;
} createType;
int spNetC4ACaching = 0;
char spCacheFilename[256] = "";
//This is usefull for debugging without threading influences:
/*#define SDL_CreateThread SDL_CreateThreadWithoutThreading
SDL_Thread* SDL_CreateThreadWithoutThreading(int (*fn)(void *),void* data)
{
fn(data);
return NULL;
}*/
typedef struct cacheStruct *cachePointer;
typedef struct cacheStruct {
char game[256];
char system[256];
char prid[256];
int score;
cachePointer next;
} cacheType;
cachePointer read_cache( void )
{
SDL_RWops *file = SDL_RWFromFile(spCacheFilename, "rb");
if (file)
{
char game[256];
char system[256];
char prid[256];
int score;
cachePointer cache = NULL;
cachePointer last = NULL;
while (1)
{
if (SDL_RWread(file,game,256,1) <= 0)
break;
if (SDL_RWread(file,system,256,1) <= 0)
break;
if (SDL_RWread(file,prid,256,1) <= 0)
break;
if (SDL_RWread(file,&score,sizeof(int),1) <= 0)
break;
cachePointer new_cache = (cachePointer)malloc(sizeof(cacheType));
memcpy(new_cache->game,game,256);
memcpy(new_cache->system,system,256);
memcpy(new_cache->prid,prid,256);
new_cache->score = score;
new_cache->next = NULL;
if (last)
last->next = new_cache;
else
cache = new_cache;
last = new_cache;
}
SDL_RWclose(file);
return cache;
}
return NULL;
}
void write_to_cache(char* game,char* system,char* prid,int score,int lock)
{
if (lock)
SDL_mutexP(spCacheMutex);
cachePointer cache = NULL;
if (spNetC4ACaching != 1)
cache = read_cache();
//Searching one of game
cachePointer mom = cache;
int nr = 0;
while (mom)
{
if (strcmp(mom->game,game) == 0)
break;
nr++;
mom = mom->next;
}
if (mom)
{
if ((spNetC4ACaching == 2 && mom->score < score) ||
(spNetC4ACaching == 3 && mom->score > score))
{
//Seek and rewrite
SDL_RWops *file = SDL_RWFromFile(spCacheFilename, "r+b");
SDL_RWseek(file,nr*(256*3+sizeof(int))+256*3,SEEK_SET);
SDL_RWwrite(file,&score,sizeof(int),1);
SDL_RWclose(file);
}
}
else
{
SDL_RWops *file = SDL_RWFromFile(spCacheFilename, "ab");
SDL_RWwrite(file,game,256,1);
SDL_RWwrite(file,system,256,1);
SDL_RWwrite(file,prid,256,1);
SDL_RWwrite(file,&score,sizeof(int),1);
SDL_RWclose(file);
}
while (cache)
{
mom = cache->next;
free(cache);
cache = mom;
}
if (lock)
SDL_mutexV(spCacheMutex);
}
int spNetC4AUberThread(getgenericPointer data)
{
int startTime = SDL_GetTicks();
SDL_Thread* thread = SDL_CreateThread(data->function,data);
while (1)
{
#ifdef REALGP2X
//TODO: Implement!
SDL_Delay(100);
#elif defined _WIN32
SDL_Delay(100);
#else
usleep(100000);
#endif
int newTime = SDL_GetTicks();
int diff = newTime - startTime;
startTime = newTime;
data->task->timeOut -= diff;
SDL_mutexP(data->task->statusMutex);
int status = data->task->status;
SDL_mutexV(data->task->statusMutex);
//only 1 second left, lets send a message
if (data->task->message == 0 && (status == SP_C4A_CANCELED || data->task->timeOut <= 1000))
data->task->message = 1;
//time is over. If the message is reset we assume the thread will finish, otherwise...
if (data->task->message == 1 && (status == SP_C4A_CANCELED || data->task->timeOut <= 0))
{
//Waiting for the write cache mutex ANYWAY.
SDL_mutexP(spCacheMutex);
SDL_KillThread(thread);
SDL_mutexV(spCacheMutex);
data->task->result = 1;
SDL_mutexP(data->task->statusMutex);
if (data->task->timeOut <= 0)
data->task->status = SP_C4A_TIMEOUT;
data->task->threadStatus = 0;
SDL_mutexV(data->task->statusMutex);
int result = data->task->result;
data->task->dataPointer = NULL;
free(data);
return result;
}
if (status <= 0) //finished somehow
{
SDL_WaitThread(thread,&(data->task->result));
SDL_mutexP(data->task->statusMutex);
data->task->threadStatus = 0;
SDL_mutexV(data->task->statusMutex);
int result = data->task->result;
data->task->dataPointer = NULL;
free(data);
return result;
}
}
}
char* my_strchr(char* buffer, char c, char ignore)
{
int i;
int in_ignore = 0;
for (i = 0; buffer[i]!=0; i++)
{
if (buffer[i] == ignore)
in_ignore = 1-in_ignore;
if (!in_ignore && buffer[i] == c)
return &(buffer[i]);
}
return NULL;
}
void fill_between_paraphrases(char* buffer, char* dest, int max_size)
{
int i,j = 0;
int in_paraphrases = 0;
for (i = 0; buffer[i]!=0; i++)
{
if (buffer[i] == '\"')
{
switch (in_paraphrases)
{
case 0: in_paraphrases = 1; break;
case 1: dest[j]=0;return;
}
continue;
}
if (in_paraphrases)
{
dest[j] = buffer[i];
j++;
if (j == max_size)
{
dest[j-1] = 0;
return;
}
}
}
}
void internal_CreateDirectoryChain( const char* directories)
{
//Creating copy:
int len = strlen(directories)+1;
#ifdef __GNUC__
char directoriesCopy[len];
#else
char* directoriesCopy = (char*)malloc( len * sizeof(char) );
#endif
memcpy(directoriesCopy,directories,len);
//Splitting in subdirectories
char* subString = directoriesCopy;
char* endOfString = strchr(subString,'/');
if (endOfString == NULL)
endOfString = strchr(subString,0);
while (endOfString)
{
char oldChar = endOfString[0];
endOfString[0] = 0;
#ifdef _WIN32
if (CreateDirectory(directoriesCopy,NULL))
{}
else
if (GetLastError() != ERROR_ALREADY_EXISTS)
break;
#else
int error = mkdir(directoriesCopy,0777);
if (errno == 0 || errno == EEXIST || errno == ENOENT) //thats okay :)
{}
else //not okay
break;
#endif
endOfString[0] = oldChar;
if (oldChar == 0)
break;
subString = &(endOfString[1]);
endOfString = strchr(subString,'/');
if (endOfString == NULL)
endOfString = strchr(subString,0);
}
#ifndef __GNUC__
free(directoriesCopy);
#endif
}
#ifdef PANDORA
#define PROFILE_FILENAME_MAKRO char *locate_filename = pnd_locate_filename ( "/media/*/pandora/appdata/c4a-mame/:.", "c4a-prof" ); \
char filename[256] = "./c4a-prof"; \
if (locate_filename) \
sprintf(filename,"%s",locate_filename); \
else \
{ \
locate_filename = pnd_locate_filename ( "/media/*/pandora/:.", "appdata" ); \
if (locate_filename) \
{ \
sprintf(filename,"%s/c4a-mame",locate_filename); \
internal_CreateDirectoryChain(filename); \
sprintf(filename,"%s/c4a-mame/c4a-prof",locate_filename); \
} \
}
#elif defined GCW || (defined DESKTOP && !defined _WIN32)
#define PROFILE_FILENAME_MAKRO char filename[256]; \
sprintf(filename,"%s/.config/compo4all",getenv("HOME"));\
internal_CreateDirectoryChain(filename);\
sprintf(filename,"%s/.config/compo4all/c4a-prof",getenv("HOME"));
#else
#define PROFILE_FILENAME_MAKRO char filename[256] = "./c4a-prof";
#endif
PREFIX void spNetC4ASetCaching(int value)
{
spNetC4ACaching = value;
}
void set_cache_filename( void )
{
PROFILE_FILENAME_MAKRO
sprintf(spCacheFilename,"%s",filename);
sprintf(&spCacheFilename[strlen(spCacheFilename)-4],"cache");
}
PREFIX spNetC4AProfilePointer spNetC4AGetProfile( void )
{
set_cache_filename();
spNetC4AProfilePointer profile = NULL;
PROFILE_FILENAME_MAKRO
//Parsing the file
SDL_RWops *file=SDL_RWFromFile(filename,"rb");
if (file == NULL)
return NULL;
profile = (spNetC4AProfilePointer)malloc(sizeof(spNetC4AProfile));
char buffer[2048];
internal_spNet_spReadOneLine(file,buffer,2048);
internal_spNet_spReadOneLine(file,buffer,2048);
char* pos = strstr( buffer, "\"longname\":");
pos+=11;
fill_between_paraphrases( pos, profile->longname, 256);
pos = strstr( buffer, "\"shortname\":");
pos+=12;
fill_between_paraphrases( pos, profile->shortname, 256);
pos = strstr( buffer, "\"prid\":");
pos+=7;
fill_between_paraphrases( pos, profile->prid, 256);
pos = strstr( buffer, "\"email\":");
pos+=8;
fill_between_paraphrases( pos, profile->email, 256);
pos = strstr( buffer, "\"password\":");
pos+=11;
fill_between_paraphrases( pos, profile->password, 256);
SDL_RWclose(file);
return profile;
}
PREFIX void spNetC4AFreeProfile(spNetC4AProfilePointer profile)
{
if (profile)
free(profile);
}
int c4a_getgame_thread(void* data)
{
getgamePointer gameData = ((getgamePointer)data);
spNetIP ip = spNetResolve("skeezix.wallednetworks.com",13001);
if (ip.address.ipv4 == SP_INVALID_IP)
{
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
spNetTCPConnection connection = spNetOpenClientTCP(ip);
if (connection == NULL)
{
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
char get_string[512] = "GET /curgamelist_1\n\n";
if (spNetSendHTTP(connection,get_string) == 0)
{
spNetCloseTCP(connection);
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
//skeezix saves the top500. So 100 byte should be enough...
//Haha. NOT! minislug had 50950 with a top 500...
char buffer[100001];
int length;
if ((length = spNetReceiveHTTP(connection,buffer,100000)) == 0)
{
spNetCloseTCP(connection);
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
buffer[length] = 0;
spNetCloseTCP(connection);
//Searching the first [
char* found = strchr( buffer, '[' );
if (found == NULL)
{
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
//Reading game by game
//Searching the starting {
while (found)
{
char* start = strchr( found, '{' );
if (start == NULL)
{
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
char* end = my_strchr( start, '}', '\"'); //ignore "text}"-parts
if (start == NULL)
{
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_ERROR;
SDL_mutexV(gameData->task->statusMutex);
return 1;
}
//Creating substring:
end[0] = 0;
//Now we search in the substring
//Search for the long name:
char* pos = strstr( start, "\"longname\":");
pos+=11;
char longname[256];
fill_between_paraphrases( pos, longname, 128);
pos = strstr( start, "\"gamename\":");
pos+=11;
char shortname[256];
fill_between_paraphrases( pos, shortname, 128);
pos = strstr( start, "\"genre\":");
pos+=8;
char genre[256];
fill_between_paraphrases( pos, genre, 128);
pos = strstr( start, "\"field\":");
pos+=8;
char field[256];
fill_between_paraphrases( pos, field, 128);
pos = strstr( start, "\"status\":");
pos+=9;
char status[256];
fill_between_paraphrases( pos, status, 128);
//Re"inserting" substring:
end[0] = '}';
found = strchr( end, '{' );
//Adding the found stuff to the array:
spNetC4AGamePointer new_game = (spNetC4AGamePointer)malloc(sizeof(spNetC4AGame));
sprintf(new_game->longname,"%s",longname);
sprintf(new_game->shortname,"%s",shortname);
sprintf(new_game->genre,"%s",genre);
if (strcmp(status,"available") == 0)
new_game->status = 1;
else
if (strcmp(status,"active") == 0)
new_game->status = 2;
else
new_game->status = 0;
if (strcmp(field,"arcade") == 0)
new_game->field = 1;
else
if (strcmp(field,"indie") == 0)
new_game->field = 0;
else
new_game->field = -1;
//sorted insert
//Searching the next and before element:
spNetC4AGamePointer before = NULL;
spNetC4AGamePointer next = *(gameData->game);
while (next)
{
if (strcmp(new_game->longname,next->longname) < 0)
break;
before = next;
next = next->next;
}
if (before == NULL) //new first element!
{
new_game->next = next;
(*(gameData->game)) = new_game;
}
else
{
before->next = new_game;
new_game->next = next;
}
}
SDL_mutexP(gameData->task->statusMutex);
gameData->task->status = SP_C4A_OK;
SDL_mutexV(gameData->task->statusMutex);
return 0;
}
PREFIX int spNetC4AGetGame(spNetC4AGamePointer* gameList,int timeOut)
{
(*gameList) = NULL;
SDL_mutexP(spGlobalC4ATask->statusMutex);
if (spGlobalC4ATask->status != SP_C4A_PROGRESS)
{
spGlobalC4ATask->status = SP_C4A_PROGRESS;
SDL_mutexV(spGlobalC4ATask->statusMutex);
//Starting a background thread, which does the fancy stuff
getgamePointer data = (getgamePointer)malloc(sizeof(getgameType));
data->function = c4a_getgame_thread;
data->task = spGlobalC4ATask;
data->game = gameList;
spGlobalC4ATask->dataPointer = data;
spGlobalC4ATask->timeOut = timeOut;
spGlobalC4ATask->threadStatus = 1;
#ifdef _MSC_VER
spGlobalC4ATask->thread = SDL_CreateThread((int (__cdecl *)(void *))spNetC4AUberThread,data);
#else
spGlobalC4ATask->thread = SDL_CreateThread((int (*)(void *))spNetC4AUberThread,data);
#endif
return 0;