Skip to content

Commit f7cfbf1

Browse files
committed
refactor: construct preload data in memory
1 parent 2a44076 commit f7cfbf1

File tree

6 files changed

+47
-19
lines changed

6 files changed

+47
-19
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@
4444
},
4545
"editor.formatOnSave": true,
4646
"markdown.validate.enabled": true
47-
}
47+
}

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ before-all::
2727
git submodule update --init --recursive || exit 1; \
2828
fi
2929

30-
cp sources/preload.js resources/preload.js
31-
3230
@if [ -n "$$UNBOUND_PK" ]; then \
3331
echo -n "$(COMMIT_HASH)" | openssl dgst -sha256 -sign <(printf '%s' "$$UNBOUND_PK" | tr -d '\r') -out resources/signature.bin 2>/dev/null; \
3432
elif [ -f "private_key.pem" ]; then \
@@ -39,4 +37,4 @@ after-stage::
3937
find $(THEOS_STAGING_DIR) -name ".DS_Store" -delete
4038

4139
after-package::
42-
rm -f resources/preload.js resources/signature.bin
40+
rm -f resources/signature.bin

headers/Utilities.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import <Security/Security.h>
33
#import <AVFoundation/AVFoundation.h>
44
#import <dlfcn.h>
5+
#import <mach-o/dyld.h>
56
#import <rootless.h>
67
#import <sys/utsname.h>
78
#import <mach-o/fat.h>
@@ -139,10 +140,14 @@ extern const CGFloat DYNAMIC_ISLAND_TOP_INSET;
139140
+ (NSArray<NSString *> *)getAvailableAppExtensions;
140141
+ (BOOL)hasAppExtension:(NSString *)extensionName;
141142

143+
+ (NSString *)getCurrentDylibName;
144+
142145
+ (NSDictionary *)getApplicationEntitlements;
143146
+ (NSDictionary *)getApplicationSignatureInfo;
144147

145148
+ (NSString *)formatEntitlementsAsPlist:(NSDictionary *)entitlements;
146149
+ (BOOL)isVerifiedBuild;
147150

151+
+ (NSString *)JSONString:(NSString *)str;
152+
148153
@end

sources/Unbound.x

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,28 @@ id gBridge = nil;
6060
// Preload Unbound's settings, plugins, themes and fonts
6161
@try
6262
{
63-
NSString *bundle = [Utilities getResource:@"preload"];
6463
NSString *settings = [Settings getSettings];
6564
NSString *plugins = [Plugins makeJSON];
6665
NSString *themes = [Themes makeJSON];
6766
NSString *availableFonts = [Fonts makeAvailableJSON];
6867
NSString *fonts = [Fonts makeJSON];
6968

70-
NSString *version = PACKAGE_VERSION;
71-
72-
NSString *json = [NSString
73-
stringWithFormat:bundle, settings, plugins, themes, fonts, availableFonts, version];
69+
NSString *origin = [Utilities JSONString:[Utilities getCurrentDylibName]];
70+
NSString *version = [Utilities JSONString:PACKAGE_VERSION];
71+
72+
NSString *preloadScript = [NSString
73+
stringWithFormat:@"this.UNBOUND_SETTINGS = %@;\n"
74+
@"this.UNBOUND_PLUGINS = %@;\n"
75+
@"this.UNBOUND_THEMES = %@;\n"
76+
@"this.UNBOUND_FONTS = %@;\n"
77+
@"this.UNBOUND_AVAILABLE_FONTS = %@;\n\n"
78+
@"this.UNBOUND_LOADER = {\n"
79+
@" origin: %@,\n"
80+
@" version: %@,\n"
81+
@"};",
82+
settings, plugins, themes, fonts, availableFonts, origin, version];
83+
84+
NSData *data = [preloadScript dataUsingEncoding:NSUTF8StringEncoding];
7485

7586
[Logger info:LOG_CATEGORY_DEFAULT
7687
format:@"Pre-loading settings, plugins, fonts and themes..."];

sources/Utilities.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,22 @@ + (BOOL)hasAppExtension:(NSString *)extensionName
10551055
return exists;
10561056
}
10571057

1058+
+ (NSString *)getCurrentDylibName
1059+
{
1060+
Dl_info info;
1061+
memset(&info, 0, sizeof(info));
1062+
1063+
IMP implementation = [self methodForSelector:@selector(getCurrentDylibName)];
1064+
1065+
if (dladdr((const void *) implementation, &info) && info.dli_fname)
1066+
{
1067+
NSString *fullPath = [NSString stringWithUTF8String:info.dli_fname];
1068+
return [fullPath lastPathComponent];
1069+
}
1070+
1071+
return nil;
1072+
}
1073+
10581074
+ (NSDictionary *)getApplicationEntitlements
10591075
{
10601076
NSDictionary *signatureInfo = [self getApplicationSignatureInfo];
@@ -1412,4 +1428,12 @@ + (UIAlertAction *)createDiscordInviteButton
14121428
}];
14131429
}
14141430

1431+
+ (NSString *)JSONString:(NSString *)str
1432+
{
1433+
if (!str)
1434+
return @"null";
1435+
NSString *escaped = [str stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
1436+
return [NSString stringWithFormat:@"\"%@\"", escaped];
1437+
}
1438+
14151439
@end

sources/preload.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)