├── LICENSE ├── README.MD └── lua ├── autorun └── glua-netmon-init.lua ├── glua-netmon ├── binarychunk.lua ├── capturedmessage.lua ├── cl_config.lua ├── cl_net.lua ├── hooks.lua ├── interfaces │ ├── cl_clienttab.lua │ ├── cl_interface.lua │ └── cl_registrytab.lua ├── netmon.lua ├── profiling │ ├── profiler.lua │ ├── profilingdata.lua │ ├── profilingfilter.lua │ └── profilingstats.lua ├── registry.lua ├── sv_config.lua ├── sv_net.lua └── utils.lua └── netmon-addon-autoreg.lua /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | 2 | # gLua Net Monitor 3 | A simple utility that captures outgoing and incoming net messages to help you debug and optimize bandwidth. 4 | Supports both the net library and the deprecated umsg library (please update your addons if you still use it). 5 | 6 | ![Net Monitor Example](https://i.imgur.com/nGGLrVG.png) 7 | 8 | ## Installation 9 | 10 | Simply drop the `gLua-net-monitor` folder in your `garrysmod/addons` folder, it'll work by itself. 11 | 12 | ## Compatibility 13 | Addons that override any net/umsg/bf_read function overrides must call the base implementation for this to work. 14 | Overriding `net.Incoming` might stop this utility from capturing incoming messages since it requires the message header to work. 15 | 16 | ## Hooks 17 | By default, the `hooks.lua` file contains some basic implementations for debugging. Feel free to get rid of them. 18 | 19 | ### OnNetMessageStarted 20 | ```lua 21 | -- Called when a net message is started. 22 | -- @param msgName: name of the message that was started. 23 | -- @param funcInfo: info about the function that started the message. 24 | hook.Add("OnNetMessageStarted", "NetStarted", function(msgName, funcInfo) end) 25 | ``` 26 | 27 | ### OnNetMessageCaptured 28 | 29 | ```lua 30 | -- Called when a net message is captured, for both outgoing and incoming messages. 31 | -- Keep your code fast in here! This is called for every net/umsg message. 32 | -- @param msg: The captured message, see NetMonitor.CapturedMessage 33 | -- @param funcInfo: For outgoing messages, the function who started the message. 34 | hook.Add("OnNetMessageCaptured", "NetCaptured", function(msg, funcInfo) end) 35 | ``` 36 | 37 | ### OnNetMessageIgnored 38 | ```lua 39 | -- Called when a net message is received but no receiving function is hooked. 40 | -- This is likely an oversight of a developer, albeit a wasteful one. 41 | -- @param msg: The captured message, see NetMonitor.CapturedMessage 42 | hook.Add("OnNetMessageIgnored", "NetIgnored", function(msg) end) 43 | ``` 44 | 45 | ### OnNetMessageDumpedData 46 | ```lua 47 | -- Called when a received net message does not read all the received data. 48 | -- Not called for the usermessage lib messages. 49 | -- Called after OnNetMessageCaptured & OnNetMessageIgnored 50 | -- This means you or an addon on your server is wasting bandwidth! 51 | -- @param msg: The captured message, see NetMonitor.CapturedMessage 52 | -- @note: The captured message data will contain a binary string dump of the remaining data. 53 | hook.Add("OnNetMessageDumpedData", "NetDumped", function(msg) end) 54 | ``` 55 | 56 | ### OnNetMessageDiscarded 57 | ```lua 58 | -- Called when a net message is discarded due to starting another without finishing the current one. 59 | -- @param msg: The captured message which got discarded, likely missing some information. 60 | -- @param funcInfo: The info about the function which called the dicarded message's net.Start() 61 | hook.Add("OnNetMessageDiscarded", "NetDiscarded", function(msg, funcInfo) end) 62 | ``` 63 | 64 | ### OnNetRegistryUpdated 65 | ```lua 66 | -- Called clientside when the server's registry is received. 67 | hook.Add("OnNetRegistryUpdated", "NetRegistryUpdate", function() end) 68 | ``` -------------------------------------------------------------------------------- /lua/autorun/glua-netmon-init.lua: -------------------------------------------------------------------------------- 1 | print("======================================") 2 | print("= Net Message Monitor by Erlite v1.0 =") 3 | print("======================================") 4 | 5 | print() 6 | print("Overriding net and umsg library functions to capture messages.") 7 | print("Do note that any addon who does so as well without calling base functions will probably not work with this one.") 8 | print() 9 | 10 | local function ClientInclude(path) 11 | if SERVER then 12 | AddCSLuaFile(path) 13 | else 14 | include(path) 15 | end 16 | end 17 | 18 | if SERVER then include("glua-netmon/sv_config.lua") end 19 | 20 | ClientInclude("glua-netmon/cl_config.lua") 21 | 22 | -- Miscellaneous utils and networking 23 | include("glua-netmon/utils.lua") 24 | include("glua-netmon/binarychunk.lua") 25 | include("glua-netmon/capturedmessage.lua") 26 | 27 | -- Registry 28 | include("glua-netmon/registry.lua") 29 | 30 | -- Profiling 31 | include("glua-netmon/profiling/profilingfilter.lua") 32 | include("glua-netmon/profiling/profilingdata.lua") 33 | include("glua-netmon/profiling/profiler.lua") 34 | 35 | -- Monitoring 36 | include("glua-netmon/netmon.lua") 37 | include("glua-netmon/hooks.lua") 38 | 39 | if SERVER then 40 | include("glua-netmon/sv_net.lua") 41 | AddCSLuaFile("glua-netmon/cl_net.lua") 42 | else 43 | include("glua-netmon/cl_net.lua") 44 | end 45 | 46 | -- Net monitor UI files 47 | ClientInclude("glua-netmon/interfaces/cl_clienttab.lua") 48 | ClientInclude("glua-netmon/interfaces/cl_registrytab.lua") 49 | ClientInclude("glua-netmon/interfaces/cl_interface.lua") 50 | 51 | 52 | -- Create capture folders if they don't exist. 53 | if NetMonitor.Utils.VerifyDataFolders() then 54 | print("Created capture data folders.") 55 | end 56 | 57 | print() 58 | print("Done!") -------------------------------------------------------------------------------- /lua/glua-netmon/binarychunk.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | 5 | NetMonitor.BinaryChunk = {} 6 | NetMonitor.BinaryChunk.__index = NetMonitor.BinaryChunk 7 | 8 | function NetMonitor.BinaryChunk:new(chunkNum, chunkAmount, chunk) 9 | local tbl = 10 | { 11 | id = chunkNum, 12 | amount = chunkAmount, 13 | len = #chunk, 14 | data = chunk 15 | } 16 | 17 | setmetatable(tbl, NetMonitor.BinaryChunk) 18 | return tbl 19 | end 20 | 21 | function NetMonitor.BinaryChunk:GetId() 22 | return self.id 23 | end 24 | 25 | function NetMonitor.BinaryChunk:GetAmount() 26 | return self.amount 27 | end 28 | 29 | function NetMonitor.BinaryChunk:GetData() 30 | return self.data 31 | end 32 | 33 | function NetMonitor.BinaryChunk:GetLength() 34 | return self.len 35 | end 36 | 37 | function net.WriteBinaryChunk(chunk) 38 | if getmetatable(chunk) != NetMonitor.BinaryChunk then 39 | Error("Cannot write invalid chunk, expected BinaryChunk, got: " .. chunk or "nil") 40 | return 41 | end 42 | 43 | net.WriteUInt(chunk:GetId(), 16) 44 | net.WriteUInt(chunk:GetAmount(), 16) 45 | net.WriteUInt(chunk:GetLength(), 16) 46 | net.WriteData(chunk:GetData(), chunk:GetLength()) 47 | end 48 | 49 | function net.ReadBinaryChunk() 50 | local id = net.ReadUInt(16) 51 | local amount = net.ReadUInt(16) 52 | local len = net.ReadUInt(16) 53 | local dat = net.ReadData(len) 54 | 55 | return NetMonitor.BinaryChunk(id, amount, dat) 56 | end 57 | 58 | setmetatable(NetMonitor.BinaryChunk, {__call = NetMonitor.BinaryChunk.new}) -------------------------------------------------------------------------------- /lua/glua-netmon/capturedmessage.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | 5 | NetMonitor.CapturedMessage = {} 6 | NetMonitor.CapturedMessage.__index = NetMonitor.CapturedMessage 7 | 8 | function NetMonitor.CapturedMessage:new(msgName, isUnreliable) 9 | if !msgName or !isstring(msgName) then 10 | Error("Invalid message name passed in captured message constructor.") 11 | end 12 | 13 | isUnreliable = isUnreliable or false 14 | 15 | local msgSender = "SERVER" 16 | local msgSenderId = -1 17 | 18 | if CLIENT then 19 | if !LocalPlayer() or !LocalPlayer().Nick or !LocalPlayer().SteamID64 then 20 | -- Client is still connecting. 21 | msgSender = "CONNECTING" 22 | msgSenderId = -1 23 | else 24 | msgSender = LocalPlayer():Nick() 25 | msgSenderId = LocalPlayer():SteamID64() 26 | end 27 | end 28 | 29 | local tbl = 30 | { 31 | name = msgName, 32 | id = util.NetworkStringToID(msgName), 33 | unreliable = isUnreliable, 34 | deprecated = false, 35 | mode = nil, 36 | senderId = msgSenderId, 37 | senderName = msgSender, 38 | recipients = nil, 39 | bits = 0, 40 | wastedBits = 0, 41 | data = {}, 42 | } 43 | 44 | setmetatable(tbl, NetMonitor.CapturedMessage) 45 | return tbl 46 | end 47 | 48 | -- Returns this message's name. 49 | function NetMonitor.CapturedMessage:GetName() 50 | return self.name 51 | end 52 | 53 | -- Returns this message's id (each message name has a unique id). 54 | function NetMonitor.CapturedMessage:GetId() 55 | return self.id 56 | end 57 | 58 | -- Returns this message's size in bits. 59 | -- For incoming messages, this is the amount of bits received. 60 | -- For outgoing messages, this is the amount of bits written. 61 | function NetMonitor.CapturedMessage:GetBits() 62 | return self.bits 63 | end 64 | 65 | -- INTERNAL: you shouldn't use this. 66 | function NetMonitor.CapturedMessage:SetBits(bits) 67 | self.bits = bits 68 | end 69 | 70 | -- Returns the amount of bits that got wasted by not being read for an incoming message. 71 | function NetMonitor.CapturedMessage:GetWastedBits() 72 | return self.wastedBits 73 | end 74 | 75 | -- INTERNAL: you shouldn't use this. 76 | function NetMonitor.CapturedMessage:SetWastedBits(waste) 77 | self.wastedBits = waste 78 | end 79 | 80 | -- Returns the recipients of this message, only valid if this message is outgoing. 81 | function NetMonitor.CapturedMessage:GetRecipients() 82 | return self.recipients or {} 83 | end 84 | 85 | -- INTERNAL: you shouldn't use this. 86 | function NetMonitor.CapturedMessage:SetRecipients(recipients) 87 | if !self.recipients then self.recipients = recipients end 88 | end 89 | 90 | -- Returns the name and id of the sender. On clients, this will be SERVER and -1 91 | function NetMonitor.CapturedMessage:GetSender() 92 | return self.senderName, self.senderId 93 | end 94 | 95 | -- INTERNAL: you shouldn't use this. 96 | function NetMonitor.CapturedMessage:SetSender(client) 97 | if !client or !isentity(client) and !client:IsPlayer() then 98 | Error("Invalid client passed as captured message sender") 99 | end 100 | 101 | self.senderId = client:SteamID64() 102 | self.senderName = client:Nick() 103 | end 104 | 105 | -- Returns this net message's mode. 106 | -- Values to expect are Send, SendToServer, Broadcast, SendPVS, SendPAS or SendOmit. 107 | -- Discarded net messages will have the Discarded mode 108 | -- Received net messages will have the Received mode 109 | function NetMonitor.CapturedMessage:GetMode() 110 | return self.mode or "Unknown" 111 | end 112 | 113 | -- INTERNAL: you shouldn't use this. 114 | function NetMonitor.CapturedMessage:SetMode(mode) 115 | if !self.mode then 116 | self.mode = mode 117 | if mode == "Received" and CLIENT then 118 | self.senderName = "SERVER" 119 | self.senderId = -1 120 | end 121 | end 122 | end 123 | 124 | -- Returns whether or not this message is sent unreliably. Only set in outgoing messages. 125 | function NetMonitor.CapturedMessage:IsUnreliable() 126 | return self.unreliable 127 | end 128 | 129 | -- Returns whether or not this message was received or sent. 130 | function NetMonitor.CapturedMessage:IsReceived() 131 | return self.mode == "Received" 132 | end 133 | 134 | -- Returns true if this message was discarded. 135 | function NetMonitor.CapturedMessage:IsDiscarded() 136 | return self.mode == "Discarded" 137 | end 138 | 139 | -- Returns true if this message was ignored. 140 | function NetMonitor.CapturedMessage:IsIgnored() 141 | return self.mode == "Ignored" 142 | end 143 | 144 | -- Returns the data written to the net message. 145 | function NetMonitor.CapturedMessage:GetData() 146 | return self.data 147 | end 148 | 149 | -- Called if the captured message was sent/received using the deprecated umsg system. 150 | function NetMonitor.CapturedMessage:SetDeprecated() 151 | self.deprecated = true 152 | end 153 | 154 | -- Whether or not this message was sent/received using the deprecated umsg system. 155 | function NetMonitor.CapturedMessage:IsUserMessage() 156 | return self.deprecated 157 | end 158 | 159 | -- Write an angle to the captured message's data. 160 | function NetMonitor.CapturedMessage:WriteAngle(ang) 161 | self.data[#self.data + 1] = {type = "ANGLE", value = ang} 162 | end 163 | 164 | -- Write a bit to the captured message's data. 165 | function NetMonitor.CapturedMessage:WriteBit(inBit) 166 | self.data[#self.data + 1] = {type = "BIT", value = inBit} 167 | end 168 | 169 | -- Write a color to the capture message's data. 170 | function NetMonitor.CapturedMessage:WriteColor(color) 171 | self.data[#self.data + 1] = {type = "COLOR", value = color} 172 | end 173 | 174 | -- Write binary data to the captured message's data. 175 | function NetMonitor.CapturedMessage:WriteData(binaryData, length) 176 | if length <= 0 then 177 | binaryData = "" 178 | elseif #binaryData > length then 179 | binaryData = string.sub(binaryData, 1, length) 180 | end 181 | 182 | self.data[#self.data + 1] = {type = "BINARY", value = binaryData} 183 | end 184 | 185 | -- Write a double to the captured message's data. 186 | function NetMonitor.CapturedMessage:WriteDouble(double) 187 | self.data[#self.data + 1] = {type = "DOUBLE", value = double} 188 | end 189 | 190 | -- Write an entity to the captured message's data. This is the entity's unique id. 191 | function NetMonitor.CapturedMessage:WriteEntity(ent) 192 | self.data[#self.data + 1] = {type = "ENTITY", value = ent:EntIndex()} 193 | end 194 | 195 | -- Write a float to the captured message's data. 196 | function NetMonitor.CapturedMessage:WriteFloat(float) 197 | self.data[#self.data + 1] = {type = "FLOAT", value = float} 198 | end 199 | 200 | -- Write an int to the captured message's data. 201 | function NetMonitor.CapturedMessage:WriteInt(int, bits) 202 | self.data[#self.data + 1] = {type = "INT", value = int} 203 | end 204 | 205 | -- Write a matrix to the captured message's data. 206 | function NetMonitor.CapturedMessage:WriteMatrix(matrix) 207 | self.data[#self.data + 1] = {type = "MATRIX", value = matrix} 208 | end 209 | 210 | -- Write a normalized vector to the captured message's data. 211 | function NetMonitor.CapturedMessage:WriteNormal(normal) 212 | self.data[#self.data + 1] = {type = "NORMAL", value = normal} 213 | end 214 | 215 | -- Write a string to the captured message's data. 216 | function NetMonitor.CapturedMessage:WriteString(str) 217 | self.data[#self.data + 1] = {type = "STRING", value = str} 218 | end 219 | 220 | -- Write a table to the captured message's data. 221 | -- If you do this, you're a bad person. 222 | function NetMonitor.CapturedMessage:WriteTable(tbl) 223 | self.data[#self.data + 1] = {type = "TABLE", value = tbl} 224 | end 225 | 226 | -- Write a matrix to the captured message's data. 227 | function NetMonitor.CapturedMessage:WriteMatrix(matrix) 228 | self.data[#self.data + 1] = {type = "MATRIX", value = matrix} 229 | end 230 | 231 | -- Write an unsigned int to the captured message's data. 232 | function NetMonitor.CapturedMessage:WriteUInt(uint, bits) 233 | self.data[#self.data + 1] = {type = "UINT", value = uint} 234 | end 235 | 236 | -- Write a vector to the captured message's data. 237 | function NetMonitor.CapturedMessage:WriteVector(vec) 238 | self.data[#self.data + 1] = {type = "VECTOR", value = vec} 239 | end 240 | 241 | -- Write a signed byte (umsg char) to the captured message's data 242 | function NetMonitor.CapturedMessage:WriteChar(char) 243 | self.data[#self.data + 1] = {type = "CHAR", value = char} 244 | end 245 | 246 | -- Write a long to the captured message's data 247 | function NetMonitor.CapturedMessage:WriteLong(long) 248 | self.data[#self.data + 1] = {type = "LONG", value = long} 249 | end 250 | 251 | -- Write a short to the captured message's data 252 | function NetMonitor.CapturedMessage:WriteShort(short) 253 | self.data[#self.data + 1] = {type = "SHORT", value = short} 254 | end 255 | 256 | -- INTERNAL: you shouldn't use this. 257 | function NetMonitor.CapturedMessage:DumpRemainingData(bits) 258 | -- If a net message wasn't completely read, we'll be able to dump the rest here. 259 | -- If this occurs, this means you're probably wasting bandwidth. 260 | if !bits or bits == 0 then return end 261 | 262 | local bitStr = "" 263 | while bits > 0 do 264 | bitStr = bitStr .. tostring(net.ReadBit()) 265 | bits = bits - 1 266 | end 267 | 268 | if bytes and bytes > 0 then 269 | self.data[#self.data + 1] = {type = "DUMP", value = bitStr} 270 | end 271 | end 272 | 273 | setmetatable(NetMonitor.CapturedMessage, {__call = NetMonitor.CapturedMessage.new}) -------------------------------------------------------------------------------- /lua/glua-netmon/cl_config.lua: -------------------------------------------------------------------------------- 1 | -- Some keys here may also be in the server config, so that you can have different options whether it's clientside or serverside. 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.Config = NetMonitor.Config or {} 5 | 6 | -- If true, only addons that registered themselves using the netmon-addon-autoreg.lua utility will be monitored. 7 | NetMonitor.Config.OnlyMonitorRegisteredAddons = false 8 | 9 | -- If true, the net monitor will automatically grab all gamemode files and register them. 10 | NetMonitor.Config.AutoRegisterGamemodeFiles = true 11 | 12 | -- If true, the net monitor will automatically grab all addon files and register them. 13 | -- This will take quite a bit of memory since it'll keep track of all lua files in the garrysmod/addons folder. 14 | -- Workshop addons cannot be registered. 15 | NetMonitor.Config.AutoRegisterAddonFiles = false 16 | 17 | MsgC(Color(0, 255, 0), "NetMonitor: Loaded client configuration", "\n") -------------------------------------------------------------------------------- /lua/glua-netmon/cl_net.lua: -------------------------------------------------------------------------------- 1 | NetMonitor = NetMonitor or {} 2 | NetMonitor.Networking = NetMonitor.Networking or {} 3 | NetMonitor.Networking.IsHost = NetMonitor.Networking.IsHost or false 4 | NetMonitor.Networking.Authed = NetMonitor.Networking.Authed or false 5 | 6 | NetMonitor.Networking.RegistryChunks = NetMonitor.Networking.RegistryChunks or {} 7 | 8 | local function ReceiveInterfacePermission(len) 9 | NetMonitor.Networking.Authed = net.ReadBool() 10 | NetMonitor.Networking.IsHost = net.ReadBool() 11 | 12 | if not NetMonitor.Networking.Authed then 13 | NetMonitor.Utils.ChatError("Permission denied!") 14 | return 15 | end 16 | 17 | NetMonitor.Interface.Open() 18 | end 19 | 20 | net.Receive("Netmon.GiveInterfacePermission", ReceiveInterfacePermission) 21 | 22 | local lastRequest = nil 23 | 24 | local function RequestInterfacePermission() 25 | if NetMonitor.Interface.Main then return end 26 | 27 | if lastRequest and CurTime() - lastRequest < 10 then return end 28 | lastRequest = CurTime() 29 | 30 | net.Start("Netmon.RequestInterfacePermission") 31 | net.SendToServer() 32 | end 33 | 34 | concommand.Add("netmon_menu", RequestInterfacePermission, nil, "Opens the network monitor interface.") 35 | 36 | local function ReceiveRegistryChunk(len) 37 | local chunk = net.ReadBinaryChunk() 38 | NetMonitor.Networking.RegistryChunks[ #NetMonitor.Networking.RegistryChunks + 1 ] = chunk 39 | 40 | if chunk:GetId() == chunk:GetAmount() then 41 | NetMonitor.Registry.UpdateFromChunks(NetMonitor.Networking.RegistryChunks) 42 | NetMonitor.Registry.RegistryChunks = {} 43 | 44 | if NetMonitor.Interface.Registry.SyncButton then 45 | NetMonitor.Interface.Registry.SyncButton:SetEnabled(true) 46 | NetMonitor.Interface.StatusLabel:SetText("NetMonitor: Synchronized registry") 47 | NetMonitor.Interface.StatusBar:SetFraction( 1 ) 48 | end 49 | else 50 | if NetMonitor.Interface.StatusBar then 51 | NetMonitor.Interface.StatusBar:SetFraction( 0.6 ) 52 | end 53 | end 54 | end 55 | 56 | net.Receive("Netmon.SendRegistryChunk", ReceiveRegistryChunk) 57 | 58 | function NetMonitor.Networking.RequestRegistry() 59 | net.Start("Netmon.RequestRegistry") 60 | net.SendToServer() 61 | end -------------------------------------------------------------------------------- /lua/glua-netmon/hooks.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | local function OnNetMessageDiscarded(msg, funcInfo) 4 | MsgC(Color(255, 90, 90), "NetMonitor: The discarded message '", msg:GetName(), "' was started in file '", funcInfo and funcInfo.source or "NO SOURCE", "' at line ", funcInfo and funcInfo.currentline or "NO LINE") 5 | MsgC(Color(255, 90, 90), "NetMonitor: this means you likely forgot to send the message before starting another one.") 6 | end 7 | 8 | hook.Add("OnNetMessageDiscarded", "NetDiscarded", OnNetMessageDiscarded) -------------------------------------------------------------------------------- /lua/glua-netmon/interfaces/cl_clienttab.lua: -------------------------------------------------------------------------------- 1 | NetMonitor = NetMonitor or {} 2 | NetMonitor.Interface = NetMonitor.Interface or {} 3 | 4 | NetMonitor.Interface.Client = NetMonitor.Interface.Client or {} 5 | NetMonitor.Interface.Client.Main = NetMonitor.Interface.Client.Main or nil 6 | 7 | NetMonitor.Interface.Client.Captures = NetMonitor.Interface.Client.Captures or {} 8 | NetMonitor.Interface.Client.Captures.Main = NetMonitor.Interface.Client.Captures.Main or nil 9 | NetMonitor.Interface.Client.Captures.List = NetMonitor.Interface.Client.Captures.List or nil 10 | NetMonitor.Interface.Client.Captures.Delete = NetMonitor.Interface.Client.Captures.Delete or nil 11 | 12 | function NetMonitor.Interface.Client.Create(parent) 13 | local main = vgui.Create( "DPropertySheet", parent) 14 | main:Dock( FILL ) 15 | main:AddSheet("Captures", NetMonitor.Interface.Client.CreateCapturesTab(main), "icon16/disk_multiple.png") 16 | main:AddSheet("Profiling", vgui.Create( "DPanel", main ), "icon16/zoom.png") 17 | main:AddSheet("Stats", vgui.Create( "DPanel", main ), "icon16/chart_line.png") 18 | main:AddSheet("Messages", vgui.Create( "DPanel", main ), "icon16/transmit.png") 19 | 20 | return main 21 | end 22 | 23 | local function RefreshCapturesList() 24 | NetMonitor.Interface.Client.Captures.List:Clear() 25 | 26 | local files, _ = file.Find( "netmon/client/*.netdat", "DATA" ) 27 | for _, f in ipairs(files) do 28 | local path = "netmon/client/" .. f 29 | local date = os.date( "%d/%m/%y at %H:%M:%S", file.Time( path, "DATA")) 30 | local size = string.NiceSize( file.Size( path, "DATA")) 31 | 32 | NetMonitor.Interface.Client.Captures.List:AddLine( f, date, size ) 33 | end 34 | 35 | NetMonitor.Interface.Client.Captures.List:SortByColumn( 2, true ) 36 | NetMonitor.Interface.Client.Captures.Delete:SetEnabled( false ) 37 | end 38 | 39 | function NetMonitor.Interface.Client.CreateCapturesTab(parent) 40 | local captures = vgui.Create( "DPanel", parent) 41 | 42 | local label = vgui.Create( "DLabel", captures) 43 | label:Dock( TOP ) 44 | label:DockMargin( 8, 8, 8, 0 ) 45 | label:SetText( "Network profiling data can be found in /garrysmod/data/netmon/client/" ) 46 | label:SetTextColor( Color(0, 0, 0) ) 47 | 48 | local captureList = vgui.Create( "DListView", captures) 49 | captureList:Dock( FILL ) 50 | captureList:DockMargin( 8, 8, 8, 0 ) 51 | captureList:AddColumn( "Name" ) 52 | captureList:AddColumn( "Date" ) 53 | captureList:AddColumn( "Size" ) 54 | 55 | function captureList:OnRowSelected(num, row) 56 | NetMonitor.Interface.Client.Captures.Delete:SetEnabled(true) 57 | end 58 | 59 | local buttonPanel = vgui.Create( "DPanel", captures ) 60 | buttonPanel:Dock( BOTTOM ) 61 | buttonPanel:SetHeight( 48 ) 62 | 63 | local refreshButton = vgui.Create( "DButton", buttonPanel ) 64 | refreshButton:Dock( LEFT ) 65 | refreshButton:DockMargin( 8, 8, 8, 8 ) 66 | refreshButton:SetText( "Refresh" ) 67 | refreshButton:SizeToContentsX( 16 ) 68 | 69 | refreshButton.DoClick = RefreshCapturesList 70 | 71 | local loadButton = vgui.Create( "DButton", buttonPanel ) 72 | loadButton:SetEnabled( false ) 73 | loadButton:Dock( LEFT ) 74 | loadButton:DockMargin( 0, 8, 8, 8 ) 75 | loadButton:SetText( "Load" ) 76 | loadButton:SizeToContentsX( 16 ) 77 | 78 | local unloadButton = vgui.Create( "DButton", buttonPanel ) 79 | unloadButton:SetEnabled( false ) 80 | unloadButton:Dock( LEFT ) 81 | unloadButton:DockMargin( 0, 8, 8, 8 ) 82 | unloadButton:SetText( "Unload" ) 83 | unloadButton:SizeToContentsX( 16 ) 84 | 85 | local deleteButton = vgui.Create( "DButton", buttonPanel ) 86 | deleteButton:SetEnabled( false ) 87 | deleteButton:Dock( LEFT ) 88 | deleteButton:DockMargin( 0, 8, 8, 8 ) 89 | deleteButton:SetText( "Delete" ) 90 | deleteButton:SizeToContentsX( 16 ) 91 | 92 | function deleteButton:DoClick() 93 | local _, selected = NetMonitor.Interface.Client.Captures.List:GetSelectedLine() 94 | if not selected then 95 | self:SetEnabled(false) 96 | return 97 | end 98 | 99 | local path = "netmon/client/" .. selected:GetValue(1) 100 | if file.Exists(path, "DATA") then 101 | file.Delete(path) 102 | end 103 | 104 | RefreshCapturesList() 105 | end 106 | 107 | NetMonitor.Interface.Client.Captures.Main = captures 108 | NetMonitor.Interface.Client.Captures.List = captureList 109 | NetMonitor.Interface.Client.Captures.Delete = deleteButton 110 | 111 | RefreshCapturesList() 112 | return captures 113 | end 114 | 115 | function NetMonitor.Interface.Client.CreateProfilingTab(parent) 116 | end -------------------------------------------------------------------------------- /lua/glua-netmon/interfaces/cl_interface.lua: -------------------------------------------------------------------------------- 1 | NetMonitor = NetMonitor or {} 2 | NetMonitor.Interface = NetMonitor.Interface or {} 3 | 4 | -- Derma 5 | NetMonitor.Interface.Main = NetMonitor.Interface.Main or nil 6 | NetMonitor.Interface.StatusBar = NetMonitor.Interface.StatusBar or nil 7 | NetMonitor.Interface.StatusLabel = NetMonitor.Interface.StatusLabel or nil 8 | 9 | function NetMonitor.Interface.Open() 10 | if not NetMonitor.Networking.Authed or NetMonitor.Interface.Main then return end 11 | 12 | local interface = vgui.Create("DFrame") 13 | interface.OnClose = function() 14 | NetMonitor.Interface.Main = nil 15 | NetMonitor.Interface.StatusBar = nil 16 | NetMonitor.Interface.StatusLabel = nil 17 | 18 | NetMonitor.Interface.Registry.Tab = nil 19 | NetMonitor.Interface.Registry.SyncButton = nil 20 | NetMonitor.Interface.Registry.AddonsList = nil 21 | NetMonitor.Interface.Registry.FileList = nil 22 | NetMonitor.Interface.Registry.Search = nil 23 | end 24 | 25 | local w, h = NetMonitor.Utils.ClampToScreen(1024, 512) 26 | interface:SetSize( w, h ) 27 | interface:Center() 28 | 29 | interface:SetMinWidth( 512 ) 30 | interface:SetMinHeight( 256 ) 31 | 32 | interface.btnMaxim:SetDisabled( false ) 33 | interface.btnMaxim.DoClick = function() 34 | interface:SetSize( ScrW(), ScrH() ) 35 | interface:SetPos( 0, 0 ) 36 | end 37 | 38 | interface.btnMinim:SetDisabled( false ) 39 | interface.btnMinim.DoClick = function() 40 | interface:SetSize( 512, 256 ) 41 | end 42 | 43 | interface:SetTitle( "Net Monitor" ) 44 | interface:SetVisible(true) 45 | interface:SetDraggable( true ) 46 | interface:ShowCloseButton( true ) 47 | interface:SetSizable( true ) 48 | interface:SetScreenLock( true ) 49 | interface:MakePopup() 50 | 51 | local tabs = vgui.Create("DPropertySheet", interface) 52 | tabs:AddSheet("Client", NetMonitor.Interface.Client.Create(tabs), "icon16/monitor.png") 53 | tabs:AddSheet("Server", vgui.Create("DPanel", tabs), "icon16/server.png") 54 | tabs:AddSheet("Registry", NetMonitor.Interface.Registry.Create(tabs), "icon16/table_multiple.png") 55 | tabs:Dock( FILL ) 56 | 57 | local footer = vgui.Create( "DPanel", interface) 58 | footer.Paint = function() end 59 | footer:Dock( BOTTOM ) 60 | footer:DockMargin( 0, 4, 0, 0 ) 61 | 62 | local statusBar = vgui.Create( "DProgress", footer ) 63 | statusBar:SetFraction( 0 ) 64 | statusBar:SetSize( 96, 24 ) 65 | statusBar:Dock( LEFT ) 66 | 67 | local status = vgui.Create( "DLabel", footer) 68 | status:Dock( LEFT ) 69 | status:DockMargin( 8, 0, 0, 0 ) 70 | status:SetText("NetMonitor: Ready") 71 | status:SizeToContentsX() 72 | 73 | local oldFunc = status.SetText 74 | function status:SetText(txt) 75 | oldFunc(self, txt) 76 | self:SizeToContentsX() 77 | end 78 | 79 | NetMonitor.Interface.Main = interface 80 | NetMonitor.Interface.StatusBar = statusBar 81 | NetMonitor.Interface.StatusLabel = status 82 | end -------------------------------------------------------------------------------- /lua/glua-netmon/interfaces/cl_registrytab.lua: -------------------------------------------------------------------------------- 1 | NetMonitor = NetMonitor or {} 2 | NetMonitor.Interface = NetMonitor.Interface or {} 3 | NetMonitor.Interface.Registry = NetMonitor.Interface.Registry or {} 4 | 5 | NetMonitor.Interface.Registry.Tab = NetMonitor.Interface.Registry.Tab or nil 6 | NetMonitor.Interface.Registry.SyncButton = NetMonitor.Interface.Registry.SyncButton or nil 7 | NetMonitor.Interface.Registry.Search = NetMonitor.Interface.Registry.Search or nil 8 | NetMonitor.Interface.Registry.AddonsList = NetMonitor.Interface.Registry.AddonsList or nil 9 | NetMonitor.Interface.Registry.FileList = NetMonitor.Interface.Registry.FileList or nil 10 | 11 | local function UpdateRegistryDisplay(refreshAddons, refreshFiles) 12 | if not NetMonitor.Interface.Registry.AddonsList or not NetMonitor.Interface.Registry.FileList then return end 13 | 14 | refreshFiles = refreshAddons or refreshFiles 15 | 16 | if refreshAddons then NetMonitor.Interface.Registry.AddonsList:Clear() end 17 | if refreshFiles then NetMonitor.Interface.Registry.FileList:Clear() end 18 | 19 | local isFirst = true 20 | local addedLines = {} 21 | 22 | local _, selected = NetMonitor.Interface.Registry.AddonsList:GetSelectedLine() 23 | local selectedName = selected and selected:GetValue( 1 ) or nil 24 | local searchTerm = NetMonitor.Interface.Registry.Search:GetValue() 25 | 26 | searchTerm = string.TrimLeft(searchTerm) 27 | searchTerm = string.TrimRight(searchTerm) 28 | 29 | -- Limited to 512 elements. 30 | local amount = 0 31 | if selectedName == "*" or selectedName == nil then 32 | local isFirst = true 33 | for addon, files in pairs(NetMonitor.Registry.AddonFiles) do 34 | if refreshAddons then 35 | if isFirst then 36 | NetMonitor.Interface.Registry.AddonsList:AddLine("*") 37 | NetMonitor.Interface.Registry.AddonsList:SelectFirstItem() 38 | local _, all = NetMonitor.Interface.Registry.AddonsList:GetSelectedLine() 39 | all:SetToolTip("Displays all registered lua files, limited to 512 files.") 40 | 41 | isFirst = false 42 | end 43 | 44 | NetMonitor.Interface.Registry.AddonsList:AddLine(addon) 45 | end 46 | 47 | if refreshFiles then 48 | for _, path in ipairs(files) do 49 | if NetMonitor.Interface.Registry.Search.ignore or NetMonitor.Utils.StringMatchesSearch(searchTerm:lower(), path) then 50 | if amount < 512 then 51 | NetMonitor.Interface.Registry.FileList:AddLine(path) 52 | amount = amount + 1 53 | end 54 | end 55 | end 56 | end 57 | end 58 | return 59 | end 60 | 61 | for addon, files in pairs(NetMonitor.Registry.AddonFiles) do 62 | if not addedLines[addon] and refreshAddons then 63 | NetMonitor.Interface.Registry.AddonsList:AddLine(addon) 64 | addedLines[addon] = true 65 | end 66 | 67 | if refreshAddons and isFirst then 68 | NetMonitor.Interface.Registry.AddonsList:SelectFirstItem() 69 | isFirst = false 70 | 71 | for _, path in ipairs(files) do 72 | if NetMonitor.Interface.Registry.Search.ignore or NetMonitor.Utils.StringMatchesSearch(searchTerm:lower(), path) then 73 | NetMonitor.Interface.Registry.FileList:AddLine(path) 74 | end 75 | end 76 | elseif refreshFiles and addon == selectedName then 77 | for _, path in ipairs(files) do 78 | if NetMonitor.Interface.Registry.Search.ignore or NetMonitor.Utils.StringMatchesSearch(searchTerm:lower(), path) then 79 | NetMonitor.Interface.Registry.FileList:AddLine(path) 80 | end 81 | end 82 | end 83 | end 84 | 85 | end 86 | 87 | hook.Add("OnNetRegistryUpdated", "NetmonInterfaceUpdateReg", function() UpdateRegistryDisplay(true, true) end) 88 | 89 | function NetMonitor.Interface.Registry.Create(parent) 90 | local registry = vgui.Create( "DPanel", parent, "Registry" ) 91 | local leftPanel = vgui.Create( "DPanel", registry) 92 | 93 | -- Left side of the registry will be a list of addons 94 | local syncButton = vgui.Create( "DButton", leftPanel ) 95 | syncButton:SetText( "Synchronize Registry" ) 96 | syncButton:Dock( TOP ) 97 | syncButton:DockMargin( 8, 8, 8, 8 ) 98 | 99 | function syncButton:DoClick() 100 | NetMonitor.Networking.RequestRegistry() 101 | self:SetEnabled(false) 102 | NetMonitor.Interface.StatusLabel:SetText("NetMonitor: Requesting registry from the server...") 103 | NetMonitor.Interface.StatusBar:SetFraction( 0.3 ) 104 | end 105 | 106 | local addonList = vgui.Create( "DListView", leftPanel ) 107 | addonList:Dock( FILL ) 108 | addonList:DockMargin( 8, 0, 8, 8 ) 109 | addonList:AddColumn( "Sources" ) 110 | 111 | local onClickLine = addonList.OnClickLine 112 | 113 | function addonList:OnClickLine(line, isSelected) 114 | onClickLine(self, line, isSelected) 115 | UpdateRegistryDisplay(false, true) 116 | end 117 | 118 | -- Disable synchronizing the registry if you're the server host. 119 | if game.SinglePlayer() or NetMonitor.Networking.IsHost then 120 | syncButton:SetEnabled( false ) 121 | syncButton:SetTooltip( "Cannot synchronize registry when you're the host: you already have everything." ) 122 | else 123 | syncButton:SetTooltip( "Request the server's registry, you only need to do this once." ) 124 | end 125 | 126 | local rightPanel = vgui.Create( "DPanel", registry ) 127 | 128 | local searchBar = vgui.Create( "DTextEntry", rightPanel ) 129 | searchBar:SetText(" Search...") 130 | searchBar:SetTooltip( "Search the file paths for the specified string, supports patterns.") 131 | searchBar:Dock( TOP ) 132 | searchBar:DockMargin( 8, 8, 8, 8 ) 133 | searchBar.ignore = true 134 | 135 | function searchBar:OnGetFocus() 136 | self:SetValue("") 137 | end 138 | 139 | function searchBar:OnLoseFocus() 140 | local str = self:GetValue() 141 | str = string.Replace(str, " ", "") 142 | 143 | if #str == 0 then 144 | self:SetText(" Search...") 145 | self.ignore = true 146 | end 147 | end 148 | 149 | function searchBar:OnChange() 150 | local value = self:GetValue() 151 | value = string.TrimLeft(value) 152 | value = string.TrimRight(value) 153 | 154 | searchBar.ignore = #value == 0 155 | UpdateRegistryDisplay(false, true) 156 | end 157 | 158 | local pathList = vgui.Create( "DListView", rightPanel) 159 | pathList:Dock( FILL ) 160 | pathList:DockMargin( 8, 2, 8, 8 ) 161 | pathList:AddColumn( "File Path" ) 162 | pathList:SetMultiSelect( false ) 163 | 164 | local divider = vgui.Create( "DHorizontalDivider", registry ) 165 | divider:Dock( FILL ) 166 | divider:SetLeft( leftPanel ) 167 | divider:SetRight( rightPanel ) 168 | divider:SetDividerWidth( 8 ) 169 | divider:SetLeftMin( 196 ) 170 | 171 | pathList.OnSizeChanged = function(newW, newH) 172 | local x, y = divider:GetSize() 173 | divider:SetRightMin(x - 256) 174 | end 175 | 176 | NetMonitor.Interface.Registry.Tab = registry 177 | NetMonitor.Interface.Registry.AddonsList = addonList 178 | NetMonitor.Interface.Registry.FileList = pathList 179 | NetMonitor.Interface.Registry.SyncButton = syncButton 180 | NetMonitor.Interface.Registry.Search = searchBar 181 | 182 | -- Populate the registry list 183 | UpdateRegistryDisplay(true, true) 184 | 185 | return registry 186 | end -------------------------------------------------------------------------------- /lua/glua-netmon/netmon.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.Registry = NetMonitor.Registry or {} 5 | NetMonitor.CurrentMessage = NetMonitor.CurrentMessage or nil 6 | NetMonitor.CurrentMessageFuncInfo = NetMonitor.CurrentMessageFuncInfo or nil 7 | NetMonitor.CurrentMessageCount = NetMonitor.CurrentMessageCount or 0 8 | NetMonitor.CurrentMessageIncomingLen = NetMonitor.CurrentMessageIncomingLen or 0 9 | NetMonitor.CurrentMessageStartBits = NetMonitor.CurrentMessageStartBits or 0 10 | 11 | NetMonitor.NetOverrides = NetMonitor.NetOverrides or {} 12 | NetMonitor.UserMessageOverrides = NetMonitor.UserMessageOverrides or {} 13 | 14 | -- Change these flags with info you need, the less you have the less expensive it is. 15 | -- Please at least keep the 'lS' flags to let the utility work correctly. 16 | -- See https://wiki.facepunch.com/gmod/debug.getinfo 17 | NetMonitor.DebugInfoFlags = NetMonitor.DebugInfoFlags or "lS" 18 | NetMonitor.DebugMode = NetMonitor.DebugMode or false 19 | 20 | local errorColor = Color(255, 90, 90) 21 | 22 | local function DebugMsg(msg) 23 | if NetMonitor.DebugMode then MsgC(Color(0, 102, 255), "NetMonitor: ", msg, "\n") end 24 | end 25 | 26 | -- This is called when a net.receive() hook starts a notify that the old one is over, or after a net.receive() hook if we're still reading. 27 | local function FinishReceivedMessage(msg, funcInfo, bitsLeft) 28 | DebugMsg("Finishing received message") 29 | if bitsLeft > 0 and not msg:IsUserMessage() then 30 | msg:DumpRemainingData(bitsLeft) 31 | msg:SetWastedBits(bitsLeft) 32 | hook.Run("OnNetMessageCaptured", msg, funcInfo) 33 | hook.Run("OnNetMessageDumpedData", msg, funcInfo) 34 | return 35 | end 36 | 37 | hook.Run("OnNetMessageCaptured", msg, funcInfo) 38 | end 39 | 40 | -- Message receiving/sending function overrides 41 | -- Can't really keep track of the old incoming function since it reads a header that we need to get the message name. 42 | -- So guess we'll just have to copy the code and just add our stuff that way. 43 | function net.Incoming(len, client) 44 | local i = net.ReadHeader() 45 | local strName = util.NetworkIDToString( i ) 46 | 47 | if ( !strName ) then return end 48 | 49 | DebugMsg("Received net message: " .. strName) 50 | 51 | -- Setup a new captured message to read data into it. 52 | -- Check that there is no current message left. If there is, this means a network message was started but not sent. 53 | if NetMonitor.CurrentMessage != nil and !NetMonitor.CurrentMessage:IsReceived() then 54 | DebugMsg("Already had a net message, discarded.") 55 | NetMonitor.CurrentMessage:SetMode("Discarded") 56 | hook.Run("OnNetMessageDiscarded", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 57 | NetMonitor.CurrentMessage = nil 58 | NetMonitor.CurrentMessageFuncInfo = nil 59 | end 60 | 61 | NetMonitor.CurrentMessage = NetMonitor.CapturedMessage(strName) 62 | NetMonitor.CurrentMessage:SetMode("Received") 63 | NetMonitor.CurrentMessage:SetBits(len - 16) 64 | 65 | if SERVER then NetMonitor.CurrentMessage:SetSender(client) end 66 | 67 | local func = net.Receivers[ strName:lower() ] 68 | if ( !func ) then 69 | DebugMsg("Message has no hooked function.") 70 | -- Still run the message received hook. However, since no function is there to read anything, we won't see what exactly was sent. 71 | -- If there's still something to read, we could just dump them into binary data. 72 | len = len - 16 73 | if len > 0 then 74 | DebugMsg("Message has bytes left to dump.") 75 | NetMonitor.CurrentMessage:DumpRemainingData(len) 76 | NetMonitor.CurrentMessage:SetMode("Ignored") 77 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, nil) 78 | hook.Run("OnNetMessageIgnored", NetMonitor.CurrentMessage, nil) 79 | hook.Run("OnNetMessageDumpedData", NetMonitor.CurrentMessage, nil) 80 | return 81 | end 82 | 83 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, nil) 84 | hook.Run("OnNetMessageIgnored", NetMonitor.CurrentMessage, nil) 85 | NetMonitor.CurrentMessage = nil 86 | return 87 | end 88 | 89 | -- len includes the 16 bit int which told us the message name 90 | len = len - 16 91 | 92 | local currentMessage = NetMonitor.CurrentMessageCount 93 | local _, bits = net.BytesLeft() 94 | 95 | NetMonitor.CurrentMessageIncomingLen = len 96 | NetMonitor.CurrentMessageStartBits = bits 97 | 98 | DebugMsg("Calling message hook. Len: " .. len) 99 | func( len, client ) 100 | DebugMsg("Message hook finished!") 101 | 102 | -- If a new message was created by the receive func, don't do anything 103 | if currentMessage != NetMonitor.CurrentMessageCount then return end 104 | 105 | _, bits = net.BytesLeft() 106 | local bitsLeft = NetMonitor.CurrentMessageIncomingLen - (NetMonitor.CurrentMessageStartBits - bits) 107 | 108 | FinishReceivedMessage(NetMonitor.CurrentMessage, nil, bitsLeft) 109 | NetMonitor.CurrentMessage = nil 110 | NetMonitor.CurrentMessageCount = NetMonitor.CurrentMessageCount + 1 111 | end 112 | 113 | NetMonitor.UserMessageOverrides.IncomingMessage = NetMonitor.UserMessageOverrides.IncomingMessage or usermessage.IncomingMessage 114 | function usermessage.IncomingMessage(name, msg) 115 | -- Check that there is no current message left. If there is, this means a network message was started but not sent. 116 | if NetMonitor.CurrentMessage != nil and !NetMonitor.CurrentMessage:IsReceived() then 117 | DebugMsg("Already had a net message, discarded.") 118 | NetMonitor.CurrentMessage:SetMode("Discarded") 119 | hook.Run("OnNetMessageDiscarded", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 120 | NetMonitor.CurrentMessage = nil 121 | NetMonitor.CurrentMessageFuncInfo = nil 122 | end 123 | 124 | -- Prepare the message for reading. 125 | NetMonitor.CurrentMessage = NetMonitor.CapturedMessage(name) 126 | NetMonitor.CurrentMessage:SetMode("Received") 127 | NetMonitor.CurrentMessage:SetDeprecated() 128 | 129 | local currentMessage = NetMonitor.CurrentMessageCount 130 | 131 | NetMonitor.UserMessageOverrides.IncomingMessage(name, msg) 132 | 133 | if currentMessage != NetMonitor.CurrentMessageCount then return end 134 | 135 | FinishReceivedMessage(NetMonitor.CurrentMessage, nil, 0) 136 | NetMonitor.CurrentMessageCount = NetMonitor.CurrentMessageCount + 1 137 | NetMonitor.CurrentMessage = nil 138 | end 139 | 140 | NetMonitor.NetOverrides.Receive = NetMonitor.NetOverrides.Receive or net.Receive 141 | function net.Receive(messageName, callback) 142 | NetMonitor.NetOverrides.Receive(messageName, callback) 143 | 144 | local info = debug.getinfo(2, NetMonitor.DebugInfoFlags) 145 | if info.short_src and info.short_src != "lua_run" and info.short_src != "lua_run_cl" then 146 | NetMonitor.Registry.RegisterReceiveFile(messageName, info.short_src) 147 | end 148 | end 149 | 150 | NetMonitor.NetOverrides.Start = NetMonitor.NetOverrides.Start or net.Start 151 | function net.Start(msgName, unreliable) 152 | unreliable = unreliable or false 153 | 154 | -- If this was called in a receive callback, notify the monitor that the old message is over. 155 | if NetMonitor.CurrentMessage != nil and NetMonitor.CurrentMessage:IsReceived() then 156 | if not NetMonitor.CurrentMessage:IsUserMessage() then 157 | local _, bits = net.BytesLeft() 158 | local bitsLeft = bits and NetMonitor.CurrentMessageIncomingLen - (NetMonitor.CurrentMessageStartBits - bits) or 0 159 | FinishReceivedMessage(NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo, bitsLeft) 160 | else 161 | FinishReceivedMessage(NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo, 0) 162 | end 163 | 164 | NetMonitor.CurrentMessage = nil 165 | NetMonitor.CurrentMessageFuncInfo = nil 166 | end 167 | 168 | NetMonitor.NetOverrides.Start(msgName, unreliable) -- This will halt execution on error, so we'll execute it now. 169 | NetMonitor.CurrentMessageCount = NetMonitor.CurrentMessageCount + 1 170 | 171 | -- Check if an outgoing captured message is still being captured. 172 | if NetMonitor.CurrentMessage != nil and !NetMonitor.CurrentMessage:IsReceived() then 173 | DebugMsg("Started a message while another was already running.") 174 | hook.Run("OnNetMessageDiscarded", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 175 | NetMonitor.CurrentMessage = nil 176 | NetMonitor.CurrentMessageFuncInfo = nil 177 | end 178 | 179 | DebugMsg("Starting a new message!") 180 | NetMonitor.CurrentMessage = NetMonitor.CapturedMessage(msgName, unreliable) 181 | NetMonitor.CurrentMessageFuncInfo = debug.getinfo(2, NetMonitor.DebugInfoFlags) 182 | hook.Run("OnNetMessageStarted", msgName, NetMonitor.CurrentMessageFuncInfo) 183 | end 184 | 185 | if SERVER then -- Server only net functions 186 | 187 | -- Deprecated umsg library. 188 | -- Seriously, please stop using that pile of garbage. 189 | NetMonitor.UserMessageOverrides.Start = NetMonitor.UserMessageOverrides.Start or umsg.Start 190 | function umsg.Start(name, filter) 191 | if NetMonitor.CurrentMessage != nil and NetMonitor.CurrentMessage:IsReceived() then 192 | if not NetMonitor.CurrentMessage:IsUserMessage() then 193 | local _, bits = net.BytesLeft() 194 | local bitsLeft = NetMonitor.CurrentMessageIncomingLen - (NetMonitor.CurrentMessageStartBits - bits) 195 | FinishReceivedMessage(NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo, bitsLeft) 196 | else 197 | FinishReceivedMessage(NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo, 0) 198 | end 199 | 200 | NetMonitor.CurrentMessage = nil 201 | NetMonitor.CurrentMessageFuncInfo = nil 202 | end 203 | 204 | NetMonitor.UserMessageOverrides.Start(name, filter) 205 | NetMonitor.CurrentMessageCount = NetMonitor.CurrentMessageCount + 1 206 | 207 | if NetMonitor.CurrentMessage != nil and !NetMonitor.CurrentMessage:IsReceived() then 208 | DebugMsg("Started a message while another was already running.") 209 | hook.Run("OnNetMessageDiscarded", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 210 | NetMonitor.CurrentMessage = nil 211 | NetMonitor.CurrentMessageFuncInfo = nil 212 | end 213 | 214 | -- Create a "deprecated" message for umsg. 215 | NetMonitor.CurrentMessage = NetMonitor.CapturedMessage(name) 216 | NetMonitor.CurrentMessage:SetDeprecated() 217 | NetMonitor.CurrentMessageFuncInfo = debug.getinfo(2, NetMonitor.DebugInfoFlags) 218 | 219 | -- Since recipients are set here, add them to the message now. 220 | if not filter then 221 | NetMonitor.CurrentMessage:SetRecipients(player.GetAll()) 222 | elseif isentity(filter) and filter:IsPlayer() then 223 | NetMonitor.CurrentMessage:SetRecipients({filter}) 224 | elseif TypeID(filter) == TYPE_RECIPIENTFILTER then 225 | NetMonitor.CurrentMessage:SetRecipients(filter:GetPlayers()) 226 | end 227 | 228 | hook.Run("OnNetMessageStarted", name, NetMonitor.CurrentMessageFuncInfo) 229 | end 230 | 231 | NetMonitor.UserMessageOverrides.End = NetMonitor.UserMessageOverrides.End or umsg.End 232 | function umsg.End() 233 | NetMonitor.UserMessageOverrides.End() 234 | 235 | NetMonitor.CurrentMessage:SetMode("Deprecated Send") 236 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 237 | NetMonitor.CurrentMessage = nil 238 | NetMonitor.CurrentMessageFuncInfo = nil 239 | end 240 | 241 | NetMonitor.NetOverrides.Send = NetMonitor.NetOverrides.Send or net.Send 242 | function net.Send(ply) 243 | local _, bits = net.BytesWritten() 244 | NetMonitor.NetOverrides.Send(ply) 245 | 246 | -- That should never happen but sure. 247 | if !NetMonitor.CurrentMessage then 248 | MsgC(errorColor, "A net message was sent, but the net monitor doesn't recall one ever starting.\n") 249 | return 250 | end 251 | 252 | NetMonitor.CurrentMessage:SetMode("Send") 253 | NetMonitor.CurrentMessage:SetBits(bits) 254 | 255 | if isentity(ply) and ply:IsPlayer() then 256 | NetMonitor.CurrentMessage:SetRecipients({ply}) 257 | elseif TypeID(ply) == TYPE_RECIPIENTFILTER then 258 | NetMonitor.CurrentMessage:SetRecipients(ply:GetPlayers()) 259 | elseif istable(ply) then 260 | NetMonitor.CurrentMessage:SetRecipients(ply) 261 | end 262 | 263 | DebugMsg("SEND") 264 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 265 | NetMonitor.CurrentMessage = nil 266 | NetMonitor.CurrentMessageFuncInfo = nil 267 | end 268 | 269 | NetMonitor.NetOverrides.SendOmit = NetMonitor.NetOverrides.SendOmit or net.SendOmit 270 | function net.SendOmit(ply) 271 | local _, bits = net.BytesWritten() 272 | NetMonitor.NetOverrides.SendOmit(ply) 273 | 274 | -- That should never happen but sure. 275 | if !NetMonitor.CurrentMessage then 276 | MsgC(errorColor, "A net message was sent, but the net monitor doesn't recall one ever starting.\n") 277 | return 278 | end 279 | 280 | NetMonitor.CurrentMessage:SetMode("SendOmit") 281 | NetMonitor.CurrentMessage:SetBits(bits) 282 | 283 | -- SendOmit switches to broadcast if the player argument is nil or empty 284 | if ply and isentity(ply) and ply:IsPlayer() then 285 | local recipients = player.GetAll() 286 | table.RemoveByValue(recipients, ply) 287 | NetMonitor.CurrentMessage:SetRecipients(recipients) 288 | elseif istable(ply) then 289 | if #ply == 0 then 290 | NetMonitor.CurrentMessage:SetMode("Broadcast") 291 | NetMonitor.CurrentMessage:SetRecipients(player.GetAll()) 292 | else 293 | local recipients = player.GetAll() 294 | for _, v in pairs(ply) do table.RemoveByValue(recipients, v) end 295 | 296 | NetMonitor.CurrentMessage:SetRecipients(recipients) 297 | end 298 | else 299 | NetMonitor.CurrentMessage:SetMode("Broadcast") 300 | NetMonitor.CurrentMessage:SetRecipients(player.GetAll()) 301 | end 302 | 303 | DebugMsg("SENDOMIT") 304 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 305 | NetMonitor.CurrentMessage = nil 306 | NetMonitor.CurrentMessageFuncInfo = nil 307 | end 308 | 309 | NetMonitor.NetOverrides.SendPAS = NetMonitor.NetOverrides.SendPAS or net.SendPAS 310 | function net.SendPAS(position) 311 | NetMonitor.NetOverrides.SendPAS(position) 312 | 313 | -- That should never happen but sure. 314 | if !NetMonitor.CurrentMessage then 315 | MsgC(errorColor, "A net message was sent, but the net monitor doesn't recall one ever starting.\n") 316 | return 317 | end 318 | 319 | local _, bits = net.BytesWritten() 320 | NetMonitor.CurrentMessage:SetMode("SendPAS") 321 | NetMonitor.CurrentMessage:SetBits(bits) 322 | 323 | -- Use a recipient filter to find all players that are in the PAS. 324 | local rf = RecipientFilter() 325 | rf:AddPAS(position) 326 | 327 | NetMonitor.CurrentMessage:SetRecipients(rf:GetPlayers()) 328 | 329 | DebugMsg("SENDPAS") 330 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 331 | NetMonitor.CurrentMessage = nil 332 | NetMonitor.CurrentMessageFuncInfo = nil 333 | end 334 | 335 | NetMonitor.NetOverrides.SendPVS = NetMonitor.NetOverrides.SendPVS or net.SendPVS 336 | function net.SendPVS(position) 337 | NetMonitor.NetOverrides.SendPVS(position) 338 | 339 | -- That should never happen but sure. 340 | if !NetMonitor.CurrentMessage then 341 | MsgC(errorColor, "A net message was sent, but the net monitor doesn't recall one ever starting.\n") 342 | return 343 | end 344 | 345 | local _, bits = net.BytesWritten() 346 | NetMonitor.CurrentMessage:SetMode("SendPVS") 347 | NetMonitor.CurrentMessage:SetBits(bits) 348 | 349 | -- Use a recipient filter to find all players that are in the PVS. 350 | local rf = RecipientFilter() 351 | rf:AddPVS(position) 352 | 353 | NetMonitor.CurrentMessage:SetRecipients(rf:GetPlayers()) 354 | 355 | DebugMsg("SENDPVS") 356 | 357 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 358 | NetMonitor.CurrentMessage = nil 359 | NetMonitor.CurrentMessageFuncInfo = nil 360 | end 361 | 362 | NetMonitor.NetOverrides.Broadcast = NetMonitor.NetOverrides.Broadcast or net.Broadcast 363 | function net.Broadcast() 364 | NetMonitor.NetOverrides.Broadcast() 365 | 366 | if !NetMonitor.CurrentMessage then 367 | MsgC(errorColor, "A net message was sent, but the net monitor doesn't recall one ever starting.\n") 368 | return 369 | end 370 | 371 | local _, bits = net.BytesWritten() 372 | NetMonitor.CurrentMessage:SetMode("Broadcast") 373 | NetMonitor.CurrentMessage:SetBits(bits or 0) 374 | NetMonitor.CurrentMessage:SetRecipients(player.GetAll()) 375 | 376 | DebugMsg("BROADCAST") 377 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 378 | NetMonitor.CurrentMessage = nil 379 | NetMonitor.CurrentMessageFuncInfo = nil 380 | end 381 | 382 | end -- Server only net functions 383 | 384 | if CLIENT then -- Client only net functions 385 | 386 | NetMonitor.NetOverrides.SendToServer = NetMonitor.NetOverrides.SendToServer or net.SendToServer 387 | function net.SendToServer() 388 | local _, bits = net.BytesWritten() 389 | NetMonitor.NetOverrides.SendToServer() 390 | 391 | if !NetMonitor.CurrentMessage then 392 | MsgC(errorColor, "A net message was sent, but the net monitor doesn't recall one ever starting.\n") 393 | return 394 | end 395 | 396 | NetMonitor.CurrentMessage:SetMode("SendToServer") 397 | NetMonitor.CurrentMessage:SetBits(bits or 0) 398 | 399 | DebugMsg("SENDTOSERVER") 400 | hook.Run("OnNetMessageCaptured", NetMonitor.CurrentMessage, NetMonitor.CurrentMessageFuncInfo) 401 | NetMonitor.CurrentMessage = nil 402 | NetMonitor.CurrentMessageFuncInfo = nil 403 | end 404 | 405 | end -- Client only net functions 406 | 407 | -- Now we override write/read functions to feed the data to the captured message. 408 | 409 | NetMonitor.NetOverrides.ReadAngle = NetMonitor.NetOverrides.ReadAngle or net.ReadAngle 410 | function net.ReadAngle() 411 | local ang = NetMonitor.NetOverrides.ReadAngle() 412 | NetMonitor.CurrentMessage:WriteAngle(ang) 413 | return ang 414 | end 415 | 416 | NetMonitor.NetOverrides.WriteAngle = NetMonitor.NetOverrides.WriteAngle or net.WriteAngle 417 | function net.WriteAngle(ang) 418 | NetMonitor.NetOverrides.WriteAngle(ang) 419 | NetMonitor.CurrentMessage:WriteAngle(ang) 420 | end 421 | 422 | NetMonitor.NetOverrides.ReadBit = NetMonitor.NetOverrides.ReadBit or net.ReadBit 423 | function net.ReadBit() 424 | local b = NetMonitor.NetOverrides.ReadBit() 425 | NetMonitor.CurrentMessage:WriteBit(b) 426 | return b 427 | end 428 | 429 | NetMonitor.NetOverrides.WriteBit = NetMonitor.NetOverrides.WriteBit or net.WriteBit 430 | function net.WriteBit(b) 431 | NetMonitor.NetOverrides.WriteBit(b) 432 | NetMonitor.CurrentMessage:WriteBit(b and 1 or 0) 433 | end 434 | 435 | NetMonitor.NetOverrides.ReadColor = NetMonitor.NetOverrides.ReadColor or net.ReadColor 436 | function net.ReadColor() 437 | local color = NetMonitor.NetOverrides.ReadColor() 438 | NetMonitor.CurrentMessage:WriteColor(color) 439 | return color 440 | end 441 | 442 | NetMonitor.NetOverrides.WriteColor = NetMonitor.NetOverrides.WriteColor or net.WriteColor 443 | function net.WriteColor(color) 444 | NetMonitor.NetOverrides.WriteColor(color) 445 | NetMonitor.CurrentMessage:WriteColor(color) 446 | end 447 | 448 | NetMonitor.NetOverrides.ReadData = NetMonitor.NetOverrides.ReadData or net.ReadData 449 | function net.ReadData(len) 450 | local data = NetMonitor.NetOverrides.ReadData(len) 451 | NetMonitor.CurrentMessage:WriteData(data, len) 452 | return data 453 | end 454 | 455 | NetMonitor.NetOverrides.WriteData = NetMonitor.NetOverrides.WriteData or net.WriteData 456 | function net.WriteData(data, len) 457 | NetMonitor.NetOverrides.WriteData(data, len) 458 | NetMonitor.CurrentMessage:WriteData(data, len) 459 | end 460 | 461 | NetMonitor.NetOverrides.ReadDouble = NetMonitor.NetOverrides.ReadDouble or net.ReadDouble 462 | function net.ReadDouble() 463 | local double = NetMonitor.NetOverrides.ReadDouble() 464 | NetMonitor.CurrentMessage:WriteDouble(double) 465 | return double 466 | end 467 | 468 | NetMonitor.NetOverrides.WriteDouble = NetMonitor.NetOverrides.WriteDouble or net.WriteDouble 469 | function net.WriteDouble(double) 470 | NetMonitor.NetOverrides.WriteDouble(double) 471 | NetMonitor.CurrentMessage:WriteDouble(double) 472 | end 473 | 474 | NetMonitor.NetOverrides.ReadEntity = NetMonitor.NetOverrides.ReadEntity or net.ReadEntity 475 | function net.ReadEntity() 476 | local ent = NetMonitor.NetOverrides.ReadEntity() 477 | NetMonitor.CurrentMessage:WriteEntity(ent) 478 | return ent 479 | end 480 | 481 | NetMonitor.NetOverrides.WriteEntity = NetMonitor.NetOverrides.WriteEntity or net.WriteEntity 482 | function net.WriteEntity(ent) 483 | NetMonitor.NetOverrides.WriteEntity(ent) 484 | NetMonitor.CurrentMessage:WriteEntity(ent) 485 | end 486 | 487 | NetMonitor.NetOverrides.ReadFloat = NetMonitor.NetOverrides.ReadFloat or net.ReadFloat 488 | function net.ReadFloat() 489 | local float = NetMonitor.NetOverrides.ReadFloat() 490 | NetMonitor.CurrentMessage:WriteFloat(float) 491 | return float 492 | end 493 | 494 | NetMonitor.NetOverrides.WriteFloat = NetMonitor.NetOverrides.WriteFloat or net.WriteFloat 495 | function net.WriteFloat(float) 496 | NetMonitor.NetOverrides.WriteFloat(float) 497 | NetMonitor.CurrentMessage:WriteFloat(float) 498 | end 499 | 500 | NetMonitor.NetOverrides.ReadInt = NetMonitor.NetOverrides.ReadInt or net.ReadInt 501 | function net.ReadInt(bits) 502 | local int = NetMonitor.NetOverrides.ReadInt(bits) 503 | NetMonitor.CurrentMessage:WriteInt(int, bits) 504 | return int 505 | end 506 | 507 | NetMonitor.NetOverrides.WriteInt = NetMonitor.NetOverrides.WriteInt or net.WriteInt 508 | function net.WriteInt(int, bits) 509 | NetMonitor.NetOverrides.WriteInt(int, bits) 510 | NetMonitor.CurrentMessage:WriteInt(int, bits) 511 | end 512 | 513 | NetMonitor.NetOverrides.ReadMatrix = NetMonitor.NetOverrides.ReadMatrix or net.ReadMatrix 514 | function net.ReadMatrix() 515 | local mat = NetMonitor.NetOverrides.ReadMatrix() 516 | NetMonitor.CurrentMessage:WriteMatrix(mat) 517 | return mat 518 | end 519 | 520 | NetMonitor.NetOverrides.WriteMatrix = NetMonitor.NetOverrides.WriteMatrix or net.WriteMatrix 521 | function net.WriteMatrix(mat) 522 | NetMonitor.NetOverrides.WriteMatrix(mat) 523 | NetMonitor.CurrentMessage:WriteMatrix(mat) 524 | end 525 | 526 | NetMonitor.NetOverrides.ReadNormal = NetMonitor.NetOverrides.ReadNormal or net.ReadNormal 527 | function net.ReadNormal() 528 | local normal = NetMonitor.NetOverrides.ReadNormal() 529 | NetMonitor.CurrentMessage:WriteNormal(normal) 530 | return normal 531 | end 532 | 533 | NetMonitor.NetOverrides.WriteNormal = NetMonitor.NetOverrides.WriteNormal or net.WriteNormal 534 | function net.WriteNormal(normal) 535 | NetMonitor.NetOverrides.WriteNormal(normal) 536 | NetMonitor.CurrentMessage:WriteNormal(normal) 537 | end 538 | 539 | NetMonitor.NetOverrides.ReadString = NetMonitor.NetOverrides.ReadString or net.ReadString 540 | function net.ReadString() 541 | local str = NetMonitor.NetOverrides.ReadString() 542 | NetMonitor.CurrentMessage:WriteString(str) 543 | return str 544 | end 545 | 546 | NetMonitor.NetOverrides.WriteString = NetMonitor.NetOverrides.WriteString or net.WriteString 547 | function net.WriteString(str) 548 | NetMonitor.NetOverrides.WriteString(str) 549 | NetMonitor.CurrentMessage:WriteString(str) 550 | end 551 | 552 | NetMonitor.NetOverrides.ReadTable = NetMonitor.NetOverrides.ReadTable or net.ReadTable 553 | function net.ReadTable() 554 | local tbl = NetMonitor.NetOverrides.ReadTable() 555 | NetMonitor.CurrentMessage:WriteTable(tbl) 556 | return tbl 557 | end 558 | 559 | NetMonitor.NetOverrides.WriteTable = NetMonitor.NetOverrides.WriteTable or net.WriteTable 560 | function net.WriteTable(tbl) 561 | NetMonitor.NetOverrides.WriteTable(tbl) 562 | NetMonitor.CurrentMessage:WriteTable(tbl) 563 | end 564 | 565 | NetMonitor.NetOverrides.ReadUInt = NetMonitor.NetOverrides.ReadUInt or net.ReadUInt 566 | function net.ReadUInt(bits) 567 | local uint = NetMonitor.NetOverrides.ReadUInt(bits) 568 | NetMonitor.CurrentMessage:WriteUInt(uint, bits) 569 | return uint 570 | end 571 | 572 | NetMonitor.NetOverrides.WriteUInt = NetMonitor.NetOverrides.WriteUInt or net.WriteUInt 573 | function net.WriteUInt(uint, bits) 574 | NetMonitor.NetOverrides.WriteUInt(uint, bits) 575 | NetMonitor.CurrentMessage:WriteUInt(uint, bits) 576 | end 577 | 578 | NetMonitor.NetOverrides.ReadVector = NetMonitor.NetOverrides.ReadVector or net.ReadVector 579 | function net.ReadVector() 580 | local vec = NetMonitor.NetOverrides.ReadVector() 581 | NetMonitor.CurrentMessage:WriteVector(vector) 582 | return vec 583 | end 584 | 585 | NetMonitor.NetOverrides.WriteVector = NetMonitor.NetOverrides.WriteVector or net.WriteVector 586 | function net.WriteVector(vector) 587 | NetMonitor.NetOverrides.WriteVector(vector) 588 | NetMonitor.CurrentMessage:WriteVector(vector) 589 | end 590 | 591 | -- UserMessage read/write functions are server only. 592 | if SERVER then 593 | 594 | NetMonitor.UserMessageOverrides.Angle = NetMonitor.UserMessageOverrides.Angle or umsg.Angle 595 | function umsg.Angle(ang) 596 | NetMonitor.UserMessageOverrides.Angle(ang) 597 | NetMonitor.CurrentMessage:WriteAngle(ang) 598 | end 599 | 600 | NetMonitor.UserMessageOverrides.Bool = NetMonitor.UserMessageOverrides.Bool or umsg.Bool 601 | function umsg.Bool(bool) 602 | NetMonitor.UserMessageOverrides.Bool(bool) 603 | NetMonitor.CurrentMessage:WriteBit(bool and 1 or 0) 604 | end 605 | 606 | NetMonitor.UserMessageOverrides.Char = NetMonitor.UserMessageOverrides.Char or umsg.Char 607 | function umsg.Char(char) 608 | NetMonitor.UserMessageOverrides.Char(char) 609 | NetMonitor.CurrentMessage:WriteChar(char) 610 | end 611 | 612 | NetMonitor.UserMessageOverrides.Entity = NetMonitor.UserMessageOverrides.Entity or umsg.Entity 613 | function umsg.Entity(ent) 614 | NetMonitor.UserMessageOverrides.Entity(ent) 615 | NetMonitor.CurrentMessage:WriteEntity(ent) 616 | end 617 | 618 | NetMonitor.UserMessageOverrides.Float = NetMonitor.UserMessageOverrides.Float or umsg.Float 619 | function umsg.Float(float) 620 | NetMonitor.UserMessageOverrides.Float(float) 621 | NetMonitor.CurrentMessage:WriteFloat(float) 622 | end 623 | 624 | NetMonitor.UserMessageOverrides.Long = NetMonitor.UserMessageOverrides.Long or umsg.Long 625 | function umsg.Long(long) 626 | NetMonitor.UserMessageOverrides.Long(long) 627 | NetMonitor.CurrentMessage:WriteLong(long) 628 | end 629 | 630 | NetMonitor.UserMessageOverrides.Short = NetMonitor.UserMessageOverrides.Short or umsg.Short 631 | function umsg.Short(short) 632 | NetMonitor.UserMessageOverrides.Short(short) 633 | NetMonitor.CurrentMessage:WriteShort(short) 634 | end 635 | 636 | NetMonitor.UserMessageOverrides.String = NetMonitor.UserMessageOverrides.String or umsg.String 637 | function umsg.String(str) 638 | NetMonitor.UserMessageOverrides.String(str) 639 | NetMonitor.CurrentMessage:WriteString(str) 640 | end 641 | 642 | NetMonitor.UserMessageOverrides.Vector = NetMonitor.UserMessageOverrides.Vector or umsg.Vector 643 | function umsg.Vector(vec) 644 | NetMonitor.UserMessageOverrides.Vector(vec) 645 | NetMonitor.CurrentMessage:WriteVector(vec) 646 | end 647 | 648 | NetMonitor.UserMessageOverrides.VectorNormal = NetMonitor.UserMessageOverrides.VectorNormal or umsg.VectorNormal 649 | function umsg.VectorNormal(vec) 650 | NetMonitor.UserMessageOverrides.VectorNormal(vec) 651 | NetMonitor.CurrentMessage:WriteNormal(vec) 652 | end 653 | 654 | end -- Usermessage Server functions 655 | 656 | -- Usermessage client-only read functions. 657 | if CLIENT then 658 | 659 | local bf_read = FindMetaTable("bf_read") 660 | 661 | NetMonitor.UserMessageOverrides.ReadAngle = NetMonitor.UserMessageOverrides.ReadAngle or bf_read.ReadAngle 662 | function bf_read:ReadAngle() 663 | local ang = NetMonitor.UserMessageOverrides.ReadAngle(self) 664 | NetMonitor.CurrentMessage:WriteAngle(ang) 665 | return ang 666 | end 667 | 668 | NetMonitor.UserMessageOverrides.ReadBool = NetMonitor.UserMessageOverrides.ReadBool or bf_read.ReadBool 669 | function bf_read:ReadBool() 670 | local bool = NetMonitor.UserMessageOverrides.ReadBool(self) 671 | NetMonitor.CurrentMessage:WriteBit(bool and 1 or 0) 672 | return bool 673 | end 674 | 675 | NetMonitor.UserMessageOverrides.ReadChar = NetMonitor.UserMessageOverrides.ReadChar or bf_read.ReadChar 676 | function bf_read:ReadChar() 677 | local char = NetMonitor.UserMessageOverrides.ReadChar(self) 678 | NetMonitor.CurrentMessage:WriteChar(char) 679 | return char 680 | end 681 | 682 | NetMonitor.UserMessageOverrides.ReadEntity = NetMonitor.UserMessageOverrides.ReadEntity or bf_read.ReadEntity 683 | function bf_read:ReadEntity() 684 | local ent = NetMonitor.UserMessageOverrides.ReadEntity(self) 685 | NetMonitor.CurrentMessage:WriteEntity(ent) 686 | return ent 687 | end 688 | 689 | NetMonitor.UserMessageOverrides.ReadFloat = NetMonitor.UserMessageOverrides.ReadFloat or bf_read.ReadFloat 690 | function bf_read:ReadFloat() 691 | local float = NetMonitor.UserMessageOverrides.ReadFloat(self) 692 | NetMonitor.CurrentMessage:WriteFloat(float) 693 | return float 694 | end 695 | 696 | NetMonitor.UserMessageOverrides.ReadLong = NetMonitor.UserMessageOverrides.ReadLong or bf_read.ReadLong 697 | function bf_read:ReadLong() 698 | local long = NetMonitor.UserMessageOverrides.ReadLong(self) 699 | NetMonitor.CurrentMessage:WriteLong(long) 700 | return long 701 | end 702 | 703 | NetMonitor.UserMessageOverrides.ReadShort = NetMonitor.UserMessageOverrides.ReadShort or bf_read.ReadShort 704 | function bf_read:ReadShort() 705 | local short = NetMonitor.UserMessageOverrides.ReadShort(self) 706 | NetMonitor.CurrentMessage:WriteShort(short) 707 | return short 708 | end 709 | 710 | NetMonitor.UserMessageOverrides.ReadString = NetMonitor.UserMessageOverrides.ReadString or bf_read.ReadString 711 | function bf_read:ReadString() 712 | local str = NetMonitor.UserMessageOverrides.ReadString(self) 713 | NetMonitor.CurrentMessage:WriteString(str) 714 | return str 715 | end 716 | 717 | NetMonitor.UserMessageOverrides.ReadVector = NetMonitor.UserMessageOverrides.ReadVector or bf_read.ReadVector 718 | function bf_read:ReadVector() 719 | local vec = NetMonitor.UserMessageOverrides.ReadVector(self) 720 | NetMonitor.CurrentMessage:WriteVector(vec) 721 | return vec 722 | end 723 | 724 | NetMonitor.UserMessageOverrides.ReadVectorNormal = NetMonitor.UserMessageOverrides.ReadVectorNormal or bf_read.ReadVectorNormal 725 | function bf_read:ReadVectorNormal() 726 | local vec = NetMonitor.UserMessageOverrides.ReadVectorNormal(self) 727 | NetMonitor.CurrentMessage:WriteVector(vec) 728 | return vec 729 | end 730 | 731 | end -- Usermessage client-only read functions. 732 | 733 | MsgC(Color(0, 255, 0), "NetMonitor: Hooked to the net and umsg libraries.", "\n") -------------------------------------------------------------------------------- /lua/glua-netmon/profiling/profiler.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.Profiler = NetMonitor.Profiler or {} 5 | NetMonitor.Profiler.Filter = NetMonitor.Profiler.Filter or NetMonitor.ProfilingFilter() 6 | 7 | 8 | MsgC(Color(0, 255, 0), "NetMonitor: Loaded profiler", "\n") -------------------------------------------------------------------------------- /lua/glua-netmon/profiling/profilingdata.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.ProfilingData = {} 5 | NetMonitor.ProfilingData.__index = NetMonitor.ProfilingData 6 | 7 | function NetMonitor.ProfilingData:new() 8 | local tbl = 9 | { 10 | meta = "ProfilingData", 11 | stats = {}, 12 | messages = {}, 13 | } 14 | 15 | setmetatable(tbl, NetMonitor.ProfilingData) 16 | return tbl 17 | end 18 | 19 | function NetMonitor.ProfilingData:AddMessage(msg) 20 | if not msg or getmetatable(msg) != NetMonitor.CapturedMessage then 21 | Error("Cannot add message to profiled data, expected CapturedMessage, got: " .. msg or "nil") 22 | return 23 | end 24 | 25 | self.messages[#self.messages + 1] = msg 26 | end 27 | 28 | setmetatable(NetMonitor.ProfilingData, {__call = NetMonitor.ProfilingData.new}) -------------------------------------------------------------------------------- /lua/glua-netmon/profiling/profilingfilter.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.ProfilingFilter = {} 5 | NetMonitor.ProfilingFilter.__index = NetMonitor.ProfilingFilter 6 | 7 | NetMonitor.ProfilingFilter.Flags = 8 | { 9 | None = 0, 10 | IncomingMessages = 1, 11 | OutgoingMessages = 2, 12 | NetLib = 4, 13 | UMsgLib = 8, 14 | SuccessfulMessages = 16, 15 | DiscardedMessages = 32, 16 | IgnoredMessages = 64, 17 | All = 255, 18 | } 19 | 20 | function NetMonitor.ProfilingFilter:new() 21 | local tbl = 22 | { 23 | flags = NetMonitor.ProfilingFilter.Flags.All, 24 | messageWhitelist = {}, 25 | messageBlacklist = {}, 26 | } 27 | 28 | setmetatable(tbl, NetMonitor.ProfilingFilter) 29 | return tbl 30 | end 31 | 32 | function NetMonitor.ProfilingFilter:DoesMessagePassFilter(msg) 33 | if not msg or getmetatable(msg) != NetMonitor.CapturedMessage then 34 | Error("Cannot check if message passes filter, expected CapturedMessage, got: " .. msg or "nil") 35 | return 36 | end 37 | 38 | if #self.messageWhitelist > 0 and not self.messageWhitelist[msg:GetName():lower()] then return false end 39 | 40 | if #self.messageBlacklist > 0 and self.messageBlacklist[msg:GetName():lower()] then return false end 41 | 42 | if not self:AllowsIncomingMessages() and msg:IsReceived() then return false end 43 | 44 | if not self:AllowsOutgoingMessages() and not msg:IsReceived() then return false end 45 | 46 | if not self:AllowsDiscardedMessages() and msg:IsDiscarded() then return false end 47 | 48 | if not self:AllowsIgnoredMessages() and msg:IsIgnored() then return false end 49 | 50 | if not self:AllowsSuccessfulMessages() and not msg:IsIgnored() and not msg:IsDiscarded() then return false end 51 | 52 | if not self:AllowsNetMessages() and not msg:IsDeprecated() then return false end 53 | 54 | if not self:AllowsUserMessages() and msg:IsDeprecated() then return false end 55 | 56 | return true 57 | end 58 | 59 | function NetMonitor.ProfilingFilter:WhitelistMessage(msg, value) 60 | self.messageWhitelist[msg:lower()] = value 61 | end 62 | 63 | function NetMonitor.ProfilingFilter:GetWhitelist() 64 | return self.messageWhitelist 65 | end 66 | 67 | function NetMonitor.ProfilingFilter:BlacklistMessage(msg, value) 68 | self.messageBlacklist[msg:lower()] = value 69 | end 70 | 71 | function NetMonitor.ProfilingFilter:GetBlacklist() 72 | return self.messageBlacklist 73 | end 74 | 75 | local function HasFlag(inFlag) 76 | return bit.band(self.flags, inFlag) > 0 77 | end 78 | 79 | local function SetFlag(inFlag) 80 | self.flags = bit.bor(self.flags, inFlag) 81 | end 82 | 83 | local function ClearFlag(inFlag) 84 | self.flags = bit.bxor(self.flags, inFlag) 85 | end 86 | 87 | function NetMonitor.ProfilingFilter:AllowsIncomingMessages() 88 | return HasFlag(NetMonitor.ProfilingFilter.Flags.IncomingMessages) 89 | end 90 | 91 | function NetMonitor.ProfilingFilter.SetAllowIncomingMessages(value) 92 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.IncomingMessages) else ClearFlag(NetMonitor.ProfilingFilter.Flags.IncomingMessages) end 93 | end 94 | 95 | function NetMonitor.ProfilingFilter:AllowsOutgoingMessages() 96 | return HasFlag(NetMonitor.ProfilingFilter.Flags.OutgoingMessages) 97 | end 98 | 99 | function NetMonitor.ProfilingFilter.SetAllowOutgoingMessages(value) 100 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.OutgoingMessages) else ClearFlag(NetMonitor.ProfilingFilter.Flags.OutgoingMessages) end 101 | end 102 | 103 | function NetMonitor.ProfilingFilter:AllowsNetMessages() 104 | return HasFlag(NetMonitor.ProfilingFilter.Flags.NetLib) 105 | end 106 | 107 | function NetMonitor.ProfilingFilter.SetAllowNetMessages(value) 108 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.NetLib) else ClearFlag(NetMonitor.ProfilingFilter.Flags.NetLib) end 109 | end 110 | 111 | function NetMonitor.ProfilingFilter:AllowsUserMessages() 112 | return HasFlag(NetMonitor.ProfilingFilter.Flags.UMsgLib) 113 | end 114 | 115 | function NetMonitor.ProfilingFilter.SetAllowUserMessages(value) 116 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.UMsgLib) else ClearFlag(NetMonitor.ProfilingFilter.Flags.UMsgLib) end 117 | end 118 | 119 | function NetMonitor.ProfilingFilter:AllowsSuccessfulMessages() 120 | return HasFlag(NetMonitor.ProfilingFilter.Flags.SuccessfulMessages) 121 | end 122 | 123 | function NetMonitor.ProfilingFilter.SetAllowSuccessfulMessages(value) 124 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.SuccessfulMessages) else ClearFlag(NetMonitor.ProfilingFilter.Flags.SuccessfulMessages) end 125 | end 126 | 127 | function NetMonitor.ProfilingFilter:AllowsDiscardedMessages() 128 | return HasFlag(NetMonitor.ProfilingFilter.Flags.DiscardedMessages) 129 | end 130 | 131 | function NetMonitor.ProfilingFilter.SetAllowDiscardedMessages(value) 132 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.DiscardedMessages) else ClearFlag(NetMonitor.ProfilingFilter.Flags.DiscardedMessages) end 133 | end 134 | 135 | function NetMonitor.ProfilingFilter:AllowsIgnoredMessages() 136 | return HasFlag(NetMonitor.ProfilingFilter.Flags.IgnoredMessages) 137 | end 138 | 139 | function NetMonitor.ProfilingFilter.SetAllowIgnoredMessages(value) 140 | if value then SetFlag(NetMonitor.ProfilingFilter.Flags.IgnoredMessages) else ClearFlag(NetMonitor.ProfilingFilter.Flags.IgnoredMessages) end 141 | end 142 | 143 | -- Net extensions 144 | function net.WriteProfilingFilter(filter) 145 | if not filter or getmetatable(filter) != NetMonitor.ProfilingFilter then 146 | Error("Cannot write profiling filter to net, expected ProfilingFilter, got: " .. msg or "nil") 147 | return 148 | end 149 | 150 | net.WriteByte(filter) 151 | 152 | local whiteList = filter:GetWhitelist() 153 | net.WriteBool(#whiteList > 0) 154 | if #whiteList > 0 then 155 | net.WriteUInt(#whiteList, 16) 156 | 157 | for m, _ in pairs(whiteList) do 158 | net.WriteString(m) 159 | end 160 | end 161 | 162 | local blackList = filter:GetBlacklist() 163 | net.WriteBool(#blackList > 0) 164 | 165 | if #blackList > 0 then 166 | net.WriteUInt(#blackList, 16) 167 | 168 | for b, _ in pairs(blackList) do 169 | net.WriteString(b) 170 | end 171 | end 172 | end 173 | 174 | function net.ReadProfilingFilter() 175 | local tbl = {} 176 | 177 | tbl.flags = net.ReadByte() 178 | tbl.messageWhitelist = {} 179 | tbl.messageBlacklist = {} 180 | 181 | if net.ReadBool() then 182 | local len = net.ReadUInt(16) 183 | 184 | for i = 1, len do 185 | local msg = net.ReadString() 186 | tbl.messageWhitelist[msg] = true 187 | end 188 | end 189 | 190 | if net.ReadBool() then 191 | local len = net.ReadUInt(16) 192 | 193 | for i = 1, len do 194 | local msg = net.ReadString() 195 | tbl.messageBlacklist[msg] = true 196 | end 197 | end 198 | 199 | setmetatable(tbl, NetMonitor.ProfilingFilter) 200 | return tbl 201 | end 202 | 203 | setmetatable(NetMonitor.ProfilingFilter, {__call = NetMonitor.ProfilingFilter.new}) -------------------------------------------------------------------------------- /lua/glua-netmon/profiling/profilingstats.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | -------------------------------------------------------------------------------- /lua/glua-netmon/registry.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.Registry = NetMonitor.Registry or {} 5 | NetMonitor.Registry.AddonFiles = NetMonitor.Registry.AddonFiles or {} 6 | NetMonitor.Registry.FileAddons = NetMonitor.Registry.FileAddons or {} 7 | NetMonitor.Registry.ReceiveFiles = NetMonitor.Registry.ReceiveFiles or {} 8 | 9 | local function RegisterDir(addonName, path) 10 | local files, _ = file.Find(path .. "/*.lua", "GAME") 11 | local _, dirs = file.Find(path .. "/*", "GAME") 12 | 13 | for _, f in ipairs(files) do 14 | local p = string.Replace(path .. "/" .. f, "//", "/") 15 | NetMonitor.Registry.RegisterAddonFile(addonName, p) 16 | end 17 | 18 | for _, dir in ipairs(dirs) do 19 | local p = string.Replace(path .. "/" .. dir, "//", "/") 20 | RegisterDir(addonName, p) 21 | end 22 | end 23 | 24 | local function Initialize() 25 | if NetMonitor.Config.AutoRegisterGamemodeFiles then 26 | NetMonitor.Registry.RegisterGamemodeFiles() 27 | end 28 | 29 | -- Addon files can only be auto registered server-side. 30 | if SERVER and NetMonitor.Config.AutoRegisterAddonFiles then 31 | NetMonitor.Registry.RegisterAddonFiles() 32 | end 33 | end 34 | 35 | hook.Add("Initialize", "InitializeNetMonitorRegistry", Initialize) 36 | 37 | function NetMonitor.Registry.RegisterGamemodeFiles() 38 | local gamemodePath = "gamemodes/" .. engine.ActiveGamemode() .. "/" 39 | local entitiesPath = gamemodePath .. "entities" 40 | local luaPath = gamemodePath .. "gamemode" 41 | 42 | if NetMonitor.Utils.DirExists(entitiesPath, "GAME") then 43 | -- Recursively load all files in the entities folder. 44 | RegisterDir(engine.ActiveGamemode(), entitiesPath) 45 | end 46 | 47 | if NetMonitor.Utils.DirExists(luaPath, "GAME") then 48 | RegisterDir(engine.ActiveGamemode(), luaPath) 49 | end 50 | end 51 | 52 | function NetMonitor.Registry.RegisterAddonFiles() 53 | if CLIENT then return end 54 | 55 | MsgC(Color(0, 255, 0), "NetMonitor: Registering all addon files.", "\n") 56 | 57 | local _, dirs = file.Find("addons/*", "GAME") 58 | for _, dir in pairs(dirs) do 59 | RegisterDir(dir, "addons/" .. dir .. "/") 60 | end 61 | end 62 | 63 | -- Registers a file as part of a specific addon. 64 | -- Used to map a net message's function info file to an addon name. 65 | function NetMonitor.Registry.RegisterAddonFile(addonName, filePath) 66 | if not addonName or not isstring(addonName) then 67 | Error("Invalid addon name specified, expected string, got: " .. addonName or "nil") 68 | return 69 | end 70 | 71 | if not filePath or not isstring(filePath) then 72 | Error("Invalid file path specified, expected string, got: " .. filePath or "nil") 73 | return 74 | end 75 | 76 | MsgC(Color(0, 255, 0), "Registered file '" .. filePath .. "' to addon '" .. addonName .. "'\n") 77 | 78 | if not NetMonitor.Registry.AddonFiles[addonName] then 79 | NetMonitor.Registry.AddonFiles[addonName] = {} 80 | end 81 | 82 | NetMonitor.Registry.AddonFiles[addonName][#NetMonitor.Registry.AddonFiles[addonName] + 1] = filePath 83 | NetMonitor.Registry.FileAddons[filePath:lower()] = addonName 84 | end 85 | 86 | function NetMonitor.Registry.RegisterReceiveFile(receivedMessage, filePath) 87 | if not receivedMessage or not isstring(receivedMessage) then 88 | Error("Invalid received message name specified, expected string, got: " .. receivedMessage or "nil") 89 | return 90 | end 91 | 92 | if not filePath or not isstring(filePath) then 93 | Error("Invalid file path specified, expected string, got: " .. filePath or "nil") 94 | return 95 | end 96 | 97 | NetMonitor.Registry.ReceiveFiles[receivedMessage:lower()] = filePath 98 | end 99 | 100 | function NetMonitor.Registry.GetAddonNameForFile(filePath) 101 | if not filePath or not isstring(filePath) then 102 | Error("Invalid file path specified, expected string, got: " .. filePath or "nil") 103 | return 104 | end 105 | 106 | return NetMonitor.Registry.AddonFiles[filePath:lower()] or "Unregistered Addon" 107 | end 108 | 109 | function NetMonitor.Registry.GetAddonNameFromReceivedMessage(messageName) 110 | if not messageName or not isstring(messageName) then 111 | Error("Invalid message name specified, expected string, got: " .. messageName or "nil") 112 | return 113 | end 114 | 115 | local filePath = NetMonitor.Registry.ReceiveFiles[messageName] 116 | if filePath then 117 | return NetMonitor.Registry.AddonFiles[filePath:lower()] or "Unregistered Addon" 118 | end 119 | 120 | return "Unregistered Addon" 121 | end 122 | 123 | function NetMonitor.Registry.IsMessageRegistered(msgName) 124 | if not msgName or not isstring(msgName) then 125 | Error("Invalid message name specified, expected string, got: " .. msgName or "nil") 126 | return 127 | end 128 | 129 | local path = NetMonitor.Registry.ReceiveFiles[msgName:lower()] 130 | if path then return NetMonitor.Registry.AddonFiles[path] != nil end 131 | 132 | return false 133 | end 134 | 135 | function NetMonitor.Registry.IsFileRegistered(filePath) 136 | if not filePath or not isstring(filePath) then 137 | Error("Invalid file path specified, expected string, got: " .. filePath or "nil") 138 | return 139 | end 140 | 141 | return NetMonitor.Registry.AddonFiles[filePath:lower()] != nil 142 | end 143 | 144 | function NetMonitor.Registry.GetAddonFiles(addon) 145 | if not addon or not isstring(addon) then 146 | Error("Invalid addon specified, expected string, got: " .. addon or "nil") 147 | return 148 | end 149 | 150 | return NetMonitor.Registry.AddonFiles[addon] or {} 151 | end 152 | 153 | local chunkSize = 64 * 1024 154 | function NetMonitor.Registry.BuildRegistryChunks() 155 | -- Jsonify the table and compress it. 156 | local json = util.TableToJSON(NetMonitor.Registry.AddonFiles) 157 | local dat = util.Compress(json) 158 | 159 | -- Send them in chunks of 64kb 160 | local chunkAmount = math.ceil(#dat / chunkSize) 161 | local id = 1 162 | local chunks = {} 163 | 164 | repeat 165 | local subDat = string.sub(dat, 1, chunkSize) 166 | dat = string.sub(dat, chunkSize) 167 | 168 | local chunk = NetMonitor.BinaryChunk(id, chunkAmount, subDat) 169 | chunks[ #chunks + 1 ] = chunk 170 | id = id + 1 171 | until id > chunkAmount 172 | 173 | return chunks 174 | end 175 | 176 | function NetMonitor.Registry.UpdateFromChunks(chunks) 177 | local str = "" 178 | 179 | for _, chunk in ipairs(chunks) do 180 | str = str .. chunk:GetData() 181 | end 182 | 183 | local json = util.Decompress(str) 184 | local tbl = util.JSONToTable(json) 185 | 186 | table.Merge(NetMonitor.Registry.AddonFiles, tbl) 187 | hook.Run("OnNetRegistryUpdated") 188 | end 189 | 190 | MsgC(Color(0, 255, 0), "NetMonitor: Loaded registry", "\n") -------------------------------------------------------------------------------- /lua/glua-netmon/sv_config.lua: -------------------------------------------------------------------------------- 1 | -- Some keys here may also be in the client config, so that you can have different options whether it's clientside or serverside. 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.Config = NetMonitor.Config or {} 5 | 6 | -- List of SteamID64s that are allowed to access the net monitor interface and data. 7 | NetMonitor.Config.AllowedSteamIDs = 8 | { 9 | ["76561198076161102"] = true, 10 | } 11 | 12 | -- List of usergroups that are allowed to access the net monitor interface and data. 13 | NetMonitor.Config.AllowedUsergroups = 14 | { 15 | ["superadmin"] = true, 16 | ["developer"] = true, 17 | } 18 | 19 | -- If true, the net monitor will automatically grab all gamemode files and register them. 20 | NetMonitor.Config.AutoRegisterGamemodeFiles = true 21 | 22 | -- If true, the net monitor will automatically grab all addon files and register them. 23 | -- This will take quite a bit of memory since it'll keep track of all lua files in the garrysmod/addons folder. 24 | -- Workshop addons cannot be registered. 25 | NetMonitor.Config.AutoRegisterAddonFiles = true 26 | 27 | -- If true, only addons that are registered in the NetMonitor.Registry will be monitored. 28 | NetMonitor.Config.OnlyMonitorRegisteredAddons = false 29 | 30 | -- The command to open the netmonitor interface. 31 | NetMonitor.Config.InterfaceCommand = "!netmon" 32 | 33 | MsgC(Color(0, 255, 0), "NetMonitor: Loaded server configuration.", "\n") -------------------------------------------------------------------------------- /lua/glua-netmon/sv_net.lua: -------------------------------------------------------------------------------- 1 | NetMonitor = NetMonitor or {} 2 | NetMonitor.Networking = NetMonitor.Networking or {} 3 | 4 | util.AddNetworkString("Netmon.RequestInterfacePermission") 5 | util.AddNetworkString("Netmon.GiveInterfacePermission") 6 | util.AddNetworkString("Netmon.RequestRegistry") 7 | util.AddNetworkString("Netmon.SendRegistryChunk") 8 | 9 | local function PlayerHasPermission(ply) 10 | return NetMonitor.Config.AllowedUsergroups[ply:GetUserGroup()] or NetMonitor.Config.AllowedSteamIDs[ply:SteamID64()] 11 | end 12 | 13 | local function PlayerSay(ply, text) 14 | if text:lower() == NetMonitor.Config.InterfaceCommand then 15 | local hasPermission = PlayerHasPermission(ply) 16 | local isServerHost = ply:IsListenServerHost() 17 | 18 | net.Start("Netmon.GiveInterfacePermission") 19 | net.WriteBool(hasPermission) 20 | net.WriteBool(isServerHost) 21 | net.Send(ply) 22 | return "" 23 | end 24 | end 25 | 26 | hook.Add("PlayerSay", "NoDisplayingNetmonCommand", PlayerSay) 27 | 28 | local function ReceiveInterfaceRequest(len, ply) 29 | local hasPermission = PlayerHasPermission(ply) 30 | local isServerHost = ply:IsListenServerHost() 31 | 32 | net.Start("Netmon.GiveInterfacePermission") 33 | net.WriteBool(hasPermission) 34 | net.WriteBool(isServerHost) 35 | net.Send(ply) 36 | end 37 | 38 | net.Receive("Netmon.RequestInterfacePermission", ReceiveInterfaceRequest) 39 | 40 | local function RequestRegistry(len, ply) 41 | -- Players without permission aren't supposed to ever send that string, they can heck off. 42 | if not PlayerHasPermission(ply) then 43 | ply:Kick("NetMonitor: Unauthorized usage of network string.") 44 | return 45 | end 46 | 47 | MsgC(Color(0, 255, 0), "NetMonitor: Player '", ply:Nick(), "' sent a request for the server's registry.", "\n") 48 | 49 | local chunks = NetMonitor.Registry.BuildRegistryChunks() 50 | for _, chunk in ipairs(chunks) do 51 | net.Start("Netmon.SendRegistryChunk") 52 | net.WriteBinaryChunk(chunk) 53 | net.Send(ply) 54 | end 55 | end 56 | 57 | net.Receive("Netmon.RequestRegistry", RequestRegistry) -------------------------------------------------------------------------------- /lua/glua-netmon/utils.lua: -------------------------------------------------------------------------------- 1 | AddCSLuaFile() 2 | 3 | NetMonitor = NetMonitor or {} 4 | NetMonitor.Utils = NetMonitor.Utils or {} 5 | 6 | -- Surrogates for file.Exists() and file.IsDir() for directories 7 | function NetMonitor.Utils.DirExists(name, path) 8 | if SERVER then return file.IsDir(name, path) end 9 | if file.IsDir(name, path) then return true end 10 | 11 | path = string.TrimRight(path, "/") 12 | local _, dirs = file.Find(name .. "*", path) 13 | if dirs == nil or #dirs == 0 then return false end 14 | 15 | local splits = string.Split(name, "/") 16 | local dirName = splits[#splits] 17 | 18 | return table.HasValue(dirs, dirName) 19 | end 20 | 21 | function NetMonitor.Utils.PathExists(name, path) 22 | return file.Exists(name, path) or (CLIENT and DirExists(name, path)) 23 | end 24 | 25 | function NetMonitor.Utils.ChatError(msg) 26 | if CLIENT then 27 | chat.AddText(Color(255, 90, 90), "NetMonitor: " .. msg) 28 | end 29 | end 30 | 31 | function NetMonitor.Utils.ClampToScreen(w, h) 32 | return math.min(ScrW(), w), math.min(ScrH(), h) 33 | end 34 | 35 | function NetMonitor.Utils.StringMatchesSearch(searchTerm, str) 36 | if #searchTerm == 0 then return true end 37 | 38 | local goodPattern, pos = pcall(string.find, str, searchTerm) 39 | if not goodPattern then 40 | local pos, _, _ = string.find(str, searchTerm, 1, true) 41 | return pos != nil 42 | end 43 | 44 | return pos != nil 45 | end 46 | 47 | function NetMonitor.Utils.VerifyDataFolders() 48 | local neededCreation = false 49 | 50 | if not file.IsDir("netmon", "DATA") then 51 | neededCreation = true 52 | file.CreateDir("netmon") 53 | end 54 | 55 | if CLIENT and not file.IsDir("netmon/client", "DATA") then 56 | neededCreation = true 57 | file.CreateDir("netmon/client") 58 | end 59 | 60 | if not file.IsDir("netmon/server", "DATA") then 61 | neededCreation = true 62 | file.CreateDir("netmon/server") 63 | end 64 | 65 | return neededCreation 66 | end -------------------------------------------------------------------------------- /lua/netmon-addon-autoreg.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | This file is a utility meant to map lua files of an addon to a name. 3 | Drop it in a folder and it will automatically grab all the lua files in current and all subdirs and map them to the given name. 4 | Remember: you need to include() it server-side, do it manually on the addon or something. 5 | 6 | If NetMonitor isn't installed, this file won't do anything. 7 | --]] 8 | 9 | AddCSLuaFile() 10 | 11 | -- BEGIN THINGS YOU CAN AND SHOULD CHANGE 12 | local AddonName = "ChangeMe" 13 | local OtherDirectoriesToAdd = 14 | { 15 | -- "/lua/path/to/dir", 16 | } 17 | -- END THINGS YOU CAN AND SHOULD CHANGE. DO NOT TOUCH ANYTHING ELSE 18 | 19 | local loadedDirs = {} 20 | 21 | local function RegisterDir(path) 22 | local files, _ = file.Find(path .. "/*.lua", "GAME") 23 | local _, dirs = file.Find(path .. "/*", "GAME") 24 | 25 | for _, f in ipairs(files) do 26 | local p = string.Replace(path .. "/" .. f, "//", "/") 27 | NetMonitor.Registry.RegisterAddonFile(AddonName, p) 28 | end 29 | 30 | for _, dir in ipairs(dirs) do 31 | local p = string.Replace(path .. "/" .. dir, "//", "/") 32 | if not loadedDirs[p] then 33 | RegisterDir(p) 34 | loadedDirs[p] = true 35 | end 36 | end 37 | end 38 | 39 | local function InitGrabFiles() 40 | if not NetMonitor or not NetMonitor.Registry then return end 41 | 42 | local info = debug.getinfo(1, "S") 43 | local path = info.short_src 44 | 45 | if path == nil then 46 | MsgC(Color(255, 90, 90), "NetMonitor: Failed to get the file path of an addon that wants to register itself. Addon name: ", AddonName or "nil", "\n") 47 | return 48 | end 49 | 50 | if not AddonName or not isstring(AddonName) then 51 | MsgC(Color(255, 90, 90), "NetMonitor: Cannot register non-string addon name. File: ", path, "\n") 52 | return 53 | end 54 | 55 | local splits = string.Split(path, "/") 56 | local fileName = splits[#splits] 57 | local dir = string.sub(path, 1, #path - #fileName) 58 | 59 | RegisterDir(dir) 60 | 61 | if #OtherDirectoriesToAdd > 0 then 62 | for i = 1, #OtherDirectoriesToAdd 63 | RegisterDir(OtherDirectoriesToAdd[i]) 64 | end 65 | end 66 | end 67 | 68 | hook.Add("Initialize", "Initialize" .. AddonName or "nil", InitGrabFiles) --------------------------------------------------------------------------------