Ejemplo n.º 1
0
        // Instantiate Cables
        // if server, Sends cables to all clients
        public override void BeforeStart()
        {
            base.BeforeStart();

            // Instantiating cables from packet. This logs cables in cableDict and storage

            /*
             * if (cablePacket != null && cablePacket.cableStorageDict != null)
             * {
             *  instantiateCables();
             * }
             * else
             * {
             *  MyLog.Default.WriteLine("beforeStart: no/empty cable packet");
             * }
             */

            // Updates cablePacket from updated storageDict, sends to all clients to ensure sync
            // (In case local client joins early?)
            if (MyAPIGateway.Multiplayer.IsServer)
            {
                //cablePacket.cableStorageDict = cableStorageDict;

                List <IMyPlayer> players = new List <IMyPlayer>(MyAPIGateway.Session.SessionSettings.MaxPlayers);
                MyAPIGateway.Players.GetPlayers(players);
                MyLog.Default.WriteLine("BeforeStart, sending packet to all players:");
                cablePacket.printPacket();
                foreach (IMyPlayer player in players)
                {
                    if (player.SteamUserId == MyAPIGateway.Multiplayer.ServerId)
                    {
                        continue;
                    }

                    try
                    {
                        var bytes   = MyAPIGateway.Utilities.SerializeToBinary(cablePacket);
                        var steamID = player.SteamUserId;
                        MyAPIGateway.Multiplayer.SendMessageTo(MOD_UID, bytes, steamID);
                    }
                    catch (Exception e)
                    {
                        MyLog.Default.WriteLine(e.Message + "\n" + e.StackTrace);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 // called by event when packet received
 private void receiveCablePacket(byte[] bytes)
 {
     MyAPIGateway.Utilities.ShowNotification("Packet received!");
     MyLog.Default.WriteLine("Packet received. dict: ");
     cablePacket = MyAPIGateway.Utilities.SerializeFromBinary <CablesPacket>(bytes);
     cablePacket.printPacket();
     if (!MyAPIGateway.Multiplayer.IsServer)
     {
         MyLog.Default.WriteLine("Not server: ");
     }
     needReloadCables = true;
 }
Ejemplo n.º 3
0
        int reloadTimer       = 0; // jank af. waiting for things to load in before trying to instantiate cables

        #region State overrides

        /**
         * register handlers
         * load cables from file
         * or load cables from server
         **/
        public override void LoadData()
        {
            Instance = this;

            // Subscribe to receiving cablemod packets
            MyAPIGateway.Multiplayer.RegisterMessageHandler(MOD_UID, receiveCablePacket);

            // If server
            // check for dictionary in file and load.
            // If not present make new dictionary.

            if (MyAPIGateway.Multiplayer.IsServer)
            {
                // Subscribe to player connection delegate
                MyVisualScriptLogicProvider.PlayerConnected += onPlayerConnect;

                if (MyAPIGateway.Utilities.FileExistsInWorldStorage(SAVE_FILENAME, typeof(CablesPacket)))
                {
                    var reader = MyAPIGateway.Utilities.ReadBinaryFileInWorldStorage(SAVE_FILENAME, typeof(CablesPacket));
                    var bytes  = reader.ReadBytes(2048); // arbitrary high
                    cablePacket = MyAPIGateway.Utilities.SerializeFromBinary <CablesPacket>(bytes);

                    MyLog.Default.WriteLine("CableSession: cableDict loaded");
                    cablePacket.printPacket();
                    needReloadCables = true;
                }
                else
                {
                    MyLog.Default.WriteLine("CableSession: new empty cabledict");
                    cablePacket = new CablesPacket(cableStorageDict);
                    cablePacket.printPacket();
                }

                //----------------tests------------------------
                //var reader = MyAPIGateway.Utilities.ReadBinaryFileInWorldStorage("testSave3.b", typeof(CableStorage));
                //var readerOut = MyAPIGateway.Utilities.SerializeFromBinary<CableStorage>(reader.ReadBytes(2048));
                //MyLog.Default.WriteLine("CableSession loaded: " + readerOut.str.i.ToString());
            }

            // If client

            else
            {
            }
        }
Ejemplo n.º 4
0
        // called by delegate when player connects (only if server)
        private void onPlayerConnect(long id)
        {
            var steamID = MyAPIGateway.Multiplayer.Players.TryGetSteamId(id);

            MyAPIGateway.Utilities.ShowNotification("Player connected: " + steamID.ToString());
            MyLog.Default.WriteLine("Player connected: " + steamID.ToString());

            // if server connecting to itself, return
            if (steamID == MyAPIGateway.Multiplayer.ServerId)
            {
                MyLog.Default.WriteLine("Local client connected");
                return;
            }

            // send cablePacket to client
            cablePacket.printPacket();

            CablesPacket packetToSend;

            MyLog.Default.WriteLine("Sending packet from cableStoragedict");
            packetToSend = new CablesPacket(cableStorageDict);

            packetToSend.printPacket();

            MyLog.Default.WriteLine("Sending packet:");

            try
            {
                var bytes = MyAPIGateway.Utilities.SerializeToBinary(packetToSend);
                MyAPIGateway.Multiplayer.SendMessageTo(MOD_UID, bytes, steamID);
            }
            catch (Exception e)
            {
                MyLog.Default.WriteLine(e.Message + "\n" + e.StackTrace);
            }
        }