public static Tuple <bool, int> LoadChunk(string ToLoad) //Doesn't actually have to be a single chunk { SavedChunk LoadedChunk; try { LoadedChunk = Newtonsoft.Json.JsonConvert.DeserializeObject <SavedChunk>(ToLoad); } catch (Newtonsoft.Json.JsonReaderException) { return(new Tuple <bool, int>(false, 0)); } int PlaceCount = 0; foreach (SavedTile SavedBranch in LoadedChunk.S) { Tuple <Items.ID, Vector3, Vector3> Info = SavedBranch.GetInfoOrNull(); if (Info != null) { Place(Info.Item1, Info.Item2, Info.Item3, 1); PlaceCount++; } } return(new Tuple <bool, int>(true, PlaceCount)); }
public static void LoadWorld(string SaveName) { Directory SaveDir = new Directory(); if (SaveDir.DirExists("user://saves/" + SaveName)) { Chunks.Clear(); RemoteLoadedChunks.Clear(); foreach (Node Branch in Game.StructureRoot.GetChildren()) { Branch.QueueFree(); } SaveDir.Open("user://saves/" + SaveName); SaveDir.ListDirBegin(true, true); int PlaceCount = 0; while (true) { string FileName = SaveDir.GetNext(); if (FileName.Empty()) { //Iterated through all files break; } string LoadedFile = System.IO.File.ReadAllText($"{OS.GetUserDataDir()}/saves/{SaveName}/{FileName}"); SavedChunk LoadedChunk; try { LoadedChunk = Newtonsoft.Json.JsonConvert.DeserializeObject <SavedChunk>(LoadedFile); } catch (Newtonsoft.Json.JsonReaderException e) { Console.Log($"ERROR: Invalid chunk file {FileName} loading save '{SaveName}'"); continue; } foreach (SavedStructure SavedBranch in LoadedChunk.S) { Tuple <Items.TYPE, Vector3, Vector3> Info = SavedBranch.GetInfoOrNull(); if (Info != null) { Place(Info.Item1, Info.Item2, Info.Item3, 0); PlaceCount++; } } } Console.Print($"Loaded {PlaceCount.ToString()} structures from save '{SaveName}'"); } else { Console.Print($"ERROR: Save '{SaveName}' does not exist"); } }