public void IsFinishedTest()
        {
            var reader = new ByteReader(new byte[] { 0x01, 0x02 });

            Assert.IsFalse(reader.IsFinished);

            reader.GetByte();
            reader.GetByte();

            Assert.IsTrue(reader.IsFinished);
        }
        public void NormalOperationTest()
        {
            byte[] bytes  = File.ReadAllBytes(Path.Combine("Files", "structdata.dat"));
            var    reader = new ByteReader(bytes);

            Assert.AreEqual(35000, reader.GetUshort());
            Assert.AreEqual(230, reader.GetByte());
            Assert.AreEqual("spam", reader.GetString(4));
            Assert.AreEqual(4211001100, reader.GetUint());
            Assert.AreEqual(1, reader.GetBits(1));
            Assert.AreEqual(2, reader.GetBits(4));
            Assert.AreEqual(1, reader.GetBits(3));
            Assert.AreEqual(10, reader.GetByte());
        }
Exemple #3
0
        public void HandleData(byte[] data)
        {
            using (var reader = new ByteReader(data))
            {
                var length = reader.GetUInt32() - 1;    // -1 because we'll read the type next
                var type   = (SftpPacketType)reader.GetByte();

                if (packetTypes.ContainsKey(type))
                {
                    var packet = Activator.CreateInstance(packetTypes[type]) as SftpPacket;
                    packet.Load(reader);

                    try
                    {
                        HandlePacket((dynamic)packet);
                    }
                    catch (RuntimeBinderException)
                    {
                        logger.LogWarning("Unhandled SFTP packet type: {Type}.", type);
                    }
                }
                else
                {
                    logger.LogWarning("Unimplemented SFTP packet type: {Type}.", type);
                }
            }
        }
        public void BitmodeBreakTest()
        {
            var reader = new ByteReader(new byte[] { 0x01, 0x02, 0x03 });

            reader.GetUshort();
            reader.GetBits(1);
            reader.GetByte();
        }
Exemple #5
0
 private Vertex GetVertex(ByteReader io)
 {
     Vertex vertex;
     vertex.Position=io.GetVector3();
     vertex.Normal=io.GetVector3();
     vertex.UV=io.GetVector2();
     vertex.DeformType=(VERTEX_DEFORM)io.GetByte();
     vertex.BoneIndices=new int[4];
     vertex.BoneWeights=new float[4];
     switch(vertex.DeformType)
     {
         case VERTEX_DEFORM.BDEF1:
             vertex.BoneIndices[0]=GetBoneIndex(io);
             break;
         case VERTEX_DEFORM.BDEF2:
             vertex.BoneIndices[0]=GetBoneIndex(io);
             vertex.BoneIndices[1]=GetBoneIndex(io);
             vertex.BoneWeights[0]=io.GetFloat();
             break;
         case VERTEX_DEFORM.BDEF4:
             vertex.BoneIndices[0]=GetBoneIndex(io);
             vertex.BoneIndices[1]=GetBoneIndex(io);
             vertex.BoneIndices[2]=GetBoneIndex(io);
             vertex.BoneIndices[3]=GetBoneIndex(io);
             vertex.BoneWeights[0]=io.GetFloat();
             vertex.BoneWeights[1]=io.GetFloat();
             vertex.BoneWeights[2]=io.GetFloat();
             vertex.BoneWeights[3]=io.GetFloat();
             break;
         case VERTEX_DEFORM.SDEF:
             throw new PmxException("not implemented");
     }
     vertex.EdgeFactor=io.GetFloat();
     return vertex;
 }
Exemple #6
0
 private Material GetMaterial(ByteReader io)
 {
     var material=new Material();
     material.Name=GetText(io);
     material.EnglishName=GetText(io);
     material.Diffuse=io.GetVector4();
     material.Specular=io.GetVector3();
     material.SpecularFactor=io.GetFloat();
     material.Ambient=io.GetVector3();
     material.Flag=io.GetByte();
     material.EdgeColor=io.GetVector4();
     material.EdgeSize=io.GetFloat();
     material.TextureIndex=GetTextureIndex(io);
     material.SphereTextureIndex=GetTextureIndex(io);
     material.SphereMode=(SPHERE_MODE)io.GetByte();
     material.UseSharedToon=io.GetByte()==0 ? false : true;
     material.ToonTextureIndex=GetTextureIndex(io);
     material.Memo=GetText(io);
     material.IndexCount=io.GetInt();
     return material;
 }
Exemple #7
0
 private Bone GetBone(ByteReader io)
 {
     var bone=new Bone();
     bone.Name=GetText(io);
     bone.EnglishName=GetText(io);
     bone.Position=io.GetVector3();
     bone.ParentIndex=GetBoneIndex(io);
     bone.Layer=io.GetInt();
     bone.Flags=(BONEFLAG)io.GetUShort();
     if(bone.HasFlag(BONEFLAG.HAS_TAILBONE)){
         bone.TailBoneIndex=GetBoneIndex(io);
     }
     else{
         bone.TailOffset=io.GetVector3();
     }
     if(bone.HasFlag(BONEFLAG.ROTATION_EFFECTED)
             || bone.HasFlag(BONEFLAG.TRANSLATION_EFFECTED)){
         bone.EffectIndex=GetBoneIndex(io);
         bone.EffectFactor=io.GetFloat();
     }
     if(bone.HasFlag(BONEFLAG.HAS_FIXEDAXIS)){
         bone.FixedAxis=io.GetVector3();
     }
     if(bone.HasFlag(BONEFLAG.HAS_LOCALAXIS)){
         bone.LocalAxisX=io.GetVector3();
         bone.LocalAxisZ=io.GetVector3();
     }
     if(bone.HasFlag(BONEFLAG.DEFORM_EXTERNAL_PARENT)){
         bone.ExternalParentKey=io.GetInt();
     }
     if(bone.HasFlag(BONEFLAG.HAS_IK)){
         var ik=new IKSolver();
         bone.IKSolver=ik;
         ik.TargetIndex=GetBoneIndex(io);
         ik.Iterations=io.GetInt();
         ik.UnitRadian=io.GetFloat();
         int Count=io.GetInt();
         ik.Chains=Enumerable.Range(1, Count).Select(_
                 =>{
                 var link=new IKLink();
                 link.BoneIndex=GetBoneIndex(io);
                 link.IsLimited=io.GetByte()==0 ? false : true;
                 if(link.IsLimited){
                 link.MinEulerRadians=io.GetVector3();
                 link.MaxEulerRadians=io.GetVector3();
                 }
                 return link;
                 }).ToArray();
     }
     return bone;
 }
Exemple #8
0
        public static Model loadFromPath(string path)
        {
            var io=new ByteReader(File.ReadAllBytes(path));
            // pmx header
            var magic=io.GetAscii(4);
            if(magic!="PMX "){
                throw new PmxException("invalid magic");
            }
            var version=io.GetFloat();
            if(version!=2.0f){
                throw new PmxException("invalid version");
            }
            // flags
            int flags=io.GetByte();
            if(flags!=8){
                throw new PmxException("invalid byte");
            }
            TEXT_ENCODING encoding=(TEXT_ENCODING)io.GetByte();
            byte additional_uv=io.GetByte();
            byte vertex_index_bytes=io.GetByte();
            byte texture_index_bytes=io.GetByte();
            byte material_index_bytes=io.GetByte();
            byte bone_index_bytes=io.GetByte();
            byte morph_index_bytes=io.GetByte();
            byte rigidbody_index_bytes=io.GetByte();

            var loader=new Loader(
                    GetTextFunc(encoding),
                    GetIndexFunc(vertex_index_bytes),
                    GetIndexFunc(texture_index_bytes),
                    GetIndexFunc(bone_index_bytes)
                    );

            return loader.load(io);
        }