1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-10-31 13:23:12 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

extend new patchType: *.apk.hpatch & *.ppk.hpatch

This commit is contained in:
sisong
2021-04-05 18:45:42 +08:00
parent 48e0849804
commit c4d2523a92
11 changed files with 222 additions and 49 deletions

View File

@@ -13,4 +13,9 @@
+ (BOOL)bsdiffPatch:(NSString *)path
origin:(NSString *)origin
toDestination:(NSString *)destination;
+ (BOOL)hdiffPatch:(NSString *)path
origin:(NSString *)origin
toDestination:(NSString *)destination;
@end

View File

@@ -8,7 +8,7 @@
#import "BSDiff.h"
#include "bspatch.h"
#include "../../../android/jni/hpatch.h"
@implementation BSDiff
@@ -34,4 +34,26 @@
return YES;
}
+ (BOOL)hdiffPatch:(NSString *)patch
origin:(NSString *)origin
toDestination:(NSString *)destination
{
if (![[NSFileManager defaultManager] fileExistsAtPath:patch]) {
return NO;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:origin]) {
return NO;
}
if ([[NSFileManager defaultManager] fileExistsAtPath:destination]) {
[[NSFileManager defaultManager] removeItemAtPath:destination error:nil];
}
int err = hpatch_by_file([origin UTF8String], [destination UTF8String], [patch UTF8String]);
if (err) {
return NO;
}
return YES;
}
@end

View File

@@ -39,6 +39,7 @@ static NSString * const BUNDLE_PATCH_NAME = @"index.bundlejs.patch";
// error def
static NSString * const ERROR_OPTIONS = @"options error";
static NSString * const ERROR_BSDIFF = @"bsdiff error";
static NSString * const ERROR_HDIFFPATCH = @"hdiffpatch error";
static NSString * const ERROR_FILE_OPERATION = @"file operation error";
// event def
@@ -53,6 +54,9 @@ typedef NS_ENUM(NSInteger, PushyType) {
PushyTypeFullDownload = 1,
PushyTypePatchFromPackage = 2,
PushyTypePatchFromPpk = 3,
//TASK_TYPE_PLAIN_DOWNLOAD=4?
PushyTypeHPatchFromPackage = 5,
PushyTypeHPatchFromPpk = 6,
};
static BOOL ignoreRollback = false;
@@ -253,6 +257,34 @@ RCT_EXPORT_METHOD(downloadPatchFromPpk:(NSDictionary *)options
}];
}
RCT_EXPORT_METHOD(downloadHPatchFromPackage:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self doPushy:PushyTypeHPatchFromPackage options:options callback:^(NSError *error) {
if (error) {
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
}
else {
resolve(nil);
}
}];
}
RCT_EXPORT_METHOD(downloadHPatchFromPpk:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[self doPushy:PushyTypeHPatchFromPpk options:options callback:^(NSError *error) {
if (error) {
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
}
else {
resolve(nil);
}
}];
}
RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
{
NSString *hash = options[@"hash"];
@@ -349,7 +381,7 @@ RCT_EXPORT_METHOD(markSuccess)
return;
}
NSString *originHash = [RCTConvert NSString:options[@"originHash"]];
if (type == PushyTypePatchFromPpk && originHash <= 0) {
if (((type == PushyTypePatchFromPpk)||(type == PushyTypeHPatchFromPpk)) && originHash <= 0) {
callback([self errorWithMessage:ERROR_OPTIONS]);
return;
}
@@ -398,19 +430,27 @@ RCT_EXPORT_METHOD(markSuccess)
else {
switch (type) {
case PushyTypePatchFromPackage:
case PushyTypeHPatchFromPackage:
{
NSString *sourceOrigin = [[NSBundle mainBundle] resourcePath];
NSString *bundleOrigin = [[RCTPushy binaryBundleURL] path];
[self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
if (type==PushyTypeHPatchFromPackage)
[self hpatch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
else
[self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
}
break;
case PushyTypePatchFromPpk:
case PushyTypeHPatchFromPpk:
{
NSString *lastVersionDir = [dir stringByAppendingPathComponent:originHash];
NSString *sourceOrigin = lastVersionDir;
NSString *bundleOrigin = [lastVersionDir stringByAppendingPathComponent:BUNDLE_FILE_NAME];
[self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
if (type==PushyTypeHPatchFromPpk)
[self hpatch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
else
[self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
}
break;
default:
@@ -424,14 +464,15 @@ RCT_EXPORT_METHOD(markSuccess)
}];
}
- (void)patch:(NSString *)hash fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback
- (void)_dopatch:(NSString *)hash fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin
callback:(void (^)(NSError *error))callback isHPatch:(BOOL)isHPatch
{
NSString *unzipDir = [[RCTPushy downloadDir] stringByAppendingPathComponent:hash];
NSString *sourcePatch = [unzipDir stringByAppendingPathComponent:SOURCE_PATCH_NAME];
NSString *bundlePatch = [unzipDir stringByAppendingPathComponent:BUNDLE_PATCH_NAME];
NSString *destination = [unzipDir stringByAppendingPathComponent:BUNDLE_FILE_NAME];
[_fileManager bsdiffFileAtPath:bundlePatch fromOrigin:bundleOrigin toDestination:destination completionHandler:^(BOOL success) {
void (^)(BOOL success) completionHandler=^(BOOL success) {
if (success) {
NSData *data = [NSData dataWithContentsOfFile:sourcePatch];
NSError *error = nil;
@@ -454,9 +495,25 @@ RCT_EXPORT_METHOD(markSuccess)
}];
}
else {
callback([self errorWithMessage:ERROR_BSDIFF]);
if (isHPatch)
callback([self errorWithMessage:ERROR_HDIFFPATCH]);
else
callback([self errorWithMessage:ERROR_BSDIFF]);
}
}];
}
if (isHPatch)
[_fileManager hdiffFileAtPath:bundlePatch fromOrigin:bundleOrigin toDestination:destination completionHandler:completionHandler];
else
[_fileManager bsdiffFileAtPath:bundlePatch fromOrigin:bundleOrigin toDestination:destination completionHandler:completionHandler];
}
- (void)patch:(NSString *)hash fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback
{
[self _dopatch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback isHPatch:false];
}
- (void)hpatch:(NSString *)hash fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback
{
[self _dopatch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback isHPatch:true];
}
- (void)clearInvalidFiles
@@ -488,6 +545,10 @@ RCT_EXPORT_METHOD(markSuccess)
return @".ipa.patch";
case PushyTypePatchFromPpk:
return @".ppk.patch";
case PushyTypeHPatchFromPackage:
return @".ipa.hpatch";
case PushyTypeHPatchFromPpk:
return @".ppk.hpatch";
default:
break;
}

View File

@@ -23,6 +23,11 @@
toDestination:(NSString *)destination
completionHandler:(void (^)(BOOL success))completionHandler;
- (void)hdiffFileAtPath:(NSString *)path
fromOrigin:(NSString *)origin
toDestination:(NSString *)destination
completionHandler:(void (^)(BOOL success))completionHandler;
- (void)copyFiles:(NSDictionary *)filesDic
fromDir:(NSString *)fromDir
toDir:(NSString *)toDir

View File

@@ -88,6 +88,19 @@
});
}
- (void)hdiffFileAtPath:(NSString *)path
fromOrigin:(NSString *)origin
toDestination:(NSString *)destination
completionHandler:(void (^)(BOOL success))completionHandler
{
dispatch_async(_opQueue, ^{
BOOL success = [BSDiff hdiffPatch:path origin:origin toDestination:destination];
if (completionHandler) {
completionHandler(success);
}
});
}
- (void)copyFiles:(NSDictionary *)filesDic
fromDir:(NSString *)fromDir
toDir:(NSString *)toDir