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 ()
 
delegate void EntryIterator (string name, int len, Func< Stream > getStream)
 
void ForEach (EntryIterator iterator)
 
byte[] GetBytes (string fileName)
 
IEnumerator< string > GetEnumerator ()
 
byte[] GetFile (string fileName)
 
byte[] GetMainAssembly (bool?windows=null)
 
byte[] GetMainPDB (bool?windows=null)
 
Stream GetStream (string fileName)
 
bool HasFile (string fileName)
 
void RemoveFromCache (IEnumerable< string > fileNames)
 
void ResetCache ()
 
void VerifyCoreFiles ()
 

Public Attributes

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 ()
 
Stream GetStream (FileEntry entry)
 
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 278 of file TmodFile.cs.

278  {
279  fileStream.Seek(fileTable[0].offset, SeekOrigin.Begin);
280  foreach (var f in fileTable) {
281  if (f.compressedSize > MAX_CACHE_SIZE || (skip?.Contains(f.name) ?? false)) {
282  fileStream.Seek(f.compressedSize, SeekOrigin.Current);
283  continue;
284  }
285 
286  f.cachedBytes = fileStream.ReadBytes(f.compressedSize);
287  }
288  }
void Terraria.ModLoader.IO.TmodFile.Close ( )

Definition at line 316 of file TmodFile.cs.

316  {
317  fileStream?.Close();
318  fileStream = null;
319  }
IDisposable Terraria.ModLoader.IO.TmodFile.EnsureOpen ( )

Definition at line 308 of file TmodFile.cs.

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

308  {
309  if (fileStream != null)
310  return new DisposeWrapper(null);
311 
312  fileStream = File.OpenRead(path);
313  return new DisposeWrapper(Close);
314  }

+ Here is the caller graph for this function:

delegate void Terraria.ModLoader.IO.TmodFile.EntryIterator ( string  name,
int  len,
Func< Stream >  getStream 
)
void Terraria.ModLoader.IO.TmodFile.ForEach ( EntryIterator  iterator)

Definition at line 146 of file TmodFile.cs.

146  {
147  foreach (var f in fileTable)
148  iterator(f.name, f.size, () => GetStream(f));
149  }
Stream GetStream(FileEntry entry)
Definition: TmodFile.cs:83
byte [] Terraria.ModLoader.IO.TmodFile.GetBytes ( string  fileName)

Definition at line 71 of file TmodFile.cs.

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

71  {
72  if (!files.TryGetValue(Sanitize(fileName), out var entry))
73  return null;
74 
75  if (entry.cachedBytes != null && entry.compressedSize == entry.size)
76  return entry.cachedBytes;
77 
78  using (var stream = GetStream(entry))
79  return stream.ReadBytes(entry.size);
80  }
IDictionary< string, FileEntry > files
Definition: TmodFile.cs:33
static string Sanitize(string path)
Stream GetStream(FileEntry entry)
Definition: TmodFile.cs:83

+ Here is the caller graph for this function:

IEnumerator<string> Terraria.ModLoader.IO.TmodFile.GetEnumerator ( )
IEnumerator IEnumerable. Terraria.ModLoader.IO.TmodFile.GetEnumerator ( )
private
byte [] Terraria.ModLoader.IO.TmodFile.GetFile ( string  fileName)
byte [] Terraria.ModLoader.IO.TmodFile.GetMainAssembly ( bool?  windows = null)

Definition at line 359 of file TmodFile.cs.

References Terraria.ModLoader.ModLoader.windows.

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

359  {
360  bool isWindows = windows.GetValueOrDefault(ModLoader.windows);
361  return HasFile("All.dll") ? GetBytes("All.dll") : isWindows ? GetBytes("Windows.dll") : GetBytes("Mono.dll");
362  }
bool HasFile(string fileName)
byte[] GetBytes(string fileName)
Definition: TmodFile.cs:71

+ Here is the caller graph for this function:

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

Definition at line 364 of file TmodFile.cs.

References Terraria.ModLoader.ModLoader.windows.

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

364  {
365  bool isWindows = windows.GetValueOrDefault(ModLoader.windows);
366  return HasFile("All.pdb") ? GetBytes("All.pdb") : isWindows ? GetBytes("Windows.pdb") : GetBytes("Mono.pdb");
367  }
bool HasFile(string fileName)
byte[] GetBytes(string fileName)
Definition: TmodFile.cs:71

+ Here is the caller graph for this function:

Stream Terraria.ModLoader.IO.TmodFile.GetStream ( FileEntry  entry)
private

Definition at line 83 of file TmodFile.cs.

References Terraria.ModLoader.IO.TmodFile.FileEntry.cachedBytes, Terraria.ModLoader.IO.TmodFile.FileEntry.compressedSize, Terraria.ModLoader.IO.TmodFile.FileEntry.name, Terraria.ModLoader.IO.TmodFile.FileEntry.offset, and Terraria.ModLoader.IO.TmodFile.FileEntry.size.

83  {
84  Stream stream;
85  if (entry.cachedBytes != null) {
86  stream = new MemoryStream(entry.cachedBytes);
87  }
88  else {
89  if (fileStream == null)
90  throw new IOException("File not open: " + path);
91  if (lastEntryReadStream != null && !lastEntryReadStream.IsDisposed)
92  throw new IOException($"Previous entry read stream not closed: {lastEntryReadStream.name}");
93 
94  stream = lastEntryReadStream = new EntryReadStream(fileStream, entry.offset, entry.compressedSize, entry.name);
95  }
96 
97  if (entry.compressedSize != entry.size)
98  stream = new DeflateStream(stream, CompressionMode.Decompress);
99 
100  return stream;
101  }
EntryReadStream lastEntryReadStream
Definition: TmodFile.cs:82
Stream Terraria.ModLoader.IO.TmodFile.GetStream ( string  fileName)

Definition at line 103 of file TmodFile.cs.

103  {
104  if (!files.TryGetValue(Sanitize(fileName), out var entry))
105  throw new KeyNotFoundException(fileName);
106 
107  return GetStream(entry);
108  }
IDictionary< string, FileEntry > files
Definition: TmodFile.cs:33
static string Sanitize(string path)
Stream GetStream(FileEntry entry)
Definition: TmodFile.cs:83
bool Terraria.ModLoader.IO.TmodFile.HasFile ( string  fileName)
void Terraria.ModLoader.IO.TmodFile.RemoveFromCache ( IEnumerable< string >  fileNames)

Definition at line 290 of file TmodFile.cs.

290  {
291  foreach (var fileName in fileNames)
292  files[fileName].cachedBytes = null;
293  }
IDictionary< string, FileEntry > files
Definition: TmodFile.cs:33
void Terraria.ModLoader.IO.TmodFile.ResetCache ( )

Definition at line 295 of file TmodFile.cs.

295  {
296  foreach (var f in fileTable)
297  f.cachedBytes = null;
298  }
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 323 of file TmodFile.cs.

323  {
324  Interface.loadMods.SubProgressText = $"Upgrading: {Path.GetFileName(path)}";
325  Logging.tML.InfoFormat("Upgrading: {0}", Path.GetFileName(path));
326 
327  using (var deflateStream = new DeflateStream(fileStream, CompressionMode.Decompress))
328  using (var reader = new BinaryReader(deflateStream)) {
329  name = reader.ReadString();
330  version = new Version(reader.ReadString());
331 
332  int count = reader.ReadInt32();
333  for (int i = 0; i < count; i++)
334  AddFile(reader.ReadString(), reader.ReadBytes(reader.ReadInt32()));
335  }
336 
337  // update buildVersion
338  var info = BuildProperties.ReadModFile(this);
339  info.buildVersion = tModLoaderVersion;
340  AddFile("Info", info.ToBytes());
341 
342  // write to the new format (also updates the file offset table)
343  Save();
344  // clear all the file contents from AddFile
345  ResetCache();
346  // Save closes the file so re-open it
347  fileStream = File.OpenRead(path);
348  // Read contract fulfilled
349  }
void Terraria.ModLoader.IO.TmodFile.VerifyCoreFiles ( )

Definition at line 351 of file TmodFile.cs.

351  {
352  if (!HasFile("Info"))
353  throw new Exception("Missing Info file");
354 
355  if (!HasFile("All.dll") && !(HasFile("Windows.dll") && HasFile("Mono.dll")))
356  throw new Exception("Missing All.dll or Windows.dll and Mono.dll");
357  }
bool HasFile(string fileName)

Member Data Documentation

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

Definition at line 151 of file TmodFile.cs.

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

Definition at line 33 of file TmodFile.cs.

FileStream Terraria.ModLoader.IO.TmodFile.fileStream
private

Definition at line 32 of file TmodFile.cs.

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

Definition at line 34 of file TmodFile.cs.

EntryReadStream Terraria.ModLoader.IO.TmodFile.lastEntryReadStream
private

Definition at line 82 of file TmodFile.cs.

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

Definition at line 26 of file TmodFile.cs.

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

Definition at line 25 of file TmodFile.cs.

readonly string Terraria.ModLoader.IO.TmodFile.path

Definition at line 30 of file TmodFile.cs.

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

Definition at line 50 of file TmodFile.cs.

Property Documentation

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

Definition at line 40 of file TmodFile.cs.

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

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

Definition at line 36 of file TmodFile.cs.