From 5e9df575da5e309f50adbc57cf84eb9a80c130e3 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 27 Jul 2017 16:59:46 +0200 Subject: [PATCH] DAMN LOOK AT THE SPEED METER --- src/danog/MadelineProto/MTProto.php | 2 +- .../MadelineProto/TL/PrettyException.php | 2 +- src/danog/MadelineProto/TL/TL.php | 2 +- src/danog/MadelineProto/TL/TLConstructor.php | 95 +++++++++---------- src/danog/MadelineProto/TL/TLMethod.php | 57 +++++------ src/danog/MadelineProto/TL/TLParams.php | 4 +- 6 files changed, 77 insertions(+), 85 deletions(-) diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index d92fee2f..b78a92d3 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -44,7 +44,7 @@ class MTProto extends \Volatile use \danog\MadelineProto\Wrappers\DialogHandler; use \danog\MadelineProto\Wrappers\Login; - const V = 64; + const V = 65; const NOT_LOGGED_IN = 0; const WAITING_CODE = 1; diff --git a/src/danog/MadelineProto/TL/PrettyException.php b/src/danog/MadelineProto/TL/PrettyException.php index 86419375..95e2a278 100644 --- a/src/danog/MadelineProto/TL/PrettyException.php +++ b/src/danog/MadelineProto/TL/PrettyException.php @@ -27,7 +27,7 @@ trait PrettyException foreach (array_reverse($this->getTrace()) as $k => $frame) { if (isset($frame['function']) && in_array($frame['function'], ['serialize_params', 'serialize_object'])) { if ($frame['args'][2] !== '') { - $this->tl_trace .= $tl ? "['".$frame['args'][2]."']" : "While serializing: \t".$frame['args'][2]; + $this->tl_trace .= $tl ? "['".$frame['args'][2]."']" : "While serializing: \t".$frame['args'][2]; $tl = true; } } else { diff --git a/src/danog/MadelineProto/TL/TL.php b/src/danog/MadelineProto/TL/TL.php index 4d120ee0..24df5e21 100644 --- a/src/danog/MadelineProto/TL/TL.php +++ b/src/danog/MadelineProto/TL/TL.php @@ -338,7 +338,6 @@ trait TL } $auto = false; - if ((!$this->is_array($object) || (isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) && $this->in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) { $object = $this->get_info($object); if (!isset($object[$type['type']])) { @@ -348,6 +347,7 @@ trait TL } if (!isset($object['_'])) { $constructorData = $this->constructors->find_by_predicate($type['type'], $layer); + if ($constructorData === false) { throw new Exception('Predicate was not set!'); } diff --git a/src/danog/MadelineProto/TL/TLConstructor.php b/src/danog/MadelineProto/TL/TLConstructor.php index 1c8bfbe1..510dc1ed 100644 --- a/src/danog/MadelineProto/TL/TLConstructor.php +++ b/src/danog/MadelineProto/TL/TLConstructor.php @@ -18,78 +18,75 @@ class TLConstructor extends \Volatile use \danog\MadelineProto\Tools; use TLParams; - public $id = []; - public $predicate = []; - public $type = []; - public $params = []; - public $layer = []; - public $key = 0; + public $by_id = []; + public $by_predicate_and_layer = []; + public $layers = []; + //public $type = []; + //public $params = []; + //public $layer = []; + //public $key = 0; public function __sleep() { - return ['id', 'predicate', 'type', 'params', 'layer', 'key']; + return ['by_predicate_and_layer', 'by_id', 'layers']; } public function add($json_dict, $scheme_type) { - $this->id[$this->key] = $json_dict['id']; - $this->predicate[$this->key] = (string) ((($scheme_type === 'mtproto' && $json_dict['predicate'] === 'message') ? 'MT' : '').$json_dict['predicate']); - $this->type[$this->key] = (($scheme_type === 'mtproto' && $json_dict['type'] === 'Message') ? 'MT' : '').$json_dict['type']; - $this->params[$this->key] = $json_dict['params']; + $predicate = (string) ((($scheme_type === 'mtproto' && $json_dict['predicate'] === 'message') ? 'MT' : '').$json_dict['predicate']); + $this->by_id[$json_dict['id']] = ['predicate' => $predicate, 'params' => $json_dict['params'], 'type' => (($scheme_type === 'mtproto' && $json_dict['type'] === 'Message') ? 'MT' : '').$json_dict['type']]; if ($scheme_type === 'secret') { - $this->layer[$this->key] = $json_dict['layer']; - } - $this->parse_params($this->key, $scheme_type === 'mtproto'); - $this->key++; + $this->by_id[$json_dict['id']]['layer'] = $json_dict['layer']; + $this->layers[$json_dict['layer']] = $json_dict['layer']; + ksort($this->layers); + } else $json_dict['layer'] = ''; + $this->by_predicate_and_layer[$json_dict['predicate'].$json_dict['layer']] = $json_dict['id']; + $this->parse_params($json_dict['id'], $scheme_type === 'mtproto'); } public function find_by_type($type) { - $key = array_search($type, (array) $this->type, true); - - return ($key === false) ? false : [ - 'id' => $this->id[$key], - 'predicate' => $this->predicate[$key], - 'type' => $this->type[$key], - 'params' => $this->array_cast_recursive($this->params[$key]), - ]; + foreach ($this->by_id as $id => $constructor) { + if ($constructor['type'] === $type) { + $constructor['id'] = $id; + $constructor['params'] = $this->array_cast_recursive($constructor['params']); + return $constructor; + } + } + return false; } public function find_by_predicate($predicate, $layer = -1) { if ($layer !== -1) { - $newlayer = -1; - $keys = array_keys((array) $this->predicate, $predicate); - foreach ($keys as $k) { - if ($this->layer[$k] <= $layer && $this->layer[$k] > $newlayer) { - $key = $k; - $newlayer = $this->layer[$k]; - } - if (!isset($key)) { - $key = $keys[0]; + foreach ($this->layers as $alayer) { + if ($alayer <= $layer && isset($this->by_predicate_and_layer[$predicate.$alayer])) { + $chosenid = $this->by_predicate_and_layer[$predicate.$alayer]; } } - } else { - $key = array_search($predicate, (array) $this->predicate, true); + if (!isset($chosenid)) return false; + $constructor = $this->by_id[$chosenid]; + $constructor['id'] = $chosenid; + $constructor['params'] = $this->array_cast_recursive($constructor['params']); + return $constructor; } - - return ($key === false) ? false : [ - 'id' => $this->id[$key], - 'predicate' => $this->predicate[$key], - 'type' => $this->type[$key], - 'params' => $this->array_cast_recursive($this->params[$key]), - ]; + if (isset($this->by_predicate_and_layer[$predicate])) { + $constructor = $this->by_id[$this->by_predicate_and_layer[$predicate]]; + $constructor['id'] = $this->by_predicate_and_layer[$predicate]; + $constructor['params'] = $this->array_cast_recursive($constructor['params']); + return $constructor; + } + return false; } public function find_by_id($id) { - $key = array_search($id, (array) $this->id, true); - - return ($key === false) ? false : [ - 'id' => $this->id[$key], - 'predicate' => $this->predicate[$key], - 'type' => $this->type[$key], - 'params' => $this->array_cast_recursive($this->params[$key]), - ]; + if (isset($this->by_id[$id])) { + $constructor = $this->by_id[$id]; + $constructor['id'] = $id; + $constructor['params'] = $this->array_cast_recursive($constructor['params']); + return $constructor; + } + return false; } } diff --git a/src/danog/MadelineProto/TL/TLMethod.php b/src/danog/MadelineProto/TL/TLMethod.php index 925d5a12..32ff14bc 100644 --- a/src/danog/MadelineProto/TL/TLMethod.php +++ b/src/danog/MadelineProto/TL/TLMethod.php @@ -17,54 +17,49 @@ class TLMethod extends \Volatile use \danog\Serializable; use \danog\MadelineProto\Tools; use TLParams; - public $id = []; - public $method = []; - public $type = []; - public $params = []; + public $by_id = []; + public $by_method = []; public $method_namespace = []; - public $key = 0; public function __sleep() { - return ['id', 'method', 'type', 'params', 'method_namespace', 'key']; + return ['by_id', 'by_method', 'method_namespace']; } public function add($json_dict) { - $this->id[$this->key] = $json_dict['id']; - $this->method[$this->key] = $json_dict['method']; - $this->type[$this->key] = $json_dict['type']; - $this->params[$this->key] = $json_dict['params']; + $this->by_id[$json_dict['id']] = ['method' => $json_dict['method'], 'type' => $json_dict['type'], 'params' => $json_dict['params']]; + $this->by_method[$json_dict['method']] = $json_dict['id']; + $namespace = explode('.', $json_dict['method']); if (isset($namespace[1])) { $this->method_namespace[$namespace[1]] = $namespace[0]; } - $this->parse_params($this->key); - $this->key++; + $this->parse_params($json_dict['id']); } - public function find_by_method($method) - { - $key = array_search($method, (array) $this->method, true); - - return ($key === false) ? false : [ - 'id' => $this->id[$key], - 'method' => $this->method[$key], - 'type' => $this->type[$key], - 'params' => $this->array_cast_recursive($this->params[$key]), - ]; - } public function find_by_id($id) { - $key = array_search($id, (array) $this->id, true); - - return ($key === false) ? false : [ - 'id' => $this->id[$key], - 'method' => $this->method[$key], - 'type' => $this->type[$key], - 'params' => $this->array_cast_recursive($this->params[$key]), - ]; + if (isset($this->by_id[$id])) { + $method = $this->by_id[$id]; + $method['id'] = $id; + $method['params'] = $this->array_cast_recursive($method['params']); + return $method; + } + return false; } + + public function find_by_method($method_name) + { + if (isset($this->by_method[$method_name])) { + $method = $this->by_id[$this->by_method[$method_name]]; + $method['id'] = $this->by_method[$method_name]; + $method['params'] = $this->array_cast_recursive($method['params']); + return $method; + } + return false; + } + } diff --git a/src/danog/MadelineProto/TL/TLParams.php b/src/danog/MadelineProto/TL/TLParams.php index 0bd128e3..511d33e7 100644 --- a/src/danog/MadelineProto/TL/TLParams.php +++ b/src/danog/MadelineProto/TL/TLParams.php @@ -16,7 +16,7 @@ trait TLParams { public function parse_params($key, $mtproto = false) { - foreach ($this->params[$key] as $kkey => $param) { + foreach ($this->by_id[$key]['params'] as $kkey => $param) { if (preg_match('/^flags\.\d*\?/', $param['type'])) { $flag = explode('?', explode('flags.', $param['type'])[1]); $param['pow'] = pow(2, $flag[0]); @@ -36,7 +36,7 @@ trait TLParams } $param['type'] = (($mtproto && $param['type'] === 'Message') ? 'MT' : '').$param['type']; $param['type'] = (($mtproto && $param['type'] === '%Message') ? '%MTMessage' : $param['type']); - $this->params[$key][$kkey] = $param; + $this->by_id[$key]['params'][$kkey] = $param; } } }