public static object TryDeserializeXmlFormatter(String filename)
        {
            object obj = null;

            Application.Log("Trying to load (XmlFormatter) " + filename);
            if (!File.Exists(filename))
            {
                Application.Log("No such file: " + filename);
            }
            else
            {
                try
                {
                    Common.XmlFormatter formatter = new Common.XmlFormatter {
                        Binder = ClientXmlFormatterBinder.Instance
                    };
                    var f = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                    obj = formatter.Deserialize(f);
                    f.Close();
                    Application.Log("Load completed (XmlFormatter) " + filename);
                }
                catch (Exception e)
                {
                    Application.Log("Error loading " + filename + ": ", e.ToString());
                }
            }
            return(obj);
        }
Beispiel #2
0
 static void Main(string[] args)
 {
     Test b2 = new Test { str = "TWO FACE" };
     Test a = new Test
     {
         x = 6,
         str = "hej",
         ints = new List<int> { 5, 6, 7, 8},
         strings = new String[] {"lala", "hej" },
         floats = new float[5, 3],
         test = new Test
         {
             x = 7,
             str = "lala",
             tests = new List<Test> { new Test { str = "FIRST", test = b2 }, new Test{ str = "SECOND", blah = Blah.Nej }, b2}
         },
         blah = Blah.Nej
     };
     a.floats[2, 2] = 3;
     a.floats[4, 1] = 9;
     Common.XmlFormatter f = new Common.XmlFormatter();
     MemoryStream m = new MemoryStream();
     f.Serialize(m, a);
     byte[] d = m.ToArray();
     foreach (char c in d)
         Console.Write(c);
     Console.ReadKey();
     m = new MemoryStream(d);
     Test b = (Test)f.Deserialize(m);
     a.DeepCompare(b);
     Console.WriteLine("All ok! : " + Common.DeepComparer.Compare(a, b));
     Console.ReadKey();
 }
 public ConnectingState(String nick, String address, int port)
 {
     this.nick = nick;
     this.address = address;
     this.port = port;
     NetworkIn = new Common.Network.S2CInterface
     {
         Connected = (ep, map) =>
         {
             FileStream f = new FileStream("Maps/" + map, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
             //BinaryFormatter b = new BinaryFormatter();
             Common.XmlFormatter b = new Common.XmlFormatter();
             Program.Instance.Map = (Common.Map.Map)b.Deserialize(f);
             f.Close();
             if (File.Exists("Maps/" + map + ".terrain"))
             {
                 BinaryFormatter bf = new BinaryFormatter();
                 f = new FileStream("Maps/" + map + ".terrain", FileMode.Open);
                 Program.Instance.Map.Heightmap = (float[,])bf.Deserialize(f);
                 f.Close();
             }
             Program.Instance.ChangeState(new LobbyState());
         }
     };
 }
Beispiel #4
0
        public Server_(String map, int clientport, bool storeCombatLog)
        {
            Log.Init();
            MapName = map;
            this.clientport = clientport;
            Socket = new Common.Network.UdpServer(clientport);
            Socket.OnSocketError += new Common.Network.UdpServer.SocketErrorDelegate((e, ep) => { throw new Exception(e.ToString()); });
            Socket.OnReceive += new Common.Network.UdpServer.ReceiveDelegate((d, e) => Invoke(() => Socket_OnReceive(d, e)));
            InitNetworkOut();
            Socket.Start();
            Log.WriteLine("Client comm inited at port " + clientport);

            if (storeCombatLog)
            {
                combatLog = new StreamWriter("Logs/CombatLog-" + DateTime.Now.ToString("yyyyMMdd-HH.mm") + ".log");
                combatLog.AutoFlush = true;
            }

            //BinaryFormatter f = new BinaryFormatter();
            Common.XmlFormatter f = new Common.XmlFormatter();
            FileStream r = new FileStream("Maps/" + MapName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            Map = (Common.Map.Map)f.Deserialize(r);
            r.Close();
            if (File.Exists("Maps/" + MapName + ".terrain"))
            {
                BinaryFormatter bf = new BinaryFormatter();
                r = new FileStream("Maps/" + MapName + ".terrain", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                Map.Heightmap = (float[,])bf.Deserialize(r);
                r.Close();
            }

            int nplayers = 0;
            List<Common.Map.Team> slotToTeam = new List<Common.Map.Team>();
            for (int i = 0; i < Map.Teams.Length; i++)
            {
                nplayers += Map.Teams[i].NumberOfPlayers;
                for (int x = 0; x < Map.Teams[i].NumberOfPlayers; x++)
                    slotToTeam.Add(Map.Teams[i]);
            }
            Players = new Player[nplayers];
            SlotToTeam = slotToTeam.ToArray();

            Log.WriteLine();
            state = new States.Lobby(this);
            state.Enter();

            updateTimer = new Timer(new TimerCallback(Update));
            updateTimer.Change(updatePeriod, 0);
            lastUpdate = DateTime.Now;

            Log.WriteLine("Server started!");
            GameStats = new Common.GameStats();
        }
        public static void SerializeCommonXmlFormatter(string filename, object o)
        {
            Application.Log("Trying to save (CommonXmlFormatter) " + filename);
            Common.XmlFormatter formatter = new Common.XmlFormatter { Binder = ClientXmlFormatterBinder.Instance };

            var tmpFilename = Path.GetTempFileName();
            Application.Log("Using temp filename " + tmpFilename);
            var f = System.IO.File.Open(tmpFilename, FileMode.Create);
            formatter.Serialize(f, o);
            f.Close();
            File.Delete(filename);
            File.Move(tmpFilename, filename);
            Application.Log("Save complete (CommonXmlFormatter) " + filename);
        }
        public static void SerializeCommonXmlFormatter(string filename, object o)
        {
            Application.Log("Trying to save (CommonXmlFormatter) " + filename);
            Common.XmlFormatter formatter = new Common.XmlFormatter {
                Binder = ClientXmlFormatterBinder.Instance
            };

            var tmpFilename = Path.GetTempFileName();

            Application.Log("Using temp filename " + tmpFilename);
            var f = System.IO.File.Open(tmpFilename, FileMode.Create);

            formatter.Serialize(f, o);
            f.Close();
            File.Delete(filename);
            File.Move(tmpFilename, filename);
            Application.Log("Save complete (CommonXmlFormatter) " + filename);
        }
Beispiel #7
0
 public QuickStart(String nick, String map, int port)
 {
     this.nick = nick;
     this.map = map;
     this.port = port;
     NetworkIn = new Common.Network.S2CInterface
     {
         Connected = (ep, map_) =>
         {
             FileStream f = new FileStream("Maps/" + map_, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
             //BinaryFormatter b = new BinaryFormatter();
             Common.XmlFormatter b = new Common.XmlFormatter();
             Program.Instance.Map = (Common.Map.Map)b.Deserialize(f);
             f.Close();
             if (File.Exists("Maps/" + map + ".terrain"))
             {
                 BinaryFormatter bf = new BinaryFormatter();
                 f = new FileStream("Maps/" + map + ".terrain", FileMode.Open);
                 Program.Instance.Map.Heightmap = (float[,])bf.Deserialize(f);
                 f.Close();
             }
             Program.Instance.NetworkOut.StartGame(Program.Instance.ServerEndpoint);
             title.Text = "Starting...";
         },
         StartGame = (ep) =>
         {
             Program.Instance.ChangeState(new GameState());
         },
         PlayerJoined = (ep, nick_, slot) =>
         {
         },
         PlayerChangedSlot = (ep, oldslot, slot) =>
         {
         },
     };
 }
 public static object TryDeserializeXmlFormatter(String filename)
 {
     object obj = null;
     Application.Log("Trying to load (XmlFormatter) " + filename);
     if (!File.Exists(filename))
     {
         Application.Log("No such file: " + filename);
     }
     else
     {
         try
         {
             Common.XmlFormatter formatter = new Common.XmlFormatter { Binder = ClientXmlFormatterBinder.Instance };
             var f = System.IO.File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
             obj = formatter.Deserialize(f);
             f.Close();
             Application.Log("Load completed (XmlFormatter) " + filename);
         }
         catch (Exception e)
         {
             Application.Log("Error loading " + filename + ": ", e.ToString());
         }
     }
     return obj;
 }