Beispiel #1
0
 public void CalculateChecksum()
 {
     Checksum = 0;
     using (var ms = new MemoryStream())
     {
         var writer = new BinaryWriter(ms);
         var reader = new BinaryReader(ms);
         writer.WriteStruct(this);
         reader.BaseStream.Position = 0;
         while (reader.BaseStream.Length > reader.BaseStream.Position)
             Checksum += reader.ReadByte();
     }
     Checksum = ~Checksum;
     Checksum = Checksum.EndianSwap();
 }
Beispiel #2
0
        /// <summary>
        /// Writes a <see cref="VersionResource"/> object to the current <see cref="Stream"/>.
        /// </summary>
        /// <param name="resource">
        /// The <see cref="VersionResource"/> to write.
        /// </param>
        public void Write(VersionResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (Stream.Length < resource.Size)
            {
                Stream.SetLength(resource.Size);
            }

            using (var stream = new SubStream(Stream, 0, Stream.Length, leaveParentOpen: true))
            using (var writer = new BinaryWriter(stream, Encoding.Unicode))
            {
                // Write the version resource header
                WriteHeader(
                    writer,
                    resource.Size,
                    resource.FixedFileInfo == null ? 0 : Marshal.SizeOf(typeof(VS_FIXEDFILEINFO)),
                    VersionDataType.Binary,
                    "VS_VERSION_INFO");

                if (resource.FixedFileInfo != null)
                {
                    writer.WriteStruct(resource.FixedFileInfo.Value);
                }

                writer.Align();

                if (resource.VarFileInfo != null)
                {
                    WriteHeader(writer, resource.VarFileInfoSize, 0, VersionDataType.Text, "VarFileInfo");

                    WriteVarFileInfo(writer, resource);
                }

                if (resource.StringFileInfo != null)
                {
                    WriteHeader(writer, resource.StringFileInfoSize, 0, VersionDataType.Text, "StringFileInfo");

                    WriteStringFileInfo(writer, resource);
                }
            }
        }
Beispiel #3
0
        private void WriteHeader(BinaryWriter writer, long length, long valueLength, VersionDataType binary, string key)
        {
            var header = new VersionHeader
            {
                Length = (ushort)length,
                ValueLength = (ushort)valueLength,
                Type = binary
            };

            writer.WriteStruct(header);
            writer.WriteUnicodeString(key);
            writer.Align();
        }
Beispiel #4
0
        public void SaveFile()
        {
            var filePath = Game.GameManager.SavePath + FileName;
            var strm = System.IO.File.OpenWrite(filePath);
            mFile.Position = 0;
            using (BinaryWriter wr = new BinaryWriter(strm))
            {
                while (true)
                {
                    string chunk = "";
                    try
                    {
                        chunk = GetChunk();
                    }
                    catch (Exception)
                    {
                        break;
                    }

                    if (chunk == "MOHD")
                    {
                        wr.Write(0x4D4F4844);
                        wr.Write(Marshal.SizeOf(mHeader));
                        wr.WriteStruct(mHeader);
                        SkipChunk();
                        continue;
                    }

                    if (chunk == "MOTX")
                    {
                        wr.Write(0x4D4F5458);
                        List<byte> textureData = new List<byte>();
                        foreach (var texture in TextureNames)
                        {
                            var bytes = Encoding.UTF8.GetBytes(texture);
                            textureData.AddRange(bytes);
                            var len = bytes.Length % 4;
                            if (len != 0)
                                textureData.AddRange(new byte[4 - len]);
                        }

                        wr.Write(textureData.Count);
                        wr.Write(textureData.ToArray());
                        SkipChunk();
                        continue;
                    }
                }
            }

            strm.Close();
        }
Beispiel #5
0
		public static bool ValidateStream(Stream stream, bool fixCommonBugs)
		{
			if (stream == null) throw new ArgumentNullException();
			if (!stream.CanSeek) throw new ArgumentException("The stream does not support seeking.");
			if (!stream.CanRead) throw new ArgumentException("The stream does not support reading.");

			var currentPos = stream.Position;

			var reader = new BinaryReader(stream, Encoding.ASCII);

			// Read the header.
			var header = (TGA_HEADER)reader.ReadStruct(typeof(TGA_HEADER));

			if (header.identsize > 0)
				stream.Seek(header.identsize, SeekOrigin.Current);

			// Check if we know the image data type.
			if (!Enum.IsDefined(typeof(TgaPixelFormat), header.PixelFormat)) throw new Exception("Unknown image data type.");

			// Check if color map is needed by data type and load it if available.
			if (header.PixelFormat == TgaPixelFormat.Indexed)
				if (!header.HasColorMap)
					throw new Exception("No color map is defined.");
				else
					reader.ReadBytes((header.colormapbits / 8) * header.colormaplength);

			if (fixCommonBugs)
			{
				fixCommonBugs = false;
				if (!header.HasColorMap && (header.PixelFormat != TgaPixelFormat.Indexed))
				{
					if (!stream.CanWrite)
						throw new ArgumentException("Tga header contains a bug but can't be fixed since the stream does not support writing.");
					if ((header.colormapstart != 0) || (header.colormaplength != 0))
					{
						stream.Position = currentPos;
						header.colormapstart = 0;
						header.colormaplength = 0;
						var wr = new BinaryWriter(stream);
						wr.WriteStruct(header);
						fixCommonBugs = true;
					}
				}
			}

			stream.Position = currentPos;

			switch (header.bits)
			{
				case 32:
				case 24:
				case 16:
					return fixCommonBugs;
				case 8:
					// Color palettes are supported.
					if (header.HasColorMap && (header.PixelFormat == TgaPixelFormat.Indexed))
						return fixCommonBugs;

					// Greyscale is supported.
					if (header.PixelFormat == TgaPixelFormat.Greyscale)
						return fixCommonBugs;
					break;
			}
			throw new NotSupportedException("Stream does not appear to be a valid TGA stream.");
		}
Beispiel #6
0
        //создаем конфиг с дефолт данными
        public static void New()
        {
            CfgDatas.CfgHdr = _ch;
            CfgDatas.ConStr = _ccs;

                using (BinaryWriter binWriter=new BinaryWriter(new FileStream(FileName,FileMode.CreateNew)))
                {
                    binWriter.WriteStruct<CfgDataStruct>(CfgDatas);

                }
                //MessageBox.Show("Config create");
                Load();
        }