Skip to content

Commit bb8ae4a

Browse files
authored
Merge pull request #7011 from pmmp/r5.41.0
R5.41.0
2 parents 90752ed + 65c5811 commit bb8ae4a

86 files changed

Lines changed: 11177 additions & 2264 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/main-php-matrix.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,17 @@ jobs:
147147
- name: Install Composer dependencies
148148
run: composer install --no-dev --prefer-dist --no-interaction
149149

150+
- name: Delete old generated code
151+
run: rm -rf generated
152+
150153
- name: Update generated code
151154
run: composer update-codegen
152155

153156
- name: Verify code is unchanged
154157
run: |
155158
git diff
156159
git diff --quiet
160+
161+
- name: Check for generated code files colliding with src files
162+
run:
163+
php build/codegen/check-file-collisions.php

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$finder = PhpCsFixer\Finder::create()
44
->in(__DIR__ . '/src')
5+
->in(__DIR__ . '/generated') //TODO: maybe this is unnecessary???
56
->in(__DIR__ . '/build')
67
->in(__DIR__ . '/tests')
78
->in(__DIR__ . '/tools')

build/generate-bedrockdata-path-consts.php renamed to build/codegen/bedrockdata-path-consts.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323

2424
namespace pocketmine\build\generate_bedrockdata_path_consts;
2525

26+
use pocketmine\utils\Filesystem;
2627
use Symfony\Component\Filesystem\Path;
2728
use function dirname;
2829
use function fclose;
2930
use function fopen;
3031
use function fwrite;
3132
use function is_dir;
3233
use function is_file;
34+
use function mkdir;
3335
use function scandir;
3436
use function str_replace;
3537
use function strtoupper;
@@ -38,7 +40,7 @@
3840
use const SCANDIR_SORT_ASCENDING;
3941
use const STDERR;
4042

41-
require dirname(__DIR__) . '/vendor/autoload.php';
43+
require dirname(__DIR__, 2) . '/vendor/autoload.php';
4244

4345
function constantify(string $permissionName) : string{
4446
return strtoupper(str_replace([".", "-"], "_", $permissionName));
@@ -78,34 +80,20 @@ function constantify(string $permissionName) : string{
7880
$consts[] = $file;
7981
}
8082

81-
$output = fopen(dirname(__DIR__) . '/src/data/bedrock/BedrockDataFiles.php', 'wb');
83+
$path = dirname(__DIR__, 2) . '/generated/data/bedrock/BedrockDataFiles.php';
84+
$dir = dirname($path);
85+
if(!@mkdir($dir, recursive: true) && !is_dir($dir)){
86+
fwrite(STDERR, "Couldn't create directory: $dir" . PHP_EOL);
87+
exit(1);
88+
}
89+
$header = Filesystem::fileGetContents(__DIR__ . "/templates/header.php");
90+
$output = fopen($path, 'wb');
8291
if($output === false){
83-
fwrite(STDERR, "Couldn't open output file" . PHP_EOL);
92+
fwrite(STDERR, "Couldn't open output file: $path" . PHP_EOL);
8493
exit(1);
8594
}
95+
fwrite($output, $header);
8696
fwrite($output, <<<'HEADER'
87-
<?php
88-
89-
/*
90-
*
91-
* ____ _ _ __ __ _ __ __ ____
92-
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
93-
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
94-
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
95-
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
96-
*
97-
* This program is free software: you can redistribute it and/or modify
98-
* it under the terms of the GNU Lesser General Public License as published by
99-
* the Free Software Foundation, either version 3 of the License, or
100-
* (at your option) any later version.
101-
*
102-
* @author PocketMine Team
103-
* @link http://www.pocketmine.net/
104-
*
105-
*
106-
*/
107-
108-
declare(strict_types=1);
10997
11098
namespace pocketmine\data\bedrock;
11199
Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,26 @@
3232
use function fopen;
3333
use function fwrite;
3434
use function is_array;
35+
use function is_dir;
3536
use function is_int;
3637
use function is_string;
3738
use function json_decode;
39+
use function mkdir;
3840
use function str_replace;
3941
use function strtoupper;
4042
use const SORT_NUMERIC;
4143

42-
require dirname(__DIR__) . '/vendor/autoload.php';
43-
44-
const HEADER = <<<'HEADER'
45-
<?php
46-
47-
/*
48-
*
49-
* ____ _ _ __ __ _ __ __ ____
50-
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
51-
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
52-
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
53-
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
54-
*
55-
* This program is free software: you can redistribute it and/or modify
56-
* it under the terms of the GNU Lesser General Public License as published by
57-
* the Free Software Foundation, either version 3 of the License, or
58-
* (at your option) any later version.
59-
*
60-
* @author PocketMine Team
61-
* @link http://www.pocketmine.net/
62-
*
63-
*
64-
*/
65-
66-
declare(strict_types=1);
67-
68-
69-
HEADER;
44+
require dirname(__DIR__, 2) . '/vendor/autoload.php';
7045

7146
/** @return resource */
7247
function safe_fopen(string $file, string $flags){
48+
$dir = dirname($file);
49+
if(!@mkdir($dir, recursive: true) && !is_dir($dir)){
50+
throw new \RuntimeException("Couldn't create directory: $dir");
51+
}
7352
$result = fopen($file, $flags);
7453
if($result === false){
75-
throw new \RuntimeException("Failed to open file");
54+
throw new \RuntimeException("Failed to open file: $file");
7655
}
7756
return $result;
7857
}
@@ -86,9 +65,12 @@ function make_const_name(string $name) : string{
8665
* @phpstan-param array<string, int> $map
8766
*/
8867
function generate(array $map, string $outputFile) : void{
68+
$headerPath = __DIR__ . "/templates/header.php";
69+
$header = Filesystem::fileGetContents($headerPath);
8970
$file = safe_fopen($outputFile, 'wb');
90-
fwrite($file, HEADER);
71+
fwrite($file, $header);
9172
fwrite($file, <<<'CLASSHEADER'
73+
9274
namespace pocketmine\data\bedrock;
9375
9476
final class BiomeIds{
@@ -128,6 +110,6 @@ private function __construct(){
128110
}
129111
$cleanedIds[$name] = $id;
130112
}
131-
generate($cleanedIds, dirname(__DIR__) . '/src/data/bedrock/BiomeIds.php');
113+
generate($cleanedIds, dirname(__DIR__, 2) . '/generated/data/bedrock/BiomeIds.php');
132114

133115
echo "Done. Don't forget to run CS fixup after generating code.\n";

build/generate-block-serializer-consts.php renamed to build/codegen/block-serializer-consts.php

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
use pocketmine\data\bedrock\block\BlockStateNames;
2828
use pocketmine\data\bedrock\block\BlockStateStringValues;
2929
use pocketmine\data\bedrock\block\BlockTypeNames;
30-
use pocketmine\errorhandler\ErrorToExceptionHandler;
3130
use pocketmine\nbt\NbtException;
3231
use pocketmine\nbt\tag\ByteTag;
3332
use pocketmine\nbt\tag\IntTag;
3433
use pocketmine\nbt\tag\StringTag;
3534
use pocketmine\network\mcpe\convert\BlockStateDictionary;
3635
use pocketmine\utils\AssumptionFailedError;
36+
use pocketmine\utils\Filesystem;
3737
use pocketmine\utils\Utils;
3838
use function array_values;
3939
use function asort;
@@ -44,9 +44,11 @@
4444
use function file_get_contents;
4545
use function fopen;
4646
use function fwrite;
47+
use function is_dir;
4748
use function is_string;
4849
use function ksort;
4950
use function mb_strtoupper;
51+
use function mkdir;
5052
use function preg_replace;
5153
use function sort;
5254
use function strrpos;
@@ -55,7 +57,7 @@
5557
use const SORT_STRING;
5658
use const STDERR;
5759

58-
require dirname(__DIR__) . '/vendor/autoload.php';
60+
require dirname(__DIR__, 2) . '/vendor/autoload.php';
5961

6062
class BlockPaletteReport{
6163
/**
@@ -98,36 +100,15 @@ function constifyMcId(string $id) : string{
98100
return strtoupper(explode(":", $id, 2)[1]);
99101
}
100102

101-
function generateClassHeader(string $className) : string{
103+
function generateClassHeader(string $className, string $fileHeader) : string{
102104
$backslashPos = strrpos($className, "\\");
103105
if($backslashPos === false){
104106
throw new AssumptionFailedError("Expected a namespaced class FQN");
105107
}
106108
$namespace = substr($className, 0, $backslashPos);
107109
$shortName = substr($className, $backslashPos + 1);
108-
return <<<HEADER
109-
<?php
110110

111-
/*
112-
*
113-
* ____ _ _ __ __ _ __ __ ____
114-
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
115-
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
116-
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
117-
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
118-
*
119-
* This program is free software: you can redistribute it and/or modify
120-
* it under the terms of the GNU Lesser General Public License as published by
121-
* the Free Software Foundation, either version 3 of the License, or
122-
* (at your option) any later version.
123-
*
124-
* @author PocketMine Team
125-
* @link http://www.pocketmine.net/
126-
*
127-
*
128-
*/
129-
130-
declare(strict_types=1);
111+
return $fileHeader . <<<HEADER
131112
132113
namespace $namespace;
133114
@@ -143,13 +124,26 @@ private function __construct(){
143124
HEADER;
144125
}
145126

127+
/** @return resource */
128+
function safe_fopen(string $file, string $flags){
129+
$dir = dirname($file);
130+
if(!@mkdir($dir, recursive: true) && !is_dir($dir)){
131+
throw new \RuntimeException("Couldn't create directory: $dir");
132+
}
133+
$result = fopen($file, $flags);
134+
if($result === false){
135+
throw new \RuntimeException("Failed to open file: $file");
136+
}
137+
return $result;
138+
}
139+
146140
/**
147141
* @phpstan-param list<string> $seenIds
148142
*/
149-
function generateBlockIds(array $seenIds) : void{
150-
$output = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => fopen(dirname(__DIR__) . '/src/data/bedrock/block/BlockTypeNames.php', 'wb'));
143+
function generateBlockIds(array $seenIds, string $fileHeader) : void{
144+
$output = safe_fopen(dirname(__DIR__, 2) . '/generated/data/bedrock/block/BlockTypeNames.php', 'wb');
151145

152-
fwrite($output, generateClassHeader(BlockTypeNames::class));
146+
fwrite($output, generateClassHeader(BlockTypeNames::class, $fileHeader));
153147

154148
foreach($seenIds as $id){
155149
fwrite($output, "\tpublic const " . constifyMcId($id) . " = \"" . $id . "\";\n");
@@ -159,10 +153,10 @@ function generateBlockIds(array $seenIds) : void{
159153
fclose($output);
160154
}
161155

162-
function generateBlockStateNames(BlockPaletteReport $data) : void{
163-
$output = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => fopen(dirname(__DIR__) . '/src/data/bedrock/block/BlockStateNames.php', 'wb'));
156+
function generateBlockStateNames(BlockPaletteReport $data, string $fileHeader) : void{
157+
$output = safe_fopen(dirname(__DIR__, 2) . '/generated/data/bedrock/block/BlockStateNames.php', 'wb');
164158

165-
fwrite($output, generateClassHeader(BlockStateNames::class));
159+
fwrite($output, generateClassHeader(BlockStateNames::class, $fileHeader));
166160
foreach(Utils::stringifyKeys($data->seenStateValues) as $state => $values){
167161
$constName = mb_strtoupper(preg_replace("/^minecraft:/", "mc_", $state) ?? throw new AssumptionFailedError("This regex is not invalid"), 'US-ASCII');
168162
fwrite($output, "\tpublic const $constName = \"$state\";\n");
@@ -172,10 +166,10 @@ function generateBlockStateNames(BlockPaletteReport $data) : void{
172166
fclose($output);
173167
}
174168

175-
function generateBlockStringValues(BlockPaletteReport $data) : void{
176-
$output = ErrorToExceptionHandler::trapAndRemoveFalse(fn() => fopen(dirname(__DIR__) . '/src/data/bedrock/block/BlockStateStringValues.php', 'wb'));
169+
function generateBlockStringValues(BlockPaletteReport $data, string $fileHeader) : void{
170+
$output = safe_fopen(dirname(__DIR__, 2) . '/generated/data/bedrock/block/BlockStateStringValues.php', 'wb');
177171

178-
fwrite($output, generateClassHeader(BlockStateStringValues::class));
172+
fwrite($output, generateClassHeader(BlockStateStringValues::class, $fileHeader));
179173
foreach(Utils::stringifyKeys($data->seenStateValues) as $stateName => $values){
180174
$anyWritten = false;
181175
sort($values, SORT_STRING);
@@ -201,6 +195,8 @@ function generateBlockStringValues(BlockPaletteReport $data) : void{
201195
exit(1);
202196
}
203197

198+
$fileHeader = Filesystem::fileGetContents(__DIR__ . "/templates/header.php");
199+
204200
$palettePath = $argv[1];
205201
$paletteRaw = file_get_contents($palettePath);
206202
if($paletteRaw === false){
@@ -216,8 +212,8 @@ function generateBlockStringValues(BlockPaletteReport $data) : void{
216212
}
217213

218214
$report = generateBlockPaletteReport($states);
219-
generateBlockIds(array_values($report->seenTypes));
220-
generateBlockStateNames($report);
221-
generateBlockStringValues($report);
215+
generateBlockIds(array_values($report->seenTypes), $fileHeader);
216+
generateBlockStateNames($report, $fileHeader);
217+
generateBlockStringValues($report, $fileHeader);
222218

223219
echo "Done. Don't forget to run CS fixup after generating code.\n";

0 commit comments

Comments
 (0)