private static void CreateBackupCallback(Object stateInfo) { AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; autoEvent.Reset(); Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; var metaOpsToStore = App.Current.Model.MetaOpManager.ChangedMetaOperators.ToList(); var savedOperatorCount = metaOpsToStore.Count(); //FIXME: can we check if home-op has been modified? if (savedOperatorCount == 0) { //Logger.Info("Skipped backup, nothing changed", savedOperatorCount); autoEvent.Set(); return; } var index = GetIndexOfLastBackup(); index++; ReduceNumberOfBackups(); var zipFilePath = BACKUP_DIRECTORY + @"\" + String.Format("#{0:D5}", index) + "-" + DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss_fff") + ".zip"; var tempPath = @"Temp\" + Guid.NewGuid() + @"\"; try { var tempConfigPath = tempPath + CONFIG_DIRECTORY; var tempOperatorsPath = tempPath + OPERATORS_DIRECTORY + @"\"; var zipPath = Path.GetDirectoryName(zipFilePath); if (!Directory.Exists(zipPath)) { Directory.CreateDirectory(zipPath); } if (!Directory.Exists(tempConfigPath)) { Directory.CreateDirectory(tempConfigPath); } if (!Directory.Exists(tempOperatorsPath)) { Directory.CreateDirectory(tempOperatorsPath); } Utilities.CopyDirectory(CONFIG_DIRECTORY, tempConfigPath, "*"); App.Current.Model.StoreHomeOperator(tempConfigPath, clearChangedFlags: false); App.Current.ProjectSettings.SaveAs(tempConfigPath + "ProjectSettings.json"); App.Current.UserSettings.SaveAs(tempConfigPath + "UserSettings.json"); App.Current.OperatorPresetManager.SavePresetsAs(tempConfigPath + "Presets.json"); Utilities.CopyDirectory(OPERATORS_DIRECTORY, tempOperatorsPath, "*.mop"); MetaManager.WriteOperators(metaOpsToStore, tempOperatorsPath, clearChangedFlags: false); var filesToBackup = new Dictionary <String, String[]>(); filesToBackup[CONFIG_DIRECTORY] = Directory.GetFiles(tempConfigPath, "*"); filesToBackup[OPERATORS_DIRECTORY] = Directory.GetFiles(tempOperatorsPath, "*"); using (ZipArchive archive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create)) { foreach (var fileEntry in filesToBackup) { foreach (var file in fileEntry.Value) { archive.CreateEntryFromFile(file, fileEntry.Key + @"\" + Path.GetFileName(file), CompressionLevel.Fastest); } } } var names = metaOpsToStore.Select(metaOperator => metaOperator.Name).ToArray(); Logger.Info("Backing up {0} changed ops: {1}", savedOperatorCount, String.Join(", ", names)); } catch (Exception ex) { DeleteFile(zipFilePath); Logger.Error("autobackup failed: {0}", ex.Message); } finally { DeletePath(tempPath); } autoEvent.Set(); }
public static void Export(Operator operatorToExport, String baseExportPath) { var firstOutputOfOperatorToExport = operatorToExport.Outputs.FirstOrDefault(); if (firstOutputOfOperatorToExport == null) { Logger.Warn("This Operators doens't have any outputs. Nothing to export."); return; } Logger.Info("Exporting started..."); try { // create output dir if (Directory.Exists(baseExportPath)) { Directory.Delete(baseExportPath, true); } Directory.CreateDirectory(baseExportPath); // traverse and collect var collectedMetaOperators = new HashSet <MetaOperator>(); firstOutputOfOperatorToExport.CollectAllMetaOperators(collectedMetaOperators); Logger.Info(" Exporting {0} operators...", collectedMetaOperators.Count); var operatorExportPath = baseExportPath + "Operators" + @"\"; Directory.CreateDirectory(operatorExportPath); MetaManager.WriteOperators(collectedMetaOperators, operatorExportPath, clearChangedFlags: false); var configExportPath = baseExportPath + "config" + @"\"; Directory.CreateDirectory(configExportPath); // make the selected op the home op (move from operators dir to config dir and rename it to Home.mop) File.Move(operatorExportPath + operatorToExport.Definition.ID + ".mop", configExportPath + "Home.mop"); App.Current.ProjectSettings.SaveAs(configExportPath + "ProjectSettings.json"); var filePathsToCopy = new List <String>(); var collectedOperators = new HashSet <Operator>(); firstOutputOfOperatorToExport.CollectAllOperators(collectedOperators); foreach (var foundOp in collectedOperators) { foreach (var input in foundOp.Inputs) { if (input.Name.EndsWith("Path") && input.Connections.Count == 0) { var context = new OperatorPartContext(); string path = input.Eval(context).Text; if (!File.Exists(path)) { Logger.Warn(" {0} links to the non-existing file {1}.", foundOp.Definition.Name, path); } else if (Path.IsPathRooted(path)) { Logger.Warn(" {0} loads {1} with an absolute filepath which can't be copied to the export directory.", foundOp.Definition.Name, path); } else { filePathsToCopy.Add(path); } } } } var soundFilePath = (string)App.Current.ProjectSettings["Soundtrack.Path"]; if (Path.IsPathRooted(soundFilePath)) { Logger.Warn(" The sound file {0} is an absolute filepath which can't be copied to the export directory.", soundFilePath); } else { filePathsToCopy.Add(soundFilePath); } filePathsToCopy.Add("assets-common/image/white.png"); filePathsToCopy.AddRange(Directory.GetFiles("assets-common/fx/", "*")); filePathsToCopy.AddRange(Directory.GetFiles("assets-common/bmfont/", "*")); var collectedDlls = new HashSet <String>(); foreach (var metaOp in collectedMetaOperators) { if (metaOp.IsBasic) { var operatorPartDefinition = metaOp.OperatorParts.First().Item2; foreach (var asmFile in operatorPartDefinition.AdditionalAssemblies) { filePathsToCopy.Add(asmFile); var info = new FileInfo(asmFile); Assembly asm = Assembly.LoadFile(info.FullName); CollectAllReferencedDlls(asm, collectedDlls); } } } filePathsToCopy.Add("Player.exe"); filePathsToCopy.Add("Player.exe.config"); /* Tom: I temporarily disabled gathering the dependencies because it triggered * a crash unless Framefield.fbx.dll could not be found in tooll's root directory. * Since the complete lib-directory is already copied above, it's not necessary, * however, we should clean this up eventually. */ /* * var playerFileInfo = new FileInfo("Player.exe"); * Assembly playerAssembly = Assembly.LoadFile(playerFileInfo.FullName); * CollectAllReferencedDlls(playerAssembly, collectedDlls); * foreach (var collectedDll in collectedDlls) * { * if (collectedDll.StartsWith(playerFileInfo.DirectoryName)) * { * var relativePath = collectedDll.Replace(playerFileInfo.DirectoryName + @"\", ""); * filePathsToCopy.Add(relativePath); * } * } */ filePathsToCopy.AddRange(Directory.GetFiles("libs/x64", "sharpdx*.dll")); filePathsToCopy.AddRange(Directory.GetFiles("libs/x64", "d3d*.dll")); filePathsToCopy.Add("libs/x64/bass.dll"); filePathsToCopy.Add("libs/x64/libfbxsdk.dll"); filePathsToCopy.Add("Core.dll"); filePathsToCopy.AddRange(Directory.GetFiles("libs/", "*")); foreach (var filePath in filePathsToCopy) { string targetFilePath = baseExportPath + filePath; string targetPath = Path.GetDirectoryName(targetFilePath); if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } if (!File.Exists(targetFilePath)) { File.Copy(Directory.GetCurrentDirectory() + @"\" + filePath, targetFilePath); } } Logger.Info("Exporting finished"); } catch (Exception ex) { Logger.Error("Failed to export: {0}", ex.Message); } }