protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing)
            {
                if (_logger != null)
                    _logger.Dispose();
                _logger = null;
            }
        }
        internal static void OnUpdate()
        {
            if ((DateTime.Now - _lastSave).TotalMinutes >= SaveIntervalMinutes)
            {
                _lastSave = DateTime.Now;

                try
                {
                    lock (OTA.Callbacks.WorldFileCallback.SavePathLock)
                    {
                        using (var pg = new ProgressLogger(1, "Saving world"))
                        {
                            OTA.Callbacks.WorldFileCallback.SavePath = null; //Clear, and use the default
                            Terraria.IO.WorldFile.saveWorld();
                            pg.Value = 1;
                        }
                    }
                }
                catch (Exception e)
                {
                    ProgramLog.Log(e, "Error during the world save process");
                }
            }
        }
        private static string[] InstallUpdate(string path)
        {
            //Extract ZIP's into a seperate folder
            //Install everything else as a plugin

            var info = new FileInfo(path);

            var ext = info.Extension.ToLower();
            if (ext == ".zip")
            {
                //Extract into a temporary directory incase of zip exceptions that could potentially corrupt our installation
                var tmp = ExtractZip(info);
                if (!String.IsNullOrEmpty(tmp))
                {
                    //Read package
                    var pkgFile = Path.Combine(tmp, "package.json");
                    if (File.Exists(pkgFile))
                    {
                        var pkg = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdatePackage>(File.ReadAllText(pkgFile));
                        if (pkg != null && pkg.Instructions != null && pkg.Instructions.Length > 0)
                        {
                            //Verify first then install
                            using (var logger = new ProgressLogger(pkg.Instructions.Length, "Verifying package"))
                            {
                                foreach (var ins in pkg.Instructions)
                                {
                                    if (String.IsNullOrEmpty(ins.PackageFileName)) throw new RepositoryError("Invalid source file");
                                    if (String.IsNullOrEmpty(ins.DestinationFileName)) ins.DestinationFileName = ins.PackageFileName; //If not provided then it means it's the same

                                    var relPath = ins.PackageFileName.Split(ins.DirectorySeperator);
                                    relPath = new string[] { tmp }.Concat(relPath).ToArray();
                                    var from = Path.Combine(relPath);

                                    if (!File.Exists(from)) throw new RepositoryError("Source file {0} does not exist", ins.PackageFileName);

                                    var toPath = ins.PackageFileName.Split(ins.DirectorySeperator);
                                    toPath = new string[] { Environment.CurrentDirectory }.Concat(toPath).ToArray();
                                    var to = Path.Combine(toPath);

                                    if (IsChild(to) == null) throw new RepositoryError("Destination file {0} is not within the TDSM install directory", ins.DestinationFileName);
                                }
                            }

                            using (var logger = new ProgressLogger(pkg.Instructions.Length, "Installing package"))
                            {
                                foreach (var ins in pkg.Instructions)
                                {
                                    var relPath = ins.PackageFileName.Split(ins.DirectorySeperator);
                                    relPath = new string[] { tmp }.Concat(relPath).ToArray();
                                    var from = Path.Combine(relPath);

                                    var toPath = ins.PackageFileName.Split(ins.DirectorySeperator);
                                    toPath = new string[] { Environment.CurrentDirectory }.Concat(toPath).ToArray();
                                    var to = IsChild(Path.Combine(toPath));

                                    if (File.Exists(to)) File.Delete(to);
                                    File.Move(from, to);
                                }
                            }

                            ProgramLog.Admin.Log("Cleaning up");
                            Directory.Delete(tmp, true);
                            File.Delete(path);

                            return pkg.PluginsToLoad;
                        }
                        else
                        {
                            throw new RepositoryError("No install instructions");
                        }
                    }
                    else
                    {
                        throw new RepositoryError("package.json is missing");
                    }
                }
                else
                {
                    throw new RepositoryError("Failed to extract package");
                }
            }
            else if (ext == ".lua" || ext == ".dll")
            {
                //Move and replace targets
                var dest = Path.Combine(Globals.PluginPath, info.Name);
                if (File.Exists(dest)) File.Delete(dest);
                File.Move(info.FullName, dest);

                return new string[] { dest };
            }
            else throw new RepositoryError("No package support for {0}", ext ?? "<unknown>");
        }
 public ProgressWebClient(string message)
 {
     _logger = new ProgressLogger(1, message);
 }
        public static BackupResult SaveWorld(string path)
        {
            if (Terraria.WorldGen.saveLock) return BackupResult.SAVE_LOCK; //Please wait for the current operation to finish.

            try
            {
                using (var pg = new ProgressLogger(1, "Backing up world"))
                {
                    if (CopyBackups)
                    {
                        var copyFrom = Terraria.Main.ActiveWorldFileData.Path;
                        if (File.Exists(copyFrom))
                        {
                            File.Copy(copyFrom, path);
                        }
                    }
                    else
                    {
                        lock (OTA.Callbacks.WorldFileCallback.SavePathLock)
                        {
                            OTA.Callbacks.WorldFileCallback.SavePath = path;
                            Terraria.IO.WorldFile.saveWorld();
                            OTA.Callbacks.WorldFileCallback.SavePath = null; //Reset
                        }
                    }
                    pg.Value = 1;
                }

                if (CompressBackups)
                    Compress(path); // it just adds ".zip" to the timestamp+".wld"

                return BackupResult.SUCCESS;
            }
            catch (Exception e)
            {
                ProgramLog.Log(e, "Error during the save process.");
            }

            return BackupResult.SAVE_FAIL;
        }
        /*public static BackupResult LoadWorld(string Name)
        {
            WorldIO.LoadWorld(null, null, Statics.WorldPath + Path.DirectorySeparatorChar + Name);
            if (WorldModify.loadFailed && !WorldModify.loadSuccess)
            {
                return BackupResult.LOAD_FAIL;
            }
            return BackupResult.SUCCESS;
        }*/
        public static BackupResult Compress(string worldPath)
        {
            //            Stopwatch stopwatch = new Stopwatch();
            //            stopwatch.Start();
            //
            //            ProgramLog.Log("Compressing backup...");

            if (!File.Exists(worldPath))
            {
                ProgramLog.Error.Log("File not Found: " + worldPath);
                return BackupResult.LOAD_FAIL;
            }

            using (var pg = new ProgressLogger(1, "Compressing backup"))
            {
                FileInfo world = new FileInfo(worldPath);
                String archivePath = String.Concat(worldPath, ".zip");

                using (FileStream inStream = world.OpenRead())
                {
                    using (FileStream outStream = File.Create(archivePath))
                    {
                        using (GZipStream alg = new GZipStream(outStream, CompressionMode.Compress))
                        {
                            // copy the input file into the compression stream
                            inStream.CopyTo(alg);
                        }
                    }
                }

                if (File.Exists(archivePath))
                {
                    if (File.Exists(worldPath))
                        File.Delete(worldPath);
                    //                    stopwatch.Stop();

                    pg.Value = 1;
            //                    ProgramLog.Log("Compression duration: " + stopwatch.Elapsed.Seconds + " Second(s)");
                    return BackupResult.SUCCESS;
                }
            }
            //                else
            //                {
            //                    stopwatch.Stop();
            ProgramLog.Error.Log("Compression Failed!");
            return BackupResult.SAVE_FAIL;
            //                }
        }
        public static void AutoPurge(string worldName)
        {
            if (!ExpirationsEnabled)
                return;

            //            ProgramLog.Log("Performing backup purge...");
            var backups = GetBackups(worldName);

            if (backups != null && backups.Length > 0)
                using (var pg = new ProgressLogger(backups.Length, "Purging old backups"))
                {

                    var expired = (from x in backups
                                                  where (DateTime.Now - File.GetCreationTime(x)).TotalMinutes >= BackupExpiryMinutes
                                                  select x).ToArray();
                    //var deleted = 0;
                    foreach (var file in expired)
                    {
                        try
                        {
                            File.Delete(file);
                            //deleted++;
                        }
                        catch
                        {
                        }
                    }
                }
            else ProgramLog.Log("No backups to be purged.");
        }
Пример #8
0
 public static void AddProgressLogger(ProgressLogger prog)
 {
     Write(new LogEntry(prog, null));
 }