/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/ladder_binary.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bnetd/ladder_binary.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 2003 Aaron
3     *
4     * This program is free software; you can redistribute it and/or
5     * modify it under the terms of the GNU General Public License
6     * as published by the Free Software Foundation; either version 2
7     * of the License, or (at your option) any later version.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the Free Software
16     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17     */
18     #define BINARY_LADDER_INTERNAL_ACCESS
19     #include "common/setup_before.h"
20     #ifdef HAVE_STDDEF_H
21     # include <stddef.h>
22     #else
23     # ifndef NULL
24     # define NULL ((void *)0)
25     # endif
26     #endif
27     #ifdef STDC_HEADERS
28     # include <stdlib.h>
29     #else
30     # ifdef HAVE_MALLOC_H
31     # include <malloc.h>
32     # endif
33     #endif
34     #ifdef HAVE_STRING_H
35     # include <string.h>
36     #else
37     # ifdef HAVE_STRINGS_H
38     # include <strings.h>
39     # endif
40     #endif
41     #include "errno.h"
42     #include "compat/strerror.h"
43     #include "account.h"
44     #include "common/eventlog.h"
45     #include "common/xalloc.h"
46     #include "ladder_binary.h"
47     #include "ladder.h"
48     #include "prefs.h"
49     #include "common/setup_after.h"
50    
51     static void dispose_filename(const char * filename)
52     {
53     if (filename) xfree((void*)filename);
54     }
55    
56     static const char * binary_ladder_type_to_filename(t_binary_ladder_types type)
57     {
58     switch (type) {
59     case WAR3_SOLO: return "WAR3_SOLO";
60     case WAR3_TEAM: return "WAR3_TEAM";
61     case WAR3_FFA : return "WAR3_FFA";
62     case WAR3_AT : return "WAR3_AT";
63     case W3XP_SOLO: return "W3XP_SOLO";
64     case W3XP_TEAM: return "W3XP_TEAM";
65     case W3XP_FFA : return "W3XP_FFA";
66     case W3XP_AT : return "W3XP_AT";
67     case STAR_AR : return "STAR_AR";
68     case STAR_AW : return "STAR_AW";
69     case STAR_AG : return "STAR_AG";
70     case STAR_CR : return "STAR_CR";
71     case STAR_CW : return "STAR_CW";
72     case STAR_CG : return "STAR_CG";
73     case SEXP_AR : return "SEXP_AR";
74     case SEXP_AW : return "SEXP_AW";
75     case SEXP_AG : return "SEXP_AG";
76     case SEXP_CR : return "SEXP_CR";
77     case SEXP_CW : return "SEXP_CW";
78     case SEXP_CG : return "SEXP_CG";
79     case W2BN_AR : return "W2BN_AR";
80     case W2BN_AW : return "W2BN_AW";
81     case W2BN_AG : return "W2BN_AG";
82     case W2BN_CR : return "W2BN_CR";
83     case W2BN_CW : return "W2BN_CW";
84     case W2BN_CG : return "W2BN_CG";
85     case W2BN_ARI : return "W2BN_ARI";
86     case W2BN_AWI : return "W2BN_AWI";
87     case W2BN_AGI : return "W2BN_AGI";
88     case W2BN_CRI : return "W2BN_CRI";
89     case W2BN_CWI : return "W2BN_CWI";
90     case W2BN_CGI : return "W2BN_CGI";
91    
92     default:
93     eventlog(eventlog_level_error,__FUNCTION__,"got invalid binary ladder type");
94     return NULL;
95     }
96     }
97    
98     extern int binary_ladder_save(t_binary_ladder_types type, unsigned int paracount, int (*_cb_get_from_ladder)())
99     { int results[10];
100     int rank = 1;
101     const char * ladder_name;
102     const char * filename;
103     int checksum, count;
104     FILE * fp;
105    
106     if ((!(ladder_name = binary_ladder_type_to_filename(type))) ||
107     (!(filename = create_filename(prefs_get_ladderdir(),ladder_name,"_LADDER"))))
108     {
109     eventlog(eventlog_level_error,__FUNCTION__,"NULL filename - aborting");
110     return -1;
111     }
112    
113     if (!(fp = fopen(filename,"wb")))
114     {
115     eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for writing (fopen: %s)",filename,pstrerror(errno));
116     dispose_filename(filename);
117     return -1;
118     }
119    
120     results[0] = magick;
121     fwrite(results,sizeof(int),1,fp); //write the magick int as header
122    
123     checksum = 0;
124    
125     while ((*_cb_get_from_ladder)(type,rank,results)!=-1)
126     {
127     fwrite(results,sizeof(int),paracount,fp);
128     for (count=0;count<paracount;count++) checksum+=results[count];
129     rank++;
130     }
131    
132     //calculate a checksum over saved data
133    
134     results[0] = checksum;
135     fwrite(results,sizeof(int),1,fp); // add checksum at the end
136    
137     fclose(fp);
138     dispose_filename(filename);
139     return 0;
140     }
141    
142     extern t_binary_ladder_load_result binary_ladder_load(t_binary_ladder_types type, unsigned int paracount, int (*_cb_add_to_ladder)())
143     { int values[10];
144     const char * ladder_name;
145     const char * filename;
146     int checksum, count;
147     FILE * fp;
148    
149     //TODO: load from file and if this fails return binary_ladder_load_failed
150     // then make sure ladder gets loaded somehow else (form accounts)
151     // compare checksum - and if it differs return load_invalid
152     // then make sure ladder gets flushed and then loaded from accounts
153     // on success don't load from accounts
154    
155     if ((!(ladder_name = binary_ladder_type_to_filename(type))) ||
156     (!(filename = create_filename(prefs_get_ladderdir(),ladder_name,"_LADDER"))))
157     {
158     eventlog(eventlog_level_error,__FUNCTION__,"NULL filename - aborting");
159     return load_failed;
160     }
161    
162     if (!(fp = fopen(filename,"rb")))
163     {
164     eventlog(eventlog_level_info,__FUNCTION__,"could not open ladder file \"%s\" - maybe ladder still empty",filename,pstrerror(errno));
165     dispose_filename(filename);
166     return load_failed;
167     }
168    
169     if ((fread(values,sizeof(int),1,fp)!=1) || (values[0]!=magick))
170     {
171     eventlog(eventlog_level_error,__FUNCTION__,"ladder file not starting with the magick int");
172     dispose_filename(filename);
173     fclose(fp);
174     return load_failed;
175     }
176    
177     checksum = 0;
178    
179     while (fread(values,sizeof(int),paracount,fp)==paracount)
180     {
181     (*_cb_add_to_ladder)(type,values);
182     for (count=0;count<paracount;count++) checksum+=values[count];
183     }
184    
185     fread(values,sizeof(int),1,fp);
186     if (feof(fp)==0)
187     {
188     eventlog(eventlog_level_error,__FUNCTION__,"got data past end.. fall back to old loading mode");
189     return illegal_checksum;
190    
191     }
192     if (values[0]!=checksum)
193     {
194     eventlog(eventlog_level_error,__FUNCTION__,"ladder file has invalid checksum... fall back to old loading mode");
195     return illegal_checksum;
196     }
197    
198     fclose(fp);
199     eventlog(eventlog_level_info,__FUNCTION__,"successfully loaded %s",filename);
200     dispose_filename(filename);
201     return load_success;
202    
203     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1