public bool LoadFromWorldGen() { DebugUtil.LogArgs("Attempting to start a new game with current world gen"); WorldGen.LoadSettings(); string worldName; try { worldName = CustomGameSettings.Instance.GetCurrentQualitySetting(CustomGameSettingConfigs.World).id; } catch { worldName = "worlds/SandstoneDefault"; } worldGen = new WorldGen(worldName, null); SimSaveFileStructure simSaveFileStructure = worldGen.LoadWorldGenSim(); if (simSaveFileStructure == null) { Debug.LogError("Attempt failed"); return(false); } worldDetailSave = simSaveFileStructure.worldDetail; if (worldDetailSave == null) { Debug.LogError("Detail is null"); } GridSettings.Reset(simSaveFileStructure.WidthInCells, simSaveFileStructure.HeightInCells); Singleton <KBatchedAnimUpdater> .Instance.InitializeGrid(); Sim.SIM_Initialize(Sim.DLL_MessageHandler); SimMessages.CreateSimElementsTable(ElementLoader.elements); SimMessages.CreateDiseaseTable(); try { FastReader reader = new FastReader(simSaveFileStructure.Sim); if (Sim.Load(reader) != 0) { DebugUtil.LogWarningArgs("\n--- Error loading save ---\nSimDLL found bad data\n"); Sim.Shutdown(); return(false); } } catch (Exception ex) { Debug.LogWarning("--- Error loading Sim FROM NEW WORLDGEN ---" + ex.Message + "\n" + ex.StackTrace); Sim.Shutdown(); return(false); } Debug.Log("Attempt success"); SceneInitializer.Instance.PostLoadPrefabs(); SceneInitializer.Instance.NewSaveGamePrefab(); worldGen.ReplayGenerate(Reset); OnWorldGenComplete.Signal(); ThreadedHttps <KleiMetrics> .Instance.StartNewGame(); return(true); }
protected override void OnSpawn() { string activeSaveFilePath = GetActiveSaveFilePath(); if (WorldGen.CanLoad(activeSaveFilePath)) { Sim.SIM_Initialize(Sim.DLL_MessageHandler); SimMessages.CreateSimElementsTable(ElementLoader.elements); SimMessages.CreateDiseaseTable(); loadedFromSave = true; loadedFromSave = Load(activeSaveFilePath); saveFileCorrupt = !loadedFromSave; if (!loadedFromSave) { SetActiveSaveFilePath(null); if (mustRestartOnFail) { MoveCorruptFile(activeSaveFilePath); Sim.Shutdown(); App.LoadScene("frontend"); return; } } } if (!loadedFromSave) { Sim.Shutdown(); if (!string.IsNullOrEmpty(activeSaveFilePath)) { DebugUtil.LogArgs("Couldn't load [" + activeSaveFilePath + "]"); } if (saveFileCorrupt) { MoveCorruptFile(activeSaveFilePath); } bool flag = WorldGen.CanLoad(WorldGen.SIM_SAVE_FILENAME); if (!flag || !LoadFromWorldGen()) { DebugUtil.LogWarningArgs("Couldn't start new game with current world gen, moving file"); if (flag) { KMonoBehaviour.isLoadingScene = true; MoveCorruptFile(WorldGen.SIM_SAVE_FILENAME); } App.LoadScene("frontend"); } } }
public unsafe static bool DoSettleSim(WorldGenSettings settings, Sim.Cell[] cells, float[] bgTemp, Sim.DiseaseCell[] dcs, WorldGen.OfflineCallbackFunction updateProgressFn, Data data, List <KeyValuePair <Vector2I, TemplateContainer> > templateSpawnTargets, Action <OfflineWorldGen.ErrorInfo> error_cb, Action <Sim.Cell[], float[], Sim.DiseaseCell[]> onSettleComplete) { Sim.SIM_Initialize(Sim.DLL_MessageHandler); SimMessages.CreateSimElementsTable(ElementLoader.elements); SimMessages.CreateWorldGenHACKDiseaseTable(WorldGen.diseaseIds); Sim.DiseaseCell[] dc = new Sim.DiseaseCell[dcs.Length]; SimMessages.SimDataInitializeFromCells(Grid.WidthInCells, Grid.HeightInCells, cells, bgTemp, dc); int num = 500; updateProgressFn(UI.WORLDGEN.SETTLESIM.key, 0f, WorldGenProgressStages.Stages.SettleSim); Vector2I min = new Vector2I(0, 0); Vector2I max = new Vector2I(Grid.WidthInCells, Grid.HeightInCells); byte[] bytes = null; using (MemoryStream memoryStream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(memoryStream)) { try { Sim.Save(writer); } catch (Exception ex) { string message = ex.Message; string stackTrace = ex.StackTrace; WorldGenLogger.LogException(message, stackTrace); return(updateProgressFn(new StringKey("Exception in Sim Save"), -1f, WorldGenProgressStages.Stages.Failure)); } } bytes = memoryStream.ToArray(); } FastReader reader = new FastReader(bytes); if (Sim.Load(reader) != 0) { updateProgressFn(UI.WORLDGEN.FAILED.key, -1f, WorldGenProgressStages.Stages.Failure); return(true); } byte[] array = new byte[Grid.CellCount]; for (int i = 0; i < Grid.CellCount; i++) { array[i] = byte.MaxValue; } for (int j = 0; j < num; j++) { SimMessages.NewGameFrame(0.2f, min, max); IntPtr intPtr = Sim.HandleMessage(SimMessageHashes.PrepareGameData, array.Length, array); updateProgressFn(UI.WORLDGEN.SETTLESIM.key, (float)j / (float)num * 100f, WorldGenProgressStages.Stages.SettleSim); if (!(intPtr == IntPtr.Zero)) { Sim.GameDataUpdate *ptr = (Sim.GameDataUpdate *)(void *) intPtr; for (int k = 0; k < ptr->numSubstanceChangeInfo; k++) { Sim.SubstanceChangeInfo substanceChangeInfo = ptr->substanceChangeInfo[k]; int cellIdx = substanceChangeInfo.cellIdx; cells[cellIdx].elementIdx = ptr->elementIdx[cellIdx]; cells[cellIdx].insulation = ptr->insulation[cellIdx]; cells[cellIdx].properties = ptr->properties[cellIdx]; cells[cellIdx].temperature = ptr->temperature[cellIdx]; cells[cellIdx].mass = ptr->mass[cellIdx]; cells[cellIdx].strengthInfo = ptr->strengthInfo[cellIdx]; } foreach (KeyValuePair <Vector2I, TemplateContainer> templateSpawnTarget in templateSpawnTargets) { Cell templateCellData; for (int l = 0; l < templateSpawnTarget.Value.cells.Count; l++) { templateCellData = templateSpawnTarget.Value.cells[l]; Vector2I key = templateSpawnTarget.Key; int x = key.x; Vector2I key2 = templateSpawnTarget.Key; int num2 = Grid.OffsetCell(Grid.XYToCell(x, key2.y), templateCellData.location_x, templateCellData.location_y); if (Grid.IsValidCell(num2)) { cells[num2].elementIdx = (byte)ElementLoader.GetElementIndex(templateCellData.element); cells[num2].temperature = templateCellData.temperature; cells[num2].mass = templateCellData.mass; dcs[num2].diseaseIdx = (byte)WorldGen.diseaseIds.FindIndex((string name) => name == templateCellData.diseaseName); dcs[num2].elementCount = templateCellData.diseaseCount; } } } } } for (int m = 0; m < Grid.CellCount; m++) { int callbackIdx = (m != Grid.CellCount - 1) ? (-1) : 2147481337; SimMessages.ModifyCell(m, cells[m].elementIdx, cells[m].temperature, cells[m].mass, dcs[m].diseaseIdx, dcs[m].elementCount, SimMessages.ReplaceType.Replace, false, callbackIdx); } bool flag = false; while (!flag) { SimMessages.NewGameFrame(0.2f, min, max); IntPtr intPtr2 = Sim.HandleMessage(SimMessageHashes.PrepareGameData, array.Length, array); if (!(intPtr2 == IntPtr.Zero)) { Sim.GameDataUpdate *ptr2 = (Sim.GameDataUpdate *)(void *) intPtr2; for (int n = 0; n < ptr2->numCallbackInfo; n++) { Sim.CallbackInfo callbackInfo = ptr2->callbackInfo[n]; if (callbackInfo.callbackIdx == 2147481337) { flag = true; break; } } } } Sim.HandleMessage(SimMessageHashes.SettleWorldGen, 0, null); bool result = SaveSim(settings, data, error_cb); onSettleComplete(cells, bgTemp, dcs); Sim.Shutdown(); return(result); }
private bool Load(IReader reader) { string a = reader.ReadKleiString(); Debug.Assert(a == "world"); Deserializer deserializer = new Deserializer(reader); SaveFileRoot saveFileRoot = new SaveFileRoot(); deserializer.Deserialize(saveFileRoot); SaveGame.GameInfo gameInfo = GameInfo; if (gameInfo.saveMajorVersion != 7) { SaveGame.GameInfo gameInfo2 = GameInfo; if (gameInfo2.saveMinorVersion >= 8) { goto IL_00f7; } } if (saveFileRoot.requiredMods != null) { saveFileRoot.active_mods = new List <Label>(); foreach (ModInfo requiredMod in saveFileRoot.requiredMods) { ModInfo current = requiredMod; saveFileRoot.active_mods.Add(new Label { id = current.assetID, version = current.lastModifiedTime, distribution_platform = Label.DistributionPlatform.Steam, title = current.description }); } saveFileRoot.requiredMods.Clear(); } goto IL_00f7; IL_00f7: KMod.Manager modManager = Global.Instance.modManager; modManager.Load(Content.LayerableFiles); if (!modManager.MatchFootprint(saveFileRoot.active_mods, Content.LayerableFiles | Content.Strings | Content.DLL | Content.Translation | Content.Animation)) { DebugUtil.LogWarningArgs("Mod footprint of save file doesn't match current mod configuration"); } Global.Instance.modManager.SendMetricsEvent(); string text = saveFileRoot.worldID; if (text == null) { try { text = CustomGameSettings.Instance.GetCurrentQualitySetting(CustomGameSettingConfigs.World).id; } catch { text = "worlds/SandstoneDefault"; } } Game.worldID = text; worldGen = new WorldGen(text, null); Game.LoadSettings(deserializer); GridSettings.Reset(saveFileRoot.WidthInCells, saveFileRoot.HeightInCells); Singleton <KBatchedAnimUpdater> .Instance.InitializeGrid(); Sim.SIM_Initialize(Sim.DLL_MessageHandler); SimMessages.CreateSimElementsTable(ElementLoader.elements); SimMessages.CreateDiseaseTable(); byte[] bytes = saveFileRoot.streamed["Sim"]; FastReader reader2 = new FastReader(bytes); if (Sim.Load(reader2) != 0) { DebugUtil.LogWarningArgs("\n--- Error loading save ---\nSimDLL found bad data\n"); Sim.Shutdown(); return(false); } SceneInitializer.Instance.PostLoadPrefabs(); mustRestartOnFail = true; if (!saveManager.Load(reader)) { Sim.Shutdown(); DebugUtil.LogWarningArgs("\n--- Error loading save ---\n"); SetActiveSaveFilePath(null); return(false); } Grid.Visible = saveFileRoot.streamed["GridVisible"]; if (saveFileRoot.streamed.ContainsKey("GridSpawnable")) { Grid.Spawnable = saveFileRoot.streamed["GridSpawnable"]; } Grid.Damage = BytesToFloat(saveFileRoot.streamed["GridDamage"]); Game.Instance.Load(deserializer); FastReader reader3 = new FastReader(saveFileRoot.streamed["Camera"]); CameraSaveData.Load(reader3); return(true); }