Skip to content
This repository has been archived by the owner on Jul 11, 2018. It is now read-only.

Commit

Permalink
Tracing and PMS tests
Browse files Browse the repository at this point in the history
- added event tracing
- added some scripts for unit testing
- opened paths in pmscripts
  • Loading branch information
alejandroliu committed Sep 7, 2015
1 parent 0415ba8 commit 090e97d
Show file tree
Hide file tree
Showing 16 changed files with 1,179 additions and 24 deletions.
32 changes: 32 additions & 0 deletions lib/maker/getevents.sh
Original file line number Diff line number Diff line change
@@ -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
)
2 changes: 1 addition & 1 deletion lib/templ/gd2/cmdoverview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
76 changes: 60 additions & 16 deletions libcommon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- end-include -->
Expand Down Expand Up @@ -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:
<!-- php:$h = 0; -->
<!-- template: gd2/cmdoverview.md -->

* 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


<!-- end-include -->

## Commands

Also, for debugging purposes, the **libcommon** command is provided, which
Expand All @@ -62,41 +79,66 @@ has the following sub-commands:
<!-- template: gd2/subcmds.md -->
* dumpmsg: Dump a plugin's messages.ini<br/>
usage: /libcommon **dumpmsg** _&lt;plugin&gt;_

This command is available when **DEBUG** is enabled.
* echo: shows the given text (variable substitutions are performed)<br/>
usage: /libcommon **echo** _[text]_

This command is available when **DEBUG** is enabled.
* motd-add: Add a server for MOTD querying<br/>
usage: /libcommon **motd-add** _&lt;server&gt;_ _[port]_

This command is available when **DEBUG** is enabled.

* motd-stat: Return the servers MOTD values<br/>
usage: /libcommon **motd-stat**

This command is available when **DEBUG** is enabled.
* query-add: Add a server for Query gathering<br/>
usage: /libcommon **query-add** _&lt;server&gt;_ _[port]_

This command is available when **DEBUG** is enabled.

* query-list: Return the available Query data<br/>
usage: /libcommon **query-list**

This command is available when **DEBUG** is enabled.
* rc: Runs the given script<br/>
usage: usage: /libcommon **rc** _&lt;script&gt;_ _[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<br/>

* trace: controls event tracing<br/>
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** _&lt;event|type|class&gt;_ _[additional options]_
- Will add the specified _event|type|class_ to the current user's
trace session.
* /libcommon **trace** _&lt;-event|type|class&gt;_ _[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<br/>
usage: /libcommon **version**

<!-- end-include -->
Expand Down Expand Up @@ -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
Expand All @@ -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 <http://www.gnu.org/licenses/>.

94 changes: 94 additions & 0 deletions libcommon/maker
Original file line number Diff line number Diff line change
Expand Up @@ -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)) == "//<!-- end-includes -->") {
$state = "";
$otxt[] = $ln;
}
continue;
}
if ($state == "methods") {
if (strtolower(trim($ln)) == "//<!-- end-methods -->") {
$state = "";
$otxt[] = $ln;
}
continue;
}
$otxt[] = $ln;
if (strtolower(trim($ln)) == "//<!-- start-includes -->") {
$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)) == "//<!-- start-methods -->") {
$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");
77 changes: 77 additions & 0 deletions libcommon/resources/events.txt
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 090e97d

Please sign in to comment.