public ContentFile(string path, ContentType type, Workshop.Item workShopItem = null) { Path = path; #if OSX || LINUX Path = Path.Replace("\\", "/"); #endif Type = type; WorkShopItem = workShopItem; }
/// <summary> /// Is the item compatible with this version of Barotrauma. Returns null if compatibility couldn't be determined (item not installed) /// </summary> public static bool?CheckWorkshopItemCompatibility(Workshop.Item item) { if (!item.Installed) { return(null); } string metaDataPath = Path.Combine(item.Directory.FullName, MetadataFileName); if (!File.Exists(metaDataPath)) { throw new FileNotFoundException("Metadata file for the Workshop item \"" + item.Title + "\" not found. The file may be corrupted."); } ContentPackage contentPackage = new ContentPackage(metaDataPath); return(contentPackage.IsCompatible()); }
public static bool UpdateWorkshopItem(Workshop.Item item, out string errorMsg) { errorMsg = ""; if (!item.Installed) { return(false); } if (!DisableWorkShopItem(item, out errorMsg)) { return(false); } if (!EnableWorkShopItem(item, allowFileOverwrite: false, errorMsg: out errorMsg)) { return(false); } return(true); }
public static bool CheckWorkshopItemUpToDate(Workshop.Item item) { if (!item.Installed) { return(false); } string metaDataPath = Path.Combine(item.Directory.FullName, MetadataFileName); if (!File.Exists(metaDataPath)) { DebugConsole.ThrowError("Metadata file for the Workshop item \"" + item.Title + "\" not found. The file may be corrupted."); return(false); } ContentPackage steamPackage = new ContentPackage(metaDataPath); ContentPackage myPackage = ContentPackage.List.Find(cp => cp.Name == steamPackage.Name); if (myPackage?.InstallTime == null) { return(false); } return(item.Modified <= myPackage.InstallTime.Value); }
public static bool CheckWorkshopItemEnabled(Workshop.Item item, bool checkContentFiles = true) { if (!item.Installed) { return(false); } string metaDataPath = Path.Combine(item.Directory.FullName, MetadataFileName); if (!File.Exists(metaDataPath)) { DebugConsole.ThrowError("Metadata file for the Workshop item \"" + item.Title + "\" not found. The file may be corrupted."); return(false); } ContentPackage contentPackage = new ContentPackage(metaDataPath); //make sure the contentpackage file is present if (!File.Exists(GetWorkshopItemContentPackagePath(contentPackage)) && !ContentPackage.List.Any(cp => cp.Name == contentPackage.Name)) { return(false); } if (checkContentFiles) { foreach (ContentFile contentFile in contentPackage.Files) { if (!File.Exists(contentFile.Path)) { return(false); } } } return(true); }
/// <summary> /// Disables a workshop item by removing the files from the game folder. /// </summary> public static bool DisableWorkShopItem(Workshop.Item item, out string errorMsg) { if (!item.Installed) { errorMsg = "Cannot disable workshop item \"" + item.Title + "\" because it has not been installed."; DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } ContentPackage contentPackage = new ContentPackage(Path.Combine(item.Directory.FullName, MetadataFileName)); string installedContentPackagePath = GetWorkshopItemContentPackagePath(contentPackage); var allPackageFiles = Directory.GetFiles(item.Directory.FullName, "*", SearchOption.AllDirectories); List <string> nonContentFiles = new List <string>(); foreach (string file in allPackageFiles) { if (file == MetadataFileName) { continue; } string relativePath = UpdaterUtil.GetRelativePath(file, item.Directory.FullName); string fullPath = Path.GetFullPath(relativePath); if (contentPackage.Files.Any(f => { string fp = Path.GetFullPath(f.Path); return(fp == fullPath); })) { continue; } if (ContentPackage.IsModFilePathAllowed(relativePath)) { nonContentFiles.Add(relativePath); } } if (File.Exists(installedContentPackagePath)) { File.Delete(installedContentPackagePath); } bool wasSub = contentPackage.Files.Any(f => f.Type == ContentType.Submarine); HashSet <string> directories = new HashSet <string>(); try { foreach (ContentFile contentFile in contentPackage.Files) { if (!ContentPackage.IsModFilePathAllowed(contentFile)) { //Workshop items are not allowed to add or modify files in the Content or Data folders; continue; } if (!File.Exists(contentFile.Path)) { continue; } File.Delete(contentFile.Path); directories.Add(Path.GetDirectoryName(contentFile.Path)); } foreach (string nonContentFile in nonContentFiles) { if (!ContentPackage.IsModFilePathAllowed(nonContentFile)) { //Workshop items are not allowed to add or modify files in the Content or Data folders; continue; } if (!File.Exists(nonContentFile)) { continue; } File.Delete(nonContentFile); directories.Add(Path.GetDirectoryName(nonContentFile)); } foreach (string directory in directories) { if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory)) { continue; } if (Directory.GetFiles(directory, "*", SearchOption.AllDirectories).Count() == 0) { Directory.Delete(directory, recursive: true); } } ContentPackage.List.RemoveAll(cp => System.IO.Path.GetFullPath(cp.Path) == System.IO.Path.GetFullPath(installedContentPackagePath)); GameMain.Config.SelectedContentPackages.RemoveWhere(cp => !ContentPackage.List.Contains(cp)); GameMain.Config.SaveNewPlayerConfig(); } catch (Exception e) { errorMsg = "Disabling the workshop item \"" + item.Title + "\" failed. " + e.Message; DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } if (wasSub) { Submarine.RefreshSavedSubs(); } errorMsg = ""; return(true); }
/// <summary> /// Enables a workshop item by moving it to the game folder. /// </summary> public static bool EnableWorkShopItem(Workshop.Item item, bool allowFileOverwrite, out string errorMsg) { if (!item.Installed) { errorMsg = TextManager.GetWithVariable("WorkshopErrorInstallRequiredToEnable", "[itemname]", item.Title); DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } string metaDataFilePath = Path.Combine(item.Directory.FullName, MetadataFileName); ContentPackage contentPackage = new ContentPackage(metaDataFilePath); string newContentPackagePath = GetWorkshopItemContentPackagePath(contentPackage); if (!contentPackage.IsCompatible()) { errorMsg = TextManager.GetWithVariables(contentPackage.GameVersion <= new Version(0, 0, 0, 0) ? "IncompatibleContentPackageUnknownVersion" : "IncompatibleContentPackage", new string[3] { "[packagename]", "[packageversion]", "[gameversion]" }, new string[3] { contentPackage.Name, contentPackage.GameVersion.ToString(), GameMain.Version.ToString() }); return(false); } if (contentPackage.CorePackage && !contentPackage.ContainsRequiredCorePackageFiles(out List <ContentType> missingContentTypes)) { errorMsg = TextManager.GetWithVariables("ContentPackageMissingCoreFiles", new string[2] { "[packagename]", "[missingfiletypes]" }, new string[2] { contentPackage.Name, string.Join(", ", missingContentTypes) }, new bool[2] { false, true }); return(false); } var allPackageFiles = Directory.GetFiles(item.Directory.FullName, "*", SearchOption.AllDirectories); List <string> nonContentFiles = new List <string>(); foreach (string file in allPackageFiles) { if (file == metaDataFilePath) { continue; } string relativePath = UpdaterUtil.GetRelativePath(file, item.Directory.FullName); string fullPath = Path.GetFullPath(relativePath); if (contentPackage.Files.Any(f => { string fp = Path.GetFullPath(f.Path); return(fp == fullPath); })) { continue; } if (ContentPackage.IsModFilePathAllowed(relativePath)) { nonContentFiles.Add(relativePath); } } if (!allowFileOverwrite) { if (File.Exists(newContentPackagePath) && !CheckFileEquality(newContentPackagePath, metaDataFilePath)) { errorMsg = TextManager.GetWithVariables("WorkshopErrorOverwriteOnEnable", new string[2] { "[itemname]", "[filename]" }, new string[2] { item.Title, newContentPackagePath }); DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } foreach (ContentFile contentFile in contentPackage.Files) { string sourceFile = Path.Combine(item.Directory.FullName, contentFile.Path); if (File.Exists(sourceFile) && File.Exists(contentFile.Path) && !CheckFileEquality(sourceFile, contentFile.Path)) { errorMsg = TextManager.GetWithVariables("WorkshopErrorOverwriteOnEnable", new string[2] { "[itemname]", "[filename]" }, new string[2] { item.Title, contentFile.Path }); DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } } } try { foreach (ContentFile contentFile in contentPackage.Files) { string sourceFile = Path.Combine(item.Directory.FullName, contentFile.Path); //path not allowed -> the content file must be a reference to an external file (such as some vanilla file outside the Mods folder) if (!ContentPackage.IsModFilePathAllowed(contentFile)) { //the content package is trying to copy a file to a prohibited path, which is not allowed if (File.Exists(sourceFile)) { errorMsg = TextManager.GetWithVariable("WorkshopErrorIllegalPathOnEnable", "[filename]", contentFile.Path); return(false); } //not trying to copy anything, so this is a reference to an external file //if the external file doesn't exist, we cannot enable the package else if (!File.Exists(contentFile.Path)) { errorMsg = TextManager.GetWithVariable("WorkshopErrorEnableFailed", "[itemname]", item.Title) + " " + TextManager.GetWithVariable("WorkshopFileNotFound", "[path]", "\"" + contentFile.Path + "\""); return(false); } continue; } else if (!File.Exists(sourceFile)) { if (File.Exists(contentFile.Path)) { //the file is already present in the game folder, all good continue; } else { //file not present in either the mod or the game folder -> cannot enable the package errorMsg = TextManager.GetWithVariable("WorkshopErrorEnableFailed", "[itemname]", item.Title) + " " + TextManager.GetWithVariable("WorkshopFileNotFound", "[path]", "\"" + contentFile.Path + "\""); return(false); } } //make sure the destination directory exists Directory.CreateDirectory(Path.GetDirectoryName(contentFile.Path)); File.Copy(sourceFile, contentFile.Path, overwrite: true); } foreach (string nonContentFile in nonContentFiles) { string sourceFile = Path.Combine(item.Directory.FullName, nonContentFile); if (!File.Exists(sourceFile)) { continue; } if (!ContentPackage.IsModFilePathAllowed(nonContentFile)) { DebugConsole.ThrowError(TextManager.GetWithVariable("WorkshopErrorIllegalPathOnEnable", "[filename]", nonContentFile)); continue; } Directory.CreateDirectory(Path.GetDirectoryName(nonContentFile)); File.Copy(sourceFile, nonContentFile, overwrite: true); } } catch (Exception e) { errorMsg = TextManager.GetWithVariable("WorkshopErrorEnableFailed", "[itemname]", item.Title) + " {" + e.Message + "}"; DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } var newPackage = new ContentPackage(contentPackage.Path, newContentPackagePath) { SteamWorkshopUrl = item.Url, InstallTime = item.Modified > item.Created ? item.Modified : item.Created }; newPackage.Save(newContentPackagePath); ContentPackage.List.Add(newPackage); if (newPackage.CorePackage) { //if enabling a core package, disable all other core packages GameMain.Config.SelectedContentPackages.RemoveWhere(cp => cp.CorePackage); } GameMain.Config.SelectedContentPackages.Add(newPackage); GameMain.Config.SaveNewPlayerConfig(); if (newPackage.Files.Any(f => f.Type == ContentType.Submarine)) { Submarine.RefreshSavedSubs(); } errorMsg = ""; return(true); }
/// <summary> /// Creates a copy of the specified workshop item in the staging folder and an editor that can be used to edit and update the item /// </summary> public static void CreateWorkshopItemStaging(Workshop.Item existingItem, out Workshop.Editor itemEditor, out ContentPackage contentPackage) { if (!existingItem.Installed) { itemEditor = null; contentPackage = null; DebugConsole.ThrowError("Cannot edit the workshop item \"" + existingItem.Title + "\" because it has not been installed."); return; } var stagingFolder = new DirectoryInfo(WorkshopItemStagingFolder); if (stagingFolder.Exists) { SaveUtil.ClearFolder(stagingFolder.FullName); } else { stagingFolder.Create(); } itemEditor = instance.client.Workshop.EditItem(existingItem.Id); itemEditor.Visibility = Workshop.Editor.VisibilityType.Public; itemEditor.Title = existingItem.Title; itemEditor.Tags = existingItem.Tags.ToList(); itemEditor.Description = existingItem.Description; itemEditor.WorkshopUploadAppId = AppID; itemEditor.Folder = stagingFolder.FullName; string previewImagePath = Path.GetFullPath(Path.Combine(itemEditor.Folder, PreviewImageName)); itemEditor.PreviewImage = previewImagePath; try { if (File.Exists(previewImagePath)) { File.Delete(previewImagePath); } Uri baseAddress = new Uri(existingItem.PreviewImageUrl); Uri directory = new Uri(baseAddress, "."); // "." == current dir, like MS-DOS string fileName = Path.GetFileName(baseAddress.LocalPath); IRestClient client = new RestClient(directory); var request = new RestRequest(fileName, Method.GET); var response = client.Execute(request); if (response.ResponseStatus == ResponseStatus.Completed) { File.WriteAllBytes(previewImagePath, response.RawBytes); } } catch (Exception e) { string errorMsg = "Failed to save workshop item preview image to \"" + previewImagePath + "\" when creating workshop item staging folder."; GameAnalyticsManager.AddErrorEventOnce("SteamManager.CreateWorkshopItemStaging:WriteAllBytesFailed" + previewImagePath, GameAnalyticsSDK.Net.EGAErrorSeverity.Error, errorMsg + "\n" + e.Message); } ContentPackage tempContentPackage = new ContentPackage(Path.Combine(existingItem.Directory.FullName, MetadataFileName)); //item already installed, copy it from the game folder if (existingItem != null && CheckWorkshopItemEnabled(existingItem, checkContentFiles: false)) { string installedItemPath = GetWorkshopItemContentPackagePath(tempContentPackage); if (File.Exists(installedItemPath)) { tempContentPackage = new ContentPackage(installedItemPath); } } if (File.Exists(tempContentPackage.Path)) { string newContentPackagePath = Path.Combine(WorkshopItemStagingFolder, MetadataFileName); File.Copy(tempContentPackage.Path, newContentPackagePath, overwrite: true); contentPackage = new ContentPackage(newContentPackagePath); foreach (ContentFile contentFile in tempContentPackage.Files) { string sourceFile; if (contentFile.Type == ContentType.Submarine && File.Exists(contentFile.Path)) { sourceFile = contentFile.Path; } else { sourceFile = Path.Combine(existingItem.Directory.FullName, contentFile.Path); } if (!File.Exists(sourceFile)) { continue; } //make sure the destination directory exists string destinationPath = Path.Combine(WorkshopItemStagingFolder, contentFile.Path); Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)); File.Copy(sourceFile, destinationPath, overwrite: true); contentPackage.AddFile(contentFile.Path, contentFile.Type); } } else { contentPackage = ContentPackage.CreatePackage(existingItem.Title, Path.Combine(WorkshopItemStagingFolder, MetadataFileName), false); contentPackage.Save(contentPackage.Path); } }
/// <summary> /// Enables a workshop item by moving it to the game folder. /// </summary> public static bool EnableWorkShopItem(Workshop.Item item, bool allowFileOverwrite, out string errorMsg) { if (!item.Installed) { errorMsg = TextManager.Get("WorkshopErrorInstallRequiredToEnable").Replace("[itemname]", item.Title); DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } string metaDataFilePath = Path.Combine(item.Directory.FullName, MetadataFileName); ContentPackage contentPackage = new ContentPackage(metaDataFilePath); string newContentPackagePath = GetWorkshopItemContentPackagePath(contentPackage); var allPackageFiles = Directory.GetFiles(item.Directory.FullName, "*", SearchOption.AllDirectories); List <string> nonContentFiles = new List <string>(); foreach (string file in allPackageFiles) { if (file == metaDataFilePath) { continue; } string relativePath = UpdaterUtil.GetRelativePath(file, item.Directory.FullName); string fullPath = Path.GetFullPath(relativePath); if (contentPackage.Files.Any(f => { string fp = Path.GetFullPath(f.Path); return(fp == fullPath); })) { continue; } if (ContentPackage.IsModFilePathAllowed(relativePath)) { nonContentFiles.Add(relativePath); } } if (!allowFileOverwrite) { if (File.Exists(newContentPackagePath)) { errorMsg = TextManager.Get("WorkshopErrorOverwriteOnEnable") .Replace("[itemname]", item.Title) .Replace("[filename]", newContentPackagePath); DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } foreach (ContentFile contentFile in contentPackage.Files) { string sourceFile = Path.Combine(item.Directory.FullName, contentFile.Path); if (File.Exists(sourceFile) && File.Exists(contentFile.Path)) { errorMsg = TextManager.Get("WorkshopErrorOverwriteOnEnable") .Replace("[itemname]", item.Title) .Replace("[filename]", contentFile.Path); DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } } } try { foreach (ContentFile contentFile in contentPackage.Files) { string sourceFile = Path.Combine(item.Directory.FullName, contentFile.Path); if (!File.Exists(sourceFile)) { continue; } if (!ContentPackage.IsModFilePathAllowed(contentFile)) { DebugConsole.ThrowError(TextManager.Get("WorkshopErrorIllegalPathOnEnable").Replace("[filename]", contentFile.Path)); continue; } //make sure the destination directory exists Directory.CreateDirectory(Path.GetDirectoryName(contentFile.Path)); File.Copy(sourceFile, contentFile.Path, overwrite: true); } foreach (string nonContentFile in nonContentFiles) { string sourceFile = Path.Combine(item.Directory.FullName, nonContentFile); if (!File.Exists(sourceFile)) { continue; } if (!ContentPackage.IsModFilePathAllowed(nonContentFile)) { DebugConsole.ThrowError(TextManager.Get("WorkshopErrorIllegalPathOnEnable").Replace("[filename]", nonContentFile)); continue; } Directory.CreateDirectory(Path.GetDirectoryName(nonContentFile)); File.Copy(sourceFile, nonContentFile, overwrite: true); } } catch (Exception e) { errorMsg = TextManager.Get("WorkshopErrorEnableFailed").Replace("[itemname]", item.Title) + " " + e.Message; DebugConsole.NewMessage(errorMsg, Microsoft.Xna.Framework.Color.Red); return(false); } var newPackage = new ContentPackage(contentPackage.Path, newContentPackagePath) { SteamWorkshopUrl = item.Url, InstallTime = item.Modified > item.Created ? item.Modified : item.Created }; newPackage.Save(newContentPackagePath); ContentPackage.List.Add(newPackage); if (newPackage.CorePackage) { //if enabling a core package, disable all other core packages GameMain.Config.SelectedContentPackages.RemoveWhere(cp => cp.CorePackage); } GameMain.Config.SelectedContentPackages.Add(newPackage); GameMain.Config.SaveNewPlayerConfig(); if (newPackage.Files.Any(f => f.Type == ContentType.Submarine)) { Submarine.RefreshSavedSubs(); } errorMsg = ""; return(true); }
/// <summary> /// Creates a copy of the specified workshop item in the staging folder and an editor that can be used to edit and update the item /// </summary> public static void CreateWorkshopItemStaging(Workshop.Item existingItem, out Workshop.Editor itemEditor, out ContentPackage contentPackage) { if (!existingItem.Installed) { itemEditor = null; contentPackage = null; DebugConsole.ThrowError("Cannot edit the workshop item \"" + existingItem.Title + "\" because it has not been installed."); return; } var stagingFolder = new DirectoryInfo(WorkshopItemStagingFolder); if (stagingFolder.Exists) { SaveUtil.ClearFolder(stagingFolder.FullName); } else { stagingFolder.Create(); } itemEditor = instance.client.Workshop.EditItem(existingItem.Id); itemEditor.Visibility = Workshop.Editor.VisibilityType.Public; itemEditor.Title = existingItem.Title; itemEditor.Tags = existingItem.Tags.ToList(); itemEditor.Description = existingItem.Description; itemEditor.WorkshopUploadAppId = AppID; itemEditor.Folder = stagingFolder.FullName; string previewImagePath = Path.GetFullPath(Path.Combine(itemEditor.Folder, PreviewImageName)); itemEditor.PreviewImage = previewImagePath; using (WebClient client = new WebClient()) { if (File.Exists(previewImagePath)) { File.Delete(previewImagePath); } client.DownloadFile(new Uri(existingItem.PreviewImageUrl), previewImagePath); } ContentPackage tempContentPackage = new ContentPackage(Path.Combine(existingItem.Directory.FullName, MetadataFileName)); //item already installed, copy it from the game folder if (existingItem != null && CheckWorkshopItemEnabled(existingItem, checkContentFiles: false)) { string installedItemPath = GetWorkshopItemContentPackagePath(tempContentPackage); if (File.Exists(installedItemPath)) { tempContentPackage = new ContentPackage(installedItemPath); } } if (File.Exists(tempContentPackage.Path)) { string newContentPackagePath = Path.Combine(WorkshopItemStagingFolder, MetadataFileName); File.Copy(tempContentPackage.Path, newContentPackagePath, overwrite: true); contentPackage = new ContentPackage(newContentPackagePath); foreach (ContentFile contentFile in tempContentPackage.Files) { string sourceFile; if (contentFile.Type == ContentType.Submarine && File.Exists(contentFile.Path)) { sourceFile = contentFile.Path; } else { sourceFile = Path.Combine(existingItem.Directory.FullName, contentFile.Path); } if (!File.Exists(sourceFile)) { continue; } //make sure the destination directory exists string destinationPath = Path.Combine(WorkshopItemStagingFolder, contentFile.Path); Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)); File.Copy(sourceFile, destinationPath, overwrite: true); contentPackage.AddFile(contentFile.Path, contentFile.Type); } } else { contentPackage = ContentPackage.CreatePackage(existingItem.Title, Path.Combine(WorkshopItemStagingFolder, MetadataFileName), false); contentPackage.Save(contentPackage.Path); } }
public ContentFile(string path, ContentType type, Workshop.Item workShopItem = null) { Path = path; Type = type; WorkShopItem = workShopItem; }