More string migration

FossilOrigin-Name: 4a87b39dfe44d37093be0ed635660eea488e329a402a3377c282966fc08d6be8
This commit is contained in:
Jonathan Schleifer 2025-03-08 03:40:26 +00:00
parent 3d8735818d
commit 4871985da9
2 changed files with 160 additions and 156 deletions

View file

@ -146,7 +146,7 @@ renderspheres(int time)
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
} }
string closeent; static OFString *closeent;
OFString *entnames[] = { OFString *entnames[] = {
@"none?", @"none?",
@"light", @"light",
@ -176,7 +176,7 @@ OFString *entnames[] = {
void void
renderents() // show sparkly thingies for map entities in edit mode renderents() // show sparkly thingies for map entities in edit mode
{ {
closeent[0] = 0; closeent = @"";
if (!editmode) if (!editmode)
return; return;
loopv(ents) loopv(ents)
@ -190,14 +190,11 @@ renderents() // show sparkly thingies for map entities in edit mode
int e = closestent(); int e = closestent();
if (e >= 0) { if (e >= 0) {
entity &c = ents[e]; entity &c = ents[e];
@autoreleasepool { closeent = [[OFString alloc]
sprintf_s(closeent)( initWithFormat:@"closest entity = %@ (%d, %d, %d, %d), "
"closest entity = %s (%d, %d, %d, %d), " @"selection = (%d, %d)",
"selection = (%d, %d)", entnames[c.type], c.attr1, c.attr2, c.attr3, c.attr4,
entnames[c.type].UTF8String, c.attr1, c.attr2, getvar(@"selxs"), getvar(@"selys")];
c.attr3, c.attr4, getvar(@"selxs"),
getvar(@"selys"));
}
} }
} }
@ -371,8 +368,8 @@ gl_drawhud(int w, int h, int curfps, int nquads, int curvert, bool underwater)
if (command) if (command)
draw_textf(@"> %@_", 20, 1570, 2, command); draw_textf(@"> %@_", 20, 1570, 2, command);
else if (closeent[0]) else if (closeent.length > 0)
draw_text(@(closeent), 20, 1570, 2); draw_text(closeent, 20, 1570, 2);
else if (player != nil) else if (player != nil)
draw_text(player, 20, 1570, 2); draw_text(player, 20, 1570, 2);
} }

View file

@ -3,31 +3,39 @@
#include "cube.h" #include "cube.h"
void void
backup(char *name, char *backupname) backup(OFString *name, OFString *backupname)
{ {
remove(backupname); [OFFileManager.defaultManager removeItemAtPath:backupname];
rename(name, backupname); [OFFileManager.defaultManager moveItemAtPath:name toPath:backupname];
} }
string cgzname, bakname, pcfname, mcfname; static OFString *cgzname, *bakname, *pcfname, *mcfname;
static void static void
setnames(const char *name) setnames(OFString *name_)
{ {
string pakname, mapname; @autoreleasepool {
const char *slash = strpbrk(name, "/\\"); const char *name = name_.UTF8String;
if (slash) { string pakname, mapname;
strn0cpy(pakname, name, slash - name + 1); const char *slash = strpbrk(name, "/\\");
strcpy_s(mapname, slash + 1); if (slash) {
} else { strn0cpy(pakname, name, slash - name + 1);
strcpy_s(pakname, "base"); strcpy_s(mapname, slash + 1);
strcpy_s(mapname, name); } else {
strcpy_s(pakname, "base");
strcpy_s(mapname, name);
}
cgzname = [[OFString alloc]
initWithFormat:@"packages/%s/%s.cgz", pakname, mapname];
bakname =
[[OFString alloc] initWithFormat:@"packages/%s/%s_%d.BAK",
pakname, mapname, lastmillis];
pcfname = [[OFString alloc]
initWithFormat:@"packages/%s/package.cfg", pakname];
mcfname = [[OFString alloc]
initWithFormat:@"packages/%s/%s.cfg", pakname, mapname];
} }
sprintf_s(cgzname)("packages/%s/%s.cgz", pakname, mapname);
sprintf_s(bakname)(
"packages/%s/%s_%d.BAK", pakname, mapname, lastmillis);
sprintf_s(pcfname)("packages/%s/package.cfg", pakname);
sprintf_s(mcfname)("packages/%s/%s.cfg", pakname, mapname);
} }
// the optimize routines below are here to reduce the detrimental effects of // the optimize routines below are here to reduce the detrimental effects of
@ -122,12 +130,10 @@ toptimize() // FIXME: only does 2x2, make atleast for 4x4 also
void void
writemap(OFString *mname, int msize, uchar *mdata) writemap(OFString *mname, int msize, uchar *mdata)
{ {
@autoreleasepool { setnames(mname);
setnames(mname.UTF8String);
}
backup(cgzname, bakname); backup(cgzname, bakname);
FILE *f = fopen(cgzname, "wb"); FILE *f = fopen([cgzname cStringWithEncoding:OFLocale.encoding], "wb");
if (!f) { if (!f) {
conoutf(@"could not write map to %s", cgzname); conoutf(@"could not write map to %s", cgzname);
return; return;
@ -140,9 +146,7 @@ writemap(OFString *mname, int msize, uchar *mdata)
OFData * OFData *
readmap(OFString *mname) readmap(OFString *mname)
{ {
@autoreleasepool { setnames(mname);
setnames(mname.UTF8String);
}
return [OFData dataWithContentsOfFile:mname]; return [OFData dataWithContentsOfFile:mname];
} }
@ -161,9 +165,10 @@ save_world(OFString *mname)
toptimize(); toptimize();
if (mname.length == 0) if (mname.length == 0)
mname = getclientmap(); mname = getclientmap();
setnames(mname.UTF8String); setnames(mname);
backup(cgzname, bakname); backup(cgzname, bakname);
gzFile f = gzopen(cgzname, "wb9"); gzFile f = gzopen(
[cgzname cStringWithEncoding:OFLocale.encoding], "wb9");
if (!f) { if (!f) {
conoutf(@"could not write map to %s", cgzname); conoutf(@"could not write map to %s", cgzname);
return; return;
@ -247,138 +252,140 @@ void
load_world(OFString *mname) // still supports all map formats that have existed load_world(OFString *mname) // still supports all map formats that have existed
// since the earliest cube betas! // since the earliest cube betas!
{ {
stopifrecording();
cleardlights();
pruneundos();
@autoreleasepool { @autoreleasepool {
setnames(mname.UTF8String); stopifrecording();
} cleardlights();
gzFile f = gzopen(cgzname, "rb9"); pruneundos();
if (!f) { setnames(mname);
conoutf(@"could not read map %s", cgzname); gzFile f = gzopen(
return; [cgzname cStringWithEncoding:OFLocale.encoding], "rb9");
} if (!f) {
gzread(f, &hdr, sizeof(header) - sizeof(int) * 16); conoutf(@"could not read map %s", cgzname);
endianswap(&hdr.version, sizeof(int), 4); return;
if (strncmp(hdr.head, "CUBE", 4) != 0)
fatal(@"while reading map: header malformatted");
if (hdr.version > MAPVERSION)
fatal(@"this map requires a newer version of cube");
if (sfactor < SMALLEST_FACTOR || sfactor > LARGEST_FACTOR)
fatal(@"illegal map size");
if (hdr.version >= 4) {
gzread(f, &hdr.waterlevel, sizeof(int) * 16);
endianswap(&hdr.waterlevel, sizeof(int), 16);
} else {
hdr.waterlevel = -100000;
}
ents.setsize(0);
loopi(hdr.numents)
{
entity &e = ents.add();
gzread(f, &e, sizeof(persistent_entity));
endianswap(&e, sizeof(short), 4);
e.spawned = false;
if (e.type == LIGHT) {
if (!e.attr2)
e.attr2 = 255; // needed for MAPVERSION<=2
if (e.attr1 > 32)
e.attr1 = 32; // 12_03 and below
} }
} gzread(f, &hdr, sizeof(header) - sizeof(int) * 16);
free(world); endianswap(&hdr.version, sizeof(int), 4);
setupworld(hdr.sfactor); if (strncmp(hdr.head, "CUBE", 4) != 0)
char texuse[256]; fatal(@"while reading map: header malformatted");
loopi(256) texuse[i] = 0; if (hdr.version > MAPVERSION)
sqr *t = NULL; fatal(@"this map requires a newer version of cube");
loopk(cubicsize) if (sfactor < SMALLEST_FACTOR || sfactor > LARGEST_FACTOR)
{ fatal(@"illegal map size");
sqr *s = &world[k]; if (hdr.version >= 4) {
int type = gzgetc(f); gzread(f, &hdr.waterlevel, sizeof(int) * 16);
switch (type) { endianswap(&hdr.waterlevel, sizeof(int), 16);
case 255: { } else {
int n = gzgetc(f); hdr.waterlevel = -100000;
for (int i = 0; i < n; i++, k++)
memcpy(&world[k], t, sizeof(sqr));
k--;
break;
} }
case 254: // only in MAPVERSION<=2 ents.setsize(0);
loopi(hdr.numents)
{ {
memcpy(s, t, sizeof(sqr)); entity &e = ents.add();
s->r = s->g = s->b = gzgetc(f); gzread(f, &e, sizeof(persistent_entity));
gzgetc(f); endianswap(&e, sizeof(short), 4);
break; e.spawned = false;
} if (e.type == LIGHT) {
case SOLID: { if (!e.attr2)
s->type = SOLID; e.attr2 =
s->wtex = gzgetc(f); 255; // needed for MAPVERSION<=2
s->vdelta = gzgetc(f); if (e.attr1 > 32)
if (hdr.version <= 2) { e.attr1 = 32; // 12_03 and below
gzgetc(f);
gzgetc(f);
} }
s->ftex = DEFAULT_FLOOR;
s->ctex = DEFAULT_CEIL;
s->utex = s->wtex;
s->tag = 0;
s->floor = 0;
s->ceil = 16;
break;
} }
default: { free(world);
if (type < 0 || type >= MAXTYPE) { setupworld(hdr.sfactor);
OFString *t = [OFString char texuse[256];
stringWithFormat:@"%d @ %d", type, k]; loopi(256) texuse[i] = 0;
fatal(@"while reading map: type out of range: ", sqr *t = NULL;
t); loopk(cubicsize)
{
sqr *s = &world[k];
int type = gzgetc(f);
switch (type) {
case 255: {
int n = gzgetc(f);
for (int i = 0; i < n; i++, k++)
memcpy(&world[k], t, sizeof(sqr));
k--;
break;
} }
s->type = type; case 254: // only in MAPVERSION<=2
s->floor = gzgetc(f); {
s->ceil = gzgetc(f); memcpy(s, t, sizeof(sqr));
if (s->floor >= s->ceil) s->r = s->g = s->b = gzgetc(f);
s->floor = s->ceil - 1; // for pre 12_13
s->wtex = gzgetc(f);
s->ftex = gzgetc(f);
s->ctex = gzgetc(f);
if (hdr.version <= 2) {
gzgetc(f);
gzgetc(f); gzgetc(f);
break;
} }
s->vdelta = gzgetc(f); case SOLID: {
s->utex = (hdr.version >= 2) ? gzgetc(f) : s->wtex; s->type = SOLID;
s->tag = (hdr.version >= 5) ? gzgetc(f) : 0; s->wtex = gzgetc(f);
s->type = type; s->vdelta = gzgetc(f);
if (hdr.version <= 2) {
gzgetc(f);
gzgetc(f);
}
s->ftex = DEFAULT_FLOOR;
s->ctex = DEFAULT_CEIL;
s->utex = s->wtex;
s->tag = 0;
s->floor = 0;
s->ceil = 16;
break;
}
default: {
if (type < 0 || type >= MAXTYPE) {
OFString *t = [OFString
stringWithFormat:@"%d @ %d", type,
k];
fatal(@"while reading map: type out of "
@"range: ",
t);
}
s->type = type;
s->floor = gzgetc(f);
s->ceil = gzgetc(f);
if (s->floor >= s->ceil)
s->floor = s->ceil - 1; // for pre 12_13
s->wtex = gzgetc(f);
s->ftex = gzgetc(f);
s->ctex = gzgetc(f);
if (hdr.version <= 2) {
gzgetc(f);
gzgetc(f);
}
s->vdelta = gzgetc(f);
s->utex =
(hdr.version >= 2) ? gzgetc(f) : s->wtex;
s->tag = (hdr.version >= 5) ? gzgetc(f) : 0;
s->type = type;
}
}
s->defer = 0;
t = s;
texuse[s->wtex] = 1;
if (!SOLID(s))
texuse[s->utex] = texuse[s->ftex] =
texuse[s->ctex] = 1;
} }
} gzclose(f);
s->defer = 0; calclight();
t = s; settagareas();
texuse[s->wtex] = 1; int xs, ys;
if (!SOLID(s)) loopi(256) if (texuse) lookuptexture(i, &xs, &ys);
texuse[s->utex] = texuse[s->ftex] = texuse[s->ctex] = 1; conoutf(@"read map %s (%d milliseconds)", cgzname,
} SDL_GetTicks() - lastmillis);
gzclose(f); conoutf(@"%s", hdr.maptitle);
calclight(); startmap(mname);
settagareas(); loopl(256)
int xs, ys; {
loopi(256) if (texuse) lookuptexture(i, &xs, &ys);
conoutf(@"read map %s (%d milliseconds)", cgzname,
SDL_GetTicks() - lastmillis);
conoutf(@"%s", hdr.maptitle);
startmap(mname);
loopl(256)
{
@autoreleasepool {
// can this be done smarter? // can this be done smarter?
OFString *aliasname = OFString *aliasname =
[OFString stringWithFormat:@"level_trigger_%d", l]; [OFString stringWithFormat:@"level_trigger_%d", l];
if (identexists(aliasname)) if (identexists(aliasname))
alias(aliasname, @""); alias(aliasname, @"");
} }
} execfile(@"data/default_map_settings.cfg");
execfile(@"data/default_map_settings.cfg"); execfile(pcfname);
@autoreleasepool { execfile(mcfname);
execfile(@(pcfname));
execfile(@(mcfname));
} }
} }