From 692055596a4808657395b62b5a3b49d1d0553095 Mon Sep 17 00:00:00 2001 From: Sreyas Date: Tue, 14 May 2019 04:27:14 +0000 Subject: [PATCH] update to latest sdk --- .gitignore | 6 + Cargo.lock | 222 +++++++++++++++++++++++++---------- Cargo.toml | 7 +- include/tgconnector.inc | 6 +- makefile | 17 ++- pawn-tests/test.pwn | 8 +- pawn.json | 19 ++- src/api.rs | 58 +++++----- src/callbacks.rs | 15 +-- src/encode.rs | 15 ++- src/internals.rs | 9 +- src/lib.rs | 61 +++++++++- src/macros.rs | 35 ++---- src/natives.rs | 250 ++++++++++++++++++++++++---------------- src/plugin.rs | 203 +++----------------------------- test-server/samp.json | 9 -- 16 files changed, 484 insertions(+), 456 deletions(-) delete mode 100644 test-server/samp.json diff --git a/.gitignore b/.gitignore index 8518da4..7758876 100644 --- a/.gitignore +++ b/.gitignore @@ -9,8 +9,14 @@ *dll *so *zip +*log /dependencies/ +/dependencies/ +/gamemodes/ +/plugins/ +/scriptfiles/ +/filterscripts/ announce samp03svr diff --git a/Cargo.lock b/Cargo.lock index 00ebfab..ffc9ac4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "MacTypes-sys" version = "2.1.0" @@ -7,31 +9,14 @@ dependencies = [ ] [[package]] -name = "autocfg" -version = "0.1.2" +name = "approx" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "backtrace" -version = "0.3.13" +name = "autocfg" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", - "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "backtrace-sys" -version = "0.1.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "bitflags" @@ -48,6 +33,16 @@ name = "cfg-if" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cgmath" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -56,6 +51,15 @@ dependencies = [ "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "colored" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "core-foundation" version = "0.5.1" @@ -73,6 +77,15 @@ dependencies = [ "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crossbeam-utils" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "encoding" version = "0.2.33" @@ -131,23 +144,11 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "failure" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", - "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "failure_derive" -version = "0.1.5" +name = "fern" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", - "synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -228,6 +229,27 @@ dependencies = [ "tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-traits" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num_cpus" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "openssl" version = "0.10.16" @@ -278,6 +300,18 @@ dependencies = [ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-cprng 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rand" version = "0.6.5" @@ -396,8 +430,8 @@ dependencies = [ ] [[package]] -name = "rustc-demangle" -version = "0.1.13" +name = "rgb" +version = "0.8.13" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -413,15 +447,54 @@ name = "ryu" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "samp" +version = "0.1.2" +source = "git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx#725c3074800c2f4c2e9b638f3c1c6e1f76dda7f6" +dependencies = [ + "fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "samp-async 0.1.0 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", + "samp-codegen 0.1.1 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", + "samp-runtime 0.1.0 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", + "samp-sdk 0.9.2 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", +] + +[[package]] +name = "samp-async" +version = "0.1.0" +source = "git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx#725c3074800c2f4c2e9b638f3c1c6e1f76dda7f6" +dependencies = [ + "crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "samp-runtime 0.1.0 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", + "samp-sdk 0.9.2 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", +] + +[[package]] +name = "samp-codegen" +version = "0.1.1" +source = "git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx#725c3074800c2f4c2e9b638f3c1c6e1f76dda7f6" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "samp-runtime" +version = "0.1.0" +source = "git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx#725c3074800c2f4c2e9b638f3c1c6e1f76dda7f6" +dependencies = [ + "samp-sdk 0.9.2 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", +] + [[package]] name = "samp-sdk" -version = "0.8.4" -source = "git+https://github.com/ZOTTCE/samp-sdk#2c58e8a3f9a504923eab3de6f51b036dd6032f4b" +version = "0.9.2" +source = "git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx#725c3074800c2f4c2e9b638f3c1c6e1f76dda7f6" dependencies = [ - "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", - "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -502,17 +575,6 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "synstructure" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tempfile" version = "3.0.5" @@ -528,14 +590,25 @@ dependencies = [ [[package]] name = "tgconnector" -version = "0.1.0" +version = "0.1.1" dependencies = [ "encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "minihttp 0.1.9 (git+https://github.com/Sreyas-Sreelal/minihttp)", - "samp-sdk 0.8.4 (git+https://github.com/ZOTTCE/samp-sdk)", + "samp 0.1.2 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)", "serde 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", + "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "threadpool" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -567,17 +640,30 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winconsole" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cgmath 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rgb 0.8.13 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] "checksum MacTypes-sys 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eaf9f0d0b1cc33a4d2aee14fb4b2eac03462ef4db29c8ac4057327d8a71ad86f" +"checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94" "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" -"checksum backtrace 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b493b66e03090ebc4343eb02f94ff944e0cbc9ac6571491d170ba026741eb5" -"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum cgmath 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "64a4b57c8f4e3a2e9ac07e0f6abc9c24b6fc9e1b54c3478cfb598f3d0023e51c" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6cdb90b60f2927f8d76139c72dbde7e10c3a2bc47c8594c9c7a66529f2687c03" "checksum core-foundation 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "286e0b41c3a20da26536c6000a280585d519fd07b3956b43aed8a79e9edce980" "checksum core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "716c271e8613ace48344f723b60b900a93150271e5be206212d052bbc0883efa" +"checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum encoding 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" "checksum encoding-index-japanese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" "checksum encoding-index-korean 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" @@ -585,8 +671,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" "checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" "checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" -"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" -"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" +"checksum fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "29d26fa0f4d433d1956746e66ec10d6bf4d6c8b93cd39965cceea7f7cc78c7dd" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum fuchsia-cprng 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "81f7f8eb465745ea9b02e2704612a9946a59fa40572086c6fd49d6ddcf30bf31" @@ -598,12 +683,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum minihttpse 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8e50e8cee436b4318ec759930d6ea5f839d14dab94e81b6fba37d492d07ebf55" "checksum miniurl 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1346e28b38a4554e6fa7f8fc49874cac3f9ecb781408bac3274fb948fee303e0" "checksum native-tls 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ff8e08de0070bbf4c31f452ea2a70db092f36f6f2e4d897adf5674477d488fb2" +"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" +"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" +"checksum num_cpus 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a23f0ed30a54abaa0c7e83b1d2d87ada7c3c23078d1d87815af3e3b6385fbba" "checksum openssl 0.10.16 (registry+https://github.com/rust-lang/crates.io-index)" = "ec7bd7ca4cce6dbdc77e7c1230682740d307d1218a87fb0349a571272be749f9" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" "checksum openssl-sys 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)" = "1bb974e77de925ef426b6bc82fce15fd45bdcbeb5728bffcfc7cdeeb7ce1c2d6" "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" +"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" "checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" @@ -617,10 +706,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" "checksum remove_dir_all 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3488ba1b9a2084d38645c4c08276a1752dcbf2c7130d74f1569681ad5d2799c5" -"checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619" +"checksum rgb 0.8.13 (registry+https://github.com/rust-lang/crates.io-index)" = "4f089652ca87f5a82a62935ec6172a534066c7b97be003cc8f702ee9a7a59c92" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" -"checksum samp-sdk 0.8.4 (git+https://github.com/ZOTTCE/samp-sdk)" = "" +"checksum samp 0.1.2 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)" = "" +"checksum samp-async 0.1.0 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)" = "" +"checksum samp-codegen 0.1.1 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)" = "" +"checksum samp-runtime 0.1.0 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)" = "" +"checksum samp-sdk 0.9.2 (git+https://github.com/ZOTTCE/samp-rs/?branch=async-amx)" = "" "checksum schannel 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "0e1a231dc10abf6749cfa5d7767f25888d484201accbd919b66ab5413c502d56" "checksum security-framework 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfab8dda0e7a327c696d893df9ffa19cadc4bd195797997f5223cf5831beaf05" "checksum security-framework-sys 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3d6696852716b589dff9e886ff83778bb635150168e83afa8ac6b8a78cb82abc" @@ -630,10 +723,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.86 (registry+https://github.com/rust-lang/crates.io-index)" = "51eac71e1171246f337655221882f2f55a9c2e1d8ddc6990cee766509f15b702" "checksum serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "27dce848e7467aa0e2fcaf0a413641499c0b745452aaca1194d24dedde9e13c9" "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" -"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "7e91405c14320e5c79b3d148e1c86f40749a36e490642202a31689cb1a3452b2" +"checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum vcpkg 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "def296d3eb3b12371b2c7d0e83bfe1403e4db2d7a0bba324a12b21c4ee13143d" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ef84b96d10db72dd980056666d7f1e7663ce93d82fa33b63e71c966f4cf5032" diff --git a/Cargo.toml b/Cargo.toml index 11780b0..e25ae4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tgconnector" -version = "0.1.0" +version = "0.1.1" authors = ["Sreyas-Sreelal"] edition = "2018" @@ -13,7 +13,10 @@ lto = true panic = "abort" [dependencies] -samp-sdk = {git = "https://github.com/ZOTTCE/samp-sdk"} +samp = {git="https://github.com/ZOTTCE/samp-rs/",branch="async-amx",features = ["async"]} +log = "0.4.6" +fern = "0.5.7" +threadpool = "1.7.1" minihttp = {git = "https://github.com/Sreyas-Sreelal/minihttp"} serde_json = "1.0.38" serde = "1.0.86" diff --git a/include/tgconnector.inc b/include/tgconnector.inc index e618f3a..504ff09 100644 --- a/include/tgconnector.inc +++ b/include/tgconnector.inc @@ -7,7 +7,7 @@ #endif #define _tgconnector_included -#define TGCONNECTOR_VERSION 10 +#define TGCONNECTOR_VERSION 11 #define INVALID_MESSAGE_ID (TGMessage:-1) #define INVALID_BOT_ID (TGBot:-1) @@ -29,8 +29,8 @@ enum TGUserStatus { }; //basic -native TGBot:TGConnect(const token[]); -native TGBot:TGConnectFromEnv(const variable[]); +native TGBot:TGConnect(const token[],thread_limit=3); +native TGBot:TGConnectFromEnv(const variable[],thread_limit=3); native TGSendMessage(TGBot:bot,const TGChatId:chatid[],const text[],TGMessage:reply_id=INVALID_MESSAGE_ID,TGParseMode:parse_mode=TGParseMode:-1,callback[]=""); native TGDeleteMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid); native TGEditMessage(TGBot:bot,TGChatId:chatid[],TGMessage:messageid,text[],TGParseMode:parse_mode=TGParseMode:-1); diff --git a/makefile b/makefile index 740824c..38306cc 100644 --- a/makefile +++ b/makefile @@ -2,15 +2,15 @@ ifdef OS TOOLCHAIN = +stable-i686-pc-windows-msvc BINARYNAME = tgconnector.dll OUPUTNAME = tgconnector.dll - CP_RELEASE = cp .\target\release\$(BINARYNAME) .\test-server\plugins\$(OUPUTNAME) - CP_DEBUG = cp .\target\debug\$(BINARYNAME) .\test-server\plugins\$(OUPUTNAME) + CP_RELEASE = cp .\target\release\$(BINARYNAME) .\plugins\$(OUPUTNAME) + CP_DEBUG = cp .\target\debug\$(BINARYNAME) .\plugins\$(OUPUTNAME) else ifeq ($(shell uname), Linux) TOOLCHAIN = +stable-i686-unknown-linux-gnu BINARYNAME = libtgconnector.so OUPUTNAME = tgconnector.so - CP_RELEASE = cp target/release/$(BINARYNAME) test-server/plugins/$(OUPUTNAME) - CP_DEBUG = cp target/debug/$(BINARYNAME) test-server/plugins/$(OUPUTNAME) + CP_RELEASE = cp target/release/$(BINARYNAME) plugins/$(OUPUTNAME) + CP_DEBUG = cp target/debug/$(BINARYNAME) plugins/$(OUPUTNAME) endif endif @@ -23,18 +23,15 @@ debug: $(CP_DEBUG) setup: - cd test-server && mkdir plugins - cd test-server && mkdir gamemodes sampctl package ensure sampctl package build - cd test-server && sampctl server ensure ensure: sampctl package ensure - + run: sampctl package build - cd test-server && sampctl server run - + sampctl package run + clean: cargo clean diff --git a/pawn-tests/test.pwn b/pawn-tests/test.pwn index f3a0da3..d62d13b 100644 --- a/pawn-tests/test.pwn +++ b/pawn-tests/test.pwn @@ -1,7 +1,7 @@ #define RUN_TESTS #include -#include +#include #include "../include/tgconnector.inc" @@ -22,11 +22,11 @@ Test:TestValidToken() { ASSERT(g_bot != INVALID_BOT_ID); } Test:TGGetBotUserId() { - new + new TGUser:userid = TGGetBotUserId(g_bot), name[34], username[32]; - + TGGetDisplayNameFromId(g_bot,userid,TGChatId:"562896556",name); new bool:name_check = !strcmp("samp",name); ASSERT(name_check); @@ -34,7 +34,7 @@ Test:TGGetBotUserId() { TGGetUserNameFromId(g_bot,userid,TGChatId:"562896556",username); new bool:username_check = !strcmp("samptg_bot",username); ASSERT(username_check); - + new TGUserStatus:status = TGGetUserChatStatus(g_bot,userid,TGChatId:"562896556"); ASSERT(status == TG_MEMBER); diff --git a/pawn.json b/pawn.json index c278f0e..c05a631 100644 --- a/pawn.json +++ b/pawn.json @@ -2,19 +2,28 @@ "user": "Sreyas-Sreelal", "repo": "tgconnector", "entry": "pawn-tests/test.pwn", - "output": "test-server/gamemodes/test.amx", + "output": "gamemodes/test.amx", "dependencies": ["sampctl/pawn-stdlib"], - "dev_dependencies": ["pawn-lang/YSI-Includes"], + "dev_dependencies": ["pawn-lang/YSI-Includes@5.x"], "include_path": "include", + "local": true, "builds": [ { "name": "test", "includes": ["./include"] } ], - "runtime": { - "plugins": ["Sreyas-Sreelal/tgconnector"] - }, + "runtimes": [ + { + "rcon_password": "|%GuRd324$\u0026|", + "port": 7777, + "plugins":["tgconnector"], + "gamemodes":["test"], + "hostname": "test", + "maxplayers": 32, + "mode" :"y_testing" + } + ], "resources": [ { "name": "tgconnector-linux-x86.zip", diff --git a/src/api.rs b/src/api.rs index a1f598c..9a2b4f8 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,10 +1,11 @@ use crate::http::{HttpMethod, HttpRequest}; use crate::methods::*; use crate::types::*; -use samp_sdk::log; +use log::error; use serde_json::{from_str, to_string}; use std::collections::VecDeque; use std::sync::mpsc::{channel, Receiver, Sender}; +use threadpool::ThreadPool; pub struct BOT { pub api_request_link: String, @@ -13,10 +14,11 @@ pub struct BOT { pub update_sender: Option>, pub send_message_reciever: Option>, pub send_message_sender: Option>, + pub pool: ThreadPool, } impl BOT { - pub fn new(bot_token: String) -> Self { + pub fn new(bot_token: String, thread_count: i32) -> Self { let (update_sender, update_reciever) = channel(); let (send_message_sender, send_message_reciever) = channel(); @@ -27,6 +29,7 @@ impl BOT { update_sender: Some(update_sender), send_message_reciever: Some(send_message_reciever), send_message_sender: Some(send_message_sender), + pool: ThreadPool::new(thread_count as usize), } } @@ -46,12 +49,12 @@ impl BOT { self.get_updates(); true } else { - log!("**[TGConnector] Error bot couldn't connect.{:?}", response); + error!("Bot couldn't connect.{:?}", response); false } } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); false } } @@ -63,7 +66,7 @@ impl BOT { let mut getupdate = GetUpdates { offset: -2 }; - std::thread::spawn(move || loop { + self.pool.execute(move || loop { let request = HttpRequest { url: format!("{}/getUpdates", api_link), method: HttpMethod::Post, @@ -96,7 +99,7 @@ impl BOT { } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); continue; } } @@ -107,7 +110,7 @@ impl BOT { let send_message_move = self.send_message_sender.clone(); let api_link = self.api_request_link.clone(); - std::thread::spawn(move || { + self.pool.execute(move || { let request = HttpRequest { url: format!("{}/sendmessage", api_link), method: HttpMethod::Post, @@ -118,7 +121,7 @@ impl BOT { Ok(response) => { let response: APIResponse = from_str(&response).unwrap(); if !response.ok { - log!("**[TGConnector] Error Couldn't send message.{:?}", response); + error!("Couldn't send message.{:?}", response); } else if callback != None { let sender = send_message_move.as_ref().unwrap(); let send_data = (response.body.unwrap(), callback.unwrap()); @@ -127,7 +130,7 @@ impl BOT { } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); } } }); @@ -136,7 +139,7 @@ impl BOT { pub fn delete_message(&self, delete_message_obj: DeleteMessage) { let api_link = self.api_request_link.clone(); - std::thread::spawn(move || { + self.pool.execute(move || { let request = HttpRequest { url: format!("{}/deletemessage", api_link), method: HttpMethod::Post, @@ -147,16 +150,15 @@ impl BOT { Ok(response) => { let response: APIResponse = from_str(&response).unwrap(); if !response.ok { - log!( - "**[TGConnector] Error Message {:?} couldn't delete. {:?}", - delete_message_obj, - response + error!( + "Message {:?} couldn't delete. {:?}", + delete_message_obj, response ); } } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); } } }); @@ -165,7 +167,7 @@ impl BOT { pub fn edit_message(&self, edit_message_obj: EditMessageText) { let api_link = self.api_request_link.clone(); - std::thread::spawn(move || { + self.pool.execute(move || { let request = HttpRequest { url: format!("{}/editmessagetext", api_link), method: HttpMethod::Post, @@ -176,16 +178,15 @@ impl BOT { Ok(response) => { let response: APIResponse = from_str(&response).unwrap(); if !response.ok { - log!( - "**[TGConnector] Error Message {:?} couldn't edit {:?}", - edit_message_obj, - response + error!( + "Message {:?} couldn't edit {:?}", + edit_message_obj, response ); } } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); } } }); @@ -204,13 +205,13 @@ impl BOT { if response.ok { response.body } else { - log!("**[TGConnector] Error get_chat_member.{:?}", response); + error!("get_chat_member.{:?}", response); None } } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); None } } @@ -229,16 +230,13 @@ impl BOT { if response.ok { response.body } else { - log!( - "**[TGConnector] Error get_chat_members_count.{:?}", - response - ); + error!("get_chat_members_count.{:?}", response); None } } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); None } } @@ -257,13 +255,13 @@ impl BOT { if response.ok { response.body } else { - log!("**[TGConnector] Error get_chat.{:?}", response); + error!("get_chat.{:?}", response); None } } Err(err) => { - log!("{:?}", err); + error!("{:?}", err); None } } diff --git a/src/callbacks.rs b/src/callbacks.rs index c714d23..e654aa4 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -1,22 +1,23 @@ -use samp_sdk::amx::AMX; -use samp_sdk::{exec, log}; +use log::error; +use samp::amx::AmxIdent; +use samp::exec_public; -pub fn on_tg_message(amx_list: &[usize], botid: usize, fromid: i32, message_id: i32) { +pub fn on_tg_message(amx_list: &[AmxIdent], botid: usize, fromid: i32, message_id: i32) { execute!(amx_list,"OnTGMessage",botid;fromid,message_id); } -pub fn on_tg_send_message(amx_list: &[usize], name: String, botid: usize, message_id: i32) { +pub fn on_tg_send_message(amx_list: &[AmxIdent], name: &str, botid: usize, message_id: i32) { execute!(amx_list,name,botid;message_id); } -pub fn on_tg_channel_post(amx_list: &[usize], botid: usize, message_id: i32) { +pub fn on_tg_channel_post(amx_list: &[AmxIdent], botid: usize, message_id: i32) { execute!(amx_list,"OnTGChannelPost",botid;message_id); } -pub fn on_tg_user_joined(amx_list: &[usize], botid: usize, userid: i32) { +pub fn on_tg_user_joined(amx_list: &[AmxIdent], botid: usize, userid: i32) { execute!(amx_list,"OnTGUserJoined",botid;userid); } -pub fn on_tg_user_left(amx_list: &[usize], botid: usize, userid: i32) { +pub fn on_tg_user_left(amx_list: &[AmxIdent], botid: usize, userid: i32) { execute!(amx_list,"OnTGUserLeft",botid;userid); } diff --git a/src/encode.rs b/src/encode.rs index e5a799a..f90e27b 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,10 +1,13 @@ use encoding::all::WINDOWS_1251; use encoding::{EncoderTrap, Encoding}; -use samp_sdk::amx::{AmxError, AmxResult}; +use std::str::from_utf8; -//Just a modification to encode function to replace the chararcter on error instead of giving up entire thing -pub fn encode_replace(string: &str) -> AmxResult> { - WINDOWS_1251 - .encode(string, EncoderTrap::Replace) - .map_err(|_| AmxError::Format) +pub fn encode_replace(string: &str) -> Result { + match WINDOWS_1251.encode(string, EncoderTrap::Replace) { + Ok(bytes) => match from_utf8(&bytes) { + Ok(data) => Ok(String::from(data)), + Err(_) => Err(()), + }, + Err(_) => Err(()), + } } diff --git a/src/internals.rs b/src/internals.rs index 3d13efb..60d42a2 100644 --- a/src/internals.rs +++ b/src/internals.rs @@ -1,8 +1,7 @@ use crate::api::BOT; use crate::callbacks; use crate::types::*; -use samp_sdk::amx::AmxResult; -use samp_sdk::types::Cell; +use samp::prelude::*; use std::collections::LinkedList; pub fn clear_caches(cache: &mut LinkedList) { @@ -133,17 +132,17 @@ pub fn on_send_message_process(plugin: &mut super::TgConnector) { if message.text != None { plugin.telegram_messages.push_front(message.text.unwrap()); plugin.telegram_chatid.push_front(message.chat.id); - callbacks::on_tg_send_message(&plugin.amx_list, callback, *id, message.message_id); + callbacks::on_tg_send_message(&plugin.amx_list, &callback, *id, message.message_id); } } } } -pub fn create_bot(plugin: &mut super::TgConnector, mut api: BOT) -> AmxResult { +pub fn create_bot(plugin: &mut super::TgConnector, mut api: BOT) -> AmxResult { if api.connect() { plugin.bots.insert(plugin.bot_context_id, api); plugin.bot_context_id += 1; - Ok(plugin.bot_context_id as Cell - 1) + Ok(plugin.bot_context_id as i32 - 1) } else { Ok(-1) } diff --git a/src/lib.rs b/src/lib.rs index e8c94e8..96dc358 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ #[macro_use] mod macros; -use plugin::TgConnector; mod api; mod callbacks; mod encode; @@ -11,6 +10,62 @@ mod natives; mod plugin; mod types; -use samp_sdk::new_plugin; +use crate::plugin::TgConnector; +use samp::initialize_plugin; -new_plugin!(TgConnector with process_tick); +use std::collections::{HashMap, LinkedList}; +initialize_plugin!( + natives: [ + TgConnector::bot_connect, + TgConnector::bot_connect_from_env, + TgConnector::bot_send_message, + TgConnector::bot_delete_message, + TgConnector::bot_edit_message, + TgConnector::get_bot_user_id, + TgConnector::cache_get_message, + TgConnector::cache_get_username, + TgConnector::cache_get_user_first_name, + TgConnector::cache_get_user_last_name, + TgConnector::cache_get_chatid, + TgConnector::cache_get_chatname, + TgConnector::cache_get_chattype, + TgConnector::get_user_status, + TgConnector::get_username_from_id, + TgConnector::get_display_name_from_id, + TgConnector::get_chat_members_count, + TgConnector::get_chat_title, + TgConnector::get_chat_description + ], + { + let samp_logger = samp::plugin::logger() + .level(log::LevelFilter::Info); + + let log_file = fern::log_file("TgConnector.log").expect("Cannot create log file!"); + + let trace_level = fern::Dispatch::new() + .level(log::LevelFilter::Trace) + .chain(log_file); + + let _ = fern::Dispatch::new() + .format(|callback, message, record| { + callback.finish(format_args!("[TgConnector] [{}]: {}", record.level().to_string().to_lowercase(), message)) + }) + .chain(samp_logger) + .chain(trace_level) + .apply(); + + TgConnector { + plugin_version: 11, + amx_list: Vec::new(), + bots: HashMap::new(), + bot_context_id: 0, + telegram_messages: LinkedList::new(), + telegram_username: LinkedList::new(), + telegram_chatname: LinkedList::new(), + telegram_chatid: LinkedList::new(), + telegram_firstname: LinkedList::new(), + telegram_lastname: LinkedList::new(), + telegram_chattype: LinkedList::new(), + } + } +); diff --git a/src/macros.rs b/src/macros.rs index c5a184c..0df6c9e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,48 +2,29 @@ macro_rules! execute { ($amx_list:ident,$name:tt,$botid:ident;$($args:tt)*) => { let mut executed: bool = false; for amx in $amx_list { - let amx = cast_amx!(amx); - let botid: usize = $botid; - match exec_callback!(amx,$name;botid,$($args)*) { - Ok(_) => { - executed = true; - } - Err(_err) => { - continue; - } + if let Some(amx) = samp::amx::get(*amx) { + let botid: usize = $botid; + let _= exec_public!(amx,$name,botid,$($args)*); + executed = true; } } if !executed { - log!("**[TGConnector] Error executing callback {}",$name); + error!("**[TGConnector] Error executing callback {}",$name); } }; } -macro_rules! exec_callback { - ($amx:ident, $name:tt; $($args:tt)*) => { - { - $amx.find_public(&$name) - .and_then(|index| exec!($amx, index; $($args)*)) - } - }; -} - -macro_rules! cast_amx { - ($amx:ident) => { - AMX::new(*$amx as *mut _) - }; -} - macro_rules! cache_get { ($cache_list:ident,$dest:ident,$size:ident) => { if $cache_list.front() != None { match encode_replace(&$cache_list.front().unwrap()) { Ok(encoded) => { - set_string!(encoded, $dest, $size); + let mut dest = $dest.into_sized_buffer($size); + let _ = samp::cell::string::put_in_buffer(&mut dest, &encoded); Ok(1) } Err(err) => { - log!( + error!( "**[TGConnector] Failed encoding {:?} \n {:?}", $cache_list.front().unwrap(), err diff --git a/src/natives.rs b/src/natives.rs index be913cf..39fbfef 100644 --- a/src/natives.rs +++ b/src/natives.rs @@ -2,50 +2,61 @@ use crate::api::BOT; use crate::encode::encode_replace; use crate::internals::create_bot; use crate::methods::*; -use samp_sdk::amx::AmxResult; -use samp_sdk::amx::AMX; -use samp_sdk::types::Cell; -use samp_sdk::{log, set_string}; +use log::error; +use samp::native; +use samp::prelude::*; impl super::TgConnector { - pub fn bot_connect(&mut self, _amx: &AMX, token: String) -> AmxResult { - let api = BOT::new(token); + #[native(name = "TGConnect")] + pub fn bot_connect( + &mut self, + _amx: &Amx, + token: AmxString, + thread_count: i32, + ) -> AmxResult { + let api = BOT::new(token.to_string(), thread_count); create_bot(self, api) } - pub fn bot_connect_from_env(&mut self, _amx: &AMX, variable: String) -> AmxResult { + #[native(name = "TGConnectFromEnv")] + pub fn bot_connect_from_env( + &mut self, + _amx: &Amx, + variable: AmxString, + thread_count: i32, + ) -> AmxResult { + let variable = variable.to_string(); let token = std::env::var_os(&variable); if token == None { - log!( - "**[TGConnector] Error environment variable {:?} is not set", - variable - ); + error!("Environment variable {:?} is not set", variable); return Ok(-1); } let token = token.unwrap().into_string().unwrap(); - let api = BOT::new(token); + let api = BOT::new(token, thread_count); create_bot(self, api) } + #[native(name = "TGSendMessage")] pub fn bot_send_message( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, - chatid: String, - text: String, + chatid: AmxString, + text: AmxString, reply_id: i32, parse_mode: i32, - callback: String, - ) -> AmxResult { + callback: AmxString, + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(0); } let reply = if reply_id == -1 { None } else { Some(reply_id) }; + let callback = callback.to_string(); let parsemode: Option<&str> = match parse_mode { 0 => Some("HTML"), @@ -60,8 +71,8 @@ impl super::TgConnector { }; let send_message_obj = SendMessage { - chat_id: chatid, - text, + chat_id: chatid.to_string(), + text: text.to_string(), reply_to_message_id: reply, parse_mode: parsemode, }; @@ -70,20 +81,21 @@ impl super::TgConnector { Ok(1) } + #[native(name = "TGDeleteMessage")] pub fn bot_delete_message( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, - chatid: String, + chatid: AmxString, messageid: i32, - ) -> AmxResult { + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(0); } let delete_message_obj = DeleteMessage { - chat_id: chatid, + chat_id: chatid.to_string(), message_id: messageid, }; @@ -91,15 +103,16 @@ impl super::TgConnector { Ok(1) } + #[native(name = "TGEditMessage")] pub fn bot_edit_message( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, - chatid: String, + chatid: AmxString, messageid: i32, - text: String, + text: AmxString, parse_mode: i32, - ) -> AmxResult { + ) -> AmxResult { let parsemode: Option<&str> = match parse_mode { 0 => Some("HTML"), 1 => Some("markdown"), @@ -107,13 +120,13 @@ impl super::TgConnector { }; if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Error Invalid bot id {} passed", botid); return Ok(0); } let edit_message_obj = EditMessageText { - chat_id: chatid, - text, + chat_id: chatid.to_string(), + text: text.to_string(), message_id: messageid, parse_mode: parsemode, }; @@ -122,74 +135,103 @@ impl super::TgConnector { Ok(1) } - pub fn get_bot_user_id(&self, _amx: &AMX, botid: usize) -> AmxResult { + #[native(name = "TGGetBotUserId")] + pub fn get_bot_user_id(&self, _amx: &Amx, botid: usize) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(-1); } Ok(self.bots[&botid].user_id) } - pub fn cache_get_message(&self, _amx: &AMX, dest: &mut Cell, size: usize) -> AmxResult { + #[native(name = "TGCacheGetMessage")] + pub fn cache_get_message( + &self, + _amx: &Amx, + dest: UnsizedBuffer, + size: usize, + ) -> AmxResult { let cache_list = &self.telegram_messages; cache_get!(cache_list, dest, size) } - pub fn cache_get_username(&self, _amx: &AMX, dest: &mut Cell, size: usize) -> AmxResult { + #[native(name = "TGCacheGetUserName")] + pub fn cache_get_username( + &self, + _amx: &Amx, + dest: UnsizedBuffer, + size: usize, + ) -> AmxResult { let cache_list = &self.telegram_username; cache_get!(cache_list, dest, size) } + #[native(name = "TGCacheGetUserFirstName")] pub fn cache_get_user_first_name( &self, - _amx: &AMX, - dest: &mut Cell, + _amx: &Amx, + dest: UnsizedBuffer, size: usize, - ) -> AmxResult { + ) -> AmxResult { let cache_list = &self.telegram_firstname; cache_get!(cache_list, dest, size) } + #[native(name = "TGCacheGetUserLastName")] pub fn cache_get_user_last_name( &self, - _amx: &AMX, - dest: &mut Cell, + _amx: &Amx, + dest: UnsizedBuffer, size: usize, - ) -> AmxResult { + ) -> AmxResult { let cache_list = &self.telegram_lastname; cache_get!(cache_list, dest, size) } - pub fn cache_get_chatid(&self, _amx: &AMX, dest: &mut Cell, size: usize) -> AmxResult { + #[native(name = "TGCacheGetChatId")] + pub fn cache_get_chatid(&self, _amx: &Amx, dest: UnsizedBuffer, size: usize) -> AmxResult { let cache_list = &self.telegram_chatid; cache_get!(cache_list, dest, size) } - pub fn cache_get_chatname(&self, _amx: &AMX, dest: &mut Cell, size: usize) -> AmxResult { + #[native(name = "TGCacheGetChatName")] + pub fn cache_get_chatname( + &self, + _amx: &Amx, + dest: UnsizedBuffer, + size: usize, + ) -> AmxResult { let cache_list = &self.telegram_chatname; cache_get!(cache_list, dest, size) } - pub fn cache_get_chattype(&self, _amx: &AMX, dest: &mut Cell, size: usize) -> AmxResult { + #[native(name = "TGCacheGetChatType")] + pub fn cache_get_chattype( + &self, + _amx: &Amx, + dest: UnsizedBuffer, + size: usize, + ) -> AmxResult { let cache_list = &self.telegram_chattype; cache_get!(cache_list, dest, size) } + #[native(name = "TGGetUserChatStatus")] pub fn get_user_status( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, userid: i32, - chatid: String, - ) -> AmxResult { + chatid: AmxString, + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("**[TGConnector] Error Invalid bot id {} passed", botid); return Ok(0); } let getchatmember = GetChatMember { user_id: userid, - chat_id: chatid, + chat_id: chatid.to_string(), }; let chatmember = self.bots[&botid].get_chat_member(getchatmember); if chatmember.is_none() { @@ -209,22 +251,24 @@ impl super::TgConnector { } } + #[native(name = "TGGetUserNameFromId")] pub fn get_username_from_id( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, userid: i32, - chatid: String, - dest: &mut Cell, + chatid: AmxString, + dest: UnsizedBuffer, size: usize, - ) -> AmxResult { + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(0); } + let getchatmember = GetChatMember { user_id: userid, - chat_id: chatid, + chat_id: chatid.to_string(), }; let chatmember = self.bots[&botid].get_chat_member(getchatmember); @@ -240,12 +284,13 @@ impl super::TgConnector { match encode_replace(username.as_ref().unwrap()) { Ok(encoded) => { - set_string!(encoded, dest, size); + let mut dest = dest.into_sized_buffer(size); + let _ = samp::cell::string::put_in_buffer(&mut dest, &encoded); Ok(1) } Err(err) => { - log!( - "**[TGConnector][get_username_from_id] Failed encoding {:?} \n {:?}", + error!( + "[get_username_from_id] Failed encoding {:?} \n {:?}", username.as_ref().unwrap(), err ); @@ -254,23 +299,24 @@ impl super::TgConnector { } } + #[native(name = "TGGetDisplayNameFromId")] pub fn get_display_name_from_id( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, userid: i32, - chatid: String, - dest: &mut Cell, + chatid: AmxString, + dest: UnsizedBuffer, size: usize, - ) -> AmxResult { + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(0); } let getchatmember = GetChatMember { user_id: userid, - chat_id: chatid, + chat_id: chatid.to_string(), }; let chatmember = self.bots[&botid].get_chat_member(getchatmember); @@ -286,52 +332,59 @@ impl super::TgConnector { match encode_replace(&displayname) { Ok(encoded) => { - set_string!(encoded, dest, size); + let mut dest = dest.into_sized_buffer(size); + let _ = samp::cell::string::put_in_buffer(&mut dest, &encoded); Ok(1) } Err(err) => { - log!( - "**[TGConnector][get_display_name_from_id] Failed encoding {:?} \n {:?}", - displayname, - err + error!( + "get_display_name_from_id] Failed encoding {:?} \n {:?}", + displayname, err ); Ok(0) } } } + #[native(name = "TGGetChatMembersCount")] pub fn get_chat_members_count( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, - chatid: String, - ) -> AmxResult { + chatid: AmxString, + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(-1); } - let getchatmembercount = GetChatMembersCount { chat_id: chatid }; + let getchatmembercount = GetChatMembersCount { + chat_id: chatid.to_string(), + }; + match self.bots[&botid].get_chat_members_count(getchatmembercount) { None => Ok(-1), Some(count) => Ok(count), } } + #[native(name = "TGGetChatTitle")] pub fn get_chat_title( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, - chatid: String, - title: &mut Cell, + chatid: AmxString, + dest: UnsizedBuffer, size: usize, - ) -> AmxResult { + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(0); } - let getchat = GetChat { chat_id: chatid }; + let getchat = GetChat { + chat_id: chatid.to_string(), + }; let chat = self.bots[&botid].get_chat(getchat); if chat.is_none() { @@ -345,34 +398,37 @@ impl super::TgConnector { let chat_title = chat.unwrap().title.unwrap(); match encode_replace(&chat_title) { Ok(encoded) => { - set_string!(encoded, title, size); + let mut dest = dest.into_sized_buffer(size); + let _ = samp::cell::string::put_in_buffer(&mut dest, &encoded); Ok(1) } Err(err) => { - log!( - "**[TGConnector][get_chat_title] Failed encoding {:?} \n {:?}", - chat_title, - err + error!( + "[get_chat_title] Failed encoding {:?} \n {:?}", + chat_title, err ); Ok(0) } } } + #[native(name = "TGGetChatDescription")] pub fn get_chat_description( &self, - _amx: &AMX, + _amx: &Amx, botid: usize, - chatid: String, - description: &mut Cell, + chatid: AmxString, + dest: UnsizedBuffer, size: usize, - ) -> AmxResult { + ) -> AmxResult { if !self.bots.contains_key(&botid) { - log!("**[TGConnector] Error Invalid bot id {} passed", botid); + error!("Invalid bot id {} passed", botid); return Ok(0); } - let getchat = GetChat { chat_id: chatid }; + let getchat = GetChat { + chat_id: chatid.to_string(), + }; let chat = self.bots[&botid].get_chat(getchat); if chat.is_none() { @@ -386,14 +442,14 @@ impl super::TgConnector { let chat_description = chat.unwrap().description.unwrap(); match encode_replace(&chat_description) { Ok(encoded) => { - set_string!(encoded, description, size); + let mut dest = dest.into_sized_buffer(size); + let _ = samp::cell::string::put_in_buffer(&mut dest, &encoded); Ok(1) } Err(err) => { - log!( - "**[TGConnector][get_chat_description] Failed encoding {:?} \n {:?}", - chat_description, - err + error!( + "[get_chat_description] Failed encoding {:?} \n {:?}", + chat_description, err ); Ok(0) } diff --git a/src/plugin.rs b/src/plugin.rs index a2fff5d..f58a40c 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,125 +1,13 @@ use crate::api::BOT; use crate::internals; -use samp_sdk::amx::{AmxResult, AMX}; -use samp_sdk::consts::*; -use samp_sdk::types::Cell; -use samp_sdk::{define_native, expand_args, get_string, log, natives}; - +use log::{error, info}; +use samp::amx::AmxIdent; +use samp::prelude::*; use std::collections::{HashMap, LinkedList}; -define_native!(bot_connect, token: String); -define_native!(bot_connect_from_env, variable: String); -define_native!(get_bot_user_id, botid: usize); - -define_native!( - bot_send_message, - botid: usize, - chatid: String, - text: String, - reply_id: i32, - parse_mode: i32, - callback: String -); - -define_native!( - cache_get_message, - dest:ref Cell, - size:usize -); - -define_native!( - cache_get_username, - dest:ref Cell, - size:usize -); - -define_native!( - cache_get_user_first_name, - dest:ref Cell, - size:usize -); - -define_native!( - cache_get_user_last_name, - dest:ref Cell, - size:usize -); - -define_native!( - cache_get_chatid, - dest:ref Cell, - size:usize -); - -define_native!( - cache_get_chattype, - dest:ref Cell, - size:usize -); - -define_native!( - cache_get_chatname, - dest:ref Cell, - size:usize -); - -define_native!( - bot_delete_message, - botid: usize, - chatid: String, - messageid: i32 -); - -define_native!( - bot_edit_message, - botid: usize, - chatid: String, - messageid: i32, - text: String, - parse_mode: i32 -); - -define_native!(get_user_status, botid: usize, userid: i32, chatid: String); - -define_native!( - get_username_from_id, - botid:usize, - userid:i32, - chatid:String, - dest:ref Cell, - size:usize -); - -define_native!( - get_display_name_from_id, - botid:usize, - userid:i32, - chatid:String, - dest:ref Cell, - size:usize -); - -define_native!(get_chat_members_count, botid: usize, chatid: String); - -define_native!( - get_chat_title, - botid:usize, - chatid:String, - title:ref Cell, - size:usize -); - -define_native!( - get_chat_description, - botid:usize, - chatid:String, - description:ref Cell, - size:usize -); - pub struct TgConnector { - plugin_version: i32, - pub amx_list: Vec, + pub plugin_version: i32, + pub amx_list: Vec, pub bots: HashMap, pub bot_context_id: usize, pub telegram_messages: LinkedList, @@ -131,82 +19,47 @@ pub struct TgConnector { pub telegram_chattype: LinkedList, } -impl TgConnector { - pub fn load(&self) -> bool { - log!( +impl SampPlugin for TgConnector { + fn on_load(&mut self) { + info!( " ############################################################### # TGConnector # - # v0.1.0 Loaded!! # + # v0.1.1 Loaded!! # # Found any bugs? Report it here: # # https://github.com/Sreyas-Sreelal/tgconnector/issues # # # ############################################################### " ); - - true } - pub fn unload(&self) { - log!("**TGConnector v0.1.0 Unloaded!"); + fn on_unload(self: Box) { + info!("**TGConnector v0.1.1 Unloaded!"); } - pub fn amx_load(&mut self, amx: &mut AMX) -> Cell { - self.amx_list.push(amx.amx as usize); - - let natives = natives! { - "TGConnect" => bot_connect, - "TGConnectFromEnv" => bot_connect_from_env, - "TGSendMessage" => bot_send_message, - "TGCacheGetMessage" => cache_get_message, - "TGCacheGetUserName" => cache_get_username, - "TGCacheGetChatId" => cache_get_chatid, - "TGCacheGetChatType" => cache_get_chattype, - "TGCacheGetChatName" => cache_get_chatname, - "TGCacheGetUserFirstName" => cache_get_user_first_name, - "TGCacheGetUserLastName" => cache_get_user_last_name, - "TGDeleteMessage" => bot_delete_message, - "TGEditMessage" => bot_edit_message, - "TGGetUserChatStatus" => get_user_status, - "TGGetUserNameFromId" => get_username_from_id, - "TGGetDisplayNameFromId" => get_display_name_from_id, - "TGGetChatMembersCount" => get_chat_members_count, - "TGGetChatTitle" => get_chat_title, - "TGGetChatDescription" => get_chat_description, - "TGGetBotUserId" => get_bot_user_id - }; + fn on_amx_load(&mut self, amx: &Amx) { + self.amx_list.push(amx.ident()); - match amx.register(&natives) { - Ok(_) => log!("Natives are successful loaded"), - Err(err) => log!("Whoops, there is an error {:?}", err), - } - - let get_version: AmxResult<&mut i32> = amx.find_pubvar("_tgconnector_version"); + let get_version = amx.find_pubvar::("_tgconnector_version"); match get_version { Ok(version) => { if *version != self.plugin_version { - log!("**[TGConnector] Warning plugin and include version doesnot match : Include {:?} Plugin {:?}",version,self.plugin_version); + info!("Warning plugin and include version doesnot match : Include {:?} Plugin {:?}",*version,self.plugin_version); } } - Err(err) => log!( - "**[TGConnector] Failed to retrive include version Reason:{:?}", - err - ), + Err(err) => error!("Failed to retrive include version Reason:{:?}", err), } - - AMX_ERR_NONE } - pub fn amx_unload(&mut self, amx: &mut AMX) -> Cell { - let raw = amx.amx as usize; + fn on_amx_unload(&mut self, amx: &Amx) { + let raw = amx.ident(); let index = self.amx_list.iter().position(|x| *x == raw).unwrap(); self.amx_list.remove(index); - AMX_ERR_NONE } - pub fn process_tick(&mut self) { + fn process_tick(&mut self) { internals::update_process(self); internals::on_send_message_process(self); @@ -219,21 +72,3 @@ impl TgConnector { internals::clear_caches(&mut self.telegram_chatid); } } - -impl Default for TgConnector { - fn default() -> Self { - TgConnector { - plugin_version: 10, - amx_list: Vec::new(), - bots: HashMap::new(), - bot_context_id: 0, - telegram_messages: LinkedList::new(), - telegram_username: LinkedList::new(), - telegram_chatname: LinkedList::new(), - telegram_chatid: LinkedList::new(), - telegram_firstname: LinkedList::new(), - telegram_lastname: LinkedList::new(), - telegram_chattype: LinkedList::new(), - } - } -} diff --git a/test-server/samp.json b/test-server/samp.json deleted file mode 100644 index d06763c..0000000 --- a/test-server/samp.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "rcon_password": "|%GuRd324$\u0026|", - "port": 7777, - "plugins":["tgconnector"], - "gamemodes":["test"], - "hostname": "test", - "maxplayers": 32, - "mode": "y_testing" -} \ No newline at end of file