Python Aidez-moi à traduire AS3 -> Python

Inscrit
26 Février 2016
Messages
82
Reactions
0
#1
Bonjour, j'essaye de reproduire ce que fais le client du jeu Blablaland (encodage et décodage des paquets) mais pour le traduire en Python c'est un peu plus dure.

Voici la classe en question en As3, il me semble que c'est la fonction readMessage() qui sert à lire les messages encodé.

(Class SocketMessage)

Code:
package net
{
    import flash.utils.*;

    public class SocketMessage extends Binary
    {

        public function SocketMessage()
        {
            return;
        }// end function

        public function duplicate() : SocketMessage
        {
            var _loc_1:* = new SocketMessage();
            _loc_1.writeBytes(this, 0, this.length);
            _loc_1.bitLength = bitLength;
            _loc_1.bitPosition = bitPosition;
            return _loc_1;
        }// end function

        public function readMessage(param1:ByteArray)
        {
            var _loc_2:* = 0;
            while (_loc_2 < param1.length)
            {
              
                if (param1[_loc_2] == 1)
                {
                    _loc_2 = _loc_2 + 1;
                    this.writeByte(param1[_loc_2] == 2 ? (1) : (0));
                }
                else
                {
                    this.writeByte(param1[_loc_2]);
                }
                _loc_2 = _loc_2 + 1;
            }
            bitLength = length * 8;
            return;
        }// end function

        public function exportMessage() : ByteArray
        {
            var _loc_1:* = new ByteArray();
            var _loc_2:* = 0;
            while (_loc_2 < this.length)
            {
              
                if (this[_loc_2] == 0)
                {
                    _loc_1.writeByte(1);
                    _loc_1.writeByte(3);
                }
                else if (this[_loc_2] == 1)
                {
                    _loc_1.writeByte(1);
                    _loc_1.writeByte(2);
                }
                else
                {
                    _loc_1.writeByte(this[_loc_2]);
                }
                _loc_2 = _loc_2 + 1;
            }
            return _loc_1;
        }// end function

    }
}
(Class Binary)
Code:
package net
{
    import flash.utils.*;

    public class Binary extends ByteArray
    {
        public var bitLength:Number;
        public var bitPosition:Number;
        public static var powList:Array;
        public static var __init:Boolean = _init();

        public function Binary()
        {
            this.bitLength = 0;
            this.bitPosition = 0;
            return;
        }// end function

        public function getDebug() : String
        {
            var _loc_1:* = new Array();
            _loc_1.push(this.bitPosition);
            _loc_1.push(this.bitLength);
            var _loc_2:* = 0;
            while (_loc_2 < length)
            {
              
                _loc_1.push(this[_loc_2].toString());
                _loc_2 = _loc_2 + 1;
            }
            return _loc_1.join("=");
        }// end function

        public function setDebug(param1:String)
        {
            var _loc_2:* = param1.split("=");
            this.bitPosition = _loc_2[0];
            this.bitLength = _loc_2[1];
            var _loc_3:* = 2;
            while (_loc_3 < _loc_2.length)
            {
              
                this.writeByte(Number(_loc_2[_loc_3]));
                _loc_3 = _loc_3 + 1;
            }
            return;
        }// end function

        public function bitReadString() : String
        {
            var _loc_4:* = 0;
            var _loc_1:* = "";
            var _loc_2:* = this.bitReadUnsignedInt(16);
            var _loc_3:* = 0;
            while (_loc_3 < _loc_2)
            {
              
                _loc_4 = this.bitReadUnsignedInt(8);
                if (_loc_4 == 255)
                {
                    _loc_4 = 8364;
                }
                _loc_1 = _loc_1 + String.fromCharCode(_loc_4);
                _loc_3 = _loc_3 + 1;
            }
            return _loc_1;
        }// end function

        public function bitReadBoolean() : Boolean
        {
            if (this.bitPosition == this.bitLength)
            {
                return false;
            }
            var _loc_1:* = Math.floor(this.bitPosition / 8);
            var _loc_2:* = this.bitPosition % 8;
            var _loc_3:* = this;
            var _loc_4:* = this.bitPosition + 1;
            _loc_3.bitPosition = _loc_4;
            return (this[_loc_1] >> 7 - _loc_2 & 1) == 1;
        }// end function

        public function bitReadUnsignedInt(param1:Number) : Number
        {
            var _loc_4:* = NaN;
            var _loc_5:* = NaN;
            var _loc_6:* = undefined;
            var _loc_7:* = undefined;
            var _loc_8:* = NaN;
            if (this.bitPosition + param1 > this.bitLength)
            {
                this.bitPosition = this.bitLength;
                return 0;
            }
            var _loc_2:* = 0;
            var _loc_3:* = param1;
            while (_loc_3 > 0)
            {
              
                _loc_4 = Math.floor(this.bitPosition / 8);
                _loc_5 = this.bitPosition % 8;
                _loc_6 = 8 - _loc_5;
                _loc_7 = Math.min(_loc_6, _loc_3);
                _loc_8 = this[_loc_4] >> _loc_6 - _loc_7 & (powList[_loc_7] - 1);
                _loc_2 = _loc_2 + _loc_8 * powList[_loc_3 - _loc_7];
                _loc_3 = _loc_3 - _loc_7;
                this.bitPosition = this.bitPosition + _loc_7;
            }
            return _loc_2;
        }// end function

        public function bitReadSignedInt(param1:Number) : Number
        {
            var _loc_2:* = this.bitReadBoolean();
            return this.bitReadUnsignedInt((param1 - 1)) * (_loc_2 ? (1) : (-1));
        }// end function

        public function bitReadBinaryData() : Binary
        {
            var _loc_1:* = this.bitReadUnsignedInt(16);
            return this.bitReadBinary(_loc_1);
        }// end function

        public function bitReadBinary(param1:uint) : Binary
        {
            var _loc_4:* = 0;
            var _loc_5:* = 0;
            var _loc_2:* = new Binary();
            var _loc_3:* = this.bitPosition;
            while (this.bitPosition - _loc_3 < param1)
            {
              
                if (this.bitPosition == this.bitLength)
                {
                    return _loc_2;
                }
                _loc_5 = Math.min(8, param1 - this.bitPosition + _loc_3);
                _loc_2.bitWriteUnsignedInt(_loc_5, this.bitReadUnsignedInt(_loc_5));
            }
            return _loc_2;
        }// end function

        public function bitWriteString(param1:String) : void
        {
            var _loc_4:* = 0;
            var _loc_2:* = Math.min(param1.length, (powList[16] - 1));
            this.bitWriteUnsignedInt(16, _loc_2);
            var _loc_3:* = 0;
            while (_loc_3 < _loc_2)
            {
              
                _loc_4 = param1.charCodeAt(_loc_3);
                if (_loc_4 == 8364)
                {
                    _loc_4 = 255;
                }
                this.bitWriteUnsignedInt(8, _loc_4);
                _loc_3 = _loc_3 + 1;
            }
            return;
        }// end function

        public function bitWriteSignedInt(param1:Number, param2:Number) : void
        {
            this.bitWriteBoolean(param2 >= 0);
            this.bitWriteUnsignedInt((param1 - 1), Math.abs(param2));
            return;
        }// end function

        public function bitWriteUnsignedInt(param1:Number, param2:Number) : void
        {
            var _loc_4:* = NaN;
            var _loc_5:* = undefined;
            var _loc_6:* = undefined;
            var _loc_7:* = NaN;
            param2 = Math.min((powList[param1] - 1), param2);
            var _loc_3:* = param1;
            while (_loc_3 > 0)
            {
              
                _loc_4 = this.bitLength % 8;
                if (_loc_4 == 0)
                {
                    writeBoolean(false);
                }
                _loc_5 = 8 - _loc_4;
                _loc_6 = Math.min(_loc_5, _loc_3);
                _loc_7 = this.Rshift(param2, Number(_loc_3 - _loc_6));
                this[(this.length - 1)] = this[(this.length - 1)] + _loc_7 * powList[_loc_5 - _loc_6];
                param2 = param2 - _loc_7 * powList[_loc_3 - _loc_6];
                _loc_3 = _loc_3 - _loc_6;
                this.bitLength = this.bitLength + _loc_6;
            }
            return;
        }// end function

        public function bitWriteUnsignedIntOLD(param1:Number, param2:Number) : void
        {
            param2 = Math.min((powList[param1] - 1), Math.abs(param2));
            var _loc_3:* = param2.toString(2);
            while (_loc_3.length < param1)
            {
              
                _loc_3 = "0" + _loc_3;
            }
            var _loc_4:* = 0;
            while (_loc_4 < param1)
            {
              
                this.bitWriteBoolean(_loc_3.charAt(_loc_4) == "1");
                _loc_4 = _loc_4 + 1;
            }
            return;
        }// end function

        public function bitWriteBoolean(param1:Boolean) : void
        {
            var _loc_2:* = this.bitLength % 8;
            if (_loc_2 == 0)
            {
                writeBoolean(false);
            }
            if (param1)
            {
                this[(this.length - 1)] = this[(this.length - 1)] + powList[7 - _loc_2];
            }
            var _loc_3:* = this;
            var _loc_4:* = this.bitLength + 1;
            _loc_3.bitLength = _loc_4;
            return;
        }// end function

        public function bitWriteBinaryData(param1:Binary) : void
        {
            var _loc_2:* = Math.min(param1.bitLength, (powList[16] - 1));
            this.bitWriteUnsignedInt(16, _loc_2);
            this.bitWriteBinary(param1);
            return;
        }// end function

        public function bitWriteBinary(param1:Binary) : void
        {
            var _loc_3:* = 0;
            var _loc_4:* = 0;
            param1.bitPosition = 0;
            var _loc_2:* = param1.bitLength;
            while (_loc_2)
            {
              
                _loc_3 = Math.min(8, _loc_2);
                _loc_4 = param1.bitReadUnsignedInt(_loc_3);
                this.bitWriteUnsignedInt(_loc_3, _loc_4);
                _loc_2 = _loc_2 - _loc_3;
            }
            return;
        }// end function

        public function bitCopyObject(param1:Object)
        {
            this.bitPosition = param1.bitPosition;
            this.bitLength = param1.bitLength;
            param1.position = 0;
            var _loc_2:* = 0;
            while (_loc_2 < param1.length)
            {
              
                writeByte(param1.readByte());
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        public function Rshift(param1:Number, param2:int) : Number
        {
            return Math.floor(param1 / powList[param2]);
        }// end function

        public function Lshift(param1:Number, param2:int) : Number
        {
            return param1 * powList[param2];
        }// end function

        public static function _init() : Boolean
        {
            powList = new Array();
            var _loc_1:* = 0;
            while (_loc_1 <= 32)
            {
              
                powList.push(Math.pow(2, _loc_1));
                _loc_1 = _loc_1 + 1;
            }
            return true;
        }// end function

    }
}
J'avais fais un petit bout de code en Python :

Code:
# -*- coding: utf-8 -*-

class SocketMessage:

    def readMessage(ByteArray):
        var = 0
        while var < len(ByteArray):
            if var[1] == 1:
                 var += 1
 

Labo

Membre Actif
Inscrit
16 Aout 2013
Messages
799
Reactions
15
#2
On ne fait pas des codes sur demande ici. Explique tes problèmes et on t'aidera.
En plus, t'as écrit une class sans méthode __init__ et var[1] au lieu de ByteArray[var] (je crois). Sans compter que tu pourrais remplacer ça par un count.
C'est pas en traduisant que l'on programme. Si tu ne sais pas ce que tu veux faire, tu n'arriveras pas à coder.
 
Inscrit
26 Février 2016
Messages
82
Reactions
0
#3
On ne fait pas des codes sur demande ici. Explique tes problèmes et on t'aidera.
En plus, t'as écrit une class sans méthode __init__ et var[1] au lieu de ByteArray[var] (je crois). Sans compter que tu pourrais remplacer ça par un count.
C'est pas en traduisant que l'on programme. Si tu ne sais pas ce que tu veux faire, tu n'arriveras pas à coder.
Je veux simplement traduire la fonction readMessage du client pour reproduire le décodage et encodage des paquets dans mon émulateur.
 

BlueDream

Administrateur
Membre du personnel
Inscrit
8 Decembre 2012
Messages
2 010
Reactions
149
#4
Bonsoir,

Je t'invite à consulter les Règles sur la demande d'aide la prochaine fois avant de poster.
http://cadernis.fr/index.php?threads/règles-sur-la-demande-daide.1531/

"La demande de bots, ou code est interdite."
Comme l'a dit Labo, on ne fait pas de code sous demande, nous pouvons t'aider à traduire en effet, mais la tu nous expose ton code sans même la moindre explication sur tes difficultés.

De plus merci d'utiliser la coloration syntaxique la prochaine fois, afin que la lisibilité de ton code soit la meilleure.
http://cadernis.fr/index.php?threads/utilisation-des-balises-bb-codes.1532/
 
Haut Bas