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

+2-2
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

-1
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

+4-9
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

+7-7
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

+1-1
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

+2-3
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

+1-2
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

+1-1
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

+1-1
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

+1
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>

Sources/Plasma/CoreLib/hsDarwin.h

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*==LICENSE==*
2+
3+
CyanWorlds.com Engine - MMOG client, server and tools
4+
Copyright (C) 2011 Cyan Worlds, Inc.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
Additional permissions under GNU GPL version 3 section 7
20+
21+
If you modify this Program, or any covered work, by linking or
22+
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
23+
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
24+
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
25+
(or a modified version of those libraries),
26+
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
27+
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
28+
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
29+
licensors of this Program grant you additional
30+
permission to convey the resulting work. Corresponding Source for a
31+
non-source form of such a combination shall include the source code for
32+
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
33+
work.
34+
35+
You can contact Cyan Worlds, Inc. by email [email protected]
36+
or by snail mail at:
37+
Cyan Worlds, Inc.
38+
14617 N Newport Hwy
39+
Mead, WA 99021
40+
41+
*==LICENSE==*/
42+
43+
#ifndef _hsDarwin_inc_
44+
#define _hsDarwin_inc_
45+
46+
#include <string_theory/string>
47+
#include <string_theory/format>
48+
49+
#ifdef HS_BUILD_FOR_APPLE
50+
#include <CoreFoundation/CoreFoundation.h>
51+
52+
[[nodiscard]]
53+
#if __has_feature(attribute_cf_returns_retained)
54+
__attribute__((cf_returns_retained))
55+
#endif
56+
inline CFStringRef CFStringCreateWithSTString(const ST::string& str)
57+
{
58+
return CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)str.data(), str.size(), kCFStringEncodingUTF8, false);
59+
}
60+
61+
inline ST::string STStringFromCFString(CFStringRef str, ST::utf_validation_t validation = ST_DEFAULT_VALIDATION)
62+
{
63+
CFRange range = CFRangeMake(0, CFStringGetLength(str));
64+
CFIndex strBufSz = 0;
65+
CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, nullptr, 0, &strBufSz);
66+
ST::char_buffer buffer;
67+
buffer.allocate(strBufSz);
68+
CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, (UInt8*)buffer.data(), strBufSz, nullptr);
69+
70+
return ST::string(buffer, validation);
71+
}
72+
73+
inline void format_type(const ST::format_spec &format, ST::format_writer &output, CFStringRef str)
74+
{
75+
ST::char_buffer utf8 = STStringFromCFString(str).to_utf8();
76+
ST::format_string(format, output, utf8.data(), utf8.size());
77+
}
78+
79+
80+
#ifdef __OBJC__
81+
@class NSString;
82+
83+
[[nodiscard]]
84+
#if __has_feature(attribute_ns_returns_retained)
85+
__attribute__((ns_returns_retained))
86+
#endif
87+
inline NSString* NSStringCreateWithSTString(const ST::string& str)
88+
{
89+
#if __has_feature(objc_arc)
90+
return (NSString*)CFBridgingRelease(CFStringCreateWithSTString(str));
91+
#else
92+
return (NSString*)CFStringCreateWithSTString(str);
93+
#endif
94+
}
95+
96+
inline ST::string STStringFromNSString(NSString* str, ST::utf_validation_t validation = ST_DEFAULT_VALIDATION)
97+
{
98+
#if __has_feature(objc_arc)
99+
return STStringFromCFString((__bridge CFStringRef)str, validation);
100+
#else
101+
return STStringFromCFString((CFStringRef)str, validation);
102+
#endif
103+
}
104+
105+
inline void format_type(const ST::format_spec &format, ST::format_writer &output, NSString* str)
106+
{
107+
ST::char_buffer utf8 = STStringFromNSString(str).to_utf8();
108+
ST::format_string(format, output, utf8.data(), utf8.size());
109+
}
110+
#endif // __OBJC__
111+
112+
#endif // HS_BUILD_FOR_APPLE
113+
114+
#endif // _hsDarwin_inc_

Sources/Plasma/CoreLib/hsSystemInfo.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
4646
#include "plFileSystem.h"
4747
#include "hsStream.h"
4848
#include "hsWindows.h"
49+
#include "hsDarwin.h"
4950

5051
#include <cstring>
5152
#include <iterator>
@@ -191,13 +192,7 @@ static inline bool IGetAppleVersion(ST::string& system)
191192
CFRelease(name);
192193
CFRelease(dict);
193194

194-
CFIndex infoLen = CFStringGetLength(info);
195-
CFIndex infoBufSz = 0;
196-
CFStringGetBytes(info, CFRangeMake(0, infoLen), kCFStringEncodingUTF8, 0, false, nullptr, 0, &infoBufSz);
197-
ST::char_buffer systemBuf;
198-
systemBuf.allocate(infoBufSz);
199-
CFStringGetBytes(info, CFRangeMake(0, infoLen), kCFStringEncodingUTF8, 0, false, (UInt8*)systemBuf.data(), infoLen, nullptr);
200-
system = ST::string(systemBuf);
195+
system = STStringFromCFString(info);
201196

202197
CFRelease(info);
203198

0 commit comments

Comments
 (0)