From 090e97de0301c69d991a1f50e61feb66d84dd6f8 Mon Sep 17 00:00:00 2001 From: Alejandro Liu Date: Mon, 7 Sep 2015 12:30:47 +0200 Subject: [PATCH] Tracing and PMS tests - added event tracing - added some scripts for unit testing - opened paths in pmscripts --- lib/maker/getevents.sh | 32 ++ lib/templ/gd2/cmdoverview.md | 2 +- libcommon/README.md | 76 ++- libcommon/maker | 94 +++ libcommon/resources/events.txt | 77 +++ libcommon/resources/messages/messages.ini | 25 + libcommon/resources/messages/spa.ini | 25 + libcommon/resources/tests/chat.pms | 24 + libcommon/resources/tests/freeze.pms | 19 + libcommon/resources/tests/trace.pms | 13 + libcommon/src/aliuly/common/PMScript.php | 2 +- libcommon/src/aliuly/loader/Main.php | 1 + libcommon/src/aliuly/loader/RcCmd.php | 27 +- libcommon/src/aliuly/loader/TraceCmd.php | 249 ++++++++ libcommon/src/aliuly/loader/TraceListener.php | 535 ++++++++++++++++++ libcommon/src/aliuly/loader/Version.php | 2 +- 16 files changed, 1179 insertions(+), 24 deletions(-) create mode 100755 lib/maker/getevents.sh create mode 100644 libcommon/resources/events.txt create mode 100644 libcommon/resources/tests/chat.pms create mode 100644 libcommon/resources/tests/freeze.pms create mode 100644 libcommon/resources/tests/trace.pms create mode 100644 libcommon/src/aliuly/loader/TraceCmd.php create mode 100644 libcommon/src/aliuly/loader/TraceListener.php diff --git a/lib/maker/getevents.sh b/lib/maker/getevents.sh new file mode 100755 index 0000000..187bbc7 --- /dev/null +++ b/lib/maker/getevents.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# +# Dig through PocketMine sources and find event classes +# +fatal() { + echo "$@" 1>&2 + exit 1 +} +[ $# -ne 1 ] && fatal "Must specify a PocketMine-MP source directory" +[ ! -d "$1" ] && fatal "$1: Is not a directory" +eventdir=$(find "$1" -type d -name "event") +[ -z "$eventdir" ] && fatal "$1: does not contain PocketMine-MP source code" + +find $eventdir -name '*.php' | ( + while read fp + do + event=$(grep '^class '<$fp | grep ' extends ' | grep 'Event' |\ + sed \ + -e 's/^class\s*//' \ + -e 's/{$//' \ + -e 's/\s*implements\s*Cancellable\s*//' \ + -e 's/\s*extends\s*/ /') + [ -z "$event" ] && continue + grep -q '@deprecated' $fp && continue + if grep -q 'handlerList' $fp ; then + handler=yes + else + handler=no + fi + echo $(basename $(dirname $fp)) $event $handler + done +) diff --git a/lib/templ/gd2/cmdoverview.md b/lib/templ/gd2/cmdoverview.md index 0493bb4..ffb5dad 100644 --- a/lib/templ/gd2/cmdoverview.md +++ b/lib/templ/gd2/cmdoverview.md @@ -16,7 +16,7 @@ echo "\n"; foreach ($segments as $i=>&$j) { ksort($j); $n = ucwords(strtr($i,"_"," ")); - echo str_repeat("#",$h)." ".$n."\n\n"; + if ($h) echo str_repeat("#",$h)." ".$n."\n\n"; foreach ($j as $a=>$b) { echo "* ".$a.": ".$snippets[$b][0]."\n"; } diff --git a/libcommon/README.md b/libcommon/README.md index 7b92b7e..360b8be 100644 --- a/libcommon/README.md +++ b/libcommon/README.md @@ -9,10 +9,10 @@ - Summary: aliuly's common library - PocketMine-MP version: 1.5 (API:1.12.0) -- DependencyPlugins: -- OptionalPlugins: -- Categories: DevTools -- Plugin Access: N/A +- DependencyPlugins: +- OptionalPlugins: +- Categories: DevTools +- Plugin Access: N/A - WebSite: https://github.com/alejandroliu/pocketmine-plugins/tree/master/libcommon @@ -54,6 +54,23 @@ It also bundles useful third party libraries: For the full API documentation go to: [GitHub pages](http://alejandroliu.github.io/pocketmine-plugins/libcommon/apidocs/index.html) +The following subcommands are available: + + + +* dumpmsg: Dump a plugin's messages.ini +* echo: shows the given text (variable substitutions are performed) +* motd-add: Add a server for MOTD querying +* motd-stat: Return the servers MOTD values +* query-add: Add a server for Query gathering +* query-list: Return the available Query data +* rc: Runs the given script +* trace: controls event tracing +* version: shows the libcommon version + + + + ## Commands Also, for debugging purposes, the **libcommon** command is provided, which @@ -62,41 +79,66 @@ has the following sub-commands: * dumpmsg: Dump a plugin's messages.ini
usage: /libcommon **dumpmsg** _<plugin>_ - + This command is available when **DEBUG** is enabled. * echo: shows the given text (variable substitutions are performed)
usage: /libcommon **echo** _[text]_ - + This command is available when **DEBUG** is enabled. * motd-add: Add a server for MOTD querying
usage: /libcommon **motd-add** _<server>_ _[port]_ - + This command is available when **DEBUG** is enabled. - + * motd-stat: Return the servers MOTD values
usage: /libcommon **motd-stat** - + This command is available when **DEBUG** is enabled. * query-add: Add a server for Query gathering
usage: /libcommon **query-add** _<server>_ _[port]_ - + This command is available when **DEBUG** is enabled. - + * query-list: Return the available Query data
usage: /libcommon **query-list** - + This command is available when **DEBUG** is enabled. * rc: Runs the given script
usage: usage: /libcommon **rc** _<script>_ _[args]_ - + This command will execute PMScripts present in the **libcommon** folder. By convention, the ".pms" suffix must be used for the file name, but the ".pms" is ommitted when issuing this command. - + The special script **autostart.pms** is executed automatically when the **libcommon** plugin gets enabled. - -* version: shows the libcomonn version
+ +* trace: controls event tracing
+ usage: /libcommon **trace** _[options]_ + + This command is available when **DEBUG** is enabled. + Trace will show to the user the different events that are being + triggered on the server. To reduce spam, events are de-duplicated. + + Sub commands: + * /libcommon **trace** + - Shows the current trace status + * /libcommon **trace** **on** + - Turns on tracing + * /libcommon **trace** **off** + - Turns off tracing + * /libcommon **trace** **events** _[type|class]_ + - Show the list of the different event types and classes. If a _type_ + or _class_ was specified, it will show the events defined for them. + * /libcommon **trace** _<event|type|class>_ _[additional options]_ + - Will add the specified _event|type|class_ to the current user's + trace session. + * /libcommon **trace** _<-event|type|class>_ _[additional options]_ + - If you start the _event|type|class_ specification name with a + **dash**, the _event|type|class_ will be removed from the current + trace session. + +* version: shows the libcommon version
usage: /libcommon **version** @@ -208,6 +250,7 @@ available: * rc * motd utils * version + * trace - 1.1.0: Update 1 * Added ItemName class (with more item names) * Removed MPMU::itemName @@ -231,3 +274,4 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . + diff --git a/libcommon/maker b/libcommon/maker index 785cb25..6947763 100755 --- a/libcommon/maker +++ b/libcommon/maker @@ -65,4 +65,98 @@ if ($ntxt != $otxt) { file_put_contents($dl,$ntxt); } +/* + * Create trace listener class + */ +function mkListener($phpfile,$lstfile) { + $events = file($lstfile,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); + $itxt = file($phpfile,FILE_IGNORE_NEW_LINES); + $state = ""; + $otxt = []; + foreach ($itxt as $ln) { + if ($state == "includes") { + if (strtolower(trim($ln)) == "//") { + $state = ""; + $otxt[] = $ln; + } + continue; + } + if ($state == "methods") { + if (strtolower(trim($ln)) == "//") { + $state = ""; + $otxt[] = $ln; + } + continue; + } + $otxt[] = $ln; + if (strtolower(trim($ln)) == "//") { + $state = "includes"; + // Generate includes... + foreach ($events as $row) { + $row = preg_split('/\s+/', $row,4); + $otxt[] = "use pocketmine\\event\\".$row[0]."\\".$row[1].";"; + } + continue; + } + if (strtolower(trim($ln)) == "//") { + $state = "methods"; + // Generate trace methods... + foreach ($events as $row) { + $row = preg_split('/\s+/', $row,4); + if ($row[3] == "no") continue; + $otxt[] = "\tpublic function on".$row[1]."(".$row[1]." \$ev){"; + $otxt[] = "\t\t\$this->trace(\$ev);"; + $otxt[] = "\t}"; + } + // Generate checkEvent method + $otxt[] = ""; + $otxt[] = "\tpublic function checkEvent(\$evname){"; + $otxt[] = "\t\tswitch(strtolower(\$evname)){"; + $types = []; + $classes = []; + foreach ($events as $row) { + $row = preg_split('/\s+/', $row,4); + $otxt[] = "\t\tcase \"".strtolower($row[1])."\":"; + $otxt[] = "\t\t\treturn [\"".$row[1]."\"];"; + if (!isset($types[$row[0]])) $types[$row[0]] = []; + if (!isset($classes[$row[2]])) $classes[$row[2]] = []; + $types[$row[0]][] = $row[1]; + $classes[$row[2]][] = $row[1]; + } + foreach ([$types,$classes] as $tab) { + foreach ($tab as $i=>$j) { + $otxt[] = "\t\tcase \"".strtolower($i)."\":"; + $otxt[] = "\t\t\treturn [\"".implode("\", \"", $j)."\"];"; + } + } + $otxt[] = "\t\tdefault:"; + $otxt[] = "\t\t\treturn null;"; + $otxt[] = "\t\t}"; + $otxt[] = "\t}"; + + // Generate event lists + $otxt[] = ""; + $otxt[] = "\tpublic function getList(){"; + $otxt[] = "\t\treturn ["; + $otxt[] = "\t\t\t\"types\" => [\"".implode("\", \"",array_keys($types))."\"],"; + $otxt[] = "\t\t\t\"classes\" => [\"".implode("\", \"",array_keys($classes))."\"],"; + $otxt[] = "\t\t];"; + $otxt[] = "\t}"; + + continue; + } + } + if ($otxt[count($otxt)-1] != "") $otxt[] = ""; + if ($itxt[count($itxt)-1] != "") $itxt[] = ""; + $itxt = implode("\n",$itxt); + $otxt = implode("\n",$otxt); + if ($itxt != $otxt) { + echo "Updating ".basename($phpfile)."\n"; + file_put_contents($phpfile,$otxt); + } +} + +mkListener(SRCDIR."src/aliuly/loader/TraceListener.php",SRCDIR."resources/events.txt"); + + exit("OK\n"); diff --git a/libcommon/resources/events.txt b/libcommon/resources/events.txt new file mode 100644 index 0000000..c666398 --- /dev/null +++ b/libcommon/resources/events.txt @@ -0,0 +1,77 @@ +block BlockFormEvent BlockGrowEvent yes +block BlockGrowEvent BlockEvent yes +block BlockPlaceEvent BlockEvent yes +block BlockSpreadEvent BlockFormEvent yes +block BlockUpdateEvent BlockEvent yes +block LeavesDecayEvent BlockEvent yes +block SignChangeEvent BlockEvent yes +block BlockBreakEvent BlockEvent yes +entity EntityArmorChangeEvent EntityEvent yes +entity EntityBlockChangeEvent EntityEvent yes +entity EntityCombustByBlockEvent EntityCombustEvent no +entity EntityCombustByEntityEvent EntityCombustEvent no +entity EntityCombustEvent EntityEvent yes +entity EntityDamageByBlockEvent EntityDamageEvent no +entity EntityDamageByChildEntityEvent EntityDamageByEntityEvent no +entity EntityDeathEvent EntityEvent yes +entity EntityDespawnEvent EntityEvent yes +entity EntityExplodeEvent EntityEvent yes +entity EntityInventoryChangeEvent EntityEvent yes +entity EntityLevelChangeEvent EntityEvent yes +entity EntityMotionEvent EntityEvent yes +entity EntityRegainHealthEvent EntityEvent yes +entity EntityShootBowEvent EntityEvent yes +entity EntitySpawnEvent EntityEvent yes +entity EntityTeleportEvent EntityEvent yes +entity ExplosionPrimeEvent EntityEvent yes +entity ItemDespawnEvent EntityEvent yes +entity ItemSpawnEvent EntityEvent yes +entity ProjectileHitEvent EntityEvent yes +entity ProjectileLaunchEvent EntityEvent yes +entity EntityDamageByEntityEvent EntityDamageEvent no +entity EntityDamageEvent EntityEvent yes +inventory CraftItemEvent Event yes +inventory FurnaceBurnEvent BlockEvent yes +inventory FurnaceSmeltEvent BlockEvent yes +inventory InventoryCloseEvent InventoryEvent yes +inventory InventoryOpenEvent InventoryEvent yes +inventory InventoryPickupArrowEvent InventoryEvent yes +inventory InventoryPickupItemEvent InventoryEvent yes +inventory InventoryTransactionEvent Event yes +level ChunkLoadEvent ChunkEvent yes +level ChunkPopulateEvent ChunkEvent yes +level ChunkUnloadEvent ChunkEvent yes +level LevelInitEvent LevelEvent yes +level LevelLoadEvent LevelEvent yes +level LevelSaveEvent LevelEvent yes +level LevelUnloadEvent LevelEvent yes +level SpawnChangeEvent LevelEvent yes +player PlayerAchievementAwardedEvent PlayerEvent yes +player PlayerAnimationEvent PlayerEvent yes +player PlayerBedEnterEvent PlayerEvent yes +player PlayerBedLeaveEvent PlayerEvent yes +player PlayerBucketEmptyEvent PlayerBucketEvent yes +player PlayerBucketFillEvent PlayerBucketEvent yes +player PlayerCommandPreprocessEvent PlayerEvent yes +player PlayerCreationEvent Event yes +player PlayerDropItemEvent PlayerEvent yes +player PlayerGameModeChangeEvent PlayerEvent yes +player PlayerItemConsumeEvent PlayerEvent yes +player PlayerItemHeldEvent PlayerEvent yes +player PlayerKickEvent PlayerEvent yes +player PlayerLoginEvent PlayerEvent yes +player PlayerMoveEvent PlayerEvent yes +player PlayerPreLoginEvent PlayerEvent yes +player PlayerRespawnEvent PlayerEvent yes +player PlayerDeathEvent EntityDeathEvent yes +player PlayerInteractEvent PlayerEvent yes +player PlayerJoinEvent PlayerEvent yes +player PlayerQuitEvent PlayerEvent yes +plugin PluginDisableEvent PluginEvent yes +plugin PluginEnableEvent PluginEvent yes +server DataPacketReceiveEvent ServerEvent yes +server DataPacketSendEvent ServerEvent yes +server RemoteServerCommandEvent ServerCommandEvent yes +server ServerCommandEvent ServerEvent yes +server LowMemoryEvent ServerEvent yes +server QueryRegenerateEvent ServerEvent yes diff --git a/libcommon/resources/messages/messages.ini b/libcommon/resources/messages/messages.ini index c7c3b30..c079781 100644 --- a/libcommon/resources/messages/messages.ini +++ b/libcommon/resources/messages/messages.ini @@ -16,17 +16,22 @@ ""="" "