Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
EricKotato committed Oct 26, 2020
0 parents commit bd7c299
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
116 changes: 116 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# TDLib Native Sciter Extension

Simple [Sciter](https://sciter.com) native extension for TDLib's JSON interface. Tested on Windows with Visual Studio 2019, possible (but haven't tested yet) to work on Linux.

## Prerequisites to build

* CMake
* Visual Studio (on Windows) or any other CMake supported compiler
* TDLib (as CMake package)
* Sciter SDK

## Build process

This instruction assumes the following:
* CMake is installed and added to your PATH environment variable
* Commands are executed in "x86 Native Tools Command Prompt for VS 2019"
* TDLib is installed in C:\TDLib
* Sciter SDK is unpacked at C:\sciter
* This repo is cloned to C:\TDLibNSE
* Build files will be located at C:\TDLibNSE\build

Generate build files (starting from C:\TDLibNSE):
```
mkdir build
cd build
cmake -A Win32 -DTd_DIR=C:\TDLib\lib\cmake\Td -DSCITER_INCLUDE_DIR=C:\sciter\include ..\TDLibNSE
```

Omit `-A Win32` to use your default system architecture.

Omit any of `-DTD_DIR` and `-DSCITER_INCLUDE_DIR` (or all) to search packages in default paths.


Build (starting from C:\TDLibNSE\build):
```
cmake --build . --clean-first --config Release
```

Omit `--clean-first` if you don't want to clean project before building.

Omit `--config Release` if you wan't to build default configuration `Debug`. You can also specify it explicitly: `--config Debug`.


## Using the library

On Windows you need to following files to be put near scapp.exe or to be found in system paths:
* TDLibNSE.dll (your built file, can be found in C:\TDLibNSE\build\Release or C:\TDLibNSE\build\Debug)
* tdjson.dll (TDLib with JSON interface, can be found at C:\TDLib\bin)
* libcrypto-1_1.dll (TDLib prereqisites)
* libssl-1_1.dll (TDLib prereqisites)
* zlib1.dll (TDLib prereqisites)

If you have installed TDLib prerequisites from vcpkg and installed vcpkg to C:\vcpkg, these files can be found at C:\vcpkg\installed\x86-windows\bin (or vcpkg\installed\x64-windows\bin for x64).

After that, you can include it in your Sciter HTML file as:
```
include library "TDLibNSE";
```
or
```
const Td = include library "TDLibNSE";
```
or even
```
namespace Td {
const tdlib = include library "TDLibNSE";
}
```

## API

API in TDLibNSE is pretty much same as TDLib new JSON interface.

* `int TDLib.createClient()` – creates new TDLib client and returns its ID.
* `void TDLib.send(int client_id, string request)` – sends request JSON encoded string to specified client_id.
* `string TDLib.receive(double timeout)` – polls for new updates from clients for timeout, returns JSON encoded string of response.
* `string TDLib.execute(string request)` – sends request JSON encoded string to TDLib, returns JSON encoded string of response.

## Example usage

```html
<html>
<head>
<title>TDLib Test</title>
<style>
#out {
font: monospace;
}
</style>
</head>
<script type="text/tiscript">
include library "TDLibNSE";
var testRequest = {
"@type": "getTextEntities",
"text": "@telegram /test_command https://telegram.org telegram.me",
"@extra": ["5", 7.0]
};
event click $(#test) {
if (TDLib) {
var j = JSON.parse(TDLib.execute(JSON.stringify(testRequest)));
$(#out).text = JSON.stringify(j);
} else {
$(#out).text = "Couldn't load TDLib.";
}
}
</script>
<body>
<button#test>Test TDLib</button>
<text#out />
</body>
</html>
```

Request structure was taken from TDLib's [Python example](https://github.com/tdlib/td/blob/master/example/python/tdjson_example.py).
21 changes: 21 additions & 0 deletions TDLibNSE/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.10)

project(TDLibNSE
VERSION 0.1
DESCRIPTION "TDLib as native Sciter extension.")

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

add_definitions(-DUNICODE -D_UNICODE)

find_package(Td 1.6.9 REQUIRED)
find_path(SCITER_INCLUDE_DIR NAMES sciter-x.h REQUIRED)

add_library(TDLibNSE SHARED tdlib_nse.cpp)
target_include_directories(TDLibNSE PUBLIC ${SCITER_INCLUDE_DIR})

target_link_libraries(TDLibNSE PRIVATE Td::TdJson)
18 changes: 18 additions & 0 deletions TDLibNSE/tdlib_nse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "tdlib_nse.h"

extern "C" {

#ifndef WINDOWS
__attribute__((visibility("default")))
#endif
BOOL SCAPI SciterLibraryInit(ISciterAPI *psapi, SCITER_VALUE *plibobject) {
#ifdef WINDOWS
#pragma comment(linker, "/EXPORT:" __FUNCTION__"=" __FUNCDNAME__)
#endif
_SAPI(psapi);
static sciter::om::hasset<tdlibnse::TDLib> tdlib_root = new tdlibnse::TDLib();
*plibobject = sciter::value::wrap_asset(tdlib_root);
return TRUE;
}

}
43 changes: 43 additions & 0 deletions TDLibNSE/tdlib_nse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include <td/telegram/td_json_client.h>
#include "sciter-x.h"

namespace tdlibnse {

class TDLib : public sciter::om::asset<TDLib> {
public:
TDLib() {}

int createClient() {
return td_create_client();
}

sciter::value send(int client_id, sciter::string request) {
td_send(client_id, aux::w2a(request.c_str()).c_str());
return sciter::value();
}

sciter::string receive(double timeout) {
const auto chars = aux::utf2w(td_receive(timeout));
return sciter::string(chars.c_str(), chars.length());
}

sciter::string execute(sciter::string request) {
const auto chars = aux::utf2w(td_execute(
aux::w2a(request.c_str()).c_str()
));
return sciter::string(chars.c_str(), chars.length());
}

SOM_PASSPORT_BEGIN(TDLib)
SOM_PASSPORT_FLAGS(SOM_EXTENDABLE_OBJECT)
SOM_FUNCS(
SOM_FUNC(createClient),
SOM_FUNC(send),
SOM_FUNC(receive),
SOM_FUNC(execute)
)
SOM_PASSPORT_END
};

} // namespace tdlibnse

0 comments on commit bd7c299

Please sign in to comment.