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