From b8b9ef2e79eea38019059509d15a7802d2ca1574 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Thu, 6 Mar 2025 01:17:59 +0000 Subject: [PATCH] Remove cvector and ivector FossilOrigin-Name: 5bbd6d8ddd29ca99ebd4b494d11fea537f9567483fc9be14932846ece1503563 --- src/client.mm | 53 ++++++++++++++++++++++++---------------- src/console.mm | 65 +++++++++++++++++++++++++++++++++----------------- src/cube.h | 2 -- src/sound.mm | 28 ++++++++++++++++------ 4 files changed, 97 insertions(+), 51 deletions(-) diff --git a/src/client.mm b/src/client.mm index f11ef8e..5bde8f2 100644 --- a/src/client.mm +++ b/src/client.mm @@ -185,7 +185,7 @@ COMMANDN(disconnect, trydisconnect, ARG_NONE) // collect c2s messages conveniently -vector messages; +static OFMutableArray *messages; void addmsg(int rel, int num, int type, ...) @@ -197,18 +197,31 @@ addmsg(int rel, int num, int type, ...) stringWithFormat:@"inconsistant msg size for %d (%d != %d)", type, num, msgsizelookup(type)]); } - if (messages.length() == 100) { + if (messages.count == 100) { conoutf(@"command flood protection (type %d)", type); return; } - ivector &msg = messages.add(); - msg.add(num); - msg.add(rel); - msg.add(type); + + OFMutableData *msg = [OFMutableData dataWithItemSize:sizeof(int) + capacity:num + 2]; + [msg addItem:&num]; + [msg addItem:&rel]; + [msg addItem:&type]; + va_list marker; va_start(marker, type); - loopi(num - 1) msg.add(va_arg(marker, int)); + loopi(num - 1) + { + int tmp = va_arg(marker, int); + [msg addItem:&tmp]; + } va_end(marker); + [msg makeImmutable]; + + if (messages == nil) + messages = [[OFMutableArray alloc] init]; + + [messages addObject:msg]; } void @@ -313,7 +326,7 @@ c2sinfo(dynent *d) // send update to the server putint(p, -1); senditemstoserver = false; serveriteminitdone = true; - }; + } if (ctext[0]) // player chat, not flood protected for // now { @@ -321,7 +334,7 @@ c2sinfo(dynent *d) // send update to the server putint(p, SV_TEXT); sendstring(ctext, p); ctext[0] = 0; - }; + } if (!c2sinit) // tell other clients who I am { packet->flags = ENET_PACKET_FLAG_RELIABLE; @@ -330,23 +343,23 @@ c2sinfo(dynent *d) // send update to the server sendstring(player1->name, p); sendstring(player1->team, p); putint(p, player1->lifesequence); - }; - loopv(messages) // send messages collected during the - // previous frames - { - ivector &msg = messages[i]; - if (msg[1]) + } + for (OFData *msg in messages) { + // send messages collected during the previous + // frames + if (*(int *)[msg itemAtIndex:1]) packet->flags = ENET_PACKET_FLAG_RELIABLE; - loopi(msg[0]) putint(p, msg[i + 2]); - }; - messages.setsize(0); + loopi(*(int *)[msg itemAtIndex:0]) + putint(p, *(int *)[msg itemAtIndex:i + 2]); + } + [messages removeAllObjects]; if (lastmillis - lastping > 250) { putint(p, SV_PING); putint(p, lastmillis); lastping = lastmillis; - }; - }; + } + } *(ushort *)start = ENET_HOST_TO_NET_16(p - start); enet_packet_resize(packet, p - start); incomingdemodata(start, p - start, true); diff --git a/src/console.mm b/src/console.mm index 9b9cd3b..6bcb214 100644 --- a/src/console.mm +++ b/src/console.mm @@ -159,7 +159,7 @@ pasteconsole() strcat_s(commandbuf, cb); } -cvector vhistory; +static OFMutableArray *vhistory; int histpos = 0; void @@ -167,9 +167,11 @@ history(int n) { static bool rec = false; - if (!rec && n >= 0 && n < vhistory.length()) { + if (!rec && n >= 0 && n < vhistory.count) { rec = true; - execute(vhistory[vhistory.length() - n - 1]); + OFString *cmd = vhistory[vhistory.count - n - 1]; + std::unique_ptr copy(strdup(cmd.UTF8String)); + execute(copy.get()); rec = false; } } @@ -192,18 +194,26 @@ keypress(int code, bool isdown, int cooked) commandbuf[i] = 0; resetcomplete(); break; - }; + } case SDLK_UP: - if (histpos) - strcpy_s( - commandbuf, vhistory[--histpos]); + if (histpos) { + @autoreleasepool { + strcpy_s(commandbuf, + vhistory[--histpos] + .UTF8String); + } + } break; case SDLK_DOWN: - if (histpos < vhistory.length()) - strcpy_s( - commandbuf, vhistory[histpos++]); + if (histpos < vhistory.count) { + @autoreleasepool { + strcpy_s(commandbuf, + vhistory[histpos++] + .UTF8String); + } + } break; case SDLK_TAB: @@ -222,28 +232,39 @@ keypress(int code, bool isdown, int cooked) if (cooked) { char add[] = {(char)cooked, 0}; strcat_s(commandbuf, add); - }; - }; + } + } } else { if (code == SDLK_RETURN) { if (commandbuf[0]) { - if (vhistory.empty() || - strcmp( - vhistory.last(), commandbuf)) { - vhistory.add(newstring( - commandbuf)); // cap this? - }; - histpos = vhistory.length(); + @autoreleasepool { + OFString *cmdbuf = + @(commandbuf); + + if (vhistory == nil) + vhistory = + [[OFMutableArray + alloc] init]; + + if (vhistory.count == 0 || + ![vhistory.lastObject + isEqual:cmdbuf]) { + // cap this? + [vhistory + addObject:cmdbuf]; + } + } + histpos = vhistory.count; if (commandbuf[0] == '/') execute(commandbuf, true); else toserver(commandbuf); - }; + } saycommand(NULL); } else if (code == SDLK_ESCAPE) { saycommand(NULL); - }; - }; + } + } } else if (!menukey(code, isdown)) { // keystrokes go to menu diff --git a/src/cube.h b/src/cube.h index 688d047..937f28f 100644 --- a/src/cube.h +++ b/src/cube.h @@ -282,8 +282,6 @@ struct vertex { }; typedef vector dvector; -typedef vector cvector; -typedef vector ivector; // globals ooh naughty diff --git a/src/sound.mm b/src/sound.mm index 0664fce..6e77f0d 100644 --- a/src/sound.mm +++ b/src/sound.mm @@ -128,15 +128,29 @@ vector samples; vector samples; #endif -cvector snames; +static OFMutableArray *snames; int -registersound(char *name) +registersound(char *name_) { - loopv(snames) if (strcmp(snames[i], name) == 0) return i; - snames.add(newstring(name)); - samples.add(NULL); - return samples.length() - 1; + @autoreleasepool { + OFString *name = @(name_); + + int i = 0; + for (OFString *iter in snames) { + if ([iter isEqual:name]) + return i; + + i++; + } + + if (snames == nil) + snames = [[OFMutableArray alloc] init]; + + [snames addObject:name]; + samples.add(NULL); + return samples.length() - 1; + } } COMMAND(registersound, ARG_1EST) @@ -240,7 +254,7 @@ playsound(int n, OFVector3D *loc) if (!samples[n]) { OFString *path = [OFString - stringWithFormat:@"packages/sounds/%s.wav", snames[n]]; + stringWithFormat:@"packages/sounds/%@.wav", snames[n]]; OFIRI *IRI = [Cube.sharedInstance.gameDataIRI IRIByAppendingPathComponent:path];