Ejemplo n.º 1
0
        public static DateTime GetRecordingTime(PropertyItem item)
        {
            if (item.Id != (int)IFD_TAGS.RecordingTime || item.Type != (int)IFD_TYPES.ASCII)
            {
                throw new EXIF_Exception(IFD_TAGS.RecordingTime);
            }

            using (System.IO.MemoryStream mem = new System.IO.MemoryStream(item.Value))
            {
                System.IO.BinaryReader reader = new System.IO.BinaryReader(mem);
                string txtDatum = "";

                // Datumsteil einlesen
                while (reader.PeekChar() != '\0' && reader.PeekChar() != ' ')
                {
                    txtDatum += reader.ReadChar();
                }
                txtDatum = txtDatum.Replace(':', '-');

                // Zeitteil einlesen
                while (reader.PeekChar() != '\0')
                {
                    txtDatum += reader.ReadChar();
                }

                DateTime datum = Convert.ToDateTime(txtDatum);
                return(datum);
            }
        }
Ejemplo n.º 2
0
        private string readStringLiteral(System.IO.Stream stream, System.IO.BinaryReader reader)
        {
            string str = "";

            SkipSpaces(stream);

            if ((char)reader.PeekChar() == '"')
            {
                reader.ReadChar();
            }

            while ((char)reader.PeekChar() != '"')
            {
                str += (char)stream.ReadByte();
            }

            if ((char)reader.PeekChar() == '"')
            {
                reader.ReadChar();
            }

            SkipSpaces(stream);

            return(str);
        }
 public MavLink4Net.Messages.IMessage Deserialize(System.IO.BinaryReader reader)
 {
     MavLink4Net.Messages.Common.AdsbVehicleMessage message = new MavLink4Net.Messages.Common.AdsbVehicleMessage();
     message.IcaoAddress  = reader.ReadUInt32();
     message.Lat          = reader.ReadInt32();
     message.Lon          = reader.ReadInt32();
     message.Altitude     = reader.ReadInt32();
     message.Heading      = reader.ReadUInt16();
     message.HorVelocity  = reader.ReadUInt16();
     message.VerVelocity  = reader.ReadInt16();
     message.Flags        = ((MavLink4Net.Messages.Common.AdsbFlags)(reader.ReadUInt16()));
     message.Squawk       = reader.ReadUInt16();
     message.AltitudeType = ((MavLink4Net.Messages.Common.AdsbAltitudeType)(reader.ReadByte()));
     message.Callsign[0]  = reader.ReadChar();
     message.Callsign[1]  = reader.ReadChar();
     message.Callsign[2]  = reader.ReadChar();
     message.Callsign[3]  = reader.ReadChar();
     message.Callsign[4]  = reader.ReadChar();
     message.Callsign[5]  = reader.ReadChar();
     message.Callsign[6]  = reader.ReadChar();
     message.Callsign[7]  = reader.ReadChar();
     message.Callsign[8]  = reader.ReadChar();
     message.EmitterType  = ((MavLink4Net.Messages.Common.AdsbEmitterType)(reader.ReadByte()));
     message.Tslc         = reader.ReadByte();
     return(message);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Reads a 0-terminated string
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public static string ReadPChar(System.IO.BinaryReader r)
        {
            char   b = r.ReadChar();
            string s = "";

            while (b != 0 && r.BaseStream.Position <= r.BaseStream.Length)
            {
                s += b;
                b  = r.ReadChar();
            }
            return(s);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// バイナリよりSZipヘッダ情報を読み取ります
 /// read szip header infomation from binary
 /// </summary>
 public void Read(System.IO.BinaryReader reader)
 {
     _magic                            = new System.Char[4];
     _magic[0]                         = reader.ReadChar();
     _magic[1]                         = reader.ReadChar();
     _magic[2]                         = reader.ReadChar();
     _magic[3]                         = reader.ReadChar();
     _version                          = reader.ReadUInt16();
     _contentCount                     = reader.ReadInt16();
     _contentNameTableCrc              = reader.ReadUInt32();
     _contentHeaderTableCrc            = reader.ReadUInt32();
     _compressedContentHeaderTableSize = reader.ReadInt32();
     _originalContentNameTableSize     = reader.ReadInt32();
     _compressedContentNameTableSize   = reader.ReadInt32();
 }
Ejemplo n.º 6
0
        public static void split(string input_path, string dir_path, int nb)
        {
            System.IO.FileStream inf = new System.IO.FileStream(input_path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader reader = new System.IO.BinaryReader(inf);
            System.IO.BinaryWriter[] writers = new System.IO.BinaryWriter[nb];
            for (int x = 0; x < nb; x++)
            {
                writers[x] = new System.IO.BinaryWriter(new System.IO.FileStream(dir_path + "/part_" + (x + 1) + ".ACDC",
                                                                    System.IO.FileMode.Create,
                                                                    System.IO.FileAccess.Write));

            }
            int i = 0;
            while (reader.PeekChar() != -1)
            {

                writers[i % nb].Write(reader.ReadChar());

                i++;
            }
            for (int j=0; j<nb; j++)
            {
                writers[j].Close();
            }
            
        }
Ejemplo n.º 7
0
        private void receivingThreadBody()
        {
            while (binaryReader != null && client != null && isAlive)
            {
                String serializedPacket = "";
                while (!serializedPacket.EndsWith(Global.END_OF_PACKET) && isAlive)
                {
                    if (stream.DataAvailable)
                    {
                        serializedPacket += binaryReader.ReadChar();
                    }
                    else
                    {
                        Thread.Sleep(200);
                    }
                }

                if (isAlive)
                {
                    System.Diagnostics.Debug.WriteLine("packet received => deserialization");
                    serializedPacket = serializedPacket.Replace(Global.END_OF_PACKET, "");
                    System.Diagnostics.Debug.WriteLine(serializedPacket);
                    receivedQueue.Enqueue(GamePacketSerialization.deserialize(serializedPacket));
                    System.Diagnostics.Debug.WriteLine("queue size: " + receivedQueue.Count);
                }
            }
        }
Ejemplo n.º 8
0
            // Convert Byte Arrays to Structs
            public static PIDInfoReceivePacket FromArray(byte[] bytes)
            {
                var reader = new System.IO.BinaryReader(new System.IO.MemoryStream(bytes));

                var s = default(PIDInfoReceivePacket);


                s.Header       = reader.ReadBytes(2);
                s.FunctionCode = reader.ReadByte();
                s.AGVID        = reader.ReadByte();

                s.Velocity = reader.ReadSingle();
                //  s.UdkVelocity = reader.ReadSingle();
                s.LinePos = reader.ReadSingle();
                //  s.UdkLinePos = reader.ReadSingle();
                s.CurrentNode       = reader.ReadByte();
                s.currentOrient     = reader.ReadChar();
                s.distanceToPreNode = reader.ReadSingle();
                //    s.CheckSum = reader.ReadUInt16();
                s.EndOfFrame = reader.ReadBytes(2);


                //  s.EndOfFrame = reader.ReadBytes(2);

                return(s);
            }
Ejemplo n.º 9
0
 private static void read(char[] arr, System.IO.BinaryReader input)
 {
     for (int i = 0, len = arr.Length; i < len; i++)
     {
         arr[i] = input.ReadChar();
     }
 }
Ejemplo n.º 10
0
        public static object ReadAs(System.IO.Stream stream, Type type)
        {
            System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);

            if (type == typeof(bool))
            {
                return(reader.ReadBoolean());
            }
            else if (type == typeof(char))
            {
                return(reader.ReadChar());
            }
            else if (type == typeof(byte))
            {
                return(reader.ReadByte());
            }
            else if (type == typeof(short))
            {
                return(reader.ReadInt16());
            }
            else if (type == typeof(int))
            {
                return(reader.ReadInt32());
            }
            else if (type == typeof(long))
            {
                return(reader.ReadInt64());
            }
            else if (type == typeof(float))
            {
                return(reader.ReadSingle());
            }
            else if (type == typeof(double))
            {
                return(reader.ReadDouble());
            }
            else if (type == typeof(sbyte))
            {
                return(reader.ReadSByte());
            }
            else if (type == typeof(ushort))
            {
                return(reader.ReadUInt16());
            }
            else if (type == typeof(uint))
            {
                return(reader.ReadUInt32());
            }
            else if (type == typeof(ulong))
            {
                return(reader.ReadUInt64());
            }
            else if (type == typeof(string))
            {
                return(reader.ReadString());
            }

            throw new ArgumentException("Usupported type!");
        }
Ejemplo n.º 11
0
 public MavLink4Net.Messages.IMessage Deserialize(System.IO.BinaryReader reader)
 {
     MavLink4Net.Messages.Common.NamedValueFloatMessage message = new MavLink4Net.Messages.Common.NamedValueFloatMessage();
     message.TimeBootMs = reader.ReadUInt32();
     message.Value      = reader.ReadSingle();
     message.Name[0]    = reader.ReadChar();
     message.Name[1]    = reader.ReadChar();
     message.Name[2]    = reader.ReadChar();
     message.Name[3]    = reader.ReadChar();
     message.Name[4]    = reader.ReadChar();
     message.Name[5]    = reader.ReadChar();
     message.Name[6]    = reader.ReadChar();
     message.Name[7]    = reader.ReadChar();
     message.Name[8]    = reader.ReadChar();
     message.Name[9]    = reader.ReadChar();
     return(message);
 }
Ejemplo n.º 12
0
        public string ReadCString()
        {
            StringBuilder cstr = new StringBuilder();

            while (true)
            {
                char c = reader.ReadChar();
                if (c == '\0')
                {
                    break;
                }
                else
                {
                    cstr.Append(c);
                }
            }
            return(cstr.ToString());
        }
 public MavLink4Net.Messages.IMessage Deserialize(System.IO.BinaryReader reader)
 {
     MavLink4Net.Messages.Common.DebugVectMessage message = new MavLink4Net.Messages.Common.DebugVectMessage();
     message.TimeUsec = reader.ReadUInt64();
     message.X        = reader.ReadSingle();
     message.Y        = reader.ReadSingle();
     message.Z        = reader.ReadSingle();
     message.Name[0]  = reader.ReadChar();
     message.Name[1]  = reader.ReadChar();
     message.Name[2]  = reader.ReadChar();
     message.Name[3]  = reader.ReadChar();
     message.Name[4]  = reader.ReadChar();
     message.Name[5]  = reader.ReadChar();
     message.Name[6]  = reader.ReadChar();
     message.Name[7]  = reader.ReadChar();
     message.Name[8]  = reader.ReadChar();
     message.Name[9]  = reader.ReadChar();
     return(message);
 }
Ejemplo n.º 14
0
 private static void read(char[,] arr, System.IO.BinaryReader input)
 {
     for (int i = 0, leng = arr.GetLength(0); i < leng; i++)
     {
         for (int j = 0, len = arr.GetLength(1); j < len; j++)
         {
             arr[i, j] = input.ReadChar();
         }
     }
 }
Ejemplo n.º 15
0
        /* https://stackoverflow.com/a/25577853 */
        public static string ReadNullTerminatedString(this System.IO.BinaryReader reader)
        {
            string str = "";
            char   ch;

            while ((ch = reader.ReadChar()) != 0)
            {
                str = str + ch;
            }
            return(str);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Read a variable number of bytes into a String until a null terminator is reached.
        /// </summary>
        /// <param name="br">Binary reader</param>
        /// <returns>The String that was read</returns>
        public static String ReadNullTerminatedString(this System.IO.BinaryReader br)
        {
            var  str = "";
            Char c;

            while ((c = br.ReadChar()) != 0)
            {
                str += c;
            }
            return(str);
        }
Ejemplo n.º 17
0
        public static string ReadNullTerminatedString(this System.IO.BinaryReader stream)
        {
            var  str = "";
            char ch;

            while ((ch = stream.ReadChar()) != 0)
            {
                str += ch;
            }
            return(str);
        }
Ejemplo n.º 18
0
        private void LoadMH()
        {
            SoundCount = MHReader.ReadUInt32();
            Interleave = MHReader.ReadUInt32();
            Array.Resize(ref Sounds, (int)SoundCount);
            for (int i = 0; i <= SoundCount - 1; i++)
            {
                Sounds[i].Type       = MHReader.ReadUInt32();
                Sounds[i].Size       = MHReader.ReadUInt32();
                Sounds[i].Offset     = MHReader.ReadUInt32();
                Sounds[i].SampleRate = MHReader.ReadUInt32();
                Sounds[i].Skip       = MHReader.ReadUInt32();
                Sounds[i].External   = false;
                switch (Sounds[i].Type)
                {
                case 0:
                {
                    MB.Position = Sounds[i].Offset + 32;
                    for (int j = 0; j <= 31; j++)
                    {
                        char ch = MBReader.ReadChar();
                        if (Strings.Asc(ch) > 0)
                        {
                            Sounds[i].Name += ch;
                        }
                        else
                        {
                            break;
                        }
                    }
                    MB.Position = Sounds[i].Offset + 16;
                    byte[] b = new byte[4];
                    b = MBReader.ReadBytes(4);
                    Sounds[i].SampleRate = (uint)(b[0] * 256 * 256 * 256 + b[1] * 256 * 256 + b[2] * 256 + b[3]);
                    break;
                }

                case 1:
                {
                    Sounds[i].Name = "Stereo";
                    break;
                }

                case 2:
                {
                    Sounds[i].Name = "Reserved";
                    break;
                }
                }
            }
            BuildTree();
        }
Ejemplo n.º 19
0
        private char readCharLiteral(System.IO.Stream stream, System.IO.BinaryReader reader)
        {
            char c = '\0';

            SkipSpaces(stream);

            if ((char)reader.PeekChar() == '\'')
            {
                reader.ReadChar();
            }

            c = reader.ReadChar();

            if ((char)reader.PeekChar() == '\'')
            {
                reader.ReadChar();
            }

            SkipSpaces(stream);

            return(c);
        }
Ejemplo n.º 20
0
        private bool skipComments(System.IO.BinaryReader reader)
        {
            string instruction = "";

            while ((char)reader.PeekChar() == ';' || (char)reader.PeekChar() == '/' || (char)reader.PeekChar() == '*')
            {
                instruction += reader.ReadChar();
            }

            if (instruction.StartsWith(";") || instruction.StartsWith("//"))
            {
                while ((char)reader.PeekChar() != '\n' && (char)reader.PeekChar() != '\r')
                {
                    reader.ReadByte();
                }
                return(true);
            }

            if (instruction.StartsWith("/*"))
            {
back:
                while ((char)reader.PeekChar() != '*')
                {
                    reader.ReadChar();
                }

                string end = "*";

                if ((char)reader.PeekChar() == '/')
                {
                    end += reader.ReadChar();
                    return(true);
                }

                goto back;
            }

            return(false);
        }
Ejemplo n.º 21
0
        public static string ReadAlignedString(System.IO.BinaryReader br)
        {
            string value = "";

            // ignore any null padding in the front.
            while (br.PeekChar() == '\0') // loop over the embeded name.
            {
                br.ReadChar();            // discard the null
            }
            while (br.PeekChar() != '\0') // loop over the embeded name.
            {
                value += br.ReadChar();
            }

            br.ReadChar();//discard the null terminating the end of the string.

            // if we are not alligned, then drop some bytes.
            while (br.BaseStream.Position % 4 != 0)
            {
                br.ReadChar(); // should be null padding for alignment
            }
            return(value);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Unserializes a BinaryStream into the Attributes of this Instance
        /// </summary>
        /// <param name="reader">The Stream that contains the FileData</param>
        internal int UnserializeName(System.IO.BinaryReader reader)
        {
            name = "";
            while (true)
            {
                char ch = reader.ReadChar();
                if (ch == 0)
                {
                    break;
                }
                name += ch;
            }

            return(name.Length + 1);
        }
        static StackObject *ReadChar_0(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj)
        {
            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
            StackObject *ptr_of_this_method;
            StackObject *__ret = ILIntepreter.Minus(__esp, 1);

            ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
            System.IO.BinaryReader instance_of_this_method = (System.IO.BinaryReader) typeof(System.IO.BinaryReader).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
            __intp.Free(ptr_of_this_method);

            var result_of_this_method = instance_of_this_method.ReadChar();

            __ret->ObjectType = ObjectTypes.Integer;
            __ret->Value      = (int)result_of_this_method;
            return(__ret + 1);
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Interpretes the data as an String Value
 /// </summary>
 /// <returns>The Value interpreted as String</returns>
 protected string AsString()
 {
     System.IO.BinaryReader br = new System.IO.BinaryReader(new System.IO.MemoryStream(Value));
     try
     {
         string ret = "";
         while (br.PeekChar() != -1)
         {
             ret += br.ReadChar();
         }
         return(ret);
     }
     catch (Exception)
     {
         return("");
     }
 }
Ejemplo n.º 25
0
        public static Node Load(Node parent, System.IO.BinaryReader br)
        {
            char ch = br.ReadChar();

            switch (ch)
            {
            case 'o':
                return(new Object(parent, br));

            case 'm':
                return(new Mesh(parent, br));

            case 'e':
                return(new ExternalObject(parent, br));
            }
            throw new Exception("Error in Common.Object file!");
        }
Ejemplo n.º 26
0
        static public string ExtractString(System.IO.BinaryReader inFile, short length, string header)
        {
            string myString = "";

            for (short i = 0; i < length; i++)
            {
                Char nextChar = inFile.ReadChar();
                if (System.Char.IsLetterOrDigit(nextChar) ||
                    System.Char.IsPunctuation(nextChar) ||
                    System.Char.IsWhiteSpace(nextChar)
                    )
                {
                    myString += nextChar;
                }
            }

            TracerWriteLine(header + " = '" + myString + "'");
            return(myString);
        }
Ejemplo n.º 27
0
        public void LeCadastroDoArquivo()
        {
            System.IO.BinaryReader leitor = null;
            Aluno a;
            int   n;

            this.lista = new System.Collections.Generic.List <Aluno>();

            try
            {
                leitor = new System.IO.BinaryReader(new System.IO.FileStream("alunos.dat", System.IO.FileMode.Open));

                n = leitor.ReadInt32();
                for (int i = 0; i < n; i++)
                {
                    a       = new Aluno();
                    a.nome  = leitor.ReadString();
                    a.idade = leitor.ReadInt32();
                    a.sexo  = leitor.ReadChar();

                    this.lista.Add(a);
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("AVISO! Arquivo alunos.dat não foi encontrado. Cadastro começará vazio.");
                Console.ReadKey();
            }
            catch (System.Exception exc)
            {
                Console.WriteLine("ERRO! " + exc.Message);
                Console.ReadKey();
            }
            finally
            {
                if (leitor != null)
                {
                    leitor.Close();
                }
            }
        }
Ejemplo n.º 28
0
 protected virtual void InternalDeserialize(ref System.IO.BinaryReader reader)
 {
     if (reader.PeekChar() == 67)
     {
         reader.ReadChar();
         this.m_id = reader.ReadInt16();
     }
     else
     {
         this.m_targets     = (SpellTargetType)reader.ReadInt32();
         this.m_targetMask  = reader.ReadString();
         this.m_id          = reader.ReadInt16();
         this.m_duration    = reader.ReadInt32();
         this.m_delay       = reader.ReadInt32();
         this.m_random      = reader.ReadInt32();
         this.m_group       = reader.ReadInt32();
         this.m_modificator = reader.ReadInt32();
         this.m_trigger     = reader.ReadBoolean();
         this.m_hidden      = reader.ReadBoolean();
         this.ParseRawZone(reader.ReadString());
     }
 }
        static int _m_ReadChar(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.IO.BinaryReader gen_to_be_invoked = (System.IO.BinaryReader)translator.FastGetCSObj(L, 1);



                {
                    char gen_ret = gen_to_be_invoked.ReadChar(  );
                    LuaAPI.xlua_pushinteger(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Ejemplo n.º 30
0
        private UpData(string file)
        {
            this.Right = false;
            var f = new System.IO.FileInfo(file);

            if (f.Length > 8 + 3)
            {
                List <byte[]> data = new List <byte[]>();
                using (System.IO.BinaryReader io = new System.IO.BinaryReader(f.Open(System.IO.FileMode.Open)))
                {
                    var verBytes = io.ReadBytes(6);
                    //      this.ROMVer = System.Text.Encoding.ASCII.GetString(verBytes);//LDC sc
                    this.ROMVer = verBytes[4].ToString() + "." + verBytes[5].ToString();//LDC增加
                    //  int a = 0;
                    do
                    {
                        var  temp = io.ReadBytes(1);
                        char id   = System.Text.Encoding.ASCII.GetChars(temp)[0];
                        if (id == 'X')
                        {
                            byte   type   = io.ReadByte();
                            byte   lenght = io.ReadByte();
                            byte[] Rdbuf  = new byte[lenght * 2 - 1]; //Rdbuf
                            byte[] buf    = new byte[lenght + 2];
                            buf[0] = type;                            //1
                            buf[1] = lenght;                          //234
                            io.Read(Rdbuf, 2, lenght * 2 - 3);        //lenght * 2 - 2 - a
                            //if (a == 0)
                            //{
                            //    a = 1;
                            //    continue;
                            //}
                            buf[2] = Rdbuf[2]; //3
                            buf[3] = Rdbuf[3]; //4
                            buf[4] = Rdbuf[4]; //5
                            lenght = (byte)(lenght - 4);
                            ushort div = 0;
                            ushort tmp16;
                            int    j = 0;
                            int    i = 0;
                            for ( ; i < lenght * 2;)//
                            {
                                tmp16  = (ushort)(Rdbuf[5 + i++] << 8);
                                tmp16 += Rdbuf[5 + i++];;
                                if (div == 0x00)
                                {
                                    div = (ushort)Math.Sqrt((double)tmp16);
                                }
                                else
                                {
                                    div = (ushort)(tmp16 / div);
                                }
                                buf[5 + j++] = (byte)div;
                            }
                            tmp16        = (ushort)(Rdbuf[5 + i++] << 8);//最后一个是检验
                            tmp16       += Rdbuf[5 + i++];
                            buf[5 + j++] = (byte)Math.Sqrt((double)tmp16);
                            data.Add(buf);
                        }
                        else
                        if (id == 'E')
                        {
                            if (io.ReadChar() == 'N' && io.ReadChar() == 'D')
                            {
                                this.Right = true;
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    } while (true);
                }
                if (this.Right)//升级数据加工
                {
                    this.Data = data.Select(m =>
                                            "S" + System.Text.Encoding.ASCII.GetString(m, 0, 1)
                                            + String.Join(String.Empty, m.Skip(1).Select(bin => bin.ToString("X2")).ToArray())
                                            ).ToArray();
                }
            }
        }
Ejemplo n.º 31
0
        private bool TryLoadCachedGroup(byte[] data, DateTime lastWriteTime)
        {
            var timer = System.Diagnostics.Stopwatch.StartNew();

            var reader = new BinaryReader(new MemoryStream(data), Encoding.UTF8);

            DateTime cacheTime = new DateTime(reader.ReadInt64(), DateTimeKind.Utc);
            if (cacheTime != lastWriteTime)
                return false;

            Dictionary<int, object> objects = new Dictionary<int, object>();
            objects.Add(0, null);

            // first pass constructs objects
            long objectTableOffset = reader.BaseStream.Position;
            int objectCount = reader.ReadInt32();
            for (int i = 0; i < objectCount; i++)
            {
                int key = reader.ReadInt32();
                object obj = CreateGroupObject(reader, key, objects);
                objects.Add(key, obj);
            }

            reader.BaseStream.Seek(objectTableOffset + 4, SeekOrigin.Begin);
            for (int i = 0; i < objectCount; i++)
            {
                int key = reader.ReadInt32();
                LoadGroupObject(reader, key, objects);
            }

            List<TemplateGroup> importsToClearOnUnload = new List<TemplateGroup>();
            Dictionary<string, CompiledTemplate> templates = new Dictionary<string, CompiledTemplate>();
            Dictionary<string, IDictionary<string, object>> dictionaries = new Dictionary<string, IDictionary<string, object>>();

            // imported groups
            int importCount = reader.ReadInt32();
            for (int i = 0; i < importCount; i++)
                importsToClearOnUnload.Add((TemplateGroup)objects[reader.ReadInt32()]);

            // delimiters
            char delimiterStartChar = reader.ReadChar();
            char delimiterStopChar = reader.ReadChar();

            // templates & aliases
            int templateCount = reader.ReadInt32();
            for (int i = 0; i < templateCount; i++)
            {
                string key = reader.ReadString();
                CompiledTemplate value = (CompiledTemplate)objects[reader.ReadInt32()];
                templates[key] = value;
            }

            // dictionaries
            int dictionaryCount = reader.ReadInt32();
            for (int i = 0; i < dictionaryCount; i++)
            {
                string name = reader.ReadString();
                IDictionary<string, object> dictionary = new Dictionary<string, object>();
                dictionaries[name] = dictionary;
                int valueCount = reader.ReadInt32();
                for (int j = 0; j < valueCount; j++)
                {
                    string key = reader.ReadString();
                    object value = objects[reader.ReadInt32()];
                    dictionary[key] = value;
                }
            }

            this._importsToClearOnUnload.AddRange(importsToClearOnUnload);
            this.delimiterStartChar = delimiterStartChar;
            this.delimiterStopChar = delimiterStopChar;

            foreach (var pair in templates)
                this.templates[pair.Key] = pair.Value;

            foreach (var pair in dictionaries)
                this.dictionaries[pair.Key] = pair.Value;

            System.Diagnostics.Debug.WriteLine(string.Format("Successfully loaded the cached group {0} in {1}ms.", Name, timer.ElapsedMilliseconds));
            return true;
        }
Ejemplo n.º 32
0
        public bool parseOpcode()
        {
            bool firstBit;
            byte op;

            // Get opcode
            op = grvReader.ReadByte();
            // Check if first bit set
            if ((op & 128) == 128)
            {
                op      &= 127;
                firstBit = true;
            }
            else
            {
                firstBit = false;
            }
            try
            {
                opCount[op]++;
            }
            catch (Exception e)
            {
                Console.WriteLine("Bad opcode: " + op);
            }

            /*
             * if (something)
             *   vars[0x103]++;
             *
             */

            //if (VDXFlags.op400D == 2)
            //{
            //    // Play midi
            //    music.playLoop(1);
            //    VDXFlags.op400D = 0;

            //}

            /* if (.op1c05 > 0) ?
             *   do things
             */

            /* if (.op2042 & 0x80)
             *   do stuff/jump
             *   .op2402 = 0
             */


            //Console.WriteLine("Opcode 0x{0:X}", op);
            //if (scriptJumps
            switch (op)
            {
                #region NOP's
            case 0x28:
            case 0x4A:
            case 0x4F:
            case 0x50:
                // 2 byte NOP
            {
                ushort arg = grvReader.ReadUInt16();
                genASM += ("\tNOP");
            }
            break;

            case 0x1E:
            case 0x48:
            case 0x4B:
                // 1 Byte NOP
            {
                byte arg = grvReader.ReadByte();
                genASM += ("\tNOP");
            }
            break;

            case 0x01:
            case 0x46:
            case 0x49:
            case 0x47:
                // NOP
                genASM += ("\tNOP");
                break;

            case 0x59:
                // Variable size NOP
            {
                ushort arg1, arg2;
                if (firstBit)
                {
                    arg1 = grvReader.ReadByte();
                }
                else
                {
                    arg1 = grvReader.ReadUInt16();
                }
                arg2 = grvReader.ReadByte();
                //Console.WriteLine("NOP variable: 0x{0:X} with args 0x{1:X}, 0x{2:X}", op, arg1, arg2);
                genASM += ("\tOp59\t0x" + Convert.ToString(arg1, 16) + ", 0x" + Convert.ToString(arg2, 16));
            }
            break;

                #endregion

                #region Bitflags (3, 5, 6, 7, A, 35, 57)

            case 0x03:
                // Enable bit 9
                genASM += ("\tFADIN");
                break;

            case 0x05:
                // Bit 8 (just still ?)
                genASM += ("\tBF8ON");
                break;

            case 0x06:
                // Bit 6
                genASM += ("\tBF6ON");
                break;

            case 0x07:
                // Bit 7 - Just audio?
                genASM += ("\tBF7ON");
                break;

            case 0x0A:
                // Bit 5 - skip stills?
                genASM += ("\tBF5ON");
                break;

            case 0x35:     // just audio
                genASM += ("\tBF7OFF");
                break;

            case 0x57:
                // unimplemented
                Console.WriteLine("op57");
                return(false);

            case 0x58:
                // unimplemented
                genASM += "\tOp58\t" + readAwk2() + "; unknown pvdx style";
                Console.WriteLine("op58");
                //return false;
                break;

                #endregion

                #region Mouse clicks - D, E, F, 10, 11, 12, 2C, 2D, 30, 44, 45, 53)

            case 0x0D:
                // Mark a rectangle clickable.  On hover, show cursor arg6.  Jump to arg5 on click
            {
                ushort left   = grvReader.ReadUInt16();
                ushort top    = grvReader.ReadUInt16();
                ushort right  = grvReader.ReadUInt16();
                ushort bottom = grvReader.ReadUInt16();
                ushort jump   = grvReader.ReadUInt16();
                scriptJumps.Add(jump);
                byte cursor = grvReader.ReadByte();
                genASM += ("\tHOTREC\tloc_" + jump + ", " + left + ", " + top + ", " + right + ", " + bottom + ", " + cursor);
            }
            break;

            case 0x0E:
                // Left 100px clickable
            {
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTL\tloc_" + offset);
            }
            break;

            case 0x0F:
                // Right 100px clickable
            {
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTR\tloc_" + offset);
            }
            break;

            case 0x10:
            case 0x11:
                // Middle 240px clickable
            {
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTC\tloc_" + offset);
            }
            break;

            case 0x12:
                // Current pos clickable
            {
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTFULL\tloc_" + offset);
            }
            break;

            case 0x30:
                // Bottom 80px clickable
            {
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTB\tloc_" + offset);
            }
            break;


            case 0x2C:
            {
                // Destination and cursor for top hotspot
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTTC\tloc_" + offset + ", " + grvReader.ReadByte());
            }
            break;

            case 0x2D:
            {
                // Destination and cursor for bottom hotspot
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTBC\tloc_" + offset + ", " + grvReader.ReadByte());
            }
            break;

            case 0x44:
            {
                // Destination for right hotspot

                //standardHotspot[1].jumpAddress = stream.ReadUInt16();
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTRD\tloc_" + offset);
            }
            break;

            case 0x45:
            {
                // Destination for left hotspot

                //standardHotspot[3].jumpAddress = stream.ReadUInt16();
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTLD\tloc_" + offset);
                //throw new Exception("Action for left hotspot");
            }
            break;

            case 0x3B:
                // Savegame stuff, inc rendering sphinx.fnt
            {
                byte   slot = grvReader.ReadByte();
                ushort left = grvReader.ReadUInt16(), top = grvReader.ReadUInt16(), right = grvReader.ReadUInt16(), bottom = grvReader.ReadUInt16();
                ushort offset = grvReader.ReadUInt16();
                byte   cursor = grvReader.ReadByte();
                genASM += "\tHOTSAV\t" + slot + ", loc_" + offset + ", " + left + ", " + top + ", " + right + ", " + bottom + ", " + cursor;
                scriptJumps.Add(offset);
            }
            break;

            case 0x0B:
                // Start input loop
                //if (inputLoopPos == 0)
                //    inputLoopPos = stream.BaseStream.Position - 1;

                //game.removeHotspot();

                // Something midi related here

                // Play loop midi perhaps - if not midi playing already?

                //Console.WriteLine("Start input loop");
                genASM += ("\tGOIN\t\t;Start input");
                break;

            case 0x0C:
                // Keyboard related
                //game.addHotspot(stream.ReadByte(), stream.ReadUInt16());
            {
                byte   val    = grvReader.ReadByte();
                ushort offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += ("\tHOTKEY\tloc_" + offset + ", " + val + "  ; " + ((char)val).ToString());
            }
            break;

            case 0x13:
                // End input loop
                // Checks if mouse inputs have passed
                // Jump to input loop start

                //stream.BaseStream.Seek(inputLoopPos, SeekOrigin.Begin);
                genASM += ("\tSTIN\t\t;Stop input");
                break;

                #endregion

            case 0x02:
                // Play "XMI".  Unfortunately that's a little hard at the moment so play MIDI conversion
            {
                genASM += ("\tPMID\t" + ReadFileRef());
            }
            break;

            case 0x04:
                // Fade out
                //game.removeVDX();
                genASM += ("\tFADOUT");
                break;

            case 0x08:
                // Loop XMI file?
            {
                genASM += ("\tMIDLOOP\t" + ReadFileRef());
            }
            break;

            case 0x4E:
            {
                genASM += ("\tMIDDELAY\t" + grvReader.ReadUInt16());
            }
            break;

                #region Display related

            case 0x22:
                // Copy bg to fg
                genASM += ("\tBG2FG\t");
                //game.copyBuffer();
                break;

            case 0x37:
                // Copy screen to game buffer
            {
                ushort left = grvReader.ReadUInt16(), top = grvReader.ReadUInt16(), right = grvReader.ReadUInt16(), bottom = grvReader.ReadUInt16();
                genASM += ("\tREC2BG\t" + left + ", " + top + ", " + right + ", " + bottom);
            }
            break;

            case 0x09:
                // Play VDX
            {
                genASM += ("\tPVDX\t" + ReadFileRef());
            }
            break;

            case 0x1C:
            {
                // Play VDX - 2nd type (simple)
                genASM += ("\tPVDX2\t" + ReadFileRef());
            }
            break;


            case 0x26:
                //throw new Exception("Other awkward - library book tripped?");

                //Console.WriteLine("Op26");
                //return false;
            {
                genASM += "\tPVDXSTR\t" + readAwk2();
                break;
            }
            //// Load a GJD here somewhere as well


            case 0x27:
                // Turns on bit 2 of bitflags (and bit 3 if firstbit is set). VDX file name (in a .gjd) follows: find it, then play it
            {
                //Console.WriteLine("Op27");
                //return false;
                genASM += "\tPVDXSTR2\t" + readAwk2() + "\t;Same as PVDXSTR but set BF1 and optionally BF2";
            }
            break;

                #endregion

            case 0x14:
                // Random number generation
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }
                genASM += ("\tRAND\t0x" + Convert.ToString(offset, 16) + ", " + grvReader.ReadByte());
            }
            break;

            case 0x15:
                // Jump
            {
                ushort jump = grvReader.ReadUInt16();
                scriptJumps.Add(jump);
                genASM += ("\tJMP\tloc_" + jump);
            }
            break;

            case 0x16:
                // Reads a string into the script variable specified in the first 2 (or 1) bytes
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tMOVS\tvar[0x" + Convert.ToString(offset, 16)) + "...] = ";

                string tmp = "";
                do
                {
                    tmp += readScriptChar(true, true, true) + ", ";
                    grvReader.BaseStream.Seek(-1, System.IO.SeekOrigin.Current);
                } while ((grvReader.ReadByte() & 0x80) == 0);
                genASM += tmp.Substring(0, tmp.Length - 2);
            }
            break;

            case 0x17:
                // Return from CALL
                //vars[0x102] = stream.ReadByte();
                genASM += ("\tRET\t" + grvReader.ReadByte());
                //stream.BaseStream.Seek(callStack.Pop(), SeekOrigin.Begin);
                break;

            case 0x18:
                // CALL
            {
                //callStack.Push(stream.BaseStream.Position + 2);
                ushort offset = grvReader.ReadUInt16();
                //stream.BaseStream.Seek(offset, SeekOrigin.Begin);

                scriptJumps.Add(offset);
                genASM += ("\tCALL\tloc_" + offset);
            }
            break;

            case 0x19:
                // Sleep
                //System.Console.WriteLine("Slept for a little bit");
                //System.Threading.Thread.Sleep(stream.ReadUInt16() * 3);
                genASM += ("\tSLEEP\t" + grvReader.ReadUInt16());
                break;

                #region String compare + jump (op 1A and 23)
            case 0x1A:
            case 0x23:
            {
                // Get var location
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                string l = ", 0x" + System.Convert.ToString(offset, 16);

                // JE or JNE?
                if (op == 0x1A)
                {
                    genASM += ("\tJNE\tloc_");
                }
                else
                {
                    genASM += ("\tJE\tloc_");
                }


                byte t;

                do
                {
                    // Read byte
                    l += ", " + readAwk1();

                    // Break when needed
                    //offset++;

                    grvReader.BaseStream.Seek(-1, System.IO.SeekOrigin.Current);
                    t = grvReader.ReadByte();
                    //l += ", " + (t & 0x7f);
                } while ((t & 0x80) != 0x80);

                offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += (offset + l);
            }
            break;
                #endregion

            case 0x1B:
                // XOR
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }
                genASM += ("\tXOR\t0x" + Convert.ToString(offset, 16));
                byte t;
                do
                {
                    t       = grvReader.ReadByte();
                    t      &= 0x4F;
                    genASM += (", " + t);
                    grvReader.BaseStream.Seek(-1, System.IO.SeekOrigin.Current);
                    t = grvReader.ReadByte();
                } while ((t & 0x80) == 0);
                genASM += ("\t;XOR variables starting at offset, with each extra parameter");
            }
            break;

            case 0x1D:
                // Swap to variables
            {
                ushort offa, offb;
                if (firstBit)
                {
                    offa = grvReader.ReadByte();
                }
                else
                {
                    offa = grvReader.ReadUInt16();
                }

                offb    = grvReader.ReadUInt16();
                genASM += ("\tSWAP\t0x" + Convert.ToString(offa, 16) + ", 0x" + Convert.ToString(offb, 16));
            }
            break;

            case 0x1F:
                // INC
                if (firstBit)
                {
                    genASM += ("\tINC\t0x" + Convert.ToString(grvReader.ReadByte(), 16));
                }
                else
                {
                    genASM += ("\tINC\t0x" + Convert.ToString(grvReader.ReadUInt16(), 16));
                }

                break;

            case 0x20:
                // DEC
                if (firstBit)
                {
                    genASM += ("\tDEC\t0x" + Convert.ToString(grvReader.ReadByte(), 16));
                }
                else
                {
                    genASM += ("\tDEC\t0x" + Convert.ToString(grvReader.ReadUInt16(), 16));
                }

                break;

            case 0x21:
            {
                // call something
                if (firstBit)
                {
                    genASM += "\tSTRCMPJNE\t" + grvReader.ReadByte();
                }
                else
                {
                    genASM += "\tSTRCMPJNE\t" + grvReader.ReadUInt16();
                }

                genASM += ", " + readAwk2();
                uint jmp = grvReader.ReadUInt16();
                genASM += " loc_" + jmp;
                scriptJumps.Add(jmp);

                //    ushort offset = 0; /* = (ushort)functionCall();*/
                //    if (offset > 0x09)
                //    {
                //        offset -= 7;
                //    }
                //    // loc_4031CF
                //    offset = vars[offset + 0x19];
                //    byte match = 1, x, y, z;

                //    // loc_4031F6
                //    do
                //    {
                //        x = vars[offset];
                //        y = awkward1();
                //        offset++;
                //        if (x != y)
                //            match = 0;

                //        // loc_403237
                //        stream.BaseStream.Seek(-1, SeekOrigin.Current);
                //        z = stream.ReadByte();
                //    } while ((z & 0x80) == 0);

                //    offset = stream.ReadUInt16();
                //    if (match == 0)
                //        stream.BaseStream.Seek(offset, SeekOrigin.Begin);
                //}
                //throw new Exception("Unknown param awk call");
            }
            break;

            case 0x33:
                // Copy string into arg1
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += "\tLOADSTR\t0x" + Convert.ToString(offset, 16);

                do
                {
                    genASM += ", " + readAwk1();
                    grvReader.BaseStream.Seek(-1, System.IO.SeekOrigin.Current);
                } while ((grvReader.ReadByte() & 0x80) == 0);

                genASM += "\t\t;Load string into variable located by [\'var[param1]-0x31\'] ";
                break;

                //ushort offset;
                //if (firstBit)
                //    offset = stream.ReadByte();
                //else
                //    offset = stream.ReadUInt16();
                //offset = vars[offset];
                //offset -= 0x31;

                //// loc_403678
                //byte b;
                //do
                //{
                //    b = awkward1();
                //    vars[offset] = b;//???
                //    offset++;

                //    vars[vars[offset++]] = (byte)(b & 0x7f);
                //    stream.BaseStream.Seek(-1, SeekOrigin.Current);
                //    b = stream.ReadByte();
                //} while ((b & 0x80) == 0);
            }


            case 0x3A:
                // "Print" string?
            {
                genASM += "\tPRINT\t";
                if (!t7g)
                {
                    genASM += grvReader.ReadUInt16() + ", " + grvReader.ReadUInt16() + ", ";
                    genASM += grvReader.ReadByte() + ", " + grvReader.ReadByte() + ", " + grvReader.ReadByte() + ", ";
                }
                string l    = "";
                bool   done = false;
                do
                {
                    l += readAwk1() + ", ";

                    if (t7g && (grvReader.BaseStream.Seek(-1, System.IO.SeekOrigin.Current) > 0 && (grvReader.ReadByte() & 0x80) != 0))
                    {
                        done = true;
                    }
                    else if (!t7g && (grvReader.PeekChar() == 0))
                    {
                        grvReader.ReadByte();
                        done = true;
                    }
                } while (!done);
                genASM += l.Substring(0, l.Length - 2);
                break;
            }


            case 0x36:
            case 0x34:
                // Check stream of characters - var[off] < b or var[offset] > b
            {
                ushort offset;
                if (op == 0x34)
                {
                    genASM += ("\tJG\tloc_");
                }
                else
                {
                    genASM += ("\tJL\tloc_");
                }

                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                string l = ", 0x" + Convert.ToString(offset, 16);
                do
                {
                    l += ", " + readAwk1();
                    grvReader.BaseStream.Seek(-1, System.IO.SeekOrigin.Current);
                } while ((grvReader.ReadByte() & 0x80) == 0);

                offset = grvReader.ReadUInt16();
                scriptJumps.Add(offset);
                genASM += (offset + l);

                //Console.WriteLine("Unimplemented 36/34");
                //return false;
                //ushort offset;
                //if (firstBit)
                //    offset = stream.ReadByte();
                //else
                //    offset = stream.ReadUInt16();

                //// Check stream to see if equal var[offset]
                //byte b;
                //byte match = 0;
                //if (op == 0x34)
                //{
                //    do
                //    {
                //        b = awkward1();
                //        if (vars[offset] > b)
                //            match = 1;

                //        //offset++;

                //        stream.BaseStream.Seek(-1, SeekOrigin.Current);
                //        b = stream.ReadByte();
                //        // Keep looping till end of string
                //    } while ((b & 0x80) == 0);
                //}
                //else
                //{
                //    do
                //    {
                //        b = awkward1();
                //        if (vars[offset] < (b & 0x7F))
                //            match = 1;
                //        offset++;

                //        stream.BaseStream.Seek(-1, SeekOrigin.Current);
                //        b = stream.ReadByte();
                //        // Keep looping till end of string
                //    } while ((b & 0x80) == 0);
                //}

                //offset = stream.ReadUInt16();
                //if (match == 1)
                //{
                //    stream.BaseStream.Seek(offset, SeekOrigin.Begin);
                //}
            }
            break;

            case 0x24:
                // MOV
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tMOV\t0x" + Convert.ToString(offset, 16) + ", " + grvReader.ReadUInt16());
            }
            break;

            case 0x25:
                // ADD
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tADD\t0x" + Convert.ToString(offset, 16) + ", " + grvReader.ReadUInt16());
            }
            break;

            case 0x29:
                // Stop MIDI
                genASM += ("\tSMID\t\t;Stop midi");
                break;

            case 0x2A:
                // Close things
                // Close current GJD
                // Stop midi
                // Clear hotspots

                genASM += ("\tEXIT");
                break;

            case 0x2E:
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tLOAD\t0x" + Convert.ToString(offset, 16));
                //BinaryReader save = new BinaryReader(new FileStream(path + "t7g4win.00" + ((vars[offset]).ToString()), FileMode.Open, FileAccess.Read));
                //vars = save.ReadBytes(vars.Length);
                //save.Close();
                //Console.WriteLine("Loading game from slot " + (vars[offset].ToString()) + "?");
            }
            break;

            case 0x2F:
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tSAVE\t0x" + Convert.ToString(offset, 16));
                //BinaryWriter save = new BinaryWriter(new FileStream(path + "t7g4win.00" + (vars[offset].ToString()), FileMode.Create, FileAccess.Write));
                //save.Write(vars, 0, vars.Length);
                //save.Close();
                //Console.WriteLine("Save game to slot" + (vars[offset].ToString()) + "?");
            }
            break;

            case 0x31:
                // Set MIDI volume.
            {
                ushort arg1 = grvReader.ReadUInt16(), arg2 = grvReader.ReadUInt16();
                genASM += ("\tMIDVOL\t" + arg1 + ", " + arg2);
            }
            break;

            case 0x32:
                // JNE
            {
                ushort arg1;
                if (firstBit)
                {
                    arg1 = grvReader.ReadByte();
                }
                else
                {
                    arg1 = grvReader.ReadUInt16();
                }

                ushort arg2   = grvReader.ReadUInt16();
                ushort offset = grvReader.ReadUInt16();

                genASM += "\tJNE\tloc_" + offset + ", " + arg1 + ", " + arg2 + "; var[var[arg1] - 0x31] != var[arg2]";
                scriptJumps.Add(offset);
            }
            break;

            case 0x38:
                // Reload stored stack pointer
                //Console.WriteLine("Reload stored stack pointer - TODO");
                genASM += ("\tRLD");
                break;

            case 0x39:
                // "Obscure" swap
            {
                //Console.WriteLine("Unimplemented 39");
                //return false;

                genASM += "\tSWAP\t vars[10*" + readScriptChar(false, true, true) + " + " + readScriptChar(false, true, true) + " + 25]";
                genASM += " <=> vars[10*" + readScriptChar(false, true, true) + " + " + readScriptChar(false, true, true) + " + 25] ";

                /*
                 *
                 * // .text:0040389B
                 * ushort arg1, arg2, arg3;
                 *
                 * arg1 = grvReader.ReadByte();
                 * genASM += ", " + arg1;
                 * arg1 &= 0x7F;
                 *
                 * if (arg1 == 0x23)
                 * {
                 *  arg1 = grvReader.ReadByte();
                 *  genASM += ", " + arg1;
                 * }
                 * //    arg1 &= 0x7F;
                 * //    arg1 = vars[arg1 - 0x61];
                 * //    arg1 *= 10;
                 * //}
                 * //else
                 * //{
                 * //    arg1 -= 0x30;
                 * //    arg1 *= 10;
                 * //}
                 *
                 * arg2 = grvReader.ReadByte();
                 * arg2 &= 0x7F;
                 * genASM += ", " + arg2;
                 *
                 * if (arg2 == 0x23)
                 * {
                 *  arg2 = grvReader.ReadByte();
                 *  genASM += ", " + arg2;
                 * }
                 * //    arg2 &= 0x7F;
                 * //    arg1 += vars[arg2 - 0x61];
                 * //}
                 * //else
                 * //{
                 * //    arg1 = vars[arg1 + arg2];
                 * //}
                 *
                 * //arg1 += 0x19;
                 *
                 * //// .text:0040394D (loc_40393E + 4lines)
                 *
                 * arg3 = grvReader.ReadByte();
                 * arg3 &= 0x7F;
                 * genASM += ", " + arg3;
                 *
                 * if (arg3 == 0x23)
                 * {
                 *  arg3 = grvReader.ReadByte();
                 *  genASM += ", " + arg3;
                 * }
                 * //    arg3 &= 0x7F;
                 * //    arg3 = vars[arg3 - 0x61];
                 * //    arg3 *= 10;
                 * //}
                 * //else
                 * //{
                 * //    arg3 -= 0x30;
                 * //    arg3 *= 10;
                 * //}
                 *
                 * arg2 = grvReader.ReadByte();
                 * arg2 &= 0x7F;
                 * genASM += ", " + arg2;
                 *
                 * if (arg2 == 0x23)
                 * {
                 *  arg2 = grvReader.ReadByte();
                 *  genASM += ", " + arg2;
                 * }
                 * //    arg2 &= 0x7F;
                 * //    arg3 += vars[arg2 - 0x61];
                 * //}
                 * //else
                 * //{
                 * //    arg3 = vars[arg3 + arg2];
                 * //}
                 *
                 * //arg3 += 0x19;
                 *
                 * //// now got an arg1 (varOffset2) and arg3 (var13C)
                 */
                //byte tmp;
                //tmp = vars[arg1];
                //vars[arg1] = vars[arg2];
                //vars[arg2] = tmp;
            }
            break;

            case 0x3C:
                // Check valid savegames
            {
                genASM += ("\tCSAVE\t\t;Check saves");
            }
            break;

            case 0x3D:
                // Reset 0x00 to 0xFF to zero
                genASM += ("\tCLRVARS");
                break;

            case 0x3E:
                // MOD
            {
                Console.WriteLine("Needs work? to trace?");
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tMOD\t0x" + Convert.ToString(offset, 16) + ", " + grvReader.ReadByte());
            }
            break;

            case 0x3F:
                // Load subscript
            {
                genASM += "\tLOADSUB\t";
                char   t;
                string ret = "";
                while ((t = grvReader.ReadChar()) != 0)
                {
                    ret += t + ", ";
                }

                genASM += ret.Substring(0, ret.Length - 2);
                break;
                //Console.WriteLine("Unimplemented 3F");
                //return false;
                //Console.WriteLine("Run a subscript");
                //char c;
                //string f = "";
                //do
                //{
                //    //t = stream.ReadByte();
                //    c = (char)stream.ReadByte();
                //    if (c != 0)
                //        f += c;// t.ToString();
                //} while (c != 0);

                //sub = new Groovie(f, game);
                //sub.passParams(ref vars, music, ref flags, vdx);
            }


            case 0x40:
                // Two params for VDX player
            {
                genASM += "\tSETCOORD\t" + grvReader.ReadUInt16() + ", " + grvReader.ReadUInt16() + "\t;Set video start @ arg1,arg2";
                //flags |= bitFlags.Bit7;
                //ushort arg1 = stream.ReadUInt16();
                //if (arg1 > 0x7FFF)
                //{
                //    /*
                //     *  .text:00403E62 154         mov     ecx, arg1
                //        .text:00403E68 154         sub     ecx, 10000h     ; Integer Subtraction
                //        .text:00403E6E 154         mov     arg1, ecx

                //     */
                //}
                //stream.ReadUInt16();
                //// Same for arg2
                //throw new Exception("Two VDX player params");
            }
            break;

            case 0x41:
                // SUB
            {
                ushort offset;
                if (firstBit)
                {
                    offset = grvReader.ReadByte();
                }
                else
                {
                    offset = grvReader.ReadUInt16();
                }

                genASM += ("\tSUB\t0x" + Convert.ToString(offset, 16) + ", " + grvReader.ReadUInt16());
            }
            break;

            case 0x42:
                //throw new Exception("Complicated");
                //stream.ReadByte();
                //Console.WriteLine("Unimplemented - Othello AI");
                //return false;
                genASM += "\tOTHELLO\t" + grvReader.ReadByte();
                break;

            case 0x43:
                // Return from subscript
                //vars[0x102] = stream.ReadByte();
                //Console.WriteLine("Unimplemented - ret from script");
                // Say we're done in this script :)
                //return false;
                genASM += ("\tRETFS\t" + grvReader.ReadByte());
                break;


            case 0x4C:
                // Save current CD number.  1, 2, -1.
            {
                genASM += ("\tCCD");
            }
            break;

            case 0x4D:
                // If arg1 == 2, play VIE logo, stop midi, play CD
                //if (stream.ReadByte() == 2)
                //{
                //    // do some other checks on 0x09 flags?

                //    //VDX vdx = new VDX("vielogo", flags, game.GraphicsDevice);
                //    //game.addVDX(vdx);
                //    //vdx.play();
                //    //while (vdx.playing)
                //    //    System.Threading.Thread.Sleep(50);
                //}
                //if (music == null)
                //    music = new GrvMusic(game);

                //music.CD();
                genASM += ("\tPCD\t" + grvReader.ReadByte());
                break;

            case 0x51:
            {
                UInt16 jmp = grvReader.ReadUInt16();
                genASM += ("\tOp51\t" + jmp);
                scriptJumps.Add(jmp);
            }
            break;

            case 0x52:
                // Do heap to game area.  Arg discarded? o2_copyfgtobg
                // .text:00403B22
                genASM += ("\tCopyFgToBg\t" + grvReader.ReadByte());
                break;

            case 0x53:
                // Load cursor stuff o_hotspot_outrect
                genASM += ("\toutrect\t" + grvReader.ReadUInt16() + ", " + grvReader.ReadUInt16() + ", " + grvReader.ReadUInt16() + ", " + grvReader.ReadUInt16() + ", " + grvReader.ReadUInt16());
                break;

            case 0x55:
                genASM += ("\tSetScriptEnd\t" + grvReader.ReadUInt16());
                break;

            case 0x56:
                // o2_playsound
                genASM += ("\tSound\t" + grvReader.ReadUInt32() + ", " + grvReader.ReadByte() + ", " + grvReader.ReadByte());
                break;

            case 0x5A:
                // o2_preview_loadgame
                genASM += ("\tPreviewLoadGame\t" + grvReader.ReadByte());
                break;

            default:
                Console.WriteLine("Invalid opcode at location 0x" + Convert.ToString(grvReader.BaseStream.Position - 1, 16) + ": " + op);
                return(false);
            }
            genASM += ("\n");
            return(true);
        }
Ejemplo n.º 33
0
        public static bool ReadDelimitedDataFrom(this System.Text.Encoding encoding, System.IO.Stream stream, char[] delimits, ulong count, out string result, out ulong read, out System.Exception any, bool includeDelimits = true)
        {
            read = 0;

            any = null;

            if (delimits == null) delimits = EmptyChar;

            if (stream == null || false == stream.CanRead || count == 0)
            {
                result = null;

                return false;
            }

            //Use default..
            if (encoding == null) encoding = System.Text.Encoding.Default;

            System.Text.StringBuilder builder = null;

            bool sawDelimit = false;

            //Make the builder
            builder = new System.Text.StringBuilder();

            long at = stream.Position;

            //Use the BinaryReader on the stream to ensure ReadChar reads in the correct size
            //This prevents manual conversion from byte to char and uses the encoding's code page.
            using (var br = new System.IO.BinaryReader(stream, encoding, true))
            {
                //Read a while permitted, check for EOS
                while (read < count && stream.CanRead)
                {
                    try
                    {
                        //Get the char in the encoding beging used
                        char cached = br.ReadChar();

                        read = (ulong)(stream.Position - at);

                        //If the Byte was a delemit
                        if (System.Array.IndexOf<char>(delimits, cached) >= 0)
                        {
                            //Indicate the delimit was seen
                            sawDelimit = true;

                            //if the delemit should be included, include it.
                            if (includeDelimits) builder.Append(cached);

                            //Do not read further
                            goto Done;
                        }

                        //Create a string and append
                        builder.Append(cached);
                    }
                    catch (System.Exception ex)
                    {
                        any = ex;

                        read = (ulong)(stream.Position - at);

                        break;
                    }
                }
            }

            Done:

            if (builder == null)
            {
                result = null;

                return sawDelimit;
            }

            result = builder.Length == 0 ? string.Empty : builder.ToString();

            return sawDelimit;
        }
Ejemplo n.º 34
0
        public override void LoadData(System.IO.FileStream __fsFileStream)
        {
            System.IO.BinaryReader _brdrReader = new System.IO.BinaryReader(__fsFileStream);

            // only read necessary params from file header
            __fsFileStream.Seek(42, System.IO.SeekOrigin.Begin);
            int _iXDimension = (int)_brdrReader.ReadInt16();
            __fsFileStream.Seek(108, System.IO.SeekOrigin.Begin);
            DataType _dtDataType = (DataType)_brdrReader.ReadInt16();
            __fsFileStream.Seek(656, System.IO.SeekOrigin.Begin);
            int _iYDimension = (int)_brdrReader.ReadInt16();
            __fsFileStream.Seek(1446, System.IO.SeekOrigin.Begin);
            UInt32 numframes = (UInt32)_brdrReader.ReadInt32();

            // Start reading the XCalibStruct.
            SpeCalib XCalib = new SpeCalib(0, 0);
            __fsFileStream.Seek(3000, System.IO.SeekOrigin.Begin);
            XCalib.Offset = (double)_brdrReader.ReadDouble();
            __fsFileStream.Seek(3008, System.IO.SeekOrigin.Begin);
            XCalib.Factor = (double)_brdrReader.ReadDouble();
            __fsFileStream.Seek(3016, System.IO.SeekOrigin.Begin);
            XCalib.current_unit = (char)_brdrReader.ReadChar();
            __fsFileStream.Seek(3098, System.IO.SeekOrigin.Begin);
            XCalib.CalibValid = (char)_brdrReader.ReadChar();
            __fsFileStream.Seek(3101, System.IO.SeekOrigin.Begin);
            XCalib.PolynomOrder = (char)_brdrReader.ReadChar();
            __fsFileStream.Seek(3263, System.IO.SeekOrigin.Begin);

            XCalib.PolynomCoeff[0] = _brdrReader.ReadDouble();
            XCalib.PolynomCoeff[1] = _brdrReader.ReadDouble();
            XCalib.PolynomCoeff[2] = _brdrReader.ReadDouble();
            XCalib.PolynomCoeff[3] = _brdrReader.ReadDouble();
            XCalib.PolynomCoeff[4] = _brdrReader.ReadDouble();
            XCalib.PolynomCoeff[5] = _brdrReader.ReadDouble();

            __fsFileStream.Seek(3311, System.IO.SeekOrigin.Begin);
            XCalib.LaserPosition = (double)_brdrReader.ReadDouble();

            // Start reading the YCalibStruct.
            SpeCalib YCalib = new SpeCalib(0, 0);
            __fsFileStream.Seek(3489, System.IO.SeekOrigin.Begin);   // move ptr to x_calib start
            YCalib.Offset = (double)_brdrReader.ReadDouble();
            __fsFileStream.Seek(3497, System.IO.SeekOrigin.Begin);
            YCalib.Factor = (double)_brdrReader.ReadDouble();
            __fsFileStream.Seek(3505, System.IO.SeekOrigin.Begin);
            YCalib.current_unit = (char)_brdrReader.ReadChar();
            __fsFileStream.Seek(3587, System.IO.SeekOrigin.Begin);
            YCalib.CalibValid = (char)_brdrReader.ReadChar();
            __fsFileStream.Seek(3590, System.IO.SeekOrigin.Begin);
            YCalib.PolynomOrder = (char)_brdrReader.ReadChar();
            __fsFileStream.Seek(3752, System.IO.SeekOrigin.Begin);

            YCalib.PolynomCoeff[0] = _brdrReader.ReadDouble();
            YCalib.PolynomCoeff[1] = _brdrReader.ReadDouble();
            YCalib.PolynomCoeff[2] = _brdrReader.ReadDouble();
            YCalib.PolynomCoeff[3] = _brdrReader.ReadDouble();
            YCalib.PolynomCoeff[4] = _brdrReader.ReadDouble();
            YCalib.PolynomCoeff[5] = _brdrReader.ReadDouble();

            __fsFileStream.Seek(3800, System.IO.SeekOrigin.Begin);
            YCalib.LaserPosition = (double)_brdrReader.ReadDouble();

            int _iDimension;
            SpeCalib _calCurrCalib;
            if (_iYDimension == 1)
            {
                _iDimension = _iXDimension;
                _calCurrCalib = XCalib;
            }
            else if (_iXDimension == 1)
            {
                _iDimension = _iYDimension;
                _calCurrCalib = YCalib;
            }
            else
            {
                throw new UnexpectedFormatException("xylib does not support 2-D images");
            }

            __fsFileStream.Seek(4100, System.IO.SeekOrigin.Begin);      // move ptr to frames-start
            for (int frm = 0; frm < (int)numframes; frm++)
            {
                Block _blkBlock = new Block();

                Column _colXCol = this.GetCalibColumn(_calCurrCalib, _iDimension);
                _blkBlock.AddColumn(_colXCol, "", true);

                ListColumn _colYCol = new ListColumn();

                for (int i = 0; i < _iDimension; ++i)
                {
                    double _dYVal = 0;
                    switch (_dtDataType)
                    {
                        case DataType.SPE_DATA_FLOAT:
                            _dYVal = (double)_brdrReader.ReadSingle();
                            break;
                        case DataType.SPE_DATA_LONG:
                            _dYVal = (double)_brdrReader.ReadInt32();
                            break;
                        case DataType.SPE_DATA_INT:
                            _dYVal = (double)_brdrReader.ReadInt16();
                            break;
                        case DataType.SPE_DATA_UINT:
                            _dYVal = (double)_brdrReader.ReadUInt16();
                            break;
                        default:
                            break;
                    }
                    _colYCol.AddValue(_dYVal);
                }
                _blkBlock.AddColumn(_colYCol, "", true);
                this.m_blcklstBlocks.Add(_blkBlock);
            }
        }
Ejemplo n.º 35
0
        private void readGameData()
        {
            textBox2.Text = path;
            if (roq != null)
                roq.stop();
            roq = null;
            if (vdx != null)
                vdx.stop();
            vdx = null;
            switch (game)
            {
                case GameID.T7G:
                    lblGame.Text = "The 7th Guest";

                    System.IO.DirectoryInfo DI = new System.IO.DirectoryInfo(path);
                    System.IO.FileInfo[] files = DI.GetFiles("*.rl");
                    this.gjdChooser.Items.Clear();

                    foreach (System.IO.FileInfo rl in files)
                    {
                        this.gjdChooser.Items.Add(rl.Name);
                    }

                    break;
                case GameID.T11H:
                    lblGame.Text = "The 11th Hour";
                    //ROQ roq = new ROQ(new System.IO.BinaryReader(new System.IO.FileStream(path + "\\media\\final_hr.rol", System.IO.FileMode.Open)));

                    this.gjdChooser.Items.Clear();
                    System.IO.BinaryReader idx = new System.IO.BinaryReader(new System.IO.FileStream(path + "\\groovie\\gjd.gjd", System.IO.FileMode.Open));
                    string name = "";
                    while (idx.BaseStream.Position < idx.BaseStream.Length)
                    {
                        if (idx.PeekChar() == 0x0A)
                        {
                            idx.ReadChar();
                            if (name.Length > 0)
                                this.gjdChooser.Items.Add(name.Substring(0, name.IndexOf(" ")));

                            name = "";
                        }
                        else
                            name += "" + idx.ReadChar();
                    }
                    idx.Close();
                    V2_RL = new List<GJD.RLData>[this.gjdChooser.Items.Count];
                    for (int i = 0; i < V2_RL.Length; i++)
                        V2_RL[i] = new List<GJD.RLData>();

                    this.gjdChooser.Items.Add("Icons");

                    idx = new System.IO.BinaryReader(new System.IO.FileStream(path + "\\groovie\\dir.rl", System.IO.FileMode.Open));
                    uint ctr = 0;
                    while (idx.BaseStream.Position < idx.BaseStream.Length)
                    {
                        // Get RL content
                        GJD.RLData rl = new GJD.RLData();
                        idx.ReadUInt32();
                        rl.offset = idx.ReadUInt32();
                        rl.length = idx.ReadUInt32();
                        rl.number = ctr;
                        ctr++;
                        ushort target = idx.ReadUInt16();
                        byte[] filename;
                        filename = idx.ReadBytes(12);
                        rl.filename = System.Text.Encoding.ASCII.GetString(filename).Trim();
                        idx.ReadBytes(6);
                        V2_RL[target].Add(rl);
                    }

                    break;
                default:
                    lblGame.Text = "None";
                    break;
            }
        }
Ejemplo n.º 36
0
        private void OpenFile(string filename)
        {
            #if !DEBUG
            try
            {
            #endif
                _filename = filename;
                fileIn = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read));

                string startFileSig = "";
                for (int i = 0; i < 4; i++) // 4 bytes
                {
                    startFileSig += fileIn.ReadChar();
                }
                if (startFileSig != "#PES")
                {
                    // This is not a file that we can read
                    readyStatus = statusEnum.ParseError;
                    lastError = "Missing #PES at beginning of file";
                    fileIn.Close();
                    return;
                }

                // PES version
                string versionString = "";
                for (int i = 0; i < 4; i++) // 4 bytes
                {
                    versionString += fileIn.ReadChar();
                }
                pesVersion = Convert.ToUInt16(versionString);

                int pecstart = fileIn.ReadInt32();

                // Read number of colors in this design
                fileIn.BaseStream.Position = pecstart + 48;
                int numColors = fileIn.ReadByte() +1;
                List<byte> colorList = new List<byte>();
                for (int x = 0; x < numColors; x++)
                {
                    colorList.Add(fileIn.ReadByte());
                }

                // Read stitch data
                fileIn.BaseStream.Position = pecstart + 532;
                bool thisPartIsDone = false;
                StitchBlock curBlock;
                int prevX = 0;
                int prevY = 0;
                int maxX = 0;
                int minX = 0;
                int maxY = 0;
                int minY = 0;
                int colorNum = -1;
                int colorIndex = 0;
                List<Stitch> tempStitches = new List<Stitch>();
                while (!thisPartIsDone)
                {
                    byte val1;
                    byte val2;
                    val1 = fileIn.ReadByte();
                    val2 = fileIn.ReadByte();
                    if (val1 == 0xff && val2 == 0x00)
                    {
                        //end of stitches
                        thisPartIsDone = true;

                        //add the last block
                        curBlock = new StitchBlock();
                        curBlock.stitches = new Stitch[tempStitches.Count];
                        tempStitches.CopyTo(curBlock.stitches);
                        curBlock.stitchesTotal = tempStitches.Count;
                        colorNum++;
                        colorIndex = colorList[colorNum];
                        curBlock.colorIndex = colorIndex;
                        curBlock.color = getColorFromIndex(colorIndex);
                        blocks.Add(curBlock);
                    }
                    else if (val1 == 0xfe && val2 == 0xb0)
                    {
                        //color switch, start a new block

                        curBlock = new StitchBlock();
                        curBlock.stitches = new Stitch[tempStitches.Count];
                        tempStitches.CopyTo(curBlock.stitches);
                        curBlock.stitchesTotal = tempStitches.Count;
                        colorNum++;
                        colorIndex = colorList[colorNum];
                        curBlock.colorIndex = colorIndex;
                        curBlock.color = getColorFromIndex(colorIndex);
                        //read useless(?) byte
                        // The value of this 'useless' byte seems to alternate
                        // between 2 and 1 for every other block. The only
                        // exception I've noted is the last block which appears
                        // to always be 0.
                        curBlock.unknownStartByte = fileIn.ReadByte();
                        blocks.Add(curBlock);

                        tempStitches = new List<Stitch>();
                    }
                    else
                    {
                        int deltaX = 0;
                        int deltaY = 0;
                        if ((val1 & 0x80) == 0x80)
                        {
                            // This is a 12-bit int. Allows for needle movement
                            // of up to +2047 or -2048.
                            deltaX = get12Bit2sComplement(val1, val2);

                            // The X value used both bytes, so read next byte
                            // for Y value.
                            val1 = fileIn.ReadByte();
                        }
                        else
                        {
                            // This is a 7-bit int. Allows for needle movement
                            // of up to +63 or -64.
                            deltaX = get7Bit2sComplement(val1);

                            // The X value only used 1 byte, so copy the second
                            // to to the first for Y value.
                            val1 = val2;
                        }

                        if ((val1 & 0x80) == 0x80)
                        {
                            // This is a 12-bit int. Allows for needle movement
                            // of up to +2047 or -2048.
                            // Read in the next byte to get the full value
                            val2 = fileIn.ReadByte();
                            deltaY = get12Bit2sComplement(val1, val2);
                        }
                        else
                        {
                            // This is a 7-bit int. Allows for needle movement
                            // of up to +63 or -64.
                            deltaY = get7Bit2sComplement(val1);
                            // Finished reading data for this stitch, no more
                            // bytes needed.
                        }
                        tempStitches.Add(
                            new Stitch(
                                new Point(prevX, prevY),
                                new Point(prevX + deltaX, prevY + deltaY)
                            )
                        );
                        prevX = prevX + deltaX;
                        prevY = prevY + deltaY;
                        if (prevX > maxX)
                        {
                            maxX = prevX;
                        }
                        else if (prevX < minX)
                        {
                            minX = prevX;
                        }

                        if (prevY > maxY)
                        {
                            maxY = prevY;
                        }
                        else if (prevY < minY)
                        {
                            minY = prevY;
                        }
                    }
                }
                imageWidth = maxX - minX;
                imageHeight = maxY - minY;
                translateStart.X = -minX;
                translateStart.Y = -minY;
                readyStatus = statusEnum.Ready;

                // Close the file
                fileIn.Close();

            #if !DEBUG
            }
            catch (System.IO.IOException ioex)
            {
                readyStatus = statusEnum.IOError;
                lastError = ioex.Message;
                if (fileIn != null)
                {
                    fileIn.Close();
                }
            }
            catch (Exception ex)
            {
                readyStatus = statusEnum.ParseError;
                lastError = ex.Message;
                if (fileIn != null)
                {
                    fileIn.Close();
                }
            }
            #endif
        }
 private void Process(string path)
 {
     if (!System.IO.File.Exists(path))
     {
         //MessageBox.Show("种子文件异常!", "提示",MessageBoxButton.OK,MessageBoxImage.Error);
     }
     TotalValues = 0L;
     _path = path;
     _torrent = new System.IO.BinaryReader(new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read),
                                 System.Text.Encoding.UTF8);
     if (_torrent.ReadChar() != 'd')
     {
         //MessageBox.Show("种子文件异常!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
         //行了
     }
     _root = ProcessDict();
     ShaHash = GetShaHash();
     _files = GetFiles();
     _torrent.Close();
 }
Ejemplo n.º 38
0
Archivo: Row.cs Proyecto: vebin/BD2
 public static Row Deserialize(byte[] bytes)
 {
     using (System.IO.MemoryStream MS = new System.IO.MemoryStream (bytes, false)) {
         using (System.IO.BinaryReader BR = new System.IO.BinaryReader (MS)) {
             byte[] columnSet = BR.ReadBytes (32);
             int FieldCount = BR.ReadInt32 ();
             object[] fields = new object[FieldCount];
             ColumnSet cs = css [columnSet];
             if (cs.Columns.Length != fields.Length)
                 throw new Exception ();
             for (int n = 0; n != fields.Length; n++) {
                 bool Null = BR.ReadBoolean ();
                 if (Null) {
                     fields [n] = null;
                     continue;
                 }
                 switch (cs.Columns [n].TFQN) {
                 case "System.Byte[]":
                     fields [n] = BR.ReadBytes (BR.ReadInt32 ());
                     break;
                 case "System.Byte":
                     fields [n] = BR.ReadByte ();
                     break;
                 case "System.SByte":
                     fields [n] = BR.ReadSByte ();
                     break;
                 case "System.Int16":
                     fields [n] = BR.ReadInt16 ();
                     break;
                 case "System.UInt16":
                     fields [n] = BR.ReadUInt16 ();
                     break;
                 case "System.Int32":
                     fields [n] = BR.ReadInt32 ();
                     break;
                 case "System.UInt32":
                     fields [n] = BR.ReadUInt32 ();
                     break;
                 case "System.Int64":
                     fields [n] = BR.ReadInt64 ();
                     break;
                 case "System.UInt64":
                     fields [n] = BR.ReadUInt64 ();
                     break;
                 case "System.Single":
                     fields [n] = BR.ReadSingle ();
                     break;
                 case "System.Double":
                     fields [n] = BR.ReadDouble ();
                     break;
                 case "System.String":
                     fields [n] = BR.ReadString ();
                     break;
                 case "System.Char":
                     fields [n] = BR.ReadChar ();
                     break;
                 case "System.Boolean":
                     fields [n] = BR.ReadBoolean ();
                     break;
                 case "System.DateTime":
                     fields [n] = new DateTime (BR.ReadInt64 ());
                     break;
                 case "System.Guid":
                     fields [n] = new Guid (BR.ReadBytes (16));
                     break;
                 }
             }
             return new Row (cs, fields);
         }
     }
 }
Ejemplo n.º 39
0
        public void LeCadastroDoArquivo()
        {
            System.IO.BinaryReader leitor = null;
            Aluno a;
            int n;

            this.lista = new System.Collections.Generic.List<Aluno>();

            try
            {
                leitor = new System.IO.BinaryReader(new System.IO.FileStream("alunos.dat", System.IO.FileMode.Open));

                n = leitor.ReadInt32();
                for (int i = 0; i < n; i++)
                {
                    a = new Aluno();
                    a.nome = leitor.ReadString();
                    a.idade = leitor.ReadInt32();
                    a.sexo = leitor.ReadChar();

                    this.lista.Add(a);
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("AVISO! Arquivo alunos.dat não foi encontrado. Cadastro começará vazio.");
                Console.ReadKey();
            }
            catch (System.Exception exc)
            {
                Console.WriteLine("ERRO! " + exc.Message);
                Console.ReadKey();
            }
            finally
            {
                if (leitor != null)
                    leitor.Close();
            }
        }
Ejemplo n.º 40
0
        private void OpenFile(string filename)
        {
            #if !DEBUG
            try
            {
            #endif
                _filename = filename;
                fileIn = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read));

                string startFileSig = "";
                for (int i = 0; i < 4; i++) // 4 bytes
                {
                    startFileSig += fileIn.ReadChar();
                }
                if (startFileSig != "#PES")
                {
                    // This is not a file that we can read
                    readyStatus = statusEnum.ParseError;
                    lastError = "Missing #PES at beginning of file";
                    fileIn.Close();
                    return;
                }

                // PES version
                string versionString = "";
                for (int i = 0; i < 4; i++) // 4 bytes
                {
                    versionString += fileIn.ReadChar();
                }
                pesVersion = Convert.ToUInt16(versionString);

                int pecstart = fileIn.ReadInt32();

                fileIn.BaseStream.Position = pecstart + 48;
                int numColors = fileIn.ReadByte() +1;
                List<byte> colorList = new List<byte>();
                for (int x = 0; x < numColors; x++)
                {
                    colorList.Add(fileIn.ReadByte());
                }

                fileIn.BaseStream.Position = pecstart + 532;
                bool thisPartIsDone = false;
                stitchBlock curBlock;
                int prevX = 0;
                int prevY = 0;
                int maxX = 0;
                int minX = 0;
                int maxY = 0;
                int minY = 0;
                int colorNum = -1;
                int colorIndex = 0;
                List<Point> tempStitches = new List<Point>();
                while (!thisPartIsDone)
                {
                    byte val1;
                    byte val2;
                    val1 = fileIn.ReadByte();
                    val2 = fileIn.ReadByte();
                    if (val1 == 255 && val2 == 0)
                    {
                        //end of stitches
                        thisPartIsDone = true;

                        //add the last block
                        curBlock = new stitchBlock();
                        curBlock.stitches = new Point[tempStitches.Count];
                        tempStitches.CopyTo(curBlock.stitches);
                        curBlock.stitchesTotal = tempStitches.Count;
                        colorNum++;
                        colorIndex = colorList[colorNum];
                        curBlock.colorIndex = colorIndex;
                        curBlock.color = getColorFromIndex(colorIndex);
                        blocks.Add(curBlock);
                    }
                    else if (val1 == 254 && val2 == 176)
                    {
                        //color switch, start a new block

                        curBlock = new stitchBlock();
                        curBlock.stitches = new Point[tempStitches.Count];
                        tempStitches.CopyTo(curBlock.stitches);
                        curBlock.stitchesTotal = tempStitches.Count;
                        colorNum++;
                        colorIndex = colorList[colorNum];
                        curBlock.colorIndex = colorIndex;
                        curBlock.color = getColorFromIndex(colorIndex);
                        blocks.Add(curBlock);

                        tempStitches = new List<Point>();

                        //read useless(?) byte
                        fileIn.ReadByte();
                    }
                    else
                    {
                        int deltaX = 0;
                        int deltaY = 0;
                        if ((val1 & 0x80) == 0x80)
                        {
                            //this is a jump stitch
                            deltaX = ((val1 & 0x0f) << 8) + val2;
                            /* dead code? bit we're checking is masked out in previous line
                            if ((deltaX & 0x0800) == 0x0800)
                            {
                                deltaX = deltaX - 4096;
                            }
                            */
                            //read next byte for Y value
                            val2 = fileIn.ReadByte();
                        }
                        else
                        {
                            //normal stitch
                            deltaX = val1;
                            if (deltaX > 63)
                            {
                                deltaX = deltaX - 128;
                            }
                        }

                        if ((val2 & 0x08) == 0x80)
                        {
                            //this is a jump stitch
                            int val3 = fileIn.ReadByte();
                            deltaY = ((val2 & 0x0f) << 8) + val3;
                            /* dead code? bit we're checking is masked out in previous line
                            if ((deltaY & 0x0800) == 0x0800)
                            {
                                deltaY = deltaY - 4096;
                            }*/
                        }
                        else
                        {
                            //normal stitch
                            deltaY = val2;
                            if (deltaY > 63)
                            {
                                deltaY = deltaY - 128;
                            }
                        }
                        tempStitches.Add(new Point(prevX + deltaX, prevY + deltaY));
                        prevX = prevX + deltaX;
                        prevY = prevY + deltaY;
                        if (prevX > maxX)
                        {
                            maxX = prevX;
                        }
                        else if (prevX < minX)
                        {
                            minX = prevX;
                        }

                        if (prevY > maxY)
                        {
                            maxY = prevY;
                        }
                        else if (prevY < minY)
                        {
                            minY = prevY;
                        }
                    }
                }
                imageWidth = maxX - minX;
                imageHeight = maxY - minY;
                translateStart.X = -minX;
                translateStart.Y = -minY;
                readyStatus = statusEnum.Ready;

                // Close the file
                fileIn.Close();

            #if !DEBUG
            }
            catch (System.IO.IOException ioex)
            {
                readyStatus = statusEnum.IOError;
                lastError = ioex.Message;
                if (fileIn != null)
                {
                    fileIn.Close();
                }
            }
            catch (Exception ex)
            {
                readyStatus = statusEnum.ParseError;
                lastError = ex.Message;
                if (fileIn != null)
                {
                    fileIn.Close();
                }
            }
            #endif
        }