Skip to content

Commit

Permalink
Automatically grab good file version (#10632)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bod9001 authored Dec 2, 2024
1 parent d6334ae commit e57bb66
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private async Task SendServerStatus()
status.ForkName = buildInfo.ForkName;
status.BuildVersion = buildInfo.BuildNumber;

status.GoodFileVersion = buildInfo.GoodFileVersion;

if (SubSceneManager.Instance == null)
{
status.CurrentMap = "loading";
Expand Down Expand Up @@ -294,6 +296,7 @@ public class ServerStatus
public string OSXDownload;
public string LinuxDownload;
public int fps;
public string GoodFileVersion;
}

//Read from Streaming Assets/config/config.json on the server
Expand Down Expand Up @@ -364,5 +367,8 @@ public class BuildInfo
public int BuildNumber;
//I.E. Unitystation, ColonialMarines, BeeStation
public string ForkName;

//What good file version does this build use
public string GoodFileVersion;
}
}
90 changes: 90 additions & 0 deletions UnityProject/Assets/Scripts/Core/Editor/GrabGoodFileVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Diagnostics;
using DatabaseAPI;
using Logs;
using Newtonsoft.Json;
using SecureStuff;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;

public class GrabGoodFileVersion : IPreprocessBuild
{
public int callbackOrder
{
get { return 9; }
}

public void OnPreprocessBuild(BuildTarget target, string path)
{
var Gamedata = AssetDatabase.LoadAssetAtPath<GameObject>(
"Assets/Prefabs/SceneConstruction/NestedManagers/GameData.prefab");
if (Gamedata.GetComponent<GameData>().DevBuild)
{
return;
}

try
{
// Get the latest good-file version tag
string latestTag = GetLatestGoodFileVersion();

var BuildInfo = JsonConvert.DeserializeObject<BuildInfo>(AccessFile.Load("buildinfo.json"));
BuildInfo.GoodFileVersion = latestTag.Replace("good-file-", "");
AccessFile.Save("buildinfo.json", JsonConvert.SerializeObject(BuildInfo));
}
catch ( Exception ex)
{
Loggy.Warning( "Not able to set good file version " + ex.ToString());

}
}

private string GetLatestGoodFileVersion()
{
try
{
// Set up the Git process to get tags
Process gitProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = "tag --list good-file-* --sort=-v:refname",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};

UnityEngine.Debug.Log("[GrabGoodFileVersion] Running Git command to fetch tags...");
UnityEngine.Debug.Log($"[GrabGoodFileVersion] Command: git {gitProcess.StartInfo.Arguments}");

// Start the process and capture the output
gitProcess.Start();
string output = gitProcess.StandardOutput.ReadToEnd().Trim();
string error = gitProcess.StandardError.ReadToEnd();
gitProcess.WaitForExit();

UnityEngine.Debug.Log($"[GrabGoodFileVersion] Git process exited with code {gitProcess.ExitCode}.");
UnityEngine.Debug.Log($"[GrabGoodFileVersion] Standard Output:\n{output}");
UnityEngine.Debug.Log($"[GrabGoodFileVersion] Standard Error:\n{error}");

if (gitProcess.ExitCode != 0)
{
UnityEngine.Debug.LogError($"[GrabGoodFileVersion] Git process failed with exit code {gitProcess.ExitCode}.");
return null;
}

// Split the output into lines and take the first line as the latest tag
string[] tags = output.Split('\n');
return tags.Length > 0 ? tags[0] : null;;
}
catch (System.Exception ex)
{
UnityEngine.Debug.LogError($"An error occurred while retrieving the Git tag: {ex.Message}");
return null;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion UnityProject/Assets/StreamingAssets/Config/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"BuildNumber": 3921,"ForkName":"Unitystation"}
{"BuildNumber":3921,"ForkName":"Unitystation","GoodFileVersion":"good-file-0.1.0"}

0 comments on commit e57bb66

Please sign in to comment.