Esempio n. 1
0
        NetOutgoingMessage DatablobDataMessage(NetOutgoingMessage msg, Entity entity, BaseDataBlob datablob)
        {
            var mStream = new MemoryStream();

            //int typeIndex = EntityManager.DataBlobTypes[datablobtype];
            //var datablob = entity.GetDataBlob<BaseDataBlob>(typeIndex);

            //Messages.Add("GetType().ToSTring(): " + datablob.GetType().ToString());
            //Messages.Add("GetType().Name: " + datablob.GetType().Name);
            //Messages.Add("GetType().AssemblyQualifiedName: " + datablob.GetType().AssemblyQualifiedName);
            //Messages.Add("GetType().FullName: " + datablob.GetType().FullName);
            //Messages.Add("pulsarTypeIndex: " + typeIndex);

            SerializationManager.Export(Game, mStream, datablob);
            byte[] systemByteArray = mStream.ToArray();
            int    len             = systemByteArray.Length;

            msg.Write(entity.Guid.ToByteArray());                       //entityGuid
            msg.Write(datablob.GetType().Name);                         //datablob name
            msg.Write(EntityManager.DataBlobTypes[datablob.GetType()]); //datablob typeIndex
            msg.Write(len);                                             //stream length
            msg.Write(systemByteArray);                                 //encoded data.
            mStream.Close();
            return(msg);
        }
        private void ImportExportSystem(StarSystem system)
        {
            //TODO: need to be able to export a system that will only save systemBodies.
            //for a generated one, just saving the seed should suffice.
            //for a created one we'll have to do more.
            //also need to be able to export a players view of a system. but that would likely be a different function altogether.
            SerializationManager.Export(_game, "testSystemExport", system);
            string jsonString  = SerializationManager.Export(_game, system);
            int    entityCount = system.GetAllEntitiesWithDataBlob <SystemBodyInfoDB>(_smAuthToken).Count;

            _game        = TestingUtilities.CreateTestUniverse(0);
            _smAuthToken = new AuthenticationToken(_game.SpaceMaster);

            StarSystem importedSystem = SerializationManager.ImportSystemJson(_game, jsonString);

            Assert.AreEqual(system.Guid, importedSystem.Guid);

            // See that the entities were imported.
            Assert.AreEqual(entityCount, importedSystem.GetAllEntitiesWithDataBlob <SystemBodyInfoDB>(_smAuthToken).Count);

            // Ensure the system was added to the game's system list.
            List <StarSystem> systems = _game.GetSystems(_smAuthToken);

            Assert.AreEqual(1, systems.Count);

            // Ensure the returned value references the same system as the game's system list
            system = _game.GetSystem(_smAuthToken, system.Guid);
            Assert.AreEqual(importedSystem, system);
        }
Esempio n. 3
0
        public void TestSelfReferencingEntity()
        {
            NameDB name       = new NameDB();
            Entity testEntity = Entity.Create(_game.GlobalManager, Guid.Empty);

            name.SetName(testEntity.Guid, "TestName");
            testEntity.SetDataBlob(name);

            //serialise the test entity into a mem stream
            var mStream = new MemoryStream();

            SerializationManager.Export(_game, mStream, testEntity);


            //create a second game, we're going to import this entity to here (this would be the case in a network game)
            var settings = new NewGameSettings {
                GameName = "Test Game2", StartDateTime = DateTime.Now, MaxSystems = 1
            };
            Game game2 = new Game(settings);


            //import the entity into the second game.
            Entity clonedEntity = SerializationManager.ImportEntity(game2, mStream, game2.GlobalManager);

            mStream.Close();

            Assert.IsTrue(testEntity.GetValueCompareHash() == clonedEntity.GetValueCompareHash(), "ValueCompareHash should match");//currently valueCompareHash does not check guid of the entity. I'm undecided wheather it should or not.
            Entity clonedTest;

            Assert.IsTrue(game2.GlobalManager.FindEntityByGuid(testEntity.Guid, out clonedTest), "Game2 should have the test entity");
            Assert.IsTrue(testEntity.Guid == clonedEntity.Guid, "Guid's need to match, if we get to this assert, then we've got two entities in game2, one of them has the correct guid but no datablobs, the other has a new guid but is complete.");
            Assert.IsTrue(ReferenceEquals(clonedTest, clonedEntity), "These should be the same object");
            Assert.IsTrue(testEntity.DataBlobs.Count == clonedTest.DataBlobs.Count);
            Assert.IsTrue(testEntity.DataBlobs.Count == clonedEntity.DataBlobs.Count);
        }
Esempio n. 4
0
        public void SaveGame(string pathToFile)
        {
            StatusText = "Saving Game...";

            SerializationManager.Export(Game, pathToFile);

            StatusText = "Game Saved";
        }
        public void GameImportExport()
        {
            // Nubmer of systems to generate for this test. Configurable.
            const int  numSystems   = 10;
            const bool generateSol  = false;
            int        totalSystems = generateSol ? numSystems + 1 : numSystems;

            // lets create a bad save game:

            // Check default nulls throw:
            Assert.Catch <ArgumentNullException>(() => SerializationManager.Export(null, File));
            Assert.Catch <ArgumentException>(() => SerializationManager.Export(_game, (string)null));
            Assert.Catch <ArgumentException>(() => SerializationManager.Export(_game, string.Empty));

            Assert.Catch <ArgumentException>(() => SerializationManager.ImportGame((string)null));
            Assert.Catch <ArgumentException>(() => SerializationManager.ImportGame(string.Empty));
            Assert.Catch <ArgumentNullException>(() => SerializationManager.ImportGame((Stream)null));

            _game = TestingUtilities.CreateTestUniverse(numSystems, _testTime, generateSol);



            // lets create a good saveGame
            SerializationManager.Export(_game, File);

            Assert.IsTrue(System.IO.File.Exists(Path.Combine(SerializationManager.GetWorkingDirectory(), File)));
            Console.WriteLine(Path.Combine(SerializationManager.GetWorkingDirectory(), File));
            // now lets give ourselves a clean game:
            _game = null;

            //and load the saved data:
            _game        = SerializationManager.ImportGame(File);
            _smAuthToken = new AuthenticationToken(_game.SpaceMaster);

            Assert.AreEqual(totalSystems, _game.GetSystems(_smAuthToken).Count);
            Assert.AreEqual(_testTime, StaticRefLib.CurrentDateTime);
            List <Entity> entities = _game.GlobalManager.GetAllEntitiesWithDataBlob <FactionInfoDB>(_smAuthToken);

            Assert.AreEqual(3, entities.Count);
            entities = _game.GlobalManager.GetAllEntitiesWithDataBlob <SpeciesDB>(_smAuthToken);
            Assert.AreEqual(2, entities.Count);

            // lets check the the refs were hocked back up:
            Entity species     = _game.GlobalManager.GetFirstEntityWithDataBlob <SpeciesDB>(_smAuthToken);
            NameDB speciesName = species.GetDataBlob <NameDB>();

            Assert.AreSame(speciesName.OwningEntity, species);

            // <?TODO: Expand this out to cover many more DBs, entities, and cases.
        }
Esempio n. 6
0
        NetOutgoingMessage EntityDataMessage(NetOutgoingMessage msg, Entity entity)
        {
            var mStream = new MemoryStream();

            SerializationManager.Export(Game, mStream, entity);
            byte[] entityByteArray = mStream.ToArray();
            int    len             = entityByteArray.Length;

            msg.Write(entity.Guid.ToByteArray());
            msg.Write(entity.GetValueCompareHash());
            msg.Write(len);
            msg.Write(entityByteArray);

            mStream.Close();
            return(msg);
        }
        public void SaveGameConsistency()
        {
            const int maxTries = 10;

            for (int numTries = 0; numTries < maxTries; numTries++)
            {
                TestingUtilities.CreateTestUniverse(10);
                SerializationManager.Export(_game, File);
                _game = SerializationManager.ImportGame(File);
                SerializationManager.Export(_game, File2);

                var fs1 = new FileStream(Path.Combine(SerializationManager.GetWorkingDirectory(), File), FileMode.Open);
                var fs2 = new FileStream(Path.Combine(SerializationManager.GetWorkingDirectory(), File2), FileMode.Open);

                if (fs1.Length == fs2.Length)
                {
                    // Read and compare a byte from each file until either a
                    // non-matching set of bytes is found or until the end of
                    // file1 is reached.
                    int file1Byte;
                    int file2Byte;
                    do
                    {
                        // Read one byte from each file.
                        file1Byte = fs1.ReadByte();
                        file2Byte = fs2.ReadByte();
                    } while ((file1Byte == file2Byte) && (file1Byte != -1));

                    // Close the files.
                    fs1.Close();
                    fs2.Close();

                    // Return the success of the comparison. "file1byte" is
                    // equal to "file2byte" at this point only if the files are
                    // the same.
                    if (file1Byte - file2Byte == 0)
                    {
                        Assert.Pass("Save Games consistent on try #" + (numTries + 1));
                    }
                }

                fs1.Close();
                fs2.Close();
            }
            Assert.Fail("SaveGameConsistency could not be verified. Please ensure saves are properly loading and saving.");
        }
        public void EntityImportExport()
        {
            // Ensure we have a test universe.
            _game        = TestingUtilities.CreateTestUniverse(10);
            _smAuthToken = new AuthenticationToken(_game.SpaceMaster);

            Assert.NotNull(_game);

            // Choose a random system.
            var rand = new Random();
            List <StarSystem> systems = _game.GetSystems(_smAuthToken);
            int        systemIndex    = rand.Next(systems.Count - 1);
            StarSystem system         = systems[systemIndex];

            // Export/Reinport all system bodies in that system.

            foreach (Entity entity in system.GetAllEntitiesWithDataBlob <SystemBodyInfoDB>(_smAuthToken))
            {
                string jsonString = SerializationManager.Export(_game, entity);

                // Clone the entity for later comparison.
                ProtoEntity clone = entity.Clone();

                // Destroy the entity.
                entity.Destroy();

                // Ensure the entity was destroyed.
                Entity foundEntity;
                Assert.IsFalse(system.FindEntityByGuid(clone.Guid, out foundEntity));

                // Import the entity back into the manager.
                Entity importedEntity = SerializationManager.ImportEntityJson(_game, jsonString, system);

                // Ensure the imported entity is valid
                Assert.IsTrue(importedEntity.IsValid);
                // Check to find the guid.
                Assert.IsTrue(system.FindEntityByGuid(clone.Guid, out foundEntity));
                // Check the ID imported correctly.
                Assert.AreEqual(clone.Guid, importedEntity.Guid);
                // Check the datablobs imported correctly.
                Assert.AreEqual(clone.DataBlobs.Where(dataBlob => dataBlob != null).ToList().Count, importedEntity.DataBlobs.Count);
                // Check the manager is the same.
                Assert.AreEqual(system, importedEntity.Manager);
            }
        }
Esempio n. 9
0
        void SendGameSettings(NetConnection recipient, Game game)
        {
            var gamesettings = game.Settings;
            var mStream      = new MemoryStream();

            SerializationManager.Export(game.Settings, mStream);

            byte[] byteArray = mStream.ToArray();

            int len = byteArray.Length;
            NetOutgoingMessage sendMsg = NetPeerObject.CreateMessage();

            sendMsg.Write((byte)ToClientMsgType.SendGameData);
            sendMsg.Write(StaticRefLib.CurrentDateTime.ToBinary());
            sendMsg.Write(len);
            sendMsg.Write(byteArray);
            NetServerObject.SendMessage(sendMsg, recipient, NetDeliveryMethod.ReliableOrdered);
        }
Esempio n. 10
0
        void SendEntityProcData(NetConnection recipient, Entity entity)
        {
            NetOutgoingMessage msg = NetPeerObject.CreateMessage();

            msg.Write((byte)ToClientMsgType.SendEntityProcData);
            var procDict = entity.Manager.ManagerSubpulses.GetInstanceProcForEntity(entity);
            var mStream  = new MemoryStream();

            SerializationManager.Export(procDict, mStream);
            byte[] procdicByteArray = mStream.ToArray();
            int    len = procdicByteArray.Length;

            msg.Write(entity.Guid.ToByteArray());
            msg.Write(len);
            msg.Write(procdicByteArray);
            mStream.Close();

            NetServerObject.SendMessage(msg, recipient, NetDeliveryMethod.ReliableOrdered);
        }
        public void CompareLoadedGameWithOrigional() //TODO do this after a few game ticks and check the comparitiveTests again.
        {
            //create a new game
            Game newGame = TestingUtilities.CreateTestUniverse(10, _testTime, true);

            Entity     ship        = newGame.GlobalManager.GetFirstEntityWithDataBlob <TransitableDB>();
            StarSystem firstSystem = newGame.Systems.First().Value;
            DateTime   jumpTime    = StaticRefLib.CurrentDateTime + TimeSpan.FromMinutes(1);

            //insert a jump so that we can compair timeloop dictionary
            InterSystemJumpProcessor.SetJump(newGame, jumpTime, firstSystem, jumpTime, ship);

            // lets create a good saveGame
            SerializationManager.Export(newGame, File);
            //then load it:
            Game loadedGame = SerializationManager.ImportGame(File);

            //run some tests
            ComparitiveTests(newGame, loadedGame);
        }
Esempio n. 12
0
        public void SendEntityCommand(EntityCommand cmd)
        {
            Guid cmdID = Guid.NewGuid();

            CommandsAwaitingServerAproval.Add(cmdID, cmd);

            NetOutgoingMessage sendMsg = NetPeerObject.CreateMessage();

            var mStream = new MemoryStream();

            SerializationManager.Export(Game, mStream, cmd);
            byte[] byteArray = mStream.ToArray();
            int    len       = byteArray.Length;

            sendMsg.Write((byte)ToServerMsgType.SendPlayerEntityCommand);
            sendMsg.Write(cmdID.ToByteArray());
            sendMsg.Write(cmd.GetType().Name);
            sendMsg.Write(len);
            sendMsg.Write(byteArray);

            NetClientObject.SendMessage(sendMsg, NetClientObject.ServerConnection, NetDeliveryMethod.ReliableOrdered);
            Messages.Add("Sent Command " + cmd.CmdID);
        }
Esempio n. 13
0
        void SendFactionData(NetConnection recipient, Entity factionEntity)
        {
            //var ownedEntities = factionEntity.GetDataBlob<FactionOwnedEntitesDB>().OwnedEntites.Values.ToArray();
            ProtoEntity factionEntityClone = factionEntity.Clone(); //clone it, then remove the AuthDB, we don't want to send that.

            factionEntityClone.RemoveDataBlob <AuthDB>();

            var mStream = new MemoryStream();

            SerializationManager.Export(Game, mStream, factionEntityClone);


            byte[]             entityByteArray = mStream.ToArray();
            int                len             = entityByteArray.Length;
            NetOutgoingMessage sendMsg         = NetPeerObject.CreateMessage();

            sendMsg.Write((byte)ToClientMsgType.SendFactionEntity);
            sendMsg.Write(factionEntity.Guid.ToByteArray());
            sendMsg.Write(factionEntityClone.GetValueCompareHash());
            sendMsg.Write(len);
            sendMsg.Write(entityByteArray);
            NetServerObject.SendMessage(sendMsg, recipient, NetDeliveryMethod.ReliableOrdered);
            mStream.Close();
        }
        private void ImportExportSystem(StarSystem system)
        {
            string jsonString  = SerializationManager.Export(_game, system);
            int    entityCount = system.SystemManager.GetAllEntitiesWithDataBlob <SystemBodyInfoDB>(_smAuthToken).Count;

            _game        = TestingUtilities.CreateTestUniverse(0);
            _smAuthToken = new AuthenticationToken(_game.SpaceMaster);

            StarSystem importedSystem = SerializationManager.ImportSystemJson(_game, jsonString);

            Assert.AreEqual(system.Guid, importedSystem.Guid);

            // See that the entities were imported.
            Assert.AreEqual(entityCount, importedSystem.SystemManager.GetAllEntitiesWithDataBlob <SystemBodyInfoDB>(_smAuthToken).Count);

            // Ensure the system was added to the game's system list.
            List <StarSystem> systems = _game.GetSystems(_smAuthToken);

            Assert.AreEqual(1, systems.Count);

            // Ensure the returned value references the same system as the game's system list
            system = _game.GetSystem(_smAuthToken, system.Guid);
            Assert.AreEqual(importedSystem, system);
        }