Inheritance: System.IO.Stream
        public async Task <string> Base64DecompressAsync(string data, Base64CompressionOptions options = null)
        {
            var _options = GetOptions(options);
            var inStream = new MemoryStream();
            var bytes    = System.Convert.FromBase64String(data);

            await inStream.WriteAsync(bytes, 0, bytes.Length);

            inStream.Position = 0;

            var outStream  = new MemoryStream();
            var outZStream = new zlib.ZOutputStream(outStream);

            try
            {
                await copyStreamAsync(inStream, outZStream, _options.DecompressBufferSize);
            }
            finally
            {
                outZStream.Close();
                outStream.Close();
                inStream.Close();
            }

            var x = outStream.ToArray();

            return(Encoding.UTF8.GetString(x));
        }
Example #2
0
        /// <summary>
        /// Cache all item templates
        /// </summary>
        /// <returns>number of cached items</returns>
        public static int CacheAllItems()
        {
            DateTime _now = DateTime.Now;
            ItemList = new List<AOItem>();
            Stream sf = new FileStream("items.dat", FileMode.Open);
            MemoryStream ms = new MemoryStream();

            ZOutputStream sm = new ZOutputStream(ms);
            CopyStream(sf, sm);

            ms.Seek(0, SeekOrigin.Begin);

            byte[] buffer = new byte[4];
            ms.Read(buffer, 0, 4);
            int packaged = BitConverter.ToInt32(buffer, 0);

            BinaryReader br = new BinaryReader(ms);
            BinaryFormatter bf = new BinaryFormatter();

            while (true)
            {
                List<AOItem> templist = (List<AOItem>) bf.Deserialize(ms);
                ItemList.AddRange(templist);
                if (templist.Count != packaged)
                {
                    break;
                }
                Console.Write("Loaded {0} items in {1}\r",
                              new object[]
                                  {ItemList.Count, new DateTime((DateTime.Now - _now).Ticks).ToString("mm:ss.ff")});
            }
            GC.Collect();
            return ItemList.Count;
        }
        public async Task <byte[]> DecompressAsync(byte[] data, ZlibCompressionOptions options)
        {
            var _options = GetOptions(options);
            var inStream = new MemoryStream();
            var bytes    = data;

            await inStream.WriteAsync(bytes, 0, bytes.Length);

            inStream.Position = 0;

            var outStream  = new MemoryStream();
            var outZStream = new zlib.ZOutputStream(outStream);

            try
            {
                await copyStreamAsync(inStream, outZStream, _options.DecompressBufferSize);
            }
            finally
            {
                outZStream.Close();
                outStream.Close();
                inStream.Close();
            }

            return(outStream.ToArray());
        }
        public string Base64Compress(string data, Base64CompressionOptions options = null)
        {
            var _options  = GetOptions(options);
            var bytes     = Encoding.UTF8.GetBytes(data);
            var outStream = new MemoryStream();
            var inStream  = new MemoryStream();

            inStream.Write(bytes, 0, bytes.Length);
            inStream.Position = 0;

            var outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            try
            {
                copyStream(inStream, outZStream, _options.CompressBufferSize);
            }
            finally
            {
                outZStream.Close();
                outStream.Close();
                inStream.Close();
            }

            var x = outStream.ToArray();

            return(System.Convert.ToBase64String(x));
        }
        public byte[] Compress(byte[] data, ZlibCompressionOptions options)
        {
            var _options  = GetOptions(options);
            var bytes     = data;
            var outStream = new MemoryStream();
            var inStream  = new MemoryStream();

            inStream.Write(bytes, 0, bytes.Length);
            inStream.Position = 0;

            var outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            try
            {
                copyStream(inStream, outZStream, _options.CompressBufferSize);
            }
            finally
            {
                outZStream.Close();
                outStream.Close();
                inStream.Close();
            }

            return(outStream.ToArray());
        }
        /// <summary>
        /// Cache all item templates
        /// </summary>
        /// <returns>number of cached items</returns>
        public static int CacheAllNanos()
        {
            DateTime _now = DateTime.Now;
            NanoList = new List<AONanos>();
            Stream sf = new FileStream("nanos.dat", FileMode.Open);
            MemoryStream ms = new MemoryStream();

            ZOutputStream sm = new ZOutputStream(ms);
            CopyStream(sf, sm);

            ms.Seek(0, SeekOrigin.Begin);

            byte[] buffer = new byte[4];
            ms.Read(buffer, 0, 4);
            int packaged = BitConverter.ToInt32(buffer, 0);

            BinaryReader br = new BinaryReader(ms);
            var bf = MessagePackSerializer.Create<List<AONanos>>();

            while (true)
            {
                List<AONanos> templist = bf.Unpack(ms);
                NanoList.AddRange(templist);
                if (templist.Count != packaged)
                {
                    break;
                }
                Console.Write("Loaded {0} Nanos in {1}\r",
                              new object[]
                                  {NanoList.Count, new DateTime((DateTime.Now - _now).Ticks).ToString("mm:ss.ff")});
            }
            GC.Collect();
            return NanoList.Count;
        }
Example #7
0
 public static Stream DecompressFrame(Stream stream, int compressedSize)
 {
     Stream outStream = new MemoryStream();
     ZOutputStream outZStream = new ZOutputStream(outStream);
     CopyStream(stream, outZStream, compressedSize);
     outStream.Position = 0;
     return outStream;
 }
Example #8
0
 public static void DecompressData(byte[] inData, out byte[] outData)
 {
     using (MemoryStream outMemoryStream = new MemoryStream())
     using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream))
     using (Stream inMemoryStream = new MemoryStream(inData))
     {
         CopyStream(inMemoryStream, outZStream);
         outZStream.finish();
         outData = outMemoryStream.ToArray();
     }
 }
Example #9
0
 public static void CompressData(byte[] inData, out byte[] outData)
 {
     using (MemoryStream outMemoryStream = new MemoryStream())
     using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
     using (Stream inMemoryStream = new MemoryStream(inData))
     {
         CopyStream(inMemoryStream, outZStream);
         outZStream.finish();
         outData = outMemoryStream.ToArray();
     }
 }
 public static byte[] decompress(byte[] data,UInt32 realsize)
 {
     MemoryStream o=new MemoryStream();
     ZOutputStream z = new ZOutputStream(o);
     MemoryStream i = new MemoryStream(data);
     CopyStream(i, z);
     byte[] res = o.ToArray();
     i.Close();
     z.Close();
     o.Close();
     return res;
 }
Example #11
0
 public static byte[] Compress(byte[] inputBytes)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (ZOutputStream stream2 = new ZOutputStream(stream, 0))
         {
             stream2.Write(inputBytes, 0, inputBytes.Length);
             stream2.finish();
             return stream.ToArray();
         }
     }
 }
Example #12
0
        private bool ReplaceContentHelperFunc(byte[] content, FileStream datFileStream)
        {
            // Store the old offsets just in case we need to perform a restore.
            // This actually isn't necessary currently, since the raf directory file is saved after packing.
            UInt32 oldOffset = FileOffset;
            UInt32 oldSize   = FileSize;

            try
            {
                // Navigate to the end of it
                datFileStream.Seek(0, SeekOrigin.End);
                UInt32 offset = (UInt32)datFileStream.Length;

                FileInfo fInfo = new FileInfo(m_fileName);

                // .fsb, .fev, and .gfx files aren't compressed
                byte[] finalContent;
                if (fInfo.Extension == ".fsb" || fInfo.Extension == ".fev" || fInfo.Extension == ".gfx")
                {
                    finalContent = content;
                }
                else
                {
                    // Start of compression
                    MemoryStream       mStream = new MemoryStream();
                    zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                    oStream.Write(content, 0, content.Length);
                    oStream.finish();
                    finalContent = mStream.ToArray();
                }

                // Write the data to the end of the .dat file
                datFileStream.Write(finalContent, 0, finalContent.Length);

                // Update entry values to represent the new changes
                FileOffset = offset;
                FileSize   = (UInt32)finalContent.Length;

                return(true);
            }
            catch (Exception)
            {
                FileOffset = oldOffset;
                FileSize   = oldSize;

                return(false);
            }
        }
        public static byte[] DecompressZlib(byte[] data)
        {
            MemoryStream output = new MemoryStream();
            try
            {
                MemoryStream input = new MemoryStream(data);
                using (ZOutputStream zs = new ZOutputStream(output))
                {
                    CopyStream(input, zs);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return output.ToArray();
        }
Example #14
0
        /// <summary>
        /// Writes compressed data to the given stream.
        /// </summary>
        /// <param name="uncompressedStream">A stream where the compressed data should be written.</param>
        /// <param name="level">The Zlib compression level that should be used. Default is Z_BEST_COMPRESSION = 9.</param>
        public ZlibCompressedWriter(Stream uncompressedStream, int level = 9)
            : base(uncompressedStream)
        {
            /* Since we need to write the number of compressed bytes that we are going to send first,
             * We cannot directly write to the uncompressedStream.
             * We first write the compressed data to zMemoryStream, and after that we write the data from it to the uncompressedStream
             * using CompressedWriter.
             */

            zMemoryStream = new MemoryStream();
            zCompressStream = new ZOutputStream(zMemoryStream, level)
            {
                //The VNC Protocol uses Z_SYNC_FLUSH as a Flush Mode
                FlushMode = zlibConst.Z_SYNC_FLUSH
            };
            Level = level;
            compressedWriter = new BinaryWriter(uncompressedStream);
            bigWriter = new BigEndianBinaryWriter(uncompressedStream);
        }
        public async Task <Stream> DecompressAsync(Stream data, ZlibCompressionOptions options)
        {
            var _options   = GetOptions(options);
            var inStream   = data;
            var outStream  = new MemoryStream();
            var outZStream = new zlib.ZOutputStream(outStream);

            try
            {
                await copyStreamAsync(inStream, outZStream, _options.DecompressBufferSize);
            }
            finally
            {
                outZStream.Close();
                outStream.Close();
                inStream.Close();
            }

            return(outStream);
        }
        public Stream Compress(Stream data, ZlibCompressionOptions options)
        {
            var _options  = GetOptions(options);
            var outStream = new MemoryStream();
            var inStream  = data;

            var outZStream = new zlib.ZOutputStream(outStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION);

            try
            {
                copyStream(inStream, outZStream, _options.CompressBufferSize);
            }
            finally
            {
                outZStream.Close();
                outStream.Close();
                inStream.Close();
            }

            return(outStream);
        }
Example #17
0
        /// <summary>
        /// Compresses an array of bytes using the DEFLATE algorithm from zlib.
        /// </summary>
        /// <param name="data">The array of bytes to compress.</param>
        /// <param name="compression">The compression level to use when compressing the data. -1 for the default, 0 for none, and 1 to 9 for fastest/worst to slowest/best.</param>
        /// <returns>The compressed byte array.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
        public static byte[] Compress(byte[] data, int compression = zlibConst.Z_DEFAULT_COMPRESSION)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            // Create a memory stream to write our compressed data to
            using (MemoryStream memoryStream = new MemoryStream())
            {
                // Create a GZIP stream and write our byte array to compress it
                using (ZOutputStream gzip = new ZOutputStream(memoryStream, compression))
                {
                    gzip.Write(data, 0, data.Length);
                }

                // Read compressed bytes back
                byte[] compressed = memoryStream.ToArray();

                // Create a new array the same as the length of the compressed data plus 4 bytes for the size
                byte[] buffer = new byte[compressed.Length + sizeof(int)];

                // Copy the compressed bytes we got earlier into our new array at offset 4
                Buffer.BlockCopy(compressed, 0, buffer, sizeof(int), compressed.Length);

                // Get the length of the compressed data array, making sure to adjust for endianness
                byte[] length = BitConverter.GetBytes(data.Length);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(length);
                }

                // Copy the length of the compressed data array into the first 4 bytes our our new array
                Buffer.BlockCopy(length, 0, buffer, 0, sizeof(int));

                return buffer;
            }
        }
Example #18
0
 /// <summary>
 /// 解压文件
 /// </summary>
 /// <param name="CompressedFileName">被解压文件名(必须输入绝对路径)</param>
 /// <param name="DecompressFileName">解压后保存的文件名(必须输入绝对路径)</param>
 /// <returns></returns>
 public static bool DecompressFile(string CompressedFileName, string DecompressFileName)
 {
     bool bResult = false;
     FileStream outFileStream = new FileStream(DecompressFileName, FileMode.Create);
     ZOutputStream outZStream = new ZOutputStream(outFileStream);
     FileStream inFileStream = new FileStream(CompressedFileName, FileMode.Open);
     try
     {
         CopyStream(inFileStream, outZStream);
         bResult = true;
     }
     catch
     {
         bResult = false;
     }
     finally
     {
         outZStream.Close();
         outFileStream.Close();
         inFileStream.Close();
     }
     return bResult;
 }
        public static OSD ZCompressOSD(OSD inOsd, bool useHeader)
        {
            OSD osd = null;

            using (MemoryStream msSinkCompressed = new MemoryStream())
            {
                using (ZOutputStream zOut = new ZOutputStream(msSinkCompressed,1))
                {
                    CopyStream(new MemoryStream(OSDParser.SerializeLLSDBinary(inOsd, useHeader)), zOut);
                    msSinkCompressed.Seek(0L, SeekOrigin.Begin);
                    osd = OSD.FromBinary(msSinkCompressed.ToArray());
                    zOut.Close();
                }

            }

            return osd;
        }
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            Console.WriteLine("**********************************************************************");
            Console.WriteLine("**                                                                  **");
            Console.WriteLine("**  AO Item and Nano Extractor/Serializer v0.85beta                  **");
            Console.WriteLine("**                                                                  **");
            Console.WriteLine("**********************************************************************");

            Console.WriteLine();

            string AOPath = string.Empty;
            bool foundAO = false;
            Console.WriteLine("Enter exit to close program");
            while (!foundAO)
            {
                if (File.Exists("config.txt"))
                {
                    TextReader tr = new StreamReader("config.txt");
                    AOPath = tr.ReadLine();
                    tr.Close();
                }

                foundAO = false;
                Console.Write("Please enter your AO Install Path [" + AOPath + "]:");
                string temp = Console.ReadLine();
                if (temp != string.Empty)
                {
                    AOPath = temp;
                }

                if (temp.ToLower() == "exit")
                {
                    return;
                }

                if (!Directory.Exists(AOPath))
                {
                    continue;
                }

                try
                {
                    extractor = new Extractor(AOPath);
                    TextWriter tw2 = new StreamWriter("config.txt", false, Encoding.GetEncoding("windows-1252"));
                    tw2.WriteLine(AOPath);
                    tw2.Close();
                    foundAO = true;
                    Console.WriteLine("Found AO Database on " + AOPath);
                }
                catch (Exception)
                {
                    foundAO = false;
                }

                // Try to add cd_image\data\db
                if (!foundAO)
                {
                    try
                    {
                        AOPath = Path.Combine(AOPath, "cd_image\\data\\db");
                        extractor = new Extractor(AOPath);
                        TextWriter tw2 = new StreamWriter("config.txt", false, Encoding.GetEncoding("windows-1252"));
                        tw2.WriteLine(AOPath);
                        tw2.Close();
                        foundAO = true;
                        Console.WriteLine("Found AO Database on " + AOPath);
                    }
                    catch (Exception)
                    {
                        foundAO = false;
                    }
                }
            }

            TextWriter tw = new StreamWriter("itemnames.sql", false, Encoding.GetEncoding("windows-1252"));
            tw.WriteLine("DROP TABLE IF EXISTS `itemnames`;");
            tw.WriteLine("CREATE TABLE `itemnames` (");
            tw.WriteLine("  `AOID` int(10) NOT NULL,");
            tw.WriteLine("  `Name` varchar(250) NOT NULL,");
            tw.WriteLine("  PRIMARY KEY (`AOID`)");
            tw.WriteLine(") ENGINE=MyIsam DEFAULT CHARSET=latin1;");
            tw.WriteLine();
            tw.Close();

            Console.WriteLine("Number of Items to extract: " + extractor.GetRecordInstances(0xF4254).Length);

            // ITEM RECORD TYPE
            Console.WriteLine("Number of Nanos to extract: " + extractor.GetRecordInstances(0xFDE85).Length);

            // NANO RECORD TYPE

            // Console.WriteLine(extractor.GetRecordInstances(0xF4241).Length); // Playfields
            // Console.WriteLine(extractor.GetRecordInstances(0xF4266).Length); // Nano Strains
            // Console.WriteLine(extractor.GetRecordInstances(0xF4264).Length); // Perks

            // GetData(@"D:\c#\extractor serializer\data\items\",0xf4254);
            // GetData(@"D:\c#\extractor serializer\data\nanos\",0xfde85);
            // GetData(@"D:\c#\extractor serializer\data\playfields\",0xf4241);
            // GetData(@"D:\c#\extractor serializer\data\nanostrains\",0xf4266);
            // GetData(@"D:\c#\extractor serializer\data\perks\",0xf4264);
            var np = new NewParser();
            var rawItemList = new List<AOItem>();
            var rawNanoList = new List<AONanos>();
            foreach (int recnum in extractor.GetRecordInstances(0xFDE85))
            {
                rawNanoList.Add(np.ParseNano(0xFDE85, recnum, extractor.GetRecordData(0xFDE85, recnum), "temp.sql"));
            }

            File.Delete("temp.sql");
            Console.WriteLine();
            Console.WriteLine("Nanos extracted: " + rawNanoList.Count);

            List<string> ItemNamesSql = new List<string>();

            foreach (int recnum in extractor.GetRecordInstances(0xF4254))
            {
                rawItemList.Add(np.ParseItem(0xF4254, recnum, extractor.GetRecordData(0xF4254, recnum), ItemNamesSql));
            }

            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("Compacting itemnames.sql");
            TextWriter itnsql = new StreamWriter("itemnames.sql", true, Encoding.GetEncoding("windows-1252"));
            while (ItemNamesSql.Count > 0)
            {
                int count = 0;
                string toWrite = string.Empty;
                while ((count < 20) && (ItemNamesSql.Count > 0))
                {
                    if (toWrite.Length > 0)
                    {
                        toWrite += ",";
                    }

                    toWrite += ItemNamesSql[0];
                    ItemNamesSql.RemoveAt(0);
                    count++;
                }

                if (toWrite != string.Empty)
                {
                    itnsql.WriteLine("INSERT INTO itemnames VALUES " + toWrite + ";");
                }
            }

            itnsql.Close();

            // SerializationContext.Default.Serializers.Register(new AOFunctionArgumentsSerializer());
            Console.WriteLine();
            Console.WriteLine("Items extracted: " + rawItemList.Count);

            Console.WriteLine();
            Console.WriteLine("Creating serialized nano data file - please wait");
            Stream sf = new FileStream("nanos.dat", FileMode.Create);

            var ds = new ZOutputStream(sf, zlibConst.Z_BEST_COMPRESSION);
            var sm = new MemoryStream();
            MessagePackSerializer<List<AONanos>> bf = MessagePackSerializer.Create<List<AONanos>>();

            var nanoList2 = new List<AONanos>();

            int maxnum = 5000;
            byte[] buffer = BitConverter.GetBytes(maxnum);
            sm.Write(buffer, 0, buffer.Length);

            foreach (AONanos nanos in rawNanoList)
            {
                nanoList2.Add(nanos);
                if (nanoList2.Count == maxnum)
                {
                    bf.Pack(sm, nanoList2);
                    sm.Flush();
                    nanoList2.Clear();
                }
            }

            bf.Pack(sm, nanoList2);
            sm.Seek(0, SeekOrigin.Begin);
            CopyStream(sm, ds);
            sm.Close();
            ds.Close();

            Console.WriteLine();
            Console.WriteLine("Checking Nanos...");
            Console.WriteLine();
            NanoHandler.CacheAllNanos("nanos.dat");
            Console.WriteLine();
            Console.WriteLine("Nanos: " + NanoHandler.NanoList.Count + " successfully converted");

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Creating serialized item data file - please wait");

            sf = new FileStream("items.dat", FileMode.Create);

            ds = new ZOutputStream(sf, zlibConst.Z_BEST_COMPRESSION);
            sm = new MemoryStream();
            MessagePackSerializer<List<AOItem>> bf2 = MessagePackSerializer.Create<List<AOItem>>();

            List<AOItem> items = new List<AOItem>();

            maxnum = 5000;
            buffer = BitConverter.GetBytes(maxnum);
            sm.Write(buffer, 0, buffer.Length);

            foreach (AOItem it in rawItemList)
            {
                items.Add(it);
                if (items.Count == maxnum)
                {
                    bf2.Pack(sm, items);
                    sm.Flush();
                    items.Clear();
                }
            }

            bf2.Pack(sm, items);
            sm.Seek(0, SeekOrigin.Begin);
            CopyStream(sm, ds);
            sm.Close();
            ds.Close();

            Console.WriteLine();
            Console.WriteLine("Checking Items...");
            Console.WriteLine();

            ItemHandler.CacheAllItems("items.dat");

            Console.WriteLine("Items: " + ItemHandler.ItemList.Count + " successfully converted");

            Console.WriteLine();
            Console.WriteLine("Further Instructions:");
            Console.WriteLine("- Copy items.dat and nanos.dat into your CellAO folder and overwrite.");
            Console.WriteLine("- Apply itemnames.sql to your database");
            Console.WriteLine("Press Enter to exit and have fun with CellAO");
            Console.ReadLine();
        }
Example #21
0
        //Inserts a file into the raf archive...
        public bool InsertFile(string fileName, byte[] content, TextWriter ostream)
        {
            if (ostream == null)
                ostream = new StreamWriter(Stream.Null);

            ostream.WriteLine("    Insert: " + fileName);
            ostream.WriteLine("        To: " + GetID());

            RAFFileListEntry fileentry = this.GetDirectoryFile().GetFileList().GetFileEntry(fileName);
            if (fileentry == null)
            {
                //Define a new entry
                ostream.WriteLine("# Create new entry in RAF Archive, using experimental hash calculations");
                ostream.WriteLine("    Get DAT File Stream");
                FileStream datFileStream = GetDataFileContentStream();
                //navigate to the end of it, add the file.
                datFileStream.Seek(0, SeekOrigin.End);
                UInt32 offset = (UInt32)datFileStream.Length;

                byte[] finalContent;
                if (nocompress.Contains(fileName))
                {
                    finalContent = content;
                }
                else
                {
                    ostream.WriteLine("    Begin compression of content");
                    MemoryStream mStream = new MemoryStream();
                    zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                    oStream.Write(content, 0, content.Length);
                    oStream.finish();
                    finalContent = mStream.ToArray();
                }

                ostream.WriteLine("    Begin DAT Write");
                datFileStream.Write(finalContent, 0, finalContent.Length);

                ostream.WriteLine("    Add file entry to in memory directory...");
                UInt32 strTableIndex = directoryFile.GetStringTable().Add(fileName);
                directoryFile.CreateFileEntry(fileName, offset, (UInt32)finalContent.Length, strTableIndex);
                //directoryFile.Save();
                //datFileStream.Close();
                ostream.WriteLine("    Done.");
                return true;

            }
            else
            {
                //store the old offsets just in case we need to perform a restore.
                //This actually isn't necessary currently, since the raf directory file is saved
                //After packing.
                UInt32 oldOffset = (UInt32)fileentry.FileOffset;
                UInt32 oldSize = (UInt32)fileentry.FileSize;
                ostream.WriteLine("Old Offset: " + oldOffset + "; Old Size: " + oldSize);

                ostream.WriteLine("Begin modify game files...  This may take a while");
                try
                {
                    ostream.WriteLine("    Get File Stream");
                    FileStream datFileStream = GetDataFileContentStream();
                    //navigate to the end of it, add the file.
                    datFileStream.Seek(0, SeekOrigin.End);
                    UInt32 offset = (UInt32)datFileStream.Length;

                    byte[] finalContent;
                    if (nocompress.Contains(fileName))
                    {
                        finalContent = content;
                    }
                    else
                    {
                        ostream.WriteLine("    Begin compression of content");
                        MemoryStream mStream = new MemoryStream();
                        zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                        oStream.Write(content, 0, content.Length);
                        oStream.finish();
                        finalContent = mStream.ToArray();
                    }

                    ostream.WriteLine("    Begin DAT Write");
                    datFileStream.Write(finalContent, 0, finalContent.Length);

                    ostream.WriteLine("    Begin FileEntry Write");
                    fileentry.FileOffset = offset;
                    fileentry.FileSize = (UInt32)finalContent.Length;
                    //directoryFile.Save();

                    //datFileStream.Close();
                    ostream.WriteLine("    Done.");
                    return true;
                }
                catch (Exception e)
                {
                    ostream.WriteLine("!! An error occurred in inserting a file.");
                    ostream.WriteLine("!! Note that the content of the raf archive has been added to");
                    ostream.WriteLine("!! But the directory file that points the game to the data in the content file");
                    ostream.WriteLine("!! Has not been modified.  In other words, if the game read the RAF archive now");
                    ostream.WriteLine("!! It wouldn't see a difference.");
                    ostream.WriteLine("!! However, the RAF archive is a bit bigger than before.");
                    ostream.WriteLine(e.ToString());

                    fileentry.FileOffset = oldOffset;
                    fileentry.FileSize = oldSize;
                    //directoryFile.GetContent();
                    return false;
                }
            }
        }
Example #22
0
        /// <summary>
        /// Generate the co-ords and faces necessary to construct a mesh from the mesh data the accompanies a prim.
        /// </summary>
        /// <param name="primName"></param>
        /// <param name="primShape"></param>
        /// <param name="size"></param>
        /// <param name="coords">Coords are added to this list by the method.</param>
        /// <param name="faces">Faces are added to this list by the method.</param>
        /// <returns>true if coords and faces were successfully generated, false if not</returns>
        private bool GenerateCoordsAndFacesFromPrimMeshData(
            string primName, PrimitiveBaseShape primShape, Vector3 size, out List<Coord> coords, out List<Face> faces)
        {
            m_log.DebugFormat("[MESH]: experimental mesh proxy generation for {0}", primName);

            coords = new List<Coord>();
            faces = new List<Face>();
            OSD meshOsd = null;

            if (primShape.SculptData.Length <= 0)
            {
                m_log.Error("[MESH]: asset data is zero length");
                return false;
            }

            long start = 0;
            using (MemoryStream data = new MemoryStream(primShape.SculptData))
            {
                try
                {
                    OSD osd = OSDParser.DeserializeLLSDBinary(data);
                    if (osd is OSDMap)
                        meshOsd = (OSDMap)osd;
                    else
                    {
                        m_log.Warn("[Mesh}: unable to cast mesh asset to OSDMap");
                        return false;
                    }
                }
                catch (Exception e)
                {
                    m_log.Error("[MESH]: Exception deserializing mesh asset header:" + e.ToString());
                }

                start = data.Position;
            }

            if (meshOsd is OSDMap)
            {
                OSDMap physicsParms = null;
                OSDMap map = (OSDMap)meshOsd;
                if (map.ContainsKey("physics_shape"))
                    physicsParms = (OSDMap)map["physics_shape"]; // old asset format
                else if (map.ContainsKey("physics_mesh"))
                    physicsParms = (OSDMap)map["physics_mesh"]; // new asset format

                if (physicsParms == null)
                {
                    m_log.Warn("[MESH]: no recognized physics mesh found in mesh asset");
                    return false;
                }

                int physOffset = physicsParms["offset"].AsInteger() + (int)start;
                int physSize = physicsParms["size"].AsInteger();

                if (physOffset < 0 || physSize == 0)
                    return false; // no mesh data in asset

                OSD decodedMeshOsd = new OSD();
                byte[] meshBytes = new byte[physSize];
                System.Buffer.BlockCopy(primShape.SculptData, physOffset, meshBytes, 0, physSize);
//                        byte[] decompressed = new byte[physSize * 5];
                try
                {
                    using (MemoryStream inMs = new MemoryStream(meshBytes))
                    {
                        using (MemoryStream outMs = new MemoryStream())
                        {
                            using (ZOutputStream zOut = new ZOutputStream(outMs))
                            {
                                byte[] readBuffer = new byte[2048];
                                int readLen = 0;
                                while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
                                {
                                    zOut.Write(readBuffer, 0, readLen);
                                }
                                zOut.Flush();
                                outMs.Seek(0, SeekOrigin.Begin);

                                byte[] decompressedBuf = outMs.GetBuffer();

                                decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    m_log.Error("[MESH]: exception decoding physical mesh: " + e.ToString());
                    return false;
                }

                OSDArray decodedMeshOsdArray = null;

                // physics_shape is an array of OSDMaps, one for each submesh
                if (decodedMeshOsd is OSDArray)
                {
//                            Console.WriteLine("decodedMeshOsd for {0} - {1}", primName, Util.GetFormattedXml(decodedMeshOsd));

                    decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
                    foreach (OSD subMeshOsd in decodedMeshOsdArray)
                    {
                        if (subMeshOsd is OSDMap)
                            AddSubMesh(subMeshOsd as OSDMap, size, coords, faces);
                    }
                }
            }

            return true;
        }
Example #23
0
        private string deflate()
        {
            int totalSize = 0;

            foreach (DSMCCDownloadDataBlock block in blocks)
                totalSize += block.DataSize;

            byte[] moduleData = new byte[totalSize];
            int dataIndex = 0;

            foreach (DSMCCDownloadDataBlock block in blocks)
            {
                for (int inputIndex = 0; inputIndex < block.DataSize; inputIndex++)
                {
                    moduleData[dataIndex] = block.Data[inputIndex];
                    dataIndex++;
                }
            }

            try
            {
                MemoryStream output = new MemoryStream();
                ZOutputStream zstream = new ZOutputStream(output);

                data = new byte[originalSize];
                zstream.Write(moduleData, 0, moduleData.Length);
                zstream.Flush();
                output.Seek(0, SeekOrigin.Begin);
                int readCount = output.Read(data, 0, data.Length);

                return (null);
            }
            catch (Exception e)
            {
                return (e.Message);
            }
        }
Example #24
0
 /// <summary>
 /// 压缩流
 /// </summary>
 /// <param name="SourceStream">需要被压缩的流数据</param>
 /// <returns></returns>
 public static Stream CompressStream(Stream SourceStream)
 {
     try
     {
         MemoryStream stmOutTemp = new MemoryStream();
         ZOutputStream outZStream = new ZOutputStream(stmOutTemp, zlibConst.Z_DEFAULT_COMPRESSION);
         CopyStream(SourceStream, outZStream);
         outZStream.finish();
         return stmOutTemp;
     }
     catch
     {
         return null;
     }
 }
Example #25
0
        /// <summary>
        /// 解压字符串
        /// </summary>
        /// <param name="SourceString">需要被解压的字符串</param>
        /// <returns></returns>
        public static string DecompressString(string SourceString)
        {
            byte[] byteSource = Convert.FromBase64String(SourceString);

            MemoryStream stmInput = new MemoryStream(byteSource);
            ZOutputStream stmOutPut = new ZOutputStream(stmInput);
            byte[] bytOutPut = new byte[stmOutPut.Length];
            stmOutPut.Position = 0;
            stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);

            if (bytOutPut != null)
            {
                return System.Text.Encoding.UTF8.GetString(bytOutPut);
            }
            else
            {
                return null;
            }
        }
Example #26
0
 /// <summary>
 /// 解压流
 /// </summary>
 /// <param name="SourceStream">需要被解缩的流数据</param>
 /// <returns></returns>
 public static Stream DecompressStream(Stream SourceStream)
 {
     try
     {
         MemoryStream stmOutput = new MemoryStream();
         ZOutputStream outZStream = new ZOutputStream(stmOutput);
         CopyStream(SourceStream, outZStream);
         outZStream.finish();
         return stmOutput;
     }
     catch
     {
         return null;
     }
 }
 public void Deserialize(OSDMap map)
 {
     try
     {
         using (MemoryStream input = new MemoryStream(map["Zipped"].AsBinary()))
         {
             using (MemoryStream output = new MemoryStream())
             {
                 using (ZOutputStream zout = new ZOutputStream(output))
                 {
                     byte[] buffer = new byte[2048];
                     int len;
                     while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                     {
                         zout.Write(buffer, 0, len);
                     }
                     zout.Flush();
                     output.Seek(0, SeekOrigin.Begin);
                     MaterialData = OSDParser.DeserializeLLSDBinary(output);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Log("Failed to decode RenderMaterials message:", Helpers.LogLevel.Warning, ex);
         MaterialData = new OSDMap();
     }
 }
Example #28
0
        private Mesh CreateMeshFromPrimMesher(string primName, PrimitiveBaseShape primShape, Vector3 size, float lod)
        {
//            m_log.DebugFormat(
//                "[MESH]: Creating physics proxy for {0}, shape {1}",
//                primName, (OpenMetaverse.SculptType)primShape.SculptType);

            PrimMesh primMesh;
            PrimMesher.SculptMesh sculptMesh;

            List<Coord> coords = new List<Coord>();
            List<Face> faces = new List<Face>();

            Image idata = null;
            string decodedSculptFileName = "";

            if (primShape.SculptEntry)
            {
                if (((OpenMetaverse.SculptType)primShape.SculptType) == SculptType.Mesh)
                {
                    if (!useMeshiesPhysicsMesh)
                        return null;

                    m_log.DebugFormat("[MESH]: experimental mesh proxy generation for {0}", primName);

                    OSD meshOsd = null;

                    if (primShape.SculptData.Length <= 0)
                    {
                        m_log.Error("[MESH]: asset data is zero length");
                        return null;
                    }

                    long start = 0;
                    using (MemoryStream data = new MemoryStream(primShape.SculptData))
                    {
                        try
                        {
                            OSD osd = OSDParser.DeserializeLLSDBinary(data);
                            if (osd is OSDMap)
                                meshOsd = (OSDMap)osd;
                            else
                            {
                                m_log.Warn("[Mesh}: unable to cast mesh asset to OSDMap");
                                return null;
                            }
                        }
                        catch (Exception e)
                        {
                            m_log.Error("[MESH]: Exception deserializing mesh asset header:" + e.ToString());
                        }

                        start = data.Position;
                    }

                    if (meshOsd is OSDMap)
                    {
                        OSDMap physicsParms = null;
                        OSDMap map = (OSDMap)meshOsd;
                        if (map.ContainsKey("physics_shape"))
                            physicsParms = (OSDMap)map["physics_shape"]; // old asset format
                        else if (map.ContainsKey("physics_mesh"))
                            physicsParms = (OSDMap)map["physics_mesh"]; // new asset format

                        if (physicsParms == null)
                        {
                            m_log.Warn("[MESH]: no recognized physics mesh found in mesh asset");
                            return null;
                        }

                        int physOffset = physicsParms["offset"].AsInteger() + (int)start;
                        int physSize = physicsParms["size"].AsInteger();

                        if (physOffset < 0 || physSize == 0)
                            return null; // no mesh data in asset

                        OSD decodedMeshOsd = new OSD();
                        byte[] meshBytes = new byte[physSize];
                        System.Buffer.BlockCopy(primShape.SculptData, physOffset, meshBytes, 0, physSize);
//                        byte[] decompressed = new byte[physSize * 5];
                        try
                        {
                            using (MemoryStream inMs = new MemoryStream(meshBytes))
                            {
                                using (MemoryStream outMs = new MemoryStream())
                                {
                                    using (ZOutputStream zOut = new ZOutputStream(outMs))
                                    {
                                        byte[] readBuffer = new byte[2048];
                                        int readLen = 0;
                                        while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
                                        {
                                            zOut.Write(readBuffer, 0, readLen);
                                        }
                                        zOut.Flush();
                                        outMs.Seek(0, SeekOrigin.Begin);

                                        byte[] decompressedBuf = outMs.GetBuffer();

                                        decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            m_log.Error("[MESH]: exception decoding physical mesh: " + e.ToString());
                            return null;
                        }

                        OSDArray decodedMeshOsdArray = null;

                        // physics_shape is an array of OSDMaps, one for each submesh
                        if (decodedMeshOsd is OSDArray)
                        {
//                            Console.WriteLine("decodedMeshOsd for {0} - {1}", primName, Util.GetFormattedXml(decodedMeshOsd));

                            decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
                            foreach (OSD subMeshOsd in decodedMeshOsdArray)
                            {
                                if (subMeshOsd is OSDMap)
                                    AddSubMesh(subMeshOsd as OSDMap, size, coords, faces);
                            }
                        }
                    }
                }
                else
                {
                    if (cacheSculptMaps && primShape.SculptTexture != UUID.Zero)
                    {
                        decodedSculptFileName = System.IO.Path.Combine(decodedSculptMapPath, "smap_" + primShape.SculptTexture.ToString());
                        try
                        {
                            if (File.Exists(decodedSculptFileName))
                            {
                                idata = Image.FromFile(decodedSculptFileName);
                            }
                        }
                        catch (Exception e)
                        {
                            m_log.Error("[SCULPT]: unable to load cached sculpt map " + decodedSculptFileName + " " + e.Message);

                        }
                        //if (idata != null)
                        //    m_log.Debug("[SCULPT]: loaded cached map asset for map ID: " + primShape.SculptTexture.ToString());
                    }

                    if (idata == null)
                    {
                        if (primShape.SculptData == null || primShape.SculptData.Length == 0)
                            return null;

                        try
                        {
                            OpenMetaverse.Imaging.ManagedImage unusedData;
                            OpenMetaverse.Imaging.OpenJPEG.DecodeToImage(primShape.SculptData, out unusedData, out idata);
                            unusedData = null;

                            //idata = CSJ2K.J2kImage.FromBytes(primShape.SculptData);

                            if (cacheSculptMaps && idata != null)
                            {
                                try { idata.Save(decodedSculptFileName, ImageFormat.MemoryBmp); }
                                catch (Exception e) { m_log.Error("[SCULPT]: unable to cache sculpt map " + decodedSculptFileName + " " + e.Message); }
                            }
                        }
                        catch (DllNotFoundException)
                        {
                            m_log.Error("[PHYSICS]: OpenJpeg is not installed correctly on this system. Physics Proxy generation failed.  Often times this is because of an old version of GLIBC.  You must have version 2.4 or above!");
                            return null;
                        }
                        catch (IndexOutOfRangeException)
                        {
                            m_log.Error("[PHYSICS]: OpenJpeg was unable to decode this. Physics Proxy generation failed");
                            return null;
                        }
                        catch (Exception ex)
                        {
                            m_log.Error("[PHYSICS]: Unable to generate a Sculpty physics proxy. Sculpty texture decode failed: " + ex.Message);
                            return null;
                        }
                    }

                    PrimMesher.SculptMesh.SculptType sculptType;
                    switch ((OpenMetaverse.SculptType)primShape.SculptType)
                    {
                        case OpenMetaverse.SculptType.Cylinder:
                            sculptType = PrimMesher.SculptMesh.SculptType.cylinder;
                            break;
                        case OpenMetaverse.SculptType.Plane:
                            sculptType = PrimMesher.SculptMesh.SculptType.plane;
                            break;
                        case OpenMetaverse.SculptType.Torus:
                            sculptType = PrimMesher.SculptMesh.SculptType.torus;
                            break;
                        case OpenMetaverse.SculptType.Sphere:
                            sculptType = PrimMesher.SculptMesh.SculptType.sphere;
                            break;
                        default:
                            sculptType = PrimMesher.SculptMesh.SculptType.plane;
                            break;
                    }

                    bool mirror = ((primShape.SculptType & 128) != 0);
                    bool invert = ((primShape.SculptType & 64) != 0);

                    sculptMesh = new PrimMesher.SculptMesh((Bitmap)idata, sculptType, (int)lod, false, mirror, invert);
                    
                    idata.Dispose();

                    sculptMesh.DumpRaw(baseDir, primName, "primMesh");

                    sculptMesh.Scale(size.X, size.Y, size.Z);

                    coords = sculptMesh.coords;
                    faces = sculptMesh.faces;
                }
            }
            else
            {
                float pathShearX = primShape.PathShearX < 128 ? (float)primShape.PathShearX * 0.01f : (float)(primShape.PathShearX - 256) * 0.01f;
                float pathShearY = primShape.PathShearY < 128 ? (float)primShape.PathShearY * 0.01f : (float)(primShape.PathShearY - 256) * 0.01f;
                float pathBegin = (float)primShape.PathBegin * 2.0e-5f;
                float pathEnd = 1.0f - (float)primShape.PathEnd * 2.0e-5f;
                float pathScaleX = (float)(primShape.PathScaleX - 100) * 0.01f;
                float pathScaleY = (float)(primShape.PathScaleY - 100) * 0.01f;

                float profileBegin = (float)primShape.ProfileBegin * 2.0e-5f;
                float profileEnd = 1.0f - (float)primShape.ProfileEnd * 2.0e-5f;
                float profileHollow = (float)primShape.ProfileHollow * 2.0e-5f;
                if (profileHollow > 0.95f)
                    profileHollow = 0.95f;

                int sides = 4;
                if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
                    sides = 3;
                else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
                    sides = 24;
                else if ((primShape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
                { // half circle, prim is a sphere
                    sides = 24;

                    profileBegin = 0.5f * profileBegin + 0.5f;
                    profileEnd = 0.5f * profileEnd + 0.5f;
                }

                int hollowSides = sides;
                if (primShape.HollowShape == HollowShape.Circle)
                    hollowSides = 24;
                else if (primShape.HollowShape == HollowShape.Square)
                    hollowSides = 4;
                else if (primShape.HollowShape == HollowShape.Triangle)
                    hollowSides = 3;

                primMesh = new PrimMesh(sides, profileBegin, profileEnd, profileHollow, hollowSides);

                if (primMesh.errorMessage != null)
                    if (primMesh.errorMessage.Length > 0)
                        m_log.Error("[ERROR] " + primMesh.errorMessage);

                primMesh.topShearX = pathShearX;
                primMesh.topShearY = pathShearY;
                primMesh.pathCutBegin = pathBegin;
                primMesh.pathCutEnd = pathEnd;

                if (primShape.PathCurve == (byte)Extrusion.Straight || primShape.PathCurve == (byte) Extrusion.Flexible)
                {
                    primMesh.twistBegin = primShape.PathTwistBegin * 18 / 10;
                    primMesh.twistEnd = primShape.PathTwist * 18 / 10;
                    primMesh.taperX = pathScaleX;
                    primMesh.taperY = pathScaleY;

                    if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
                    {
                        ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
                        if (profileBegin < 0.0f) profileBegin = 0.0f;
                        if (profileEnd > 1.0f) profileEnd = 1.0f;
                    }
#if SPAM
                m_log.Debug("****** PrimMesh Parameters (Linear) ******\n" + primMesh.ParamsToDisplayString());
#endif
                    try
                    {
                        primMesh.ExtrudeLinear();
                    }
                    catch (Exception ex)
                    {
                        ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
                        return null;
                    }
                }
                else
                {
                    primMesh.holeSizeX = (200 - primShape.PathScaleX) * 0.01f;
                    primMesh.holeSizeY = (200 - primShape.PathScaleY) * 0.01f;
                    primMesh.radius = 0.01f * primShape.PathRadiusOffset;
                    primMesh.revolutions = 1.0f + 0.015f * primShape.PathRevolutions;
                    primMesh.skew = 0.01f * primShape.PathSkew;
                    primMesh.twistBegin = primShape.PathTwistBegin * 36 / 10;
                    primMesh.twistEnd = primShape.PathTwist * 36 / 10;
                    primMesh.taperX = primShape.PathTaperX * 0.01f;
                    primMesh.taperY = primShape.PathTaperY * 0.01f;

                    if (profileBegin < 0.0f || profileBegin >= profileEnd || profileEnd > 1.0f)
                    {
                        ReportPrimError("*** CORRUPT PRIM!! ***", primName, primMesh);
                        if (profileBegin < 0.0f) profileBegin = 0.0f;
                        if (profileEnd > 1.0f) profileEnd = 1.0f;
                    }
#if SPAM
                m_log.Debug("****** PrimMesh Parameters (Circular) ******\n" + primMesh.ParamsToDisplayString());
#endif
                    try
                    {
                        primMesh.ExtrudeCircular();
                    }
                    catch (Exception ex)
                    {
                        ReportPrimError("Extrusion failure: exception: " + ex.ToString(), primName, primMesh);
                        return null;
                    }
                }

                primMesh.DumpRaw(baseDir, primName, "primMesh");

                primMesh.Scale(size.X, size.Y, size.Z);

                coords = primMesh.coords;
                faces = primMesh.faces;
            }

            // Remove the reference to any JPEG2000 sculpt data so it can be GCed
            primShape.SculptData = Utils.EmptyBytes;

            int numCoords = coords.Count;
            int numFaces = faces.Count;

            // Create the list of vertices
            List<Vertex> vertices = new List<Vertex>();
            for (int i = 0; i < numCoords; i++)
            {
                Coord c = coords[i];
                vertices.Add(new Vertex(c.X, c.Y, c.Z));
            }

            Mesh mesh = new Mesh();
            // Add the corresponding triangles to the mesh
            for (int i = 0; i < numFaces; i++)
            {
                Face f = faces[i];
                mesh.Add(new Triangle(vertices[f.v1], vertices[f.v2], vertices[f.v3]));
            }

            return mesh;
        }
Example #29
0
        public static void Convertitems()
        {

            SqlWrapper sql = new SqlWrapper();
            DataTable dt_items = sql.ReadDatatable("SELECT * FROM Items order by aoid asc");

            Stream sf = new FileStream("items.dat", FileMode.Create);

            ZOutputStream ds = new ZOutputStream(sf, zlibConst.Z_BEST_COMPRESSION);
            MemoryStream sm = new MemoryStream();

            //DeflateStream sm = new DeflateStream(sf, CompressionMode.Compress);



            BinaryFormatter bf = new BinaryFormatter();

            Console.WriteLine("Processing data... This will take a while!");
            List<AOItem> ITEMS = new List<AOItem>();
            int count = dt_items.Rows.Count;
            int oldperc = 0;
            int countall = 0;
            byte[] buffer = BitConverter.GetBytes(maxnum);
            sm.Write(buffer, 0, buffer.Length);
            if (File.Exists("items2.dat"))
            {
                ItemHandler.CacheAllItems("items2.dat");
                foreach (AOItem aoi in ItemHandler.ItemList)
                {
                    ITEMS.Add(aoi);
                    if (ITEMS.Count == maxnum)
                    {
                        bf.Serialize(sm, ITEMS);
                        sm.Flush();
                        ITEMS.Clear();
                        countall += maxnum;
                    }
                }
            }
            else
            {
                foreach (DataRow itemrow in dt_items.Rows)
                {
                    AOItem _item = new AOItem();
                    _item.LowID = (Int32)itemrow[0];
                    _item.HighID = (Int32)itemrow[0];
                    _item.Quality = (Int32)itemrow[2];
                    _item.ItemType = (Int32)itemrow[3];

                    DataTable dt_itemevents = sql.ReadDatatable("SELECT * FROM item_events WHERE itemid=" + _item.LowID + " ORDER BY eventid asc");

                    foreach (DataRow eventrow in dt_itemevents.Rows)
                    {
                        AOEvents aoe = new AOEvents();
                        aoe.EventType = (Int32)eventrow[2];
                        int eventid = (Int32)eventrow["eventid"];

                        DataTable dt_itemeventfunctions = sql.ReadDatatable("SELECT * FROM item_functions WHERE itemid=" + _item.LowID + " AND eventid=" + eventid + " ORDER BY functionid asc");

                        foreach (DataRow eventfunctionrow in dt_itemeventfunctions.Rows)
                        {
                            int eventfuncid = (Int32)eventfunctionrow["functionid"];
                            AOFunctions aof = new AOFunctions();
                            aof.FunctionType = (Int32)eventfunctionrow[3];
                            aof.Target = (Int32)eventfunctionrow[4];
                            aof.TickCount = (Int32)eventfunctionrow[5];
                            aof.TickInterval = (uint)(Int32)eventfunctionrow[6];

                            DataTable functionargs = sql.ReadDatatable("SELECT * FROM item_function_arguments WHERE functionid=" + eventfuncid + " AND eventid=" + eventid + " AND itemid=" + _item.LowID + " ORDER BY attrid asc");

                            foreach (DataRow attrs in functionargs.Rows)
                            {
                                if (!(attrs["argvalint"] is DBNull))
                                {
                                    aof.Arguments.Add((Int32)attrs["argvalint"]);
                                }
                                else
                                    if (!(attrs["argvalsingle"] is DBNull))
                                    {
                                        aof.Arguments.Add((Single)(float)attrs["argvalsingle"]);
                                    }
                                    else
                                        if (!(attrs["argvalstring"] is DBNull))
                                        {
                                            string s = attrs["argvalstring"].ToString();
                                            aof.Arguments.Add(s);
                                        }
                                        else
                                            throw (new NotSupportedException("No Argument value given, all NULL: " + _item.LowID));
                            }

                            DataTable reqs = sql.ReadDatatable("SELECT * from  item_function_reqs WHERE functionid=" + eventfuncid + " AND eventid=" + eventid + " AND itemid=" + _item.LowID + " ORDER BY reqid asc");

                            foreach (DataRow rrow in reqs.Rows)
                            {
                                AORequirements aor = new AORequirements();
                                aor.Statnumber = (Int32)rrow["attrnum"];
                                aor.Value = (Int32)rrow["attrval"];
                                aor.Target = (Int32)rrow["target"];
                                aor.Operator = (Int32)rrow["operator"];
                                aor.ChildOperator = (Int32)rrow["child_op"];
                                aof.Requirements.Add(aor);
                            }

                            aoe.Functions.Add(aof);
                        }

                        _item.Events.Add(aoe);
                    }

                    DataTable dt_actions = sql.ReadDatatable("SELECT * FROM item_actions WHERE itemid=" + _item.LowID);

                    foreach (DataRow acrow in dt_actions.Rows)
                    {
                        AOActions aoa = new AOActions();
                        aoa.ActionType = (Int32)acrow["actionnum"];

                        DataTable reqs = sql.ReadDatatable("SELECT * FROM item_action_reqs WHERE itemid=" + _item.LowID + " AND actionid=" + ((Int32)acrow["actionid"]) + " ORDER BY reqid ASC");

                        foreach (DataRow rrow in reqs.Rows)
                        {
                            AORequirements aor = new AORequirements();
                            aor.Statnumber = (Int32)rrow["attrnum"];
                            aor.Value = (Int32)rrow["attrval"];
                            aor.Target = (Int32)rrow["target"];
                            aor.Operator = (Int32)rrow["operator"];
                            aor.ChildOperator = (Int32)rrow["child_op"];
                            aoa.Requirements.Add(aor);
                        }
                        _item.Actions.Add(aoa);
                    }

                    DataTable dtdef = sql.ReadDatatable("SELECT * FROM item_defense_attributes where itemid=" + _item.LowID + " ORDER BY defenseid asc");

                    foreach (DataRow defrow in dtdef.Rows)
                    {
                        AOItemAttribute aoia = new AOItemAttribute();
                        aoia.Stat = (Int32)defrow["num"];
                        aoia.Value = (Int32)defrow["value"];
                        _item.Defend.Add(aoia);
                    }

                    DataTable dtatt = sql.ReadDatatable("select * FROM item_attack_attributes where itemid=" + _item.LowID + " ORDER BY attackid asc");
                    foreach (DataRow defrow in dtatt.Rows)
                    {
                        AOItemAttribute aoia = new AOItemAttribute();
                        aoia.Stat = (Int32)defrow["num"];
                        aoia.Value = (Int32)defrow["value"];
                        _item.Defend.Add(aoia);
                    }

                    DataTable attributes = sql.ReadDatatable("SELECT * FROM item_attributes WHERE itemid=" + _item.LowID + " ORDER BY attributeid asc");
                    foreach (DataRow atrow in attributes.Rows)
                    {
                        AOItemAttribute aoia = new AOItemAttribute();
                        aoia.Stat = (Int32)atrow["num"];
                        aoia.Value = (Int32)atrow["value"];
                        _item.Stats.Add(aoia);
                    }


                    ITEMS.Add(_item);

                    int perc = Convert.ToInt32(Math.Floor((double)(ITEMS.Count + countall) / count * 100.0));
                    if (perc != oldperc)
                    {
                        Console.Write("\rDone " + perc.ToString().PadLeft(3) + "%");
                        oldperc = perc;
                    }
                    if (ITEMS.Count == maxnum)
                    {
                        bf.Serialize(sm, ITEMS);
                        sm.Flush();
                        ITEMS.Clear();
                        countall += maxnum;
                    }
                }
            }
            bf.Serialize(sm, ITEMS);
            sm.Seek(0, SeekOrigin.Begin);
            Console.WriteLine();
            CopyStream(sm, ds);
            sm.Close();
            ds.Close();
        }
Example #30
0
        /// <summary>
        /// decompresses a gzipped OSD object
        /// </summary>
        /// <param name="decodedOsd"></param> the OSD object
        /// <param name="meshBytes"></param>
        /// <returns></returns>
        private static OSD DecompressOsd(byte[] meshBytes)
        {
            OSD decodedOsd = null;

            using (MemoryStream inMs = new MemoryStream(meshBytes))
            {
                using (MemoryStream outMs = new MemoryStream())
                {
                    using (ZOutputStream zOut = new ZOutputStream(outMs))
                    {
                        byte[] readBuffer = new byte[2048];
                        int readLen = 0;
                        while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
                        {
                            zOut.Write(readBuffer, 0, readLen);
                        }
                        zOut.Flush();
                        outMs.Seek(0, SeekOrigin.Begin);

                        byte[] decompressedBuf = outMs.GetBuffer();

                        decodedOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
                    }
                }
            }
            return decodedOsd;
        }
Example #31
0
        // parses convex hulls component
        private bool hulls(byte[] data, int offset, int size, out int nvertices, out int nhulls)
        {
            nvertices = 0;
            nhulls = 1;

            OSD decodedMeshOsd = new OSD();
            byte[] meshBytes = new byte[size];
            System.Buffer.BlockCopy(data, offset, meshBytes, 0, size);
            try
            {
                using (MemoryStream inMs = new MemoryStream(meshBytes))
                {
                    using (MemoryStream outMs = new MemoryStream())
                    {
                        using (ZOutputStream zOut = new ZOutputStream(outMs))
                        {
                            byte[] readBuffer = new byte[4096];
                            int readLen = 0;
                            while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
                            {
                                zOut.Write(readBuffer, 0, readLen);
                            }
                            zOut.Flush();
                            outMs.Seek(0, SeekOrigin.Begin);

                            byte[] decompressedBuf = outMs.GetBuffer();
                            decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
                        }
                    }
                }
            }
            catch
            {
                return false;
            }

            OSDMap cmap = (OSDMap)decodedMeshOsd;
            if (cmap == null)
                return false;

            byte[] dummy;

            // must have one of this
            if (cmap.ContainsKey("BoundingVerts"))
            {
                dummy = cmap["BoundingVerts"].AsBinary();
                nvertices = dummy.Length / bytesPerCoord;
            }
            else
                return false;

/* upload is done with convex shape type
            if (cmap.ContainsKey("HullList"))
            {
                dummy = cmap["HullList"].AsBinary();
                nhulls += dummy.Length;
            }


            if (cmap.ContainsKey("Positions"))
            {
                dummy = cmap["Positions"].AsBinary();
                nvertices = dummy.Length / bytesPerCoord;
            }
 */

            return true;
        }
Example #32
0
        public bool PackRAF(string sourceDirectory, string targetDirectory)
        {
            //RAFHashManager.Init(); //Inits if it needs to load the hashes dict...

            //archivedPaths = RAFHashManager.GetKeys();

            uncompressedFiles = new List <string>();
            uncompressedFiles.AddRange(File.ReadAllLines(Environment.CurrentDirectory + "\\nocompress.txt"));

            sourceDirectory = sourceDirectory.Replace("/", "\\");
            String[] files = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories);

            ostream.WriteLine("Begin packing RAF");

            ostream.WriteLine("Count content bytes + file name bytes");

            UInt32        totalNameBytes            = 0;
            List <UInt32> stringTableContentOffsets = new List <UInt32>();
            List <String> archivedPaths             = new List <string>(); //Misnomer - name of the files once archived

            foreach (String filePath in files)
            {
                string archivePath = filePath.Replace(sourceDirectory, "").Replace("\\", "/");
                stringTableContentOffsets.Add((UInt32)totalNameBytes);
                totalNameBytes += (UInt32)(archivePath.Length + 1);
                archivedPaths.Add(archivePath);//raf uses / in storage name.
            }
            foreach (String archivePath in archivedPaths)
            {
                stringTableContentOffsets.Add((UInt32)totalNameBytes);
                totalNameBytes += (UInt32)(archivePath.Length + 1);
                //archivedPaths.Add(archivePath);//raf uses / in storage name.
            }

            //Calculate total bytes of header file:
            // ulong magic = 0x18BE0EF0                             6*4
            // ulong version = 1
            // toc:
            // ulong mgrIndex = 0?
            // ulong fileListOffset
            // ulong tableListOffset
            // ...filler...
            // @fileListOffset:
            //      header:
            //          ulong countFiles                            4
            //      ulong hashName                                  4*4*n
            //      ulong offsetInDataFile
            //      ulong size(Compressed/noncompressed)
            //      ulong name string @ string table
            // ....filler....
            // @tableListOffset
            //      header:                                         4*2
            //          ulong sizeOfData ( including header )
            //          ulong stringsContained
            //      ulong offsetOfDataAfterTableListOffset          4*2*n
            //      ulong stringLength (including null terminating char)

            //TODO: Check if string table contains strings other than filenames

            //List<byte> datBytes = new List<byte>();
            UInt32        totalDatBytes  = 0;
            List <UInt32> datFileOffsets = new List <UInt32>();
            List <UInt32> fileSizes      = new List <UInt32>();

            //We start by packing the files into the .dat... We just archive all files and remember their offsets for later

            PrepareDirectory(targetDirectory);
            FileStream datFileStream = File.Create(targetDirectory + "\\output.raf.dat");

            for (int i = 0; i < archivedPaths.Count; i++)
            {
                //datFileOffsets.Add((UInt32)datBytes.Count);

                //LoL_Audio.fev and LoL_Audio.fsb aren't compressed.  We write them raw.
                if (uncompressedFiles.Contains(archivedPaths[i].ToLower()))
                {
                    //Write raw
                    ostream.WriteLine("No Compress: " + archivedPaths[i]);
                    fileSizes.Add((UInt32) new FileInfo(sourceDirectory + archivedPaths[i]).Length);
                    datFileOffsets.Add((UInt32)totalDatBytes);
                    //datBytes.AddRange(File.ReadAllBytes(sourceDirectory + archivedPaths[i]));
                    byte[] content = File.ReadAllBytes(sourceDirectory + archivedPaths[i]);
                    datFileStream.Write(content, 0, content.Length);
                    totalDatBytes += (UInt32)content.Length;
                }
                else
                {
                    ostream.WriteLine("Compress: " + archivedPaths[i]);
                    //FileStream fStream = new FileStream(sourceDirectory + archivedPaths[i], FileMode.Open);
                    //fStream.Seek(0, SeekOrigin.Begin);
                    //ostreamWriteLine("FStream Length: " + fStream.Length);
                    byte[] fileContent = File.ReadAllBytes(sourceDirectory + archivedPaths[i]);
                    ostream.WriteLine("Original Size:" + fileContent.Length);
                    MemoryStream       mStream = new MemoryStream();
                    zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                    oStream.Write(fileContent, 0, fileContent.Length);
                    oStream.finish();
                    byte[] compressedContent = mStream.ToArray();
                    datFileOffsets.Add((UInt32)totalDatBytes);
                    datFileStream.Write(compressedContent, 0, compressedContent.Length);
                    //datBytes.AddRange(compressedContent);//contentBuffer.SubArray(0, length));
                    //count += (UInt32)length;
                    totalDatBytes += (UInt32)compressedContent.Length;
                    ostream.WriteLine("Done, {0} bytes".F(compressedContent.Length));
                    fileSizes.Add((UInt32)compressedContent.Length);
                }
            }

            UInt32 finalLength = (UInt32)(2 * 4 + 3 * 4 + 4 + 4 * 4 * archivedPaths.Count + 4 * 2 + 4 * 2 * archivedPaths.Count + totalNameBytes);

            byte[] buffer = new byte[finalLength]; //0x18BE0EF0.. note that the settings to 0 aren't necessary, but they're
                                                   // for readability
            //Magic
            StoreUInt32InBuffer((UInt32)0x18BE0EF0, (UInt32)0, ref buffer);

            //Version
            StoreUInt32InBuffer((UInt32)0x00000001, (UInt32)4, ref buffer);

            //mgrIndex = 0
            StoreUInt32InBuffer((UInt32)0x00000000, (UInt32)8, ref buffer);

            //fileListOffset
            StoreUInt32InBuffer((UInt32)20, (UInt32)12, ref buffer);

            //stringTableOffset
            StoreUInt32InBuffer((UInt32)(20 + 4 + 16 * archivedPaths.Count), (UInt32)16, ref buffer);

            //Start file list
            UInt32 offset = 0x14; //20, header of file list

            //Store # entries in list
            StoreUInt32InBuffer((UInt32)archivedPaths.Count, offset, ref buffer);
            offset += 4;

            //Store entries
            for (int i = 0; i < archivedPaths.Count; i++)
            {
                ostream.WriteLine("Store Entry: " + archivedPaths[i]);
                //Hash of string name?  Get it from previous RAF files.
                StoreUInt32InBuffer(GetStringHash(archivedPaths[i]), offset, ref buffer);

                // Offset to the start of the archived file in the data file
                ostream.WriteLine("  Dat Offset: " + datFileOffsets[i].ToString("x") + "; i=" + i);
                StoreUInt32InBuffer(datFileOffsets[i], offset + 4, ref buffer);

                // Size of this archived file
                StoreUInt32InBuffer(fileSizes[i], offset + 8, ref buffer);
                // Index of the name of the archvied file in the string table
                StoreUInt32InBuffer((UInt32)i, offset + 12, ref buffer);
                offset += 16;
            }

            //Create String Tables
            UInt32 stringTableOffset = offset = (UInt32)(20 + 4 + 16 * archivedPaths.Count); //This should be equivalent to offset= offset at the moment...

            //Header: Uint32 size of all data including header
            //        UINT32 Number of strings in the table

            StoreUInt32InBuffer((UInt32)totalNameBytes, offset, ref buffer);
            offset += 4;
            StoreUInt32InBuffer((UInt32)archivedPaths.Count, offset, ref buffer);
            offset += 4;
            //Write entries: UINT32 Offset from start of table, UINT32 Size of String + NUL
            //Also write the actual string values
            for (int i = 0; i < archivedPaths.Count; i++)
            {
                ostream.WriteLine("Store String: " + archivedPaths[i]);
                //Write the offset for the string after the table's offset
                //               offset after table = header + entry count * 8 + [strings offset] * 8
                UInt32 offsetAfterTable = (UInt32)(8 + archivedPaths.Count * 8 + stringTableContentOffsets[i]);
                StoreUInt32InBuffer(offsetAfterTable, offset, ref buffer);
                offset += 4;

                //Length
                byte[] stringBytes = Encoding.ASCII.GetBytes(archivedPaths[i]);
                StoreUInt32InBuffer((UInt32)archivedPaths[i].Length + 1, offset, ref buffer);
                offset += 4;

                ostream.WriteLine("  STO:" + stringTableOffset.ToString("x"));
                ostream.WriteLine("  finalIndex:" + (stringTableOffset + 8 + archivedPaths.Count * 8 + stringTableContentOffsets[i]).ToString("x"));
                //Copy the string contents to where offsetAfterTable points to
                for (int j = 0; j < stringBytes.Length; j++)
                {
                    buffer[stringTableOffset + 8 + archivedPaths.Count * 8 + stringTableContentOffsets[i] + j] = stringBytes[j];
                }
                //StoreUInt32InBuffer((UInt32)archivedPaths.Count, offset, ref buffer);
                //offset += 4;
            }

            //We are done.  Write the files
            ostream.WriteLine("All files processed.  Writing to disk.");

            //buffer
            ostream.WriteLine("Prepare.");
            PrepareDirectory(targetDirectory);

            ostream.WriteLine("Write RAF Directory File.");
            File.WriteAllBytes(targetDirectory + "\\output.raf", buffer);

            ostream.WriteLine("Finalize");
            datFileStream.Flush();
            datFileStream.Close();
            //ostreamWriteLine("Write RAF Content File. Length:"+datBytes.Count);
            //File.WriteAllBytes(targetDirectory + "\\output.raf.dat", datBytes.ToArray());
            return(true);
        }
Example #33
0
        public static OSD ZDecompressOSD(byte[] data)
        {
            OSD ret;

            using (MemoryStream input = new MemoryStream(data))
            using (MemoryStream output = new MemoryStream())
            using (ZOutputStream zout = new ZOutputStream(output))
            {
                CopyStream(input, zout);
                zout.finish();
                output.Seek(0, SeekOrigin.Begin);
                ret = OSDParser.DeserializeLLSDBinary(output);
            }

            return ret;
        }
Example #34
0
        // parses a LOD or physics mesh component
        private bool submesh(byte[] data, int offset, int size, out int ntriangles)
        {
            ntriangles = 0;

            OSD decodedMeshOsd = new OSD();
            byte[] meshBytes = new byte[size];
            System.Buffer.BlockCopy(data, offset, meshBytes, 0, size);
            try
            {
                using (MemoryStream inMs = new MemoryStream(meshBytes))
                {
                    using (MemoryStream outMs = new MemoryStream())
                    {
                        using (ZOutputStream zOut = new ZOutputStream(outMs))
                        {
                            byte[] readBuffer = new byte[4096];
                            int readLen = 0;
                            while ((readLen = inMs.Read(readBuffer, 0, readBuffer.Length)) > 0)
                            {
                                zOut.Write(readBuffer, 0, readLen);
                            }
                            zOut.Flush();
                            outMs.Seek(0, SeekOrigin.Begin);

                            byte[] decompressedBuf = outMs.GetBuffer();
                            decodedMeshOsd = OSDParser.DeserializeLLSDBinary(decompressedBuf);
                        }
                    }
                }
            }
            catch
            {
                return false;
            }

            OSDArray decodedMeshOsdArray = null;

            byte[] dummy;

            decodedMeshOsdArray = (OSDArray)decodedMeshOsd;
            foreach (OSD subMeshOsd in decodedMeshOsdArray)
            {
                if (subMeshOsd is OSDMap)
                {
                    OSDMap subtmpmap = (OSDMap)subMeshOsd;
                    if (subtmpmap.ContainsKey("NoGeometry") && ((OSDBoolean)subtmpmap["NoGeometry"]))
                        continue;

                    if (!subtmpmap.ContainsKey("Position"))
                        return false;

                    if (subtmpmap.ContainsKey("TriangleList"))
                    {
                        dummy = subtmpmap["TriangleList"].AsBinary();
                        ntriangles += dummy.Length / bytesPerCoord;
                    }
                    else
                        return false;
                }
            }

            return true;
        }
Example #35
0
        public bool PackRAF(string sourceDirectory, string targetDirectory)
        {
            //RAFHashManager.Init(); //Inits if it needs to load the hashes dict...

            //archivedPaths = RAFHashManager.GetKeys();

            uncompressedFiles = new List<string>();
            uncompressedFiles.AddRange(File.ReadAllLines(Environment.CurrentDirectory + "\\nocompress.txt"));

            sourceDirectory = sourceDirectory.Replace("/", "\\");
            String[] files = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories);

            ostream.WriteLine("Begin packing RAF");

            ostream.WriteLine("Count content bytes + file name bytes");

            UInt32 totalNameBytes = 0;
            List<UInt32> stringTableContentOffsets = new List<UInt32>();
            List<String> archivedPaths = new List<string>();        //Misnomer - name of the files once archived
            foreach (String filePath in files)
            {
                string archivePath = filePath.Replace(sourceDirectory, "").Replace("\\", "/");
                stringTableContentOffsets.Add((UInt32)totalNameBytes);
                totalNameBytes += (UInt32)(archivePath.Length + 1);
                archivedPaths.Add(archivePath);//raf uses / in storage name.
            }
            foreach (String archivePath in archivedPaths)
            {
                stringTableContentOffsets.Add((UInt32)totalNameBytes);
                totalNameBytes += (UInt32)(archivePath.Length + 1);
                //archivedPaths.Add(archivePath);//raf uses / in storage name.
            }

            //Calculate total bytes of header file: 
            // ulong magic = 0x18BE0EF0                             6*4
            // ulong version = 1
            // toc:
            // ulong mgrIndex = 0?
            // ulong fileListOffset 
            // ulong tableListOffset
            // ...filler...
            // @fileListOffset:
            //      header:
            //          ulong countFiles                            4
            //      ulong hashName                                  4*4*n
            //      ulong offsetInDataFile
            //      ulong size(Compressed/noncompressed)
            //      ulong name string @ string table
            // ....filler....
            // @tableListOffset
            //      header:                                         4*2
            //          ulong sizeOfData ( including header )
            //          ulong stringsContained
            //      ulong offsetOfDataAfterTableListOffset          4*2*n
            //      ulong stringLength (including null terminating char)

            //TODO: Check if string table contains strings other than filenames

            //List<byte> datBytes = new List<byte>();
            UInt32 totalDatBytes = 0;
            List<UInt32> datFileOffsets = new List<UInt32>();
            List<UInt32> fileSizes = new List<UInt32>();
            //We start by packing the files into the .dat... We just archive all files and remember their offsets for later

            PrepareDirectory(targetDirectory);
            FileStream datFileStream = File.Create(targetDirectory + "\\output.raf.dat");
            for(int i = 0; i < archivedPaths.Count; i++)
            {
                //datFileOffsets.Add((UInt32)datBytes.Count);

                //LoL_Audio.fev and LoL_Audio.fsb aren't compressed.  We write them raw.
                if (uncompressedFiles.Contains(archivedPaths[i].ToLower()))
                {
                    //Write raw
                    ostream.WriteLine("No Compress: " + archivedPaths[i]);
                    fileSizes.Add((UInt32)new FileInfo(sourceDirectory + archivedPaths[i]).Length);
                    datFileOffsets.Add((UInt32)totalDatBytes);
                    //datBytes.AddRange(File.ReadAllBytes(sourceDirectory + archivedPaths[i]));
                    byte[] content = File.ReadAllBytes(sourceDirectory + archivedPaths[i]);
                    datFileStream.Write(content, 0, content.Length);
                    totalDatBytes += (UInt32)content.Length;
                }
                else
                {
                    ostream.WriteLine("Compress: " + archivedPaths[i]);
                    //FileStream fStream = new FileStream(sourceDirectory + archivedPaths[i], FileMode.Open);
                    //fStream.Seek(0, SeekOrigin.Begin);
                    //ostreamWriteLine("FStream Length: " + fStream.Length);
                    byte[] fileContent = File.ReadAllBytes(sourceDirectory + archivedPaths[i]);
                    ostream.WriteLine("Original Size:" + fileContent.Length);
                    MemoryStream mStream = new MemoryStream();
                    zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                    oStream.Write(fileContent, 0, fileContent.Length);
                    oStream.finish();
                    byte[] compressedContent = mStream.ToArray();
                    datFileOffsets.Add((UInt32)totalDatBytes);
                    datFileStream.Write(compressedContent, 0, compressedContent.Length);
                    //datBytes.AddRange(compressedContent);//contentBuffer.SubArray(0, length));
                    //count += (UInt32)length;
                    totalDatBytes += (UInt32)compressedContent.Length;
                    ostream.WriteLine("Done, {0} bytes".F(compressedContent.Length));
                    fileSizes.Add((UInt32)compressedContent.Length);
                }
            }

            UInt32 finalLength = (UInt32)(2*4 + 3*4 + 4 + 4*4*archivedPaths.Count + 4*2 + 4*2*archivedPaths.Count + totalNameBytes);
            byte[] buffer = new byte[finalLength]; //0x18BE0EF0.. note that the settings to 0 aren't necessary, but they're
                                                    // for readability
            //Magic
            StoreUInt32InBuffer((UInt32)0x18BE0EF0, (UInt32)0, ref buffer);

            //Version
            StoreUInt32InBuffer((UInt32)0x00000001, (UInt32)4, ref buffer);

            //mgrIndex = 0
            StoreUInt32InBuffer((UInt32)0x00000000, (UInt32)8, ref buffer);
            
            //fileListOffset
            StoreUInt32InBuffer((UInt32)        20, (UInt32)12, ref buffer);
            
            //stringTableOffset
            StoreUInt32InBuffer((UInt32)(20+4+16*archivedPaths.Count), (UInt32)16, ref buffer);

            //Start file list
            UInt32 offset = 0x14; //20, header of file list

            //Store # entries in list
            StoreUInt32InBuffer((UInt32)archivedPaths.Count, offset, ref buffer);
            offset += 4;

            //Store entries
            for (int i = 0; i < archivedPaths.Count; i++)
            {
                ostream.WriteLine("Store Entry: " + archivedPaths[i]);
                //Hash of string name?  Get it from previous RAF files.
                StoreUInt32InBuffer(GetStringHash(archivedPaths[i]) , offset, ref buffer);

                // Offset to the start of the archived file in the data file
                ostream.WriteLine("  Dat Offset: " + datFileOffsets[i].ToString("x") + "; i="+i);
                StoreUInt32InBuffer(datFileOffsets[i]               , offset+4, ref buffer);

                // Size of this archived file
                StoreUInt32InBuffer(fileSizes[i]                    , offset+8, ref buffer);
                // Index of the name of the archvied file in the string table
                StoreUInt32InBuffer((UInt32)i                       , offset+12, ref buffer);
                offset += 16;
            }

            //Create String Tables
            UInt32 stringTableOffset = offset = (UInt32)(20 + 4 + 16 * archivedPaths.Count); //This should be equivalent to offset= offset at the moment...

            //Header: Uint32 size of all data including header
            //        UINT32 Number of strings in the table

            StoreUInt32InBuffer((UInt32)totalNameBytes, offset, ref buffer);
            offset += 4;
            StoreUInt32InBuffer((UInt32)archivedPaths.Count, offset, ref buffer);
            offset += 4;
            //Write entries: UINT32 Offset from start of table, UINT32 Size of String + NUL
            //Also write the actual string values
            for (int i = 0; i < archivedPaths.Count; i++)
            {
                ostream.WriteLine("Store String: " + archivedPaths[i]);
                //Write the offset for the string after the table's offset
                //               offset after table = header + entry count * 8 + [strings offset] * 8
                UInt32 offsetAfterTable = (UInt32)(8 + archivedPaths.Count * 8 + stringTableContentOffsets[i]);
                StoreUInt32InBuffer(offsetAfterTable, offset, ref buffer);
                offset += 4;
                
                //Length
                byte[] stringBytes = Encoding.ASCII.GetBytes(archivedPaths[i]);
                StoreUInt32InBuffer((UInt32)archivedPaths[i].Length + 1, offset, ref buffer);
                offset += 4;

                ostream.WriteLine("  STO:" + stringTableOffset.ToString("x"));
                ostream.WriteLine("  finalIndex:" + (stringTableOffset + 8 + archivedPaths.Count * 8 + stringTableContentOffsets[i]).ToString("x"));
                //Copy the string contents to where offsetAfterTable points to
                for (int j = 0; j < stringBytes.Length; j++)
                {
                    buffer[stringTableOffset+ 8 + archivedPaths.Count * 8 + stringTableContentOffsets[i] + j] = stringBytes[j];
                }
                //StoreUInt32InBuffer((UInt32)archivedPaths.Count, offset, ref buffer);
                //offset += 4;                
            }

            //We are done.  Write the files
            ostream.WriteLine("All files processed.  Writing to disk.");

            //buffer
            ostream.WriteLine("Prepare.");
            PrepareDirectory(targetDirectory);
            
            ostream.WriteLine("Write RAF Directory File.");
            File.WriteAllBytes(targetDirectory + "\\output.raf", buffer);

            ostream.WriteLine("Finalize");
            datFileStream.Flush();
            datFileStream.Close();
            //ostreamWriteLine("Write RAF Content File. Length:"+datBytes.Count);
            //File.WriteAllBytes(targetDirectory + "\\output.raf.dat", datBytes.ToArray());
            return true;
        }
Example #36
0
        public static byte[] ZCompressOSD(OSD data)
        {
            byte[] ret = null;

            using (MemoryStream outMemoryStream = new MemoryStream())
            using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, zlibConst.Z_BEST_COMPRESSION))
            using (Stream inMemoryStream = new MemoryStream(OSDParser.SerializeLLSDBinary(data, false)))
            {
                CopyStream(inMemoryStream, outZStream);
                outZStream.finish();
                ret = outMemoryStream.ToArray();
            }

            return ret;
        }
Example #37
0
        internal static Map readTmx(string tmx)
        {
            XElement xml;
            int width;
            int height;
            int tilewidthOfMap;
            int tileheightOfMap;

            try
            {
                xml = XElement.Load(tmx);
            }
            catch (Exception e)
            {
                Logger.bw.ReportProgress(1, e.Message);
                return null;
            }

            try
            {
                width = Convert.ToInt32(xml.Attribute("width").Value);
                height = Convert.ToInt32(xml.Attribute("height").Value);
            }
            catch (Exception e)
            {
                Logger.bw.ReportProgress(1, "Map has no \"width\" or \"height\". " + e.Message);
                return null;
            }

            try
            {
                tilewidthOfMap = Convert.ToInt32(xml.Attribute("tilewidth").Value);
                tileheightOfMap = Convert.ToInt32(xml.Attribute("tileheight").Value);
            }
            catch (Exception e)
            {
                Logger.bw.ReportProgress(1, "Can't read tilewidth or tileheight. " + e.Message);
                return null;
            }
            if (tilewidthOfMap != 32 || tileheightOfMap != 32)
            {
                Logger.bw.ReportProgress(1, "Tilewidth and tileheight have to be 32x32.");
                return null;
            }

            Map map = new Map(width, height);

            foreach (XElement child in xml.Elements())
            {
                if (child.Name == "tileset")
                {
                    Tileset tileset;
                    uint firstgid;
                    string key;

                    try
                    {
                        firstgid = Convert.ToUInt32(child.Attribute("firstgid").Value);
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Tileset has no \"firstgid\" attribute. " + e.Message);
                        return null;
                    }

                    var sourceAttribute = child.Attribute("source");
                    var imageElement = child.Element("image");
                    if (sourceAttribute != null) // external tileset
                    {
                        key = sourceAttribute.Value;
                        if (!tilesets.TryGetValue(key, out tileset))
                        {
                            XElement xmlExtern;
                            string path = Path.Combine(pathMaps, key);
                            try
                            {
                                xmlExtern = XElement.Load(path);
                            }
                            catch (Exception e)
                            {
                                Logger.bw.ReportProgress(1, "Problem loading tileset " + path + ": " + e.Message);
                                return null;
                            }
                            tileset = readTileset(xmlExtern, Path.GetDirectoryName(path));
                            if (tileset == null)
                                return null;
                            tilesets.Add(key, tileset);
                        }
                    }
                    else if (imageElement != null) // internal tileset
                    {
                        tileset = readTileset(child, Repo.pathMaps);
                        if (tileset == null)
                            return null;
                    }
                    else
                    {
                        Logger.bw.ReportProgress(1, "Tileset with firstgid " + firstgid + " has neither image nor source.");
                        return null;
                    }

                    map.tilesetList.Add(firstgid, tileset);
                }
                else if (child.Name == "layer")
                {
                    string name;
                    int widthLayer;
                    int heightLayer;
                    XElement data;
                    string encoding = "";
                    string compression = "";

                    try
                    {
                        name = child.Attribute("name").Value;
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Layer has no \"name\" attribute. " + e.Message);
                        return null;
                    }
                    if (name.ToLowerInvariant() == "collision")
                        continue;

                    try
                    {
                        widthLayer = Convert.ToInt32(child.Attribute("width").Value);
                        heightLayer = Convert.ToInt32(child.Attribute("height").Value);
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Layer \"" + name + "\" has no \"width\" or \"height\". " + e.Message);
                        return null;
                    }

                    try
                    {
                        data = child.Element("data");
                    }
                    catch (Exception e)
                    {
                        Logger.bw.ReportProgress(1, "Layer \"" + name + "\" has no \"data\" attribute. " + e.Message);
                        return null;
                    }
                    foreach (var a in data.Attributes())
                    {
                        if (a.Name == "encoding")
                            encoding = a.Value;
                        else if (a.Name == "compression")
                            compression = a.Value;
                    }

                    string dataString = data.Value;
                    if (encoding == "csv" && compression == "")
                    {
                        using (Stream numberStream = new MemoryStream())
                        {
                            foreach (string s in Regex.Split(dataString, ","))
                            {
                                uint number;
                                try
                                {
                                    number = uint.Parse(s, System.Globalization.NumberStyles.Integer);
                                }
                                catch (Exception e)
                                {
                                    Logger.bw.ReportProgress(1, "In Layer \"" + name + "\" can't parse number \"" + s + "\". "
                                                 + e.Message);
                                    return null;
                                }
                                byte[] ba = BitConverter.GetBytes(number);
                                numberStream.Write(ba, 0, 4);
                            }
                            numberStream.Position = 0;
                            if (!readLayer(map, widthLayer, heightLayer, numberStream))
                                return null;
                        }
                    }
                    else if (encoding == "base64" && compression == "gzip")
                    {
                        byte[] compressed = System.Convert.FromBase64String(dataString);
                        using (var compressedStream = new MemoryStream(compressed))
                        using (var decompressedStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                        {
                            if (!readLayer(map, widthLayer, heightLayer, decompressedStream))
                                return null;
                        }
                    }
                    else if (encoding == "base64" && compression == "zlib")
                    {
                        byte[] compressed = System.Convert.FromBase64String(dataString);
                        using (MemoryStream output = new MemoryStream())
                        using (var compressedStream = new MemoryStream(compressed))
                        using (var zStream = new ZOutputStream(output))
                        {
                            CopyStream(compressedStream, zStream);
                            byte[] array = output.ToArray();
                            using (MemoryStream decompressedStream = new MemoryStream(array))
                            {
                                if (!readLayer(map, widthLayer, heightLayer, decompressedStream))
                                    return null;
                            }
                        }
                    }
                    else
                    {
                        Logger.bw.ReportProgress(1, "Layer \"" + name + "\" has unsupported encoding or compression.");
                        return null;
                    }
                }
            }
            return map;
        }
Example #38
0
        private bool ReplaceContentHelperFunc(byte[] content, FileStream datFileStream)
        {
            // Store the old offsets just in case we need to perform a restore.
            // This actually isn't necessary currently, since the raf directory file is saved after packing.
            var oldOffset = FileOffset;
            var oldSize = FileSize;

            try
            {
                // Navigate to the end of it
                datFileStream.Seek(0, SeekOrigin.End);
                var offset = (UInt32) datFileStream.Length;

                var fInfo = new FileInfo(FileName);

                // .fsb, .fev, and .gfx files aren't compressed
                byte[] finalContent;
                if (fInfo.Extension == ".fsb" || fInfo.Extension == ".fev" || fInfo.Extension == ".gfx")
                {
                    finalContent = content;
                }
                else
                {
                    // Start of compression
                    var mStream = new MemoryStream();
                    var oStream = new ZOutputStream(mStream, zlibConst.Z_DEFAULT_COMPRESSION);
                    //using default compression level
                    oStream.Write(content, 0, content.Length);
                    oStream.finish();
                    finalContent = mStream.ToArray();
                }

                // Write the data to the end of the .dat file
                datFileStream.Write(finalContent, 0, finalContent.Length);

                // Update entry values to represent the new changes
                FileOffset = offset;
                FileSize = (UInt32) finalContent.Length;

                return true;
            }
            catch (Exception)
            {
                FileOffset = oldOffset;
                FileSize = oldSize;

                return false;
            }
        }
Example #39
0
        //Inserts a file into the raf archive...
        public bool InsertFile(string fileName, byte[] content, TextWriter ostream)
        {
            if (ostream == null)
            {
                ostream = new StreamWriter(Stream.Null);
            }

            ostream.WriteLine("    Insert: " + fileName);
            ostream.WriteLine("        To: " + GetID());

            RAFFileListEntry fileentry = this.GetDirectoryFile().GetFileList().GetFileEntry(fileName);

            if (fileentry == null)
            {
                //Define a new entry
                ostream.WriteLine("# Create new entry in RAF Archive, using experimental hash calculations");
                ostream.WriteLine("    Get DAT File Stream");
                FileStream datFileStream = GetDataFileContentStream();
                //navigate to the end of it, add the file.
                datFileStream.Seek(0, SeekOrigin.End);
                UInt32 offset = (UInt32)datFileStream.Length;

                byte[] finalContent;
                if (nocompress.Contains(fileName))
                {
                    finalContent = content;
                }
                else
                {
                    ostream.WriteLine("    Begin compression of content");
                    MemoryStream       mStream = new MemoryStream();
                    zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                    oStream.Write(content, 0, content.Length);
                    oStream.finish();
                    finalContent = mStream.ToArray();
                }

                ostream.WriteLine("    Begin DAT Write");
                datFileStream.Write(finalContent, 0, finalContent.Length);

                ostream.WriteLine("    Add file entry to in memory directory...");
                UInt32 strTableIndex = directoryFile.GetStringTable().Add(fileName);
                directoryFile.CreateFileEntry(fileName, offset, (UInt32)finalContent.Length, strTableIndex);
                //directoryFile.Save();
                //datFileStream.Close();
                ostream.WriteLine("    Done.");
                return(true);
            }
            else
            {
                //store the old offsets just in case we need to perform a restore.
                //This actually isn't necessary currently, since the raf directory file is saved
                //After packing.
                UInt32 oldOffset = (UInt32)fileentry.FileOffset;
                UInt32 oldSize   = (UInt32)fileentry.FileSize;
                ostream.WriteLine("Old Offset: " + oldOffset + "; Old Size: " + oldSize);

                ostream.WriteLine("Begin modify game files...  This may take a while");
                try
                {
                    ostream.WriteLine("    Get File Stream");
                    FileStream datFileStream = GetDataFileContentStream();
                    //navigate to the end of it, add the file.
                    datFileStream.Seek(0, SeekOrigin.End);
                    UInt32 offset = (UInt32)datFileStream.Length;

                    byte[] finalContent;
                    if (nocompress.Contains(fileName))
                    {
                        finalContent = content;
                    }
                    else
                    {
                        ostream.WriteLine("    Begin compression of content");
                        MemoryStream       mStream = new MemoryStream();
                        zlib.ZOutputStream oStream = new zlib.ZOutputStream(mStream, zlib.zlibConst.Z_DEFAULT_COMPRESSION); //using default compression level
                        oStream.Write(content, 0, content.Length);
                        oStream.finish();
                        finalContent = mStream.ToArray();
                    }

                    ostream.WriteLine("    Begin DAT Write");
                    datFileStream.Write(finalContent, 0, finalContent.Length);

                    ostream.WriteLine("    Begin FileEntry Write");
                    fileentry.FileOffset = offset;
                    fileentry.FileSize   = (UInt32)finalContent.Length;
                    //directoryFile.Save();

                    //datFileStream.Close();
                    ostream.WriteLine("    Done.");
                    return(true);
                }
                catch (Exception e)
                {
                    ostream.WriteLine("!! An error occurred in inserting a file.");
                    ostream.WriteLine("!! Note that the content of the raf archive has been added to");
                    ostream.WriteLine("!! But the directory file that points the game to the data in the content file");
                    ostream.WriteLine("!! Has not been modified.  In other words, if the game read the RAF archive now");
                    ostream.WriteLine("!! It wouldn't see a difference.");
                    ostream.WriteLine("!! However, the RAF archive is a bit bigger than before.");
                    ostream.WriteLine(e.ToString());

                    fileentry.FileOffset = oldOffset;
                    fileentry.FileSize   = oldSize;
                    //directoryFile.GetContent();
                    return(false);
                }
            }
        }