Skip to content

Commit a31db1d

Browse files
committed
Finalize file reference db schema
1 parent 1fae26a commit a31db1d

4 files changed

Lines changed: 41 additions & 20 deletions

File tree

src/TL_filerefs.tl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Root
2-
fileReferenceOrigins ctxs:Vector<Origin> = FileReferenceOrigins;
2+
fileReferenceOrigins db_schema:string ctxs:Vector<Origin> = FileReferenceOrigins;
33

44
origin flags:# predicate:string is_constructor:flags.0?true action:ActionOp needs_parent:flags.3?string parent_is_constructor:flags.4?true = origin;
55

tools/FileRefExtractor/BuildMode/Ast.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public function __construct(
4949

5050
public function getOutput(): string
5151
{
52-
$value = ['_' => 'fileReferenceOrigins', 'ctxs' => $this->output];
52+
$schema = '';
53+
foreach ($this->outputSchema as $constructor => $params) {
54+
$schema .= self::stringifySchema($constructor, $params)."\n";
55+
}
56+
$value = ['_' => 'fileReferenceOrigins', 'db_schema' => $schema, 'ctxs' => $this->output];
5357
Magic::start(false);
5458

5559
$s = new TLSchema;
@@ -65,10 +69,24 @@ private static function stringifySchema(string $constructor, array $params): str
6569
{
6670
$paramsStr = "$constructor ";
6771
foreach ($params as $name => $type) {
72+
Assert::notContains($type, 'InputPeer', "$constructor cannot contain InputPeer");
73+
Assert::notContains($type, 'InputUser', "$constructor cannot contain InputUser");
74+
Assert::notContains($type, 'InputChannel', "$constructor cannot contain InputChannel");
75+
if ($constructor !== 'fileSourceBotApp'
76+
&& $constructor !== 'fileSourceTheme'
77+
&& $constructor !== 'fileSourceWallPaper'
78+
) {
79+
Assert::notContains($name, 'access_hash', "$constructor cannot contain an access hash");
80+
}
6881
$paramsStr .= "$name:$type ";
6982
}
7083
$paramsStr .= '= FileSource;';
71-
return $paramsStr;
84+
85+
$clean = preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $paramsStr);
86+
$id = hash('crc32b', $clean);
87+
$id = str_pad($id, 8, '0', STR_PAD_LEFT);
88+
$paramsStr = substr($paramsStr, \strlen($constructor)+1);
89+
return "$constructor#$id $paramsStr";
7290
}
7391
public function addNode(TLContext $ctx, ?array $action = null, ?string $why = null): void
7492
{
@@ -98,7 +116,7 @@ public function addNode(TLContext $ctx, ?array $action = null, ?string $why = nu
98116
}
99117
$names = [
100118
'flags' => '#',
101-
...$names
119+
...$names,
102120
];
103121
}
104122

@@ -132,7 +150,6 @@ public function addNode(TLContext $ctx, ?array $action = null, ?string $why = nu
132150
$this->outputSchema[$constructor] = $names;
133151
}
134152

135-
136153
$this->storedFlags = 0;
137154
$this->stored = [];
138155
$this->storedNames = [];

tools/FileRefExtractor/Ops/CopyMethodCallOp.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ public function build(TLContext $tl): void
4545
if (isset($arg['pow'])) {
4646
$args[$arg['name']] = new CopyOp([[$this->method, $arg['name'], Path::FLAG_PASSTHROUGH]]);
4747
} else {
48-
$args[$arg['name']] = new CopyOp([[$this->method, $arg['name']]]);
48+
if ($arg['type'] === 'InputPeer') {
49+
$args[$arg['name']] = new GetInputPeerOp(new Path([[$this->method, $arg['name']]]));
50+
} elseif ($arg['type'] === 'InputUser') {
51+
$args[$arg['name']] = new GetInputUserOp(new Path([[$this->method, $arg['name']]]));
52+
} else {
53+
$args[$arg['name']] = new CopyOp([[$this->method, $arg['name']]]);
54+
}
4955
}
5056
}
5157
$result = new CallOp(

tools/gen_filerefmap.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
$TL = new TLWrapper($TL);
3939
$locations = [];
40-
4140
Logger::log("Generating file reference map...");
4241

4342
foreach ($TL->getConstructorsOfType('Message') as $constructor => $_) {
@@ -196,19 +195,19 @@
196195

197196
foreach (['stories.createAlbum', 'stories.getAlbums', 'stories.updateAlbum'] as $m) {
198197
$locations[$m][] = new CallOp('stories.getAlbums', [
199-
'peer' => new CopyOp([[$m, 'peer']]),
198+
'peer' => new GetInputPeerOp(new Path([[$m, 'peer']])),
200199
'hash' => new PrimitiveLiteralOp('long', 0),
201200
], 'fileSourceStoryAlbum');
202201
}
203202

204203
$locations['bots.getPreviewMedias'][] = new CopyMethodCallOp('bots.getPreviewMedias', 'fileSourceBotPreviewMedia');
205204
$locations['bots.getPreviewInfo'][] = new CopyMethodCallOp('bots.getPreviewInfo', 'fileSourceBotPreviewInfo');
206205
$locations['bots.addPreviewMedia'][] = new CallOp('bots.getPreviewInfo', [
207-
'bot' => new CopyOp([['bots.addPreviewMedia', 'bot']]),
206+
'bot' => new GetInputUserOp(new Path([['bots.addPreviewMedia', 'bot']])),
208207
'lang_code' => new CopyOp([['bots.addPreviewMedia', 'lang_code']]),
209208
], 'fileSourceBotPreviewInfo');
210209
$locations['bots.editPreviewMedia'][] = new CallOp('bots.getPreviewInfo', [
211-
'bot' => new CopyOp([['bots.editPreviewMedia', 'bot']]),
210+
'bot' => new GetInputUserOp(new Path([['bots.editPreviewMedia', 'bot']])),
212211
'lang_code' => new CopyOp([['bots.editPreviewMedia', 'lang_code']]),
213212
], 'fileSourceBotPreviewInfo');
214213

@@ -249,7 +248,7 @@
249248
$locations['starsTransaction'][] = new CallOp(
250249
'payments.getStarsTransactionsByID',
251250
[
252-
'peer' => new CopyOp(new Path([[$method, 'peer']], true)),
251+
'peer' => new GetInputPeerOp(new Path([[$method, 'peer']], true)),
253252
...($method === 'payments.getStarsSubscriptions' ? [] : ['ton' => new CopyOp(new Path([[$method, 'ton', Path::FLAG_PASSTHROUGH]], true))]),
254253
'id' => new ArrayOp(new ConstructorOp(
255254
'inputStarsTransaction',
@@ -358,7 +357,7 @@
358357
$locations['photo'][] = new CallOp(
359358
'photos.getUserPhotos',
360359
[
361-
'user_id' => new CopyOp(new Path([['photos.getUserPhotos', 'user_id']], true)),
360+
'user_id' => new GetInputUserOp(new Path([['photos.getUserPhotos', 'user_id']], true)),
362361
'offset' => new PrimitiveLiteralOp('int', -1),
363362
'max_id' => new CopyOp([['photo', 'id']]),
364363
'limit' => new PrimitiveLiteralOp('int', 1),
@@ -384,7 +383,7 @@
384383
$locations[$method][] = new CallOp(
385384
'photos.getUserPhotos',
386385
[
387-
'user_id' => new CopyOp(
386+
'user_id' => new GetInputUserOp(new Path(
388387
[[
389388
$method,
390389
'bot',
@@ -393,7 +392,7 @@
393392
[]
394393
),
395394
]]
396-
),
395+
)),
397396
'offset' => new PrimitiveLiteralOp('int', -1),
398397
'max_id' => new CopyOp([[$method, ''], ['photos.photo', 'photo'], ['photo', 'id']]),
399398
'limit' => new PrimitiveLiteralOp('int', 1),
@@ -404,8 +403,8 @@
404403
$locations['photos.uploadContactProfilePhoto'][] = new CallOp(
405404
'photos.getUserPhotos',
406405
[
407-
'user_id' => new CopyOp(
408-
[['photos.uploadContactProfilePhoto', 'user_id']],
406+
'user_id' => new GetInputUserOp(
407+
new Path([['photos.uploadContactProfilePhoto', 'user_id']]),
409408
),
410409
'offset' => new PrimitiveLiteralOp('int', -1),
411410
'max_id' => new CopyOp([['photos.uploadContactProfilePhoto', ''], ['photos.photo', 'photo'], ['photo', 'id']]),
@@ -495,21 +494,20 @@
495494
unset($stack[$pos]);
496495
};
497496

498-
499497
$pre = [
500498
'fileSourceMessage' => [
501499
'flags' => '#',
502500
'from_scheduled' => 'flags.0?true',
503501
'peer' => 'long',
504-
'id' => 'int'
502+
'id' => 'int',
505503
],
506504
'fileSourceStarsTransaction' => [
507505
'flags' => '#',
508-
'peer' => 'InputPeer',
506+
'peer' => 'long',
509507
'id' => 'string',
510508
'refund' => 'flags.0?true',
511509
'ton' => 'flags.1?true',
512-
]
510+
],
513511
];
514512

515513
$validated = [];

0 commit comments

Comments
 (0)