public bool LoadGame(string fullname)
    {
        bool   debug_noresource = weNeedNoResources, refreshUI = false;
        Stream fs = File.Open(fullname, FileMode.Open);
        double realHashSum = GetHashSum(fs, true);
        var    data        = new byte[8];

        fs.Read(data, 0, 8);
        double readedHashSum = System.BitConverter.ToDouble(data, 0);
        string errorReason   = "reason not stated";

        if (realHashSum == readedHashSum)
        {
            fs.Position = 0;
            SetPause(true);
            loading = true;


            // НАЧАЛО ЗАГРУЗКИ
            #region gms mainPartLoading
            data = new byte[5];
            fs.Read(data, 0, data.Length);
            int saveSystemVersion = System.BitConverter.ToInt32(data, 0); // может пригодиться в дальнейшем
            gameMode = (GameMode)data[4];
            //
            if (sessionPrepared)
            {
                ClearPreviousSessionData();
                refreshUI = true;
            }
            else
            {
                PrepareSession();
            }
            //start reading
            data = new byte[68];
            fs.Read(data, 0, data.Length);
            gameSpeed                      = System.BitConverter.ToSingle(data, 0);
            lifeGrowCoefficient            = System.BitConverter.ToSingle(data, 4);
            demolitionLossesPercent        = System.BitConverter.ToSingle(data, 8);
            lifepowerLossesPercent         = System.BitConverter.ToSingle(data, 12);
            LUCK_COEFFICIENT               = System.BitConverter.ToSingle(data, 16);
            sellPriceCoefficient           = System.BitConverter.ToSingle(data, 20);
            tradeVesselsTrafficCoefficient = System.BitConverter.ToSingle(data, 24);
            upgradeDiscount                = System.BitConverter.ToSingle(data, 28);
            upgradeCostIncrease            = System.BitConverter.ToSingle(data, 32);
            warProximity                   = System.BitConverter.ToSingle(data, 36);

            difficulty = (Difficulty)data[40];
            int i = 41;
            prevCutHeight = data[i++];
            day           = data[i++];
            month         = data[i++];

            year              = System.BitConverter.ToUInt32(data, i); i += 4;
            timeGone          = System.BitConverter.ToSingle(data, i); i += 4;
            gearsDegradeSpeed = System.BitConverter.ToSingle(data, i); i += 4;
            labourTimer       = System.BitConverter.ToSingle(data, i); i += 4;
            RecruitingCenter.SetHireCost(System.BitConverter.ToSingle(data, i)); i += 4;
            gameID = System.BitConverter.ToInt32(data, i); i += 4;
            #endregion

            DockSystem.LoadDockSystem(fs, saveSystemVersion);
            var b = fs.ReadByte();
            if (b == 1)
            {
                globalMap.Load(fs, saveSystemVersion);
            }
            if (loadingFailed)
            {
                errorReason = "global map error";
                goto FAIL;
            }

            environmentMaster.Load(fs);
            if (loadingFailed)
            {
                errorReason = "environment error";
                goto FAIL;
            }

            Artifact.LoadStaticData(fs); // crews & monuments
            if (loadingFailed)
            {
                errorReason = "artifacts load failure";
                goto FAIL;
            }

            Crew.LoadStaticData(fs);
            if (loadingFailed)
            {
                errorReason = "crews load failure";
                goto FAIL;
            }
            //
            if (colonyController == null)
            {
                PrepareColonyController(false);
            }
            //
            if (mainChunk == null)
            {
                mainChunk = Chunk.InitializeChunk();
            }
            mainChunk.LoadChunkData(fs);
            if (loadingFailed)
            {
                errorReason = "chunk load failure";
                goto FAIL;
            }
            else
            {
                if (blockersRestoreEvent != null)
                {
                    blockersRestoreEvent();
                }
            }

            Settlement.TotalRecalculation(); // Totaru Annihiration no imoto-chan
            if (loadingFailed)
            {
                errorReason = "settlements load failure";
                goto FAIL;
            }

            colonyController.Load(fs); // < --- COLONY CONTROLLER
            if (loadingFailed)
            {
                errorReason = "colony controller load failure";
                goto FAIL;
            }

            if (loadingFailed)
            {
                errorReason = "dock load failure";
                goto FAIL;
            }
            QuestUI.current.Load(fs);
            if (loadingFailed)
            {
                errorReason = "quest load failure";
                goto FAIL;
            }
            Expedition.LoadStaticData(fs);
            Knowledge.Load(fs);
            b = fs.ReadByte();
            if (b == 1)
            {
                executingScenario = Scenario.StaticLoad(fs);
            }
            FollowingCamera.main.Load(fs);
            fs.Close();

            FollowingCamera.main.WeNeedUpdate();
            loading         = false;
            currentSavename = fullname;

            //Debug.Log("recalculation event");
            if (afterloadRecalculationEvent != null)
            {
                afterloadRecalculationEvent();
                afterloadRecalculationEvent = null;
            }
            SetPause(false);
            //Debug.Log("power grid");
            colonyController.FORCED_PowerGridRecalculation();
            //Debug.Log("docks");
            colonyController.SYSTEM_DocksRecalculation();
            //Debug.Log("end");

            if (refreshUI)
            {
                UIController.GetCurrent().GameWasReloaded();
            }

            DEBUG_STOP = true;
            return(true);
        }
        else
        {
            AnnouncementCanvasController.MakeImportantAnnounce(Localization.GetAnnouncementString(GameAnnouncements.LoadingFailed) + " : hashsum incorrect");
            if (soundEnabled)
            {
                audiomaster.Notify(NotificationSound.SystemError);
            }
            SetPause(true);
            fs.Close();
            return(false);
        }
FAIL:
        AnnouncementCanvasController.MakeImportantAnnounce(Localization.GetAnnouncementString(GameAnnouncements.LoadingFailed) + " : data corruption");
        if (soundEnabled)
        {
            audiomaster.Notify(NotificationSound.SystemError);
        }
        Debug.Log(errorReason);
        SetPause(true);
        fs.Close();
        if (debug_noresource)
        {
            weNeedNoResources = true;
        }
        return(false);
    }
Exemple #2
0
    public bool LoadGame(string fullname)
    {
        bool       debug_noresource = weNeedNoResources;
        FileStream fs          = File.Open(fullname, FileMode.Open);
        double     realHashSum = GetHashSum(fs, true);
        var        data        = new byte[8];

        fs.Read(data, 0, 8);
        double readedHashSum = System.BitConverter.ToDouble(data, 0);
        string errorReason   = "reason not stated";

        if (realHashSum == readedHashSum)
        {
            fs.Position = 0;
            SetPause(true);
            loading = true;
            // ОЧИСТКА
            StopAllCoroutines();
            if (Zeppelin.current != null)
            {
                Destroy(Zeppelin.current);
            }
            if (mainChunk != null)
            {
                mainChunk.ClearChunk();
            }
            // очистка подписчиков на ивенты невозможна, сами ивенты к этому моменту недоступны
            Crew.Reset();
            Expedition.GameReset();
            Structure.ResetToDefaults_Static(); // все наследуемые resetToDefaults внутри
            if (colonyController != null)
            {
                colonyController.ResetToDefaults();                           // подчищает все списки
            }
            else
            {
                colonyController = gameObject.AddComponent <ColonyController>();
                colonyController.Prepare();
            }
            //UI.current.Reset();


            // НАЧАЛО ЗАГРУЗКИ
            if (gameStarted)
            {
                SetDefaultValues();
            }
            #region gms mainPartLoading
            data = new byte[4];
            fs.Read(data, 0, 4);
            uint saveSystemVersion = System.BitConverter.ToUInt32(data, 0); // может пригодиться в дальнейшем
            //start writing
            data = new byte[65];
            fs.Read(data, 0, data.Length);
            gameSpeed                      = System.BitConverter.ToSingle(data, 0);
            lifeGrowCoefficient            = System.BitConverter.ToSingle(data, 4);
            demolitionLossesPercent        = System.BitConverter.ToSingle(data, 8);
            lifepowerLossesPercent         = System.BitConverter.ToSingle(data, 12);
            LUCK_COEFFICIENT               = System.BitConverter.ToSingle(data, 16);
            sellPriceCoefficient           = System.BitConverter.ToSingle(data, 20);
            tradeVesselsTrafficCoefficient = System.BitConverter.ToSingle(data, 24);
            upgradeDiscount                = System.BitConverter.ToSingle(data, 28);
            upgradeCostIncrease            = System.BitConverter.ToSingle(data, 32);
            warProximity                   = System.BitConverter.ToSingle(data, 36);

            difficulty    = (Difficulty)data[40];
            startGameWith = (GameStart)data[41];
            prevCutHeight = data[42];
            day           = data[43];
            month         = data[44];

            year              = System.BitConverter.ToUInt32(data, 45);
            timeGone          = System.BitConverter.ToSingle(data, 49);
            gearsDegradeSpeed = System.BitConverter.ToSingle(data, 53);
            labourTimer       = System.BitConverter.ToSingle(data, 57);
            RecruitingCenter.SetHireCost(System.BitConverter.ToSingle(data, 61));
            #endregion

            DockSystem.LoadDockSystem(fs);
            globalMap.Load(fs);
            if (loadingFailed)
            {
                errorReason = "global map error";
                goto FAIL;
            }

            environmentMaster.Load(fs);
            if (loadingFailed)
            {
                errorReason = "environment error";
                goto FAIL;
            }

            Artifact.LoadStaticData(fs); // crews & monuments
            if (loadingFailed)
            {
                errorReason = "artifacts load failure";
                goto FAIL;
            }

            Crew.LoadStaticData(fs);
            if (loadingFailed)
            {
                errorReason = "crews load failure";
                goto FAIL;
            }

            if (mainChunk == null)
            {
                GameObject g = new GameObject("chunk");
                mainChunk = g.AddComponent <Chunk>();
            }
            mainChunk.LoadChunkData(fs);
            if (loadingFailed)
            {
                errorReason = "chunk load failure";
                goto FAIL;
            }
            else
            {
                if (blockersRestoreEvent != null)
                {
                    blockersRestoreEvent();
                }
            }


            Settlement.TotalRecalculation(); // Totaru Annihirationu no imoto-chan
            if (loadingFailed)
            {
                errorReason = "settlements load failure";
                goto FAIL;
            }

            colonyController.Load(fs); // < --- COLONY CONTROLLER
            if (loadingFailed)
            {
                errorReason = "colony controller load failure";
                goto FAIL;
            }

            if (loadingFailed)
            {
                errorReason = "dock load failure";
                goto FAIL;
            }
            QuestUI.current.Load(fs);
            if (loadingFailed)
            {
                errorReason = "quest load failure";
                goto FAIL;
            }
            Expedition.LoadStaticData(fs);
            Knowledge.Load(fs);
            fs.Close();

            FollowingCamera.main.WeNeedUpdate();
            loading  = false;
            savename = fullname;

            if (afterloadRecalculationEvent != null)
            {
                afterloadRecalculationEvent();
                afterloadRecalculationEvent = null;
            }
            SetPause(false);
            colonyController.FORCED_PowerGridRecalculation();

            return(true);
        }
        else
        {
            GameLogUI.MakeImportantAnnounce(Localization.GetAnnouncementString(GameAnnouncements.LoadingFailed) + " : hashsum incorrect");
            if (soundEnabled)
            {
                audiomaster.Notify(NotificationSound.SystemError);
            }
            SetPause(true);
            fs.Close();
            return(false);
        }
FAIL:
        GameLogUI.MakeImportantAnnounce(Localization.GetAnnouncementString(GameAnnouncements.LoadingFailed) + " : data corruption");
        if (soundEnabled)
        {
            audiomaster.Notify(NotificationSound.SystemError);
        }
        print(errorReason);
        SetPause(true);
        fs.Close();
        if (debug_noresource)
        {
            weNeedNoResources = true;
        }
        return(false);
    }