Terraria ModLoader  0.10.1.5
A framework for Terraria mods
Terraria.ModLoader.IO.TmodFile Class Reference
+ Inheritance diagram for Terraria.ModLoader.IO.TmodFile:
+ Collaboration diagram for Terraria.ModLoader.IO.TmodFile:

Classes

class  DisposeWrapper
 
class  FileEntry
 

Public Member Functions

void CacheFiles (ISet< string > skip=null)
 
void Close ()
 
IDisposable EnsureOpen ()
 
byte[] GetBytes (FileEntry entry)
 
byte[] GetBytes (string fileName)
 
IEnumerator< FileEntryGetEnumerator ()
 
byte[] GetFile (string fileName)
 
byte[] GetMainAssembly (bool?windows=null)
 
byte[] GetMainPDB (bool?windows=null)
 
Stream GetStream (FileEntry entry, bool newFileStream=false)
 
Stream GetStream (string fileName, bool newFileStream=false)
 
bool HasFile (string fileName)
 
void RemoveFromCache (IEnumerable< string > fileNames)
 
void ResetCache ()
 
void VerifyCoreFiles ()
 

Public Attributes

const float COMPRESSION_TRADEOFF = 0.9f
 
int Count => fileTable.Length
 
const uint MAX_CACHE_SIZE = 1 << 17
 
const uint MIN_COMPRESS_SIZE = 1 << 10
 
readonly string path
 

Properties

byte[] hash [get, private set]
 
string name [get, set]
 
Version tModLoaderVersion [get, private set]
 
Version version [get, set]
 

Private Member Functions

IEnumerator IEnumerable. GetEnumerator ()
 
void Upgrade ()
 

Static Private Member Functions

static string Sanitize (string path)
 
static bool ShouldCompress (string fileName)
 

Private Attributes

IDictionary< string, FileEntryfiles = new Dictionary<string, FileEntry>()
 
FileStream fileStream
 
FileEntry[] fileTable
 
EntryReadStream lastEntryReadStream
 
bool validModBrowserSignature = new byte[256]
 

Detailed Description

Definition at line 14 of file TmodFile.cs.

Member Function Documentation

void Terraria.ModLoader.IO.TmodFile.CacheFiles ( ISet< string >  skip = null)

Definition at line 294 of file TmodFile.cs.

294  {
295  fileStream.Seek(fileTable[0].Offset, SeekOrigin.Begin);
296  foreach (var f in fileTable) {
297  if (f.CompressedLength > MAX_CACHE_SIZE || (skip?.Contains(f.Name) ?? false)) {
298  fileStream.Seek(f.CompressedLength, SeekOrigin.Current);
299  continue;
300  }
301 
302  f.cachedBytes = fileStream.ReadBytes(f.CompressedLength);
303  }
304  }
void Terraria.ModLoader.IO.TmodFile.Close ( )

Definition at line 332 of file TmodFile.cs.

332  {
333  fileStream?.Close();
334  fileStream = null;
335  }
IDisposable Terraria.ModLoader.IO.TmodFile.EnsureOpen ( )

Definition at line 324 of file TmodFile.cs.

Referenced by Terraria.ModLoader.AssemblyManager.LoadedMod.LoadAssemblies().

324  {
325  if (fileStream != null)
326  return new DisposeWrapper(null);
327 
328  fileStream = File.OpenRead(path);
329  return new DisposeWrapper(Close);
330  }

+ Here is the caller graph for this function:

byte [] Terraria.ModLoader.IO.TmodFile.GetBytes ( FileEntry  entry)

Definition at line 86 of file TmodFile.cs.

References Terraria.ModLoader.IO.TmodFile.FileEntry.IsCompressed, and Terraria.ModLoader.IO.TmodFile.FileEntry.Length.

Referenced by Terraria.ModLoader.AssemblyManager.LoadedMod.LoadAssemblies().

86  {
87  if (entry.cachedBytes != null && !entry.IsCompressed)
88  return entry.cachedBytes;
89 
90  using (var stream = GetStream(entry))
91  return stream.ReadBytes(entry.Length);
92  }
Stream GetStream(FileEntry entry, bool newFileStream=false)
Definition: TmodFile.cs:97

+ Here is the caller graph for this function:

byte [] Terraria.ModLoader.IO.TmodFile.GetBytes ( string  fileName)
IEnumerator IEnumerable. Terraria.ModLoader.IO.TmodFile.GetEnumerator ( )
private
IEnumerator<FileEntry> Terraria.ModLoader.IO.TmodFile.GetEnumerator ( )

Definition at line 163 of file TmodFile.cs.

References Terraria.ModLoader.IO.TmodFile.FileEntry.CompressedLength, Terraria.ModLoader.IO.TmodFile.FileEntry.Length, and Terraria.ModLoader.ModLoader.version.

163  {
164  foreach (var entry in fileTable)
165  yield return entry;
166  }
byte [] Terraria.ModLoader.IO.TmodFile.GetFile ( string  fileName)
byte [] Terraria.ModLoader.IO.TmodFile.GetMainAssembly ( bool?  windows = null)

Definition at line 380 of file TmodFile.cs.

References Terraria.ModLoader.ModLoader.windows.

Referenced by Terraria.ModLoader.AssemblyManager.LoadedMod.LoadAssemblies().

380  {
381  bool isWindows = windows.GetValueOrDefault(ModLoader.windows);
382  return HasFile("All.dll") ? GetBytes("All.dll") : isWindows ? GetBytes("Windows.dll") : GetBytes("Mono.dll");
383  }
bool HasFile(string fileName)
byte[] GetBytes(FileEntry entry)
Definition: TmodFile.cs:86

+ Here is the caller graph for this function:

byte [] Terraria.ModLoader.IO.TmodFile.GetMainPDB ( bool?  windows = null)

Definition at line 385 of file TmodFile.cs.

References Terraria.ModLoader.ModLoader.windows.

Referenced by Terraria.ModLoader.AssemblyManager.LoadedMod.LoadAssemblies().

385  {
386  bool isWindows = windows.GetValueOrDefault(ModLoader.windows);
387  return HasFile("All.pdb") ? GetBytes("All.pdb") : isWindows ? GetBytes("Windows.pdb") : GetBytes("Mono.mdb");
388  }
bool HasFile(string fileName)
byte[] GetBytes(FileEntry entry)
Definition: TmodFile.cs:86

+ Here is the caller graph for this function:

Stream Terraria.ModLoader.IO.TmodFile.GetStream ( FileEntry  entry,
bool  newFileStream = false 
)

Definition at line 97 of file TmodFile.cs.

References Terraria.ModLoader.IO.TmodFile.FileEntry.IsCompressed.

97  {
98  Stream stream;
99  if (entry.cachedBytes != null) {
100  stream = new MemoryStream(entry.cachedBytes);
101  }
102  else if (newFileStream) {
103  stream = new EntryReadStream(File.OpenRead(path), entry, false);
104  }
105  else {
106  if (fileStream == null)
107  throw new IOException("File not open: " + path);
108 
109  if (lastEntryReadStream != null && !lastEntryReadStream.IsClosed)
110  throw new IOException($"Previous entry read stream not closed: {lastEntryReadStream.Name}");
111 
112  stream = lastEntryReadStream = new EntryReadStream(fileStream, entry, true);
113  }
114 
115  if (entry.IsCompressed)
116  stream = new DeflateStream(stream, CompressionMode.Decompress);
117 
118  return stream;
119  }
EntryReadStream lastEntryReadStream
Definition: TmodFile.cs:96
Stream Terraria.ModLoader.IO.TmodFile.GetStream ( string  fileName,
bool  newFileStream = false 
)

Definition at line 121 of file TmodFile.cs.

121  {
122  if (!files.TryGetValue(Sanitize(fileName), out var entry))
123  throw new KeyNotFoundException(fileName);
124 
125  return GetStream(entry, newFileStream);
126  }
IDictionary< string, FileEntry > files
Definition: TmodFile.cs:48
static string Sanitize(string path)
Stream GetStream(FileEntry entry, bool newFileStream=false)
Definition: TmodFile.cs:97
bool Terraria.ModLoader.IO.TmodFile.HasFile ( string  fileName)
void Terraria.ModLoader.IO.TmodFile.RemoveFromCache ( IEnumerable< string >  fileNames)

Definition at line 306 of file TmodFile.cs.

306  {
307  foreach (var fileName in fileNames)
308  files[fileName].cachedBytes = null;
309  }
IDictionary< string, FileEntry > files
Definition: TmodFile.cs:48
void Terraria.ModLoader.IO.TmodFile.ResetCache ( )

Definition at line 311 of file TmodFile.cs.

311  {
312  foreach (var f in fileTable)
313  f.cachedBytes = null;
314  }
static string Terraria.ModLoader.IO.TmodFile.Sanitize ( string  path)
staticprivate
static bool Terraria.ModLoader.IO.TmodFile.ShouldCompress ( string  fileName)
staticprivate
void Terraria.ModLoader.IO.TmodFile.Upgrade ( )
private

Definition at line 337 of file TmodFile.cs.

337  {
338  Interface.loadMods.SubProgressText = $"Upgrading: {Path.GetFileName(path)}";
339  Logging.tML.InfoFormat("Upgrading: {0}", Path.GetFileName(path));
340 
341  using (var deflateStream = new DeflateStream(fileStream, CompressionMode.Decompress, true))
342  using (var reader = new BinaryReader(deflateStream)) {
343  name = reader.ReadString();
344  version = new Version(reader.ReadString());
345 
346  int count = reader.ReadInt32();
347  for (int i = 0; i < count; i++)
348  AddFile(reader.ReadString(), reader.ReadBytes(reader.ReadInt32()));
349  }
350 
351  // update buildVersion
352  var info = BuildProperties.ReadModFile(this);
353  info.buildVersion = tModLoaderVersion;
354  AddFile("Info", info.ToBytes());
355 
356  // make a backup
357  fileStream.Seek(0, SeekOrigin.Begin);
358  var backupFolder = Path.Combine(Path.GetDirectoryName(path), "UpgradeBackup");
359  Directory.CreateDirectory(backupFolder);
360  using (var backupStream = File.OpenWrite(Path.Combine(backupFolder, Path.GetFileName(path))))
361  fileStream.CopyTo(backupStream);
362 
363  // write to the new format (also updates the file offset table)
364  Save();
365  // clear all the file contents from AddFile
366  ResetCache();
367  // Save closes the file so re-open it
368  fileStream = File.OpenRead(path);
369  // Read contract fulfilled
370  }
void Terraria.ModLoader.IO.TmodFile.VerifyCoreFiles ( )

Definition at line 372 of file TmodFile.cs.

372  {
373  if (!HasFile("Info"))
374  throw new Exception("Missing Info file");
375 
376  if (!HasFile("All.dll") && !(HasFile("Windows.dll") && HasFile("Mono.dll")))
377  throw new Exception("Missing All.dll or Windows.dll and Mono.dll");
378  }
bool HasFile(string fileName)

Member Data Documentation

const float Terraria.ModLoader.IO.TmodFile.COMPRESSION_TRADEOFF = 0.9f

Definition at line 41 of file TmodFile.cs.

int Terraria.ModLoader.IO.TmodFile.Count => fileTable.Length

Definition at line 160 of file TmodFile.cs.

IDictionary<string, FileEntry> Terraria.ModLoader.IO.TmodFile.files = new Dictionary<string, FileEntry>()
private

Definition at line 48 of file TmodFile.cs.

FileStream Terraria.ModLoader.IO.TmodFile.fileStream
private

Definition at line 47 of file TmodFile.cs.

FileEntry [] Terraria.ModLoader.IO.TmodFile.fileTable
private

Definition at line 49 of file TmodFile.cs.

EntryReadStream Terraria.ModLoader.IO.TmodFile.lastEntryReadStream
private

Definition at line 96 of file TmodFile.cs.

const uint Terraria.ModLoader.IO.TmodFile.MAX_CACHE_SIZE = 1 << 17

Definition at line 40 of file TmodFile.cs.

const uint Terraria.ModLoader.IO.TmodFile.MIN_COMPRESS_SIZE = 1 << 10

Definition at line 39 of file TmodFile.cs.

readonly string Terraria.ModLoader.IO.TmodFile.path

Definition at line 45 of file TmodFile.cs.

bool Terraria.ModLoader.IO.TmodFile.validModBrowserSignature = new byte[256]
private

Definition at line 65 of file TmodFile.cs.

Property Documentation

byte [] Terraria.ModLoader.IO.TmodFile.hash
getprivate set
string Terraria.ModLoader.IO.TmodFile.name
getset

Definition at line 55 of file TmodFile.cs.

Referenced by Terraria.ModLoader.ModNet.ModHeader.ModHeader().

Version Terraria.ModLoader.IO.TmodFile.tModLoaderVersion
getprivate set

Definition at line 51 of file TmodFile.cs.