Esempio n. 1
0
        public static bool Prefix(ref int ___ticksGameInt, ref TickManager __instance)
        {
            Rand.PushState(___ticksGameInt);
            getValuePatch.SendDiagDataToServer();
            getValuePatch.diagData.Clear();
            CooperateRimming.dumpRand = true;

            if (sw == null)
            {
                sw    = new Stopwatch();
                ACKSW = new Stopwatch();
                sw.Start();
                ACKSW.Start();

                /*fix for newly loaded game*/
                TickManagerPatch.nextCommunicationTick = Verse.Find.TickManager.TicksGame;
                TickManagerPatch.nextProcessionTick    = Verse.Find.TickManager.TicksGame;

                if (!imInSync)
                {
                    if (nextCommunicationTick == Verse.Find.TickManager.TicksGame)
                    {
                        imInSync = SyncTickData.FlushSyncTickData(nextProcessionTick + syncRoundLength * syncTickRoundOffset);
                    }
                }

                if (cachedData == null)
                {
                    return(false);
                }
                //for (; cachedData == null;)
                {
                }
            }
            shouldReallyTick = false;

            if (NetDemo.HasAllDataForFrame(Verse.Find.TickManager.TicksGame))
            {
                NetDemo.Receive();
            }

            if (sw.ElapsedMilliseconds > (1000 / ticksPerSecond) && !__instance.Paused)
            {
                sw.Reset();
                sw.Start();
                bool canNormallyTick = nextProcessionTick > Verse.Find.TickManager.TicksGame;

                //RimLog.Message("Tick " + ___ticksGameInt + " canNormallyTick " + canNormallyTick + "| comm " + nextCommunicationTick + " | " + nextProcessionTick);

                if (canNormallyTick)
                {
                    //Utilities.RimLog.Message("normal tick at " + Verse.Find.TickManager.TicksGame + " nsync " + nextSyncTickValue);
                    ulong i = 0;
                    foreach (var act in RandContextCounter.replacement_seed)
                    {
                        act((ulong)___ticksGameInt + (i++));
                    }
                    __instance.DoSingleTick();
                }

                if (SyncTickData.cliendID > -1 && ACKSW.ElapsedMilliseconds > 50)
                {
                    ACKSW.Reset();
                    ACKSW.Start();
                    //RimLog.Message("Sending state request for " + nextSyncTickValue);
                    //NetDemo.SendStateRequest(nextSyncTickValue, SyncTickData.cliendID);
                }


                if (!imInSync)
                {
                    if (nextCommunicationTick == Verse.Find.TickManager.TicksGame)
                    {
                        imInSync = SyncTickData.FlushSyncTickData(nextProcessionTick + syncRoundLength * syncTickRoundOffset);
                    }
                }

                {
                    if (nextProcessionTick == Verse.Find.TickManager.TicksGame)
                    {
#if FILE_TRANSFER
                        bool allSyncDataAvailable = SyncTickData.tickFileNames(___ticksGameInt).All(u => System.IO.File.Exists(u + ".sync"));
#else
#endif

                        //RimLog.Message("Frame " + ___ticksGameInt + " : " + " ::: " + allSyncDataAvailable + "[" + ___ticksGameInt + "] :: " + nextSyncTickValue + " [is synced : ] " + imInSync);

                        Action onA = LocalDB.OnApply;

                        if (cachedData != null)
                        {
                            IsSyncTick = true;

                            //RimLog.Message("Synctick happened at " + ___ticksGameInt);

                            SyncTickData.IsDeserializing = true;
                            //JobTrackerPatch.FlushCData();
                            shouldReallyTick = true;
                            streamholder.WriteLine("pre-deserialize tick at " + Verse.Find.TickManager.TicksGame, "tickstate");
                            streamholder.WriteLine("data applied at " + Verse.Find.TickManager.TicksGame, "tickstate");

                            //lock (LocalDB.OnApply)
                            {
                                Rand.PushState(___ticksGameInt);
                                try
                                {
                                    //RimLog.Message("applying at tick " + Verse.Find.TickManager.TicksGame);

                                    var cd = cachedData;
                                    cachedData = null;

                                    ulong i = 0;
                                    foreach (var act in RandContextCounter.replacement_seed)
                                    {
                                        act((ulong)___ticksGameInt + (i++));
                                    }

                                    foreach (var a in cd)
                                    {
                                        a.AcceptResult();
                                    }

                                    TickManagerPatch.SetSyncTickValue(nextProcessionTick + syncRoundLength);
                                    nextCommunicationTick += syncRoundLength;
                                    //Interlocked.Exchange(ref LocalDB.OnApply, null);
                                    //LocalDB.clientLocalStorage.ForEach(u => u.Value.AcceptResult());

                                    //nextSyncTickValue = Verse.Find.TickManager.TicksGame + syncRoundLength;
                                }
                                catch (Exception ee)
                                {
                                    RimLog.Message(ee.ToString());
                                }
                                Rand.PopState();

                                LocalDB.clientLocalStorage.Clear();
                            }
                            //SyncTickData.Apply(___ticksGameInt);
                            //__instance.DoSingleTick();

                            imInSync = false;
                        }
                    }
                }
            }

            //ReferenceTranspilerMethod(ref ___ticksGameInt);
            Rand.PopState();
            return(false);
        }