Skip to content

Commit

Permalink
Add RuntimeSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus committed Oct 25, 2023
1 parent 3a8d18d commit e98face
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 52 deletions.
2 changes: 0 additions & 2 deletions Editor/Helpers/BuildManagerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ public static SuccessfulBuildTargets GetSuccessfulBuildTargets(DistributionPlatf
}
}

private static string buildPath = null;
public static string BuildPath {
get {
return General.paths.BuildsFolder;
}
}

private static string addonsPath = null;
public static string AddonsPath {
get {
return General.paths.AddonsFolder;
Expand Down
2 changes: 1 addition & 1 deletion Editor/Helpers/Steam/SteamWebApiGetBranches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void UpdateBetaBranches(int appID) {

void UpdateFunc() {
if (webRequest.isDone) {
if (!webRequest.isNetworkError) {
if (webRequest.result != UnityWebRequest.Result.ConnectionError) {
try {
ApiResponse response = JsonConvert.DeserializeObject<ApiResponse>(webRequest.downloadHandler.text);
if (response != null && response.Response != null && response.Response.Betas != null) {
Expand Down
57 changes: 57 additions & 0 deletions Runtime/BuildManagerRuntimeSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.IO;
using UnityEditor;
using UnityEngine;

namespace BuildManager {
public class BuildManagerRuntimeSettings : ScriptableObject {
[SerializeField]
private string versionInfoPath = "VersionInfo.json";
public string VersionInfoPath {
get {
return Path.Combine("Assets/Resources/", versionInfoPath);
}
}

[SerializeField]
private string steamAppIdPath = "SteamAppId.json";
public string SteamAppIdPath {
get {
return Path.Combine("Assets/Resources/", steamAppIdPath);
}
}

[SerializeField]
private string gitHeadPath = "../../../.git/logs/HEAD";
public string GitHeadPath {
get {
return gitHeadPath;
}
}


private static BuildManagerRuntimeSettings instance;
public static BuildManagerRuntimeSettings Instance {
get {
if (instance == null) {
instance = Resources.Load<BuildManagerRuntimeSettings>("Settings/BuildManagerRuntimeSettings");
}

if(instance == null) {
BuildManagerRuntimeSettings asset = CreateInstance<BuildManagerRuntimeSettings>();

AssetDatabase.CreateAsset(asset, "Assets/Resources/Settings/BuildManagerRuntimeSettings.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}

return instance;
}
}


[InitializeOnLoadMethod]
public static void Initialize() {
instance = Instance;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/BuildManagerRuntimeSettings.cs.meta

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

78 changes: 40 additions & 38 deletions Runtime/SteamAppID.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Newtonsoft.Json.Linq;
using System.IO;

namespace BuildManager {
#if UNITY_EDITOR || STEAM
/// <summary>
Expand All @@ -6,57 +9,56 @@ namespace BuildManager {
/// This is relevant for editor tools like the build manager but also the steam backend service.
/// </summary>
public class SteamAppID {

/// <summary>
/// The internally managed appID
/// </summary>
private static int appID = 0;

/// <summary>
/// Static AppID setter/getter. Please keep in mind that setting this variable to another value,
/// forces a rewrite of the SteamAppID.cs file.
/// Static AppID setter/getter
/// </summary>
public static int AppID {
get {
return appID;
return GetSteamAppIdValue<int>("steamAppId");
}
#if UNITY_EDITOR
set {
if (appID != value) {

// convert GUID of SteamAppID.cs into asset path
string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath("86d0eecdc263a50499f9bbb431e0a96b");
SetSteamAppIdValue("steamAppId", value.ToString());
}
}

// replace appID with
string contents = System.IO.File.ReadAllText(assetPath);
string appIDVariable = nameof(appID) + " = ";
contents = contents.Replace(appIDVariable + appID, appIDVariable + value);
#region JSON
/// <summary>
/// Try to Load VersionInfo JSON File, otherwise it creates a new one
/// </summary>
/// <returns>T</returns>
private static T GetSteamAppIdValue<T>(string key) {
if (!File.Exists(BuildManagerRuntimeSettings.Instance.SteamAppIdPath)) {
//Create Default JSON File
CreateDefaultSteamAppIdFile();
}

// write to disk
System.IO.File.WriteAllText(assetPath, contents);
JObject json = JObject.Parse(File.ReadAllText(BuildManagerRuntimeSettings.Instance.SteamAppIdPath));
return json.Value<T>(key);
}

// change steam_appid.txt file to contain the correct app id
SetSteamAppidTxtFile();
UnityEditor.AssetDatabase.Refresh();
}
}
/// <summary>
/// Set JSON value for given key
/// </summary>
/// <param name="key">Json Key</param>
/// <param name="value">Json Value</param>
private static void SetSteamAppIdValue(string key, string value) {
JObject json = JObject.Parse(File.ReadAllText(BuildManagerRuntimeSettings.Instance.SteamAppIdPath));
json[key] = value;
File.WriteAllText(BuildManagerRuntimeSettings.Instance.SteamAppIdPath, json.ToString());
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
}

#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
private static void SetSteamAppidTxtFile() {
string pathToSteamAppidFile = System.IO.Path.Combine(UnityEngine.Application.dataPath, "../steam_appid.txt");
if (System.IO.File.Exists(pathToSteamAppidFile)) {
string appIDString = appID.ToString();
if (System.IO.File.ReadAllText(pathToSteamAppidFile) != appIDString) {
System.IO.File.WriteAllText(pathToSteamAppidFile, appIDString);
}
} else {
UnityEngine.Debug.Log("[Steam] steam_appid.txt could not be found at path: " + System.IO.Path.GetFullPath(pathToSteamAppidFile));
}
/// <summary>
/// Create Default JSON File
/// </summary>
private static void CreateDefaultSteamAppIdFile() {
JObject json = new JObject();
json["steamAppId"] = "0";
File.WriteAllText(BuildManagerRuntimeSettings.Instance.SteamAppIdPath, json.ToString());
}
#endif
#endregion
}
#endif
}
20 changes: 9 additions & 11 deletions Runtime/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class VersionInfo {
/// A specialized, custom version name
/// </summary>
public const string customVersionName = "";
private static string versionInfoPath = Path.Combine(Path.Combine(Application.dataPath, "Resources"), "VersionInfo.json");

#region BuildCounter
/// <summary>
Expand Down Expand Up @@ -94,9 +93,9 @@ public static void UpdateGitHash() {

public static string CreateGitHash() {
string latestCommitHash = "";
string path = System.IO.Path.Combine(Application.dataPath, "../../../.git/logs/HEAD");
if (System.IO.File.Exists(path)) {
string[] lines = System.IO.File.ReadAllLines(path);
string path = BuildManagerRuntimeSettings.Instance.GitHeadPath;
if (File.Exists(path)) {
string[] lines = File.ReadAllLines(path);
if (lines != null && lines.Length > 0) {
string latestLine = lines[lines.Length - 1];
string[] latestLineData = latestLine.Split(' ');
Expand Down Expand Up @@ -241,12 +240,12 @@ public static string VersionName {
/// </summary>
/// <returns>T</returns>
private static T GetVersionInfoValue<T>(string key) {
if (!File.Exists(versionInfoPath)) {
if (!File.Exists(BuildManagerRuntimeSettings.Instance.VersionInfoPath)) {
//Create Default JSON File
CreateDefaultVersionInfo();
}

JObject json = JObject.Parse(File.ReadAllText(versionInfoPath));
JObject json = JObject.Parse(File.ReadAllText(BuildManagerRuntimeSettings.Instance.VersionInfoPath));
return json.Value<T>(key);
}

Expand All @@ -256,9 +255,9 @@ private static T GetVersionInfoValue<T>(string key) {
/// <param name="key">Json Key</param>
/// <param name="value">Json Value</param>
private static void SetVersionInfoValue(string key, string value) {
JObject json = JObject.Parse(File.ReadAllText(versionInfoPath));
JObject json = JObject.Parse(File.ReadAllText(BuildManagerRuntimeSettings.Instance.VersionInfoPath));
json[key] = value;
File.WriteAllText(versionInfoPath, json.ToString());
File.WriteAllText(BuildManagerRuntimeSettings.Instance.VersionInfoPath, json.ToString());
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
Expand All @@ -273,7 +272,7 @@ private static void CreateDefaultVersionInfo() {
json["buildCounter"] = "0";
json["buildTimestamp"] = "";
json["gitHash"] = "";
File.WriteAllText(versionInfoPath, json.ToString());
File.WriteAllText(BuildManagerRuntimeSettings.Instance.VersionInfoPath, json.ToString());
}
#endregion

Expand Down Expand Up @@ -356,5 +355,4 @@ public void Log() {
}
}
}
}

}

0 comments on commit e98face

Please sign in to comment.