├── .gitignore ├── LICENSE ├── README.md ├── example ├── .gitignore ├── Cargo.lock ├── Cargo.toml └── src │ └── lib.rs ├── justfile ├── plugin.yml ├── resources └── include │ ├── all.h │ ├── api.h │ ├── command.h │ ├── common.h │ └── manifest.h ├── rust ├── .gitignore ├── Cargo.toml ├── build.rs └── src │ └── lib.rs └── src └── SOFe └── FfiPluginLoader ├── FfiPluginBase.php ├── FfiPluginLoader.php └── Main.php /.gitignore: -------------------------------------------------------------------------------- 1 | rust-target 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FfiPluginLoader 2 | Loads .so files as PocketMine plugins 3 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /example/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aho-corasick" 5 | version = "0.7.14" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "b476ce7103678b0c6d3d395dbbae31d48ff910bd28be979ba5d48c6351131d0d" 8 | dependencies = [ 9 | "memchr", 10 | ] 11 | 12 | [[package]] 13 | name = "ansi_term" 14 | version = "0.11.0" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" 17 | dependencies = [ 18 | "winapi", 19 | ] 20 | 21 | [[package]] 22 | name = "anyhow" 23 | version = "1.0.33" 24 | source = "registry+https://github.com/rust-lang/crates.io-index" 25 | checksum = "a1fd36ffbb1fb7c834eac128ea8d0e310c5aeb635548f9d58861e1308d46e71c" 26 | 27 | [[package]] 28 | name = "atty" 29 | version = "0.2.14" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 32 | dependencies = [ 33 | "hermit-abi", 34 | "libc", 35 | "winapi", 36 | ] 37 | 38 | [[package]] 39 | name = "bindgen" 40 | version = "0.55.1" 41 | source = "registry+https://github.com/rust-lang/crates.io-index" 42 | checksum = "75b13ce559e6433d360c26305643803cb52cfbabbc2b9c47ce04a58493dfb443" 43 | dependencies = [ 44 | "bitflags", 45 | "cexpr", 46 | "cfg-if 0.1.10", 47 | "clang-sys", 48 | "clap", 49 | "env_logger", 50 | "lazy_static", 51 | "lazycell", 52 | "log", 53 | "peeking_take_while", 54 | "proc-macro2", 55 | "quote", 56 | "regex", 57 | "rustc-hash", 58 | "shlex", 59 | "which", 60 | ] 61 | 62 | [[package]] 63 | name = "bitflags" 64 | version = "1.2.1" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 67 | 68 | [[package]] 69 | name = "cexpr" 70 | version = "0.4.0" 71 | source = "registry+https://github.com/rust-lang/crates.io-index" 72 | checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" 73 | dependencies = [ 74 | "nom", 75 | ] 76 | 77 | [[package]] 78 | name = "cfg-if" 79 | version = "0.1.10" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 82 | 83 | [[package]] 84 | name = "cfg-if" 85 | version = "1.0.0" 86 | source = "registry+https://github.com/rust-lang/crates.io-index" 87 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 88 | 89 | [[package]] 90 | name = "clang-sys" 91 | version = "1.0.1" 92 | source = "registry+https://github.com/rust-lang/crates.io-index" 93 | checksum = "fa785e9017cb8e8c8045e3f096b7d1ebc4d7337cceccdca8d678a27f788ac133" 94 | dependencies = [ 95 | "glob", 96 | "libc", 97 | "libloading", 98 | ] 99 | 100 | [[package]] 101 | name = "clap" 102 | version = "2.33.3" 103 | source = "registry+https://github.com/rust-lang/crates.io-index" 104 | checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" 105 | dependencies = [ 106 | "ansi_term", 107 | "atty", 108 | "bitflags", 109 | "strsim", 110 | "textwrap", 111 | "unicode-width", 112 | "vec_map", 113 | ] 114 | 115 | [[package]] 116 | name = "env_logger" 117 | version = "0.7.1" 118 | source = "registry+https://github.com/rust-lang/crates.io-index" 119 | checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" 120 | dependencies = [ 121 | "atty", 122 | "humantime", 123 | "log", 124 | "regex", 125 | "termcolor", 126 | ] 127 | 128 | [[package]] 129 | name = "example-plugin" 130 | version = "0.2.0" 131 | dependencies = [ 132 | "anyhow", 133 | "pocketmine", 134 | ] 135 | 136 | [[package]] 137 | name = "glob" 138 | version = "0.3.0" 139 | source = "registry+https://github.com/rust-lang/crates.io-index" 140 | checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" 141 | 142 | [[package]] 143 | name = "hermit-abi" 144 | version = "0.1.17" 145 | source = "registry+https://github.com/rust-lang/crates.io-index" 146 | checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" 147 | dependencies = [ 148 | "libc", 149 | ] 150 | 151 | [[package]] 152 | name = "humantime" 153 | version = "1.3.0" 154 | source = "registry+https://github.com/rust-lang/crates.io-index" 155 | checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" 156 | dependencies = [ 157 | "quick-error", 158 | ] 159 | 160 | [[package]] 161 | name = "lazy_static" 162 | version = "1.4.0" 163 | source = "registry+https://github.com/rust-lang/crates.io-index" 164 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 165 | 166 | [[package]] 167 | name = "lazycell" 168 | version = "1.3.0" 169 | source = "registry+https://github.com/rust-lang/crates.io-index" 170 | checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" 171 | 172 | [[package]] 173 | name = "libc" 174 | version = "0.2.79" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743" 177 | 178 | [[package]] 179 | name = "libloading" 180 | version = "0.6.5" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "1090080fe06ec2648d0da3881d9453d97e71a45f00eb179af7fdd7e3f686fdb0" 183 | dependencies = [ 184 | "cfg-if 1.0.0", 185 | "winapi", 186 | ] 187 | 188 | [[package]] 189 | name = "log" 190 | version = "0.4.11" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" 193 | dependencies = [ 194 | "cfg-if 0.1.10", 195 | ] 196 | 197 | [[package]] 198 | name = "memchr" 199 | version = "2.3.4" 200 | source = "registry+https://github.com/rust-lang/crates.io-index" 201 | checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" 202 | 203 | [[package]] 204 | name = "nom" 205 | version = "5.1.2" 206 | source = "registry+https://github.com/rust-lang/crates.io-index" 207 | checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" 208 | dependencies = [ 209 | "memchr", 210 | "version_check", 211 | ] 212 | 213 | [[package]] 214 | name = "once_cell" 215 | version = "1.4.1" 216 | source = "registry+https://github.com/rust-lang/crates.io-index" 217 | checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" 218 | 219 | [[package]] 220 | name = "peeking_take_while" 221 | version = "0.1.2" 222 | source = "registry+https://github.com/rust-lang/crates.io-index" 223 | checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" 224 | 225 | [[package]] 226 | name = "pocketmine" 227 | version = "0.1.0" 228 | dependencies = [ 229 | "anyhow", 230 | "bindgen", 231 | "libc", 232 | "once_cell", 233 | "static_assertions", 234 | ] 235 | 236 | [[package]] 237 | name = "proc-macro2" 238 | version = "1.0.24" 239 | source = "registry+https://github.com/rust-lang/crates.io-index" 240 | checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" 241 | dependencies = [ 242 | "unicode-xid", 243 | ] 244 | 245 | [[package]] 246 | name = "quick-error" 247 | version = "1.2.3" 248 | source = "registry+https://github.com/rust-lang/crates.io-index" 249 | checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" 250 | 251 | [[package]] 252 | name = "quote" 253 | version = "1.0.7" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" 256 | dependencies = [ 257 | "proc-macro2", 258 | ] 259 | 260 | [[package]] 261 | name = "regex" 262 | version = "1.4.1" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | checksum = "8963b85b8ce3074fecffde43b4b0dded83ce2f367dc8d363afc56679f3ee820b" 265 | dependencies = [ 266 | "aho-corasick", 267 | "memchr", 268 | "regex-syntax", 269 | "thread_local", 270 | ] 271 | 272 | [[package]] 273 | name = "regex-syntax" 274 | version = "0.6.20" 275 | source = "registry+https://github.com/rust-lang/crates.io-index" 276 | checksum = "8cab7a364d15cde1e505267766a2d3c4e22a843e1a601f0fa7564c0f82ced11c" 277 | 278 | [[package]] 279 | name = "rustc-hash" 280 | version = "1.1.0" 281 | source = "registry+https://github.com/rust-lang/crates.io-index" 282 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" 283 | 284 | [[package]] 285 | name = "shlex" 286 | version = "0.1.1" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" 289 | 290 | [[package]] 291 | name = "static_assertions" 292 | version = "1.1.0" 293 | source = "registry+https://github.com/rust-lang/crates.io-index" 294 | checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" 295 | 296 | [[package]] 297 | name = "strsim" 298 | version = "0.8.0" 299 | source = "registry+https://github.com/rust-lang/crates.io-index" 300 | checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 301 | 302 | [[package]] 303 | name = "termcolor" 304 | version = "1.1.0" 305 | source = "registry+https://github.com/rust-lang/crates.io-index" 306 | checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" 307 | dependencies = [ 308 | "winapi-util", 309 | ] 310 | 311 | [[package]] 312 | name = "textwrap" 313 | version = "0.11.0" 314 | source = "registry+https://github.com/rust-lang/crates.io-index" 315 | checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" 316 | dependencies = [ 317 | "unicode-width", 318 | ] 319 | 320 | [[package]] 321 | name = "thread_local" 322 | version = "1.0.1" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" 325 | dependencies = [ 326 | "lazy_static", 327 | ] 328 | 329 | [[package]] 330 | name = "unicode-width" 331 | version = "0.1.8" 332 | source = "registry+https://github.com/rust-lang/crates.io-index" 333 | checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" 334 | 335 | [[package]] 336 | name = "unicode-xid" 337 | version = "0.2.1" 338 | source = "registry+https://github.com/rust-lang/crates.io-index" 339 | checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" 340 | 341 | [[package]] 342 | name = "vec_map" 343 | version = "0.8.2" 344 | source = "registry+https://github.com/rust-lang/crates.io-index" 345 | checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" 346 | 347 | [[package]] 348 | name = "version_check" 349 | version = "0.9.2" 350 | source = "registry+https://github.com/rust-lang/crates.io-index" 351 | checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" 352 | 353 | [[package]] 354 | name = "which" 355 | version = "3.1.1" 356 | source = "registry+https://github.com/rust-lang/crates.io-index" 357 | checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" 358 | dependencies = [ 359 | "libc", 360 | ] 361 | 362 | [[package]] 363 | name = "winapi" 364 | version = "0.3.9" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 367 | dependencies = [ 368 | "winapi-i686-pc-windows-gnu", 369 | "winapi-x86_64-pc-windows-gnu", 370 | ] 371 | 372 | [[package]] 373 | name = "winapi-i686-pc-windows-gnu" 374 | version = "0.4.0" 375 | source = "registry+https://github.com/rust-lang/crates.io-index" 376 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 377 | 378 | [[package]] 379 | name = "winapi-util" 380 | version = "0.1.5" 381 | source = "registry+https://github.com/rust-lang/crates.io-index" 382 | checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" 383 | dependencies = [ 384 | "winapi", 385 | ] 386 | 387 | [[package]] 388 | name = "winapi-x86_64-pc-windows-gnu" 389 | version = "0.4.0" 390 | source = "registry+https://github.com/rust-lang/crates.io-index" 391 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 392 | -------------------------------------------------------------------------------- /example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example-plugin" 3 | version = "0.2.0" 4 | authors = ["SOFe "] 5 | edition = "2018" 6 | 7 | [lib] 8 | crate-type = ["cdylib"] 9 | 10 | [dependencies] 11 | anyhow = "1.0.33" 12 | pocketmine = {path = "../rust"} 13 | -------------------------------------------------------------------------------- /example/src/lib.rs: -------------------------------------------------------------------------------- 1 | pocketmine::plugin!("example-plugin" version "0.2.0": Main); 2 | 3 | struct Main; 4 | 5 | impl pocketmine::Plugin for Main { 6 | fn init(api: pocketmine::Api) -> anyhow::Result { 7 | println!("Plugin initialized"); 8 | Ok(Self) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /justfile: -------------------------------------------------------------------------------- 1 | example: 2 | cd example && cargo build --target-dir=../rust-target --release 3 | -------------------------------------------------------------------------------- /plugin.yml: -------------------------------------------------------------------------------- 1 | name: FfiPluginLoader 2 | version: 0.1.0 3 | api: 4.0.0 4 | main: SOFe\FfiPluginLoader\Main 5 | load: STARTUP 6 | -------------------------------------------------------------------------------- /resources/include/all.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "common.h" 4 | #include "command.h" 5 | #include "api.h" 6 | -------------------------------------------------------------------------------- /resources/include/api.h: -------------------------------------------------------------------------------- 1 | typedef struct { 2 | ffi_api ffi; 3 | command_api command; 4 | } api; 5 | -------------------------------------------------------------------------------- /resources/include/command.h: -------------------------------------------------------------------------------- 1 | typedef enum { 2 | COMMAND_SENDER_TYPE_CONSOLE = 0, 3 | COMMAND_SENDER_TYPE_PLAYER = 1, 4 | COMMAND_SENDER_TYPE_OTHER = 2, 5 | } command_sender_type; 6 | 7 | typedef struct { 8 | php_object value; 9 | string language; 10 | string name; 11 | php_int screen_line_height; 12 | } command_sender; 13 | 14 | typedef struct { 15 | void (*register_command)(string name, string description, string usage, string_list aliases, string permission); 16 | void (*handle_command)(string name, php_object command_sender, string_list args); 17 | void (*send_message)(command_sender, string message); 18 | } command_api; 19 | -------------------------------------------------------------------------------- /resources/include/common.h: -------------------------------------------------------------------------------- 1 | typedef ptrdiff_t php_int; 2 | 3 | // PHP objects are exposed to FFI as a php_object. 4 | // This counts as a reference to the object, 5 | // and free_object() must be explicitly call to free the reference. 6 | // php_object values passed to FFI must be freed exactly the same number of times as passed. 7 | typedef php_int php_object; 8 | 9 | // PHP strings are passed over FFI as a char pointer. 10 | // When passed from PHP, it must be free_string()ed explicitly as long as unused. 11 | // When passed to PHP, it must be NUL-terminated and live for at least the time of the function call. 12 | typedef const char * php_string; 13 | 14 | // This type represents strings that cannot leak. 15 | // 16 | // When used in parameters, this implies the pointer is only valid before the function returns. 17 | // When used in return value, this implies the string is static and always valid. 18 | typedef const char * string; 19 | 20 | // Lists of PHP string are passed over FFI as a pointer to the first string and a size. 21 | // This has identical memory semantics as a string. 22 | // The strings are NUL-terminated in contiguous memory. 23 | // ptr can be anything if size is 0. 24 | typedef struct { 25 | const char *ptr; 26 | size_t size; 27 | } string_list; 28 | 29 | typedef struct { 30 | void (*free_object)(php_object); 31 | void (*free_string)(string); 32 | } ffi_api; 33 | 34 | char errstr[4096]; 35 | -------------------------------------------------------------------------------- /resources/include/manifest.h: -------------------------------------------------------------------------------- 1 | string plugin_name(); 2 | string plugin_version(); 3 | string plugin_ffi_version(); 4 | 5 | void plugin_entry_point(api *api); 6 | -------------------------------------------------------------------------------- /rust/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pocketmine" 3 | version = "0.1.0" 4 | authors = ["PMMP Team ", "SOFe "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | anyhow = "1.0.33" 9 | libc = "0.2.79" 10 | once_cell = "1.4.1" 11 | static_assertions = "1.1.0" 12 | 13 | [build-dependencies] 14 | bindgen = "0.55.1" 15 | 16 | -------------------------------------------------------------------------------- /rust/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::path::PathBuf; 3 | 4 | fn main() { 5 | let bindings = bindgen::Builder::default() 6 | .header( 7 | PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()) 8 | .join("../resources/include/all.h") 9 | .display() 10 | .to_string(), 11 | ) 12 | .parse_callbacks(Box::new(bindgen::CargoCallbacks)) 13 | .generate() 14 | .unwrap(); 15 | 16 | let out = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bind.rs"); 17 | bindings.write_to_file(out).unwrap(); 18 | } 19 | -------------------------------------------------------------------------------- /rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::rc::Rc; 2 | 3 | use ffi::size_t; 4 | use libc::c_char; 5 | 6 | #[doc(hidden)] 7 | pub extern crate libc; 8 | #[doc(hidden)] 9 | pub extern crate static_assertions; 10 | #[doc(hidden)] 11 | pub use once_cell::sync::OnceCell; 12 | 13 | // #[doc(hidden)] 14 | #[allow(non_camel_case_types, non_upper_case_globals)] 15 | pub mod ffi { 16 | include!(concat!(env!("OUT_DIR"), "/bind.rs")); 17 | } 18 | 19 | #[macro_export] 20 | macro_rules! plugin { 21 | ( 22 | $name:literal version $version:literal: $impl:ty 23 | ) => { 24 | $crate::static_assertions::assert_impl_all!($impl: $crate::Plugin); 25 | 26 | static __POCKETMINE_PLUGIN: $crate::OnceCell<$impl> = $crate::OnceCell::new(); 27 | 28 | /// FFI function 29 | #[no_mangle] 30 | pub fn plugin_main() -> *const $crate::libc::c_char { 31 | (concat!($name, "\0")).as_ptr() as *const $crate::libc::c_char 32 | } 33 | 34 | /// FFI function 35 | #[no_mangle] 36 | pub fn plugin_version() -> *const $crate::libc::c_char { 37 | (concat!($version, "\0")).as_ptr() as *const $crate::libc::c_char 38 | } 39 | 40 | /// FFI function 41 | #[no_mangle] 42 | pub fn plugin_ffi_version() -> *const $crate::libc::c_char { 43 | "0.1.0".as_ptr() as *const $crate::libc::c_char 44 | } 45 | 46 | /// FFI function 47 | /// 48 | /// # Safety 49 | /// See manifest.h 50 | #[no_mangle] 51 | pub unsafe fn plugin_entry_point(api: $crate::ffi::api) -> bool { 52 | let plugin = match <$impl as $crate::Plugin>::init($crate::Api::from(api)) { 53 | Ok(plugin) => plugin, 54 | Err(err) => { 55 | unsafe { 56 | // This function is always called from the main thread 57 | $crate::set_errstr(err); 58 | } 59 | return false; 60 | } 61 | }; 62 | let _ = __POCKETMINE_PLUGIN.set(plugin); // plugin_entry_point should only be called once 63 | true 64 | } 65 | }; 66 | } 67 | 68 | /// Sets the error string to use 69 | /// 70 | /// # Safety 71 | /// This function must be called on the main thread. 72 | pub unsafe fn set_errstr(err: impl ToString) { 73 | let mut err = err.to_string(); 74 | let mut bytes = err.as_bytes_mut(); // safety: we won't use the original `err` again 75 | if bytes.len() > 4095 { 76 | bytes = &mut bytes[..4095]; 77 | for byte in bytes.iter_mut().skip(4092) { 78 | *byte = b'.'; 79 | } 80 | } 81 | 82 | let errstr = ffi::errstr.as_mut_ptr() as *mut u8; // safety: only called from main thread 83 | let errstr = std::slice::from_raw_parts_mut(errstr, 4096); // safety: i8 and u8 have same size 84 | errstr.copy_from_slice(bytes); 85 | errstr[bytes.len()] = b'\0'; 86 | } 87 | 88 | pub struct Api { 89 | ffi: Rc, 90 | } 91 | 92 | impl From for Api { 93 | fn from(ffi: ffi::api) -> Self { 94 | Self { ffi: Rc::new(ffi) } 95 | } 96 | } 97 | 98 | pub struct CommandApi<'t>(&'t Api); 99 | 100 | impl<'t> CommandApi<'t> { 101 | pub fn register( 102 | &self, 103 | name: impl Into, 104 | description: impl Into, 105 | usage: impl Into, 106 | aliases: &[&str], 107 | permission: impl Into, 108 | ) { 109 | let mut name = name.into(); 110 | name.push('\0'); 111 | let mut description = description.into(); 112 | description.push('\0'); 113 | let mut usage = usage.into(); 114 | usage.push('\0'); 115 | let mut permission = permission.into(); 116 | permission.push('\0'); 117 | 118 | let mut aliases_c = String::new(); 119 | for alias in aliases { 120 | aliases_c += alias; 121 | aliases_c.push('\0'); 122 | } 123 | let aliases_list = ffi::string_list { 124 | ptr: aliases_c.as_ptr() as *const c_char, 125 | size: aliases.len() as size_t, 126 | }; 127 | 128 | let f = self.0.ffi.command.register_command.unwrap(); 129 | unsafe { 130 | f( 131 | name.as_ptr() as *const c_char, 132 | description.as_ptr() as *const c_char, 133 | usage.as_ptr() as *const c_char, 134 | aliases_list, 135 | permission.as_ptr() as *const c_char, 136 | ); 137 | } 138 | } 139 | } 140 | 141 | pub trait Plugin: Sized { 142 | fn init(api: Api) -> Result; 143 | } 144 | -------------------------------------------------------------------------------- /src/SOFe/FfiPluginLoader/FfiPluginBase.php: -------------------------------------------------------------------------------- 1 | getFile()); 10 | $ffi->plugin_main(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/SOFe/FfiPluginLoader/FfiPluginLoader.php: -------------------------------------------------------------------------------- 1 | plugin_name()); 30 | $version = FFI::string($ffi->plugin_version()); 31 | $api = FFI::string($ffi->plugin_ffi_version()); 32 | // TODO check $api against $this->getDescription()->getVersion() 33 | return new PluginDescription([ 34 | "name" => $name, 35 | "version" => $version, 36 | "api" => [VersionInfo::BASE_VERSION], 37 | "main" => FfiPluginBase::class, 38 | ]); 39 | } 40 | 41 | public function loadPlugin(string $path) : void { 42 | self::loadSo($path); 43 | } 44 | 45 | public function getAccessProtocol() : string { 46 | return ""; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/SOFe/FfiPluginLoader/Main.php: -------------------------------------------------------------------------------- 1 | getServer()->getPluginManager()->registerInterface(new FfiPluginLoader); 10 | $this->getServer()->getPluginManager()->loadPlugins($this->getServer()->getPluginPath(), [FfiPluginLoader::class]); 11 | $this->getServer()->enablePlugins(PluginLoadOrder::STARTUP()); 12 | } 13 | } 14 | --------------------------------------------------------------------------------