Skip to content

Commit

Permalink
Set icudt.dat file path at runtime when loading library
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmojo committed Jul 14, 2023
1 parent b265b7e commit f2d4f2b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class TypeCollector
typeof(System.Enum),
typeof(System.Array),
typeof(byte[]),
typeof(string[])
typeof(string[]),
typeof(System.AppContext)
};

private static readonly Type[] UNSUPPORTED_TYPES = new [] {
Expand Down
11 changes: 6 additions & 5 deletions Samples/Beyond.NET.Sample.Native/Beyond.NET.Sample.Native.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
<!-- Culture stuff for iOS Support -->
<Choose>
<When Condition="$(RuntimeIdentifier.Contains('ios'))">
<PropertyGroup>
<!-- If globalization is not required, you can enable this instead of providing an icudt.dat file on iOS -->
<!-- <PropertyGroup>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
</PropertyGroup> -->

<!-- Currently disabled because we can't make it work in an XCTest bundle, see https://github.com/royalapplications/beyondnet/issues/53 -->
<!-- ItemGroup>
<!-- When this is set, an icudt.dat must be placed in the resulting bundle -->
<ItemGroup>
<RuntimeHostConfigurationOption Include="ICU_DAT_FILE_PATH" Value="icudt.dat" />
</ItemGroup -->
</ItemGroup>
</When>
</Choose>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
E7C8CF0B29C1FE5800C4230F /* SystemArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7C8CF0A29C1FE5800C4230F /* SystemArrayTests.swift */; };
E7C8CF0D29C2139B00C4230F /* PersonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7C8CF0C29C2139B00C4230F /* PersonTests.swift */; };
E7C8CF0F29C2180200C4230F /* AnimalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7C8CF0E29C2180200C4230F /* AnimalTests.swift */; };
E7CB97C62A612A8A0086CF43 /* icudt.dat in Resources */ = {isa = PBXBuildFile; fileRef = E71E42282A6030760075FE5F /* icudt.dat */; };
E7CB97CE2A612BE30086CF43 /* __LibraryInit.m in Sources */ = {isa = PBXBuildFile; fileRef = E7CB97CC2A612BE30086CF43 /* __LibraryInit.m */; };
E7EB338629C8879100392F3D /* AddressTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7EB338529C8879100392F3D /* AddressTests.swift */; };
E7ED7A1829D35DF4008C96E0 /* SystemVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7ED7A1729D35DF4008C96E0 /* SystemVersionTests.swift */; };
E7ED7A1A29D445C8008C96E0 /* SystemActionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7ED7A1929D445C8008C96E0 /* SystemActionTests.swift */; };
Expand Down Expand Up @@ -198,6 +200,7 @@
E7C8CF0A29C1FE5800C4230F /* SystemArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemArrayTests.swift; sourceTree = "<group>"; };
E7C8CF0C29C2139B00C4230F /* PersonTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PersonTests.swift; sourceTree = "<group>"; };
E7C8CF0E29C2180200C4230F /* AnimalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimalTests.swift; sourceTree = "<group>"; };
E7CB97CC2A612BE30086CF43 /* __LibraryInit.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = __LibraryInit.m; sourceTree = "<group>"; };
E7E7491B2A026A1C009590A8 /* Generated_C.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Generated_C.h; path = ../../Generated/Generated_C.h; sourceTree = "<group>"; };
E7E7491C2A026A1C009590A8 /* Generated_Swift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Generated_Swift.swift; path = ../../Generated/Generated_Swift.swift; sourceTree = "<group>"; };
E7EB338529C8879100392F3D /* AddressTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -261,6 +264,7 @@
children = (
E7C8CF0129C1FA6C00C4230F /* Tests */,
E7E749192A0269AB009590A8 /* Playground */,
E7CB97CA2A612BC10086CF43 /* LibraryInit */,
E7E7491A2A0269DD009590A8 /* Generated */,
E70D116029BF9F2600B4E738 /* BeyondNETSampleSwift.h */,
);
Expand Down Expand Up @@ -383,6 +387,14 @@
path = TestClasses;
sourceTree = "<group>";
};
E7CB97CA2A612BC10086CF43 /* LibraryInit */ = {
isa = PBXGroup;
children = (
E7CB97CC2A612BE30086CF43 /* __LibraryInit.m */,
);
path = LibraryInit;
sourceTree = "<group>";
};
E7E749192A0269AB009590A8 /* Playground */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -468,6 +480,7 @@
};
E71E41E52A60030F0075FE5F = {
CreatedOnToolsVersion = 14.3.1;
LastSwiftMigration = 1430;
};
E7B491D32A5FDBD900A7C75D = {
CreatedOnToolsVersion = 14.3.1;
Expand Down Expand Up @@ -507,6 +520,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
E7CB97C62A612A8A0086CF43 /* icudt.dat in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -620,6 +634,7 @@
files = (
E71E41F62A6003F30075FE5F /* Generated_Swift.swift in Sources */,
E71E41FB2A6010970075FE5F /* SwiftSyntaxPlayground.swift in Sources */,
E7CB97CE2A612BE30086CF43 /* __LibraryInit.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -808,6 +823,7 @@
E71E41EA2A60030F0075FE5F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
Expand Down Expand Up @@ -837,6 +853,7 @@
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -845,6 +862,7 @@
E71E41EB2A60030F0075FE5F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
E71E42112A6015C90075FE5F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E71E42102A6015C90075FE5F /* Assets.xcassets */; };
E71E42242A6017710075FE5F /* libBeyondDotNETSampleNative.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = E71E42232A6017710075FE5F /* libBeyondDotNETSampleNative.xcframework */; };
E71E42252A6017710075FE5F /* libBeyondDotNETSampleNative.xcframework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = E71E42232A6017710075FE5F /* libBeyondDotNETSampleNative.xcframework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
E71E422E2A6032A30075FE5F /* icudt.dat in Resources */ = {isa = PBXBuildFile; fileRef = E71E422A2A6030820075FE5F /* icudt.dat */; };
E71E424B2A603E030075FE5F /* BeyondNETSampleSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E71E424A2A603E030075FE5F /* BeyondNETSampleSwift.framework */; };
E71E424C2A603E030075FE5F /* BeyondNETSampleSwift.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = E71E424A2A603E030075FE5F /* BeyondNETSampleSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
Expand All @@ -38,7 +37,6 @@
E71E420E2A6015C80075FE5F /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
E71E42102A6015C90075FE5F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
E71E42232A6017710075FE5F /* libBeyondDotNETSampleNative.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = libBeyondDotNETSampleNative.xcframework; path = ../libBeyondDotNETSampleNative.xcframework; sourceTree = "<group>"; };
E71E422A2A6030820075FE5F /* icudt.dat */ = {isa = PBXFileReference; lastKnownFileType = file; name = icudt.dat; path = ../icudt.dat; sourceTree = "<group>"; };
E71E424A2A603E030075FE5F /* BeyondNETSampleSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = BeyondNETSampleSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand All @@ -58,7 +56,6 @@
E71E42002A6015C80075FE5F = {
isa = PBXGroup;
children = (
E71E422A2A6030820075FE5F /* icudt.dat */,
E71E420B2A6015C80075FE5F /* Source */,
E71E420A2A6015C80075FE5F /* Products */,
E71E421A2A6016040075FE5F /* Frameworks */,
Expand Down Expand Up @@ -152,7 +149,6 @@
buildActionMask = 2147483647;
files = (
E71E42112A6015C90075FE5F /* Assets.xcassets in Resources */,
E71E422E2A6032A30075FE5F /* icudt.dat in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
52 changes: 52 additions & 0 deletions Samples/Beyond.NET.Sample.Swift/Source/LibraryInit/__LibraryInit.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#import <Foundation/Foundation.h>
#import <TargetConditionals.h>

#import <Generated_C.h>

@interface __DNLibraryInit : NSObject
@end

@implementation __DNLibraryInit

+ (void)load {
#if TARGET_OS_IOS
// iOS/iOS Simulator
NSBundle* bundle = [NSBundle bundleForClass:__DNLibraryInit.class];

if (!bundle) {
return;
}

System_Exception_t ex = NULL;
System_String_t name = DNStringFromC("ICU_DAT_FILE_PATH");

if (!name) {
return;
}

NSString* icuPath = [bundle pathForResource:@"icudt" ofType:@"dat"];

if (!icuPath) {
return;
}

System_String_t icuPathDN = DNStringFromC(icuPath.UTF8String);

System_AppContext_SetData(name,
icuPathDN,
&ex);

System_String_Destroy(name);
System_String_Destroy(icuPathDN);

if (ex) {
NSLog(@"Error: Setting icudt.dat path failed.");
}
#elif TARGET_OS_MAC && !TARGET_OS_IPHONE
// macOS
#else
// Other platform
#endif
}

@end

0 comments on commit f2d4f2b

Please sign in to comment.