Skip to content

Instantly share code, notes, and snippets.

@iUltimateLP
Last active August 31, 2023 20:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iUltimateLP/636709e9ae1472d4ac5df96cc0f2ee79 to your computer and use it in GitHub Desktop.
Save iUltimateLP/636709e9ae1472d4ac5df96cc0f2ee79 to your computer and use it in GitHub Desktop.
Example Unreal Engine 4/5 Build.cs script that automatically fetches the current Perforce changelist and embeds it into code
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
using System;
using UnrealBuildTool;
using System.Diagnostics;
public class BuildScriptUtils
{
// Runs p4.exe to determine the current changelist
// Returns true if it could determine the changelist, false if not
// The out changelist parameter is the current changelist, or 0.
public static bool GetP4Changelist(out int changelist)
{
// Connection constants
const string P4_IP = "your-p4-server.com:1666";
const string P4_USER = "username";
const string P4_PASS = "password";
// Default value for the changelist
changelist = 0;
// Try to get the last line the process output
try
{
// Try to obtain the current P4 changelist by running p4.exe
Process process = new Process();
process.StartInfo.FileName = "p4.exe";
process.StartInfo.Arguments = "-p " + P4_IP + " -u " + P4_USER + " -P " + P4_PASS + " changes -m 1 -s submitted";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
// The response of the command above looks like this:
// Change 420 on 2022/08/02 by User@Workspace 'Some changelist message'
string changelistStr = process.StandardOutput.ReadLine();
if (changelistStr != null && changelistStr.Length > 0)
{
// Remove the "Change " at the beginning and then take the first string
changelistStr = changelistStr.Substring("Change ".Length);
changelistStr = changelistStr.Split(' ')[0];
// Parse as int
changelist = int.Parse(changelistStr);
// Debug print
Console.WriteLine("Determined P4 changelist: " + changelist);
return true;
}
else
{
throw new Exception();
}
}
catch (Exception e)
{
// Catch any exception and just return false
Console.WriteLine("Error determining P4 changelist!");
return false;
}
}
}
public class ExampleProject : ModuleRules
{
public ExampleProject(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
// Public modules used by the game
PublicDependencyModuleNames.AddRange(new string[] {
"Core",
"CoreUObject",
"Engine",
"InputCore"
});
// Get the current P4 changelist and set it as a code definition
int changelist = 0;
if (BuildScriptUtils.GetP4Changelist(out changelist))
{
// Save it as a definition so the game can access it
PublicDefinitions.Add("PROJECT_CL=" + changelist);
}
}
}
@gustmes
Copy link

gustmes commented Dec 16, 2022

Is there a way to run the p4 process without hard coding your password in code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment