Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new folder, copies the specified files there and creates a metadata file with install instructions.
        /// </summary>
        public static void CreateWorkshopItemStaging(List <ContentFile> contentFiles, out Workshop.Editor itemEditor, out ContentPackage contentPackage)
        {
            var stagingFolder = new DirectoryInfo(WorkshopItemStagingFolder);

            if (stagingFolder.Exists)
            {
                SaveUtil.ClearFolder(stagingFolder.FullName);
            }
            else
            {
                stagingFolder.Create();
            }
            Directory.CreateDirectory(Path.Combine(WorkshopItemStagingFolder, "Submarines"));
            Directory.CreateDirectory(Path.Combine(WorkshopItemStagingFolder, "Mods"));
            Directory.CreateDirectory(Path.Combine(WorkshopItemStagingFolder, "Mods", "ModName"));

            itemEditor                     = instance.client.Workshop.CreateItem(Workshop.ItemType.Community);
            itemEditor.Visibility          = Workshop.Editor.VisibilityType.Public;
            itemEditor.WorkshopUploadAppId = AppID;
            itemEditor.Folder              = stagingFolder.FullName;

            string previewImagePath = Path.GetFullPath(Path.Combine(itemEditor.Folder, PreviewImageName));

            File.Copy("Content/DefaultWorkshopPreviewImage.png", previewImagePath);

            //copy content files to the staging folder
            List <string> copiedFilePaths = new List <string>();

            foreach (ContentFile file in contentFiles)
            {
                string relativePath    = UpdaterUtil.GetRelativePath(Path.GetFullPath(file.Path), Environment.CurrentDirectory);
                string destinationPath = Path.Combine(stagingFolder.FullName, relativePath);
                //make sure the directory exists
                Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
                File.Copy(file.Path, destinationPath);
                copiedFilePaths.Add(destinationPath);
            }
            System.Diagnostics.Debug.Assert(copiedFilePaths.Count == contentFiles.Count);

            //create a new content package and include the copied files in it
            contentPackage = ContentPackage.CreatePackage("ContentPackage", Path.Combine(itemEditor.Folder, MetadataFileName), false);
            for (int i = 0; i < copiedFilePaths.Count; i++)
            {
                contentPackage.AddFile(copiedFilePaths[i], contentFiles[i].Type);
            }

            contentPackage.Save(Path.Combine(stagingFolder.FullName, MetadataFileName));
        }
Ejemplo n.º 2
0
        /// <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);
            }
        }
Ejemplo n.º 3
0
        /// <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);
            }
        }