public static void SaveWorld(string saveName) { currentSaveName = saveName; World = ConstructLoadedWorld(); string filePath = Application.persistentDataPath + "/Worlds/" + saveName; Directory.CreateDirectory(filePath); Directory.CreateDirectory(filePath + "/Players"); Directory.CreateDirectory(filePath + "/Objects"); for (int i = 0; i < GameManager.singleton.Players.Count; i++) { File.WriteAllLines(filePath + "/Players/" + GameManager.singleton.Players[i].GetComponent <GameProfile>().ProfileName, World.Players[i]); } for (int x = 0; x < GenerationManager.currentGeneration.size.x; x++) { for (int y = 0; y < GenerationManager.currentGeneration.size.y; y++) { if (World.Objects[x, y] != null) { string roomPath = filePath + "/Objects/" + x + "x" + y; Directory.CreateDirectory(roomPath); foreach (LoadedWorld.LoadedObject loadedObject in World.Objects[x, y]) { int index = 0; while (File.Exists(roomPath + "/" + loadedObject.AssetID + "_" + index)) { index++; } File.WriteAllLines(roomPath + "/" + loadedObject.AssetID + "_" + index, loadedObject.Data); } } } } File.WriteAllLines(filePath + "/worldInfo", new [] { GenerationManager.currentGeneration.Seed + "", GameManager.singleton.seedToSpawn + "" // TODO: Добавить сохранение индекса уровня }); if (File.Exists(filePath + postfix)) { File.Delete(filePath + postfix); } ZipFile.CreateFromDirectory(filePath, filePath + postfix); Directory.Delete(filePath, true); }
public static void LoadWorld(string saveName) { currentSaveName = saveName; string filePath = Application.persistentDataPath + "/Worlds/" + saveName; if (!File.Exists(filePath + postfix)) { return; } ZipFile.ExtractToDirectory(filePath + postfix, filePath); string[] playerFiles = Directory.GetFiles(filePath + "/Players"); List <List <string> > players = playerFiles.Select(file => File.ReadAllLines(file).ToList()).ToList(); List <LoadedWorld.LoadedObject>[,] loadedObjects = new List <LoadedWorld.LoadedObject> [GameManager.DefGenerationSize.x, GameManager.DefGenerationSize.y]; string[] roomDirectories = Directory.GetDirectories(filePath + "/Objects"); foreach (string roomDirectory in roomDirectories) { string[] splitted = roomDirectory.Split('\\')[1].Split('x'); Vector2Int roomPosition = new Vector2Int(int.Parse(splitted[0]), int.Parse(splitted[1])); loadedObjects[roomPosition.x, roomPosition.y] = new List <LoadedWorld.LoadedObject>(); string[] objectFiles = Directory.GetFiles(roomDirectory); foreach (string objectFile in objectFiles) { loadedObjects[roomPosition.x, roomPosition.y].Add( new LoadedWorld.LoadedObject( objectFile.Split('\\')[2].Split('_')[0], File.ReadAllLines(objectFile).ToList() ) ); } } string[] worldInfo = File.ReadAllLines(filePath + "/worldInfo"); int seedToGeneration = int.Parse(worldInfo[0]); int seedToSpawn = int.Parse(worldInfo[1]); Directory.Delete(filePath, true); World = new LoadedWorld(players, loadedObjects, seedToGeneration, seedToSpawn, 0); }