Exemple #1
0
 internal static void HandleFileAssociation(string file)
 {
     Console.WriteLine($"Attempting to install {file}");
     if (File.Exists(file))
     {
         string modName = Path.GetFileNameWithoutExtension(file);
         if (ModLoader.ModPath != Path.GetDirectoryName(file))
         {
             File.Copy(file, Path.Combine(ModLoader.ModPath, Path.GetFileName(file)), true);
             File.Delete(file);
             Console.WriteLine($"{modName} installed successfully");
         }
         ModLoader.EnableMod(modName);
         Console.WriteLine($"{modName} enabled");
     }
     Console.WriteLine("Press any key to exit...");
     Console.ReadKey();
     Environment.Exit(0);
 }
Exemple #2
0
        private void Build(BuildingMod mod)
        {
            try {
                status.SetStatus(Language.GetTextValue("tModLoader.Building", mod.Name));

                List <LocalMod> refMods = null;
                BuildModForPlatform(mod, ref refMods, true);
                BuildModForPlatform(mod, ref refMods, false);

                if (Program.LaunchParameters.ContainsKey("-eac"))
                {
                    mod.properties.eacPath = Path.ChangeExtension(Program.LaunchParameters["-eac"], "pdb");
                    status.SetStatus(Language.GetTextValue("tModLoader.EnabledEAC", mod.properties.eacPath));
                }

                mod.modFile.AddFile("Info", mod.properties.ToBytes());

                status.SetStatus(Language.GetTextValue("tModLoader.Packaging", mod));
                status.SetProgress(0, 1);

                var resources = Directory.GetFiles(mod.path, "*", SearchOption.AllDirectories)
                                .Where(res => !IgnoreResource(mod, res))
                                .ToList();

                status.SetProgress(packedResourceCount = 0, resources.Count);
                Parallel.ForEach(resources, resource => AddResource(mod, resource));


                ModLoader.GetMod(mod.Name)?.File?.Close();                 // if the mod is currently loaded, the file-handle needs to be released
                mod.modFile.Save();
                mod.modFile.Close();
                ModLoader.EnableMod(mod.Name);
            } catch (Exception e) {
                e.Data["mod"] = mod.Name;
                throw;
            }
        }
Exemple #3
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);

                    ModLoader.GetMod(downloadingMod.name)?.Close();
                    downloadingLength = reader.ReadInt64();
                    downloadingFile   = new FileStream(downloadingMod.path, FileMode.Create);
                    return;
                }

                var 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);
                }

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

                Netplay.disconnect = true;
                downloadingMod     = null;
            }
        }
Exemple #4
0
        internal static void ReceiveMod(BinaryReader reader)
        {
            if (downloadingMod == null)
            {
                return;
            }

            try
            {
                if (downloadingFile == null)
                {
                    Interface.downloadMod.SetDownloading(reader.ReadString());
                    Interface.downloadMod.SetCancel(() =>
                    {
                        downloadingFile?.Close();
                        downloadingMod     = null;
                        Netplay.disconnect = true;
                        Main.menuMode      = 0;
                    });
                    Main.menuMode = Interface.downloadModID;

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

                var bytes = reader.ReadBytes((int)Math.Min(downloadingLength - downloadingFile.Position, CHUNK_SIZE));
                downloadingFile.Write(bytes, 0, bytes.Length);
                Interface.downloadMod.SetProgress(downloadingFile.Position, downloadingLength);

                if (downloadingFile.Position == downloadingLength)
                {
                    downloadingFile.Close();
                    var mod = new TmodFile(downloadingMod.path);
                    mod.Read(TmodFile.LoadedState.Info);

                    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();
                }
                catch { }

                File.Delete(downloadingMod.path);
                ErrorLogger.LogException(e, Language.GetTextValue("tModLoader.MPErrorModDownloadError", downloadingMod.name));
                downloadingMod = null;
            }
        }
Exemple #5
0
        internal static void ServerModMenu()
        {
            bool exit = false;

            while (!exit)
            {
                Console.WriteLine("Terraria Server " + Main.versionNumber2 + " - " + ModLoader.versionedName);
                Console.WriteLine();
                TmodFile[] mods = ModLoader.FindMods();
                for (int k = 0; k < mods.Length; k++)
                {
                    BuildProperties properties = BuildProperties.ReadModFile(mods[k]);
                    string          name       = properties.displayName;
                    name = mods[k].name;
                    string line = (k + 1) + "\t\t" + name + "(";
                    line += (ModLoader.IsEnabled(mods[k]) ? "enabled" : "disabled") + ")";
                    Console.WriteLine(line);
                }
                Console.WriteLine("e\t\tEnable All");
                Console.WriteLine("d\t\tDisable All");
                Console.WriteLine("r\t\tReload and return to world menu");
                Console.WriteLine("Type a number to switch between enabled/disabled");
                Console.WriteLine();
                Console.WriteLine("Type a command: ");
                string command = Console.ReadLine();
                if (command == null)
                {
                    command = "";
                }
                command = command.ToLower();
                Console.Clear();
                if (command == "e")
                {
                    foreach (TmodFile mod in mods)
                    {
                        ModLoader.EnableMod(mod);
                    }
                }
                else if (command == "d")
                {
                    foreach (TmodFile mod in mods)
                    {
                        ModLoader.DisableMod(mod);
                    }
                }
                else if (command == "r")
                {
                    Console.WriteLine("Unloading mods...");
                    ModLoader.Unload();
                    ModLoader.do_Load(null);
                    exit = true;
                }
                else
                {
                    int value;
                    if (Int32.TryParse(command, out value))
                    {
                        value--;
                        if (value >= 0 && value < mods.Length)
                        {
                            ModLoader.SetModActive(mods[value], !ModLoader.IsEnabled(mods[value]));
                        }
                    }
                }
            }
        }
Exemple #6
0
        internal static void ServerModMenu()
        {
            bool exit = false;

            while (!exit)
            {
                Console.WriteLine("Terraria Server " + Main.versionNumber2 + " - " + ModLoader.versionedName);
                Console.WriteLine();
                TmodFile[] mods = ModLoader.FindMods();
                for (int k = 0; k < mods.Length; k++)
                {
                    BuildProperties properties = BuildProperties.ReadModFile(mods[k]);
                    string          name       = properties.displayName;
                    name = mods[k].name;
                    string line = (k + 1) + "\t\t" + name + "(";
                    line += (ModLoader.IsEnabled(mods[k]) ? "enabled" : "disabled") + ")";
                    Console.WriteLine(line);
                }
                if (mods.Length == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"No mods were found in: \"{ModLoader.ModPath}\"\nIf you are running a dedicated server, you may wish to use the 'modpath' command line switch or server config setting to specify a custom mods directory.\n");
                    Console.ResetColor();
                }
                Console.WriteLine("e\t\tEnable All");
                Console.WriteLine("d\t\tDisable All");
                Console.WriteLine("r\t\tReload and return to world menu");
                Console.WriteLine("Type a number to switch between enabled/disabled");
                Console.WriteLine();
                Console.WriteLine("Type a command: ");
                string command = Console.ReadLine();
                if (command == null)
                {
                    command = "";
                }
                command = command.ToLower();
                Console.Clear();
                if (command == "e")
                {
                    foreach (TmodFile mod in mods)
                    {
                        ModLoader.EnableMod(mod);
                    }
                }
                else if (command == "d")
                {
                    foreach (TmodFile mod in mods)
                    {
                        ModLoader.DisableMod(mod);
                    }
                }
                else if (command == "r")
                {
                    Console.WriteLine("Unloading mods...");
                    ModLoader.Unload();
                    ModLoader.do_Load(null);
                    exit = true;
                }
                else
                {
                    int value;
                    if (Int32.TryParse(command, out value))
                    {
                        value--;
                        if (value >= 0 && value < mods.Length)
                        {
                            ModLoader.SetModActive(mods[value], !ModLoader.IsEnabled(mods[value]));
                        }
                    }
                }
            }
        }
Exemple #7
0
        internal static void ReceiveMod(BinaryReader reader)
        {
            if (downloadingMod == null)
            {
                return;
            }

            try {
                if (downloadingFile == null)
                {
                    // TODO migrate to download UI, create new StreamingDownloadRequest
                    // TODO I hope this works fine without UI for the moment...
                    //Interface.downloadMod.SetDownloading(reader.ReadString());
                    //Interface.downloadMod.SetCancel(CancelDownload);
                    Main.menuMode = Interface.downloadModID;

                    ModLoader.GetMod(downloadingMod.name)?.File?.Close();
                    downloadingLength = reader.ReadInt64();
                    downloadingFile   = new FileStream(downloadingMod.path, FileMode.Create);
                    return;
                }

                var bytes = reader.ReadBytes((int)Math.Min(downloadingLength - downloadingFile.Position, CHUNK_SIZE));
                downloadingFile.Write(bytes, 0, bytes.Length);
                //Interface.downloadMod.SetProgress(downloadingFile.Position, downloadingLength);

                if (downloadingFile.Position == downloadingLength)
                {
                    downloadingFile.Close();
                    var mod = new TmodFile(downloadingMod.path);
                    mod.Read();
                    mod.Close();

                    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 { }

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

                Netplay.disconnect = true;
                downloadingMod     = null;
            }
        }
Exemple #8
0
        internal static void SyncClientMods(BinaryReader reader)
        {
            AllowVanillaClients = reader.ReadBoolean();

            Main.statusText = "Syncing Mods";
            var clientMods  = ModLoader.LoadedMods;
            var modFiles    = ModLoader.FindMods();
            var needsReload = false;

            downloadQueue.Clear();
            var syncSet     = new HashSet <string>();
            var blockedList = new List <ModHeader>();

            int n = reader.ReadInt32();

            for (int i = 0; i < n; i++)
            {
                var header = new ModHeader(reader.ReadString(), new Version(reader.ReadString()), reader.ReadBytes(20), reader.ReadBoolean());
                syncSet.Add(header.name);

                var clientMod = clientMods.SingleOrDefault(m => m.Name == header.name);
                if (clientMod != null)
                {
                    if (header.Matches(clientMod.File))
                    {
                        continue;
                    }

                    header.path = clientMod.File.path;
                }
                else
                {
                    var disabledVersions = modFiles.Where(m => m.name == header.name).ToArray();
                    var matching         = disabledVersions.FirstOrDefault(header.Matches);
                    if (matching != null)
                    {
                        ModLoader.EnableMod(matching);
                        needsReload = true;
                        continue;
                    }

                    if (disabledVersions.Length > 0)
                    {
                        header.path = disabledVersions[0].path;
                    }
                }

                if (downloadModsFromServers && (header.signed || !onlyDownloadSignedMods))
                {
                    downloadQueue.Enqueue(header);
                }
                else
                {
                    blockedList.Add(header);
                }
            }

            foreach (var mod in clientMods)
            {
                if (mod.Side == ModSide.Both && !syncSet.Contains(mod.Name))
                {
                    ModLoader.DisableMod(mod.File);
                    needsReload = true;
                }
            }

            if (blockedList.Count > 0)
            {
                var msg = "The following mods are installed on the server but cannot be downloaded ";
                msg += downloadModsFromServers
                                        ? "because you only accept mods signed by the mod browser"
                                        : "because you have disabled automatic mod downloading";
                msg += ".\nYou will need to change your settings or acquire the mods from the server owner.\n";
                foreach (var mod in blockedList)
                {
                    msg += "\n    " + mod;
                }

                ErrorLogger.LogMissingMods(msg);
                return;
            }

            if (downloadQueue.Count > 0)
            {
                DownloadNextMod();
            }
            else
            {
                OnModsDownloaded(needsReload);
            }
        }