Skip to content

Commit 4f8ab6b

Browse files
committed
Autoadd crc to schema
1 parent 5cc68dd commit 4f8ab6b

3 files changed

Lines changed: 68 additions & 45 deletions

File tree

src/TL_filerefs.tl

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
// Root
2-
fileReferenceOrigins db_schema:string ctxs:Vector<Origin> = FileReferenceOrigins;
2+
fileReferenceOrigins#8260b721 db_schema:string ctxs:Vector<Origin> = FileReferenceOrigins;;
33

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

66
// For string => TypedOp dictionaries
7-
typedOpArg key:string value:TypedOp = TypedOpArg;
7+
typedOpArg#3a2930c2 key:string value:TypedOp = TypedOpArg;;
88

99
// Actions
10-
callOp method:string args:Vector<TypedOpArg> stored_constructor:string = ActionOp;
11-
getMessageOp flags:# peer:TypedOp id:TypedOp from_scheduled:flags.0?TypedOp stored_constructor:string = ActionOp;
12-
noOp why:string = ActionOp;
10+
callOp#59aef5ce method:string args:Vector<TypedOpArg> stored_constructor:string = ActionOp;;
11+
getMessageOp#4f27329e flags:# peer:TypedOp id:TypedOp from_scheduled:flags.0?TypedOp stored_constructor:string = ActionOp;;
12+
noOp#c4d8df73 why:string = ActionOp;;
1313

1414
// Field extraction path
15-
paramNotFlag = ParamFlag;
16-
paramIsFlagAbortIfEmpty = ParamFlag;
17-
paramIsFlagFallback fallback:TypedOp = ParamFlag;
18-
paramIsFlagPassthrough = ParamFlag;
15+
paramNotFlag#acd9d5cf = ParamFlag;;
16+
paramIsFlagAbortIfEmpty#f8fe9fee = ParamFlag;;
17+
paramIsFlagFallback#202b77a1 fallback:TypedOp = ParamFlag;;
18+
paramIsFlagPassthrough#1dc6e17d = ParamFlag;;
1919

20-
pathPart flags:# constructor:string param:string flag:ParamFlag = PathPart;
20+
pathPart#7b8b07d4 flags:# constructor:string param:string flag:ParamFlag = PathPart;;
2121

22-
path parts:Vector<PathPart> = Path;
23-
pathParent parts:Vector<PathPart> = Path;
22+
path#0c3586a2 parts:Vector<PathPart> = Path;;
23+
pathParent#58f13684 parts:Vector<PathPart> = Path;;
2424

2525
// Extractor+storer
26-
extractAndStore from:Path to:string = FieldExtractor;
26+
extractAndStore#72069549 from:Path to:string = FieldExtractor;;
2727

28-
extractInputStickerSetFromDocumentAttributesAndStore from:Path to:string = FieldExtractor;
29-
extractInputStickerSetFromStickerSetAndStore from:Path to:string = FieldExtractor;
28+
extractInputStickerSetFromDocumentAttributesAndStore#369d8d14 from:Path to:string = FieldExtractor;;
29+
extractInputStickerSetFromStickerSetAndStore#c167d470 from:Path to:string = FieldExtractor;;
3030

31-
extractPeerIdFromPeerAndStore from:Path to:string = FieldExtractor;
32-
extractPeerIdFromInputPeerAndStore from:Path to:string = FieldExtractor;
31+
extractPeerIdFromPeerAndStore#7d33019c from:Path to:string = FieldExtractor;;
32+
extractPeerIdFromInputPeerAndStore#a51acfb4 from:Path to:string = FieldExtractor;;
3333

34-
extractChannelIdFromChannelAndStore from:Path to:string = FieldExtractor;
35-
extractChannelIdFromInputChannelAndStore from:Path to:string = FieldExtractor;
34+
extractChannelIdFromChannelAndStore#5675bc97 from:Path to:string = FieldExtractor;;
35+
extractChannelIdFromInputChannelAndStore#b662660e from:Path to:string = FieldExtractor;;
3636

37-
extractUserIdFromUserAndStore from:Path to:string = FieldExtractor;
38-
extractUserIdFromInputUserAndStore from:Path to:string = FieldExtractor;
37+
extractUserIdFromUserAndStore#4778ec63 from:Path to:string = FieldExtractor;;
38+
extractUserIdFromInputUserAndStore#7720aa2e from:Path to:string = FieldExtractor;;
3939

4040
// Typed constructors, the type is specified to simplify codegen,
4141
// but isn't strictly necessary as it can be inferred from the TypedOpOp.
4242
// It is fully pre-validated during the generation of the definition file.
43-
typedOp type:string op:TypedOpOp = TypedOp;
43+
typedOp#705b10ec type:string op:TypedOpOp = TypedOp;;
4444

4545
// The actual ops
46-
copyOp from:FieldExtractor = TypedOpOp;
46+
copyOp#1630c9f5 from:FieldExtractor = TypedOpOp;;
4747

48-
getInputChannelByIdOp from:FieldExtractor = TypedOpOp;
49-
getInputUserByIdOp from:FieldExtractor = TypedOpOp;
50-
getInputPeerByIdOp from:FieldExtractor = TypedOpOp;
48+
getInputChannelByIdOp#3eb8d855 from:FieldExtractor = TypedOpOp;;
49+
getInputUserByIdOp#a3a4e6c2 from:FieldExtractor = TypedOpOp;;
50+
getInputPeerByIdOp#acfb2a4a from:FieldExtractor = TypedOpOp;;
5151

5252
// Literals & constructors (methods not allowed or needed here)
53-
constructorOp constructor:string args:Vector<TypedOpArg> = TypedOpOp;
53+
constructorOp#107f8d8a constructor:string args:Vector<TypedOpArg> = TypedOpOp;;
5454

55-
vectorOp values:Vector<TypedOp> = TypedOpOp;
55+
vectorOp#f8fb8f72 values:Vector<TypedOp> = TypedOpOp;;
5656

57-
intLiteralOp value:int = TypedOpOp;
58-
longLiteralOp value:long = TypedOpOp;
59-
stringLiteralOp value:string = TypedOpOp;
60-
boolLiteralOp value:Bool = TypedOpOp;
61-
doubleLiteralOp value:double = TypedOpOp;
62-
themeFormatLiteralOp = TypedOpOp;
57+
intLiteralOp#cbfabe7c value:int = TypedOpOp;;
58+
longLiteralOp#d08b8d3a value:long = TypedOpOp;;
59+
stringLiteralOp#2b56ea8e value:string = TypedOpOp;;
60+
boolLiteralOp#37e07911 value:Bool = TypedOpOp;;
61+
doubleLiteralOp#3651e3bf value:double = TypedOpOp;;
62+
themeFormatLiteralOp#8e4f9208 = TypedOpOp;;

tools/FileRefExtractor/BuildMode/Ast.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,38 @@ public function __construct(
4747
) {
4848
}
4949

50-
public function write(string $dbFile, string $schemaFile): void
50+
private static function crc(string $schema): string
5151
{
52-
$schema = '';
53-
foreach ($this->outputSchema as $constructor => $params) {
54-
$schema .= self::stringifySchema($constructor, $params)."\n";
52+
$clean = preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $schema);
53+
$id = hash('crc32b', $clean);
54+
$id = str_pad($id, 8, '0', STR_PAD_LEFT);
55+
return $id;
56+
}
57+
58+
public function finalize(string $schemaFile, string $refMapFile, string $dbSchemaFile): void
59+
{
60+
$schema = explode("\n", file_get_contents($schemaFile));
61+
foreach ($schema as &$line) {
62+
$line = trim($line);
63+
if (str_starts_with($line, '//') || !$line) {
64+
continue;
65+
}
66+
$id = self::crc($line);
67+
68+
$line = explode(" ", $line, 2);
69+
$line[0] .= "#$id";
70+
$line = implode(" ", $line);
71+
$line .= ';';
5572
}
73+
$schema = implode("\n", $schema);
5674
file_put_contents($schemaFile, $schema);
57-
$value = ['_' => 'fileReferenceOrigins', 'db_schema' => $schema, 'ctxs' => $this->output];
75+
76+
$dbSchema = '';
77+
foreach ($this->outputSchema as $constructor => $params) {
78+
$dbSchema .= self::stringifySchema($constructor, $params)."\n";
79+
}
80+
file_put_contents($dbSchemaFile, $dbSchema);
81+
$value = ['_' => 'fileReferenceOrigins', 'db_schema' => $dbSchema, 'ctxs' => $this->output];
5882
Magic::start(false);
5983

6084
$s = new TLSchema;
@@ -64,7 +88,7 @@ public function write(string $dbFile, string $schemaFile): void
6488
$serialized = $TL->serializeObject(['type' => 'FileReferenceOrigins'], $value, '');
6589
$valueDe = $TL->deserialize($serialized, ['type' => '', 'connection' => null, 'encrypted' => true]);
6690
Assert::true($value == $valueDe);
67-
file_put_contents($dbFile, $serialized);
91+
file_put_contents($refMapFile, $serialized);
6892
}
6993

7094
private static function stringifySchema(string $constructor, array $params): string
@@ -84,9 +108,7 @@ private static function stringifySchema(string $constructor, array $params): str
84108
}
85109
$paramsStr .= '= FileSource;';
86110

87-
$clean = preg_replace(['/:bytes /', '/;/', '/#[a-f0-9]+ /', '/ [a-zA-Z0-9_]+\\:flags\\.[0-9]+\\?true/', '/[<]/', '/[>]/', '/ /', '/^ /', '/ $/', '/\\?bytes /', '/{/', '/}/'], [':string ', '', ' ', '', ' ', ' ', ' ', '', '', '?string ', '', ''], $paramsStr);
88-
$id = hash('crc32b', $clean);
89-
$id = str_pad($id, 8, '0', STR_PAD_LEFT);
111+
$id = self::crc($paramsStr);
90112
$paramsStr = substr($paramsStr, \strlen($constructor)+1);
91113
return "$constructor#$id $paramsStr";
92114
}

tools/gen_filerefmap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ static function (array $stack) use ($locations, $TL, $tmp, &$validated, $storyMe
607607
}
608608
}
609609

610-
$output->write(
610+
$output->finalize(
611+
__DIR__.'/../src/TL_filerefs.tl',
611612
__DIR__.'/../src/file_ref_map.dat',
612613
__DIR__.'/../src/TL_filerefs_db.tl',
613614
);

0 commit comments

Comments
 (0)