Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     public void OnStatusChanged(StatusCode statusCode)
     {
         if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
             Debug.Log(string.Format("OnStatusChanged: {0}", statusCode.ToString()));

         switch (statusCode)
         {
             case StatusCode.Connect:
                 if (this.State == global::PeerState.ConnectingToNameServer)
                 {
                     if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
                         Debug.Log("Connected to NameServer.");

                     this.server = ServerConnection.NameServer;
                     if (this.CustomAuthenticationValues != null)
                     {
                         this.CustomAuthenticationValues.Secret = null; // when connecting to NameServer, invalidate any auth values
                     }
                 }

                 if (this.State == global::PeerState.ConnectingToGameserver)
                 {
                     if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
                         Debug.Log("Connected to gameserver.");

                     this.server = ServerConnection.GameServer;
                     this.State = global::PeerState.ConnectedToGameserver;
                 }

                 if (this.State == global::PeerState.ConnectingToMasterserver)
                 {
                     if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
                         Debug.Log("Connected to masterserver.");

                     this.server = ServerConnection.MasterServer;
                     this.State = global::PeerState.ConnectedToMaster;

                     if (this.IsInitialConnect)
                     {
                         this.IsInitialConnect = false; // after handling potential initial-connect issues with special messages, we are now sure we can reach a server
                         SendMonoMessage(PhotonNetworkingMessage.OnConnectedToPhoton);
                     }
                 }

                 this.EstablishEncryption(); // always enable encryption

                 if (this.IsAuthorizeSecretAvailable)
                 {
                     // if we have a token we don't have to wait for encryption (it is encrypted anyways, so encryption is just optional later on)
                     this.didAuthenticate = this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, this.CloudRegion.ToString());
                     if (this.didAuthenticate)
                     {
                         this.State = global::PeerState.Authenticating;
                     }
                 }
                 break;

             case StatusCode.EncryptionEstablished:
                 // on nameserver, the "process" is stopped here, so the developer/game can either get regions or authenticate with a specific region
                 if (this.server == ServerConnection.NameServer)
                 {
                     this.State = global::PeerState.ConnectedToNameServer;

                     if (!this.didAuthenticate && this.CloudRegion == CloudRegionCode.none)
                     {
                         // this client is not setup to connect to a default region. find out which regions there are!
                         this.OpGetRegions(this.mAppId);
                     }
                 }

                 // we might need to authenticate automatically now, so the client can do anything at all
                 if (!this.didAuthenticate && (!this.IsUsingNameServer || this.CloudRegion != CloudRegionCode.none))
                 {
                     // once encryption is availble, the client should send one (secure) authenticate. it includes the AppId (which identifies your app on the Photon Cloud)
                     this.didAuthenticate = this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, this.CloudRegion.ToString());
                     if (this.didAuthenticate)
                     {
                         this.State = global::PeerState.Authenticating;
                     }
                 }
                 break;

             case StatusCode.EncryptionFailedToEstablish:
                 Debug.LogError("Encryption wasn't established: " + statusCode + ". Going to authenticate anyways.");
                 this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, this.CloudRegion.ToString()); // TODO: check if there are alternatives
                 break;

             case StatusCode.Disconnect:
                 this.didAuthenticate = false;
                 this.isFetchingFriends = false;
                 if (server == ServerConnection.GameServer) this.LeftRoomCleanup();
                 if (server == ServerConnection.MasterServer) this.LeftLobbyCleanup();

                 if (this.State == global::PeerState.DisconnectingFromMasterserver)
                 {
                     if (this.Connect(this.mGameserver, ServerConnection.GameServer))
                     {
                         this.State = global::PeerState.ConnectingToGameserver;
                     }
                 }
                 else if (this.State == global::PeerState.DisconnectingFromGameserver || this.State == global::PeerState.DisconnectingFromNameServer)
                 {
                     if (this.Connect(this.MasterServerAddress, ServerConnection.MasterServer))
                     {
                         this.State = global::PeerState.ConnectingToMasterserver;
                     }
                 }
                 else
                 {
                     if (this.CustomAuthenticationValues != null)
                     {
                         this.CustomAuthenticationValues.Secret = null; // invalidate any custom auth secrets
                     }

                     this.State = global::PeerState.PeerCreated; // if we set another state here, we could keep clients from connecting in OnDisconnectedFromPhoton right here.
                     SendMonoMessage(PhotonNetworkingMessage.OnDisconnectedFromPhoton);
                 }
                 break;

             case StatusCode.SecurityExceptionOnConnect:
             case StatusCode.ExceptionOnConnect:
                 this.State = global::PeerState.PeerCreated;
                 if (this.CustomAuthenticationValues != null)
                 {
                     this.CustomAuthenticationValues.Secret = null; // invalidate any custom auth secrets
                 }

                 DisconnectCause cause = (DisconnectCause)statusCode;
                 SendMonoMessage(PhotonNetworkingMessage.OnFailedToConnectToPhoton, cause);
                 break;

             case StatusCode.Exception:
                 if (this.IsInitialConnect)
                 {
                     Debug.LogError("Exception while connecting to: " + this.ServerAddress + ". Check if the server is available.");
                     if (this.ServerAddress == null || this.ServerAddress.StartsWith("127.0.0.1"))
                     {
                         Debug.LogWarning("The server address is 127.0.0.1 (localhost): Make sure the server is running on this machine. Android and iOS emulators have their own localhost.");
                         if (this.ServerAddress == this.mGameserver)
                         {
                             Debug.LogWarning("This might be a misconfiguration in the game server config. You need to edit it to a (public) address.");
                         }
                     }

                     this.State = global::PeerState.PeerCreated;
                     cause = (DisconnectCause)statusCode;
                     SendMonoMessage(PhotonNetworkingMessage.OnFailedToConnectToPhoton, cause);
                 }
                 else
                 {
                     this.State = global::PeerState.PeerCreated;

                     cause = (DisconnectCause)statusCode;
                     SendMonoMessage(PhotonNetworkingMessage.OnConnectionFail, cause);
                 }

                 this.Disconnect();
                 break;

             case StatusCode.TimeoutDisconnect:
             case StatusCode.ExceptionOnReceive:
             case StatusCode.DisconnectByServer:
             case StatusCode.DisconnectByServerLogic:
             case StatusCode.DisconnectByServerUserLimit:
                 if (this.IsInitialConnect)
                 {
                     Debug.LogWarning(statusCode + " while connecting to: " + this.ServerAddress + ". Check if the server is available.");

                     cause = (DisconnectCause)statusCode;
                     SendMonoMessage(PhotonNetworkingMessage.OnFailedToConnectToPhoton, cause);
                 }
                 else
                 {
                     cause = (DisconnectCause)statusCode;
                     SendMonoMessage(PhotonNetworkingMessage.OnConnectionFail, cause);
                 }
                 if (this.CustomAuthenticationValues != null)
                 {
                     this.CustomAuthenticationValues.Secret = null; // invalidate any custom auth secrets
                 }

                 this.Disconnect();
                 break;

             case StatusCode.SendError:
                 // this.mListener.clientErrorReturn(statusCode);
                 break;

             case StatusCode.QueueOutgoingReliableWarning:
             case StatusCode.QueueOutgoingUnreliableWarning:
             case StatusCode.QueueOutgoingAcksWarning:
             case StatusCode.QueueSentWarning:
                 // this.mListener.warningReturn(statusCode);
                 break;

             case StatusCode.QueueIncomingReliableWarning:
             case StatusCode.QueueIncomingUnreliableWarning:
                 Debug.Log(statusCode + ". This client buffers many incoming messages. This is OK temporarily. With lots of these warnings, check if you send too much or execute messages too slow. " + (PhotonNetwork.isMessageQueueRunning? "":"Your isMessageQueueRunning is false. This can cause the issue temporarily.") );
                 break;

             // // TCP "routing" is an option of Photon that's not currently needed (or supported) by PUN
             //case StatusCode.TcpRouterResponseOk:
             // break;
             //case StatusCode.TcpRouterResponseEndpointUnknown:
             //case StatusCode.TcpRouterResponseNodeIdUnknown:
             //case StatusCode.TcpRouterResponseNodeNotReady:

             // this.DebugReturn(DebugLevel.ERROR, "Unexpected router response: " + statusCode);
             // break;

             default:

                 // this.mListener.serverErrorReturn(statusCode.value());
                 Debug.LogError("Received unknown status code: " + statusCode);
                 break;
         }

         this.externalListener.OnStatusChanged(statusCode);
     }