Пример #1
0
        protected void Awake()
        {
            //validFrames = new BitArray(SimpleSyncSettings.frameCount + 1);
            packObjValidMask = new FastBitMask128(SimpleSyncSettings.FrameCount);

            if (!_rigidbody)
            {
                _rigidbody = transform.GetNestedComponentInChildren <Rigidbody>();
            }

            if (!_rigidbody)
            {
                _rigidbody2D = transform.GetNestedComponentInChildren <Rigidbody2D>();
            }

#if PUN_2_OR_NEWER
            pv = GetComponent <PhotonView>();
            if (pv == null)
            {
                pv = gameObject.AddComponent <PhotonView>();
            }
#endif
            CollectAndReorderInterfaces();

            this.IndexColliders();

            GetComponentsInChildren(true, onAwakeCallbacks);

            /// OnAwake Callbacks
            int cnt = onAwakeCallbacks.Count;
            for (int i = 0; i < cnt; ++i)
            {
                onAwakeCallbacks[i].OnAwake();
            }
        }
Пример #2
0
 /// Struct Type Constructor
 public PackObjectInfo(
     FastBitMask128 defaultReadyMask,
     PackStructDelegate packStructToBuffer,
     //PackStructDelegate unpackStructFromBuffer,
     PackFrameDelegate packFrameToBuffer,
     UnpackFrameDelegate unpackFrameFromBuffer,
     int maxBits,
     Func <PackFrame> factoryFrame,
     Func <IntPtr, int, PackFrame[]> factoryFramesStruct,
     PackCopyFrameToStructDelegate copyFrameToStruct,
     Action <IntPtr, PackFrame> captureStruct,
     Action <PackFrame, PackFrame> copyFrameToFrame,
     PackSnapshotStructDelegate snapStruct,
     PackInterpFrameToFrameDelegate interpFrameToFrame,
     PackInterpFrameToStructDelegate interpFrameToStruct,
     int fieldCount
     )
 {
     this.defaultReadyMask   = defaultReadyMask;
     this.PackStructToBuffer = packStructToBuffer;
     //this.unpackStructFromBuffer = unpackStructFromBuffer;
     this.PackFrameToBuffer     = packFrameToBuffer;
     this.UnpackFrameFromBuffer = unpackFrameFromBuffer;
     this.maxBits             = maxBits;
     this.maxBytes            = (maxBits + 7) >> 3;
     this.FactoryFrame        = factoryFrame;
     this.FactoryFramesStruct = factoryFramesStruct;
     this.CopyFrameToStruct   = copyFrameToStruct;
     this.CaptureStruct       = captureStruct;
     this.CopyFrameToFrame    = copyFrameToFrame;
     this.SnapStruct          = snapStruct;
     this.InterpFrameToFrame  = interpFrameToFrame;
     this.InterpFrameToStruct = interpFrameToStruct;
     this.fieldCount          = fieldCount;
 }
Пример #3
0
        public ConnectionTick(int originToLocal, int localToOrigin)
        {
            this.originToLocal = originToLocal;
            this.localToOrigin = localToOrigin;

            validFrames     = new FastBitMask128(SimpleSyncSettings.FrameCount + 1);
            frameArriveTime = new float[frameCount];
        }
Пример #4
0
        /// <summary>
        /// Find all of the callback interfaces on children, and add them to the callback list, respecting the ApplyOrder value.
        /// </summary>
        private void CollectAndReorderInterfaces()
        {
            /// Collect all components to avoid doing this over and over
            //GetComponentsInChildren(true, reusableFindSyncObjs);
            transform.GetNestedComponentsInChildren(reusableComponents);

            int cnt = reusableComponents.Count;

            for (int order = 0; order <= ApplyOrderConstants.MAX_ORDER_VAL; ++order)
            {
                for (int index = 0; index < cnt; ++index)
                {
                    var comp = reusableComponents[index];

                    /// Don't include self, or you will stack overflow hard.
                    if (comp == this)
                    {
                        continue;
                    }

                    var iApplyOrder = comp as IApplyOrder;

                    /// Apply any objects without IApplyOrder to the middle timing of 5
                    if (ReferenceEquals(iApplyOrder, null))
                    {
                        if (order == 5)
                        {
                            AddInterfaces(comp);
                            AddPackObjects(comp);
                        }
                    }
                    else
                    if (iApplyOrder.ApplyOrder == order)
                    {
                        AddInterfaces(comp);
                    }
                }
            }

            syncObjReadyMask = new FastBitMask128(indexedSyncObjs.Count);
            packObjReadyMask = new FastBitMask128(indexedPackObjs.Count);

            for (int i = 0; i < indexedSyncObjs.Count; ++i)
            {
                var so = indexedSyncObjs[i];
                so.SyncObjIndex = i;

                ///// Add NetObj to ReadyStateChange callback
                ///// and Simulate firing of ReadyState changes on syncObjs that we may have missed due to order of exec.
                //so.onReadyCallbacks += OnSyncObjReadyChange;
                OnSyncObjReadyChange(so, so.ReadyState);
            }
        }
Пример #5
0
        public static SerializationFlags Pack(PackFrame obj, PackFrame prevFrame, ref FastBitMask128 mask, ref int maskOffset, byte[] buffer, ref int bitposition, int frameId, SerializationFlags writeFlags)
        {
            var packable             = obj as PackFrame_TestPackObject;
            var prev                 = prevFrame as PackFrame_TestPackObject;
            SerializationFlags flags = SerializationFlags.None;

            {
                var flag = rotationPacker(ref packable.rotation, prev.rotation, buffer, ref bitposition, frameId, writeFlags);
                mask[maskOffset] = flag != SerializationFlags.None;     flags |= flag; maskOffset++;
            }
            {
                var flag = intorobotoPacker(ref packable.intoroboto, prev.intoroboto, buffer, ref bitposition, frameId, writeFlags);
                mask[maskOffset] = flag != SerializationFlags.None;     flags |= flag; maskOffset++;
            }
            return(flags);
        }
Пример #6
0
        public static void Apply(PackFrame src, System.Object trg, ref FastBitMask128 mask, ref int maskOffset)
        {
            var s = src as PackFrame_TestPackObject;
            var t = trg as emotitron.TestPackObject;

            { if (mask[maskOffset])
              {
                  var hold = t.rotation; t.rotation = s.rotation; t.RotationHook(s.rotation, hold);
              }
            } maskOffset++;
            { if (mask[maskOffset])
              {
                  t.intoroboto = s.intoroboto;
              }
            } maskOffset++;
        }
Пример #7
0
        public ConnectionTickOffsets(int connId, int originToLocal, int localToOrigin /*, int originToLocalTick, int localToOriginTick*/)
        {
            this.connId             = connId;
            this.originToLocalFrame = originToLocal;
            this.localToOriginFrame = localToOrigin;
            //this.originToLocalTick = originToLocalTick;
            //this.localToOriginTick = localToOriginTick;

            int frameCount = TickEngineSettings.frameCount;

            validFrameMask             = new FastBitMask128(frameCount + 1);
            frameArriveTime            = new float[frameCount];
            frameTimeBeforeConsumption = new float[frameCount];
            for (int i = 0; i < frameCount; ++i)
            {
                frameTimeBeforeConsumption[i] = float.PositiveInfinity;
            }
        }
Пример #8
0
        public static SerializationFlags Unpack(PackFrame obj, ref FastBitMask128 mask, ref FastBitMask128 isCompleteMask, ref int maskOffset, byte[] buffer, ref int bitposition, int frameId, SerializationFlags writeFlags)
        {
            var packable             = obj as PackFrame_TestPackObject;
            SerializationFlags flags = SerializationFlags.None;

            {
                if (mask[maskOffset])
                {
                    var flag = rotationUnpacker(ref packable.rotation, buffer, ref bitposition, frameId, writeFlags);
                    isCompleteMask[maskOffset] = (flag & SerializationFlags.IsComplete) != 0; mask[maskOffset] = flag != 0; flags |= flag;
                }
                maskOffset++;
            }
            {
                if (mask[maskOffset])
                {
                    var flag = intorobotoUnpacker(ref packable.intoroboto, buffer, ref bitposition, frameId, writeFlags);
                    isCompleteMask[maskOffset] = (flag & SerializationFlags.IsComplete) != 0; mask[maskOffset] = flag != 0; flags |= flag;
                }
                maskOffset++;
            }
            return(flags);
        }
Пример #9
0
        public void OnDeserialize(int sourceFrameId, int originFrameId, int localframeId, byte[] buffer, ref int bitposition, bool hasData)
        {
#if SNS_WARNINGS
            bufferAddTime[localframeId] = Time.time;
#endif

            SerializationFlags flags = SerializationFlags.None;

            if (hasData)
            {
#if SNS_WARNINGS
                /// Integrity check
                if (buffer.Read(ref bitposition, 8) != 111)
                {
                    Debug.LogError("Failed Integrity check pre PackObjs.");
                }
#endif
                FrameArrival arrival = NetMaster.CheckFrameArrival(localframeId);

                /// Deserialize Pack Objects
                //int prevFrameId = ((localframeId == 0) ? SimpleSyncSettings.FrameCount : localframeId) - 1;
                int pcnt = packObjRecords.Count;
                for (int i = 0; i < pcnt; ++i)
                {
                    var p      = packObjRecords[i];
                    var pframe = p.packFrames[localframeId];

                    int mcnt = p.info.fieldCount;
                    for (int m = 0; m < mcnt; ++m)
                    {
                        pframe.mask[m] = buffer.ReadBool(ref bitposition);
                    }

                    int maskOffset = 0;

                    Debug.Log("PRE");
                    var flag = p.info.UnpackFrameFromBuffer(pframe, ref pframe.mask, ref pframe.isCompleteMask, ref maskOffset, buffer, ref bitposition, originFrameId, SerializationFlags.None);
                    flags |= flag;

                    //Debug.Log(localframeId + " Des READY? flg: " + flag + "  " + p.readyMask.AllAreTrue + " " + p.readyMask.PrintMask(null) + " "
                    //	+ p.readyMask.Seg1 + ":" + p.readyMask.AllTrue1 + " " + p.readyMask.Seg2 + ":" + p.readyMask.AllTrue2);

                    /// Experimental - Apply valid values as they arrive if pack object isn't fully ready. Ensure even a late arriving full update counts toward Ready
                    if (!p.readyMask.AllAreTrue /*&& (flag & SerializationFlags.IsComplete) != 0*/)
                    {
                        /// Add any always ready bits (Triggers)
                        p.readyMask.OR(p.info.defaultReadyMask);
                        p.readyMask.OR(pframe.isCompleteMask);
                        /// Only write to syncvars that are not already marked as valid
                        FastBitMask128 newchangesmask = !p.readyMask & pframe.isCompleteMask;
                        maskOffset = 0;
                        p.info.CopyFrameToObj(pframe, p.component, ref newchangesmask, ref maskOffset);

                        Debug.Log(localframeId + "<b> Des PRE COMPLETE? </b>" + p.readyMask.AllAreTrue + " " + p.readyMask.PrintMask(null) + " "
                                  + p.readyMask.Seg1 + ":" + p.readyMask.AllTrue1 + " " + p.readyMask.Seg2 + ":" + p.readyMask.AllTrue2);
                    }
                }

                packObjValidMask[localframeId] = true;

#if SNS_WARNINGS
                /// Integrity check
                if (buffer.Read(ref bitposition, 8) != 123)
                {
                    Debug.LogError(name + " Failed Integrity check post PackObjs. " + localframeId);
                }
#endif
                /// Deserialize SyncObjs
                int cnt = onNetDeserializeCallbacks.Count;
                for (int i = 0; i < cnt; ++i)
                {
                    var flag = onNetDeserializeCallbacks[i].OnNetDeserialize(originFrameId, localframeId, buffer, ref bitposition, arrival);
                    flags |= flag;

                    /// Experimental - immediately apply complete frames to unready sync objects.
                    //if (arrival == FrameArrival.IsLate && (flag & SerializationFlags.IsComplete) != 0 && !syncObjReadyMask[i])
                    //	Debug.Log("Call an early Apply here when a method exists for that.");
                }

#if SNS_WARNINGS
                /// Integrity check
                if (buffer.Read(ref bitposition, 8) != 234)
                {
                    Debug.LogError(name + " Failed Integrity check post SyncObjs. " + localframeId);
                }
#endif
            }
        }
Пример #10
0
        public static void SnapshotCallback(PackFrame snapframe, PackFrame targframe, System.Object trg, ref FastBitMask128 readyMask, ref int maskOffset)
        {
            var snap     = snapframe as PackFrame_TestPackObject;
            var targ     = targframe as PackFrame_TestPackObject;
            var t        = trg as emotitron.TestPackObject;
            var snapmask = snapframe.mask;
            var targmask = targframe.mask;

            if (readyMask[maskOffset])
            {
                var snapval = snapmask[maskOffset] ? snap.rotation : t.rotation; var targval = targmask[maskOffset] ? targ.rotation : snapval; t.SnapshotHook(snapval, targval);
            }
            maskOffset++;
            maskOffset++;
        }
Пример #11
0
        public static void Interpolate(PackFrame start, PackFrame end, System.Object trg, float time, ref FastBitMask128 readyMask, ref int maskOffset)
        {
            var s    = start as PackFrame_TestPackObject;
            var e    = end as PackFrame_TestPackObject;
            var t    = trg as emotitron.TestPackObject;
            var mask = end.mask;

            if (readyMask[maskOffset] && mask[maskOffset])
            {
                t.rotation = (System.Single)((e.rotation - s.rotation) * time) + s.rotation;
            }
            maskOffset++;
            maskOffset++;
        }
Пример #12
0
        public static void Interpolate(PackFrame start, PackFrame end, PackFrame trg, float time, ref FastBitMask128 readyMask, ref int maskOffset)
        {
            var s    = start as PackFrame_TestPackObject;
            var e    = end as PackFrame_TestPackObject;
            var t    = trg as PackFrame_TestPackObject;
            var mask = end.mask;

            if (mask[maskOffset])
            {
                t.rotation = (System.Single)((e.rotation - s.rotation) * time) + s.rotation;
            }
            maskOffset++;
            if (mask[maskOffset])
            {
                t.intoroboto = (System.Int32)((e.intoroboto - s.intoroboto) * time) + s.intoroboto;
            }
            maskOffset++;
        }
Пример #13
0
        public static void Initialize()
        {
            if (initialized)
            {
                return;
            }
            isInitializing = true;
            int            maxBits        = 0;
            var            pObjAttr       = (typeof(emotitron.TestPackObject).GetCustomAttributes(typeof(PackObjectAttribute), false)[0] as PackObjectAttribute);
            var            defaultKeyRate = pObjAttr.defaultKeyRate;
            FastBitMask128 defReadyMask   = new FastBitMask128(TOTAL_FIELDS);
            int            fieldindex     = 0;

            SyncHalfFloatAttribute rotationPackAttr = (SyncHalfFloatAttribute)(typeof(emotitron.TestPackObject).GetField("rotation").GetCustomAttributes(typeof(SyncVarBaseAttribute), false)[0] as SyncHalfFloatAttribute);

            rotationPacker   = (rotationPackAttr as IPackSingle).Pack;
            rotationUnpacker = (rotationPackAttr as IPackSingle).Unpack;
            rotationPackAttr.Initialize(typeof(System.Single));
            if (rotationPackAttr.keyRate == KeyRate.UseDefault)
            {
                rotationPackAttr.keyRate = (KeyRate)defaultKeyRate;
            }
            if (rotationPackAttr.syncAs == SyncAs.Auto)
            {
                rotationPackAttr.syncAs = pObjAttr.syncAs;
            }
            if (rotationPackAttr.syncAs == SyncAs.Auto)
            {
                rotationPackAttr.syncAs = SyncAs.State;
            }
            if (rotationPackAttr.syncAs == SyncAs.Trigger)
            {
                defReadyMask[fieldindex] = true;
            }
            maxBits += 16; fieldindex++;

            SyncRangedIntAttribute intorobotoPackAttr = (SyncRangedIntAttribute)(typeof(emotitron.TestPackObject).GetField("intoroboto").GetCustomAttributes(typeof(SyncVarBaseAttribute), false)[0] as SyncRangedIntAttribute);

            intorobotoPacker   = (intorobotoPackAttr as IPackInt32).Pack;
            intorobotoUnpacker = (intorobotoPackAttr as IPackInt32).Unpack;
            intorobotoPackAttr.Initialize(typeof(System.Int32));
            if (intorobotoPackAttr.keyRate == KeyRate.UseDefault)
            {
                intorobotoPackAttr.keyRate = (KeyRate)defaultKeyRate;
            }
            if (intorobotoPackAttr.syncAs == SyncAs.Auto)
            {
                intorobotoPackAttr.syncAs = pObjAttr.syncAs;
            }
            if (intorobotoPackAttr.syncAs == SyncAs.Auto)
            {
                intorobotoPackAttr.syncAs = SyncAs.State;
            }
            if (intorobotoPackAttr.syncAs == SyncAs.Trigger)
            {
                defReadyMask[fieldindex] = true;
            }
            maxBits += 2; fieldindex++;

            packObjInfo = new PackObjectDatabase.PackObjectInfo(defReadyMask, Pack, Pack, Unpack, maxBits, PackFrame_TestPackObject.Factory, PackFrame_TestPackObject.Factory, PackFrame_TestPackObject.Apply, PackFrame_TestPackObject.Capture, PackFrame_TestPackObject.Copy, PackFrame_TestPackObject.SnapshotCallback, PackFrame_TestPackObject.Interpolate, PackFrame_TestPackObject.Interpolate, TOTAL_FIELDS);
            PackObjectDatabase.packObjInfoLookup.Add(typeof(emotitron.TestPackObject), packObjInfo);
            isInitializing = false;
            initialized    = true;
        }