// This saves the current game to disk. Will overwrite any previous saves public static void Save(int num = 0, bool quit = false) { // wait to not overwrite save.ini and screen capture ZipManager.Wait4PreviousSave(); Game.Get().cc.TakeScreenshot(delegate { SaveWithScreen(num, quit); }); }
private static void SaveWithScreen(int num, bool quit = false) { Game game = Game.Get(); try { if (!Directory.Exists(Game.AppData())) { Directory.CreateDirectory(Game.AppData()); } if (!Directory.Exists(ContentData.GameTypePath)) { Directory.CreateDirectory(ContentData.GameTypePath); } if (!Directory.Exists(Path.Combine(ContentData.GameTypePath, "Save"))) { Directory.CreateDirectory(Path.Combine(ContentData.GameTypePath, "Save")); } string tempValkyriePath = ContentData.TempValyriePath; if (!Directory.Exists(tempValkyriePath)) { Directory.CreateDirectory(tempValkyriePath); } File.WriteAllText(Path.Combine(tempValkyriePath, "save.ini"), game.quest.ToString()); Vector2 screenSize = new Vector2(Screen.width, Screen.height); float scale = 4f / 30f; var targetSizeX = Mathf.RoundToInt(screenSize.x * scale); var targetSizeY = Mathf.RoundToInt(screenSize.y * scale); var outTex = ResizeScreenShotTexture(game.cc.screenShot, targetSizeX, targetSizeY); File.WriteAllBytes(Path.Combine(tempValkyriePath, "image.png"), outTex.EncodeToPNG()); // Check if we should update the zip file or write a new one with quest content // first autosave is a new zip file, following autosave just update the zip bool zip_update = false; if (num == 0 && game.quest.firstAutoSaveDone) { zip_update = true; } else if (num == 0) { game.quest.firstAutoSaveDone = true; } // Quest content can be in original path, or savegame path string quest_content_path; if (game.quest.fromSavegame) { quest_content_path = ContentData.ValkyrieLoadQuestPath; } else { quest_content_path = game.quest.originalPath; } // zip in a separate thread ZipManager.WriteZipAsync(tempValkyriePath, quest_content_path, SaveFile(num), zip_update); } catch (IOException e) { ValkyrieDebug.Log("Warning: Unable to write to save file. " + e.Message); } if (quit) { ZipManager.Wait4PreviousSave(); GameStateManager.MainMenu(); } }
private static void SaveWithScreen(int num, bool quit = false) { Game game = Game.Get(); try { if (!Directory.Exists(Game.AppData())) { Directory.CreateDirectory(Game.AppData()); } if (!Directory.Exists(ContentData.GameTypePath)) { Directory.CreateDirectory(ContentData.GameTypePath); } if (!Directory.Exists(Path.Combine(ContentData.GameTypePath, "Save"))) { Directory.CreateDirectory(Path.Combine(ContentData.GameTypePath, "Save")); } string tempValkyriePath = ContentData.TempValyriePath; if (!Directory.Exists(tempValkyriePath)) { Directory.CreateDirectory(tempValkyriePath); } File.WriteAllText(Path.Combine(tempValkyriePath, "save.ini"), game.quest.ToString()); Vector2 screenSize = new Vector2(Screen.width, Screen.height); Color[] screenColor = game.cc.screenShot.GetPixels(0); float scale = 4f / 30f; Texture2D outTex = new Texture2D(Mathf.RoundToInt(screenSize.x * scale), Mathf.RoundToInt(screenSize.y * scale), TextureFormat.RGB24, false); Color[] outColor = new Color[outTex.width * outTex.height]; for (int i = 0; i < outColor.Length; i++) { float xX = (float)i % (float)outTex.width; float xY = Mathf.Floor((float)i / (float)outTex.width); Vector2 vCenter = new Vector2(xX, xY) / scale; int xXFrom = (int)Mathf.Max(Mathf.Floor(vCenter.x - (0.5f / scale)), 0); int xXTo = (int)Mathf.Min(Mathf.Ceil(vCenter.x + (0.5f / scale)), screenSize.x); int xYFrom = (int)Mathf.Max(Mathf.Floor(vCenter.y - (0.5f / scale)), 0); int xYTo = (int)Mathf.Min(Mathf.Ceil(vCenter.y + (0.5f / scale)), screenSize.y); Color oColorTemp = new Color(); float xGridCount = 0; for (int iy = xYFrom; iy < xYTo; iy++) { for (int ix = xXFrom; ix < xXTo; ix++) { int index = (int)(((float)iy * screenSize.x) + ix); if (index >= screenColor.Length || index < 0) { continue; } oColorTemp += screenColor[index]; xGridCount++; } } outColor[i] = oColorTemp / (float)xGridCount; } outTex.SetPixels(outColor); outTex.Apply(); File.WriteAllBytes(Path.Combine(tempValkyriePath, "image.png"), outTex.EncodeToPNG()); // Check if we should update the zip file or write a new one with quest content // first autosave is a new zip file, following autosave just update the zip bool zip_update = false; if (num == 0 && game.quest.firstAutoSaveDone) { zip_update = true; } else if (num == 0) { game.quest.firstAutoSaveDone = true; } // Quest content can be in original path, or savegame path string quest_content_path; if (game.quest.fromSavegame) { quest_content_path = ContentData.ValkyrieLoadQuestPath; } else { quest_content_path = game.quest.originalPath; } // zip in a separate thread ZipManager.WriteZipAsync(tempValkyriePath, quest_content_path, SaveFile(num), zip_update); } catch (System.IO.IOException e) { ValkyrieDebug.Log("Warning: Unable to write to save file. " + e.Message); } if (quit) { ZipManager.Wait4PreviousSave(); Destroyer.MainMenu(); } }