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

update pushy reference method (#499)

* update pushy reference method

* update
This commit is contained in:
波仔糕
2025-05-12 14:23:45 +08:00
committed by GitHub
parent e8ec85c65f
commit 18d9b75545
37 changed files with 2 additions and 9 deletions

View File

@@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.13)
project(rnupdate)
set(HDIFFPATCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/HDiffPatch)
set(LZMA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lzma)
set(HDP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/pushy.c
${CMAKE_CURRENT_SOURCE_DIR}/hpatch.c
${HDIFFPATCH_DIR}/libHDiffPatch/HPatch/patch.c
${HDIFFPATCH_DIR}/file_for_patch.c
${LZMA_DIR}/C/LzmaDec.c
${LZMA_DIR}/C/Lzma2Dec.c
)
set(CMAKE_VERBOSE_MAKEFILE on)
add_library(rnupdate SHARED
${HDP_SOURCES}
)
target_include_directories(rnupdate PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${HDIFFPATCH_DIR}
${HDIFFPATCH_DIR}/libHDiffPatch/HPatch
${LZMA_DIR}/C
)
target_link_libraries(rnupdate PUBLIC
libace_napi.z.so
)
file(GLOB rnoh_pushy_SRC CONFIGURE_DEPENDS *.cpp)
add_library(rnoh_pushy SHARED ${rnoh_pushy_SRC})
target_include_directories(rnoh_pushy PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(rnoh_pushy PUBLIC rnoh)

View File

@@ -0,0 +1,55 @@
/**
* MIT License
*
* Copyright (C) 2023 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef GEO_LOCATION_PACKAGE_H
#define GEO_LOCATION_PACKAGE_H
#include "RNOH/Package.h"
#include "PushyTurboModule.h"
using namespace rnoh;
using namespace facebook;
class PushyTurboModuleFactoryDelegate : public TurboModuleFactoryDelegate {
public:
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override
{
if (name == "Pushy") {
return std::make_shared<PushyTurboModule>(ctx, name);
}
return nullptr;
};
};
namespace rnoh {
class PushyPackage : public Package {
public:
PushyPackage(Package::Context ctx) : Package(ctx){}
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override
{
return std::make_unique<PushyTurboModuleFactoryDelegate>();
}
};
} // namespace rnoh
#endif

View File

@@ -0,0 +1,142 @@
#include "PushyTurboModule.h"
using namespace rnoh;
using namespace facebook;
static jsi::Value _hostFunction_PushyTurboModule_getConstants(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"getConstants", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_setLocalHashInfo(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"setLocalHashInfo", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_getLocalHashInfo(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"getLocalHashInfo", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_setUuid(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"setUuid", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_reloadUpdate(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"reloadUpdate", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_setNeedUpdate(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"setNeedUpdate", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_markSuccess(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"markSuccess", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_downloadPatchFromPpk(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"downloadPatchFromPpk", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_downloadPatchFromPackage(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"downloadPatchFromPackage", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_downloadFullUpdate(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"downloadFullUpdate", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_downloadAndInstallApk(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"downloadAndInstallApk", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_addListener(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"addListener", args, count));
}
static jsi::Value _hostFunction_PushyTurboModule_removeListeners(
jsi::Runtime &rt,
react::TurboModule & turboModule,
const jsi::Value* args,
size_t count)
{
return jsi::Value(static_cast<ArkTSTurboModule &> (turboModule).call(rt,"removeListeners", args, count));
}
PushyTurboModule::PushyTurboModule(const ArkTSTurboModule::Context ctx, const std::string name)
: ArkTSTurboModule(ctx,name)
{
methodMap_["getConstants"]= MethodMetadata{1, _hostFunction_PushyTurboModule_getConstants};
methodMap_["setLocalHashInfo"]= MethodMetadata{2, _hostFunction_PushyTurboModule_setLocalHashInfo};
methodMap_["getLocalHashInfo"]= MethodMetadata{3, _hostFunction_PushyTurboModule_getLocalHashInfo};
methodMap_["setUuid"]= MethodMetadata{1, _hostFunction_PushyTurboModule_setUuid};
methodMap_["reloadUpdate"]= MethodMetadata{0, _hostFunction_PushyTurboModule_reloadUpdate};
methodMap_["setNeedUpdate"]= MethodMetadata{0, _hostFunction_PushyTurboModule_setNeedUpdate};
methodMap_["markSuccess"]= MethodMetadata{0, _hostFunction_PushyTurboModule_markSuccess};
methodMap_["downloadPatchFromPpk"]= MethodMetadata{0, _hostFunction_PushyTurboModule_downloadPatchFromPpk};
methodMap_["downloadPatchFromPackage"]= MethodMetadata{0, _hostFunction_PushyTurboModule_downloadPatchFromPackage};
methodMap_["downloadFullUpdate"]= MethodMetadata{0, _hostFunction_PushyTurboModule_downloadFullUpdate};
methodMap_["downloadAndInstallApk"]= MethodMetadata{0, _hostFunction_PushyTurboModule_downloadAndInstallApk};
methodMap_["addListener"]= MethodMetadata{1, _hostFunction_PushyTurboModule_addListener};
methodMap_["removeListeners"]= MethodMetadata{1, _hostFunction_PushyTurboModule_removeListeners};
}

View File

@@ -0,0 +1,38 @@
/**
* MIT License
*
* Copyright (C) 2023 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef GEO_LOCATION_TURBOMODULE_H
#define GEO_LOCATION_TURBOMODULE_H
#include <ReactCommon/TurboModule.h>
#include "RNOH/ArkTSTurboModule.h"
namespace rnoh {
class JSI_EXPORT PushyTurboModule : public ArkTSTurboModule {
public:
PushyTurboModule(const ArkTSTurboModule::Context ctx, const std::string name);
};
} // namespace rnoh
#endif

View File

@@ -0,0 +1,137 @@
// hpatch.c
// Copyright 2021 housisong, All rights reserved
#include "hpatch.h"
#include "HDiffPatch/libHDiffPatch/HPatch/patch.h"
#include "HDiffPatch/file_for_patch.h"
//#define _CompressPlugin_zlib
//#define _CompressPlugin_bz2
#define _CompressPlugin_lzma
#define _CompressPlugin_lzma2
#define _IsNeedIncludeDefaultCompressHead 0
#include "lzma/C/LzmaDec.h"
#include "lzma/C/Lzma2Dec.h"
#include "HDiffPatch/decompress_plugin_demo.h"
#define kMaxLoadMemOldSize ((1<<20)*8)
#define _check(v,errorType) do{ \
if (!(v)){ if (result==kHPatch_ok) result=errorType; if (!_isInClear){ goto _clear; }; } }while(0)
int hpatch_getInfo_by_mem(hpatch_singleCompressedDiffInfo* out_patinfo,
const uint8_t* pat,size_t patsize){
hpatch_TStreamInput patStream;
mem_as_hStreamInput(&patStream,pat,pat+patsize);
if (!getSingleCompressedDiffInfo(out_patinfo,&patStream,0))
return kHPatch_error_info;//data error;
return kHPatch_ok; //ok
}
static hpatch_TDecompress* getDecompressPlugin(const char* compressType){
#ifdef _CompressPlugin_zlib
if (zlibDecompressPlugin.is_can_open(compressType))
return &zlibDecompressPlugin;
#endif
#ifdef _CompressPlugin_bz2
if (bz2DecompressPlugin.is_can_open(compressType))
return &bz2DecompressPlugin;
#endif
#ifdef _CompressPlugin_lzma
if (lzmaDecompressPlugin.is_can_open(compressType))
return &lzmaDecompressPlugin;
#endif
#ifdef _CompressPlugin_lzma2
if (lzma2DecompressPlugin.is_can_open(compressType))
return &lzma2DecompressPlugin;
#endif
return 0;
}
static int hpatch_by_stream(const hpatch_TStreamInput* old,hpatch_BOOL isLoadOldAllToMem,const hpatch_TStreamInput* pat,
hpatch_TStreamOutput* out_new,const hpatch_singleCompressedDiffInfo* patInfo){
int result=kHPatch_ok;
int _isInClear=hpatch_FALSE;
hpatch_TDecompress* decompressPlugin=0;
uint8_t* temp_cache=0;
size_t temp_cache_size;
hpatch_singleCompressedDiffInfo _patinfo;
hpatch_TStreamInput _old;
{// info
if (!patInfo){
_check(getSingleCompressedDiffInfo(&_patinfo,pat,0),kHPatch_error_info);
patInfo=&_patinfo;
}
_check(old->streamSize==patInfo->oldDataSize,kHPatch_error_old_size);
_check(out_new->streamSize>=patInfo->newDataSize,kHPatch_error_new_size);
out_new->streamSize=patInfo->newDataSize;
if (strlen(patInfo->compressType)>0){
decompressPlugin=getDecompressPlugin(patInfo->compressType);
_check(decompressPlugin,kHPatch_error_compressType);
}
}
{// mem
size_t mem_size;
size_t oldSize=(size_t)old->streamSize;
isLoadOldAllToMem=isLoadOldAllToMem&&(old->streamSize<=kMaxLoadMemOldSize);
temp_cache_size=patInfo->stepMemSize+hpatch_kFileIOBufBetterSize*3;
mem_size=temp_cache_size+(isLoadOldAllToMem?oldSize:0);
temp_cache=malloc(mem_size);
_check(temp_cache,kHPatch_error_malloc);
if (isLoadOldAllToMem){//load old to mem
uint8_t* oldMem=temp_cache+temp_cache_size;
_check(old->read(old,0,oldMem,oldMem+oldSize),kHPatch_error_old_fread);
mem_as_hStreamInput(&_old,oldMem,oldMem+oldSize);
old=&_old;
}
}
_check(patch_single_compressed_diff(out_new,old,pat,patInfo->diffDataPos,
patInfo->uncompressedSize,decompressPlugin,patInfo->coverCount,
patInfo->stepMemSize,temp_cache,temp_cache+temp_cache_size),kHPatch_error_patch);
_clear:
_isInClear=hpatch_TRUE;
if (temp_cache){ free(temp_cache); temp_cache=0; }
return result;
}
int hpatch_by_mem(const uint8_t* old,size_t oldsize,uint8_t* newBuf,size_t newsize,
const uint8_t* pat,size_t patsize,const hpatch_singleCompressedDiffInfo* patInfo){
hpatch_TStreamInput oldStream;
hpatch_TStreamInput patStream;
hpatch_TStreamOutput newStream;
mem_as_hStreamInput(&oldStream,old,old+oldsize);
mem_as_hStreamInput(&patStream,pat,pat+patsize);
mem_as_hStreamOutput(&newStream,newBuf,newBuf+newsize);
return hpatch_by_stream(&oldStream,hpatch_FALSE,&patStream,&newStream,patInfo);
}
int hpatch_by_file(const char* oldfile, const char* newfile, const char* patchfile){
int result=kHPatch_ok;
int _isInClear=hpatch_FALSE;
int patch_result;
hpatch_TFileStreamInput oldStream;
hpatch_TFileStreamInput patStream;
hpatch_TFileStreamOutput newStream;
hpatch_TFileStreamInput_init(&oldStream);
hpatch_TFileStreamInput_init(&patStream);
hpatch_TFileStreamOutput_init(&newStream);
_check(hpatch_TFileStreamInput_open(&oldStream,oldfile),kHPatch_error_old_fopen);
_check(hpatch_TFileStreamInput_open(&patStream,patchfile),kHPatch_error_pat_fopen);
_check(hpatch_TFileStreamOutput_open(&newStream,newfile,~(hpatch_StreamPos_t)0),kHPatch_error_new_fopen);
patch_result=hpatch_by_stream(&oldStream.base,hpatch_TRUE,&patStream.base,&newStream.base,0);
if (patch_result!=kHPatch_ok){
_check(!oldStream.fileError,kHPatch_error_old_fread);
_check(!patStream.fileError,kHPatch_error_pat_fread);
_check(!newStream.fileError,kHPatch_error_new_fwrite);
_check(hpatch_FALSE,patch_result);
}
_clear:
_isInClear=hpatch_TRUE;
_check(hpatch_TFileStreamInput_close(&oldStream),kHPatch_error_old_fclose);
_check(hpatch_TFileStreamInput_close(&patStream),kHPatch_error_pat_fclose);
_check(hpatch_TFileStreamOutput_close(&newStream),kHPatch_error_new_fclose);
return result;
}

View File

@@ -0,0 +1,44 @@
// hpatch.h
// import HDiffPatch, support patchData created by "hdiffz -SD -c-lzma2 oldfile newfile patchfile"
// Copyright 2021 housisong, All rights reserved
#ifndef HDIFFPATCH_PATCH_H
#define HDIFFPATCH_PATCH_H
# include <stdint.h> //for uint8_t
#include "HDiffPatch/libHDiffPatch/HPatch/patch_types.h" //for hpatch_singleCompressedDiffInfo
#ifdef __cplusplus
extern "C" {
#endif
//result
enum {
kHPatch_ok = 0,
kHPatch_error_malloc =-1,
kHPatch_error_info =-2,
kHPatch_error_compressType =-3,
kHPatch_error_patch =-4,
kHPatch_error_old_fopen =-5,
kHPatch_error_old_fread =-6,
kHPatch_error_old_fclose =-7,
kHPatch_error_pat_fopen =-8,
kHPatch_error_pat_fread =-9,
kHPatch_error_pat_fclose =-10,
kHPatch_error_new_fopen =-11,
kHPatch_error_new_fwrite =-12,
kHPatch_error_new_fclose =-13,
kHPatch_error_old_size =-14,
kHPatch_error_new_size =-15,
};
int hpatch_getInfo_by_mem(hpatch_singleCompressedDiffInfo* out_patinfo,
const uint8_t* pat,size_t patsize);
//patInfo can NULL
int hpatch_by_mem(const uint8_t* old,size_t oldsize, uint8_t* newBuf,size_t newsize,
const uint8_t* pat,size_t patsize,const hpatch_singleCompressedDiffInfo* patInfo);
int hpatch_by_file(const char* oldfile, const char* newfile, const char* patchfile);
#ifdef __cplusplus
}
#endif
#endif //HDIFFPATCH_PATCH_H

View File

@@ -0,0 +1,117 @@
#include "pushy.h"
#include "hpatch.h"
#include <napi/native_api.h>
#include <js_native_api.h>
#include <js_native_api_types.h>
#define _check(v,errInfo) do { \
if (!(v)) { \
_isError = hpatch_TRUE; \
_errInfo = errInfo; \
goto _clear; \
} \
} while(0)
napi_value HdiffPatch(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 2;
napi_value args[2];
hpatch_BOOL _isError = hpatch_FALSE;
const char* _errInfo = "";
// 获取参数
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
if (status != napi_ok || argc < 2) {
napi_throw_error(env, NULL, "Wrong number of arguments");
return NULL;
}
// 获取origin buffer
bool isTypedArray;
status = napi_is_typedarray(env, args[0], &isTypedArray);
if (status != napi_ok || !isTypedArray) {
napi_throw_error(env, NULL, "First argument must be a TypedArray");
return NULL;
}
uint8_t* originPtr;
size_t originLength;
status = napi_get_typedarray_info(env, args[0], NULL, &originLength,
(void**)&originPtr, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to get origin buffer");
return NULL;
}
// 获取patch buffer
status = napi_is_typedarray(env, args[1], &isTypedArray);
if (status != napi_ok || !isTypedArray) {
napi_throw_error(env, NULL, "Second argument must be a TypedArray");
return NULL;
}
uint8_t* patchPtr;
size_t patchLength;
status = napi_get_typedarray_info(env, args[1], NULL, &patchLength,
(void**)&patchPtr, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to get patch buffer");
return NULL;
}
// 执行patch操作
hpatch_singleCompressedDiffInfo patInfo;
_check(((originLength==0)||originPtr) && patchPtr && (patchLength>0), "Corrupt patch");
_check(kHPatch_ok==hpatch_getInfo_by_mem(&patInfo, patchPtr, patchLength), "Error info in hpatch");
_check(originLength==patInfo.oldDataSize, "Error oldDataSize in hpatch");
size_t newsize = (size_t)patInfo.newDataSize;
if (sizeof(size_t)!=sizeof(hpatch_StreamPos_t))
_check(newsize==patInfo.newDataSize, "Error newDataSize in hpatch");
// 创建结果buffer
napi_value resultBuffer;
uint8_t* outPtr;
void* data;
status = napi_create_arraybuffer(env, newsize, &data, &resultBuffer);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to create result buffer");
return NULL;
}
outPtr = (uint8_t*)data;
// 执行patch
_check(kHPatch_ok==hpatch_by_mem(originPtr, originLength, outPtr, newsize,
patchPtr, patchLength, &patInfo), "hpatch");
return resultBuffer;
_clear:
if (_isError) {
napi_throw_error(env, NULL, _errInfo);
return NULL;
}
return NULL;
}
// 模块初始化
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_value fn;
status = napi_create_function(env, NULL, 0, HdiffPatch, NULL, &fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to wrap native function");
return NULL;
}
status = napi_set_named_property(env, exports, "hdiffPatch", fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to populate exports");
return NULL;
}
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

View File

@@ -0,0 +1,8 @@
#ifndef _DOWNLOAD_TASK_H_
#define _DOWNLOAD_TASK_H_
#include <napi/native_api.h>
napi_value HdiffPatch(napi_env env, napi_callback_info info);
#endif // _DOWNLOAD_TASK_H_