| 1 |
<?php |
| 2 |
|
| 3 |
chdir('buildtools'); |
| 4 |
|
| 5 |
require __DIR__ . '/vendor/autoload.php'; |
| 6 |
|
| 7 |
use Dpp\StructGeneratorInterface; |
| 8 |
|
| 9 |
if (count($argv) < 2) { |
| 10 |
die("You must specify a generator type\n"); |
| 11 |
} else { |
| 12 |
$generatorName = $argv[1]; |
| 13 |
$generator = new $generatorName(); |
| 14 |
} |
| 15 |
|
| 16 |
chdir('..'); |
| 17 |
|
| 18 |
|
| 19 |
exec("cat src/dpp/cluster/*.cpp", $clustercpp); |
| 20 |
|
| 21 |
|
| 22 |
$blacklist = [ |
| 23 |
]; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
$forcedReturn = [ |
| 29 |
'direct_message_create' => 'message', |
| 30 |
'guild_get_members' => 'guild_member_map', |
| 31 |
'guild_search_members' => 'guild_member_map', |
| 32 |
'message_create' => 'message', |
| 33 |
'message_edit' => 'message', |
| 34 |
'message_add_reaction' => 'confirmation', |
| 35 |
'message_delete_reaction' => 'confirmation', |
| 36 |
'message_delete_reaction_emoji' => 'confirmation', |
| 37 |
'message_delete_all_reactions' => 'confirmation', |
| 38 |
'message_delete_own_reaction' => 'confirmation', |
| 39 |
'channel_edit_permissions' => 'confirmation', |
| 40 |
'channel_typing' => 'confirmation', |
| 41 |
'message_get_reactions' => 'emoji_map', |
| 42 |
'thread_create_in_forum' => 'thread', |
| 43 |
'threads_get_private_archived' => 'thread_map', |
| 44 |
'threads_get_public_archived' => 'thread_map', |
| 45 |
'threads_get_active' => 'active_threads', |
| 46 |
'user_get_cached' => 'user_identified', |
| 47 |
'channel_pins_get' => 'message_pin_map' |
| 48 |
]; |
| 49 |
|
| 50 |
|
| 51 |
$header = explode("\n", file_get_contents('include/dpp/cluster.h')); |
| 52 |
|
| 53 |
|
| 54 |
const STATE_SEARCH_FOR_FUNCTION = 0; |
| 55 |
const STATE_IN_FUNCTION = 1; |
| 56 |
const STATE_END_OF_FUNCTION = 2; |
| 57 |
|
| 58 |
$state = STATE_SEARCH_FOR_FUNCTION; |
| 59 |
$currentFunction = $parameters = $returnType = ''; |
| 60 |
$content = $generator->generateHeaderStart(); |
| 61 |
$cppcontent = $generator->generatecppStart(); |
| 62 |
|
| 63 |
if (!$generator->checkForChanges()) { |
| 64 |
exit(0); |
| 65 |
} |
| 66 |
|
| 67 |
$lastFunc = '<none>'; |
| 68 |
$l = 0; |
| 69 |
|
| 70 |
foreach ($clustercpp as $cpp) { |
| 71 |
$l++; |
| 72 |
|
| 73 |
if ($state == STATE_SEARCH_FOR_FUNCTION && |
| 74 |
preg_match('/^\s*void\s+cluster::([^(]+)\s*\((.*)command_completion_event_t\s*callback\s*\)/', $cpp, $matches)) { |
| 75 |
$currentFunction = $matches[1]; |
| 76 |
$parameters = preg_replace('/,\s*$/', '', $matches[2]); |
| 77 |
if (!in_array($currentFunction, $blacklist)) { |
| 78 |
$state = STATE_IN_FUNCTION; |
| 79 |
} |
| 80 |
|
| 81 |
} elseif ($state == STATE_IN_FUNCTION) { |
| 82 |
|
| 83 |
if (preg_match('/^\}\s*$/', $cpp)) { |
| 84 |
$state = STATE_END_OF_FUNCTION; |
| 85 |
|
| 86 |
} elseif (preg_match('/rest_request<([^>]+)>/', $cpp, $matches)) { |
| 87 |
|
| 88 |
$returnType = $matches[1]; |
| 89 |
} elseif (preg_match('/rest_request_list<([^>]+)>/', $cpp, $matches)) { |
| 90 |
|
| 91 |
$returnType = $matches[1] . '_map'; |
| 92 |
} elseif (preg_match('/rest_request_vector<([^>]+)>/', $cpp, $matches)) { |
| 93 |
|
| 94 |
$returnType = $matches[1] . '_list'; |
| 95 |
} elseif (preg_match('/callback\(confirmation_callback_t\(\w+, ([^(]+)\(.*, \w+\)\)/', $cpp, $matches)) { |
| 96 |
|
| 97 |
$returnType = $matches[1]; |
| 98 |
} elseif (!empty($forcedReturn[$currentFunction])) { |
| 99 |
|
| 100 |
$returnType = $forcedReturn[$currentFunction]; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if ($state == STATE_END_OF_FUNCTION && !empty($currentFunction) && !empty($returnType)) { |
| 105 |
if (!in_array($currentFunction, $blacklist)) { |
| 106 |
$parameterList = explode(',', $parameters); |
| 107 |
$parameterNames = []; |
| 108 |
$parameterTypes = []; |
| 109 |
foreach ($parameterList as $parameter) { |
| 110 |
$parts = explode(' ', trim($parameter)); |
| 111 |
$name = trim(preg_replace('/[\s\*\&]+/', '', $parts[count($parts) - 1])); |
| 112 |
$parameterNames[] = $name; |
| 113 |
$parameterTypes[] = trim(substr($parameter, 0, strlen($parameter) - strlen($name))); |
| 114 |
} |
| 115 |
$content .= getComments($generator, $currentFunction, $returnType, $parameterNames) . "\n"; |
| 116 |
$fullParameters = getFullParameters($currentFunction, $parameterNames); |
| 117 |
$parameterNames = trim(join(', ', $parameterNames)); |
| 118 |
$parameterTypes = trim(join(', ', $parameterTypes)); |
| 119 |
if (!empty($parameterNames)) { |
| 120 |
$parameterNames = ', ' . $parameterNames; |
| 121 |
} |
| 122 |
$noDefaults = $parameters; |
| 123 |
$parameters = !empty($fullParameters) ? $fullParameters : $parameters; |
| 124 |
$content .= $generator->generateHeaderDef($returnType, $currentFunction, $parameters, $noDefaults, $parameterTypes, $parameterNames); |
| 125 |
$cppcontent .= $generator->generateCppDef($returnType, $currentFunction, $parameters, $noDefaults, $parameterTypes, $parameterNames); |
| 126 |
} |
| 127 |
$lastFunc = $currentFunction; |
| 128 |
$currentFunction = $parameters = $returnType = ''; |
| 129 |
$state = STATE_SEARCH_FOR_FUNCTION; |
| 130 |
} |
| 131 |
} |
| 132 |
if ($state != STATE_SEARCH_FOR_FUNCTION) { |
| 133 |
die("\n\n\nBuilding headers is broken ($l) - state machine finished in the middle of function $currentFunction (previous $lastFunc) with parameters $parameters rv $returnType state=$state\n\n\n"); |
| 134 |
} |
| 135 |
|
| 136 |
$content .= <<<EOT |
| 137 |
|
| 138 |
/* End of auto-generated definitions */ |
| 139 |
|
| 140 |
EOT; |
| 141 |
$cppcontent .= <<<EOT |
| 142 |
|
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
EOT; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
function getFullParameters(string $currentFunction, array $parameters): string |
| 156 |
{ |
| 157 |
global $header; |
| 158 |
foreach ($header as $line) { |
| 159 |
if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\((.*' . join('.*', $parameters) . '.*)command_completion_event_t\s*callback\s*/', $line, $matches)) { |
| 160 |
return preg_replace('/,\s*$/', '', $matches[1]); |
| 161 |
} |
| 162 |
} |
| 163 |
return ''; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
function getComments(StructGeneratorInterface $generator, string $currentFunction, string $returnType, array $parameters): string |
| 175 |
{ |
| 176 |
global $header; |
| 177 |
|
| 178 |
foreach ($header as $i => $line) { |
| 179 |
if (preg_match('/^\s*void\s+' . $currentFunction . '\s*\(.*' . join('.*', $parameters) . '.*command_completion_event_t\s*callback\s*/', $line)) { |
| 180 |
|
| 181 |
$lineIndex = 1; |
| 182 |
for ($n = $i; $n != 0; --$n, $lineIndex++) { |
| 183 |
$header[$n] = preg_replace('/^\t+/', '', $header[$n]); |
| 184 |
$header[$n] = preg_replace('/@see (.+?)$/', '@see dpp::cluster::' . $currentFunction . "\n * @see \\1", $header[$n]); |
| 185 |
$header[$n] = preg_replace('/@param callback .*$/', '@return ' . $returnType . ' returned object on completion', $header[$n]); |
| 186 |
if (preg_match('/\s*\* On success /i', $header[$n])) { |
| 187 |
$header[$n] = ""; |
| 188 |
} |
| 189 |
if (preg_match('/\s*\/\*\*\s*$/', $header[$n])) { |
| 190 |
$part = array_slice($header, $n, $lineIndex - 1); |
| 191 |
array_splice($part, count($part) - 1, 0, $generator->getCommentArray()); |
| 192 |
return str_replace("\n\n", "\n", join("\n", $part)); |
| 193 |
} |
| 194 |
} |
| 195 |
return ''; |
| 196 |
} |
| 197 |
} |
| 198 |
return ''; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
$generator->saveHeader($content); |
| 203 |
$generator->savecpp($cppcontent); |