Ejemplo n.º 1
0
        private static void LoadMod(string modFile, BuildProperties properties)
        {
            AddAssemblyResolver();
            Assembly modCode;

            using (FileStream fileStream = File.OpenRead(modFile))
            {
                using (BinaryReader reader = new BinaryReader(fileStream))
                {
                    fileStream.Seek(reader.ReadInt32(), SeekOrigin.Current);
                    modCode = Assembly.Load(reader.ReadBytes(reader.ReadInt32()));
                    for (string texturePath = reader.ReadString(); texturePath != "endImages"; texturePath = reader.ReadString())
                    {
                        byte[] imageData = reader.ReadBytes(reader.ReadInt32());
                        using (MemoryStream buffer = new MemoryStream(imageData))
                        {
                            textures[texturePath] = Texture2D.FromStream(Main.instance.GraphicsDevice, buffer);
                        }
                    }
                    for (string soundPath = reader.ReadString(); soundPath != "endSounds"; soundPath = reader.ReadString())
                    {
                        byte[] soundData = reader.ReadBytes(reader.ReadInt32());
                        //      ErrorLogger.Log("sound data: "+ soundPath +" "+imageData.Length);
                        using (MemoryStream buffer = new MemoryStream(soundData))
                        {
                            sounds[soundPath] = SoundEffect.FromStream(buffer);
                        }
                    }
                }
            }
            Type[] classes = modCode.GetTypes();
            foreach (Type type in classes)
            {
                if (type.IsSubclassOf(typeof(Mod)))
                {
                    Mod mod = (Mod)Activator.CreateInstance(type);
                    mod.file = modFile;
                    mod.code = modCode;
                    mod.Init();
                    if (mods.ContainsKey(mod.Name))
                    {
                        throw new Exception("Two mods share the internal name " + mod.Name);
                    }
                    mods[mod.Name] = mod;
                }
            }
        }
Ejemplo n.º 2
0
        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))
                {
                    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":
                            string       mp3Path    = Path.ChangeExtension(path, null);
                            ushort       wFormatTag = 1;
                            ushort       nChannels;
                            uint         nSamplesPerSec;
                            uint         nAvgBytesPerSec;
                            ushort       nBlockAlign;
                            ushort       wBitsPerSample = 16;
                            MemoryStream output         = new MemoryStream();
                            using (MemoryStream yourMp3FileStream = new MemoryStream(data))
                                using (var input = new MP3Sharp.MP3Stream(yourMp3FileStream))
                                    using (var writer = new BinaryWriter(output, Encoding.UTF8))
                                    {
                                        var headerSize = 44;
                                        output.Position = headerSize;
                                        input.CopyTo(output);
                                        UInt32 wavDataLength = (UInt32)output.Length - 44;
                                        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;
                                        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.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;
                    loadOrder.Push(mod.Name);
                }
            }
        }