public void WriteTo(BinaryWriter w)
		{
			// http://www.pkware.com/documents/casestudies/APPNOTE.TXT

			var offsets = new Queue();
			var Items = this.Entries;
			
			#region Local file header:
			foreach (var v in Items)
			{
				// if we box unit, it will actually represent Long 
				// this is for tostring functionality

				offsets.Enqueue((uint)w.BaseStream.Position);

				//var h = v.Header;
				var file_name = Encoding.ASCII.GetBytes(v.FileName);

				//        local file header signature     4 bytes  (0x04034b50)
				w.Write(ZIPFileEntryHeader.FileHeader);
				//        version needed to extract       2 bytes
				w.Write((short)0x000A);
				//        general purpose bit flag        2 bytes
				w.Write((short)0);

				//        compression method              2 bytes
				w.Write(ZIPFileEntryHeader.UNCOMPRESSED);
				//        last mod file time              2 bytes
				//        last mod file date              2 bytes
				w.WriteUInt32(ToMsDosDateTime(DateTime.Now));

				//        crc-32                          4 bytes
				w.WriteUInt32((uint)Crc32Helper.GetCrc32(v.Data.ToArray()));

				//        compressed size                 4 bytes
				w.WriteUInt32((uint)v.Data.Length);
				//        uncompressed size               4 bytes
				w.WriteUInt32((uint)v.Data.Length);


				//        file name length                2 bytes
				w.Write((short)file_name.Length);

				//        extra field length              2 bytes
				w.Write((short)0);

				//        file name (variable size)
				w.Write(file_name);

				//        extra field (variable size)

				v.Data.WriteTo(w.BaseStream);

			}
			#endregion

			var p = w.BaseStream.Position;


			#region Central directory structure
			foreach (var v in Items)
			{
				//var h = v.Header;
				var offset = (uint)offsets.Dequeue();

				var file_name = Encoding.ASCII.GetBytes(v.FileName);


				//       central file header signature   4 bytes  (0x02014b50)
				w.Write((int)0x02014b50);
				//       version made by                 2 bytes
				w.WriteUInt16((ushort)0x0014);
				//       version needed to extract       2 bytes
				w.WriteUInt16((ushort)0x000A);
				//       general purpose bit flag        2 bytes
				w.WriteUInt16((ushort)0x0000);
				//       compression method              2 bytes
				w.Write(ZIPFileEntryHeader.UNCOMPRESSED);
				//       last mod file time              2 bytes
				//       last mod file date              2 bytes
				w.WriteUInt32(ToMsDosDateTime(DateTime.Now));
				//       crc-32                          4 bytes
				w.WriteUInt32((uint)Crc32Helper.GetCrc32(v.Data.ToArray()));
				//       compressed size                 4 bytes
				w.WriteUInt32((uint)v.Data.Length);
				//       uncompressed size               4 bytes
				w.WriteUInt32((uint)v.Data.Length);
				//       file name length                2 bytes
				w.WriteUInt16((ushort)file_name.Length);
				//       extra field length              2 bytes
				w.WriteUInt16((ushort)0x0000);
				//       file comment length             2 bytes
				w.WriteUInt16((ushort)0x0000);
				//       disk number start               2 bytes
				w.WriteUInt16((ushort)0x0000);
				//       internal file attributes        2 bytes
				w.WriteUInt16((ushort)0x0000);
				//       external file attributes        4 bytes
				w.Write((int)0x0000);
				//       relative offset of local header 4 bytes
				w.WriteUInt32((uint)offset);
				//       file name (variable size)
				w.Write(file_name);
				//       extra field (variable size)
				//       file comment (variable size)
			}
			#endregion

			//      

			var q = w.BaseStream.Position;
			var z = q - p;

			#region I.  End of central directory record:
			//end of central dir signature    4 bytes  (0x06054b50)
			w.WriteUInt32((uint)0x06054b50);

			//number of this disk             2 bytes
			w.WriteUInt16((ushort)0);
			//number of the disk with the
			//start of the central directory  2 bytes
			w.WriteUInt16((ushort)0);
			//total number of entries in the
			//central directory on this disk  2 bytes
			w.WriteUInt16((ushort)Items.Length);
			//total number of entries in
			//the central directory           2 bytes
			w.WriteUInt16((ushort)Items.Length);
			//size of the central directory   4 bytes
			w.WriteUInt32((uint)z);
			//offset of start of central
			//directory with respect to
			//the starting disk number        4 bytes
			w.WriteUInt32((uint)p);
			//.ZIP file comment length        2 bytes
			w.WriteUInt16((ushort)0);
			//.ZIP file comment       (variable size)
			#endregion


		}
Пример #2
0
        static void SaveInfos(BinaryWriter writer)
        {
            var section = new SaveInfoSection();
            section.Type = ScummHelper.MakeTag('I', 'N', 'F', 'O');
            section.Version = InfoSectionVersion;
            section.Size = SaveInfoSectionSize;

            // TODO: still save old format for older versions
            section.TimeTValue = 0;
            section.PlayTime = 0;

            //TimeDate curTime;
            //_system->getTimeAndDate(curTime);

            //section.date = ((curTime.tm_mday & 0xFF) << 24) | (((curTime.tm_mon + 1) & 0xFF) << 16) | ((curTime.tm_year + 1900) & 0xFFFF);
            //section.time = ((curTime.tm_hour & 0xFF) << 8) | ((curTime.tm_min) & 0xFF);

            writer.WriteUInt32BigEndian(section.Type);
            writer.WriteUInt32BigEndian(section.Version);
            writer.WriteUInt32BigEndian(section.Size);
            writer.WriteUInt32BigEndian(section.TimeTValue);
            writer.WriteUInt32BigEndian(section.PlayTime);
            writer.WriteUInt32BigEndian(section.Date);
            writer.WriteUInt16(section.Time);
        }
Пример #3
0
        private int PrepareSaveData(byte[] destBuf)
        {
            using (var stream = new MemoryStream(destBuf))
            {
                using (var bw = new BinaryWriter(stream))
                {
                    bw.BaseStream.Seek(4, SeekOrigin.Begin);
                    bw.WriteUInt32(SaveFileRevision);
                    bw.WriteUInt32((uint) SystemVars.Instance.GameVersion.Version.Minor);

                    bw.WriteUInt16(_skySound.SaveSounds[0]);
                    bw.WriteUInt16(_skySound.SaveSounds[1]);

                    bw.WriteUInt32(_skyMusic.CurrentMusic);
                    bw.WriteUInt32(_savedCharSet);
                    bw.WriteUInt32(_savedMouse);
                    bw.WriteUInt32(SystemVars.Instance.CurrentPalette);
                    for (var cnt = 0; cnt < Logic.NumSkyScriptVars; cnt++)
                        bw.WriteUInt32(_skyLogic.ScriptVariables[cnt]);
                    var loadedFilesList = _skyDisk.LoadedFilesList;

                    for (var cnt = 0; cnt < 60; cnt++)
                        bw.WriteUInt32(loadedFilesList[cnt]);

                    for (var cnt = 0; cnt < _skyCompact.SaveIds.Length; cnt++)
                    {
                        var rawCpt = _skyCompact.FetchCptRaw(_skyCompact.SaveIds[cnt]);
                        bw.WriteBytes(rawCpt, rawCpt.Length);
                    }

                    var length = bw.BaseStream.Position;
                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    bw.WriteUInt32((uint) length);
                    return (int) length;
                }
            }
        }
Пример #4
0
        private void SaveGameToFile(byte slot)
        {
            ushort cnt;
            var fName = $"sword1.{slot:D3}";
            ushort[] liveBuf = new ushort[ObjectMan.TOTAL_SECTIONS];

            using (var stream = _saveFileMan.OpenForSaving(fName))
            using (var outf = new BinaryWriter(stream))
            {
                //if (!outf)
                //{
                //    // Display an error message and do nothing
                //    displayMessage(0, "Unable to create file '%s'. (%s)", fName, _saveFileMan.popErrorDesc().c_str());
                //    return;
                //}

                outf.WriteUInt32((uint)SAVEGAME_HEADER);

                outf.Write(StrToBytes(_saveNames[slot], 40));
                outf.WriteByte(SAVEGAME_VERSION);

                if (!IsPanelShown()) // Generate a thumbnail only if we are outside of game menu
                    SaveThumbnail(outf);

                // Date / time
                DateTime curTime = DateTime.Now;

                uint saveDate =
                    (uint)
                        ((curTime.Day & 0xFF) << 24 | ((curTime.Month + 1) & 0xFF) << 16 |
                         ((curTime.Year + 1900) & 0xFFFF));
                ushort saveTime = (ushort)((curTime.Hour & 0xFF) << 8 | ((curTime.Minute) & 0xFF));

                outf.WriteUInt32BigEndian(saveDate);
                outf.WriteUInt16BigEndian(saveTime);

                // TODO: outf.WriteUInt32BigEndian(g_engine.getTotalPlayTime() / 1000); replaced by 0
                outf.WriteUInt32BigEndian(0);

                _objMan.SaveLiveList(liveBuf);
                for (cnt = 0; cnt < ObjectMan.TOTAL_SECTIONS; cnt++)
                    outf.WriteUInt16(liveBuf[cnt]);

                var cpt = _objMan.FetchObject(Logic.PLAYER);
                Logic.ScriptVars[(int)ScriptVariableNames.CHANGE_DIR] = (uint)cpt.dir;
                Logic.ScriptVars[(int)ScriptVariableNames.CHANGE_X] = (uint)cpt.xcoord;
                Logic.ScriptVars[(int)ScriptVariableNames.CHANGE_Y] = (uint)cpt.ycoord;
                Logic.ScriptVars[(int)ScriptVariableNames.CHANGE_STANCE] = StaticRes.STAND;
                Logic.ScriptVars[(int)ScriptVariableNames.CHANGE_PLACE] = (uint)cpt.place;

                for (cnt = 0; cnt < Logic.NUM_SCRIPT_VARS; cnt++)
                    outf.WriteUInt32(Sword1.Logic.ScriptVars[cnt]);

                uint playerSize = (SwordObject.Size - 12000) / 4;
                var playerRaw = new UIntAccess(cpt.Data, cpt.Offset);
                for (var cnt2 = 0; cnt2 < playerSize; cnt2++)
                    outf.WriteUInt32(playerRaw[cnt2]);
            }
            // TODO: error
            //if (outf.err())
            //    displayMessage(0, "Couldn't write to file '%s'. Device full? (%s)", fName, _saveFileMan.popErrorDesc().c_str());
            //delete outf;
        }