/// <summary> /// Loads all custom textures from disk into texture objects. This is done on a separate thread so it is not done redundantly multiple times on each atlas thread /// </summary> /// <param name="CustomFolderPaths">The list of absolute paths containing custom contour icon images to be loaded</param> /// <param name="token">The cancellation token</param> /// <returns>The list of textures</returns> public static Task LoadCustomContourIconsAsync(List <string> CustomFolderPaths, CancellationToken token) { ParseCustomTexturesTask = Task.Run(() => { Logging.Info(LogOptions.MethodName, "Custom contour icon images task starting"); ParseStopwatch.Restart(); //parse each folder list to create a list of all custom contour icons Logging.Debug(LogOptions.MethodName, "Custom contour icon images folder count: {0}", CustomFolderPaths.Count); List <string> customContourIconFilesList = new List <string>(); foreach (string folder in CustomFolderPaths) { string realFolder = MacroUtils.MacroReplace(folder, ReplacementTypes.FilePath); Logging.Info(LogOptions.MethodName, "Checking for custom contour icon images in directory {0}", realFolder); token.ThrowIfCancellationRequested(); if (!Directory.Exists(realFolder)) { Logging.Warning(LogOptions.MethodName, "Directory {0} does not exist, skipping", realFolder); continue; } customContourIconFilesList.AddRange(FileUtils.DirectorySearch(realFolder, SearchOption.TopDirectoryOnly, false, "*", 5, 3, false)); } //filter the list to just image files //{ "*.jpg", "*.png", "*.bmp" } Logging.Debug(LogOptions.MethodName, "List created, filtering for only png,jpg,bmp image files", CustomFolderPaths.Count); customContourIconFilesList = customContourIconFilesList.Where(filepath => { if (Path.GetExtension(filepath).ToLower().Contains("png")) { return(true); } else if (Path.GetExtension(filepath).ToLower().Contains("jpg")) { return(true); } else if (Path.GetExtension(filepath).ToLower().Contains("bmp")) { return(true); } else { return(false); } }).ToList(); customContourIconFilesList = customContourIconFilesList.Distinct().ToList(); if (customContourIconFilesList.Count == 0) { Logging.Warning(LogOptions.MethodName, "Total of 0 custom contour icons found to parse (Is this the intent?)"); return; } //just in case, dispose of the old one DisposeParsedCustomTextures(); CustomContourIconImages = new List <Texture>(); Logging.Debug(LogOptions.MethodName, "Loading custom images into data lists for atlas creator", CustomFolderPaths.Count); foreach (string customContourIconFilePath in customContourIconFilesList) { token.ThrowIfCancellationRequested(); //load the bitmap as well Bitmap customContourIconImage = new Bitmap(customContourIconFilePath); //don't care about the x an y for the custom textures CustomContourIconImages.Add(new Texture() { Name = Path.GetFileNameWithoutExtension(customContourIconFilePath), Height = customContourIconImage.Height, Width = customContourIconImage.Width, X = 0, Y = 0, AtlasImage = customContourIconImage }); customContourIconImage = null; } Logging.Info(LogOptions.MethodName, "Custom images parsing task completed in {0} msec", ParseStopwatch.ElapsedMilliseconds); ParseStopwatch.Stop(); }); return(ParseCustomTexturesTask); }
private void TestPatchButton_Click(object sender, RoutedEventArgs e) { //test from selection components if (PatchSettings.SwitchToLogWhenTestingPatch) { RightSideTabControl.SelectedItem = LogOutputTab; } Logging.Patcher("TestPatch() start", LogLevel.Info); Logging.Patcher("Checking UI elements for valid patch information...", LogLevel.Info); //make new patch element Patch patchToTest = new Patch(); //check input from UI left panel side: //file location Logging.Patcher("File to Patch location mode: {0}", LogLevel.Info, FilePathTypeCombobox.SelectedItem ?? "(null)"); if (FilePathTypeCombobox.SelectedItem == null) { Logging.Patcher("Invalid file path type", LogLevel.Info); return; } switch (FilePathTypeCombobox.SelectedItem.ToString()) { case "Absolute": Logging.Patcher("Checking if absolute file path {0} exists...", LogLevel.Info, FileToPatchTextbox.Text); if (File.Exists(FileToPatchTextbox.Text)) { Logging.Patcher("File Exists!", LogLevel.Info); patchToTest.File = FileToPatchTextbox.Text; patchToTest.CompletePath = FileToPatchTextbox.Text; } else { Logging.Patcher("File does not exist, aborting", LogLevel.Info); return; } break; case "Relative": Logging.Patcher("Using relative macro {0}", LogLevel.Info, PatchPathCombobox.SelectedItem ?? "(null)"); string completePathForPatchFile; switch (PatchPathCombobox.SelectedItem.ToString()) { case "app": if (Directory.Exists(PatchSettings.AppMacro)) { Logging.Patcher("app macro folder path exists...", LogLevel.Info); if (FileToPatchTextbox.Text.Contains("versiondir") && string.IsNullOrWhiteSpace(PatchSettings.VersiondirMacro)) { Logging.Patcher("versiondir macro found, but versiondir patch setting macro is blank, aborting", LogLevel.Info); return; } completePathForPatchFile = PatchSettings.AppMacro + FileToPatchTextbox.Text; completePathForPatchFile = MacroUtils.MacroReplace(completePathForPatchFile, ReplacementTypes.ZipFilePath); } else { Logging.Patcher("app macro folder path does not exist, aborting", LogLevel.Info); return; } break; case "appData": if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))) { Logging.Patcher("appData macro folder path exists...", LogLevel.Info); completePathForPatchFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + FileToPatchTextbox.Text; } else { Logging.Patcher("appData macro folder path does not exist, aborting", LogLevel.Info); return; } break; default: Logging.Patcher("Invalid path macro", LogLevel.Info); return; } Logging.Patcher("relative path built as {0}", LogLevel.Info, completePathForPatchFile); if (File.Exists(completePathForPatchFile)) { Logging.Patcher("File exists!", LogLevel.Info); patchToTest.File = FileToPatchTextbox.Text; patchToTest.CompletePath = completePathForPatchFile; } else { Logging.Patcher("File does not exist, aborting (did you forget to add \"\\\" to the beginning of the path?", LogLevel.Info); } break; default: Logging.Patcher("Invalid file path type, aborting", LogLevel.Info); return; } //check patch type if (PatchTypeCombobox.SelectedItem == null) { Logging.Patcher("Invalid Patch Type, aborting", LogLevel.Info); return; } patchToTest.Type = PatchTypeCombobox.SelectedItem as string; //check patch mode switch (patchToTest.Type.ToLower()) { case "regex": case "regx": if (!string.IsNullOrWhiteSpace(PatchModeCombobox.SelectedItem as string)) { Logging.Patcher("Type=regex, invalid patch type: {0}", LogLevel.Error, PatchModeCombobox.SelectedItem as string); Logging.Patcher("valid types are: (null)"); return; } //set the lines if (string.IsNullOrWhiteSpace(PatchLinesPathTextbox.Text)) { Logging.Patcher("Type=regex, Lines to patch is blank", LogLevel.Error); return; } else { patchToTest.Line = PatchLinesPathTextbox.Text; } break; case "xml": //check if path/lines is valid (has string values) if (string.IsNullOrWhiteSpace(PatchLinesPathTextbox.Text)) { Logging.Patcher("invalid xpath", LogLevel.Error); return; } if (!validXmlModes.Contains((PatchModeCombobox.SelectedItem as string).ToLower())) { Logging.Patcher("Type=xml, invalid patch type: {0}", LogLevel.Error, PatchModeCombobox.SelectedItem as string); Logging.Patcher("valid types are: {0}", LogLevel.Error, string.Join(",", validXmlModes)); return; } patchToTest.Path = PatchLinesPathTextbox.Text; break; case "json": //check if path/lines is valid (has string values) if (string.IsNullOrWhiteSpace(PatchLinesPathTextbox.Text)) { Logging.Patcher("invalid jsonpath"); return; } if (!validJsonModes.Contains((PatchModeCombobox.SelectedItem as string).ToLower())) { Logging.Patcher("Type=json, invalid patch type: {0}", LogLevel.Info, PatchModeCombobox.SelectedItem as string); Logging.Patcher("valid types are: {0}", LogLevel.Info, string.Join(",", validJsonModes)); return; } patchToTest.Path = PatchLinesPathTextbox.Text; break; default: throw new BadMemeException("invalid patch type, but you should probably make this a enum not strings"); } patchToTest.Mode = PatchModeCombobox.SelectedItem as string; //check followPath true ONLY for json if (!patchToTest.Type.Equals("json") && (bool)PatchFollowPathSetting.IsChecked) { Logging.Patcher("Types=json, followPathSetting must be false!"); return; } //check search and replace if (string.IsNullOrWhiteSpace(PatchReplaceTextbox.Text) && string.IsNullOrWhiteSpace(PatchSearchTextbox.Text)) { Logging.Patcher("patch replace and search are blank, invalid patch"); return; } if (string.IsNullOrWhiteSpace(PatchSearchTextbox.Text)) { Logging.Patcher("patch search is blank (is this the intent?)", LogLevel.Warning); } patchToTest.Search = PatchSearchTextbox.Text; if (string.IsNullOrWhiteSpace(PatchReplaceTextbox.Text)) { Logging.Patcher("patch replace is blank (is this the intent?)", LogLevel.Info); } patchToTest.Replace = PatchReplaceTextbox.Text; //put patch into patch test methods Logging.Patcher("Running patch...", LogLevel.Info); switch (Patcher.RunPatchFromEditor(patchToTest)) { case PatchExitCode.Error: Logging.Patcher("Patch failed with errors. Check the log for details.", LogLevel.Error); break; case PatchExitCode.Warning: Logging.Patcher("Patch completed with warnings. Check the log for details.", LogLevel.Warning); break; case PatchExitCode.Success: Logging.Patcher("Patch completed successfully!", LogLevel.Info); break; } }
public static XmlDocument SavePatchToXmlDocument(List <Patch> PatchesList) { XmlDocument doc = new XmlDocument(); XmlElement patchHolder = doc.CreateElement("patchs"); doc.AppendChild(patchHolder); int counter = 0; foreach (Patch patch in PatchesList) { Logging.Patcher("[SavePatchToXmlDocument]: Saving patch {0} of {1}: {2}", LogLevel.Info, ++counter, PatchesList.Count, patch.ToString()); Logging.Patcher("{0}", LogLevel.Info, patch.DumpPatchInfoForLog); XmlElement xmlPatch = doc.CreateElement("patch"); patchHolder.AppendChild(xmlPatch); XmlElement version = doc.CreateElement("version"); version.InnerText = patch.Version.ToString(); xmlPatch.AppendChild(version); XmlElement type = doc.CreateElement("type"); type.InnerText = patch.Type; xmlPatch.AppendChild(type); XmlElement mode = doc.CreateElement("mode"); mode.InnerText = patch.Mode; xmlPatch.AppendChild(mode); XmlElement patchPath = doc.CreateElement("patchPath"); patchPath.InnerText = patch.PatchPath; xmlPatch.AppendChild(patchPath); XmlElement followPath = doc.CreateElement("followPath"); followPath.InnerText = patch.Version == 1 ? false.ToString() : patch.FollowPath.ToString(); xmlPatch.AppendChild(followPath); XmlElement file = doc.CreateElement("file"); file.InnerText = patch.File; xmlPatch.AppendChild(file); if (patch.Type.Equals("regex")) { XmlElement line = doc.CreateElement("line"); line.InnerText = string.Join(",", patch.Lines); xmlPatch.AppendChild(line); } else { XmlElement line = doc.CreateElement("path"); line.InnerText = patch.Path; xmlPatch.AppendChild(line); } XmlElement search = doc.CreateElement("search"); search.InnerText = patch.Search; xmlPatch.AppendChild(search); XmlElement replace = doc.CreateElement("replace"); replace.InnerText = MacroUtils.MacroReplace(patch.Replace, ReplacementTypes.TextEscape); xmlPatch.AppendChild(replace); } return(doc); }
/// <summary> /// Creates a shortcut on the user's desktop /// </summary> /// <param name="shortcut">The shortcut parameters</param> /// <param name="sb">The StringBuilder to log the path to the created file</param> public static void CreateShortcut(Shortcut shortcut, StringBuilder sb) { Logging.Info(shortcut.ToString()); Logging.Info("Creating shortcut {0}", shortcut.Name); //build the full macro for path (target) and name (also filename) string target = MacroUtils.MacroReplace(shortcut.Path, ReplacementTypes.FilePath).Replace(@"/", @"\"); string filename = string.Format("{0}.lnk", shortcut.Name); string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); shortcutPath = Path.Combine(shortcutPath, filename); Logging.Info("Target={0}", target); Logging.Info("ShortcutPath={0}", shortcutPath); if (!File.Exists(target)) { Logging.Warning("Target does not exist, skipping shortcut", target); return; } //target exists, but do we need to update the shortcut? if (File.Exists(shortcutPath)) { Logging.Debug("Shortcut path exists, checking if update needed"); //get the target path IShellLink link = (IShellLink) new ShellLink(); (link as IPersistFile).Load(shortcutPath, (int)StgmConstants.STGM_READ); StringBuilder oldTargetSB = new StringBuilder((int)Win32Value.MAX_PATH); link.GetPath(oldTargetSB, oldTargetSB.Capacity, null, (int)SLGP.RAWPATH); string oldTarget = oldTargetSB.ToString(); Logging.Debug("New target = {0}, old target = {1}", target, oldTarget); if (!target.Equals(oldTarget)) { //needs update Logging.Debug("Updating target"); link.SetDescription("Created by Relhax Modpack"); link.SetPath(target); link.SetWorkingDirectory(Path.GetDirectoryName(target)); //The arguments used when executing the target (none used for now) link.SetArguments(""); (link as IPersistFile).Save(shortcutPath, false); } else { //no update needed Logging.Debug("No update needed"); return; } } else { Logging.Debug("Shortcut path does not exist, creating"); IShellLink link = (IShellLink) new ShellLink(); // setup shortcut information link.SetDescription("Created by Relhax Modpack"); link.SetPath(target); link.SetIconLocation(target, 0); link.SetWorkingDirectory(Path.GetDirectoryName(target)); //The arguments used when executing the exe (none used for now) link.SetArguments(""); (link as IPersistFile).Save(shortcutPath, false); } //getting here means that the target is updated or created, so log it to the installer sb.AppendLine(shortcutPath); }