示例#1
0
        public void ExtractEmptyDirectories()
        {
            var tempFilePath = GetTempFilePath();
            Assert.IsNotNull(tempFilePath, "No permission to execute this test?");

            var name = Path.Combine(tempFilePath, "x.zip");

            EnsureTestDirectoryIsEmpty(tempFilePath);

            var targetDir = Path.Combine(tempFilePath, ZipTempDir + @"\floyd");
            using (var fs = File.Create(name))
            {
                using (var zOut = new ZipOutputStream(fs))
                {
                    zOut.PutNextEntry(new ZipEntry("floyd/"));
                }
            }

            var fastZip = new FastZip();
            fastZip.CreateEmptyDirectories = true;
            fastZip.ExtractZip(name, targetDir, "zz");

            File.Delete(name);
            Assert.IsTrue(Directory.Exists(targetDir), "Empty directory should be created");
        }
示例#2
0
        public void NonAsciiPasswords()
        {
            const string tempName1 = "a.dat";

            var target = new MemoryStream();

            var tempFilePath = GetTempFilePath();
            Assert.IsNotNull(tempFilePath, "No permission to execute this test?");

            var addFile = Path.Combine(tempFilePath, tempName1);
            MakeTempFile(addFile, 1);

            var password = "******";
            try
            {
                var fastZip = new FastZip();
                fastZip.Password = password;

                fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null);

                var archive = new MemoryStream(target.ToArray());
                using (var zf = new ZipFile(archive))
                {
                    zf.Password = password;
                    Assert.AreEqual(1, zf.Count);
                    var entry = zf[0];
                    Assert.AreEqual(tempName1, entry.Name);
                    Assert.AreEqual(1, entry.Size);
                    Assert.IsTrue(zf.TestArchive(true));
                    Assert.IsTrue(entry.IsCrypted);
                }
            }
            finally
            {
                File.Delete(tempName1);
            }
        }
示例#3
0
        public static void UnZip(string zip_path, string target_dir)
        {
            FastZip fastzip = new FastZip();

            fastzip.ExtractZip(zip_path, target_dir, null);
        }
示例#4
0
        public static void StartAsync(SimulationModel simulation)
        {
            Debug.Assert(Instance.CurrentSimulation == null);

            Instance.Actions.Enqueue(() =>
            {
                using (var db = DatabaseManager.Open())
                {
                    AssetBundle textureBundle = null;
                    AssetBundle mapBundle     = null;
                    try
                    {
                        if (Config.Headless && (simulation.Headless.HasValue && !simulation.Headless.Value))
                        {
                            throw new Exception("Simulator is configured to run in headless mode, only headless simulations are allowed");
                        }

                        simulation.Status = "Starting";
                        NotificationManager.SendNotification("simulation", SimulationResponse.Create(simulation), simulation.Owner);
                        Instance.LoaderUI.SetLoaderUIState(LoaderUI.LoaderUIStateType.PROGRESS);

                        Instance.SimConfig = new SimulationConfig()
                        {
                            Name           = simulation.Name,
                            Clusters       = db.Single <ClusterModel>(simulation.Cluster).Ips.Split(',').Where(c => c != "127.0.0.1").ToArray(),
                            ClusterName    = db.Single <ClusterModel>(simulation.Cluster).Name,
                            ApiOnly        = simulation.ApiOnly.GetValueOrDefault(),
                            Headless       = simulation.Headless.GetValueOrDefault(),
                            Interactive    = simulation.Interactive.GetValueOrDefault(),
                            TimeOfDay      = simulation.TimeOfDay.GetValueOrDefault(new DateTime(1980, 3, 24, 12, 0, 0)),
                            Rain           = simulation.Rain.GetValueOrDefault(),
                            Fog            = simulation.Fog.GetValueOrDefault(),
                            Wetness        = simulation.Wetness.GetValueOrDefault(),
                            Cloudiness     = simulation.Cloudiness.GetValueOrDefault(),
                            UseTraffic     = simulation.UseTraffic.GetValueOrDefault(),
                            UsePedestrians = simulation.UsePedestrians.GetValueOrDefault(),
                            Seed           = simulation.Seed,
                        };

                        if (simulation.Vehicles == null || simulation.Vehicles.Length == 0 || simulation.ApiOnly.GetValueOrDefault())
                        {
                            Instance.SimConfig.Agents = Array.Empty <AgentConfig>();
                        }
                        else
                        {
                            Instance.SimConfig.Agents = simulation.Vehicles.Select(v =>
                            {
                                var vehicle = db.SingleOrDefault <VehicleModel>(v.Vehicle);

                                var config = new AgentConfig()
                                {
                                    Name        = vehicle.Name,
                                    Url         = vehicle.Url,
                                    AssetBundle = vehicle.LocalPath,
                                    Connection  = v.Connection,
                                    Sensors     = vehicle.Sensors,
                                };

                                if (!string.IsNullOrEmpty(vehicle.BridgeType))
                                {
                                    config.Bridge = Config.Bridges.Find(bridge => bridge.Name == vehicle.BridgeType);
                                    if (config.Bridge == null)
                                    {
                                        throw new Exception($"Bridge {vehicle.BridgeType} not found");
                                    }
                                }

                                return(config);
                            }).ToArray();
                        }

                        //Initialize network for the master, client has initialized network since startup
                        if (Instance.SimConfig.Clusters == null || Instance.SimConfig.Clusters.Length == 0)
                        {
                            if (Config.RunAsMaster)
                            {
                                Instance.Network.Initialize(SimulationNetwork.ClusterNodeType.NotClusterNode, Instance.NetworkSettings);
                            }
                        }
                        else if (Config.RunAsMaster)
                        {
                            Instance.Network.Initialize(SimulationNetwork.ClusterNodeType.Master, Instance.NetworkSettings);
                            Instance.Network.Master.Simulation = Instance.SimConfig;
                        }

                        // load environment
                        if (Instance.SimConfig.ApiOnly)
                        {
                            var api  = Instantiate(Instance.ApiManagerPrefab);
                            api.name = "ApiManager";

                            Instance.CurrentSimulation = simulation;

                            // ready to go!
                            Instance.CurrentSimulation.Status = "Running";
                            NotificationManager.SendNotification("simulation", SimulationResponse.Create(simulation), simulation.Owner);

                            Instance.LoaderUI.SetLoaderUIState(LoaderUI.LoaderUIStateType.READY);
                        }
                        else
                        {
                            var mapModel      = db.Single <MapModel>(simulation.Map);
                            var mapBundlePath = mapModel.LocalPath;
                            mapBundle         = null;
                            textureBundle     = null;

                            ZipFile zip = new ZipFile(mapBundlePath);
                            {
                                string manfile;
                                ZipEntry entry = zip.GetEntry("manifest");
                                using (var ms = zip.GetInputStream(entry))
                                {
                                    int streamSize = (int)entry.Size;
                                    byte[] buffer  = new byte[streamSize];
                                    streamSize     = ms.Read(buffer, 0, streamSize);
                                    manfile        = Encoding.UTF8.GetString(buffer);
                                }

                                Manifest manifest;

                                try
                                {
                                    manifest = new Deserializer().Deserialize <Manifest>(manfile);
                                }
                                catch
                                {
                                    throw new Exception("Out of date AssetBundle, rebuild or download latest AssetBundle.");
                                }

                                if (manifest.additionalFiles != null)
                                {
                                    foreach (string key in manifest.additionalFiles.Keys)
                                    {
                                        if (key.Contains("pointcloud"))
                                        {
                                            if (!Directory.Exists(Path.Combine(Application.persistentDataPath, manifest.assetGuid)))
                                            {
                                                Directory.CreateDirectory(Path.Combine(Application.persistentDataPath, manifest.assetGuid));
                                                FastZip fastZip = new FastZip();
                                                fastZip.ExtractZip(mapBundlePath, Path.Combine(Application.persistentDataPath, manifest.assetGuid), ".*\\.(pcnode|pcindex|pcmesh)$");
                                            }
                                        }
                                    }
                                }

                                if (manifest.bundleFormat != BundleConfig.Versions[BundleConfig.BundleTypes.Environment])
                                {
                                    zip.Close();

                                    // TODO: proper exception
                                    throw new ZipException("BundleFormat version mismatch");
                                }

                                if (zip.FindEntry($"{manifest.assetGuid}_environment_textures", false) != -1)
                                {
                                    var texStream = zip.GetInputStream(zip.GetEntry($"{manifest.assetGuid}_environment_textures"));
                                    textureBundle = AssetBundle.LoadFromStream(texStream, 0, 1 << 20);
                                }

                                string platform = SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows ? "windows" : "linux";
                                var mapStream   = zip.GetInputStream(zip.GetEntry($"{manifest.assetGuid}_environment_main_{platform}"));
                                mapBundle       = AssetBundle.LoadFromStream(mapStream, 0, 1 << 20);

                                if (mapBundle == null)
                                {
                                    throw new Exception($"Failed to load environment from '{mapModel.Name}' asset bundle");
                                }

                                textureBundle?.LoadAllAssets();

                                var scenes = mapBundle.GetAllScenePaths();
                                if (scenes.Length != 1)
                                {
                                    throw new Exception($"Unsupported environment in '{mapModel.Name}' asset bundle, only 1 scene expected");
                                }

                                var sceneName = Path.GetFileNameWithoutExtension(scenes[0]);
                                Instance.SimConfig.MapName = sceneName;
                                Instance.SimConfig.MapUrl  = mapModel.Url;

                                var isMasterSimulation = Instance.SimConfig.Clusters.Length > 0;
                                var loader             =
                                    SceneManager.LoadSceneAsync(sceneName,
                                                                isMasterSimulation ? LoadSceneMode.Additive : LoadSceneMode.Single);
                                loader.completed += op =>
                                {
                                    if (op.isDone)
                                    {
                                        if (isMasterSimulation)
                                        {
                                            SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName));
                                        }
                                        textureBundle?.Unload(false);
                                        mapBundle.Unload(false);
                                        zip.Close();
                                        NodeTreeLoader[] loaders = FindObjectsOfType <NodeTreeLoader>();
                                        foreach (NodeTreeLoader l in loaders)
                                        {
                                            l.UpdateData(Path.Combine(Application.persistentDataPath, manifest.assetGuid, $"pointcloud_{Utilities.Utility.StringToGUID(l.GetDataPath())}".ToString()));
                                        }

                                        SetupScene(simulation);
                                    }
                                };
                            }
                        }
                    }
                    catch (ZipException ex)
                    {
                        Debug.Log($"Failed to start '{simulation.Name}' simulation");
                        Debug.LogException(ex);

                        // NOTE: In case of failure we have to update Simulation state
                        simulation.Status = "Invalid";
                        simulation.Error  = "Out of date Map AssetBundle. Please check content website for updated bundle or rebuild the bundle.";
                        db.Update(simulation);

                        if (SceneManager.GetActiveScene().name != Instance.LoaderScene)
                        {
                            SceneManager.LoadScene(Instance.LoaderScene);
                        }

                        textureBundle?.Unload(false);
                        mapBundle?.Unload(false);
                        AssetBundle.UnloadAllAssetBundles(true);
                        Instance.CurrentSimulation = null;

                        // TODO: take ex.Message and append it to response here
                        NotificationManager.SendNotification("simulation", SimulationResponse.Create(simulation), simulation.Owner);
                    }
                    catch (Exception ex)
                    {
                        Debug.Log($"Failed to start '{simulation.Name}' simulation");
                        Debug.LogException(ex);

                        // NOTE: In case of failure we have to update Simulation state
                        simulation.Status = "Invalid";
                        simulation.Error  = ex.Message;
                        db.Update(simulation);

                        if (SceneManager.GetActiveScene().name != Instance.LoaderScene)
                        {
                            SceneManager.LoadScene(Instance.LoaderScene);
                        }

                        textureBundle?.Unload(false);
                        mapBundle?.Unload(false);
                        AssetBundle.UnloadAllAssetBundles(true);
                        Instance.CurrentSimulation = null;

                        // TODO: take ex.Message and append it to response here
                        NotificationManager.SendNotification("simulation", SimulationResponse.Create(simulation), simulation.Owner);
                    }
                }
            });
        }
示例#5
0
        public ReleasePackage CreateDeltaPackage(ReleasePackage basePackage, ReleasePackage newPackage, string outputFile)
        {
            Contract.Requires(basePackage != null);
            Contract.Requires(!String.IsNullOrEmpty(outputFile) && !File.Exists(outputFile));

            if (basePackage.Version > newPackage.Version)
            {
                var message = String.Format(
                    "You cannot create a delta package based on version {0} as it is a later version than {1}",
                    basePackage.Version,
                    newPackage.Version);
                throw new InvalidOperationException(message);
            }

            if (basePackage.ReleasePackageFile == null)
            {
                throw new ArgumentException("The base package's release file is null", "basePackage");
            }

            if (!File.Exists(basePackage.ReleasePackageFile))
            {
                throw new FileNotFoundException("The base package release does not exist", basePackage.ReleasePackageFile);
            }

            if (!File.Exists(newPackage.ReleasePackageFile))
            {
                throw new FileNotFoundException("The new package release does not exist", newPackage.ReleasePackageFile);
            }

            string baseTempPath = null;
            string tempPath     = null;

            using (Utility.WithTempDirectory(out baseTempPath))
                using (Utility.WithTempDirectory(out tempPath)) {
                    var baseTempInfo = new DirectoryInfo(baseTempPath);
                    var tempInfo     = new DirectoryInfo(tempPath);

                    this.Log().Info("Extracting {0} and {1} into {2}",
                                    basePackage.ReleasePackageFile, newPackage.ReleasePackageFile, tempPath);

                    var fz = new FastZip();
                    fz.ExtractZip(basePackage.ReleasePackageFile, baseTempInfo.FullName, null);
                    fz.ExtractZip(newPackage.ReleasePackageFile, tempInfo.FullName, null);

                    // Collect a list of relative paths under 'lib' and map them
                    // to their full name. We'll use this later to determine in
                    // the new version of the package whether the file exists or
                    // not.
                    var baseLibFiles = baseTempInfo.GetAllFilesRecursively()
                                       .Where(x => x.FullName.ToLowerInvariant().Contains("lib" + Path.DirectorySeparatorChar))
                                       .ToDictionary(k => k.FullName.Replace(baseTempInfo.FullName, ""), v => v.FullName);

                    var newLibDir = tempInfo.GetDirectories().First(x => x.Name.ToLowerInvariant() == "lib");

                    foreach (var libFile in newLibDir.GetAllFilesRecursively())
                    {
                        createDeltaForSingleFile(libFile, tempInfo, baseLibFiles);
                    }

                    ReleasePackage.addDeltaFilesToContentTypes(tempInfo.FullName);
                    fz.CreateZip(outputFile, tempInfo.FullName, true, null);
                }

            return(new ReleasePackage(outputFile));
        }
示例#6
0
        public static void ZipDirectory(string directory, string output_filename)
        {
            var fastzip = new FastZip();

            fastzip.CreateZip(output_filename, directory, true, "");
        }
        public IActionResult New(NewViewModel model, int[] ProblemLabels, int[] RecommendedValue)
        {
            var user = HttpContext.Session.Get <UserProfileModel>("CurrentUser");

            if (user == null)
            {
                TempData["Message"] = "请先登录";
                return(RedirectToAction("Login", "Auth"));
            }
            //创建新的Problem
            var problem = new Problem
            {
                OrderNumber = (1000 + _context.Problems.LongCount()).ToString(),
                Title       = model.Title,
                Description = model.Description,
                AuthorId    = user.Id.ToString(),
                PublishTime = DateTime.UtcNow,
                IsDelete    = false
            };

            var exampleData = new TestData
            {
                Input  = model.ExampleData.Input,
                Output = model.ExampleData.Output
            };

            problem.SetExampleData(exampleData);

            var judgeProfile = new JudgeProfile
            {
                MemoryLimit = model.MemoryLimit,
                TimeLimit   = model.TimeLimit,
            };

            problem.SetJudgeProfile(judgeProfile);

            problem.SetPassRate(new PassRate
            {
                Submit = 0,
                Pass   = 0
            });

            _context.Problems.Add(problem);
            _context.SaveChanges();


            //增加题目标签

            long ProblemId = _context.Problems.OrderByDescending(x => x.Id).FirstOrDefault().Id;

            int count = 0;

            foreach (var item in _context.LabelProfiles.ToList())
            {
                if (ProblemLabels.Length == count)
                {
                    break;
                }
                if (ProblemLabels[count] == item.Id)
                {
                    ProblemLabel problemLabel = new ProblemLabel();
                    problemLabel.LabelId   = item.Id.ToString();
                    problemLabel.ProblemId = ProblemId.ToString();
                    problemLabel.Weight    = RecommendedValue[count];
                    _context.ProblemLabels.Add(problemLabel);
                    _context.SaveChanges();
                    count++;
                }
            }



            //创建文件路径
            var judgeDataStorageDirectory = Path.Combine(Directory.GetCurrentDirectory(), "JudgeDataStorage");

            if (!Directory.Exists(judgeDataStorageDirectory))
            {
                Directory.CreateDirectory(judgeDataStorageDirectory);
            }

            //复制zip到指定路径
            var problemDirectory = Path.Combine(judgeDataStorageDirectory, _context.Problems.LongCount().ToString());

            Directory.CreateDirectory(problemDirectory);
            var zipFilePath = Path.Combine(problemDirectory, "judge.zip");

            var stream = new FileStream(zipFilePath, FileMode.Create);

            model.TestDatas.CopyTo(stream);
            stream.Close();

            //解压zip
            var targetDirectory = Path.Combine(problemDirectory, "data");
            var fastZip         = new FastZip();

            fastZip.ExtractZip(zipFilePath, targetDirectory, null);

            return(RedirectToAction(nameof(Index)));
        }
示例#8
0
        private async Task <string> DownloadRelease(List <Asset> assets, bool isWindows, string version)
        {
            var variants         = new Variants();
            var artifactFileName = variants.GetArtifactFileName(variant);
            var targetAsset      = assets.FirstOrDefault(a => a.Browser_download_url.EndsWith(artifactFileName, StringComparison.OrdinalIgnoreCase) && artifactFileName.Length > 0);

            if (targetAsset == null)
            {
                logger.Error("Failed to find asset to download!");
                return(null);
            }

            var url = targetAsset.Browser_download_url;

            var data = await client.GetResultAsync(SetDownloadHeaders(new WebRequest()
            {
                Url = url, EmulateBrowser = true, Type = RequestType.GET
            }));

            while (data.IsRedirect)
            {
                data = await client.GetResultAsync(new WebRequest()
                {
                    Url = data.RedirectingTo, EmulateBrowser = true, Type = RequestType.GET
                });
            }

            var tempDir = Path.Combine(Path.GetTempPath(), "JackettUpdate-" + version + "-" + DateTime.Now.Ticks);

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }

            Directory.CreateDirectory(tempDir);

            if (isWindows)
            {
                var zipPath = Path.Combine(tempDir, "Update.zip");
                File.WriteAllBytes(zipPath, data.ContentBytes);
                var fastZip = new FastZip();
                fastZip.ExtractZip(zipPath, tempDir, null);
            }
            else
            {
                var gzPath = Path.Combine(tempDir, "Update.tar.gz");
                File.WriteAllBytes(gzPath, data.ContentBytes);
                Stream inStream   = File.OpenRead(gzPath);
                Stream gzipStream = new GZipInputStream(inStream);

                var tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
                tarArchive.ExtractContents(tempDir);
                tarArchive.Close();
                gzipStream.Close();
                inStream.Close();

                if (variant == Variants.JackettVariant.CoreMacOs || variant == Variants.JackettVariant.CoreLinuxAmdx64 ||
                    variant == Variants.JackettVariant.CoreLinuxArm32 || variant == Variants.JackettVariant.CoreLinuxArm64 ||
                    variant == Variants.JackettVariant.Mono)
                {
                    //Calling the file permission service to limit usage to netcoreapp. The Mono.Posix.NETStandard library causes issues outside of .NET Core
                    //https://github.com/xamarin/XamarinComponents/issues/282

                    // When the files get extracted, the execute permission for jackett and JackettUpdater don't get carried across

                    var jackettPath = tempDir + "/Jackett/jackett";
                    filePermissionService.MakeFileExecutable(jackettPath);

                    var jackettUpdaterPath = tempDir + "/Jackett/JackettUpdater";
                    filePermissionService.MakeFileExecutable(jackettUpdaterPath);

                    if (variant == Variants.JackettVariant.CoreMacOs)
                    {
                        filePermissionService.MakeFileExecutable(tempDir + "/Jackett/install_service_macos");
                        filePermissionService.MakeFileExecutable(tempDir + "/Jackett/uninstall_jackett_macos");
                    }
                    else if (variant == Variants.JackettVariant.Mono)
                    {
                        var systemdPath = tempDir + "/Jackett/install_service_systemd_mono.sh";
                        filePermissionService.MakeFileExecutable(systemdPath);
                    }
                    else
                    {
                        var systemdPath = tempDir + "/Jackett/install_service_systemd.sh";
                        filePermissionService.MakeFileExecutable(systemdPath);

                        var launcherPath = tempDir + "/Jackett/jackett_launcher.sh";
                        filePermissionService.MakeFileExecutable(launcherPath);
                    }
                }
            }

            return(tempDir);
        }
示例#9
0
        static void DownloadBhavcopies(int type, int days, string dir)
        {
            //http://www.nseindia.com/content/historical/EQUITIES/2013/FEB/cm06FEB2013bhav.csv.zip
            //http://www.nseindia.com/content/historical/DERIVATIVES/2013/APR/fo01APR2013bhav.csv.zip

            string       baseURL     = "http://www.nseindia.com/content/historical/";
            const string REFERER_EQT = "http://www.nseindia.com/products/content/equities/equities/archieve_eq.htm";
            const string REFERER_FNO = "http://www.nseindia.com/products/content/derivatives/equities/historical_fo.htm";

            List <string> PRODUCT      = new List <string>(3);
            string        DOWNLOAD_DIR = dir;

            if (type == 0 || type == 2)
            {
                PRODUCT.Add("EQUITIES");
            }

            if (type == 1 || type == 2)
            {
                PRODUCT.Add("DERIVATIVES");
            }

            foreach (var prod in PRODUCT)
            {
                string productUrl  = baseURL + prod + "/";
                string downloadDir = Path.Combine(DOWNLOAD_DIR, prod);
                if (!Directory.Exists(downloadDir))
                {
                    Directory.CreateDirectory(downloadDir);
                }

                var st  = DateTime.Today.AddDays(-days);
                var end = DateTime.Today;

                var referer    = REFERER_FNO;
                var filePrefix = "fo";

                if (prod == "EQUITIES")
                {
                    filePrefix = "cm";
                    referer    = REFERER_EQT;
                }

                for (DateTime dt = st; dt.Date <= end.Date; dt = dt.AddDays(1))
                {
                    int    yr    = dt.Year;
                    string yrUrl = productUrl + yr + "/";

                    string mon    = dt.ToString("MMM").ToUpper();
                    string monUrl = yrUrl + mon + "/";
                    int    day    = dt.Day;

                    string fileName = filePrefix + (day <= 9 ? "0" + day : day.ToString()) + mon + yr.ToString() + "bhav.csv.zip";
                    string dtUrl    = monUrl + fileName;

                    var filePath = Path.Combine(downloadDir, fileName);

                    if (File.Exists(filePath.Replace(".zip", "")))
                    {
                        continue;
                    }

                    HttpHelper.DownloadZipFile(dtUrl, filePath, referer);

                    if (File.Exists(filePath))
                    {
                        FastZip fz = new FastZip();
                        fz.ExtractZip(filePath, downloadDir, null);
                        File.Delete(filePath);
                    }
                }
            }
        }
        private void LoadPrimPackage(string filename)
        {
            string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName());

            try
            {
                // Create a temporary directory
                Directory.CreateDirectory(tempPath);

                FastZip fastzip = new FastZip();
                fastzip.ExtractZip(filename, tempPath, String.Empty);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            // Check for the prims.xml file
            string primsFile = System.IO.Path.Combine(tempPath, "prims.xml");

            if (!File.Exists(primsFile))
            {
                MessageBox.Show("prims.xml not found in the archive");
                return;
            }

            OSD osd = null;

            try { osd = OSDParser.DeserializeLLSDXml(File.ReadAllText(primsFile)); }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

            if (osd != null && osd.Type == OSDType.Map)
            {
                List <Primitive> primList = Helpers.OSDToPrimList(osd);
                Prims = new List <FacetedMesh>(primList.Count);

                for (int i = 0; i < primList.Count; i++)
                {
                    Primitive   prim = primList[i];
                    FacetedMesh mesh = null;

                    if (prim.Sculpt.SculptTexture != UUID.Zero)
                    {
                        Image sculptTexture = null;
                        if (LoadTexture(tempPath, prim.Sculpt.SculptTexture, ref sculptTexture))
                        {
                            mesh = Render.Plugin.GenerateFacetedSculptMesh(prim, (Bitmap)sculptTexture, DetailLevel.Highest);
                        }
                    }
                    else
                    {
                        mesh = Render.Plugin.GenerateFacetedMesh(prim, DetailLevel.Highest);
                    }

                    if (mesh != null)
                    {
                        LoadMesh(mesh, tempPath);
                    }
                }

                // Setup the dropdown list of prims
                PopulatePrimCombobox();

                glControl.Invalidate();
            }
            else
            {
                MessageBox.Show("Failed to load LLSD formatted primitive data from " + filename);
            }

            Directory.Delete(tempPath);
        }
示例#11
0
        public override void Run()
        {
            if (!File.Exists(_hbManager.Settings.HonorbuddyPath))
            {
                _hbManager.Profile.Pause();
                _hbManager.Profile.Log(string.Format("path to honorbuddy.exe does not exist: {0}", _hbManager.Settings.HonorbuddyPath));
            }
            Log.Write("Checking for new  Honorbuddy update");
            // get local honorbuddy file version.
            FileVersionInfo localFileVersionInfo = FileVersionInfo.GetVersionInfo(_hbManager.Settings.HonorbuddyPath);
            // download the latest Honorbuddy version string from server
            var client = new WebClient {
                Proxy = null
            };
            string latestHbVersion = client.DownloadString(_hbManager.Profile.Settings.HonorbuddySettings.UseHBBeta ? HbBetaVersionUrl : HbVersionUrl);

            // check if local version is different from remote honorbuddy version.
            if (localFileVersionInfo.FileVersion != latestHbVersion)
            {
                Log.Write("New version of Honorbuddy is available.");
                var originalFileName = Path.GetFileName(_hbManager.Settings.HonorbuddyPath);
                // close all instances of Honorbuddy
                Log.Write("Closing all instances of Honorbuddy");
                var psi = new ProcessStartInfo("taskKill", "/IM " + originalFileName)
                {
                    WindowStyle = ProcessWindowStyle.Hidden
                };

                Process.Start(psi);
                // download the new honorbuddy zip
                Log.Write("Downloading new version of Honorbuddy");
                _hbManager.Profile.Status = "Downloading new version of HB";
                string tempFileName = Path.GetTempFileName();

                client.DownloadFile(_hbManager.Profile.Settings.HonorbuddySettings.UseHBBeta ? HbBetaUpdateUrl : HbUpdateUrl, tempFileName);
                //Log.Write("Deleting old .exe and .dll files");

                //var assembliesToDelete = new List<string>(_dllNames);
                //assembliesToDelete.Add(originalFileName);
                //foreach (var fileName in assembliesToDelete)
                //{
                //    var fullPath = Path.Combine(Settings.HonorbuddyPath, fileName);
                //    if (File.Exists(fullPath))
                //    {
                //        try
                //        {
                //            File.Delete(fullPath);
                //        }
                //        catch { }
                //    }
                //}

                // extract the downloaded zip
                var hbFolder = Path.GetDirectoryName(_hbManager.Settings.HonorbuddyPath);
                Log.Write("Extracting Honorbuddy to {0}", hbFolder);
                _hbManager.Profile.Status = "Extracting Honorbuddy";
                var zip = new FastZip();
                zip.ExtractZip(tempFileName, hbFolder, FastZip.Overwrite.Always, s => true, ".*", ".*", false);

                // delete the downloaded zip
                Log.Write("Deleting temporary file");
                File.Delete(tempFileName);

                // rename the Honorbuddy.exe if original .exe was different
                if (originalFileName != "Honorbuddy.exe")
                {
                    File.Delete(_hbManager.Settings.HonorbuddyPath);
                    Log.Write("Renaming Honorbuddy.exe to {0}", originalFileName);
                    File.Move(Path.Combine(hbFolder, "Honorbuddy.exe"), _hbManager.Settings.HonorbuddyPath);
                }
            }
            else
            {
                Log.Write("Honorbuddy is up-to-date");
            }
            _lastUpdateCheck = DateTime.Now;
        }
示例#12
0
        internal static void ImportSessionFromZip(Form1 form)
        {
            OpenFileDialog       openFileDialog       = form.openFileDialog;
            ToolStripStatusLabel toolStripStatusLabel = form.toolStripStatusLabel;
            ToolStripProgressBar toolStripProgressBar = form.toolStripProgressBar;

            openFileDialog.InitialDirectory = ConstantUtil.ApplicationExecutionPath();
            openFileDialog.Multiselect      = false;
            openFileDialog.Filter           = String.Format("{0} (*.zip)|*.zip", LanguageUtil.GetCurrentLanguageString("ExportSessionDescription", className)); //DtPad session archive (*.zip)|*.zip
            openFileDialog.FileName         = "*.zip";

            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            String  dpsFileName  = String.Empty;
            ZipFile toImportFile = null;

            try
            {
                toImportFile = new ZipFile(openFileDialog.FileName);

                foreach (ZipEntry zipEntry in toImportFile)
                {
                    if (!zipEntry.Name.EndsWith(".dps"))
                    {
                        continue;
                    }

                    dpsFileName = zipEntry.Name;
                    break;
                }

                if (String.IsNullOrEmpty(dpsFileName))
                {
                    WindowManager.ShowAlertBox(form, LanguageUtil.GetCurrentLanguageString("ArchiveNotExport", className));
                    return;
                }
            }
            finally
            {
                if (toImportFile != null)
                {
                    toImportFile.Close();
                }
            }

            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog
            {
                Description  = LanguageUtil.GetCurrentLanguageString("folderBrowserDialogDescription", className),
                RootFolder   = Environment.SpecialFolder.MyDocuments,
                SelectedPath = FileUtil.GetInitialFolder(form)
            };

            if (folderBrowserDialog.ShowDialog(form) != DialogResult.OK)
            {
                return;
            }

            toolStripProgressBar.Value   = 0;
            toolStripProgressBar.Visible = true;

            FastZipEvents events = new FastZipEvents {
                ProcessFile = ProcessFileMethod
            };
            FastZip importFile = new FastZip(events);

            toolStripProgressBar.PerformStep();

            importFile.ExtractZip(openFileDialog.FileName, folderBrowserDialog.SelectedPath, FastZip.Overwrite.Prompt, OverwritePrompt, String.Empty, String.Empty, true);
            yesAllImportSession = false;

            if (stopImportSession)
            {
                toolStripProgressBar.PerformStep();
                toolStripProgressBar.PerformStep();

                toolStripProgressBar.Visible = false;
                return;
            }

            toolStripProgressBar.PerformStep();

            String importStatus = String.Format(LanguageUtil.GetCurrentLanguageString("ImportStatusLabel", className), openFileDialog.FileName);

            toolStripProgressBar.PerformStep();
            toolStripProgressBar.PerformStep();

            toolStripStatusLabel.Text = importStatus;
            WindowManager.ShowInfoBox(form, importStatus + "!");

            toolStripProgressBar.Visible = false;

            OpenSession(form, Path.Combine(folderBrowserDialog.SelectedPath, dpsFileName));
        }
示例#13
0
        private static bool handleNewFiles()
        {
            //todo: this method is going to become horribly inefficient with a lot of osz2 files being stored in this directory.

            //BeatmapManager.SongWatcher.EnableRaisingEvents = false;

            bool foundNew = false;

            string songsDir = BeatmapManager.SongsDirectory;

            //todo: could cache the number of files present now
            foreach (string f in Directory.GetFiles(BeatmapManager.SongsDirectory))
            {
                FileType ext = GeneralHelper.GetFileType(f);

                switch (ext)
                {
                case FileType.Database:
                    //Handle thumbs.db files
                    GeneralHelper.FileDelete(f);
                    break;

                case FileType.AudioTrack:
                case FileType.Beatmap:
                {
                    string filename = Path.GetFileName(f);

                    if (status != null)
                    {
                        status.SetStatus(string.Format(LocalisationManager.GetString(OsuString.BeatmapImport_Importing), (filename.Length > 50 ? filename.Remove(50) + @"..." : filename)));
                    }

                    BeatmapManager.SongWatcher.EnableRaisingEvents = false;

                    try
                    {
                        //name of .osu or .mp3 is used to identify containing folder
                        string newFolder = Path.Combine(songsDir, Path.GetFileNameWithoutExtension(f));
                        if (!Directory.Exists(newFolder))
                        {
                            Directory.CreateDirectory(newFolder);
                            BeatmapManager.ChangedFolders.Add(newFolder);
                            BeatmapManager.FolderFileCount++;
                        }

                        newFolder = Path.Combine(newFolder, filename);
                        if (!GeneralHelper.FileMove(f, newFolder, true))
                        {
                            try
                            {
                                File.Copy(f, newFolder);
                            }
                            catch
                            {
                                NotificationManager.ShowMessage(string.Format(LocalisationManager.GetString(OsuString.BeatmapImport_FileInUse), filename));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        handleCorruptFile(e, f);
                    }

                    BeatmapManager.SongWatcher.EnableRaisingEvents = true;
                    foundNew = true;
                }
                break;

                case FileType.Zip:
                case FileType.BeatmapPackage:
                {
                    //todo: instant osz2 conversion maybe?

                    string filename      = Path.GetFileName(f);
                    string filenameNoExt = Path.GetFileNameWithoutExtension(f).Replace(@"-novid", string.Empty);

                    //Remove invalid characters
                    filenameNoExt = GeneralHelper.WindowsPathStrip(filenameNoExt);

                    if (status != null)
                    {
                        status.SetStatus(string.Format(LocalisationManager.GetString(OsuString.BeatmapImport_Importing), (filename.Length > 50 ? filename.Remove(50) + @"..." : filename)));
                    }

                    short difficultyCharSpace = 36;         //Most difficulty names with brackets + extension will fall within this value
                    int   allowedPathLength   = GeneralHelper.MAX_PATH_LENGTH - difficultyCharSpace;

                    string tempFolder = GeneralHelper.GetTempPath(@"_beatmaps");

                    GeneralHelper.FileDelete(tempFolder);

                    List <string> failedFiles       = new List <string>();
                    List <string> failedDirectories = new List <string>();

                    try
                    {
                        FastZipEvents zipEvents = new FastZipEvents();

                        zipEvents.DirectoryFailure += (events, args) =>
                        {
                            if (args == null)
                            {
                                return;
                            }

                            failedDirectories.Add(Path.GetDirectoryName(args.Name));
                        };

                        zipEvents.FileFailure += (events, args) =>
                        {
                            if (args == null)
                            {
                                return;
                            }

                            failedFiles.Add(Path.GetFileName(args.Name));
                        };

                        FastZip fz = new FastZip(zipEvents);

                        fz.ExtractZip(f, tempFolder, @".*");

                        int failedCount = failedFiles.Count + failedDirectories.Count;

                        if (failedCount > 0)
                        {
                            handleExtractFailure(failedFiles, failedDirectories);
                            bool validBeatmap = checkRequiredFiles(tempFolder);

                            if (!validBeatmap)
                            {
                                throw new Exception("Beatmap missing required files");
                            }
                        }

                        int songsDirLength   = Path.GetFullPath(songsDir).Length;
                        int highestDirLength = filenameNoExt.Length;
                        if (songsDirLength + (filenameNoExt.Length * 2) >= allowedPathLength)
                        {
                            highestDirLength = (GeneralHelper.GetMaxPathLength(tempFolder) - tempFolder.Length);
                        }

                        int charsOver = songsDirLength + filenameNoExt.Length + highestDirLength - GeneralHelper.MAX_PATH_LENGTH;

                        if (charsOver > 0)
                        {
                            if (charsOver < filenameNoExt.Length - 1)
                            {
                                filenameNoExt = filenameNoExt.Remove(filenameNoExt.Length - charsOver);
                            }
                            else
                            {
                                throw new PathTooLongException();
                            }
                        }

                        //Force removal of readonly flag.
                        FileInfo myFile = new FileInfo(f);
                        if ((myFile.Attributes & FileAttributes.ReadOnly) > 0)
                        {
                            myFile.Attributes &= ~FileAttributes.ReadOnly;
                        }

                        string dir = Path.Combine(songsDir, filenameNoExt);

                        GeneralHelper.RecursiveMove(tempFolder, dir);
                        BeatmapManager.FolderFileCount++;
                        if (!BeatmapManager.ChangedFolders.Contains(dir))
                        {
                            BeatmapManager.ChangedFolders.Add(dir);
                        }

                        GeneralHelper.FileDelete(f);
                    }
                    catch (Exception e)
                    {
                        if (!(e is PathTooLongException))
                        {
                            int failedCount = failedFiles.Count + failedDirectories.Count;

                            if (failedCount > 0)
                            {
                                handleExtractFailure(failedFiles, failedDirectories);
                            }
                        }

                        handleCorruptFile(e, f);

                        try
                        {
                            Directory.Delete(tempFolder, true);
                        }
                        catch { }
                    }

                    foundNew = true;
                    break;
                }
                }
            }

            return(foundNew);
        }
示例#14
0
        public void LimitExtractPath()
        {
            string tempPath = GetTempFilePath();

            Assert.IsNotNull(tempPath, "No permission to execute this test?");

            var uniqueName = "SharpZipLib.Test_" + DateTime.Now.Ticks.ToString("x");

            tempPath = Path.Combine(tempPath, uniqueName);
            var extractPath = Path.Combine(tempPath, "output");

            const string contentFile = "content.txt";

            var contentFilePathBad = Path.Combine("..", contentFile);
            var extractFilePathBad = Path.Combine(tempPath, contentFile);
            var archiveFileBad     = Path.Combine(tempPath, "test-good.zip");

            var contentFilePathGood = Path.Combine("childDir", contentFile);
            var extractFilePathGood = Path.Combine(extractPath, contentFilePathGood);
            var archiveFileGood     = Path.Combine(tempPath, "test-bad.zip");

            try
            {
                Directory.CreateDirectory(extractPath);

                // Create test input
                void CreateTestFile(string archiveFile, string contentPath)
                {
                    using (var zf = ZipFile.Create(archiveFile))
                    {
                        zf.BeginUpdate();
                        zf.Add(new StringMemoryDataSource($"Content of {archiveFile}"), contentPath);
                        zf.CommitUpdate();
                    }
                }

                CreateTestFile(archiveFileGood, contentFilePathGood);
                CreateTestFile(archiveFileBad, contentFilePathBad);

                Assert.IsTrue(File.Exists(archiveFileGood), "Good test archive was not created");
                Assert.IsTrue(File.Exists(archiveFileBad), "Bad test archive was not created");

                var fastZip = new FastZip();

                Assert.DoesNotThrow(() =>
                {
                    fastZip.ExtractZip(archiveFileGood, extractPath, "");
                }, "Threw exception on good file name");

                Assert.IsTrue(File.Exists(extractFilePathGood), "Good output file not created");

                Assert.Throws <SharpZipLib.Core.InvalidNameException>(() =>
                {
                    fastZip.ExtractZip(archiveFileBad, extractPath, "");
                }, "No exception was thrown for bad file name");

                Assert.IsFalse(File.Exists(extractFilePathBad), "Bad output file created");

                Assert.DoesNotThrow(() =>
                {
                    fastZip.ExtractZip(archiveFileBad, extractPath, FastZip.Overwrite.Never, null, "", "", true, true);
                }, "Threw exception on bad file name when traversal explicitly allowed");

                Assert.IsTrue(File.Exists(extractFilePathBad), "Bad output file not created when traversal explicitly allowed");
            }
            finally
            {
                Directory.Delete(tempPath, true);
            }
        }
示例#15
0
        public void Encryption(ZipEncryptionMethod encryptionMethod)
        {
            const string tempName1 = "a.dat";

            var target = new MemoryStream();

            string tempFilePath = GetTempFilePath();

            Assert.IsNotNull(tempFilePath, "No permission to execute this test?");

            string addFile = Path.Combine(tempFilePath, tempName1);

            MakeTempFile(addFile, 1);

            try
            {
                var fastZip = new FastZip
                {
                    Password = "******",
                    EntryEncryptionMethod = encryptionMethod
                };

                fastZip.CreateZip(target, tempFilePath, false, @"a\.dat", null);

                var archive = new MemoryStream(target.ToArray());
                using (ZipFile zf = new ZipFile(archive))
                {
                    zf.Password = "******";
                    Assert.AreEqual(1, zf.Count);
                    ZipEntry entry = zf[0];
                    Assert.AreEqual(tempName1, entry.Name);
                    Assert.AreEqual(1, entry.Size);
                    Assert.IsTrue(zf.TestArchive(true, TestStrategy.FindFirstError, (status, message) =>
                    {
                        if (!string.IsNullOrEmpty(message))
                        {
                            Console.WriteLine($"{message} ({status.Entry?.Name ?? "-"})");
                        }
                    }));
                    Assert.IsTrue(entry.IsCrypted);

                    switch (encryptionMethod)
                    {
                    case ZipEncryptionMethod.ZipCrypto:
                        Assert.That(entry.AESKeySize, Is.Zero, "AES key size should be 0 for ZipCrypto encrypted entries");
                        break;

                    case ZipEncryptionMethod.AES128:
                        Assert.That(entry.AESKeySize, Is.EqualTo(128), "AES key size should be 128 for AES128 encrypted entries");
                        break;

                    case ZipEncryptionMethod.AES256:
                        Assert.That(entry.AESKeySize, Is.EqualTo(256), "AES key size should be 256 for AES256 encrypted entries");
                        break;
                    }
                }
            }
            finally
            {
                File.Delete(tempName1);
            }
        }