Cube  Check-in [c09457f7ad]

Overview
Comment:More file handling cleanups
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c09457f7ad6613411be70250bdc33917e92298e80706560a9a205c997b679297
User & Date: js on 2025-03-05 22:42:56
Other Links: manifest | tags
Context
2025-03-05
23:18
Move classes into separate files check-in: 14861826d4 user: js tags: trunk
22:42
More file handling cleanups check-in: c09457f7ad user: js tags: trunk
22:25
Merge accidental fork check-in: 7cfa53cdf1 user: js tags: trunk
Changes

Modified src/Cube.mm from [46551226f2] to [b09eeb8e8f].

64
65
66
67
68
69
70












71
72
73
74
75
76
77

	if (sdesc == nil)
		sdesc = @"";
	if (ip == nil)
		ip = @"";
	if (passwd == nil)
		passwd = @"";













	if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0)
		fatal(@"Unable to initialize SDL");

	log(@"net");
	if (enet_initialize() < 0)
		fatal(@"Unable to initialise network module");







>
>
>
>
>
>
>
>
>
>
>
>







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

	if (sdesc == nil)
		sdesc = @"";
	if (ip == nil)
		ip = @"";
	if (passwd == nil)
		passwd = @"";

	_gameDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
	_userDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];

	[OFFileManager.defaultManager
	    createDirectoryAtIRI:[_userDataIRI
	                             IRIByAppendingPathComponent:@"demos"]
	           createParents:true];
	[OFFileManager.defaultManager
	    createDirectoryAtIRI:[_userDataIRI
	                             IRIByAppendingPathComponent:@"savegames"]
	           createParents:true];

	if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | par) < 0)
		fatal(@"Unable to initialize SDL");

	log(@"net");
	if (enet_initialize() < 0)
		fatal(@"Unable to initialise network module");
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	SDL_SetRelativeMouseMode(SDL_TRUE);
	SDL_ShowCursor(0);

	log(@"gl");
	gl_init(_width, _height);

	log(@"basetex");
	_gameDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
	_userDataIRI = [OFFileManager.defaultManager currentDirectoryIRI];
	int xs, ys;
	if (!installtex(2,
	        [_userDataIRI IRIByAppendingPathComponent:@"data/newchars.png"],
	        &xs, &ys, false) ||
	    !installtex(3,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/base.png"],







<
<







126
127
128
129
130
131
132


133
134
135
136
137
138
139
	SDL_SetRelativeMouseMode(SDL_TRUE);
	SDL_ShowCursor(0);

	log(@"gl");
	gl_init(_width, _height);

	log(@"basetex");


	int xs, ys;
	if (!installtex(2,
	        [_userDataIRI IRIByAppendingPathComponent:@"data/newchars.png"],
	        &xs, &ys, false) ||
	    !installtex(3,
	        [_userDataIRI
	            IRIByAppendingPathComponent:@"data/martin/base.png"],

Modified src/savegamedemo.mm from [01f038147b] to [38fcd6b7f4].

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
stopifrecording()
{
	if (demorecording)
		stop();
}

void
savestate(char *fn)
{

	stop();


	f = gzopen(fn, "wb9");
	if (!f) {
		conoutf(@"could not write %s", fn);
		return;
	}
	gzwrite(f, (void *)"CUBESAVE", 8);
	gzputc(f, islittleendian);
	gzputi(SAVEGAMEVERSION);
	gzputi(sizeof(dynent));
	@autoreleasepool {
		gzwrite(f, getclientmap().UTF8String, _MAXDEFSTR);
	}
	gzputi(gamemode);
	gzputi(ents.length());
	loopv(ents) gzputc(f, ents[i].spawned);
	gzwrite(f, player1, sizeof(dynent));
	dvector &monsters = getmonsters();
	gzputi(monsters.length());
	loopv(monsters) gzwrite(f, monsters[i], sizeof(dynent));
	gzputi(players.length());
	loopv(players)
	{
		gzput(players[i] == NULL);
		gzwrite(f, players[i], sizeof(dynent));

	}
}

void
savegame(OFString *name)
{
	@autoreleasepool {
		if (!m_classicsp) {
			conoutf(@"can only save classic sp games");
			return;
		}
		sprintf_sd(fn)("savegames/%s.csgz", name.UTF8String);





		savestate(fn);
		stop();
		conoutf(@"wrote %s", fn);
	}
}
COMMAND(savegame, ARG_1STR)

void
loadstate(char *fn)
{

	stop();
	if (multiplayer())
		return;


	f = gzopen(fn, "rb9");
	if (!f) {
		conoutf(@"could not open %s", fn);
		return;
	}

	string buf;
	gzread(f, buf, 8);
	if (strncmp(buf, "CUBESAVE", 8))
		goto out;
	if (gzgetc(f) != islittleendian)
		goto out; // not supporting save->load accross incompatible
		          // architectures simpifies things a LOT

	if (gzgeti() != SAVEGAMEVERSION || gzgeti() != sizeof(dynent))
		goto out;
	string mapname;
	gzread(f, mapname, _MAXDEFSTR);
	nextmode = gzgeti();
	@autoreleasepool {
		// continue below once map has been loaded and client & server
		// have updated
		changemap(@(mapname));
	}
	return;
out:
	conoutf(@"aborting: savegame/demo from a different version of cube or "
	        @"cpu architecture");
	stop();

}

void
loadgame(OFString *name)
{
	@autoreleasepool {

		sprintf_sd(fn)("savegames/%s.csgz", name.UTF8String);


		loadstate(fn);
	}
}
COMMAND(loadgame, ARG_1STR)

void
loadgameout()
{







|

>
|
>
>
|
|
|
|
|
|
|
|
|
<

<
|
|
|
|
|
|
|
|
|
|
|
|
>






<
|
|
|
|
|
>
>
>
>
>
|

|





|

>
|
|
|
>
>
|
|
|
|
|

|
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>






>
|
>
>
|







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
stopifrecording()
{
	if (demorecording)
		stop();
}

void
savestate(OFIRI *IRI)
{
	@autoreleasepool {
		stop();
		f = gzopen([IRI.fileSystemRepresentation
		               cStringWithEncoding:OFLocale.encoding],
		    "wb9");
		if (!f) {
			conoutf(@"could not write %@", IRI.string);
			return;
		}
		gzwrite(f, (void *)"CUBESAVE", 8);
		gzputc(f, islittleendian);
		gzputi(SAVEGAMEVERSION);
		gzputi(sizeof(dynent));

		gzwrite(f, getclientmap().UTF8String, _MAXDEFSTR);

		gzputi(gamemode);
		gzputi(ents.length());
		loopv(ents) gzputc(f, ents[i].spawned);
		gzwrite(f, player1, sizeof(dynent));
		dvector &monsters = getmonsters();
		gzputi(monsters.length());
		loopv(monsters) gzwrite(f, monsters[i], sizeof(dynent));
		gzputi(players.length());
		loopv(players)
		{
			gzput(players[i] == NULL);
			gzwrite(f, players[i], sizeof(dynent));
		}
	}
}

void
savegame(OFString *name)
{

	if (!m_classicsp) {
		conoutf(@"can only save classic sp games");
		return;
	}

	@autoreleasepool {
		OFString *path =
		    [OFString stringWithFormat:@"savegames/%@.csgz", name];
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
		    IRIByAppendingPathComponent:path];
		savestate(IRI);
		stop();
		conoutf(@"wrote %@", IRI.string);
	}
}
COMMAND(savegame, ARG_1STR)

void
loadstate(OFIRI *IRI)
{
	@autoreleasepool {
		stop();
		if (multiplayer())
			return;
		f = gzopen([IRI.fileSystemRepresentation
		               cStringWithEncoding:OFLocale.encoding],
		    "rb9");
		if (!f) {
			conoutf(@"could not open %@", IRI.string);
			return;
		}

		string buf;
		gzread(f, buf, 8);
		if (strncmp(buf, "CUBESAVE", 8))
			goto out;
		if (gzgetc(f) != islittleendian)
			goto out; // not supporting save->load accross
			          // incompatible architectures simpifies things
			          // a LOT
		if (gzgeti() != SAVEGAMEVERSION || gzgeti() != sizeof(dynent))
			goto out;
		string mapname;
		gzread(f, mapname, _MAXDEFSTR);
		nextmode = gzgeti();
		@autoreleasepool {
			// continue below once map has been loaded and client &
			// server have updated
			changemap(@(mapname));
		}
		return;
	out:
		conoutf(@"aborting: savegame/demo from a different version of "
		        @"cube or cpu architecture");
		stop();
	}
}

void
loadgame(OFString *name)
{
	@autoreleasepool {
		OFString *path =
		    [OFString stringWithFormat:@"savegames/%@.csgz", name];
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
		    IRIByAppendingPathComponent:path];
		loadstate(IRI);
	}
}
COMMAND(loadgame, ARG_1STR)

void
loadgameout()
{
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
int playbacktime = 0;
int ddamage, bdamage;
OFVector3D dorig;

void
record(OFString *name)
{
	@autoreleasepool {
		if (m_sp) {
			conoutf(@"cannot record singleplayer games");
			return;
		}

		int cn = getclientnum();
		if (cn < 0)
			return;
		sprintf_sd(fn)("demos/%s.cdgz", name.UTF8String);





		savestate(fn);
		gzputi(cn);
		conoutf(@"started recording demo to %s", fn);
		demorecording = true;
		starttime = lastmillis;
		ddamage = bdamage = 0;
	}
}
COMMAND(record, ARG_1STR)








<
|
|
|
|
>
|
|
|
|
>
>
>
>
>
|

|







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
int playbacktime = 0;
int ddamage, bdamage;
OFVector3D dorig;

void
record(OFString *name)
{

	if (m_sp) {
		conoutf(@"cannot record singleplayer games");
		return;
	}

	int cn = getclientnum();
	if (cn < 0)
		return;

	@autoreleasepool {
		OFString *path =
		    [OFString stringWithFormat:@"demos/%@.cdgz", name];
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
		    IRIByAppendingPathComponent:path];
		savestate(IRI);
		gzputi(cn);
		conoutf(@"started recording demo to %@", IRI.string);
		demorecording = true;
		starttime = lastmillis;
		ddamage = bdamage = 0;
	}
}
COMMAND(record, ARG_1STR)

311
312
313
314
315
316
317

318


319
320
321
322
323
324
325
326
	}
}

void
demo(OFString *name)
{
	@autoreleasepool {

		sprintf_sd(fn)("demos/%s.cdgz", name.UTF8String);


		loadstate(fn);
		demoloading = true;
	}
}
COMMAND(demo, ARG_1STR)

void
stopreset()







>
|
>
>
|







330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
	}
}

void
demo(OFString *name)
{
	@autoreleasepool {
		OFString *path =
		    [OFString stringWithFormat:@"demos/%@.cdgz", name];
		OFIRI *IRI = [Cube.sharedInstance.userDataIRI
		    IRIByAppendingPathComponent:path];
		loadstate(IRI);
		demoloading = true;
	}
}
COMMAND(demo, ARG_1STR)

void
stopreset()

Modified src/sound.mm from [320b4f8793] to [577f0e91da].

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#else
		samples[n] = FSOUND_Sample_Load(n,
		    IRI.fileSystemRepresentation.UTF8String, FSOUND_LOOP_OFF, 0,
		    0);
#endif

		if (!samples[n]) {
			conoutf(@"failed to load sample: %s",
			    IRI.string.UTF8String);
			return;
		}
	}

#ifdef USE_MIXER
	int chan = Mix_PlayChannel(-1, samples[n], 0);
#else







|
<







250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
#else
		samples[n] = FSOUND_Sample_Load(n,
		    IRI.fileSystemRepresentation.UTF8String, FSOUND_LOOP_OFF, 0,
		    0);
#endif

		if (!samples[n]) {
			conoutf(@"failed to load sample: %@", IRI.string);

			return;
		}
	}

#ifdef USE_MIXER
	int chan = Mix_PlayChannel(-1, samples[n], 0);
#else