예제 #1
0
        /// <summary>
        /// Do NOT call this method from update thread.
        /// </summary>
        public static ResultData DownloadModsBlocking(List<SubscribedItem> mods)
        {
            int counter = 0;
            string numMods = mods.Count.ToString();
            VRage.Collections.CachingList<SubscribedItem> failedMods = new VRage.Collections.CachingList<SubscribedItem>();
            VRage.Collections.CachingList<SubscribedItem> mismatchMods = new VRage.Collections.CachingList<SubscribedItem>();
            bool downloadingFailed = false;

            long startTime = Stopwatch.GetTimestamp();
            Parallel.ForEach<SubscribedItem>(mods, delegate(SubscribedItem mod)
            {
                if (!MySteam.IsOnline)
                {
                    downloadingFailed = true;
                    return;
                }

                if (m_stop)
                {
                    downloadingFailed = true;
                    return;
                }


                bool devTagMismatch = mod.Tags != null && mod.Tags.Contains(MySteamWorkshop.WORKSHOP_DEVELOPMENT_TAG) && MyFinalBuildConstants.IS_STABLE;

                if (devTagMismatch)
                {
                    mismatchMods.Add(mod);
                }

                var localPackedModFullPath = Path.Combine(m_workshopModsPath, mod.PublishedFileId + m_workshopModSuffix);

                // If mod is up to date, no need to download it.
                if (!IsModUpToDateBlocking(localPackedModFullPath, mod, true))
                {
                    // If the mod fails to download, we need to flag it for failure, log it, then stop
                    if (!DownloadItemBlocking(localPackedModFullPath, mod.UGCHandle))
                    {
                        failedMods.Add(mod);
                        downloadingFailed = true;
                        m_stop = true;
                    }
                }
                else
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Up to date mod: Id = {0}, title = '{1}'", mod.PublishedFileId, mod.Title));
                }

                if (m_asyncDownloadScreen != null)
                {
                    using (m_modLock.AcquireExclusiveUsing())
                    {
                        counter++;
                        m_asyncDownloadScreen.ProgressTextString = MyTexts.GetString(MyCommonTexts.ProgressTextDownloadingMods) + " " + counter.ToString() + " of " + numMods;
                    }
                }
                
            });

            long endTime = Stopwatch.GetTimestamp();

            if (downloadingFailed)
            {
                failedMods.ApplyChanges();
                if (failedMods.Count > 0)
                {
                    foreach (var mod in failedMods)
                    {
                        MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to download mod: Id = {0}, title = '{1}'", mod.PublishedFileId, mod.Title));
                    }
                }
                else if (!m_stop)
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to download mods because Steam is not in Online Mode."));
                }
                else
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to download mods because download was stopped."));
                }
                return new ResultData();
            }

            ResultData ret = new ResultData();
            ret.Success = true;
            ret.MismatchMods = "";
            mismatchMods.ApplyChanges();
            foreach(var mod in mismatchMods)
            {
                ret.MismatchMods += mod.Title + Environment.NewLine; 
            }

            double duration = (double)(endTime - startTime) / (double)Stopwatch.Frequency;
            MySandboxGame.Log.WriteLineAndConsole(string.Format("Mod download time: {0:0.00} seconds", duration));
            return ret;
        }
        /// <summary>
        /// Do NOT call this method from update thread.
        /// </summary>
        public static bool DownloadModsBlocking(List<SubscribedItem> mods)
        {
            VRage.Collections.CachingList<SubscribedItem> failedMods = new VRage.Collections.CachingList<SubscribedItem>();
            bool downloadingFailed = false;

            long startTime = Stopwatch.GetTimestamp();
            Parallel.ForEach<SubscribedItem>(mods, delegate(SubscribedItem mod)
            {
                if (!MySteam.IsOnline)
                {
                    downloadingFailed = true;
                    return;
                }

                if (m_stop)
                {
                    downloadingFailed = true;
                    return;
                }

                var localPackedModFullPath = Path.Combine(m_workshopModsPath, mod.PublishedFileId + m_workshopModSuffix);

                // If mod is up to date, no need to download it.
                if (!IsModUpToDateBlocking(localPackedModFullPath, mod, true))
                {
                    // If the mod fails to download, we need to flag it for failure, log it, then stop
                    if (!DownloadItemBlocking(localPackedModFullPath, mod.UGCHandle))
                    {
                        failedMods.Add(mod);
                        downloadingFailed = true;
                        m_stop = true;
                    }
                }
                else
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Up to date mod: Id = {0}, title = '{1}'", mod.PublishedFileId, mod.Title));
                }
            });

            long endTime = Stopwatch.GetTimestamp();

            if (downloadingFailed)
            {
                failedMods.ApplyChanges();
                if (failedMods.Count > 0)
                {
                    foreach (var mod in failedMods)
                    {
                        MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to download mod: Id = {0}, title = '{1}'", mod.PublishedFileId, mod.Title));
                    }
                }
                else if (!m_stop)
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to download mods because Steam is not in Online Mode."));
                }
                else
                {
                    MySandboxGame.Log.WriteLineAndConsole(string.Format("Failed to download mods because download was stopped."));
                }
                return false;
            }

            double duration = (double)(endTime - startTime) / (double)Stopwatch.Frequency;
            MySandboxGame.Log.WriteLineAndConsole(string.Format("Mod download time: {0:0.00} seconds", duration));
            return true;
        }