diff --git a/Assets/Rivet/Editor/BuildPipeline.cs b/Assets/Rivet/Editor/BuildPipeline.cs index 4331c12..99d0a0d 100644 --- a/Assets/Rivet/Editor/BuildPipeline.cs +++ b/Assets/Rivet/Editor/BuildPipeline.cs @@ -1,7 +1,7 @@ -using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; using UnityEngine; +using System.IO; public class BuildScript : IPreprocessBuildWithReport, IPostprocessBuildWithReport { @@ -9,17 +9,43 @@ public class BuildScript : IPreprocessBuildWithReport, IPostprocessBuildWithRepo public void OnPreprocessBuild(BuildReport report) { + // Check if StreamingAssets folder exists and create it if it doesn't + string streamingAssetsPath = Application.streamingAssetsPath; + if (!Directory.Exists(streamingAssetsPath)) + { + Directory.CreateDirectory(streamingAssetsPath); + } + + // If either is null, add a build error + if (string.IsNullOrEmpty(ExtensionData.ApiEndpoint)) + { + Debug.LogError("Rivet API endpoint is not set. Please set the API endpoint in the Rivet settings."); + } + + if (string.IsNullOrEmpty(ExtensionData.RivetToken)) + { + Debug.LogError("Rivet token is not set. Please set the Rivet token in the Rivet settings."); + } + // Create the asset file before the build - RivetSettings data = ScriptableObject.CreateInstance(); - data.ApiEndpoint = ExtensionData.ApiEndpoint; - data.RivetToken = ExtensionData.RivetToken; - AssetDatabase.CreateAsset(data, "Assets/rivet_export.asset"); - AssetDatabase.SaveAssets(); + RivetSettings data = new RivetSettings + { + ApiEndpoint = ExtensionData.ApiEndpoint, + RivetToken = ExtensionData.RivetToken + }; + + string json = JsonUtility.ToJson(data); + string filePath = Path.Combine(Application.streamingAssetsPath, "rivet_export.json"); + File.WriteAllText(filePath, json); } public void OnPostprocessBuild(BuildReport report) { // Delete the asset file after the build - AssetDatabase.DeleteAsset("Assets/rivet_export.asset"); + string filePath = Path.Combine(Application.streamingAssetsPath, "rivet_export.json"); + if (File.Exists(filePath)) + { + File.Delete(filePath); + } } -} \ No newline at end of file +} diff --git a/Assets/Rivet/Editor/RivetPlugin.cs b/Assets/Rivet/Editor/RivetPlugin.cs index 25e0516..78777f5 100644 --- a/Assets/Rivet/Editor/RivetPlugin.cs +++ b/Assets/Rivet/Editor/RivetPlugin.cs @@ -1,15 +1,56 @@ using UnityEngine; using UnityEditor; +/// +/// Provides extension data for the Rivet plugin. +/// public static class ExtensionData { - public static string RivetToken { get; set; } + private static string rivetToken; private static string apiEndpoint = "https://api.rivet.gg"; + /// + /// Gets or sets the Rivet token. + /// + /// + /// The Rivet token is used for authentication with the Rivet API. + /// When the token is set, it is also stored in the PlayerPrefs for persistence. + /// + public static string RivetToken + { + get { return rivetToken; } + set + { + rivetToken = value; + // This might not be called from the main thread, so we need to + // delay the call to PlayerPrefs + UnityEditor.EditorApplication.delayCall += () => + { + PlayerPrefs.SetString("RivetToken", value); + }; + } + } + + /// + /// Gets or sets the API endpoint. + /// + /// + /// The API endpoint is the base URL for the Rivet API. + /// When the endpoint is set, it is also stored in the PlayerPrefs for persistence. + /// public static string ApiEndpoint { get { return apiEndpoint; } - set { apiEndpoint = value; } + set + { + apiEndpoint = value; + // This might not be called from the main thread, so we need to + // delay the call to PlayerPrefs + UnityEditor.EditorApplication.delayCall += () => + { + PlayerPrefs.SetString("ApiEndpoint", value); + }; + } } } diff --git a/Assets/Rivet/Editor/Windows/Plugin.cs b/Assets/Rivet/Editor/Windows/Plugin.cs index 8a6e0a6..fda666e 100644 --- a/Assets/Rivet/Editor/Windows/Plugin.cs +++ b/Assets/Rivet/Editor/Windows/Plugin.cs @@ -7,6 +7,7 @@ using System.Net; using System.Collections.Generic; using System.Security.Cryptography; +using System.IO; public struct BootstrapData { @@ -95,6 +96,7 @@ public void OnEnter(RivetPluginWindow pluginWindow) new System.Threading.Thread(() => { GetBootstrapData(); + GetNamespaceToken(); }).Start(); } @@ -281,6 +283,13 @@ public void OnGUI() // Build the player var result = BuildPipeline.BuildPlayer(buildPlayerOptions); + // If the build failed, log an error, and don't continue + if (result.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded) + { + Debug.LogError("Build failed: " + result.summary.result); + return; + } + // Run deploy with CLI new System.Threading.Thread(() => { diff --git a/Assets/Rivet/Runtime/RivetManager.cs b/Assets/Rivet/Runtime/RivetManager.cs index b0bb327..4261912 100644 --- a/Assets/Rivet/Runtime/RivetManager.cs +++ b/Assets/Rivet/Runtime/RivetManager.cs @@ -8,6 +8,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; +using System.IO; [JsonConverter(typeof(StringEnumConverter))] public enum CreateLobbyRequestPublicity @@ -61,8 +62,8 @@ public struct RivetPlayer [JsonProperty("token")] public string Token; } -[CreateAssetMenu(fileName = "RivetSettings", menuName = "ScriptableObjects/RivetSettings", order = 1)] -public class RivetSettings : ScriptableObject +[System.Serializable] +public class RivetSettings { public string? RivetToken; public string? ApiEndpoint; @@ -70,12 +71,6 @@ public class RivetSettings : ScriptableObject public class RivetManager : MonoBehaviour { - [HideInInspector] - public string? RivetToken = null; - - [HideInInspector] - public string? ApiEndpoint = null; - [HideInInspector] public string? MatchmakerApiEndpoint => ApiEndpoint + "/matchmaker"; @@ -85,15 +80,61 @@ public class RivetManager : MonoBehaviour /// public FindLobbyResponse? FindLobbyResponse { get; private set; } - private void Start() + [HideInInspector] + public string? RivetToken => GetRivetToken(); + + [HideInInspector] + public string? ApiEndpoint => GetApiEndpoint(); + + private string? GetRivetToken() { - // Try to load Rivet runtime settings - var rivetSettings = Resources.Load("RivetSettings"); - if (rivetSettings != null) + string? token = PlayerPrefs.GetString("RivetToken"); + if (string.IsNullOrEmpty(token)) { - RivetToken = rivetSettings.RivetToken; - ApiEndpoint = rivetSettings.ApiEndpoint; + var rivetSettings = LoadRivetSettings(); + if (rivetSettings != null) + { + token = rivetSettings.RivetToken; + } } + return token; + } + + private string? GetApiEndpoint() + { + string? endpoint = PlayerPrefs.GetString("ApiEndpoint"); + if (string.IsNullOrEmpty(endpoint)) + { + var rivetSettings = LoadRivetSettings(); + if (rivetSettings != null) + { + endpoint = rivetSettings.ApiEndpoint; + } + } + return endpoint; + } + + private RivetSettings? LoadRivetSettings() + { + string filePath = Path.Combine(Application.streamingAssetsPath, "rivet_export.json"); + if (File.Exists(filePath)) + { + string json = File.ReadAllText(filePath); + RivetSettings rivetSettings = JsonUtility.FromJson(json); + return rivetSettings; + } + else + { + Debug.LogError("File not found: " + filePath); + return null; + } + } + + // Start function that debugs the Rivet token and API endpoint + private void Start() + { + Debug.Log("Rivet Token: " + RivetToken); + Debug.Log("API Endpoint: " + ApiEndpoint); } #region API: Matchmaker.Lobbies