Example #1
0
        public static T ReadStruct <T>(this Stream Stream) where T : struct
        {
            var Size   = Marshal.SizeOf(typeof(T));
            var Buffer = Stream.ReadBytes(Size);

            return(StructUtils.BytesToStruct <T>(Buffer));
        }
Example #2
0
        public static bool IsValid(byte[] MagicData)
        {
            try
            {
                var ImageVersion = StructUtils.BytesToStruct <ImageVersionStruct>(MagicData);
                switch (ImageVersion.Version)
                {
                case 1:
                    break;

                case 2:
                    var ImageHeader = StructUtils.BytesToStruct <ImageHeaderStructV2>(MagicData);
                    if (ImageHeader.Surface2DCount > 10000)
                    {
                        return(false);
                    }
                    if (ImageHeader.Surface3DCount > 10000)
                    {
                        return(false);
                    }
                    break;

                default:
                    return(false);
                }
                return(true);
            }
            catch
            {
                //Console.WriteLine(Exception);
            }
            return(false);
        }
        private byte[] HandlePacket_GetRankingInfo(byte[] RequestContent)
        {
            var Request = StructUtils.BytesToStruct <GetRankingInfo_RequestStruct>(RequestContent);

            var Index    = ServerManager.ServerIndices[Request.RankingIndex];
            var Response = default(GetRankingInfo_ResponseStruct);

            Response = new GetRankingInfo_ResponseStruct()
            {
                Result      = 0,
                Length      = Index.Tree.Count,
                Direction   = Index.SortingDirection,
                TopScore    = 0,
                BottomScore = 0,
                MaxElements = -1,
                TreeHeight  = -1
                              //TreeHeight = Index.Tree.height
            };

            if (Index.Tree.Count > 0)
            {
                Response.TopScore    = Index.Tree.FrontElement.ScoreValue;
                Response.BottomScore = Index.Tree.BackElement.ScoreValue;
            }

            return(StructUtils.StructToBytes(Response));
        }
        private byte[] HandlePacket_ListElements(byte[] RequestContent)
        {
            List <ListElements_ResponseEntryStruct> ResponseEntries = new List <ListElements_ResponseEntryStruct>();

            var Request = StructUtils.BytesToStruct <ListElements_RequestStruct>(RequestContent);

            // http://stackoverflow.com/questions/7032290/what-happens-to-an-awaiting-thread-in-c-sharp-async-ctp
            {
                var RankingIndex       = ServerManager.ServerIndices[Request.RankingIndexId];
                int CurrentEntryOffset = Request.Offset;

                if (Request.Offset >= 0)
                {
                    foreach (var UserScore in RankingIndex.GetRange(Request.Offset, Request.Count))
                    {
                        ResponseEntries.Add(new ListElements_ResponseEntryStruct()
                        {
                            Position       = CurrentEntryOffset,
                            UserId         = UserScore.UserId,
                            ScoreValue     = UserScore.ScoreValue,
                            ScoreTimeStamp = UserScore.ScoreTimeStamp,
                        });
                        CurrentEntryOffset++;
                    }
                }
            }

            return(StructUtils.StructArrayToBytes(ResponseEntries.ToArray()));
        }
Example #5
0
        protected void ProcessDirectoryRecord(IsoNode parentIsoNode)
        {
            var directoryStart  = parentIsoNode.DirectoryRecord.Extent * SectorSize;
            var directoryLength = parentIsoNode.DirectoryRecord.Size;
            var directoryStream = Stream.SliceWithLength(directoryStart, directoryLength);

            while (!directoryStream.Eof())
            {
                //writefln("%08X : %08X : %08X", directoryStream.position, directoryStart, directoryLength);
                var directoryRecordSize = (byte)directoryStream.ReadByte();

                // Even if a directory spans multiple sectors, the directory entries are not permitted to cross the sector boundary (unlike the path table).
                // Where there is not enough space to record an entire directory entry at the end of a sector, that sector is zero-padded and the next
                // consecutive sector is used.
                if (directoryRecordSize == 0)
                {
                    directoryStream.Position = MathUtils.NextAligned(directoryStream.Position, SectorSize);
                    //Console.WriteLine("AlignedTo: {0:X}", DirectoryStream.Position);
                    continue;
                }

                directoryStream.Position = directoryStream.Position - 1;

                //Console.WriteLine("[{0}:{1:X}-{2:X}]", DirectoryRecordSize, DirectoryStream.Position, DirectoryStream.Position + DirectoryRecordSize);

                var directoryRecordBytes = new byte[directoryRecordSize];
                directoryStream.Read(directoryRecordBytes, 0, directoryRecordSize);
                var directoryRecord = StructUtils.BytesToStruct <DirectoryRecord>(directoryRecordBytes);

                var name =
                    Encoding.UTF8.GetString(directoryRecordBytes.Slice(sizeof(DirectoryRecord),
                                                                       directoryRecord.NameLength));

                //Console.WriteLine("{0}", name); Console.ReadKey();

                if (name == "\x00" || name == "\x01")
                {
                    continue;
                }

                //writefln("   %s", name);

                var childIsoNode = new IsoNode(this, directoryRecord, name, parentIsoNode);
                parentIsoNode.Childs2.Add(childIsoNode);
                parentIsoNode.ChildsByName[childIsoNode.Name] = childIsoNode;
                parentIsoNode.ChildsByNameUpperCase[childIsoNode.Name.ToUpper()] = childIsoNode;
            }

            foreach (var child in parentIsoNode.Childs2)
            {
                if (child.IsDirectory)
                {
                    ProcessDirectoryRecord(child);
                }
            }
        }
Example #6
0
 static public bool IsValid(byte[] Data)
 {
     try
     {
         var Header = StructUtils.BytesToStruct <HeaderStruct>(Data);
         if (Header.Magic != "TO8CHTX")
         {
             return(false);
         }
         return(true);
     }
     catch
     {
     }
     return(false);
 }
Example #7
0
    /// <summary>
    ///
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="Stream"></param>
    /// <returns></returns>
    public static T ReadStructPartially <T>(this Stream Stream) where T : struct
    {
        var Size          = Marshal.SizeOf(typeof(T));
        var BufferPartial = Stream.ReadBytes(Math.Min((int)Stream.Available(), Size));

        byte[] Buffer;
        if (BufferPartial.Length < Size)
        {
            Buffer = new byte[Size];
            BufferPartial.CopyTo(Buffer, 0);
        }
        else
        {
            Buffer = BufferPartial;
        }
        return(StructUtils.BytesToStruct <T>(Buffer));
    }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static T ReadStructPartially <T>(this Stream stream) where T : struct
        {
            var size          = Marshal.SizeOf(typeof(T));
            var bufferPartial = stream.ReadBytes(Math.Min((int)stream.Available(), size));

            byte[] buffer;
            if (bufferPartial.Length < size)
            {
                buffer = new byte[size];
                bufferPartial.CopyTo(buffer, 0);
            }
            else
            {
                buffer = bufferPartial;
            }
            return(StructUtils.BytesToStruct <T>(buffer));
        }
        private byte[] HandlePacket_RemoveAllElements(byte[] RequestContent)
        {
            var Request = StructUtils.BytesToStruct <RemoveAllElements_RequestStruct>(RequestContent);

            uint Count = 0;

            {
                var RankingIndex = ServerManager.ServerIndices[Request.RankingIndexId];
                Count = (uint)RankingIndex.Tree.Count;
                RankingIndex.RemoveAllItems();
            }

            return(StructUtils.StructToBytes(new RemoveAllElements_ResponseStruct()
            {
                Result = 0,
                Count = Count,
            }));
        }
        async private Task <byte[]> HandlePacketAsync_RemoveAllElements(byte[] RequestContent)
        {
            var Request = StructUtils.BytesToStruct <RemoveAllElements_RequestStruct>(RequestContent);

            uint Count = 0;

            await EnqueueTaskAsync((uint)Request.RankingIndexId, () =>
            {
                var RankingIndex = ServerManager.ServerIndices[Request.RankingIndexId];
                Count            = (uint)RankingIndex.Tree.Count;
                RankingIndex.RemoveAllItems();
            });

            return(StructUtils.StructToBytes(new RemoveAllElements_ResponseStruct()
            {
                Result = 0,
                Count = Count,
            }));
        }
Example #11
0
 public static bool IsValid(byte[] MagicData)
 {
     try
     {
         var Header = StructUtils.BytesToStruct <HeaderStruct>(MagicData);
         var Magic  = Encoding.ASCII.GetString(Header.Magic);
         if (Magic != "FPS4")
         {
             return(false);
         }
         if (Header.ListCount > 10000)
         {
             return(false);
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        private async Task <byte[]> HandlePacketAsync_GetElement(byte[] RequestContent)
        {
            var Request       = StructUtils.BytesToStruct <GetElement_RequestStruct>(RequestContent);
            int IndexPosition = -1;
            var UserScore     = default(ServerIndices.UserScore);

            await EnqueueTaskAsync((uint)Request.RankingIndex, () =>
            {
                var Ranking = ServerManager.ServerIndices[Request.RankingIndex];
                try
                {
                    UserScore     = Ranking.GetUserScore(Request.UserId);
                    IndexPosition = Ranking.Tree.GetItemPosition(UserScore);
                }
                catch
                {
                }
            });

            if (IndexPosition == -1 || UserScore == null)
            {
                return(StructUtils.StructToBytes(new GetElement_ResponseStruct()
                {
                    Position = -1,
                    UserId = 0,
                    ScoreValue = 0,
                    ScoreTimeStamp = 0,
                }));
            }
            else
            {
                return(StructUtils.StructToBytes(new GetElement_ResponseStruct()
                {
                    Position = IndexPosition,
                    UserId = UserScore.UserId,
                    ScoreValue = UserScore.ScoreValue,
                    ScoreTimeStamp = UserScore.ScoreTimeStamp,
                }));
            }
        }
Example #13
0
 public TType ReadStruct <TType>(uint Address) where TType : struct => StructUtils.BytesToStruct <TType>(ReadBytes(Address, PointerUtils.Sizeof <TType>()));
Example #14
0
        static public int DetectVersion(byte[] MagicData, long FileSize, bool ThrowException = false)
        {
            var Warnings = new List <String>();

            MagicData = MagicData.Take(0x10).ToArray();
            if (MagicData.Length < 0x10)
            {
                if (ThrowException)
                {
                    throw (new Exception("Start to short"));
                }
                return(-1);
            }

            // Version type (0, 1, 3, 4)
            {
                var HeaderStruct = StructUtils.BytesToStruct <TalesCompression1_3.HeaderStruct>(MagicData);
                //Console.WriteLine("[1]");

                bool Handled = true;

                switch (HeaderStruct.Version)
                {
#if true
                case 0:
                    if (HeaderStruct.CompressedLength != HeaderStruct.UncompressedLength)
                    {
                        Warnings.Add("Compressed/Uncompressed Length must match");
                    }
                    if (HeaderStruct.CompressedLength >= 64 * 1024 * 1024)
                    {
                        Warnings.Add("Compressed/Uncompressed Length too big");
                    }
                    break;
#endif
                case 1:
                case 3:
                case 4:
                    if (HeaderStruct.CompressedLength >= 64 * 1024 * 1024)
                    {
                        Warnings.Add("Compressed Length too big");
                    }
                    if (HeaderStruct.UncompressedLength >= 64 * 1024 * 1024)
                    {
                        Warnings.Add("Uncompressed Length too big");
                    }
                    if (HeaderStruct.CompressedLength > HeaderStruct.UncompressedLength)
                    {
                        Warnings.Add("Compressed size is bigger than the uncompressed size");
                    }

                    /*
                     *      var DiffSize = Math.Abs((long)HeaderStruct.CompressedLength - FileSize);
                     *      if (DiffSize >= 0x10000)
                     *      {
                     *              Warnings.Add(String.Format("Invalid compressed size {0}", DiffSize));
                     *      }
                     */

                    break;

                default:
                    Handled = false;
                    break;
                }

                //Console.WriteLine("[2]");

                if (Handled)
                {
                    if (!Warnings.Any())
                    {
                        return(HeaderStruct.Version);
                    }
                }

                //Console.WriteLine("[3]");
            }

            //Console.WriteLine("[4]");

            // Check other types.
            {
                if (MagicData.SequenceEqual(TalesCompression15_Lzx.Signature))
                {
                    //Console.WriteLine("[5]");
                    return(15);
                }
            }

            if (!ThrowException)
            {
                return(-1);
            }
            else
            {
                //Console.WriteLine("[6]");

                throw (new NotSupportedException("Version not detected : " + Warnings.Implode(",") + " : " + MagicData.ToHexString()));
                //throw (new Exception());
            }
        }
Example #15
0
 public TType ReadStruct <TType>(uint Address) where TType : struct
 {
     return(StructUtils.BytesToStruct <TType>(ReadBytes(Address, Marshal.SizeOf(typeof(TType)))));
 }
        private byte[] HandlePacket_GetRankingNameById(byte[] RequestContent)
        {
            var Request = StructUtils.BytesToStruct <GetRankingNameById_RequestStruct>(RequestContent);

            return(Encoding.UTF8.GetBytes(ServerManager.ServerIndices[Request.RankingIndex].IndexName));
        }