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 ExecuteRPC(Hashtable rpcData, PhotonPlayer sender)
     {
         if (rpcData == null || !rpcData.ContainsKey((byte)0))
         {
             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
             return;
         }

         // ts: updated with "flat" event data
         int netViewID = (int)rpcData[(byte)0]; // LIMITS PHOTONVIEWS&PLAYERS
         int otherSidePrefix = 0; // by default, the prefix is 0 (and this is not being sent)
         if (rpcData.ContainsKey((byte)1))
         {
             otherSidePrefix = (short)rpcData[(byte)1];
         }

         string inMethodName;
         if (rpcData.ContainsKey((byte)5))
         {
             int rpcIndex = (byte)rpcData[(byte)5]; // LIMITS RPC COUNT
             if (rpcIndex > PhotonNetwork.PhotonServerSettings.RpcList.Count - 1)
             {
                 Debug.LogError("Could not find RPC with index: " + rpcIndex + ". Going to ignore! Check PhotonServerSettings.RpcList");
                 return;
             }
             else
             {
                 inMethodName = PhotonNetwork.PhotonServerSettings.RpcList[rpcIndex];
             }
         }
         else
         {
             inMethodName = (string)rpcData[(byte)3];
         }

         object[] inMethodParameters = null;
         if (rpcData.ContainsKey((byte)4))
         {
             inMethodParameters = (object[])rpcData[(byte)4];
         }

         if (inMethodParameters == null)
         {
             inMethodParameters = new object[0];
         }

         PhotonView photonNetview = this.GetPhotonView(netViewID);
         if (photonNetview == null)
         {
             int viewOwnerId = netViewID/PhotonNetwork.MAX_VIEW_IDS;
             bool owningPv = (viewOwnerId == this.mLocalActor.ID);
             bool ownerSent = (viewOwnerId == sender.ID);

             if (owningPv)
             {
                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! View was/is ours." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID);
             }
             else
             {
                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! Was remote PV." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID + " Maybe GO was destroyed but RPC not cleaned up.");
             }
             return;
         }

         if (photonNetview.prefix != otherSidePrefix)
         {
             Debug.LogError(
                 "Received RPC \"" + inMethodName + "\" on viewID " + netViewID + " with a prefix of " + otherSidePrefix
                 + ", our prefix is " + photonNetview.prefix + ". The RPC has been ignored.");
             return;
         }

         // Get method name
         if (inMethodName == string.Empty)
         {
             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
             return;
         }

         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
             Debug.Log("Received RPC: " + inMethodName);


         // SetReceiving filtering
         if (photonNetview.group != 0 && !allowedReceivingGroups.Contains(photonNetview.group))
         {
             return; // Ignore group
         }

         Type[] argTypes = new Type[0];
         if (inMethodParameters.Length > 0)
         {
             argTypes = new Type[inMethodParameters.Length];
             int i = 0;
             for (int index = 0; index < inMethodParameters.Length; index++)
             {
                 object objX = inMethodParameters[index];
                 if (objX == null)
                 {
                     argTypes[i] = null;
                 }
                 else
                 {
                     argTypes[i] = objX.GetType();
                 }

                 i++;
             }
         }

         int receivers = 0;
         int foundMethods = 0;
         MonoBehaviour[] mbComponents = photonNetview.GetComponents(); // NOTE: we could possibly also cache MonoBehaviours per view?!
         for (int componentsIndex = 0; componentsIndex < mbComponents.Length; componentsIndex++)
         {
             MonoBehaviour monob = mbComponents[componentsIndex];
             if (monob == null)
             {
                 Debug.LogError("ERROR You have missing MonoBehaviours on your gameobjects!");
                 continue;
             }

             Type type = monob.GetType();

             // Get [RPC] methods from cache
             List cachedRPCMethods = null;
             if (this.monoRPCMethodsCache.ContainsKey(type))
             {
                 cachedRPCMethods = this.monoRPCMethodsCache[type];
             }

             if (cachedRPCMethods == null)
             {
                 List entries = SupportClass.GetMethods(type, typeof(RPC));

                 this.monoRPCMethodsCache[type] = entries;
                 cachedRPCMethods = entries;
             }

             if (cachedRPCMethods == null)
             {
                 continue;
             }

             // Check cache for valid methodname+arguments
             for (int index = 0; index < cachedRPCMethods.Count; index++)
             {
                 MethodInfo mInfo = cachedRPCMethods[index];
                 if (mInfo.Name == inMethodName)
                 {
                     foundMethods++;
                     ParameterInfo[] pArray = mInfo.GetParameters();
                     if (pArray.Length == argTypes.Length)
                     {
                         // Normal, PhotonNetworkMessage left out
                         if (this.CheckTypeMatch(pArray, argTypes))
                         {
                             receivers++;
                             object result = mInfo.Invoke((object)monob, inMethodParameters);
                             if (mInfo.ReturnType == typeof(IEnumerator))
                             {
                                 monob.StartCoroutine((IEnumerator)result);
                             }
                         }
                     }
                     else if ((pArray.Length - 1) == argTypes.Length)
                     {
                         // Check for PhotonNetworkMessage being the last
                         if (this.CheckTypeMatch(pArray, argTypes))
                         {
                             if (pArray[pArray.Length - 1].ParameterType == typeof(PhotonMessageInfo))
                             {
                                 receivers++;

                                 int sendTime = (int)rpcData[(byte)2];
                                 object[] deParamsWithInfo = new object[inMethodParameters.Length + 1];
                                 inMethodParameters.CopyTo(deParamsWithInfo, 0);
                                 deParamsWithInfo[deParamsWithInfo.Length - 1] = new PhotonMessageInfo(sender, sendTime, photonNetview);

                                 object result = mInfo.Invoke((object)monob, deParamsWithInfo);
                                 if (mInfo.ReturnType == typeof(IEnumerator))
                                 {
                                     monob.StartCoroutine((IEnumerator)result);
                                 }
                             }
                         }
                     }
                     else if (pArray.Length == 1 && pArray[0].ParameterType.IsArray)
                     {
                         receivers++;
                         object result = mInfo.Invoke((object)monob, new object[] { inMethodParameters });
                         if (mInfo.ReturnType == typeof(IEnumerator))
                         {
                             monob.StartCoroutine((IEnumerator)result);
                         }
                     }
                 }
             }
         }

         // Error handling
         if (receivers != 1)
         {
             string argsString = string.Empty;
             for (int index = 0; index < argTypes.Length; index++)
             {
                 Type ty = argTypes[index];
                 if (argsString != string.Empty)
                 {
                     argsString += ", ";
                 }

                 if (ty == null)
                 {
                     argsString += "null";
                 }
                 else
                 {
                     argsString += ty.Name;
                 }
             }

             if (receivers == 0)
             {
                 if (foundMethods == 0)
                 {
                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" marked with the [RPC](C#) or @RPC(JS) property! Args: " + argsString);
                 }
                 else
                 {
                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString);
                 }
             }
             else
             {
                 Debug.LogError("PhotonView with ID " + netViewID + " has " + receivers + " methods \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString + ". Should be just one?");
             }
         }
     }