예제 #1
0
        /// <summary>
        /// Returns whether or not a texture with the specified name exists. texture will be populated with null if not found, and the texture if found.
        /// </summary>
        /// <param name="name">The texture name that is requested</param>
        /// <param name="texture">The texture itself will be output to this</param>
        /// <returns>True if the texture is found, false otherwise.</returns>
        internal static bool TryGetTexture(string name, out Asset <Texture2D> texture)
        {
            texture = null;

            if (Main.dedServ || string.IsNullOrWhiteSpace(name) || !name.Contains('/'))
            {
                return(false);
            }

            SplitName(name, out string modName, out string subName);

            if (modName == "Terraria")
            {
                if ((Main.instance.Content as TMLContentManager).ImageExists(subName))
                {
                    texture = Main.Assets.Request <Texture2D>(subName);

                    return(true);
                }

                return(false);
            }

            if (ModLoader.TryGetMod(modName, out var mod) && mod.TextureExists(subName))
            {
                texture = mod.GetTexture(subName);

                return(true);
            }

            return(false);
        }
예제 #2
0
        /// <summary>
        /// Returns whether or not a file with the specified name exists.
        /// </summary>
        public static bool FileExists(string name)
        {
            if (!name.Contains('/'))
            {
                return(false);
            }

            SplitName(name, out string modName, out string subName);

            return(ModLoader.TryGetMod(modName, out var mod) && mod.FileExists(subName));
        }
예제 #3
0
        /// <summary>
        /// Gets the byte representation of the file with the specified name. The name is in the format of "ModFolder/OtherFolders/FileNameWithExtension". Throws an ArgumentException if the file does not exist.
        /// </summary>
        /// <exception cref="MissingResourceException">Missing mod: " + name</exception>
        public static byte[] GetFileBytes(string name)
        {
            SplitName(name, out string modName, out string subName);

            if (!ModLoader.TryGetMod(modName, out var mod))
            {
                throw new MissingResourceException("Missing mod: " + name);
            }

            return(mod.GetFileBytes(subName));
        }
예제 #4
0
        /// <summary>
        /// Finds a command by name. Handles mod prefixing. Replies with error messages.
        /// </summary>
        /// <param name="mc">The found command, or null if an error was encountered.</param>
        /// <returns>True if a ModCommand was found, or an error message was replied. False if the command is unrecognized.</returns>
        internal static bool GetCommand(CommandCaller caller, string name, out ModCommand mc)
        {
            string modName = null;

            if (name.Contains(':'))
            {
                var split = name.Split(':');
                modName = split[0];
                name    = split[1];
            }

            mc = null;

            if (!Commands.TryGetValue(name, out List <ModCommand> cmdList))
            {
                return(false);
            }

            cmdList = cmdList.Where(c => Matches(c.Type, caller.CommandType)).ToList();
            if (cmdList.Count == 0)
            {
                return(false);
            }

            if (modName != null)
            {
                if (!ModLoader.TryGetMod(modName, out var mod))
                {
                    caller.Reply("Unknown Mod: " + modName, Color.Red);
                }
                else
                {
                    mc = cmdList.SingleOrDefault(c => c.Mod == mod);
                    if (mc == null)
                    {
                        caller.Reply("Mod: " + modName + " does not have a " + name + " command.", Color.Red);
                    }
                }
            }
            else if (cmdList.Count > 1)
            {
                caller.Reply("Multiple definitions of command /" + name + ". Try:", Color.Red);
                foreach (var c in cmdList)
                {
                    caller.Reply(c.Mod.Name + ":" + c.Command, Color.LawnGreen);
                }
            }
            else
            {
                mc = cmdList[0];
            }
            return(true);
        }
예제 #5
0
        /// <summary>
        /// Gets the music with the specified name. The name is in the same format as for texture names. Throws an ArgumentException if the music does not exist. Note: SoundMP3 is in the Terraria.ModLoader namespace.
        /// </summary>
        /// <exception cref="MissingResourceException">Missing mod: " + name</exception>
        public static Music GetMusic(string name)
        {
            if (Main.dedServ)
            {
                return(null);
            }

            SplitName(name, out string modName, out string subName);

            if (!ModLoader.TryGetMod(modName, out var mod))
            {
                throw new MissingResourceException("Missing mod: " + name);
            }

            return(mod.GetMusic(subName));
        }
예제 #6
0
        /// <summary>
        /// Returns whether or not a texture with the specified name exists.
        /// </summary>
        public static bool TextureExists(string name)
        {
            if (Main.dedServ || string.IsNullOrWhiteSpace(name) || !name.Contains('/'))
            {
                return(false);
            }

            SplitName(name, out string modName, out string subName);

            if (modName == "Terraria")
            {
                return((Main.instance.Content as TMLContentManager).ImageExists(subName));
            }

            return(ModLoader.TryGetMod(modName, out var mod) && mod.TextureExists(subName));
        }
예제 #7
0
        /// <summary>
        /// Gets the texture with the specified name. The name is in the format of "ModFolder/OtherFolders/FileNameWithoutExtension". Throws an ArgumentException if the texture does not exist. If a vanilla texture is desired, the format "Terraria/Images/FileNameWithoutExtension" will reference an image from the "terraria/Images/Content" folder. Note: Texture2D is in the Microsoft.Xna.Framework.Graphics namespace.
        /// </summary>
        /// <exception cref="MissingResourceException">Missing mod: " + name</exception>
        public static Asset <Texture2D> GetTexture(string name)
        {
            if (Main.dedServ)
            {
                return(null);
            }

            SplitName(name, out string modName, out string subName);

            if (modName == "Terraria")
            {
                return(Main.Assets.Request <Texture2D>(subName));
            }

            if (!ModLoader.TryGetMod(modName, out var mod))
            {
                throw new MissingResourceException($"Missing mod: {name}");
            }

            return(mod.GetTexture(subName));
        }
예제 #8
0
        // Receive a mod when connecting to server
        internal static void ReceiveMod(BinaryReader reader)
        {
            if (downloadingMod == null)
            {
                return;
            }

            try {
                if (downloadingFile == null)
                {
                    Interface.progress.Show(displayText: reader.ReadString(), cancel: CancelDownload);

                    if (ModLoader.TryGetMod(downloadingMod.name, out var mod))
                    {
                        mod.Close();
                    }

                    downloadingLength = reader.ReadInt64();
                    downloadingFile   = new FileStream(downloadingMod.path, FileMode.Create);
                    return;
                }

                byte[] bytes = reader.ReadBytes((int)Math.Min(downloadingLength - downloadingFile.Position, CHUNK_SIZE));
                downloadingFile.Write(bytes, 0, bytes.Length);
                Interface.progress.Progress = downloadingFile.Position / (float)downloadingLength;

                if (downloadingFile.Position == downloadingLength)
                {
                    downloadingFile.Close();

                    var mod = new TmodFile(downloadingMod.path);

                    using (mod.Open()) { }

                    if (!downloadingMod.Matches(mod))
                    {
                        throw new Exception(Language.GetTextValue("tModLoader.MPErrorModHashMismatch"));
                    }

                    if (downloadingMod.signed && !mod.ValidModBrowserSignature)
                    {
                        throw new Exception(Language.GetTextValue("tModLoader.MPErrorModNotSigned"));
                    }

                    ModLoader.EnableMod(mod.name);

                    if (downloadQueue.Count > 0)
                    {
                        DownloadNextMod();
                    }
                    else
                    {
                        OnModsDownloaded(true);
                    }
                }
            }
            catch (Exception e) {
                try {
                    downloadingFile?.Close();
                    File.Delete(downloadingMod.path);
                }
                catch (Exception exc2) {
                    Logging.tML.Error("Unknown error during mod sync", exc2);
                }

                string msg = Language.GetTextValue("tModLoader.MPErrorModDownloadError", downloadingMod.name);
                Logging.tML.Error(msg, e);
                Interface.errorMessage.Show(msg + e, 0);

                Netplay.Disconnect = true;
                downloadingMod     = null;
            }
        }