Skip to content

Commit

Permalink
Add hsDarwin with helpers for string conversion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dpogue committed Aug 31, 2023
1 parent 085186d commit 94ad648
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 174 deletions.
2 changes: 0 additions & 2 deletions Sources/Plasma/Apps/plClient/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ elseif(APPLE)
Mac-Cocoa/PLSPatcherWindowController.mm
Mac-Cocoa/PLSPatcher.mm
Mac-Cocoa/PLSServerStatus.mm
Mac-Cocoa/StringTheory_NSString.mm
)
set(plClient_HEADERS ${plClient_HEADERS}
Mac-Cocoa/PLSKeyboardEventMonitor.h
Expand All @@ -107,7 +106,6 @@ elseif(APPLE)
Mac-Cocoa/PLSPatcherWindowController.h
Mac-Cocoa/PLSPatcher.h
Mac-Cocoa/PLSServerStatus.h
Mac-Cocoa/StringTheory_NSString.h
)
set(RESOURCES
Mac-Cocoa/MainMenu.xib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#import "PLSLoginWindowController.h"
#include <regex>
#import "PLSServerStatus.h"
#include "StringTheory_NSString.h"
#include "hsDarwin.h"
#include "pfPasswordStore/pfPasswordStore.h"
#include "plNetGameLib/plNetGameLib.h"
#include "plProduct.h"
Expand Down Expand Up @@ -116,7 +116,7 @@ - (void)save
{
// windows segments by product name here. in since user defaults belong to this product, we
// don't need to do that.
NSString* serverName = [NSString stringWithSTString:GetServerDisplayName()];
NSString* serverName = NSStringCreateWithSTString(GetServerDisplayName());
NSMutableDictionary* settingsDictionary =
[[[NSUserDefaults standardUserDefaults] dictionaryForKey:serverName] mutableCopy];
if (!settingsDictionary)
Expand All @@ -141,17 +141,17 @@ - (void)save

- (void)load
{
NSString* serverName = [NSString stringWithSTString:GetServerDisplayName()];
NSString* serverName = NSStringCreateWithSTString(GetServerDisplayName());
NSDictionary* settingsDictionary =
[[NSUserDefaults standardUserDefaults] dictionaryForKey:serverName];
self.username = [settingsDictionary objectForKey:@"LastAccountName"];
self.rememberPassword = [[settingsDictionary objectForKey:@"RememberPassword"] boolValue];

if (self.rememberPassword) {
pfPasswordStore* store = pfPasswordStore::Instance();
ST::string username = [self.username STString];
ST::string username = STStringFromNSString(self.username);
ST::string password = store->GetPassword(username);
self.password = [NSString stringWithSTString:password];
self.password = NSStringCreateWithSTString(password);
}
}

Expand Down Expand Up @@ -214,7 +214,7 @@ - (void)windowDidLoad

[self.window center];
[self.productTextField
setStringValue:[NSString stringWithSTString:plProduct::ProductString().c_str()]];
setStringValue:NSStringCreateWithSTString(plProduct::ProductString())];
}

- (NSNibName)windowNibName
Expand Down Expand Up @@ -263,7 +263,7 @@ - (void)loginAttemptEndedWithResult:(ENetError)result

- (IBAction)needAccountButtonHit:(id)sender
{
NSString* urlString = [NSString stringWithSTString:GetServerSignupUrl()];
NSString* urlString = NSStringCreateWithSTStrinNSStringCreateWithSTString(GetServerSignupUrl());
NSURL* url = [NSURL URLWithString:urlString];
if (url) {
[[NSWorkspace sharedWorkspace] openURL:url];
Expand Down
8 changes: 4 additions & 4 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSPatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@

#include <unordered_set>
#include <string_theory/format>
#include "StringTheory_NSString.h"

#include "HeadSpin.h"
#include "hsDarwin.h"
#include "hsTimer.h"

#include "pfPatcher/pfPatcher.h"
Expand Down Expand Up @@ -108,15 +108,15 @@ - (void)start

void Patcher::IOnDownloadBegin(const plFileName& file)
{
NSString* fileName = [NSString stringWithSTString:file.AsString()];
NSString* fileName = NSStringCreateWithSTString(file.AsString());
dispatch_async(dispatch_get_main_queue(), ^{
[parent.delegate patcher:parent beganDownloadOfFile:fileName];
});
}

void Patcher::IOnProgressTick(uint64_t curBytes, uint64_t totalBytes, const ST::string& status)
{
NSString* statusString = [NSString stringWithSTString:status];
NSString* statusString = NSStringCreateWithSTString(status);

dispatch_async(dispatch_get_main_queue(), ^{
[parent.delegate patcher:parent
Expand Down Expand Up @@ -148,7 +148,7 @@ bool IApproveDownload(const plFileName& file)
[patcher.delegate patcherCompleted:patcher];
});
} else {
NSString* msgString = [NSString stringWithSTString:msg];
NSString* msgString = NSStringCreateWithSTString(msg);

dispatch_async(dispatch_get_main_queue(), ^{
[parent.delegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <string_theory/string>
#include "PLSServerStatus.h"
#include "plProduct.h"
#include "hsDarwin.h"

@interface PLSPatcherWindowController ()

Expand Down Expand Up @@ -111,8 +112,7 @@ - (void)windowDidLoad
[super windowDidLoad];

[self.progressBar startAnimation:self];
self.productLabel.stringValue =
[NSString stringWithUTF8String:plProduct::ProductString().c_str()];
self.productLabel.stringValue = NSStringCreateWithSTString(plProduct::ProductString().c_str());
// register for an async notification of when status loads
[[PLSServerStatus sharedStatus]
addObserver:self
Expand Down
5 changes: 2 additions & 3 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSServerStatus.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
*==LICENSE==*/

#import "PLSServerStatus.h"
#include <string_theory/string>
#include "StringTheory_NSString.h"
#include "hsDarwin.h"
#include "plNetGameLib/plNetGameLib.h"

@interface PLSServerStatus () <NSURLSessionDelegate>
Expand All @@ -63,7 +62,7 @@ + (id)sharedStatus

- (void)loadServerStatus
{
NSString* urlString = [NSString stringWithSTString:GetServerStatusUrl()];
NSString* urlString = NSStringCreateWithSTString(GetServerStatusUrl());
NSURL* url = [NSURL URLWithString:urlString];
NSURLSessionConfiguration* URLSessionConfiguration =
[NSURLSessionConfiguration ephemeralSessionConfiguration];
Expand Down
57 changes: 0 additions & 57 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/StringTheory_NSString.h

This file was deleted.

63 changes: 0 additions & 63 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/StringTheory_NSString.mm

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
#import "PLSPatcherWindowController.h"
#import "PLSServerStatus.h"
#import "PLSView.h"
#import "StringTheory_NSString.h"

// stdlib
#include <algorithm>
#include <regex>
#include <unordered_set>

// Plasma engine
#include "hsDarwin.h"
#include "plClient/plClient.h"
#include "plClient/plClientLoader.h"
#include "plCmdParser.h"
Expand Down Expand Up @@ -473,7 +473,7 @@ - (void)startClient

- (void)updateWindowTitle
{
NSString* productTitle = [NSString stringWithSTString:plProduct::LongName()];
NSString* productTitle = NSStringCreateWithSTString(plProduct::LongName());
[self.window setTitle:productTitle];
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Plasma/CoreLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(CoreLib_HEADERS
hsBounds.h
hsColorRGBA.h
hsCpuID.h
hsDarwin.h
hsExceptions.h
hsExceptionStack.h
hsFastMath.h
Expand Down Expand Up @@ -81,7 +82,6 @@ target_link_libraries(
Threads::Threads
$<$<AND:$<CONFIG:Debug>,$<BOOL:${USE_VLD}>>:VLD::VLD>
"$<$<PLATFORM_ID:Darwin>:-framework Accelerate>"
PRIVATE
"$<$<PLATFORM_ID:Darwin>:-framework CoreFoundation>"
)
target_include_directories(
Expand Down
1 change: 1 addition & 0 deletions Sources/Plasma/CoreLib/_CoreLibPch.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com

#include "HeadSpin.h"
#include "hsWindows.h"
#include "hsDarwin.h"

#include <string_theory/formatter>
#include <string_theory/string>
Expand Down
Loading

0 comments on commit 94ad648

Please sign in to comment.