2019-10-21 09:59:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2019-07-29 20:51:53 +08:00
|
|
|
//
|
|
|
|
// DoricShaderPlugin.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2019/7/29.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DoricShaderPlugin.h"
|
2019-07-31 14:18:20 +08:00
|
|
|
#import "DoricRootNode.h"
|
2019-11-18 16:33:23 +08:00
|
|
|
#import "DoricUtil.h"
|
2021-02-01 18:10:11 +08:00
|
|
|
#import "DoricExtensions.h"
|
2019-11-18 16:33:23 +08:00
|
|
|
|
|
|
|
#import <objc/runtime.h>
|
2019-07-29 20:51:53 +08:00
|
|
|
|
|
|
|
@implementation DoricShaderPlugin
|
|
|
|
|
2022-07-27 14:28:08 +08:00
|
|
|
- (DoricThreadMode)threadMode:(NSString *)method {
|
|
|
|
return DoricThreadModeUI;
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:51:53 +08:00
|
|
|
- (void)render:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
2019-11-25 11:53:31 +08:00
|
|
|
if (!argument) {
|
2019-11-22 17:07:16 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-03-29 17:53:27 +08:00
|
|
|
[self.doricContext.performanceProfile prepare:@"Render"];
|
2022-07-27 14:28:08 +08:00
|
|
|
if (self.doricContext == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[self.doricContext.performanceProfile start:@"Render"];
|
|
|
|
NSString *viewId = [argument optString:@"id"];
|
2019-11-29 09:35:20 +08:00
|
|
|
|
2022-07-27 14:28:08 +08:00
|
|
|
if (self.doricContext.rootNode.viewId == nil && [@"Root" isEqualToString:[argument optString:@"type"]]) {
|
|
|
|
self.doricContext.rootNode.viewId = viewId;
|
|
|
|
[self.doricContext.rootNode blend:[argument optObject:@"props"]];
|
|
|
|
[self.doricContext.rootNode requestLayout];
|
|
|
|
} else {
|
|
|
|
DoricViewNode *viewNode = [self.doricContext targetViewNode:viewId];
|
|
|
|
[viewNode blend:[argument optObject:@"props"]];
|
2023-02-07 14:11:42 +08:00
|
|
|
if (![viewNode isKindOfClass:DoricRootNode.class]) {
|
|
|
|
[viewNode.view.doricLayout apply];
|
|
|
|
}
|
2022-07-27 14:28:08 +08:00
|
|
|
[viewNode requestLayout];
|
|
|
|
}
|
|
|
|
[promise resolve:nil];
|
|
|
|
[self.doricContext.performanceProfile end:@"Render"];
|
2019-07-30 11:25:05 +08:00
|
|
|
}
|
|
|
|
|
2020-03-10 18:51:53 +08:00
|
|
|
- (void)command:(NSDictionary *)argument withPromise:(DoricPromise *)promise {
|
2022-07-27 14:28:08 +08:00
|
|
|
if (self.doricContext == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSArray *viewIds = [argument optArray:@"viewIds"];
|
|
|
|
id args = argument[@"args"];
|
|
|
|
NSString *name = [argument optString:@"name"];
|
|
|
|
DoricViewNode *viewNode = nil;
|
|
|
|
for (NSString *viewId in viewIds) {
|
2019-11-18 16:33:23 +08:00
|
|
|
if (!viewNode) {
|
2022-07-27 14:28:08 +08:00
|
|
|
viewNode = [self.doricContext targetViewNode:viewId];
|
2019-11-18 16:33:23 +08:00
|
|
|
} else {
|
2022-07-27 14:28:08 +08:00
|
|
|
if ([viewNode isKindOfClass:[DoricSuperNode class]]) {
|
|
|
|
viewNode = [((DoricSuperNode *) viewNode) subNodeWithViewId:viewId];
|
|
|
|
}
|
2019-11-18 16:33:23 +08:00
|
|
|
}
|
2022-07-27 14:28:08 +08:00
|
|
|
}
|
|
|
|
if (!viewNode) {
|
|
|
|
[promise reject:@"Cannot find opposite view"];
|
|
|
|
} else {
|
|
|
|
[self findClass:[viewNode class] target:viewNode method:name promise:promise argument:args];
|
|
|
|
}
|
2019-11-18 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)createParamWithMethodName:(NSString *)method promise:(DoricPromise *)promise argument:(id)argument {
|
2023-07-07 18:31:06 +08:00
|
|
|
if ([method isEqualToString:@"withPromise"] || [method isEqualToString:@"promise"]) {
|
2019-11-18 16:33:23 +08:00
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
return argument;
|
|
|
|
}
|
|
|
|
|
2020-03-10 18:51:53 +08:00
|
|
|
- (void)findClass:(Class)clz target:(id)target method:(NSString *)name promise:(DoricPromise *)promise argument:(id)argument {
|
2019-11-18 16:33:23 +08:00
|
|
|
unsigned int count;
|
|
|
|
Method *methods = class_copyMethodList(clz, &count);
|
|
|
|
BOOL isFound = NO;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
NSString *methodName = [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding];
|
|
|
|
NSArray *array = [methodName componentsSeparatedByString:@":"];
|
|
|
|
if (array && [array count] > 0) {
|
2023-07-07 18:31:06 +08:00
|
|
|
NSString *firstPart = array[0];
|
|
|
|
if (![firstPart isEqualToString:name]) {
|
|
|
|
firstPart = [firstPart componentsSeparatedByString:@"With"][0];
|
|
|
|
}
|
|
|
|
if ([firstPart isEqualToString:name]) {
|
2019-11-18 16:33:23 +08:00
|
|
|
isFound = YES;
|
|
|
|
SEL selector = NSSelectorFromString(methodName);
|
|
|
|
NSMethodSignature *methodSignature = [target methodSignatureForSelector:selector];
|
|
|
|
if (methodSignature) {
|
|
|
|
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
|
2020-04-23 17:42:32 +08:00
|
|
|
[invocation retainArguments];
|
2019-11-18 16:33:23 +08:00
|
|
|
invocation.selector = selector;
|
|
|
|
invocation.target = target;
|
2020-03-25 19:32:01 +08:00
|
|
|
void *retValue;
|
|
|
|
@try {
|
|
|
|
NSMutableArray *tempArray = [NSMutableArray new];
|
|
|
|
|
|
|
|
for (NSUInteger idx = 2; idx < methodSignature.numberOfArguments; idx++) {
|
|
|
|
if (idx - 2 > [array count]) {
|
|
|
|
break;
|
2019-11-18 16:33:23 +08:00
|
|
|
}
|
2020-03-25 19:32:01 +08:00
|
|
|
id param = [self createParamWithMethodName:array[idx - 2] promise:promise argument:argument];
|
|
|
|
if (param) {
|
|
|
|
[tempArray addObject:param];
|
|
|
|
}
|
|
|
|
[invocation setArgument:¶m atIndex:idx];
|
2019-11-27 10:19:25 +08:00
|
|
|
}
|
2020-03-25 19:32:01 +08:00
|
|
|
[invocation invoke];
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
DoricLog(@"CallNative Error:%@", exception.reason);
|
|
|
|
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
|
|
|
|
}
|
|
|
|
const char *retType = methodSignature.methodReturnType;
|
|
|
|
if (!strcmp(retType, @encode(void))) {
|
|
|
|
} else {
|
|
|
|
[invocation getReturnValue:&retValue];
|
|
|
|
id returnValue = (__bridge id) retValue;
|
|
|
|
[promise resolve:returnValue];
|
|
|
|
}
|
2019-11-18 16:33:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (methods) {
|
|
|
|
free(methods);
|
|
|
|
}
|
|
|
|
if (!isFound) {
|
|
|
|
Class superclass = class_getSuperclass(clz);
|
|
|
|
if (superclass && superclass != [NSObject class]) {
|
2020-03-10 18:51:53 +08:00
|
|
|
[self findClass:superclass target:target method:name promise:promise argument:argument];
|
2019-11-18 16:33:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 20:51:53 +08:00
|
|
|
@end
|