source: abuse/trunk/src/nfclient.cpp @ 555

Last change on this file since 555 was 555, checked in by Sam Hocevar, 10 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 5.3 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if (defined(__MACH__) || !defined(__APPLE__))
16#   include <sys/types.h>
17#endif
18#include <fcntl.h>
19#include <unistd.h>
20#include <ctype.h>
21
22#include "common.h"
23
24#include "netface.h"
25
26#include "specs.h"
27#include "nfserver.h"
28#include "dprint.h"
29#include "crc.h"
30#include "cache.h"
31
32#include "gserver.h"
33
34#if !defined __CELLOS_LV2__
35
36void remove_client(int client_number) { ; }
37
38CrcManager *net_crcs = NULL;
39extern net_protocol *prot;
40
41class nfs_file : public bFILE
42{
43  jFILE *local;
44  int nfs_fd;
45  int offset;
46  public :
47  nfs_file(char const *filename, char const *mode);
48  virtual int open_failure();
49  virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
50  int new_read(void *buf, size_t count);       // returns number of bytes read
51  virtual int unbuffered_write(void const *buf, size_t count);      // returns number of bytes written
52  virtual int unbuffered_seek(long offset, int whence);  // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
53  virtual int unbuffered_tell();
54  virtual int file_size();
55  virtual ~nfs_file();
56} ;
57
58bFILE *open_nfs_file(char const *filename, char const *mode)
59{
60  return new nfs_file(filename,mode);
61}
62
63
64nfs_file::nfs_file(char const *filename, char const *mode)
65{
66  local=NULL;
67  nfs_fd=-1;
68
69  int local_only=0;
70  char const *s=mode;
71  for (; *s; s++)    // check to see if writeable file, if so don't go through nfs
72    if (*s=='w' || *s=='W' || *s=='a' || *s=='A')
73      local_only=1;
74
75  char name[256], *c;
76  char const *f = filename;
77  c = name;
78  while (*f) { *c=*(f++); *c=toupper(*c); c++; } *c=0;
79  if (strstr(name,"REGISTER"))
80    local_only=1;
81
82  if (net_crcs && !local_only)
83  {
84    int fail1,fail2,fail3=0;
85    char const *local_filename = filename;
86    if (filename[0]=='/' && filename[1]=='/')
87    { local_filename+=2;
88      while (*local_filename && *local_filename!='/') local_filename++;
89      local_filename++;
90    }
91
92    int remote_file_num=net_crcs->get_filenumber(local_filename);
93    uint32_t remote_crc=net_crcs->get_crc(remote_file_num,fail2);
94    if (!fail2)
95    {
96      int local_file_num=crc_manager.get_filenumber(local_filename);
97      uint32_t local_crc=crc_manager.get_crc(local_file_num,fail1);
98      if (fail1)
99      {
100    bFILE *fp=new jFILE(local_filename,"rb");
101    if (!fp->open_failure())
102    {
103      local_crc=crc_file(fp);
104      crc_manager.set_crc(local_file_num,local_crc);
105    } else fail3=1;
106    delete fp;
107      }
108
109      if (!fail3)
110      {
111    if (local_crc==remote_crc)
112          local_only=1;
113      }
114    }
115  }
116
117
118  if (local_only)
119  {
120    local=new jFILE(filename,mode);
121    if (local->open_failure()) { delete local; local=NULL; }
122  }
123  else
124  {
125
126
127    char nm[256];
128    strcpy(nm,filename);
129    nfs_fd=NF_open_file(nm,mode);
130    if (nfs_fd==-2)
131    {
132      local=new jFILE(nm,mode);
133      if (local->open_failure()) { delete local; local=NULL; }
134      nfs_fd=-1;
135    }
136  }
137}
138
139
140int nfs_file::open_failure()
141{
142  if (local==NULL && nfs_fd<0) return 1;
143  else return 0;
144}
145
146
147int nfs_file::unbuffered_read(void *buf, size_t count)      // returns number of bytes read
148{
149  if (local)
150    return local->read(buf,count);
151  else if (nfs_fd>=0)
152  {
153    long a=NF_read(nfs_fd,buf,count);
154    if (a>(long)count)
155    {
156      fprintf(stderr,"ooch read too much\n");
157    }
158    return a;
159  }
160  else return 0;
161}
162
163int nfs_file::unbuffered_write(void const *buf, size_t count)      // returns number of bytes written
164{
165  if (local)
166    return local->write(buf,count);
167  else
168  {
169    fprintf(stderr,"write to nfs file not allowed for now!\n");
170    exit(0);
171  }
172  return 0;
173}
174
175
176int nfs_file::unbuffered_seek(long off, int whence) // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
177{
178  if (local)
179    return local->seek(off,whence);
180  else if (nfs_fd>=0)
181  {
182    if (whence!=SEEK_SET)
183      fprintf(stderr,"JC's a fork\n");
184    else
185      return NF_seek(nfs_fd,off);
186  }
187  return 0;
188}
189
190int nfs_file::unbuffered_tell()
191{
192  if (local)          return local->tell();
193  else if (nfs_fd>=0) return NF_tell(nfs_fd);
194  else                return 0;
195}
196
197
198int nfs_file::file_size()
199{
200  if (local)          return local->file_size();
201  else if (nfs_fd>=0) return NF_filelength(nfs_fd);
202  else                return 0;
203}
204
205nfs_file::~nfs_file()
206{
207  flush_writes();
208  if (local)          delete local;
209  else if (nfs_fd>=0) NF_close(nfs_fd);
210}
211
212int set_file_server(net_address *addr)
213{
214  if (NF_set_file_server(addr))
215  {
216    if (net_crcs)
217    {
218      net_crcs->clean_up();
219      delete net_crcs;
220    }
221
222    net_crcs=new CrcManager();
223    if (!net_crcs->load_crc_file(NET_CRC_FILENAME))
224    {
225      delete net_crcs;
226      net_crcs=NULL;
227      return 0;
228    }
229    return 1;
230  }
231  return 0;
232}
233
234
235int set_file_server(char const *name)
236{
237  if (prot)
238  {
239    net_address *addr=prot->get_node_address(name,DEFAULT_COMM_PORT,0);
240    if (!addr) { dprintf("\nUnable to locate server\n"); return 0; }
241    if (!set_file_server(addr))
242    {
243      delete addr;
244      return 0;
245    } else return 1;
246  } else return 0;
247}
248
249#endif
250
Note: See TracBrowser for help on using the repository browser.