Skip to content

Commit

Permalink
[mbr] Add an apple sample
Browse files Browse the repository at this point in the history
Works on iOS simulator and Mac Catalyst
  • Loading branch information
lambdageek committed Apr 5, 2021
1 parent 621d798 commit 7670aca
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/mono/sample/mbr/DeltaHelper/DeltaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private static Action<Assembly, byte[], byte[], byte[]> MakeUpdateMethod (Method

private static void LoadMetadataUpdate (Assembly assm, byte[] dmeta_data, byte[] dil_data, byte[] dpdb_data)
{
UpdateMethod (assm, dmeta_data, dil_data, dpdb_data);
// UpdateMethod (assm, dmeta_data, dil_data, dpdb_data);
System.Reflection.Metadata.AssemblyExtensions.ApplyUpdate (assm, dmeta_data, dil_data, dpdb_data);
}

DeltaHelper () { }
Expand Down
21 changes: 20 additions & 1 deletion src/mono/sample/mbr/apple/AppleDelta.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
<RuntimeIdentifier>$(TargetOS.ToLower())-$(TargetArchitecture)</RuntimeIdentifier>
<DefineConstants Condition="'$(ArchiveTests)' == 'true'">$(DefineConstants);CI_TEST</DefineConstants>
<MonoForceInterpreter>true</MonoForceInterpreter>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="ChangeablePart.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DeltaHelper\DeltaHelper.csproj" />
</ItemGroup>

<PropertyGroup>
<DeltaScript>deltascript.json</DeltaScript>
<DeltaCount>1</DeltaCount>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetOS)' == 'MacCatalyst'">
Expand Down Expand Up @@ -50,6 +65,7 @@
ProjectName="AppleDelta"
MonoRuntimeHeaders="$(MicrosoftNetCoreAppRuntimePackDir)native\include\mono-2.0"
Assemblies="@(BundleAssemblies)"
NativeMainSource="$(MSBuildThisFileDirectory)\main.m"
MainLibraryFileName="AppleDelta.dll"
GenerateXcodeProject="True"
BuildAppBundle="True"
Expand All @@ -66,11 +82,14 @@
<Message Importance="High" Text="Xcode: $(XcodeProjectPath)"/>
<Message Importance="High" Text="App: $(AppBundlePath)"/>

<Exec Condition="'$(TargetOS)' == 'iOSSimulator'" Command="dotnet xharness apple run --app=$(AppBundlePath) --targets=ios-simulator --output-directory=/tmp/out" />
<Exec Condition="'$(TargetOS)' == 'iOSSimulator'" Command="dotnet xharness apple run --app=$(AppBundlePath) --targets=ios-simulator-64 --output-directory=/tmp/out" />

<!-- run on MacCatalyst -->
<Exec Condition="'$(TargetOS)' == 'MacCatalyst'" Command="dotnet xharness apple run --app=$(AppBundlePath) --targets=maccatalyst --output-directory=/tmp/out" />

</Target>

<!-- Set RoslynILDiffFullPath property to the path of roslynildiff -->
<Import Project="..\DeltaHelper.targets" />

</Project>
9 changes: 9 additions & 0 deletions src/mono/sample/mbr/apple/ChangeablePart.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public class ChangeablePart
{
public static int UpdateCounter (ref int counter)
{
return ++counter;
}
}
9 changes: 9 additions & 0 deletions src/mono/sample/mbr/apple/ChangeablePart_v1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public class ChangeablePart
{
public static int UpdateCounter (ref int counter)
{
return --counter;
}
}
16 changes: 15 additions & 1 deletion src/mono/sample/mbr/apple/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,36 @@ public static class Program
[DllImport("__Internal")]
unsafe private static extern void ios_register_button_click(delegate* unmanaged<void> callback);

[DllImport("__Internal")]
unsafe private static extern void ios_register_applyupdate_click(delegate* unmanaged<void> callback);

private static int counter = 0;

// Called by native code, see main.m
[UnmanagedCallersOnly]
private static void OnButtonClick()
{
ios_set_text("OnButtonClick! #" + counter++);
ios_set_text("OnButtonClick! #" + ChangeablePart.UpdateCounter (ref counter));
}

[UnmanagedCallersOnly]
private static void OnApplyUpdateClick()
{
deltaHelper.Update (typeof(ChangeablePart).Assembly);
}

static MonoDelta.DeltaHelper deltaHelper;

public static async Task Main(string[] args)
{
unsafe {
// Register a managed callback (will be called by UIButton, see main.m)
delegate* unmanaged<void> unmanagedPtr = &OnButtonClick;
ios_register_button_click(unmanagedPtr);
delegate* unmanaged<void> unmanagedPtr2 = &OnApplyUpdateClick;
ios_register_applyupdate_click(unmanagedPtr2);
}
deltaHelper = MonoDelta.DeltaHelper.Make();
const string msg = "Hello World!\n.NET 5.0";
for (int i = 0; i < msg.Length; i++)
{
Expand Down
5 changes: 5 additions & 0 deletions src/mono/sample/mbr/apple/deltascript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"changes": [
{"document": "ChangeablePart.cs", "update": "ChangeablePart_v1.cs"},
]
}
30 changes: 28 additions & 2 deletions src/mono/sample/mbr/apple/main.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <stdlib.h>

#import <UIKit/UIKit.h>
#import "runtime.h"

Expand All @@ -24,6 +26,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

UILabel *label;
void (*clickHandlerPtr)(void);
void (*clickHandlerApplyUpdatePtr)(void);

@implementation ViewController

Expand All @@ -35,15 +38,23 @@ - (void)viewDidLoad {
label.font = [UIFont boldSystemFontOfSize: 30];
label.numberOfLines = 2;
label.textAlignment = NSTextAlignmentCenter;
label.text = @"Hello, wire me up!\n(dllimport ios_set_text)";
[self.view addSubview:label];

UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(50, 300, 200, 50)];
[button setTitle:@"Click me" forState:UIControlStateNormal];
[button setFrame:CGRectMake(50, 250, 200, 50)];
[button setTitle:@"Click me (wire me up)" forState:UIControlStateNormal];
[button setExclusiveTouch:YES];
[self.view addSubview:button];

UIButton *apply_button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[apply_button addTarget:self action:@selector(applyUpdateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[apply_button setFrame:CGRectMake(50, 300, 200, 50)];
[apply_button setTitle:@"ApplyUpdate" forState:UIControlStateNormal];
[apply_button setExclusiveTouch:YES];
[self.view addSubview:apply_button];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
mono_ios_runtime_init ();
});
Expand All @@ -54,6 +65,12 @@ -(void) buttonClicked:(UIButton*)sender
clickHandlerPtr();
}

-(void) applyUpdateButtonClicked:(UIButton*)sender
{
if (clickHandlerApplyUpdatePtr)
clickHandlerApplyUpdatePtr();
}

@end

// called from C# sample
Expand All @@ -63,6 +80,13 @@ -(void) buttonClicked:(UIButton*)sender
clickHandlerPtr = ptr;
}

// called from C# sample
void
ios_register_applyupdate_click (void* ptr)
{
clickHandlerApplyUpdatePtr = ptr;
}

// called from C# sample
void
ios_set_text (const char* value)
Expand All @@ -75,6 +99,8 @@ -(void) buttonClicked:(UIButton*)sender

int main(int argc, char * argv[]) {
@autoreleasepool {
setenv("DOTNET_MODIFIABLE_ASSEMBLIES", "Debug", 1);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

0 comments on commit 7670aca

Please sign in to comment.