-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSaveLoadData.cs
More file actions
127 lines (99 loc) · 3.91 KB
/
SaveLoadData.cs
File metadata and controls
127 lines (99 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;
using System.Threading;
public class SaveLoadData
{
public SaveChunk currentChunk;
public MapGenerator mapGenerator;
public SaveLoadData()
{
currentChunk = null;
}
public SaveLoadData(SaveChunk currentChunk)
{
this.currentChunk = currentChunk;
}
//------------CHUNK SAVING AND LOADING--------------
public static void SaveChunk(string persistentDataPath, int x, int y, SaveChunk chunkInfo, string worldName)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file;
if (File.Exists(persistentDataPath + "/Worlds/" + worldName + "/chunk" + x + "," + y + ".dat"))
{
file = File.Open(persistentDataPath + "/Worlds/" + worldName + "/chunk" + x + "," + y + ".dat", FileMode.Open);
}
else
{
file = File.Create(persistentDataPath + "/Worlds/" + worldName + "/chunk" + x + "," + y + ".dat");
}
bf.Serialize(file, chunkInfo);
file.Close();
}
public Chunk LoadChunk(string persistentDataPath, float x, float y, string worldName)
{
try
{
if (File.Exists(persistentDataPath + "/Worlds/" + worldName + "/chunk" + x + "," + y + ".dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file;
file = File.Open(persistentDataPath + "/Worlds/" + worldName + "/chunk" + x + "," + y + ".dat", FileMode.Open);
SaveChunk chunkData = (SaveChunk)bf.Deserialize(file);
file.Close();
return (Chunk)chunkData;
}
else
{
Debug.LogError("Chunk " + x + " " + y + " Not Found");
return null;
}
}
catch(IOException ex)
{
Debug.Log(ex.StackTrace);
mapGenerator.chunkThread.Abort();
return null;
}
}
//------------WORLD SAVING--------------
public static void SaveWorld(int seed, int octaves, int frequency, float persistance, float scale, string worldName)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file;
Debug.Log("Creating New World!");
//Creates new Worlds directory if one is not there already
if (!Directory.Exists(Application.persistentDataPath + "/Worlds"))
{
Debug.Log("Worlds directory missing - creating new folder");
Directory.CreateDirectory(Application.persistentDataPath + "/Worlds");
Debug.Log("<color=green>Worlds directory created!</color>");
}
file = File.Create(Application.persistentDataPath + "/Worlds/" + worldName + ".dat");
Debug.Log("<color=green>Created New World!</color>");
SaveWorld worldData = new SaveWorld(worldName, seed, octaves, frequency, persistance, scale);
bf.Serialize(file, worldData);
file.Close();
Debug.Log("<color=green>World Successfully Saved!</color>");
}
public static SaveWorld LoadWorld(string worldName)
{
if (File.Exists(Application.persistentDataPath + "/Worlds/" + worldName + ".dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file;
file = File.Open(Application.persistentDataPath + "/Worlds/" + worldName + ".dat", FileMode.Open);
SaveWorld worldData = (SaveWorld)bf.Deserialize(file);
file.Close();
Debug.Log("<color=green>Loaded World!</color>");
return worldData;
}
else
{
Debug.Log("World " + worldName + " not found");
return null;
}
}
}