Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WorldFileData.cs
Go to the documentation of this file.
1using System;
3using System.IO;
9
10namespace Terraria.IO;
11
12public class WorldFileData : FileData
13{
14 private const ulong GUID_IN_WORLD_FILE_VERSION = 777389080577uL;
15
17
18 public int WorldSizeX;
19
20 public int WorldSizeY;
21
23
24 private string _seedText = "";
25
26 private int _seed;
27
28 public bool IsValid = true;
29
30 public Guid UniqueId;
31
33
34 public int GameMode;
35
36 public bool DrunkWorld;
37
38 public bool NotTheBees;
39
40 public bool ForTheWorthy;
41
42 public bool Anniversary;
43
44 public bool DontStarve;
45
46 public bool RemixWorld;
47
48 public bool NoTrapsWorld;
49
50 public bool ZenithWorld;
51
52 public bool HasCorruption = true;
53
54 public bool IsHardMode;
55
56 public bool DefeatedMoonlord;
57
58 public string SeedText => _seedText;
59
60 public int Seed => _seed;
61
63
64 public bool HasCrimson
65 {
66 get
67 {
68 return !HasCorruption;
69 }
70 set
71 {
73 }
74 }
75
77
78 public bool UseGuidAsMapName => WorldGeneratorVersion >= 777389080577L;
79
80 public string GetWorldName(bool allowCropping = false)
81 {
82 string text = Name;
83 if (text == null)
84 {
85 return text;
86 }
87 if (allowCropping)
88 {
89 int num = 530;
90 text = FontAssets.MouseText.Value.CreateCroppedText(text, (float)num);
91 }
92 return text;
93 }
94
95 public string GetFullSeedText(bool allowCropping = false)
96 {
97 int num = 0;
98 if (WorldSizeX == 4200 && WorldSizeY == 1200)
99 {
100 num = 1;
101 }
102 if (WorldSizeX == 6400 && WorldSizeY == 1800)
103 {
104 num = 2;
105 }
106 if (WorldSizeX == 8400 && WorldSizeY == 2400)
107 {
108 num = 3;
109 }
110 int num2 = 0;
111 if (HasCorruption)
112 {
113 num2 = 1;
114 }
115 if (HasCrimson)
116 {
117 num2 = 2;
118 }
119 int num3 = GameMode + 1;
120 string text = _seedText;
121 if (allowCropping)
122 {
123 int num4 = 340;
124 text = FontAssets.MouseText.Value.CreateCroppedText(text, (float)num4);
125 }
126 return $"{num}.{num3}.{num2}.{text}";
127 }
128
130 : base("World")
131 {
132 }
133
134 public WorldFileData(string path, bool cloudSave)
135 : base("World", path, cloudSave)
136 {
137 }
138
139 public override void SetAsActive()
140 {
141 Main.ActiveWorldFileData = this;
142 }
143
144 public void SetWorldSize(int x, int y)
145 {
146 WorldSizeX = x;
147 WorldSizeY = y;
148 switch (x)
149 {
150 case 4200:
151 _worldSizeName = Language.GetText("UI.WorldSizeSmall");
152 break;
153 case 6400:
154 _worldSizeName = Language.GetText("UI.WorldSizeMedium");
155 break;
156 case 8400:
157 _worldSizeName = Language.GetText("UI.WorldSizeLarge");
158 break;
159 default:
160 _worldSizeName = Language.GetText("UI.WorldSizeUnknown");
161 break;
162 }
163 }
164
165 public static WorldFileData FromInvalidWorld(string path, bool cloudSave)
166 {
167 WorldFileData worldFileData = new WorldFileData(path, cloudSave);
168 worldFileData.GameMode = 0;
169 worldFileData.SetSeedToEmpty();
170 worldFileData.WorldGeneratorVersion = 0uL;
171 worldFileData.Metadata = FileMetadata.FromCurrentSettings(FileType.World);
172 worldFileData.SetWorldSize(1, 1);
173 worldFileData.HasCorruption = true;
174 worldFileData.IsHardMode = false;
175 worldFileData.IsValid = false;
176 worldFileData.Name = FileUtilities.GetFileName(path, includeExtension: false);
177 worldFileData.UniqueId = Guid.Empty;
178 if (!cloudSave)
179 {
180 worldFileData.CreationTime = File.GetCreationTime(path);
181 }
182 else
183 {
184 worldFileData.CreationTime = DateTime.Now;
185 }
186 return worldFileData;
187 }
188
189 public void SetSeedToEmpty()
190 {
191 SetSeed("");
192 }
193
194 public void SetSeed(string seedText)
195 {
196 _seedText = seedText;
197 WorldGen.currentWorldSeed = seedText;
198 if (!int.TryParse(seedText, out _seed))
199 {
200 _seed = Crc32.Calculate(seedText);
201 }
202 _seed = ((_seed == int.MinValue) ? int.MaxValue : Math.Abs(_seed));
203 }
204
205 public void SetSeedToRandom()
206 {
207 SetSeed(new UnifiedRandom().Next().ToString());
208 }
209
210 public override void MoveToCloud()
211 {
212 if (!base.IsCloudSave)
213 {
214 string worldPathFromName = Main.GetWorldPathFromName(Name, cloudSave: true);
215 if (FileUtilities.MoveToCloud(base.Path, worldPathFromName))
216 {
217 Main.LocalFavoriteData.ClearEntry(this);
218 _isCloudSave = true;
219 _path = worldPathFromName;
220 Main.CloudFavoritesData.SaveFavorite(this);
221 }
222 }
223 }
224
225 public override void MoveToLocal()
226 {
227 if (base.IsCloudSave)
228 {
229 string worldPathFromName = Main.GetWorldPathFromName(Name, cloudSave: false);
230 if (FileUtilities.MoveToLocal(base.Path, worldPathFromName))
231 {
232 Main.CloudFavoritesData.ClearEntry(this);
233 _isCloudSave = false;
234 _path = worldPathFromName;
235 Main.LocalFavoriteData.SaveFavorite(this);
236 }
237 }
238 }
239
240 public void Rename(string newDisplayName)
241 {
242 if (newDisplayName != null)
243 {
244 WorldGen.RenameWorld(this, newDisplayName, OnWorldRenameSuccess);
245 }
246 }
247
248 public void CopyToLocal(string newFileName = null, string newDisplayName = null)
249 {
250 if (!base.IsCloudSave)
251 {
252 if (newFileName == null)
253 {
254 newFileName = Guid.NewGuid().ToString();
255 }
256 string worldPathFromName = Main.GetWorldPathFromName(newFileName, cloudSave: false);
257 FileUtilities.Copy(base.Path, worldPathFromName, cloud: false);
258 _path = worldPathFromName;
259 if (newDisplayName != null)
260 {
261 WorldGen.RenameWorld(this, newDisplayName, OnWorldRenameSuccess);
262 }
263 }
264 }
265
266 private void OnWorldRenameSuccess(string newWorldName)
267 {
268 Name = newWorldName;
270 }
271
273 {
275 Main.menuMode = 0;
276 yield break;
277 }
278}
static int Calculate(string value)
Definition Crc32.cs:35
static DateTime GetCreationTime(string path)
Definition File.cs:164
static double Abs(double value)
static void PlaySound(int type, Vector2 position, int style=1)
static Asset< DynamicSpriteFont > MouseText
Definition FontAssets.cs:10
static FileMetadata FromCurrentSettings(FileType type)
string GetWorldName(bool allowCropping=false)
override void SetAsActive()
void OnWorldRenameSuccess(string newWorldName)
void SetWorldSize(int x, int y)
void CopyToLocal(string newFileName=null, string newDisplayName=null)
override void MoveToCloud()
void SetSeed(string seedText)
string GetFullSeedText(bool allowCropping=false)
IEnumerator DelayedGoToTitleScreen()
void Rename(string newDisplayName)
WorldFileData(string path, bool cloudSave)
static WorldFileData FromInvalidWorld(string path, bool cloudSave)
const ulong GUID_IN_WORLD_FILE_VERSION
override void MoveToLocal()
static LocalizedText GetText(string key)
Definition Language.cs:10
static string GetWorldPathFromName(string worldName, bool cloudSave)
Definition Main.cs:4488
static FavoritesFile CloudFavoritesData
Definition Main.cs:359
static FavoritesFile LocalFavoriteData
Definition Main.cs:357
static List< IEnumerator > DelayedProcesses
Definition Main.cs:431
static bool MoveToCloud(string localPath, string cloudPath)
static string GetFileName(string path, bool includeExtension=true)
static void Copy(string source, string destination, bool cloud, bool overwrite=true)
static bool MoveToLocal(string cloudPath, string localPath)
static void RenameWorld(WorldFileData data, string newName, Action< string > callback=null)
Definition WorldGen.cs:3152
static DateTime Now
Definition DateTime.cs:103
static Guid NewGuid()
Definition Guid.cs:1283
static readonly Guid Empty
Definition Guid.cs:86