Skip to content

Commit ed4e09f

Browse files
committed
Add source code generator for packet id constants
1 parent 8239220 commit ed4e09f

File tree

7 files changed

+187
-7
lines changed

7 files changed

+187
-7
lines changed

controllers/Packet/Index.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,44 @@
44

55
use \BNETDocs\Libraries\Common;
66
use \BNETDocs\Libraries\Controller;
7-
use \BNETDocs\Libraries\Packet;
87
use \BNETDocs\Libraries\Exceptions\UnspecifiedViewException;
98
use \BNETDocs\Libraries\Gravatar;
9+
use \BNETDocs\Libraries\Packet;
10+
use \BNETDocs\Libraries\Pair;
1011
use \BNETDocs\Libraries\Router;
1112
use \BNETDocs\Libraries\UserSession;
1213
use \BNETDocs\Models\Packet\Index as PacketIndexModel;
14+
use \BNETDocs\Views\Packet\IndexCpp as PacketIndexCppView;
1315
use \BNETDocs\Views\Packet\IndexHtml as PacketIndexHtmlView;
1416
use \BNETDocs\Views\Packet\IndexJSON as PacketIndexJSONView;
17+
use \BNETDocs\Views\Packet\IndexJava as PacketIndexJavaView;
18+
use \BNETDocs\Views\Packet\IndexPHP as PacketIndexPHPView;
19+
use \BNETDocs\Views\Packet\IndexVB as PacketIndexVBView;
1520
use \DateTime;
1621
use \DateTimeZone;
1722

1823
class Index extends Controller {
1924

2025
public function run(Router &$router) {
2126
switch ($router->getRequestPathExtension()) {
27+
case "cpp":
28+
$view = new PacketIndexCppView();
29+
break;
2230
case "htm": case "html": case "":
2331
$view = new PacketIndexHtmlView();
2432
break;
33+
case "java":
34+
$view = new PacketIndexJavaView();
35+
break;
2536
case "json":
2637
$view = new PacketIndexJSONView();
2738
break;
39+
case "php":
40+
$view = new PacketIndexPHPView();
41+
break;
42+
case "vb":
43+
$view = new PacketIndexVBView();
44+
break;
2845
default:
2946
throw new UnspecifiedViewException();
3047
}
@@ -33,8 +50,8 @@ public function run(Router &$router) {
3350
$model->packets = Packet::getAllPackets();
3451
$model->user_session = UserSession::load($router);
3552

36-
// Alphabetically sort the packets for HTML
37-
if ($view instanceof PacketIndexHtmlView && $model->packets) {
53+
// Alphabetically sort the packets for non-json
54+
if (!$view instanceof PacketIndexJSONView && $model->packets) {
3855
usort($model->packets, function($a, $b){
3956
$a1 = $a->getPacketApplicationLayerId();
4057
$b1 = $b->getPacketApplicationLayerId();
@@ -65,9 +82,13 @@ public function run(Router &$router) {
6582
}
6683
}
6784

85+
// Include timestamp if non-html
86+
if (!$view instanceof PacketIndexHtmlView) {
87+
$model->timestamp = new DateTime("now", new DateTimeZone("UTC"));
88+
}
89+
6890
// Objectify for JSON
6991
if ($view instanceof PacketIndexJSONView) {
70-
$model->timestamp = new DateTime("now", new DateTimeZone("UTC"));
7192
$packets = [];
7293
foreach ($model->packets as $packet) {
7394
$user = $packet->getUser();
@@ -106,6 +127,17 @@ public function run(Router &$router) {
106127
$model->packets = $packets;
107128
}
108129

130+
// Remove duplicates if non-html and non-json
131+
if (!$view instanceof PacketIndexHtmlView
132+
&& !$view instanceof PacketIndexJSONView) {
133+
$packets = [];
134+
foreach ($model->packets as $pkt) {
135+
// This removes duplicates by overwriting keys that already exist.
136+
$packets[$pkt->getPacketId().$pkt->getPacketName()] = $pkt;
137+
}
138+
$model->packets = $packets;
139+
}
140+
109141
// Post-filter summary of packets
110142
$model->sum_packets = count($model->packets);
111143

libraries/Router.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ public function route(Pair &$redirect = null) {
288288
break;
289289
case "packet":
290290
switch ($subpath) {
291-
case "index": case "index.htm": case "index.html":
292-
case "index.json":
291+
case "index": case "index.cpp": case "index.htm":
292+
case "index.html": case "index.java": case "index.json":
293+
case "index.php": case "index.vb":
293294
$controller = new PacketIndexController();
294295
break;
295296
case "popular": case "popular.htm": case "popular.html":

static/a/BNETDocs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* BNETDocs: Phoenix
3-
* Copyright (C) 2003-2015 BNETDocs CC-BY-NC-SA 4.0
3+
* Copyright (C) 2003-2016 BNETDocs CC-BY-NC-SA 4.0
44
* <https://dev.bnetdocs.org/legal>
55
*
66
* The site should **ALWAYS** work if JavaScript is disabled. This script is

views/Packet/IndexCpp.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace BNETDocs\Views\Packet;
4+
5+
use \BNETDocs\Libraries\Common;
6+
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
7+
use \BNETDocs\Libraries\Model;
8+
use \BNETDocs\Libraries\View;
9+
use \BNETDocs\Models\Packet\Index as PacketIndexModel;
10+
11+
class IndexCpp extends View {
12+
13+
public function getMimeType() {
14+
return "text/x-c;charset=utf-8";
15+
}
16+
17+
public function render(Model &$model) {
18+
if (!$model instanceof PacketIndexModel) {
19+
throw new IncorrectModelException();
20+
}
21+
echo "/**\n";
22+
echo " * BNETDocs: Phoenix\n";
23+
echo " * Copyright (C) 2003-2016 BNETDocs CC-BY-NC-SA 4.0\n";
24+
echo " * <" . Common::relativeUrlToAbsolute("/legal") . ">\n";
25+
echo " *\n";
26+
echo " * Packet ID constants for C++\n";
27+
echo " * Generated by BNETDocs on "
28+
. $model->timestamp->format("r") . "\n";
29+
echo " */\n\n";
30+
foreach ($model->packets as $pkt) {
31+
echo "#define " . $pkt->getPacketName() . " "
32+
. $pkt->getPacketId(true) . "\n";
33+
}
34+
}
35+
36+
}

views/Packet/IndexJava.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace BNETDocs\Views\Packet;
4+
5+
use \BNETDocs\Libraries\Common;
6+
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
7+
use \BNETDocs\Libraries\Model;
8+
use \BNETDocs\Libraries\View;
9+
use \BNETDocs\Models\Packet\Index as PacketIndexModel;
10+
11+
class IndexJava extends View {
12+
13+
public function getMimeType() {
14+
return "text/x-java-source;charset=utf-8";
15+
}
16+
17+
public function render(Model &$model) {
18+
if (!$model instanceof PacketIndexModel) {
19+
throw new IncorrectModelException();
20+
}
21+
echo "/**\n";
22+
echo " * BNETDocs: Phoenix\n";
23+
echo " * Copyright (C) 2003-2016 BNETDocs CC-BY-NC-SA 4.0\n";
24+
echo " * <" . Common::relativeUrlToAbsolute("/legal") . ">\n";
25+
echo " *\n";
26+
echo " * Packet ID constants for Java\n";
27+
echo " * Generated by BNETDocs on "
28+
. $model->timestamp->format("r") . "\n";
29+
echo " */\n\n";
30+
foreach ($model->packets as $pkt) {
31+
echo "static final byte " . $pkt->getPacketName() . " = "
32+
. $pkt->getPacketId(true) . ";\n";
33+
}
34+
}
35+
36+
}

views/Packet/IndexPHP.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace BNETDocs\Views\Packet;
4+
5+
use \BNETDocs\Libraries\Common;
6+
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
7+
use \BNETDocs\Libraries\Model;
8+
use \BNETDocs\Libraries\View;
9+
use \BNETDocs\Models\Packet\Index as PacketIndexModel;
10+
11+
class IndexPHP extends View {
12+
13+
public function getMimeType() {
14+
// There isn't an assigned MIME-type from IANA.
15+
// <https://cweiske.de/tagebuch/php-mimetype.htm>
16+
return "text/x-php;charset=utf-8";
17+
}
18+
19+
public function render(Model &$model) {
20+
if (!$model instanceof PacketIndexModel) {
21+
throw new IncorrectModelException();
22+
}
23+
echo "<?php\n\n";
24+
echo "/**\n";
25+
echo " * BNETDocs: Phoenix\n";
26+
echo " * Copyright (C) 2003-2016 BNETDocs CC-BY-NC-SA 4.0\n";
27+
echo " * <" . Common::relativeUrlToAbsolute("/legal") . ">\n";
28+
echo " *\n";
29+
echo " * Packet ID constants for PHP\n";
30+
echo " * Generated by BNETDocs on "
31+
. $model->timestamp->format("r") . "\n";
32+
echo " */\n\n";
33+
echo "namespace BNETDocs\Packets;\n\n";
34+
foreach ($model->packets as $pkt) {
35+
echo "define(\"" . $pkt->getPacketName() . "\", "
36+
. $pkt->getPacketId(true) . ");\n";
37+
}
38+
}
39+
40+
}

views/Packet/IndexVB.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace BNETDocs\Views\Packet;
4+
5+
use \BNETDocs\Libraries\Common;
6+
use \BNETDocs\Libraries\Exceptions\IncorrectModelException;
7+
use \BNETDocs\Libraries\Model;
8+
use \BNETDocs\Libraries\View;
9+
use \BNETDocs\Models\Packet\Index as PacketIndexModel;
10+
11+
class IndexVB extends View {
12+
13+
public function getMimeType() {
14+
return "text/x-vb;charset=utf-8";
15+
}
16+
17+
public function render(Model &$model) {
18+
if (!$model instanceof PacketIndexModel) {
19+
throw new IncorrectModelException();
20+
}
21+
echo "' BNETDocs: Phoenix\n";
22+
echo "' Copyright (C) 2003-2016 BNETDocs CC-BY-NC-SA 4.0\n";
23+
echo "' <" . Common::relativeUrlToAbsolute("/legal") . ">\n";
24+
echo "'\n";
25+
echo "' Packet ID constants for Visual Basic 6\n";
26+
echo "' Generated by BNETDocs on "
27+
. $model->timestamp->format("r") . "\n";
28+
echo "\n";
29+
foreach ($model->packets as $pkt) {
30+
echo "CONST " . $pkt->getPacketName() . "& = &H"
31+
. substr("0" . strtoupper(dechex($pkt->getPacketId())), -2) . "\n";
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)