internal static bool CreateModReferenceDlls(BuildProperties properties) { TmodFile[] refFiles = new TmodFile[properties.modReferences.Length]; for (int k = 0; k < properties.modReferences.Length; k++) { string modReference = properties.modReferences[k]; string filePath = ModLoader.ModPath + Path.DirectorySeparatorChar + modReference + ".tmod"; TmodFile refFile = new TmodFile(filePath); refFile = new TmodFile(filePath); refFile.Read(); if (!refFile.ValidMod()) { ErrorLogger.LogModReferenceError(refFile.Name); return false; } refFiles[k] = refFile; } for (int k = 0; k < refFiles.Length; k++) { TmodFile refFile = refFiles[k]; string modReference = properties.modReferences[k]; byte[] data1; byte[] data2; if (refFile.HasFile("All")) { data1 = refFile.GetFile("All"); data2 = refFile.GetFile("All"); } else { data1 = refFile.GetFile("Windows"); data2 = refFile.GetFile("Other"); } string refFileName = ModLoader.ModSourcePath + Path.DirectorySeparatorChar + modReference; File.WriteAllBytes(refFileName + "1.dll", data1); File.WriteAllBytes(refFileName + "2.dll", data2); } return true; }
private static void LoadMod(TmodFile modFile, BuildProperties properties) { AddAssemblyResolver(); Interface.loadMods.SetProgressReading(Path.GetFileNameWithoutExtension(modFile.Name)); Assembly modCode; string rootDirectory; if (modFile.HasFile("All")) { modCode = Assembly.Load(modFile.GetFile("All")); } else { modCode = Assembly.Load(modFile.GetFile(windows ? "Windows" : "Other")); } using (MemoryStream memoryStream = new MemoryStream(modFile.GetFile("Resources"))) { using (BinaryReader reader = new BinaryReader(memoryStream)) { memoryStream.Seek(reader.ReadInt32(), SeekOrigin.Current); rootDirectory = reader.ReadString(); for (string path = reader.ReadString(); path != "end"; path = reader.ReadString()) { byte[] data = reader.ReadBytes(reader.ReadInt32()); files[path] = data; string extension = Path.GetExtension(path); switch (extension) { case ".png": string texturePath = Path.ChangeExtension(path, null); using (MemoryStream buffer = new MemoryStream(data)) { textures[texturePath] = Texture2D.FromStream(Main.instance.GraphicsDevice, buffer); } break; case ".wav": string soundPath = Path.ChangeExtension(path, null); using (MemoryStream buffer = new MemoryStream(data)) { sounds[soundPath] = SoundEffect.FromStream(buffer); } break; case ".mp3": // TODO, better way to do this? string mp3Path = Path.ChangeExtension(path, null); MemoryStream wavData = new MemoryStream(); MemoryStream wavFile = new MemoryStream(); ushort wFormatTag = 1; ushort nChannels; uint nSamplesPerSec; uint nAvgBytesPerSec; ushort nBlockAlign; ushort wBitsPerSample = 16; using (MemoryStream buffer = new MemoryStream(data)) { using (MP3Sharp.MP3Stream s = new MP3Sharp.MP3Stream(buffer)) { s.CopyTo(wavData); nChannels = (ushort)s.ChannelCount; nSamplesPerSec = (uint)s.Frequency; } } nBlockAlign = (ushort)(nChannels * (wBitsPerSample / 8)); nAvgBytesPerSec = (uint)(nSamplesPerSec * nChannels * (wBitsPerSample / 8)); using (BinaryWriter bw = new BinaryWriter(wavFile)) { bw.Write("RIFF".ToCharArray()); bw.Write((UInt32)(wavData.Length + 36)); bw.Write("WAVE".ToCharArray()); bw.Write("fmt ".ToCharArray()); bw.Write(16); bw.Write(wFormatTag); bw.Write(nChannels); bw.Write(nSamplesPerSec); bw.Write(nAvgBytesPerSec); bw.Write(nBlockAlign); bw.Write(wBitsPerSample); bw.Write("data".ToCharArray()); bw.Write((UInt32)(wavData.Length)); bw.Write(wavData.ToArray()); } using (MemoryStream buffer = new MemoryStream(wavFile.ToArray())) { sounds[mp3Path] = SoundEffect.FromStream(buffer); } break; } } } } Type[] classes = modCode.GetTypes(); foreach (Type type in classes) { if (type.IsSubclassOf(typeof(Mod))) { Mod mod = (Mod)Activator.CreateInstance(type); mod.file = modFile.Name; mod.code = modCode; mod.Init(); if (mods.ContainsKey(mod.Name)) { throw new DuplicateNameException("Two mods share the internal name " + mod.Name); } if (rootDirectory != mod.Name) { throw new MissingResourceException("Mod name " + mod.Name + " does not match source directory name " + rootDirectory); } mods[mod.Name] = mod; } } }
private static void LoadMod(TmodFile modFile, BuildProperties properties) { AddAssemblyResolver(); string fileName = Path.GetFileNameWithoutExtension(modFile.Name); Interface.loadMods.SetProgressReading(fileName, 0, 2); Assembly modCode; string rootDirectory; if (modFile.HasFile("All")) { modCode = Assembly.Load(modFile.GetFile("All")); } else { modCode = Assembly.Load(modFile.GetFile(windows ? "Windows" : "Other")); } Interface.loadMods.SetProgressReading(fileName, 1, 2); using (MemoryStream memoryStream = new MemoryStream(modFile.GetFile("Resources"))) { using (BinaryReader reader = new BinaryReader(memoryStream)) { rootDirectory = reader.ReadString(); for (string path = reader.ReadString(); path != "end"; path = reader.ReadString()) { byte[] data = reader.ReadBytes(reader.ReadInt32()); files[path] = data; if (Main.dedServ) { continue; } string extension = Path.GetExtension(path); switch (extension) { case ".png": string texturePath = Path.ChangeExtension(path, null); using (MemoryStream buffer = new MemoryStream(data)) { textures[texturePath] = Texture2D.FromStream(Main.instance.GraphicsDevice, buffer); } break; case ".wav": string soundPath = Path.ChangeExtension(path, null); using (MemoryStream buffer = new MemoryStream(data)) { sounds[soundPath] = SoundEffect.FromStream(buffer); } break; case ".mp3": string mp3Path = Path.ChangeExtension(path, null); string wavCacheFilename = mp3Path.Replace('/', '_') + "_" + properties.version + ".wav"; if (WAVCacheIO.WAVCacheAvailable(wavCacheFilename)) { sounds[mp3Path] = SoundEffect.FromStream(WAVCacheIO.GetWavStream(wavCacheFilename)); break; } ushort wFormatTag = 1; ushort nChannels; uint nSamplesPerSec; uint nAvgBytesPerSec; ushort nBlockAlign; ushort wBitsPerSample = 16; const int headerSize = 44; MemoryStream output = new MemoryStream(headerSize + data.Length); using (MemoryStream yourMp3FileStream = new MemoryStream(data)) { using (MP3Sharp.MP3Stream input = new MP3Sharp.MP3Stream(yourMp3FileStream)) { using (BinaryWriter writer = new BinaryWriter(output, Encoding.UTF8)) { output.Position = headerSize; input.CopyTo(output); UInt32 wavDataLength = (UInt32)output.Length - headerSize; output.Position = 0; nChannels = (ushort)input.ChannelCount; nSamplesPerSec = (uint)input.Frequency; nBlockAlign = (ushort)(nChannels * (wBitsPerSample / 8)); nAvgBytesPerSec = (uint)(nSamplesPerSec * nChannels * (wBitsPerSample / 8)); //write the header writer.Write("RIFF".ToCharArray()); //4 writer.Write((UInt32)(wavDataLength + 36)); // 4 writer.Write("WAVE".ToCharArray()); //4 writer.Write("fmt ".ToCharArray()); //4 writer.Write(16); //4 writer.Write(wFormatTag); // writer.Write((ushort)nChannels); writer.Write(nSamplesPerSec); writer.Write(nAvgBytesPerSec); writer.Write(nBlockAlign); writer.Write(wBitsPerSample); writer.Write("data".ToCharArray()); writer.Write((UInt32)(wavDataLength)); output.Position = 0; WAVCacheIO.SaveWavStream(output, wavCacheFilename); sounds[mp3Path] = SoundEffect.FromStream(output); } } } break; } } } } Type[] classes = modCode.GetTypes(); foreach (Type type in classes) { if (type.IsSubclassOf(typeof(Mod))) { Mod mod = (Mod)Activator.CreateInstance(type); mod.file = modFile.Name; mod.buildVersion = properties.modBuildVersion; mod.code = modCode; mod.Init(); if (mod.Name == "Terraria") { throw new DuplicateNameException("Mods cannot be named Terraria"); } if (mods.ContainsKey(mod.Name)) { throw new DuplicateNameException("Two mods share the internal name " + mod.Name); } if (rootDirectory != mod.Name) { throw new MissingResourceException("Mod name " + mod.Name + " does not match source directory name " + rootDirectory); } mods[mod.Name] = mod; loadOrder.Push(mod.Name); } } }
private static void LoadMod(TmodFile modFile, BuildProperties properties) { AddAssemblyResolver(); Interface.loadMods.SetProgressReading(Path.GetFileNameWithoutExtension(modFile.Name)); Assembly modCode; string rootDirectory; if (modFile.HasFile("All")) { modCode = Assembly.Load(modFile.GetFile("All")); } else { modCode = Assembly.Load(modFile.GetFile(windows ? "Windows" : "Other")); } using (MemoryStream memoryStream = new MemoryStream(modFile.GetFile("Resources"))) { using (BinaryReader reader = new BinaryReader(memoryStream)) { memoryStream.Seek(reader.ReadInt32(), SeekOrigin.Current); rootDirectory = reader.ReadString(); for (string path = reader.ReadString(); path != "end"; path = reader.ReadString()) { byte[] data = reader.ReadBytes(reader.ReadInt32()); files[path] = data; string extension = Path.GetExtension(path); switch (extension) { case ".png": string texturePath = Path.ChangeExtension(path, null); using (MemoryStream buffer = new MemoryStream(data)) { textures[texturePath] = Texture2D.FromStream(Main.instance.GraphicsDevice, buffer); } break; case ".wav": string soundPath = Path.ChangeExtension(path, null); using (MemoryStream buffer = new MemoryStream(data)) { sounds[soundPath] = SoundEffect.FromStream(buffer); } break; } } } } Type[] classes = modCode.GetTypes(); foreach (Type type in classes) { if (type.IsSubclassOf(typeof(Mod))) { Mod mod = (Mod)Activator.CreateInstance(type); mod.file = modFile.Name; mod.code = modCode; mod.Init(); if (mods.ContainsKey(mod.Name)) { throw new DuplicateNameException("Two mods share the internal name " + mod.Name); } if (rootDirectory != mod.Name) { throw new MissingResourceException("Mod name " + mod.Name + " does not match source directory name " + rootDirectory); } mods[mod.Name] = mod; } } }