Exemplo n.º 1
0
        public SavFile Read(byte[] data)
        {
            var ret = new SavFile();

            byte[] buffer = new byte[4];

            using (var ms = new MemoryStream(data))
            {
                ms.Read(buffer, 0, buffer.Length);
                uint magic = BitConverter.ToUInt32(buffer, 0);

                if (magic != SavMagic)
                {
                    throw new InvalidDataException("Wrong sav magic");
                }

                ms.Seek(4, SeekOrigin.Current);

                buffer = new byte[16];
                ms.Read(buffer, 0, buffer.Length);
                Array.Copy(buffer, ret.Checksum, 16);

                buffer = new byte[4];
                ms.Read(buffer, 0, buffer.Length);
                ret.FileSize = BitConverter.ToUInt32(buffer, 0);

                if (data.Length != ret.FileSize)
                {
                    throw new InvalidDataException("Header doesn't match to filesize");
                }

                ms.Read(buffer, 0, buffer.Length);
                ret.ContentSize1 = BitConverter.ToUInt32(buffer, 0);

                ms.Read(buffer, 0, buffer.Length);
                ret.ContentSize2 = BitConverter.ToUInt32(buffer, 0);

                buffer = new byte[8];
                ms.Read(buffer, 0, buffer.Length);

                ret.SaveDate = BitConverter.ToUInt64(buffer, 0);
            }

            return(ret);
        }
Exemplo n.º 2
0
        public void Save(string filename, SavFile file)
        {
            using (FileStream fsout = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter brout = new BinaryWriter(fsout))
                {
                    brout.Write(Common.WriteStruct(file.Header));

                    brout.Write((short)file.ResearchItems.Count);
                    for (int i = 0; i < file.ResearchItems.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.ResearchItems[i]));
                    }

                    for (int i = 0; i < 34; i++)
                    {
                        var t = new ResearchItem();
                        t.RequiredBy    = new ResearchableItem[5];
                        t.RequiredBy[0] = ResearchableItem.End;
                        brout.Write(Common.WriteStruct(t));
                    }

                    for (int i = 0; i < 7; i++)
                    {
                        brout.Write(file.CurrentResearchItems[i]);
                    }

                    brout.Write(file.Unknown1);
                    brout.Write(file.Unknown2);
                    brout.Write((int)file.ResearchManagementType);
                    brout.Write(file.Unknown3);

                    brout.Write(file.StarSystems.Count);
                    for (int i = 0; i < file.StarSystems.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.StarSystems[i]));
                    }

                    brout.Write(file.PlanetStructures.Count);
                    for (int i = 0; i < file.PlanetStructures.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.PlanetStructures[i]));
                    }

                    brout.Write((short)file.Planets.Count);
                    for (int i = 0; i < file.Planets.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.Planets[i]));
                    }

                    brout.Write(file.Unknown4);

                    brout.Write((short)file.Ships.Count);
                    for (int i = 0; i < file.Ships.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.Ships[i]));
                    }

                    brout.Write(file.Unknown5);

                    brout.Write((short)file.Diplomacies.Count);
                    for (int i = 0; i < file.Diplomacies.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.Diplomacies[i]));
                    }

                    brout.Write(file.Unknown6);

                    brout.Write(file.StarLanes.Count);
                    for (int i = 0; i < file.StarLanes.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.StarLanes[i]));
                    }

                    for (int i = 0; i < file.StarSystems.Count; i++)
                    {
                        brout.Write(Common.WriteStruct(file.StarLaneCounts[i]));
                    }

                    var t2 = new byte[100];
                    for (int i = 0; i < 100; i++)
                    {
                        t2[i] = 0xff;
                    }
                    for (int i = 0; i < 100 - file.StarSystems.Count; i++)
                    {
                        //TODO: Blank starlanecount structs should all be 0xff
                        //brout.Write(WriteStruct(new StarLaneCount()));
                        brout.Write(t2);
                    }

                    brout.Write(file.Unknown7);
                }
            }
        }
Exemplo n.º 3
0
        public SavFile Load(string filename)
        {
            var file = new SavFile();

            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    file.Header = (Header)Common.ReadStruct(br, typeof(Header));


                    var reseachItemCount = br.ReadInt16();
                    for (int i = 0; i < reseachItemCount; i++)
                    {
                        var ri = (ResearchItem)Common.ReadStruct(br, typeof(ResearchItem));
                        file.ResearchItems.Add(ri);
                    }

                    // Unused research items
                    for (int i = 0; i < 34; i++)
                    {
                        Common.ReadStruct(br, typeof(ResearchItem));
                    }

                    // Current researchs
                    for (int i = 0; i < 7; i++)
                    {
                        file.CurrentResearchItems.Add(br.ReadInt16());
                    }


                    file.Unknown1 = br.ReadBytes(10);
                    file.Unknown2 = br.ReadInt32();
                    file.ResearchManagementType = (ManagementType)br.ReadInt32();
                    file.Unknown3 = br.ReadInt32();


                    var systemCount = br.ReadInt32();
                    for (int i = 0; i < systemCount; i++)
                    {
                        var ss = (StarSystem)Common.ReadStruct(br, typeof(StarSystem));
                        file.StarSystems.Add(ss);
                    }


                    var planetStructureCount = br.ReadInt32();
                    for (int i = 0; i < planetStructureCount; i++)
                    {
                        var ps = (PlanetStructure)Common.ReadStruct(br, typeof(PlanetStructure));
                        file.PlanetStructures.Add(ps);
                    }


                    // Planets (system info view)
                    var planetCount = br.ReadInt16();
                    for (int i = 0; i < planetCount; i++)
                    {
                        var p = (Planet)Common.ReadStruct(br, typeof(Planet));
                        file.Planets.Add(p);
                    }


                    file.Unknown4 = br.ReadInt16();


                    // Ships
                    var shipCount = br.ReadInt16();
                    for (int i = 0; i < shipCount; i++)
                    {
                        var s = (Ship)Common.ReadStruct(br, typeof(Ship));
                        file.Ships.Add(s);
                    }


                    file.Unknown5 = br.ReadInt16();


                    // Diplmacy
                    var diplomacyCount = br.ReadInt16();
                    for (int i = 0; i < diplomacyCount; i++)
                    {
                        var d = (Diplomacy)Common.ReadStruct(br, typeof(Diplomacy));
                        file.Diplomacies.Add(d);
                    }


                    file.Unknown6 = br.ReadInt16();


                    // Star Lanes
                    var starLaneCount = br.ReadInt32();
                    for (int i = 0; i < starLaneCount; i++)
                    {
                        var sl = (StarLane)Common.ReadStruct(br, typeof(StarLane));
                        file.StarLanes.Add(sl);
                    }


                    // There are 100 blocks of 100 bytes each, however only systemCount of them are used
                    // each block has an entry for each system
                    // the value of that entry is the minimum number of starlane jumps from this system to that
                    // the intersection will always therefore be 0 (starlane jumps from this system, to this system, is 0)
                    // e.g. 0 4 5 2 indicates
                    // from system 0 to system 0 is 0 jumps
                    // from system 0 to system 1 is 4 jumps
                    // from system 0 to system 2 is 5 jumps
                    // from system 0 to system 3 is 2 jumps
                    for (int i = 0; i < systemCount; i++)
                    {
                        var slc = (StarLaneCount)Common.ReadStruct(br, typeof(StarLaneCount));
                        file.StarLaneCounts.Add(slc);
                    }

                    // Read the unused starlane count records
                    for (int i = 0; i < 100 - systemCount; i++)
                    {
                        Common.ReadStruct(br, typeof(StarLaneCount));
                    }


                    // 7672 unknown bytes
                    file.Unknown7 = br.ReadBytes(7672);
                }
                return(file);
            }
        }
Exemplo n.º 4
0
 public StreamToFile(FileStream stream)
 {
     this.Stream       = stream;
     this.SavFile      = new SavFile();
     this.BinaryReader = new BinaryReader(this.Stream, Encoding.Default);
 }