Ejemplo n.º 1
0
 public void addNPCsToSyncList(ReplicatedProperties rep)
 {
     synchronizedComponents.Add(rep.server_id, rep);
 }
Ejemplo n.º 2
0
    public static byte decodeRawData(byte[] src, Dictionary <int, ReplicatedProperties> synchronizedComponents)
    {
        int offset       = 0;
        int commandCount = deserializeToInt(src, ref offset);

        for (int j = 0; j < commandCount; ++j)
        {
            ushort opcode = deserializeToUShort(src, ref offset);
            if (opcode == (ushort)NetOpCodes.SpawnPrefab)
            { // this should not run on server.
                int id_count = src[offset];
                offset++;
                int[] serverInstIds = new int[id_count];
                bool  found         = false;
                int   tmpOffset     = offset;
                for (int i = 0; i < id_count; ++i)
                {
                    serverInstIds[i] = deserializeToInt(src, ref tmpOffset);
                    if (synchronizedComponents.ContainsKey(serverInstIds[i]))
                    {
                        found = true;
                        break;
                    }
                }
                offset += 4 * id_count;

                string path = deserializeToString(src, ref offset);
                if (found == false)
                {
                    UnityEngine.Object     o              = Resources.Load(path);
                    GameObject             spawnedGO      = GameObject.Instantiate(o) as GameObject;
                    ReplicatedProperties[] rep_components = spawnedGO.GetComponents <ReplicatedProperties>();

                    for (int i = 0; i < id_count; ++i)
                    {
                        synchronizedComponents.Add(serverInstIds[i], rep_components[i]);
                        Debug.Log("spawning " + path + ":" + serverInstIds[i] + ":" + rep_components[i].GetType());
                    }
                }
                else
                {
                    Debug.Log("spawning a prefab from server that already exists: " + path);
                }
            }
            else if (opcode == (ushort)NetOpCodes.RPCFunc)
            {
                int    component_id = deserializeToInt(src, ref offset);
                ushort skipIndex    = deserializeToUShort(src, ref offset);
                // the number of arguments is inferred by rpc_id
                if (synchronizedComponents.ContainsKey(component_id))
                {
                    ushort rpc_id = deserializeToUShort(src, ref offset);
                    synchronizedComponents[component_id].rpcReceive(rpc_id, src, ref offset);
                }
                else
                {
                    Debug.Log("rpc to entity: " + component_id + " not found!");
                    offset = skipIndex;
                }
            }
            else if (opcode == (ushort)NetOpCodes.Replication)   // this should not run on server.

            // variable type is not needed. it can be inferred from varOffset.
            {
                ushort totalLength  = deserializeToUShort(src, ref offset);
                int    bkLength     = offset;
                int    component_id = deserializeToInt(src, ref offset);
                ushort varOffset    = deserializeToUShort(src, ref offset);

                if (synchronizedComponents.ContainsKey(component_id))
                {
                    ReplicatedProperties propComp = synchronizedComponents[component_id];
                    if (propComp != null)
                    {
                        propComp.stateRepReceive(varOffset, src, ref offset);
                    }
                    else
                    {
                        Debug.Log("rep to entity: " + component_id + " not found!");
                        offset = bkLength + totalLength;
                    }
                }
                else
                {
                    Debug.Log("rep to entity: " + component_id + " not found!");
                    offset = bkLength + totalLength;
                }
            }
            else
            {
                Debug.Log("unrecongized cmd " + opcode);
            }
        }
        return(0);
    }