Using GPT4 to automate the publishing of a Unity Game Called Xintra to the itch.io platform

White Paper: Converting a Batch File to an Executable with User Input in C#



Introduction

This white paper discusses how to convert a batch file into an executable (.exe) file using C# and Visual Studio 2022. The original batch file uploads a game called 'Xintra' to itch.io using the butler command-line tool. 

The goal is to turn this into an executable that asks the user for specific input parameters (e.g., version number) and then runs the batch file with those parameters. What makes this interesting is that the blog and the code were created by GPT4.

Prompt Provided to GPT4 

"Hey, so I have a .bat file (pushXintra.bat) that uploads my game to itch.io. Can you turn this into and executable and if you can give the C# code to do so along with instructions on how to compile it into one standalone .exe using Visual Studio 2022. Let me know when you are ready, and I will paste the .bat file for you below."

Process Overview

Create a new C# Console App project in Visual Studio 2022.

Modify the Program.cs file to include the following features:

Accept user input for specific parameters (version number).

Generate a temporary batch file with the provided input parameters.

Run the temporary batch file.

Delete the temporary batch file after execution.

Build and publish the .exe file using Visual Studio 2022.

Step-by-Step Guide

Step 1: Create a new C# Console App project in Visual Studio 2022

Open Visual Studio 2022 and create a new C# Console App project. Name it "PushToItchIO" or any preferred name.


Step 2: Modify the Program.cs file

Replace the contents of the Program.cs file with the following code:

using System;
using System.Diagnostics;
using System.IO;

namespace PushToItchIO
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the version number (e.g., 0.0.140):");
            string versionNumber = Console.ReadLine();

            string batchFileContents = $@"
@echo off
cls
echo ""Uploading patch for Xintra to itch.io""
echo ""Version: {versionNumber}""

pause
REM butler push -v ""XintraV{versionNumber}"" yourusername/xintra:windows --userversion {versionNumber}

set pathToBuild=D:\\TEST\\
REM Extract the zip file to a folder like below
set soureFile=""XintraV{versionNumber}""
set butlerName=yourusername/xintra:windows --userversion {versionNumber}

D:
cd %pathToBuild%

butler push -v %soureFile% %butlerName%
echo ""Working...""
echo ""Checking Status...""
timeout /t 360 /nobreak
butler status yourusername/xintra:windows
";

            string tempBatchFilePath = CreateTempBatchFile(batchFileContents);
            RunBatchFile(tempBatchFilePath);
            File.Delete(tempBatchFilePath);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static string CreateTempBatchFile(string contents)
        {
            string tempFilePath = Path.GetTempFileName().Replace(".tmp", ".bat");
            File.WriteAllText(tempFilePath, contents);
            return tempFilePath;
        }

        private static void RunBatchFile(string filePath)
        {
            ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/c \"" + filePath + "\"");
            processInfo.CreateNoWindow = false; // Show the batch file window
            processInfo.UseShellExecute = true; // Use shell execute to allow interaction with the batch file window

            Process process = new Process();
            process.StartInfo = processInfo;
            process.Start();
            process.WaitForExit(); // Wait for the batch file to finish executing
        }
    }
}
--------------------------------------------------------------------
The code includes the following modifications:

Accept user input for the version number.
Generate a temporary batch file with the provided input parameters.
Run the temporary batch file.
--------------------------------------------------------------------
Delete the temporary batch file after execution.

Here's a brief explanation of the main components of the code:

Main method:

Asks the user for the version number.
Creates the batchFileContents string with the original batch file content and inserts 
the version number.
Calls the CreateTempBatchFile method to create a temporary batch file with the
generated contents.
Calls the RunBatchFile method to execute the temporary batch file.
Deletes the temporary batch file after execution.
Waits for the user to press any key to exit.
CreateTempBatchFile method:

Takes a string contents as input, which represents the content of the batch file.
Creates a temporary file with a .bat extension.
Writes the provided contents to the temporary file.
Returns the path to the temporary file.
RunBatchFile method:

Takes a string filePath as input, which represents the path to the batch file.
Creates a ProcessStartInfo object with the necessary settings to execute the batch file.
Starts a new Process with the configured ProcessStartInfo object.
Waits for the batch file to finish executing.
Step 3: Build and publish the .exe file using Visual Studio 2022
In Visual Studio 2022, click on "Build" in the menu and then select "Build Solution" 
to build the project.
Once the build is successful, click on "Build" again in the menu and select 
"Publish PushToItchIO" (or the name of your project).
In the "Publish" window, click "Next" and choose the "Folder" target type.
Select a folder where you want the published .exe file to be saved and click "Finish".
Visual Studio 2022 will publish the .exe file to the specified folder. 
You can now share and run the .exe file on compatible systems.
Conclusion
This white paper provided a step-by-step guide on converting a batch file into 
an executable using C# and Visual Studio 2022. 
The resulting executable file accepts user input for the version number and runs 
the batch file with the provided input. 
The user can interact with the batch file window directly, and the program waits for 
the user to press any key to exit after the batch file execution is complete.

Comments

Popular posts from this blog

Clone Command Bar Button in Model Driven App: Order & OrderDetails

Model-Driven Apps: Modern Command Bar Customization

Knowledge shared is power squared!