From 2df8a6e3315717d03094846caf41bf0f26a29666 Mon Sep 17 00:00:00 2001 From: Efox Date: Fri, 30 Jun 2023 15:31:21 -0300 Subject: [PATCH] v1.0.1 Improved docs. --- README.md | 204 +++++++++++++++++++++++++++++++++++++++++++-------- lib/main.js | 141 ++++++++++++++++++++--------------- package.json | 6 +- 3 files changed, 258 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 12df4d9..69c8a29 100644 --- a/README.md +++ b/README.md @@ -1,37 +1,179 @@ -# node-bright-sdk -[Koffi](https://koffi.dev) bindings to the [Bright SDK](https://bright-sdk.com) (formerly Luminati SDK) for [Node.js](https://nodejs.org), [Electron](https://electronjs.org) and [NW.js](https://nwjs.io) applications. Unofficial. Feel free to contribute. +# BrightSDK -# Install +[![License](https://img.shields.io/github/license/efoxbr/node-bright-sdk.svg)](https://opensource.org/licenses/LICENSE) -``` +A module that provides an interface for interacting with the Bright SDK (formerly Luminati SDK) on Node.js/NW.js/Electron applications. Unofficial, feel free to contribute. + + +## Installation + +```shell npm install efoxbr/node-bright-sdk ``` -# Sample usage -``` -var BrightSDK = require('node-bright-sdk'); -// var brd = new BrightSDK({dllPath: '/.../sdk.dll'}) -var brd = new BrightSDK() -brd.setAppName('MyApp'); -brd.setTosLink('https://./license/'); -brd.setLogoLink(path.resolve('default_icon.png')); -brd.setBgColor('#ffffffff'); -brd.setBtnColor('#ff1036a5'); -brd.setTxtColor('#ff000000'); -// brd.setNotPeerTxt(brd.NOT_PEER_TXT_I_DISAGREE); -brd.on('choice', () => { - var choice = brd.getChoice(); - switch(choice){ - case brd.CHOICE_NONE: - console.log('user choice cleared') - break - case brd.CHOICE_PEER: - console.log('user agree') - break - case brd.CHOICE_NOT_PEER: - console.log('user disagree') - break - } -} -brd.initUI('my-id') +## Usage + +```javascript +const BrightSDK = require('node-bright-sdk'); + +// Create a new instance of BrightSDK +const brightSDK = new BrightSDK(); + +// Initialize the BrightSDK +brightSDK.init(YOUR_UID); + +// Example usage +brightSDK.on('choice', choice => { + console.log('Choice has changed to '+ choice); +}); + +// Perform other operations with the BrightSDK +// ... + +// Uninitialize the BrightSDK +brightSDK.uninit(); ``` + +## API + +### `new BrightSDK(opts)` + +Creates a new instance of the BrightSDK. + +- `opts` (optional): An object containing options. + - `debug` (boolean): Enable debugging mode (default: `false`). + - `dllPath` (string): The path to the DLL file (default: determined based on the platform and architecture). + - `dir` (string): The directory to search for the DLL file (default: the directory of the main executable file). + +### checkSupported() + +Checks if the Bright SDK is supported. + +### clearChoice() + +Clears the current choice made by the user. + +### getChoice() + +Gets the current choice made by the user. + +### init(ID) + +Initializes the SDK. + +### initUI(ID) + +Initializes the UI component of the SDK. + +### isSupported2() + +Checks if the Bright SDK is supported. Returns an integer value. + +### isSupported() + +Checks if the Bright SDK is supported. Returns an integer value. + +### setSkipConsentOnInit(value) + +Sets whether to skip the consent screen during SDK initialization. + +- `value`: A boolean value indicating whether to skip consent on initialization. + +### setAppName(name) + +Sets the name of the application. + +- `name`: The name of the application. + +### setBgColor(color) + +Sets the background color of the SDK UI. + +- `color`: The background color in string format. + +### setBtnColor(color) + +Sets the button color of the SDK UI. + +- `color`: The button color in string format. + +### setDlgPos(position) + +Sets the position of the SDK dialog. + +- `position`: The position of the dialog. + +### setDlgPosType(positionType) + +Sets the type of position for the SDK dialog. + +- `positionType`: The type of position for the dialog. + +### setLogoLink(link) + +Sets the logo link of the SDK UI. + +- `link`: The logo link. + +### setNotPeerTxt(txt) + +Sets the text for non-peer users in the SDK UI. + +- `txt`: The text for non-peer users. + +### setTosLink(link) + +Sets the Terms of Service (TOS) link of the SDK UI. + +- `link`: The TOS link. + +### setTxtColor(color) + +Sets the text color of the SDK UI. + +- `color`: The text color in string format. + +### setTxtCulture(culture) + +Sets the text culture of the SDK UI. + +- `culture`: The text culture. + +### setBenefitTxt(txt) + +Sets the benefit text of the SDK UI. + +- `txt`: The benefit text. + +### uninit() + +Uninitializes the BrightSDK and frees any allocated resources. + +### Events + +#### Event: `choice` + +Emitted when the choice has changed. + +### Constants + +The following constants are available on the BrightSDK instance: + +- `CHOICE_NONE` (number): Represents the choice value for none (0). +- `CHOICE_PEER` (number): Represents the choice value for peer (1). +- `CHOICE_NOT_PEER` (number): Represents the choice value for not peer (2). +- `PEER_TXT_NO_ADS` (number): Represents the peer text value for no ads (0). +- `PEER_TXT_PREMIUM` (number): Represents the peer text value for premium (1). +- `PEER_TXT_FREE` (number): Represents the peer text value for free (2). +- `PEER_TXT_DONATE` (number): Represents the peer text value for donate (3). +- `PEER_TXT_I_AGREE` (number): Represents the peer text value for "I agree" (4). +- `NOT_PEER_TXT_ADS` (number): Represents the not peer text value for ads (0). +- `NOT_PEER_TXT_LIMITED` (number): Represents the not peer text value for limited (1). +- `NOT_PEER_TXT_PREMIUM` (number): Represents the not peer text value for premium (2). +- `NOT_PEER_TXT_NO_DONATE` (number): Represents the not peer text value for no donate (3). +- `NOT_PEER_TXT_NOT_AGREE` (number): Represents the not peer text value for not agree (4). +- `NOT_PEER_TXT_I_DISAGREE` (number): Represents the not peer text value for "I disagree" (5). +- `NOT_PEER_TXT_SUBSCRIBE` (number): Represents the not peer text value for subscribe (6). +- `NOT_PEER_TXT_BUY` (number): Represents the not peer text value for buy (7). +- `NOT_PEER_TXT_NO_THANK_YOU` (number): Represents the not peer text value for "No, thank you" (9). +- `DLG_POS_TYPE_CENTER_OWNER` (number): Represents the dialog position type as center owner (0). \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 57e77aa..d1a7013 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,55 +1,47 @@ -"use strict"; const EventEmitter = require('events') class BrightSDK extends EventEmitter { - constructor(opts={}){ - super() - this.CHOICE_NONE = 0 - this.CHOICE_PEER = 1 - this.CHOICE_NOT_PEER = 2 - this.PEER_TXT_NO_ADS = 0 - this.PEER_TXT_PREMIUM = 1 - this.PEER_TXT_FREE = 2 - this.PEER_TXT_DONATE = 3 - this.PEER_TXT_I_AGREE = 4 - this.NOT_PEER_TXT_ADS = 0 - this.NOT_PEER_TXT_LIMITED = 1 - this.NOT_PEER_TXT_PREMIUM = 2 - this.NOT_PEER_TXT_NO_DONATE = 3 - this.NOT_PEER_TXT_NOT_AGREE = 4 - this.NOT_PEER_TXT_I_DISAGREE = 5 - this.NOT_PEER_TXT_SUBSCRIBE = 6 - this.NOT_PEER_TXT_BUY = 7 - this.NOT_PEER_TXT_NO_THANK_YOU = 9 - this.DLG_POS_TYPE_CENTER_OWNER = 0 - this.DLG_POS_TYPE_CENTER_SCREEN = 1 - this.DLG_POS_TYPE_MANUAL = 2 - this.dllPath = opts.dllPath || '' - this.arch = process.arch === 'ia32' ? 32 : 64 - this.console = opts.console ? opts.console : {log: () => {}, error: () => {}} - if(!this.dllPath || typeof(this.dllPath) !== 'string'){ - const path = require('path') - this.dllPath = path.join(opts.dir || process.cwd(), './lum_sdk' + this.arch +'.dll') - } - this.ffi = require('koffi') - if(!this.ffi){ - throw '[bright sdk] Koffi module unavailable.' + constructor(opts = {}) { + super() + this.CHOICE_NONE = 0 + this.CHOICE_PEER = 1 + this.CHOICE_NOT_PEER = 2 + this.PEER_TXT_NO_ADS = 0 + this.PEER_TXT_PREMIUM = 1 + this.PEER_TXT_FREE = 2 + this.PEER_TXT_DONATE = 3 + this.PEER_TXT_I_AGREE = 4 + this.NOT_PEER_TXT_ADS = 0 + this.NOT_PEER_TXT_LIMITED = 1 + this.NOT_PEER_TXT_PREMIUM = 2 + this.NOT_PEER_TXT_NO_DONATE = 3 + this.NOT_PEER_TXT_NOT_AGREE = 4 + this.NOT_PEER_TXT_I_DISAGREE = 5 + this.NOT_PEER_TXT_SUBSCRIBE = 6 + this.NOT_PEER_TXT_BUY = 7 + this.NOT_PEER_TXT_NO_THANK_YOU = 9 + this.DLG_POS_TYPE_CENTER_OWNER = 0 + this.DLG_POS_TYPE_CENTER_SCREEN = 1 + this.DLG_POS_TYPE_MANUAL = 2 + this.debug = !!opts.debug + this.dllPath = opts.dllPath || '' + this.arch = process.arch === 'ia32' ? 32 : 64 + if (!this.dllPath || typeof this.dllPath !== 'string') { + const path = require('path') + this.dllPath = path.join(opts.dir || path.dirname(process.execPath), 'lum_sdk' + this.arch + '.dll') + } + this.dllPath = this.dllPath.split('\\').join('/') + if(!opts.skipPreparing){ + this.prepare() } - this.console.log('[bright sdk] Loading DLL', this.dllPath) - this.lib = this.ffi.load(this.dllPath) - let table - const cbaddr = this.arch == 64 ? '?brd_sdk_set_choice_change_cb@@YAXP6AXH@Z@Z' : '_lum_sdk_set_choice_change_cb_c@4'; - if(!global.brd_sdk_choice_change_t){ - global.brd_sdk_choice_change_t = this.ffi.callback('void *GetCallback(int)') - } - const callback = this.ffi.register(() => { - console.error('CHOICED') - this.emit('choice') - }, this.ffi.pointer(global.brd_sdk_choice_change_t)) - const setChoiceChangeCallback = this.lib.func(cbaddr, 'void', ['GetCallback *cb']) - setChoiceChangeCallback(callback) - if(this.arch == 64){ - table = { + } + prepare(){ + this.ffi = require('ffi-napi') + this.ref = require('ref-napi') + + this.intPtr = this.ref.refType('int') + if(this.arch == 64){ + this.table = { 'checkSupported': [ 'lum_sdk_check_supported', ['void', []] @@ -76,11 +68,11 @@ class BrightSDK extends EventEmitter { ], 'isSupported2': [ 'lum_sdk_is_supported_c', - ['int', ['int']] + ['int', [this.intPtr]] ], 'isSupported': [ 'lum_sdk_is_supported_c', - ['int', ['int']] + ['int', [this.intPtr]] ], 'setSkipConsentOnInit': [ 'brd_sdk_set_skip_consent_on_init_c', @@ -133,10 +125,14 @@ class BrightSDK extends EventEmitter { 'uninit': [ 'lum_sdk_uninit_c', ['void', []] - ] + ], + 'setChoiceChangeCallback': [ + 'lum_sdk_set_choice_change_cb_c', + ['void', ['pointer']] + ] } } else { - table = { + this.table = { 'checkSupported': [ '_lum_sdk_check_supported@0', ['void', []] @@ -163,11 +159,11 @@ class BrightSDK extends EventEmitter { ], 'isSupported2': [ '_lum_sdk_is_supported2_c@8', - ['int', ['int']] + ['int', [this.intPtr]] ], 'isSupported': [ '_lum_sdk_is_supported_c@0', - ['int', ['int']] + ['int', [this.intPtr]] ], 'setSkipConsentOnInit': [ '_brd_sdk_set_skip_consent_on_init_c@4', @@ -220,13 +216,40 @@ class BrightSDK extends EventEmitter { 'uninit': [ '_lum_sdk_uninit_c@0', ['void', []] - ] + ], + 'setChoiceChangeCallback': [ + '_lum_sdk_set_choice_change_cb_c@4', + ['void', ['pointer']] + ] } } - Object.keys(table).forEach(k => { - this[k] = this.lib.func(table[k][0], table[k][1][0], table[k][1][1]) - }) + const bindings = {}, start = this.time(), ks = Object.keys(this.table) + ks.forEach((k) => { + bindings[this.table[k][0]] = this.table[k][1] + }) + this.debug && console.log('[bright sdk] Loading DLL: '+ this.dllPath) + const lib = new this.ffi.Library(this.dllPath, bindings) + this.loadTime = (this.time() - start) + this.debug && console.log('[bright sdk] Lum library loaded in ' + this.loadTime + ' secs.') + ks.forEach((k) => { + this[k] = lib[this.table[k][0]] + }) + process.nextTick(() => this.setupChoiceChangeCallback()) + } + + time(){ + return ((new Date()).getTime() / 1000) } + + setupChoiceChangeCallback() { + if (!global.brd_sdk_choice_change_t) { + global.brd_sdk_choice_change_t = new this.ffi.Callback('void', ['int'], () => { + console.error('CHOICED') + this.emit('choice') + }) + this.setChoiceChangeCallback(global.brd_sdk_choice_change_t) + } + } } -module.exports = BrightSDK +module.exports = BrightSDK \ No newline at end of file diff --git a/package.json b/package.json index e359638..b449e6c 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "url": "https://github.com/efoxbr" }, "dependencies": { - "koffi": "^2.3.9" + "ffi-napi": "^3.1.0" }, - "description": "FFI bindings to the Bright SDK (formerly Luminati SDK) for NW.js applications.", + "description": "Interface for interacting with the Bright SDK (formerly Luminati SDK) on Node.js/NW.js/Electron applications.", "homepage": "https://github.com/efoxbr/node-bright-sdk/", "license": "MIT", "main": "./lib/main.js", "name": "node-bright-sdk", - "version": "1.0.0" + "version": "1.0.1" }