Ejemplo n.º 1
0
        public static bool DecryptLevel(string source, string target)
        {
            if (!File.Exists(source))
            {
                return(false);
            }
            if (File.Exists(target))
            {
                File.Delete(target);
            }
            File.Copy(source, target);

            bool result = false;

            var iStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            var oStream = new FileStream(target, FileMode.Open, FileAccess.Write, FileShare.Read);
            var sr      = new BinaryReaderEx(iStream);
            var sw      = new BinaryWriterEx(oStream);

            try
            {
                sw.Seek(3, SeekOrigin.Begin);
                sw.Write((byte)0);

                sr.BaseStream.Seek(14, SeekOrigin.Begin);
                sw.BaseStream.Seek(14, SeekOrigin.Begin);

                uint   size;
                byte[] header = new byte[_keyLength];

                int chunk;
                for (chunk = 0; chunk < 4; ++chunk)
                {
                    size = sr.ReadUInt32();
                    sr.ReadBlockArray(out header, _keyLength);
                    sw.Seek(4, SeekOrigin.Current);
                    sw.WriteBlockArray(Decrypt(header, (int)size >> (chunk + 5) & 0x0F, (int)size >> (chunk + 1) & 0x0F));
                    sw.BaseStream.Seek(size - _keyLength + sizeof(uint), SeekOrigin.Current);
                    sr.BaseStream.Seek(size - _keyLength + sizeof(uint), SeekOrigin.Current);
                }

                result = true;
            }
            finally
            {
                sw.Close();
                sr.Close();
                oStream.Close();
                iStream.Close();
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void Write(BinaryWriterEx bw)
        {
            int pos = (int)bw.BaseStream.Position;

            if (isAnimated)
            {
                if ((pos & 1) != 1)
                {
                    bw.BaseStream.Position -= 1;
                }


                bw.Write(texpos);
                bw.Write(numFrames);
                bw.Write(numFrames);

                bw.Write(0);

                bw.WriteArrayUInt32(ptrs);
                int i = 0;
                foreach (uint ptr in ptrs)
                {
                    bw.Seek((int)ptr + 4, SeekOrigin.Begin);
                    animframes[i].Write(bw);
                    i++;
                }

                bw.Seek((int)texpos + 4, SeekOrigin.Begin);
            }

            bool isWritten = false; //are we trying to re-write data, if so skip writing.

            for (int i = 0; i < 3; i++)
            {
                isWritten = midlods[i].Write(bw);
            }
            if (isWritten == false)
            {
                bw.Write(ptrHi);

                //loosely assume we got a valid pointer
                if (ptrHi > 0x30000 && ptrHi < 0xB0000)
                {
                    bw.Seek((int)ptrHi + 4, SeekOrigin.Begin);

                    for (int i = 0; i < 16; i++)
                    {
                        hi[i].Write(bw);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void Write(BinaryWriterEx bw)
        {
            bw.Write(System.Text.Encoding.ASCII.GetBytes(name));
            for (int i = 0; i < 16 - name.Length; i++)
            {
                bw.Write((byte)0);
            }
            bw.Write(modelOffset);

            scale.Write(bw);

            bw.Write(null1);
            bw.Write(unk1);

            bw.Write((int)0);
            bw.Write((int)0);
            bw.Write((int)0);

            position.Write(bw);
            angle.Write(bw);

            bw.Write(evt);

            bw.Seek((int)modelOffset + 4, SeekOrigin.Begin);
            bw.WriteStringFixed(ModelName);
        }
        public void BinaryWriterExBench()
        {
            binaryWriterEx_.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < N; ++i)
            {
                binaryWriterEx_.Write(Math.PI);
            }
        }
Ejemplo n.º 5
0
        public void Write(BinaryWriterEx bw)
        {
            bw.Write(cnt);

            for (int i = 0; i < cnt; i++)
            {
                bw.Write(ptrs[i]);
            }


            bw.Seek(6, SeekOrigin.Current);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Writes all entries to stream using binary writer.
        /// </summary>
        /// <param name="filename">BinaryWriterEx object.</param>
        public void Write(BinaryWriterEx bw)
        {
            List <string> dEntries = new List <string>();

            foreach (var entry in Entries)
            {
                if (!dEntries.Contains(entry))
                {
                    dEntries.Add(entry);
                }
            }

            var list = new Dictionary <string, int>();

            bw.Write((int)Entries.Count());
            bw.Seek(4); //get back here when we know the offset

            foreach (string entry in dEntries)
            {
                Console.WriteLine(entry);
                list.Add(entry, (int)bw.BaseStream.Position);
                bw.Write(Encoding.Default.GetBytes(entry.Replace("|", "" + (char)0xD)));
                bw.Write((byte)0);
            }

            bw.Jump(((bw.BaseStream.Position / 4) + 1) * 4);

            int lastoff = (int)bw.BaseStream.Position;

            foreach (var entry in Entries)
            {
                bw.Write(list[entry]);
            }

            bw.Write("MISSING MSG\0".ToCharArray());

            bw.Jump(4);
            bw.Write(lastoff);
        }
Ejemplo n.º 7
0
        public bool Write(BinaryWriterEx bw)
        {
            bool isWritten = IsWritten();

            if (isWritten == false)
            {
                bw.Seek((int)offset + 4, SeekOrigin.Begin);
                uv[0].Write(bw);

                bw.Write(buf0);

                uv[1].Write(bw);

                bw.Write(buf1);

                bw.Write(check);

                uv[2].Write(bw);
                uv[3].Write(bw);
                ToWrite(true);
            }

            return(isWritten);
        }
Ejemplo n.º 8
0
        public void Write()
        {
            using (FileStream fs = new FileStream(LevSavePath, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriterEx bw = new BinaryWriterEx(fs))
                {
                    bw.Flush();
                    bw.Seek((int)0, SeekOrigin.Begin);
                    bw.Write(fileSize);
                    header.Write(bw);
                    bw.Seek((int)header.ptrMeshInfo + 4, SeekOrigin.Begin);
                    meshinfo.Write(bw);
                    bw.Seek((int)meshinfo.ptrVertexArray + 4, SeekOrigin.Begin);
                    foreach (Vertex vert in verts)
                    {
                        vert.Write(bw);
                    }
                    bw.Seek((int)header.ptrRestartPts + 4, SeekOrigin.Begin);
                    foreach (var restartPoint in restartPts)
                    {
                        restartPoint.Write(bw);
                    }
                    bw.Seek((int)meshinfo.ptrVisDataArray + 4, SeekOrigin.Begin);
                    foreach (var visdat in visdata)
                    {
                        visdat.Write(bw);
                    }
                    bw.Seek((int)meshinfo.ptrQuadBlockArray + 4, SeekOrigin.Begin);
                    foreach (var quad in quads)
                    {
                        quad.Write(bw);
                    }

                    /*
                     * if (header.ptrSkybox != 0) {
                     *  bw.Seek((int) header.ptrSkybox + 4, SeekOrigin.Begin);
                     *  skybox.Write(bw);
                     * }*/


                    if (header.ptrVcolAnim != 0)
                    {
                        bw.Seek((int)header.ptrVcolAnim + 4, SeekOrigin.Begin);
                        foreach (var vertanim in vertanims)
                        {
                            vertanim.Write(bw);
                        }
                    }

                    if (header.ptrAiNav != 0)
                    {
                        bw.Seek((int)header.ptrAiNav + 4, SeekOrigin.Begin);
                        nav.Write(bw);
                    }

                    if (header.ptrTrialData != 0)
                    {
                        bw.Seek((int)header.ptrTrialData + 4, SeekOrigin.Begin);
                        trial.Write(bw);
                    }

                    if (header.cntSpawnPts != 0)
                    {
                        bw.Seek((int)header.ptrSpawnPts + 4, SeekOrigin.Begin);
                        unkadv.Write(bw);
                    }

                    bw.Seek((int)posu1_ptr + 4, SeekOrigin.Begin);
                    for (int i = 0; i < posu1_cnt; i++)
                    {
                        posu1[i].Write(bw);
                    }


                    //write pickup models
                    //starts out right, but anims ruin it
                    bw.Seek((int)(header.ptrModelsPtr + 4), SeekOrigin.Begin);
                    for (int i = 0; i < header.numModels; i++)
                    {
                        bw.Seek((int)pickUpModelDataLocation[i * 2 + 0] + 4, SeekOrigin.Begin);
                        bw.Write(pickUpModelDataLocation[i * 2 + 1]);
                        bw.Seek((int)pickUpModelDataLocation[i * 2 + 1] + 4, SeekOrigin.Begin);
                        //dynamics[i].Write(bw);
                    }

                    bw.Close();
                }
                fs.Close();
            }
        }
Ejemplo n.º 9
0
        public void Write(BinaryWriterEx bw)
        {
            bw.WriteArrayInt16(ind);

            bw.Write((ushort)quadFlags);
            //bw.Write(unk1);

            // bw.Write(drawOrderLow);
            bw.Write(bitvalue);
            bw.Write(drawOrderHigh);

            bw.WriteArrayUInt32(ptrTexMid);

            bb.Write(bw);

            bw.Write((byte)terrainFlag);
            bw.Write(WeatherIntensity);
            bw.Write(WeatherType);
            bw.Write(TerrainFlagUnknown);

            bw.Write(id);

            bw.Write(trackPos);
            bw.Write(midunk);

            Debug.Log(bw.BaseStream.Position);
            bw.Write(ptrTexLow);
            bw.Write(offset2);
            Debug.Log(offset2);

            foreach (Vector2s v in unk3)
            {
                v.Write(bw);

                Debug.Log(v);
            }

            Debug.Log(ptrTexLow);

            long texpos = bw.BaseStream.Position;

            bw.Seek((int)ptrTexLow + 4, SeekOrigin.Begin);
            texlow.Write(bw);

            int texIndex = 0;

            Debug.Log((ptrTexLow + 4) / 16 + ", " + ptrTexMid.Length / 16);
            if (38435 == (ptrTexLow + 4) / 16)
            {
                Debug.Log("----------THE INTERESTING ONE----------");
            }
            foreach (uint u in ptrTexMid)
            {
                bw.Seek((int)u + 4, SeekOrigin.Begin);
                if (tex[texIndex] != null)
                {
                    tex[texIndex].Write(bw);
                }
                texIndex++;
            }
            if (38435 == (ptrTexLow + 4) / 16)
            {
                Debug.Log("----------THE INTERESTING ONE----------");
            }
            bw.Seek((int)texpos, SeekOrigin.Begin);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Writes CTR model in original CTR format.
        /// </summary>
        /// <param name="bw">BinaryWriterEx object.</param>
        /// <param name="mode">Write mode (writes wither header or data).</param>
        public void Write(BinaryWriterEx bw, CtrWriteMode mode)
        {
            int pos = 0;

            switch (mode)
            {
            case CtrWriteMode.Header:
                pos = (int)bw.BaseStream.Position;
                bw.Write(name.ToCharArray().Take(16).ToArray());
                bw.Jump(pos + 16);
                bw.Write(unk0);
                bw.Write(lodDistance);
                bw.Write(billboard);
                scale.Write(bw);
                bw.Write(ptrCmd);
                bw.Write(ptrVerts);
                bw.Write(ptrTex);
                bw.Write(ptrClut);
                bw.Write(unk3);
                bw.Write(numAnims);
                bw.Write(ptrAnims);
                bw.Write(unk4);

                break;

            case CtrWriteMode.Data:

                //write commands

                bw.Jump(ptrCmd + 4);

                bw.Write(cmdNum);

                foreach (var c in drawList)
                {
                    bw.Write(c.GetValue());
                }

                bw.Write(0xFFFFFFFF);


                //write texturelayouts

                if (tl.Count > 0)
                {
                    bw.Jump(ptrTex + 4);

                    pos = (int)bw.BaseStream.Position;

                    for (int i = 0; i < tl.Count; i++)
                    {
                        //CtrModel.ptrs.Add((int)bw.BaseStream.Position - 4);

                        UIntPtr ptr = (UIntPtr)(pos + 4 * tl.Count + i * 12);
                        bw.Write(ptr);
                    }

                    foreach (var t in tl)
                    {
                        t.Write(bw);
                    }
                }


                //write vertices

                bw.Jump(ptrVerts + 4);

                posOffset.Write(bw);

                bw.Seek(16);

                bw.Write(vrenderMode);

                Console.WriteLine(name);

                foreach (var x in vtx)
                {
                    x.Write(bw);
                    Console.WriteLine(x.X.ToString("X2") + x.Y.ToString("X2") + x.Z.ToString("X2"));
                }

                Console.WriteLine("---");


                //write clut

                bw.Jump(ptrClut + 4);

                foreach (var x in cols)
                {
                    x.W = 0;
                    x.Write(bw);
                    Console.WriteLine(x.X.ToString("X2") + x.Y.ToString("X2") + x.Z.ToString("X2") + x.W.ToString("X2"));
                }


                break;

            default: Helpers.Panic(this, "unknown mode"); break;
            }
        }
Ejemplo n.º 11
0
        public void Write(BinaryWriterEx bw)
        {
            bw.Write(ptrMeshInfo);
            bw.Write(ptrSkybox);
            bw.Write(ptrTexArray);

            bw.Write(numInstances);
            bw.Write(ptrInstances);
            bw.Write(numModels);
            bw.Write(ptrModelsPtr);

            bw.Write(unkptr3);
            bw.Write(unkptr4);
            bw.Write(ptrPickupHeadersPtrArray);
            bw.Write(unkptr5);

            bw.Write(null1);
            bw.Write(null2);

            bw.Write(cntWater);
            bw.Write(ptrWater);
            bw.Write(ptrNamedTex);
            bw.Write(ptrNamedTexArray);
            bw.Write(ptrRestartMain);

            for (int i = 0; i < someData.Length; i++)
            {
                someData[i].Write(bw);
            }

            for (int i = 0; i < startGrid.Length; i++)
            {
                startGrid[i].Write(bw);
            }

            bw.Write(somePtr4);
            bw.Write(somePtr5);
            bw.Write(ptrLowTexArray);
            backColor.Write(bw);
            bw.Write(bgMode);

            bw.Write(ptrBuildStart);
            bw.Write(ptrBuildEnd);
            bw.Write(ptrBuildType);

            bw.Write(skip);

            bw.Write(cntTrialData);
            bw.Write(ptrTrialData);
            bw.Write(cntu2);
            bw.Write(ptru2);
            bw.Write(cntSpawnPts);
            bw.Write(ptrSpawnPts);
            bw.Write(cntRestartPts);
            bw.Write(ptrRestartPts);

            bw.Write(skip2);

            for (int i = 0; i < bgColor.Length; i++)
            {
                bgColor[i].Write(bw);
            }

            bw.Write(skip2_unkPtr);

            bw.Write(cntVcolAnim);
            bw.Write(ptrVcolAnim);

            bw.Write(skip23);


            bw.Write(ptrAiNav);

            bw.Write(skip3);

            bw.Seek((int)ptrBuildStart + 4, SeekOrigin.Begin);
            bw.WriteStringFixed(DateTime.Now.ToString());

            bw.Seek((int)ptrBuildEnd + 4, SeekOrigin.Begin);
            bw.WriteStringFixed(DateTime.Now.ToString());

            bw.Seek((int)ptrBuildType + 4, SeekOrigin.Begin);
            bw.WriteStringFixed(ptrBuildTypeStr);
        }
Ejemplo n.º 12
0
        public void WriteTr5(BinaryWriterEx writer)
        {
            var roomStartOffset = writer.BaseStream.Position;

            var xela = System.Text.Encoding.ASCII.GetBytes("XELA");

            writer.Write(xela);

            var startOfRoomPosition = writer.BaseStream.Position;
            var roomDataSize        = 0;

            writer.Write((uint)roomDataSize);

            var separator = 0xcdcdcdcd;

            writer.Write(separator);

            var EndSDOffsetPosition = writer.BaseStream.Position;
            var EndSDOffset         = (uint)0;

            writer.Write(EndSDOffset);

            var StartOfSDOffsetPosition = writer.BaseStream.Position;
            var StartOfSDOffset         = (uint)0;

            writer.Write(StartOfSDOffset);

            writer.Write(separator);

            var EndPortalOffsetPosition = writer.BaseStream.Position;
            var EndPortalOffset         = (uint)0;

            writer.Write(EndPortalOffset);

            // tr5_room_info
            writer.Write(Info.X);
            writer.Write(0);
            writer.Write(Info.Z);
            writer.Write(Info.YBottom);
            writer.Write(Info.YTop);

            writer.Write(NumZSectors);
            writer.Write(NumXSectors);

            writer.Write(AmbientIntensity);

            writer.Write((ushort)Lights.Count);
            writer.Write((ushort)StaticMeshes.Count);

            writer.Write(ReverbInfo);
            writer.Write(AlternateGroup);
            writer.Write(WaterScheme);
            writer.Write((byte)0);

            writer.Write((uint)0x00007fff);
            writer.Write((uint)0x00007fff);
            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);
            writer.Write(0xffffffff);

            writer.Write(AlternateRoom);
            writer.Write(Flags);
            writer.Write((ushort)0xffff);

            writer.Write((ushort)0);
            writer.Write((uint)0);
            writer.Write((uint)0);

            writer.Write(0xcdcdcdcd);
            writer.Write((uint)0);

            writer.Write((float)Info.X);
            writer.Write((float)Info.YBottom);
            writer.Write((float)Info.Z);

            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);
            writer.Write((uint)0);
            writer.Write(0xcdcdcdcd);

            writer.Write((uint)Triangles.Count);
            writer.Write((uint)Quads.Count);

            writer.Write((uint)0);

            writer.Write((uint)(Lights.Count * 88));
            writer.Write((uint)Lights.Count);

            writer.Write((uint)0);

            writer.Write(Info.YTop);
            writer.Write(Info.YBottom);

            writer.Write((uint)1);

            var LayerOffsetPosition = writer.BaseStream.Position;
            var LayerOffset         = 0;

            writer.Write(LayerOffset);

            var VerticesOffsetPosition = writer.BaseStream.Position;
            var VerticesOffset         = 0;

            writer.Write(VerticesOffset);

            var PolyOffsetPosition = writer.BaseStream.Position;
            var PolyOffset         = 0;

            writer.Write(PolyOffset);
            writer.Write(PolyOffset);

            writer.Write((uint)(Vertices.Count * 28));

            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);
            writer.Write(0xcdcdcdcd);

            // Start of room data (after 216 bytes from XELA)
            foreach (var light in Lights)
            {
                writer.Write((float)light.X);
                writer.Write((float)light.Y);
                writer.Write((float)light.Z);
                writer.Write(light.Color.Red / 255.0f);
                writer.Write(light.Color.Green / 255.0f);
                writer.Write(light.Color.Blue / 255.0f);

                writer.Write(0xcdcdcdcd);

                writer.Write(light.In);
                writer.Write(light.Out);

                writer.Write((float)(light.LightType == 2 ? Math.Acos(light.In) * 2.0f : 0));
                writer.Write((float)(light.LightType == 2 ? Math.Acos(light.Out) * 2.0f : 0));
                writer.Write(light.CutOff);

                writer.Write(light.DirectionX);
                writer.Write(light.DirectionY);
                writer.Write(light.DirectionZ);

                writer.Write(light.X);
                writer.Write(light.Y);
                writer.Write(light.Z);

                writer.Write((int)light.DirectionX);
                writer.Write((int)light.DirectionY);
                writer.Write((int)light.DirectionZ);

                writer.Write(light.LightType);

                writer.Write((byte)0xcd);
                writer.Write((byte)0xcd);
                writer.Write((byte)0xcd);
            }

            StartOfSDOffset = (uint)(writer.BaseStream.Position - roomStartOffset - 216);
            writer.WriteBlockArray(Sectors);
            EndSDOffset = (uint)(StartOfSDOffset + NumXSectors * NumZSectors * 8);

            writer.Write((ushort)Portals.Count);
            if (Portals.Count != 0)
            {
                writer.WriteBlockArray(Portals);
            }

            writer.Write((ushort)0xcdcd);
            EndPortalOffset = (uint)(writer.BaseStream.Position - roomStartOffset - 216);

            if (StaticMeshes.Count != 0)
            {
                writer.WriteBlockArray(StaticMeshes);
            }

            LayerOffset = (int)(writer.BaseStream.Position - roomStartOffset - 216);

            // Write just one layer
            writer.Write((uint)Vertices.Count);
            writer.Write((ushort)0);
            writer.Write((ushort)Quads.Count);
            writer.Write((ushort)Triangles.Count);
            writer.Write((ushort)0);

            writer.Write((ushort)0);
            writer.Write((ushort)0);

            writer.Write(1024.0f);
            writer.Write((float)Info.YBottom);
            writer.Write(1024.0f);
            writer.Write((NumXSectors - 1) * 1024.0f);
            writer.Write((float)Info.YTop);
            writer.Write((NumZSectors - 1) * 1024.0f);

            writer.Write((uint)0);
            var LayerVerticesOffset = writer.BaseStream.Position;

            writer.Write((uint)0);
            var LayerPolygonsOffset = writer.BaseStream.Position;

            writer.Write((uint)0);
            writer.Write((uint)0);

            PolyOffset = LayerOffset + 56;

            for (var k = 0; k < Quads.Count; k++)
            {
                Quads[k].Write(writer);
                writer.Write((ushort)0);
            }
            for (var k = 0; k < Triangles.Count; k++)
            {
                Triangles[k].Write(writer);
                writer.Write((ushort)0);
            }

            if (Triangles.Count % 2 != 0)
            {
                writer.Write((ushort)0xcdcd);
            }

            VerticesOffset = (int)(writer.BaseStream.Position - roomStartOffset - 216);

            foreach (var vertex in Vertices)
            {
                writer.Write((float)vertex.Position.X);
                writer.Write((float)vertex.Position.Y);
                writer.Write((float)vertex.Position.Z);
                writer.Write((float)vertex.Normal.X);
                writer.Write((float)vertex.Normal.Y);
                writer.Write((float)vertex.Normal.Z);
                writer.Write(vertex.Color);
            }

            var endOfRoomOffset = writer.BaseStream.Position;

            roomDataSize = (int)(endOfRoomOffset - roomStartOffset - 8);

            writer.Seek((int)startOfRoomPosition, SeekOrigin.Begin);
            writer.Write(roomDataSize);

            writer.Seek((int)EndSDOffsetPosition, SeekOrigin.Begin);
            writer.Write((int)EndSDOffset);
            writer.Write((int)StartOfSDOffset);

            writer.Seek((int)EndPortalOffsetPosition, SeekOrigin.Begin);
            writer.Write((int)EndPortalOffset);

            writer.Seek((int)LayerOffsetPosition, SeekOrigin.Begin);
            writer.Write(LayerOffset);
            writer.Write(VerticesOffset);
            writer.Write(PolyOffset);
            writer.Write(PolyOffset);

            writer.Seek((int)LayerVerticesOffset, SeekOrigin.Begin);
            writer.Write(VerticesOffset);
            writer.Write(PolyOffset);
            writer.Write(PolyOffset);

            writer.Seek((int)endOfRoomOffset, SeekOrigin.Begin);
        }