From 570b9e3bb3f491e73c499c95d2ca7c941986977e Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Fri, 7 Mar 2025 19:55:34 +0000 Subject: [PATCH] Migrate more strings FossilOrigin-Name: 5ef6284dcf38a9367998d964d81cf7cf8340d76c87d58efaa350c9d930ccb416 --- src/client.mm | 2 +- src/command.mm | 57 ++++++++++++++++++++-------------- src/console.mm | 75 ++++++++++++++++++++++----------------------- src/protos.h | 6 ++-- src/renderextras.mm | 4 +-- 5 files changed, 76 insertions(+), 68 deletions(-) diff --git a/src/client.mm b/src/client.mm index 5bde8f2..1790aac 100644 --- a/src/client.mm +++ b/src/client.mm @@ -166,7 +166,7 @@ trydisconnect() string ctext; void -toserver(char *text) +toserver(const char *text) { conoutf(@"%s:\f %s", player1->name, text); strn0cpy(ctext, text, 80); diff --git a/src/command.mm b/src/command.mm index 6e9915a..c64ea07 100644 --- a/src/command.mm +++ b/src/command.mm @@ -511,32 +511,43 @@ resetcomplete() } void -complete(char *s) +complete(OFString *s_) { - if (*s != '/') { - string t; - strcpy_s(t, s); - strcpy_s(s, "/"); - strcat_s(s, t); - } - if (!s[1]) - return; - if (!completesize) { - completesize = (int)strlen(s) - 1; - completeidx = 0; - } - __block int idx = 0; - [idents enumerateKeysAndObjectsUsingBlock:^( - OFString *name, Ident *ident, bool *stop) { - if (strncmp(ident.name.UTF8String, s + 1, completesize) == 0 && - idx++ == completeidx) { + @autoreleasepool { + std::unique_ptr copy(strdup(s_.UTF8String)); + char *s = copy.get(); + + if (*s != '/') { + string t; + strcpy_s(t, s); strcpy_s(s, "/"); - strcat_s(s, ident.name.UTF8String); + strcat_s(s, t); } - }]; - completeidx++; - if (completeidx >= idx) - completeidx = 0; + + if (!s[1]) + return; + + if (!completesize) { + completesize = strlen(s) - 1; + completeidx = 0; + } + + __block int idx = 0; + [idents enumerateKeysAndObjectsUsingBlock:^( + OFString *name, Ident *ident, bool *stop) { + if (strncmp(ident.name.UTF8String, s + 1, + completesize) == 0 && + idx++ == completeidx) { + strcpy_s(s, "/"); + strcat_s(s, ident.name.UTF8String); + } + }]; + + completeidx++; + + if (completeidx >= idx) + completeidx = 0; + } } bool diff --git a/src/console.mm b/src/console.mm index 6bcb214..c48b11d 100644 --- a/src/console.mm +++ b/src/console.mm @@ -18,7 +18,7 @@ const int WORDWRAP = 80; int conskip = 0; bool saycommandon = false; -string commandbuf; +static OFMutableString *commandbuf; void setconskip(int n) @@ -128,18 +128,21 @@ bindkey(OFString *key, OFString *action) COMMANDN(bind, bindkey, ARG_2STR) void -saycommand(char *init) // turns input to the command line on or off +saycommand(const char *init) // turns input to the command line on or off { saycommandon = (init != NULL); if (saycommandon) SDL_StartTextInput(); else SDL_StopTextInput(); + if (!editmode) Cube.sharedInstance.repeatsKeys = saycommandon; + if (!init) init = ""; - strcpy_s(commandbuf, init); + + commandbuf = [[OFMutableString alloc] initWithUTF8String:init]; } COMMAND(saycommand, ARG_VARI) @@ -155,12 +158,13 @@ COMMAND(mapmsg, ARG_1STR) void pasteconsole() { - char *cb = SDL_GetClipboardText(); - strcat_s(commandbuf, cb); + @autoreleasepool { + [commandbuf appendString:@(SDL_GetClipboardText())]; + } } static OFMutableArray *vhistory; -int histpos = 0; +static int histpos = 0; void history(int n) @@ -189,31 +193,24 @@ keypress(int code, bool isdown, int cooked) case SDLK_BACKSPACE: case SDLK_LEFT: { - for (int i = 0; commandbuf[i]; i++) - if (!commandbuf[i + 1]) - commandbuf[i] = 0; + [commandbuf + deleteCharactersInRange: + OFMakeRange(commandbuf.length - 1, 1)]; + resetcomplete(); break; } case SDLK_UP: - if (histpos) { - @autoreleasepool { - strcpy_s(commandbuf, - vhistory[--histpos] - .UTF8String); - } - } + if (histpos) + commandbuf = + [vhistory[--histpos] mutableCopy]; break; case SDLK_DOWN: - if (histpos < vhistory.count) { - @autoreleasepool { - strcpy_s(commandbuf, - vhistory[histpos++] - .UTF8String); - } - } + if (histpos < vhistory.count) + commandbuf = + [vhistory[histpos++] mutableCopy]; break; case SDLK_TAB: @@ -225,22 +222,17 @@ keypress(int code, bool isdown, int cooked) (KMOD_LCTRL | KMOD_RCTRL)) { pasteconsole(); return; - }; + } default: resetcomplete(); - if (cooked) { - char add[] = {(char)cooked, 0}; - strcat_s(commandbuf, add); - } + if (cooked) + [commandbuf appendFormat:@"%c", cooked]; } } else { if (code == SDLK_RETURN) { - if (commandbuf[0]) { + if (commandbuf.length > 0) { @autoreleasepool { - OFString *cmdbuf = - @(commandbuf); - if (vhistory == nil) vhistory = [[OFMutableArray @@ -248,17 +240,22 @@ keypress(int code, bool isdown, int cooked) if (vhistory.count == 0 || ![vhistory.lastObject - isEqual:cmdbuf]) { + isEqual:commandbuf]) { // cap this? [vhistory - addObject:cmdbuf]; + addObject: + [commandbuf + copy]]; } } histpos = vhistory.count; - if (commandbuf[0] == '/') - execute(commandbuf, true); - else - toserver(commandbuf); + if ([commandbuf hasPrefix:@"/"]) { + std::unique_ptr copy( + strdup( + commandbuf.UTF8String)); + execute(copy.get(), true); + } else + toserver(commandbuf.UTF8String); } saycommand(NULL); } else if (code == SDLK_ESCAPE) { @@ -281,7 +278,7 @@ keypress(int code, bool isdown, int cooked) } } -char * +OFString * getcurcommand() { return saycommandon ? commandbuf : NULL; diff --git a/src/protos.h b/src/protos.h index 866dd21..a4ce99e 100644 --- a/src/protos.h +++ b/src/protos.h @@ -11,7 +11,7 @@ extern int execute(char *p, bool down = true); extern void exec(OFString *cfgfile); extern bool execfile(OFString *cfgfile); extern void resetcomplete(); -extern void complete(char *s); +extern void complete(OFString *s); extern void alias(OFString *name, OFString *action); extern OFString *getalias(OFString *name); extern void writecfg(); @@ -20,7 +20,7 @@ extern void writecfg(); extern void keypress(int code, bool isdown, int cooked); extern void renderconsole(); extern void conoutf(OFConstantString *format, ...); -extern char *getcurcommand(); +extern OFString *getcurcommand(); extern void writebinds(OFStream *stream); // init @@ -71,7 +71,7 @@ extern void setarraypointers(); extern void localservertoclient(uchar *buf, int len); extern void connects(OFString *servername); extern void disconnect(int onlyclean = 0, int async = 0); -extern void toserver(char *text); +extern void toserver(const char *text); extern void addmsg(int rel, int num, int type, ...); extern bool multiplayer(); extern bool allowedittoggle(); diff --git a/src/renderextras.mm b/src/renderextras.mm index 49bd52a..00ee4da 100644 --- a/src/renderextras.mm +++ b/src/renderextras.mm @@ -366,11 +366,11 @@ gl_drawhud(int w, int h, int curfps, int nquads, int curvert, bool underwater) glEnable(GL_TEXTURE_2D); @autoreleasepool { - char *command = getcurcommand(); + OFString *command = getcurcommand(); char *player = playerincrosshair(); if (command) - draw_textf(@"> %s_", 20, 1570, 2, command); + draw_textf(@"> %@_", 20, 1570, 2, command); else if (closeent[0]) draw_text(@(closeent), 20, 1570, 2); else if (player)