Skip to content

Commit 19978b8

Browse files
committed
Add hsDarwin with helpers for string conversion
The NSString category in the macOS plClient folder is useful, but there are several other spots where we're wanting to convert between CFString/NSString and ST::string, so it make sense to have helpers available throughout the engine. These helper functions are written so that they can be used from C++ code without needing to bring in Objective-C.
1 parent 085186d commit 19978b8

17 files changed

+310
-60
lines changed

Sources/Plasma/Apps/plClient/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,22 @@ if(WIN32)
9292
elseif(APPLE)
9393
set(plClient_SOURCES ${plClient_SOURCES}
9494
Mac-Cocoa/main.mm
95+
Mac-Cocoa/NSString+StringTheory.mm
9596
Mac-Cocoa/PLSKeyboardEventMonitor.mm
9697
Mac-Cocoa/PLSView.mm
9798
Mac-Cocoa/PLSLoginWindowController.mm
9899
Mac-Cocoa/PLSPatcherWindowController.mm
99100
Mac-Cocoa/PLSPatcher.mm
100101
Mac-Cocoa/PLSServerStatus.mm
101-
Mac-Cocoa/StringTheory_NSString.mm
102102
)
103103
set(plClient_HEADERS ${plClient_HEADERS}
104+
Mac-Cocoa/NSString+StringTheory.h
104105
Mac-Cocoa/PLSKeyboardEventMonitor.h
105106
Mac-Cocoa/PLSView.h
106107
Mac-Cocoa/PLSLoginWindowController.h
107108
Mac-Cocoa/PLSPatcherWindowController.h
108109
Mac-Cocoa/PLSPatcher.h
109110
Mac-Cocoa/PLSServerStatus.h
110-
Mac-Cocoa/StringTheory_NSString.h
111111
)
112112
set(RESOURCES
113113
Mac-Cocoa/MainMenu.xib

Sources/Plasma/Apps/plClient/Mac-Cocoa/StringTheory_NSString.h renamed to Sources/Plasma/Apps/plClient/Mac-Cocoa/NSString+StringTheory.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ NS_ASSUME_NONNULL_BEGIN
4747

4848
@interface NSString (StringTheory)
4949

50-
- (id)initWithSTString:(const ST::string&)string;
5150
+ (id)stringWithSTString:(const ST::string&)string;
5251

5352
- (const ST::string)STString;

Sources/Plasma/Apps/plClient/Mac-Cocoa/StringTheory_NSString.mm renamed to Sources/Plasma/Apps/plClient/Mac-Cocoa/NSString+StringTheory.mm

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,19 @@
4040
4141
*==LICENSE==*/
4242

43-
#include "StringTheory_NSString.h"
43+
#include "NSString+StringTheory.h"
44+
#include "hsDarwin.h"
4445

4546
@implementation NSString (StringTheory)
4647

47-
- (id)initWithSTString:(const ST::string&)string
48-
{
49-
self = [self initWithUTF8String:string.c_str()];
50-
return self;
51-
}
52-
5348
+ (id)stringWithSTString:(const ST::string&)string
5449
{
55-
return [[NSString alloc] initWithSTString:string];
50+
return NSStringCreateWithSTString(string);
5651
}
5752

5853
- (const ST::string)STString
5954
{
60-
return ST::string([self UTF8String]);
55+
return STStringFromNSString(self);
6156
}
6257

6358
@end

Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSLoginWindowController.mm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#import "PLSLoginWindowController.h"
4444
#include <regex>
4545
#import "PLSServerStatus.h"
46-
#include "StringTheory_NSString.h"
46+
#import "NSString+StringTheory.h"
4747
#include "pfPasswordStore/pfPasswordStore.h"
4848
#include "plNetGameLib/plNetGameLib.h"
4949
#include "plProduct.h"
@@ -128,8 +128,8 @@ - (void)save
128128
[[NSUserDefaults standardUserDefaults] synchronize];
129129

130130
if (self.password && ![self.password isEqualToString:FAKE_PASS_STRING]) {
131-
ST::string username = ST::string([self.username cStringUsingEncoding:NSUTF8StringEncoding]);
132-
ST::string password = ST::string([self.password cStringUsingEncoding:NSUTF8StringEncoding]);
131+
ST::string username = [self.username STString];
132+
ST::string password = [self.password STString];
133133

134134
pfPasswordStore* store = pfPasswordStore::Instance();
135135
if (self.rememberPassword)
@@ -160,8 +160,8 @@ - (void)storeHash:(ShaDigest&)namePassHash
160160
// Hash username and password before sending over the 'net.
161161
// -- Legacy compatibility: @gametap (and other usernames with domains in them) need
162162
// to be hashed differently.
163-
ST::string username = ST::string([self.username cStringUsingEncoding:NSUTF8StringEncoding]);
164-
ST::string password = ST::string([self.password cStringUsingEncoding:NSUTF8StringEncoding]);
163+
ST::string username = [self.username STString];
164+
ST::string password = [self.password STString];
165165
static const std::regex re_domain("[^@]+@([^.]+\\.)*([^.]+)\\.[^.]+");
166166
std::cmatch match;
167167
std::regex_search(username.c_str(), match, re_domain);
@@ -185,7 +185,7 @@ - (void)makeCurrent
185185
ShaDigest hash;
186186
[self storeHash:hash];
187187

188-
ST::string username = ST::string([self.username cStringUsingEncoding:NSUTF8StringEncoding]);
188+
ST::string username = [self.username STString];
189189
NetCommSetAccountUsernamePassword(username, hash);
190190
NetCommSetAuthTokenAndOS(nullptr, u"mac");
191191
}
@@ -214,7 +214,7 @@ - (void)windowDidLoad
214214

215215
[self.window center];
216216
[self.productTextField
217-
setStringValue:[NSString stringWithSTString:plProduct::ProductString().c_str()]];
217+
setStringValue:[NSString stringWithSTString:plProduct::ProductString()]];
218218
}
219219

220220
- (NSNibName)windowNibName

Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSPatcher.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
*==LICENSE==*/
4242

4343
#import "PLSPatcher.h"
44+
#import "NSString+StringTheory.h"
4445

4546
#include <unordered_set>
4647
#include <string_theory/format>
47-
#include "StringTheory_NSString.h"
4848

4949
#include "HeadSpin.h"
5050
#include "hsTimer.h"

Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSPatcherWindowController.mm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*==LICENSE==*/
4242

4343
#import "PLSPatcherWindowController.h"
44-
#include <string_theory/string>
44+
#import "NSString+StringTheory.h"
4545
#include "PLSServerStatus.h"
4646
#include "plProduct.h"
4747

@@ -111,8 +111,7 @@ - (void)windowDidLoad
111111
[super windowDidLoad];
112112

113113
[self.progressBar startAnimation:self];
114-
self.productLabel.stringValue =
115-
[NSString stringWithUTF8String:plProduct::ProductString().c_str()];
114+
self.productLabel.stringValue = [NSString stringWithSTString:plProduct::ProductString()];
116115
// register for an async notification of when status loads
117116
[[PLSServerStatus sharedStatus]
118117
addObserver:self

Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSServerStatus.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
*==LICENSE==*/
4242

4343
#import "PLSServerStatus.h"
44-
#include <string_theory/string>
45-
#include "StringTheory_NSString.h"
44+
#import "NSString+StringTheory.h"
4645
#include "plNetGameLib/plNetGameLib.h"
4746

4847
@interface PLSServerStatus () <NSURLSessionDelegate>

Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
#import <QuartzCore/QuartzCore.h>
4646

4747
// Cocoa client
48+
#import "NSString+StringTheory.h"
4849
#import "PLSKeyboardEventMonitor.h"
4950
#import "PLSLoginWindowController.h"
5051
#import "PLSPatcherWindowController.h"
5152
#import "PLSServerStatus.h"
5253
#import "PLSView.h"
53-
#import "StringTheory_NSString.h"
5454

5555
// stdlib
5656
#include <algorithm>

Sources/Plasma/CoreLib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ set(CoreLib_HEADERS
4040
hsBounds.h
4141
hsColorRGBA.h
4242
hsCpuID.h
43+
hsDarwin.h
4344
hsExceptions.h
4445
hsExceptionStack.h
4546
hsFastMath.h
@@ -81,7 +82,6 @@ target_link_libraries(
8182
Threads::Threads
8283
$<$<AND:$<CONFIG:Debug>,$<BOOL:${USE_VLD}>>:VLD::VLD>
8384
"$<$<PLATFORM_ID:Darwin>:-framework Accelerate>"
84-
PRIVATE
8585
"$<$<PLATFORM_ID:Darwin>:-framework CoreFoundation>"
8686
)
8787
target_include_directories(

Sources/Plasma/CoreLib/_CoreLibPch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
5959

6060
#include "HeadSpin.h"
6161
#include "hsWindows.h"
62+
#include "hsDarwin.h"
6263

6364
#include <string_theory/formatter>
6465
#include <string_theory/string>

0 commit comments

Comments
 (0)