Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <fstream>
- using std::ifstream;
- using std::ofstream;
- #include <map>
- using std::map;
- #include <vector>
- using std::vector;
- #include <array>
- // Not using std::array since it conflicts with System::array's use
- #include <filesystem>
- using std::experimental::filesystem::path;
- using std::experimental::filesystem::recursive_directory_iterator;
- using std::experimental::filesystem::create_directories;
- using std::experimental::filesystem::exists;
- using std::experimental::filesystem::copy;
- using std::experimental::filesystem::remove;
- #include <string>
- using std::string;
- using std::getline;
- #include <msclr\marshal_cppstd.h>
- using msclr::interop::marshal_as;
- // Global data because I don't know how forms work
- // Change this at some point?
- const std::array<char, 9> PROHIBITED_CHARACTERS
- {
- '\\', '/', ':', '*', '?', '"', '<', '>', '|'
- };
- string gCurrentScreen;
- vector<string> gSkippedScreens;
- namespace BountyScreenshot {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- using namespace System::IO;
- using namespace System::Reflection;
- /// <summary>
- /// Summary for Window
- /// </summary>
- public ref class Window : public System::Windows::Forms::Form
- {
- private: Assembly^ _assembly;
- public:
- Window(void)
- {
- InitializeComponent();
- //
- //TODO: Add the constructor code here
- //
- }
- void ChooseAlbumFolder()
- {
- if (folderBrowserSwitchAlbum->ShowDialog() == System::Windows::Forms::DialogResult::OK)
- {
- labelAlbumPath->Text = folderBrowserSwitchAlbum->SelectedPath;
- }
- }
- void ChooseDestinationFolder()
- {
- if (folderBrowserDestination->ShowDialog() == System::Windows::Forms::DialogResult::OK)
- {
- labelDestinationPath->Text = folderBrowserDestination->SelectedPath;
- }
- }
- void CheckFoldersSelected()
- {
- if (labelAlbumPath->Text != "No Album Folder Selected" &&
- labelDestinationPath->Text != "No Destination Folder Selected")
- {
- buttonStart->Enabled = true;
- labelAppStatus->Text = "Status: Ready to start";
- }
- }
- string OrganiseFiles()
- {
- // Import game list
- map<string, string> gameList;
- ifstream knownGamesIn;
- knownGamesIn.open("GameIDs.txt");
- if (knownGamesIn.is_open())
- {
- string currentLine, gameID, gameName;
- int tabPos;
- while (getline(knownGamesIn, currentLine))
- {
- tabPos = currentLine.find('\t');
- gameID = currentLine.substr(0, tabPos);
- gameName = currentLine.substr(tabPos + 1);
- gameList[gameID] = gameName;
- }
- knownGamesIn.close();
- }
- string pathString = marshal_as<string>(labelAlbumPath->Text);
- path current;
- for (auto& p : recursive_directory_iterator(pathString))
- {
- current = p;
- string fileName = current.filename().generic_string();
- if (fileName.size() == 53) // AAAAAAAAAAAAAAAA-BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.jpg (or .mp4)
- {
- string format = fileName.substr(fileName.size() - 4);
- string dateInfo = fileName.substr(0, 16);
- string gameID = fileName.substr(17, 32);
- string gameName = "";
- bool skip = false;
- if (gameList.count(gameID) == 0) // If game ID isn't in list
- {
- for (int i = 0; i < gSkippedScreens.size() && !skip; i++)
- {
- if (fileName == gSkippedScreens[i]) skip = true;
- }
- if (!skip)
- {
- delete pictureBoxScreenshotView->Image;
- if (format == ".jpg")
- pictureBoxScreenshotView->Image = Image::FromFile(marshal_as<System::String^>(current.generic_string()));
- else
- {
- _assembly = Assembly::GetExecutingAssembly();
- pictureBoxScreenshotView->Image = Image::FromStream(_assembly->GetManifestResourceStream("Video.png"));
- }
- gCurrentScreen = fileName;
- return gameID;
- }
- }
- else gameName = gameList[gameID];
- // If current cycle says to skip, don't copy it
- // Would otherwise be copied directly into the destination folder
- if (!skip)
- {
- // Build directory path, replacing illegal characters from game name with '_'
- string dir = marshal_as<string>(labelDestinationPath->Text) + "\\" + gameName;
- for (int i = dir.size() - gameName.size(); i < dir.size(); i++) for (int ch = 0; ch < PROHIBITED_CHARACTERS.size(); ch++) if (dir[i] == PROHIBITED_CHARACTERS[ch]) dir[i] = '_';
- while (dir[dir.size() - 1] == ' ') dir.pop_back(); // Remove possible spaces from end of folder since folders can't end in a space
- // Build full file path, assumes default Switch screenshot formatting, so no error checking needed here
- string file = dir + "\\" + dateInfo + format;
- // Create directory if it doesn't exist
- if (!exists(dir)) create_directories(dir);
- // Copy file if not already copied
- if (!exists(file))
- {
- copy(p, file);
- }
- }
- }
- }
- return "FINISH";
- }
- void UpdateIDList()
- {
- ofstream knownGamesOut;
- string gameID = marshal_as<string>(labelAppStatus->Text).substr(38); // Grab the game ID from the label
- string gameName = marshal_as<string>(textBoxNameEntry->Text); // Grab the name from user input
- string line = gameID + ' ' + gameName + '\n';
- knownGamesOut.open("GameIDs.txt", std::ios::app);
- knownGamesOut << line;
- knownGamesOut.close();
- // Resume copying
- delete pictureBoxScreenshotView->Image;
- _assembly = Assembly::GetExecutingAssembly();
- pictureBoxScreenshotView->Image = Image::FromStream(_assembly->GetManifestResourceStream("Screenshot-Placeholder.png"));
- labelAppStatus->Text = "Status: Organising screenshots...";
- buttonSubmit->Enabled = false;
- buttonSkip->Enabled = false;
- textBoxNameEntry->Text = "";
- textBoxNameEntry->Enabled = false;
- backgroundWorkerOrganiser->RunWorkerAsync();
- }
- protected:
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- ~Window()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::FolderBrowserDialog^ folderBrowserSwitchAlbum;
- private: System::Windows::Forms::Button^ buttonOpenAlbum;
- private: System::Windows::Forms::Label^ labelAlbumPath;
- private: System::Windows::Forms::FolderBrowserDialog^ folderBrowserDestination;
- private: System::Windows::Forms::Button^ buttonDestination;
- private: System::Windows::Forms::Label^ labelDestinationPath;
- private: System::Windows::Forms::PictureBox^ pictureBoxScreenshotView;
- private: System::Windows::Forms::TextBox^ textBoxNameEntry;
- private: System::Windows::Forms::Label^ labelAppStatus;
- private: System::Windows::Forms::Label^ labelNameEntry;
- private: System::Windows::Forms::Button^ buttonStart;
- private: System::ComponentModel::BackgroundWorker^ backgroundWorkerOrganiser;
- private: System::Windows::Forms::Button^ buttonSubmit;
- private: System::Windows::Forms::Button^ buttonSkip;
- protected:
- private:
- /// <summary>
- /// Required designer variable.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- void InitializeComponent(void)
- {
- System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Window::typeid));
- this->folderBrowserSwitchAlbum = (gcnew System::Windows::Forms::FolderBrowserDialog());
- this->buttonOpenAlbum = (gcnew System::Windows::Forms::Button());
- this->labelAlbumPath = (gcnew System::Windows::Forms::Label());
- this->folderBrowserDestination = (gcnew System::Windows::Forms::FolderBrowserDialog());
- this->buttonDestination = (gcnew System::Windows::Forms::Button());
- this->labelDestinationPath = (gcnew System::Windows::Forms::Label());
- this->pictureBoxScreenshotView = (gcnew System::Windows::Forms::PictureBox());
- this->textBoxNameEntry = (gcnew System::Windows::Forms::TextBox());
- this->labelAppStatus = (gcnew System::Windows::Forms::Label());
- this->labelNameEntry = (gcnew System::Windows::Forms::Label());
- this->buttonStart = (gcnew System::Windows::Forms::Button());
- this->backgroundWorkerOrganiser = (gcnew System::ComponentModel::BackgroundWorker());
- this->buttonSubmit = (gcnew System::Windows::Forms::Button());
- this->buttonSkip = (gcnew System::Windows::Forms::Button());
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBoxScreenshotView))->BeginInit();
- this->SuspendLayout();
- //
- // folderBrowserSwitchAlbum
- //
- this->folderBrowserSwitchAlbum->Description = L"Eg. D:\\Nintendo\\Album";
- this->folderBrowserSwitchAlbum->RootFolder = System::Environment::SpecialFolder::MyComputer;
- this->folderBrowserSwitchAlbum->ShowNewFolderButton = false;
- //
- // buttonOpenAlbum
- //
- this->buttonOpenAlbum->Location = System::Drawing::Point(13, 13);
- this->buttonOpenAlbum->Name = L"buttonOpenAlbum";
- this->buttonOpenAlbum->Size = System::Drawing::Size(135, 23);
- this->buttonOpenAlbum->TabIndex = 0;
- this->buttonOpenAlbum->Text = L"Open Album Folder";
- this->buttonOpenAlbum->UseVisualStyleBackColor = true;
- this->buttonOpenAlbum->Click += gcnew System::EventHandler(this, &Window::buttonOpenAlbum_Click);
- //
- // labelAlbumPath
- //
- this->labelAlbumPath->AutoSize = true;
- this->labelAlbumPath->Location = System::Drawing::Point(154, 18);
- this->labelAlbumPath->Name = L"labelAlbumPath";
- this->labelAlbumPath->Size = System::Drawing::Size(130, 13);
- this->labelAlbumPath->TabIndex = 1;
- this->labelAlbumPath->Text = L"No Album Folder Selected";
- //
- // buttonDestination
- //
- this->buttonDestination->Location = System::Drawing::Point(13, 43);
- this->buttonDestination->Name = L"buttonDestination";
- this->buttonDestination->Size = System::Drawing::Size(135, 23);
- this->buttonDestination->TabIndex = 2;
- this->buttonDestination->Text = L"Open Destination Folder";
- this->buttonDestination->UseVisualStyleBackColor = true;
- this->buttonDestination->Click += gcnew System::EventHandler(this, &Window::buttonDestination_Click);
- //
- // labelDestinationPath
- //
- this->labelDestinationPath->AutoSize = true;
- this->labelDestinationPath->Location = System::Drawing::Point(154, 48);
- this->labelDestinationPath->Name = L"labelDestinationPath";
- this->labelDestinationPath->Size = System::Drawing::Size(154, 13);
- this->labelDestinationPath->TabIndex = 3;
- this->labelDestinationPath->Text = L"No Destination Folder Selected";
- //
- // pictureBoxScreenshotView
- //
- this->pictureBoxScreenshotView->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"pictureBoxScreenshotView.Image")));
- this->pictureBoxScreenshotView->InitialImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"pictureBoxScreenshotView.InitialImage")));
- this->pictureBoxScreenshotView->Location = System::Drawing::Point(11, 140);
- this->pictureBoxScreenshotView->Name = L"pictureBoxScreenshotView";
- this->pictureBoxScreenshotView->Size = System::Drawing::Size(360, 202);
- this->pictureBoxScreenshotView->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
- this->pictureBoxScreenshotView->TabIndex = 4;
- this->pictureBoxScreenshotView->TabStop = false;
- //
- // textBoxNameEntry
- //
- this->textBoxNameEntry->Enabled = false;
- this->textBoxNameEntry->Location = System::Drawing::Point(11, 114);
- this->textBoxNameEntry->Name = L"textBoxNameEntry";
- this->textBoxNameEntry->Size = System::Drawing::Size(236, 20);
- this->textBoxNameEntry->TabIndex = 5;
- this->textBoxNameEntry->KeyUp += gcnew System::Windows::Forms::KeyEventHandler(this, &Window::textBoxNameEntry_KeyUp);
- //
- // labelAppStatus
- //
- this->labelAppStatus->AutoSize = true;
- this->labelAppStatus->Location = System::Drawing::Point(12, 352);
- this->labelAppStatus->Name = L"labelAppStatus";
- this->labelAppStatus->Size = System::Drawing::Size(198, 13);
- this->labelAppStatus->TabIndex = 6;
- this->labelAppStatus->Text = L"Status: Waiting for folders to be selected";
- //
- // labelNameEntry
- //
- this->labelNameEntry->AutoSize = true;
- this->labelNameEntry->Location = System::Drawing::Point(12, 98);
- this->labelNameEntry->Name = L"labelNameEntry";
- this->labelNameEntry->Size = System::Drawing::Size(177, 13);
- this->labelNameEntry->TabIndex = 7;
- this->labelNameEntry->Text = L"Enter the name of the current game:";
- //
- // buttonStart
- //
- this->buttonStart->Enabled = false;
- this->buttonStart->Location = System::Drawing::Point(13, 72);
- this->buttonStart->Name = L"buttonStart";
- this->buttonStart->Size = System::Drawing::Size(359, 23);
- this->buttonStart->TabIndex = 8;
- this->buttonStart->Text = L"Start Organising";
- this->buttonStart->UseVisualStyleBackColor = true;
- this->buttonStart->Click += gcnew System::EventHandler(this, &Window::buttonStart_Click);
- //
- // backgroundWorkerOrganiser
- //
- this->backgroundWorkerOrganiser->WorkerSupportsCancellation = true;
- this->backgroundWorkerOrganiser->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Window::backgroundWorkerOrganiser_DoWork);
- this->backgroundWorkerOrganiser->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &Window::backgroundWorkerOrganiser_ProgressChanged);
- this->backgroundWorkerOrganiser->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &Window::backgroundWorkerOrganiser_RunWorkerCompleted);
- //
- // buttonSubmit
- //
- this->buttonSubmit->Enabled = false;
- this->buttonSubmit->Location = System::Drawing::Point(253, 112);
- this->buttonSubmit->Name = L"buttonSubmit";
- this->buttonSubmit->Size = System::Drawing::Size(56, 23);
- this->buttonSubmit->TabIndex = 11;
- this->buttonSubmit->Text = L"Submit";
- this->buttonSubmit->UseVisualStyleBackColor = true;
- this->buttonSubmit->Click += gcnew System::EventHandler(this, &Window::buttonSubmit_Click);
- //
- // buttonSkip
- //
- this->buttonSkip->Enabled = false;
- this->buttonSkip->Location = System::Drawing::Point(315, 112);
- this->buttonSkip->Name = L"buttonSkip";
- this->buttonSkip->Size = System::Drawing::Size(56, 23);
- this->buttonSkip->TabIndex = 12;
- this->buttonSkip->Text = L"Skip";
- this->buttonSkip->UseVisualStyleBackColor = true;
- this->buttonSkip->Click += gcnew System::EventHandler(this, &Window::buttonSkip_Click);
- //
- // Window
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(384, 391);
- this->Controls->Add(this->buttonSkip);
- this->Controls->Add(this->buttonSubmit);
- this->Controls->Add(this->buttonStart);
- this->Controls->Add(this->labelNameEntry);
- this->Controls->Add(this->labelAppStatus);
- this->Controls->Add(this->textBoxNameEntry);
- this->Controls->Add(this->pictureBoxScreenshotView);
- this->Controls->Add(this->labelDestinationPath);
- this->Controls->Add(this->buttonDestination);
- this->Controls->Add(this->labelAlbumPath);
- this->Controls->Add(this->buttonOpenAlbum);
- this->MaximizeBox = false;
- this->MaximumSize = System::Drawing::Size(400, 430);
- this->MinimumSize = System::Drawing::Size(400, 430);
- this->Name = L"Window";
- this->Text = L"Switch Screenshot Organiser";
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBoxScreenshotView))->EndInit();
- this->ResumeLayout(false);
- this->PerformLayout();
- }
- #pragma endregion
- private: System::Void buttonOpenAlbum_Click(System::Object^ sender, System::EventArgs^ e)
- {
- ChooseAlbumFolder();
- CheckFoldersSelected();
- }
- private: System::Void buttonDestination_Click(System::Object^ sender, System::EventArgs^ e)
- {
- ChooseDestinationFolder();
- CheckFoldersSelected();
- }
- private: System::Void backgroundWorkerOrganiser_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
- {
- e->Result = marshal_as<String^>(OrganiseFiles());
- }
- private: System::Void backgroundWorkerOrganiser_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e)
- {
- }
- private: System::Void backgroundWorkerOrganiser_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e)
- {
- string ret = marshal_as<string>(static_cast<String^>(e->Result));
- if (ret == "FINISH")
- {
- labelAppStatus->Text = "Status: Finished!";
- buttonOpenAlbum->Enabled = true;
- buttonDestination->Enabled = true;
- buttonStart->Enabled = true;
- }
- else
- {
- labelAppStatus->Text = "Status: Waiting for game name for ID \n" + e->Result;
- buttonSubmit->Enabled = true;
- buttonSkip->Enabled = true;
- textBoxNameEntry->Enabled = true;
- textBoxNameEntry->Focus();
- }
- }
- private: System::Void buttonStart_Click(System::Object^ sender, System::EventArgs^ e)
- {
- labelAppStatus->Text = "Status: Organising screenshots...";
- buttonOpenAlbum->Enabled = false;
- buttonDestination->Enabled = false;
- buttonStart->Enabled = false;
- backgroundWorkerOrganiser->RunWorkerAsync();
- }
- private: System::Void buttonSubmit_Click(System::Object^ sender, System::EventArgs^ e)
- {
- if (textBoxNameEntry->Text != "")
- {
- UpdateIDList();
- }
- }
- private: System::Void textBoxNameEntry_KeyUp(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
- {
- if (e->KeyCode == Keys::Enter && textBoxNameEntry->Text != "")
- {
- UpdateIDList();
- }
- }
- private: System::Void buttonSkip_Click(System::Object^ sender, System::EventArgs^ e)
- {
- gSkippedScreens.push_back(gCurrentScreen);
- // Resume copying
- delete pictureBoxScreenshotView->Image;
- _assembly = Assembly::GetExecutingAssembly();
- pictureBoxScreenshotView->Image = Image::FromStream(_assembly->GetManifestResourceStream("Screenshot-Placeholder.png"));
- labelAppStatus->Text = "Status: Organising screenshots...";
- buttonSubmit->Enabled = false;
- buttonSkip->Enabled = false;
- textBoxNameEntry->Text = "";
- textBoxNameEntry->Enabled = false;
- backgroundWorkerOrganiser->RunWorkerAsync();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement