1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
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.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 // net_vcr.c
21 
22 #include "quakedef.h"
23 #include "net_vcr.h"
24 
25 extern int vcrFile;
26 
27 // This is the playback portion of the VCR.  It reads the file produced
28 // by the recorder and plays it back to the host.  The recording contains
29 // everything necessary (events, timestamps, and data) to duplicate the game
30 // from the viewpoint of everything above the network layer.
31 
32 static struct
33 {
34 	double	time;
35 	int		op;
36 	long	session;
37 }	next;
38 
VCR_Init(void)39 int VCR_Init (void)
40 {
41 	net_drivers[0].Init = VCR_Init;
42 
43 	net_drivers[0].SearchForHosts = VCR_SearchForHosts;
44 	net_drivers[0].Connect = VCR_Connect;
45 	net_drivers[0].CheckNewConnections = VCR_CheckNewConnections;
46 	net_drivers[0].QGetMessage = VCR_GetMessage;
47 	net_drivers[0].QSendMessage = VCR_SendMessage;
48 	net_drivers[0].CanSendMessage = VCR_CanSendMessage;
49 	net_drivers[0].Close = VCR_Close;
50 	net_drivers[0].Shutdown = VCR_Shutdown;
51 
52 	Sys_FileRead(vcrFile, &next, sizeof(next));
53 	return 0;
54 }
55 
VCR_ReadNext(void)56 void VCR_ReadNext (void)
57 {
58 	if (Sys_FileRead(vcrFile, &next, sizeof(next)) == 0)
59 	{
60 		next.op = 255;
61 		Sys_Error ("=== END OF PLAYBACK===\n");
62 	}
63 	if (next.op < 1 || next.op > VCR_MAX_MESSAGE)
64 		Sys_Error ("VCR_ReadNext: bad op");
65 }
66 
67 
VCR_Listen(qboolean state)68 void VCR_Listen (qboolean state)
69 {
70 }
71 
72 
VCR_Shutdown(void)73 void VCR_Shutdown (void)
74 {
75 }
76 
77 
VCR_GetMessage(qsocket_t * sock)78 int VCR_GetMessage (qsocket_t *sock)
79 {
80 	int	ret;
81 
82 	if (host_time != next.time || next.op != VCR_OP_GETMESSAGE || next.session != *(long *)(&sock->driverdata))
83 		Sys_Error ("VCR missmatch");
84 
85 	Sys_FileRead(vcrFile, &ret, sizeof(int));
86 	if (ret != 1)
87 	{
88 		VCR_ReadNext ();
89 		return ret;
90 	}
91 
92 	Sys_FileRead(vcrFile, &net_message.cursize, sizeof(int));
93 	Sys_FileRead(vcrFile, net_message.data, net_message.cursize);
94 
95 	VCR_ReadNext ();
96 
97 	return 1;
98 }
99 
100 
VCR_SendMessage(qsocket_t * sock,sizebuf_t * data)101 int VCR_SendMessage (qsocket_t *sock, sizebuf_t *data)
102 {
103 	int	ret;
104 
105 	if (host_time != next.time || next.op != VCR_OP_SENDMESSAGE || next.session != *(long *)(&sock->driverdata))
106 		Sys_Error ("VCR missmatch");
107 
108 	Sys_FileRead(vcrFile, &ret, sizeof(int));
109 
110 	VCR_ReadNext ();
111 
112 	return ret;
113 }
114 
115 
VCR_CanSendMessage(qsocket_t * sock)116 qboolean VCR_CanSendMessage (qsocket_t *sock)
117 {
118 	qboolean	ret;
119 
120 	if (host_time != next.time || next.op != VCR_OP_CANSENDMESSAGE || next.session != *(long *)(&sock->driverdata))
121 		Sys_Error ("VCR missmatch");
122 
123 	Sys_FileRead(vcrFile, &ret, sizeof(int));
124 
125 	VCR_ReadNext ();
126 
127 	return ret;
128 }
129 
130 
VCR_Close(qsocket_t * sock)131 void VCR_Close (qsocket_t *sock)
132 {
133 }
134 
135 
VCR_SearchForHosts(qboolean xmit)136 void VCR_SearchForHosts (qboolean xmit)
137 {
138 }
139 
140 
VCR_Connect(char * host)141 qsocket_t *VCR_Connect (char *host)
142 {
143 	return NULL;
144 }
145 
146 
VCR_CheckNewConnections(void)147 qsocket_t *VCR_CheckNewConnections (void)
148 {
149 	qsocket_t	*sock;
150 
151 	if (host_time != next.time || next.op != VCR_OP_CONNECT)
152 		Sys_Error ("VCR missmatch");
153 
154 	if (!next.session)
155 	{
156 		VCR_ReadNext ();
157 		return NULL;
158 	}
159 
160 	sock = NET_NewQSocket ();
161 	*(long *)(&sock->driverdata) = next.session;
162 
163 	Sys_FileRead (vcrFile, sock->address, NET_NAMELEN);
164 	VCR_ReadNext ();
165 
166 	return sock;
167 }
168