Terraria 1.3.5.3 Source Code

This commit is contained in:
MikeyIsBaeYT 2021-10-27 18:03:19 -04:00
commit 4b21dac4b6
503 changed files with 409032 additions and 0 deletions

68
IO/FavoritesFile.cs Normal file
View file

@ -0,0 +1,68 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.FavoritesFile
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;
using Terraria.Utilities;
namespace Terraria.IO
{
public class FavoritesFile
{
public readonly string Path;
public readonly bool IsCloudSave;
private Dictionary<string, Dictionary<string, bool>> _data = new Dictionary<string, Dictionary<string, bool>>();
public FavoritesFile(string path, bool isCloud)
{
this.Path = path;
this.IsCloudSave = isCloud;
}
public void SaveFavorite(FileData fileData)
{
if (!this._data.ContainsKey(fileData.Type))
this._data.Add(fileData.Type, new Dictionary<string, bool>());
this._data[fileData.Type][fileData.GetFileName()] = fileData.IsFavorite;
this.Save();
}
public void ClearEntry(FileData fileData)
{
if (!this._data.ContainsKey(fileData.Type))
return;
this._data[fileData.Type].Remove(fileData.GetFileName());
this.Save();
}
public bool IsFavorite(FileData fileData)
{
if (!this._data.ContainsKey(fileData.Type))
return false;
string fileName = fileData.GetFileName();
bool flag;
return this._data[fileData.Type].TryGetValue(fileName, out flag) && flag;
}
public void Save() => FileUtilities.WriteAllBytes(this.Path, Encoding.ASCII.GetBytes(JsonConvert.SerializeObject((object) this._data, (Formatting) 1)), this.IsCloudSave);
public void Load()
{
if (!FileUtilities.Exists(this.Path, this.IsCloudSave))
{
this._data.Clear();
}
else
{
this._data = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, bool>>>(Encoding.ASCII.GetString(FileUtilities.ReadAllBytes(this.Path, this.IsCloudSave)));
if (this._data != null)
return;
this._data = new Dictionary<string, Dictionary<string, bool>>();
}
}
}
}

54
IO/FileData.cs Normal file
View file

@ -0,0 +1,54 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.FileData
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
using Terraria.Utilities;
namespace Terraria.IO
{
public abstract class FileData
{
protected string _path;
protected bool _isCloudSave;
public FileMetadata Metadata;
public string Name;
public readonly string Type;
protected bool _isFavorite;
public string Path => this._path;
public bool IsCloudSave => this._isCloudSave;
public bool IsFavorite => this._isFavorite;
protected FileData(string type) => this.Type = type;
protected FileData(string type, string path, bool isCloud)
{
this.Type = type;
this._path = path;
this._isCloudSave = isCloud;
this._isFavorite = (isCloud ? Main.CloudFavoritesData : Main.LocalFavoriteData).IsFavorite(this);
}
public void ToggleFavorite() => this.SetFavorite(!this.IsFavorite);
public string GetFileName(bool includeExtension = true) => FileUtilities.GetFileName(this.Path, includeExtension);
public void SetFavorite(bool favorite, bool saveChanges = true)
{
this._isFavorite = favorite;
if (!saveChanges)
return;
(this.IsCloudSave ? Main.CloudFavoritesData : Main.LocalFavoriteData).SaveFavorite(this);
}
public abstract void SetAsActive();
public abstract void MoveToCloud();
public abstract void MoveToLocal();
}
}

74
IO/FileMetadata.cs Normal file
View file

@ -0,0 +1,74 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.FileMetadata
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
using System;
using System.IO;
namespace Terraria.IO
{
public class FileMetadata
{
public const ulong MAGIC_NUMBER = 27981915666277746;
public const int SIZE = 20;
public FileType Type;
public uint Revision;
public bool IsFavorite;
private FileMetadata()
{
}
public void Write(BinaryWriter writer)
{
writer.Write((ulong) (27981915666277746L | (long) this.Type << 56));
writer.Write(this.Revision);
writer.Write((ulong) (this.IsFavorite.ToInt() & 1 | 0));
}
public void IncrementAndWrite(BinaryWriter writer)
{
++this.Revision;
this.Write(writer);
}
public static FileMetadata FromCurrentSettings(FileType type) => new FileMetadata()
{
Type = type,
Revision = 0,
IsFavorite = false
};
public static FileMetadata Read(BinaryReader reader, FileType expectedType)
{
FileMetadata fileMetadata = new FileMetadata();
fileMetadata.Read(reader);
if (fileMetadata.Type != expectedType)
throw new FileFormatException("Expected type \"" + Enum.GetName(typeof (FileType), (object) expectedType) + "\" but found \"" + Enum.GetName(typeof (FileType), (object) fileMetadata.Type) + "\".");
return fileMetadata;
}
private void Read(BinaryReader reader)
{
long num1 = (long) reader.ReadUInt64();
if ((num1 & 72057594037927935L) != 27981915666277746L)
throw new FileFormatException("Expected Re-Logic file format.");
byte num2 = (byte) ((ulong) num1 >> 56 & (ulong) byte.MaxValue);
FileType fileType = FileType.None;
FileType[] values = (FileType[]) Enum.GetValues(typeof (FileType));
for (int index = 0; index < values.Length; ++index)
{
if (values[index] == (FileType) num2)
{
fileType = values[index];
break;
}
}
this.Type = fileType != FileType.None ? fileType : throw new FileFormatException("Found invalid file type.");
this.Revision = reader.ReadUInt32();
this.IsFavorite = ((long) reader.ReadUInt64() & 1L) == 1L;
}
}
}

16
IO/FileType.cs Normal file
View file

@ -0,0 +1,16 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.FileType
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
namespace Terraria.IO
{
public enum FileType : byte
{
None,
Map,
World,
Player,
}
}

143
IO/PlayerFileData.cs Normal file
View file

@ -0,0 +1,143 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.PlayerFileData
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Terraria.Social;
using Terraria.Utilities;
namespace Terraria.IO
{
public class PlayerFileData : FileData
{
private Player _player;
private TimeSpan _playTime = TimeSpan.Zero;
private Stopwatch _timer = new Stopwatch();
private bool _isTimerActive;
public Player Player
{
get => this._player;
set
{
this._player = value;
if (value == null)
return;
this.Name = this._player.name;
}
}
public PlayerFileData()
: base(nameof (Player))
{
}
public PlayerFileData(string path, bool cloudSave)
: base(nameof (Player), path, cloudSave)
{
}
public static PlayerFileData CreateAndSave(Player player)
{
PlayerFileData playerFile = new PlayerFileData();
playerFile.Metadata = FileMetadata.FromCurrentSettings(FileType.Player);
playerFile.Player = player;
playerFile._isCloudSave = SocialAPI.Cloud != null && SocialAPI.Cloud.EnabledByDefault;
playerFile._path = Main.GetPlayerPathFromName(player.name, playerFile.IsCloudSave);
(playerFile.IsCloudSave ? Main.CloudFavoritesData : Main.LocalFavoriteData).ClearEntry((FileData) playerFile);
Player.SavePlayer(playerFile, true);
return playerFile;
}
public override void SetAsActive()
{
Main.ActivePlayerFileData = this;
Main.player[Main.myPlayer] = this.Player;
}
public override void MoveToCloud()
{
if (this.IsCloudSave || SocialAPI.Cloud == null)
return;
string playerPathFromName = Main.GetPlayerPathFromName(this.Name, true);
if (!FileUtilities.MoveToCloud(this.Path, playerPathFromName))
return;
string fileName = this.GetFileName(false);
string path = Main.PlayerPath + Path.DirectorySeparatorChar.ToString() + fileName + Path.DirectorySeparatorChar.ToString();
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path);
for (int index = 0; index < files.Length; ++index)
{
string cloudPath = Main.CloudPlayerPath + "/" + fileName + "/" + FileUtilities.GetFileName(files[index]);
FileUtilities.MoveToCloud(files[index], cloudPath);
}
}
Main.LocalFavoriteData.ClearEntry((FileData) this);
this._isCloudSave = true;
this._path = playerPathFromName;
Main.CloudFavoritesData.SaveFavorite((FileData) this);
}
public override void MoveToLocal()
{
if (!this.IsCloudSave || SocialAPI.Cloud == null)
return;
string playerPathFromName = Main.GetPlayerPathFromName(this.Name, false);
if (!FileUtilities.MoveToLocal(this.Path, playerPathFromName))
return;
string fileName = this.GetFileName(false);
string mapPath = Path.Combine(Main.CloudPlayerPath, fileName);
foreach (string str in SocialAPI.Cloud.GetFiles().Where<string>((Func<string, bool>) (path => path.StartsWith(mapPath, StringComparison.CurrentCultureIgnoreCase) && path.EndsWith(".map", StringComparison.CurrentCultureIgnoreCase))))
{
string localPath = Path.Combine(Main.PlayerPath, fileName, FileUtilities.GetFileName(str));
FileUtilities.MoveToLocal(str, localPath);
}
Main.CloudFavoritesData.ClearEntry((FileData) this);
this._isCloudSave = false;
this._path = playerPathFromName;
Main.LocalFavoriteData.SaveFavorite((FileData) this);
}
public void UpdatePlayTimer()
{
if (Main.instance.IsActive && !Main.gamePaused && Main.hasFocus && this._isTimerActive)
this.StartPlayTimer();
else
this.PausePlayTimer();
}
public void StartPlayTimer()
{
this._isTimerActive = true;
if (this._timer.IsRunning)
return;
this._timer.Start();
}
public void PausePlayTimer()
{
if (!this._timer.IsRunning)
return;
this._timer.Stop();
}
public TimeSpan GetPlayTime() => this._timer.IsRunning ? this._playTime + this._timer.Elapsed : this._playTime;
public void StopPlayTimer()
{
this._isTimerActive = false;
if (!this._timer.IsRunning)
return;
this._playTime += this._timer.Elapsed;
this._timer.Reset();
}
public void SetPlayTime(TimeSpan time) => this._playTime = time;
}
}

180
IO/Preferences.cs Normal file
View file

@ -0,0 +1,180 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.Preferences
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Terraria.Localization;
namespace Terraria.IO
{
public class Preferences
{
private Dictionary<string, object> _data = new Dictionary<string, object>();
private readonly string _path;
private readonly JsonSerializerSettings _serializerSettings;
public readonly bool UseBson;
private readonly object _lock = new object();
public bool AutoSave;
public event Action<Preferences> OnSave;
public event Action<Preferences> OnLoad;
public event Preferences.TextProcessAction OnProcessText;
public Preferences(string path, bool parseAllTypes = false, bool useBson = false)
{
this._path = path;
this.UseBson = useBson;
if (parseAllTypes)
this._serializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = (TypeNameHandling) 4,
MetadataPropertyHandling = (MetadataPropertyHandling) 1,
Formatting = (Formatting) 1
};
else
this._serializerSettings = new JsonSerializerSettings()
{
Formatting = (Formatting) 1
};
}
public bool Load()
{
lock (this._lock)
{
if (!File.Exists(this._path))
return false;
try
{
if (!this.UseBson)
{
this._data = JsonConvert.DeserializeObject<Dictionary<string, object>>(File.ReadAllText(this._path), this._serializerSettings);
}
else
{
using (FileStream fileStream = File.OpenRead(this._path))
{
using (BsonReader bsonReader = new BsonReader((Stream) fileStream))
this._data = JsonSerializer.Create(this._serializerSettings).Deserialize<Dictionary<string, object>>((JsonReader) bsonReader);
}
}
if (this._data == null)
this._data = new Dictionary<string, object>();
if (this.OnLoad != null)
this.OnLoad(this);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
public bool Save(bool createFile = true)
{
lock (this._lock)
{
try
{
if (this.OnSave != null)
this.OnSave(this);
if (!createFile && !File.Exists(this._path))
return false;
Directory.GetParent(this._path).Create();
if (!createFile)
File.SetAttributes(this._path, FileAttributes.Normal);
if (!this.UseBson)
{
string text = JsonConvert.SerializeObject((object) this._data, this._serializerSettings);
if (this.OnProcessText != null)
this.OnProcessText(ref text);
File.WriteAllText(this._path, text);
File.SetAttributes(this._path, FileAttributes.Normal);
}
else
{
using (FileStream fileStream = File.Create(this._path))
{
using (BsonWriter bsonWriter = new BsonWriter((Stream) fileStream))
{
File.SetAttributes(this._path, FileAttributes.Normal);
JsonSerializer.Create(this._serializerSettings).Serialize((JsonWriter) bsonWriter, (object) this._data);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(Language.GetTextValue("Error.UnableToWritePreferences", (object) this._path));
Console.WriteLine(ex.ToString());
Monitor.Exit(this._lock);
return false;
}
return true;
}
}
public void Clear() => this._data.Clear();
public void Put(string name, object value)
{
lock (this._lock)
{
this._data[name] = value;
if (!this.AutoSave)
return;
this.Save();
}
}
public bool Contains(string name)
{
lock (this._lock)
return this._data.ContainsKey(name);
}
public T Get<T>(string name, T defaultValue)
{
lock (this._lock)
{
try
{
object obj1;
if (!this._data.TryGetValue(name, out obj1))
return defaultValue;
switch (obj1)
{
case T obj:
return obj;
case JObject _:
return JsonConvert.DeserializeObject<T>(((object) (JObject) obj1).ToString());
default:
return (T) Convert.ChangeType(obj1, typeof (T));
}
}
catch
{
return defaultValue;
}
}
}
public void Get<T>(string name, ref T currentValue) => currentValue = this.Get<T>(name, currentValue);
public List<string> GetAllKeys() => this._data.Keys.ToList<string>();
public delegate void TextProcessAction(ref string text);
}
}

2382
IO/WorldFile.cs Normal file

File diff suppressed because it is too large Load diff

134
IO/WorldFileData.cs Normal file
View file

@ -0,0 +1,134 @@
// Decompiled with JetBrains decompiler
// Type: Terraria.IO.WorldFileData
// Assembly: Terraria, Version=1.3.5.3, Culture=neutral, PublicKeyToken=null
// MVID: 68659D26-2BE6-448F-8663-74FA559E6F08
// Assembly location: C:\Users\mikeyisbaeyt\Downloads\depotdownloader-2.4.5\depots\105601\6707058\Terraria.exe
using System;
using System.IO;
using Terraria.Localization;
using Terraria.Utilities;
namespace Terraria.IO
{
public class WorldFileData : FileData
{
private const ulong GUID_IN_WORLD_FILE_VERSION = 777389080577;
public DateTime CreationTime;
public int WorldSizeX;
public int WorldSizeY;
public ulong WorldGeneratorVersion;
private string _seedText = "";
private int _seed;
public bool IsValid = true;
public Guid UniqueId;
public LocalizedText _worldSizeName;
public bool IsExpertMode;
public bool HasCorruption = true;
public bool IsHardMode;
public string SeedText => this._seedText;
public int Seed => this._seed;
public string WorldSizeName => this._worldSizeName.Value;
public bool HasCrimson
{
get => !this.HasCorruption;
set => this.HasCorruption = !value;
}
public bool HasValidSeed => this.WorldGeneratorVersion > 0UL;
public bool UseGuidAsMapName => this.WorldGeneratorVersion >= 777389080577UL;
public WorldFileData()
: base("World")
{
}
public WorldFileData(string path, bool cloudSave)
: base("World", path, cloudSave)
{
}
public override void SetAsActive() => Main.ActiveWorldFileData = this;
public void SetWorldSize(int x, int y)
{
this.WorldSizeX = x;
this.WorldSizeY = y;
switch (x)
{
case 4200:
this._worldSizeName = Language.GetText("UI.WorldSizeSmall");
break;
case 6400:
this._worldSizeName = Language.GetText("UI.WorldSizeMedium");
break;
case 8400:
this._worldSizeName = Language.GetText("UI.WorldSizeLarge");
break;
default:
this._worldSizeName = Language.GetText("UI.WorldSizeUnknown");
break;
}
}
public static WorldFileData FromInvalidWorld(string path, bool cloudSave)
{
WorldFileData worldFileData = new WorldFileData(path, cloudSave);
worldFileData.IsExpertMode = false;
worldFileData.SetSeedToEmpty();
worldFileData.WorldGeneratorVersion = 0UL;
worldFileData.Metadata = FileMetadata.FromCurrentSettings(FileType.World);
worldFileData.SetWorldSize(1, 1);
worldFileData.HasCorruption = true;
worldFileData.IsHardMode = false;
worldFileData.IsValid = false;
worldFileData.Name = FileUtilities.GetFileName(path, false);
worldFileData.UniqueId = Guid.Empty;
worldFileData.CreationTime = cloudSave ? DateTime.Now : File.GetCreationTime(path);
return worldFileData;
}
public void SetSeedToEmpty() => this.SetSeed("");
public void SetSeed(string seedText)
{
this._seedText = seedText;
if (!int.TryParse(seedText, out this._seed))
this._seed = seedText.GetHashCode();
this._seed = Math.Abs(this._seed);
}
public void SetSeedToRandom() => this.SetSeed(new UnifiedRandom().Next().ToString());
public override void MoveToCloud()
{
if (this.IsCloudSave)
return;
string worldPathFromName = Main.GetWorldPathFromName(this.Name, true);
if (!FileUtilities.MoveToCloud(this.Path, worldPathFromName))
return;
Main.LocalFavoriteData.ClearEntry((FileData) this);
this._isCloudSave = true;
this._path = worldPathFromName;
Main.CloudFavoritesData.SaveFavorite((FileData) this);
}
public override void MoveToLocal()
{
if (!this.IsCloudSave)
return;
string worldPathFromName = Main.GetWorldPathFromName(this.Name, false);
if (!FileUtilities.MoveToLocal(this.Path, worldPathFromName))
return;
Main.CloudFavoritesData.ClearEntry((FileData) this);
this._isCloudSave = false;
this._path = worldPathFromName;
Main.LocalFavoriteData.SaveFavorite((FileData) this);
}
}
}