Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions Assets/Rivet/Editor/BuildPipeline.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;

public class BuildScript : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }

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<RivetSettings>();
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);
}
}
}
}
45 changes: 43 additions & 2 deletions Assets/Rivet/Editor/RivetPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,56 @@
using UnityEngine;
using UnityEditor;

/// <summary>
/// Provides extension data for the Rivet plugin.
/// </summary>
public static class ExtensionData
{
public static string RivetToken { get; set; }
private static string rivetToken;
private static string apiEndpoint = "https://api.rivet.gg";

/// <summary>
/// Gets or sets the Rivet token.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
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);
};
}
}

/// <summary>
/// Gets or sets the API endpoint.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
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);
};
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions Assets/Rivet/Editor/Windows/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.IO;

public struct BootstrapData
{
Expand Down Expand Up @@ -95,6 +96,7 @@ public void OnEnter(RivetPluginWindow pluginWindow)
new System.Threading.Thread(() =>
{
GetBootstrapData();
GetNamespaceToken();
}).Start();
}

Expand Down Expand Up @@ -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(() =>
{
Expand Down
69 changes: 55 additions & 14 deletions Assets/Rivet/Runtime/RivetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System.IO;

[JsonConverter(typeof(StringEnumConverter))]
public enum CreateLobbyRequestPublicity
Expand Down Expand Up @@ -61,21 +62,15 @@ 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;
}

public class RivetManager : MonoBehaviour
{
[HideInInspector]
public string? RivetToken = null;

[HideInInspector]
public string? ApiEndpoint = null;

[HideInInspector]
public string? MatchmakerApiEndpoint => ApiEndpoint + "/matchmaker";

Expand All @@ -85,15 +80,61 @@ public class RivetManager : MonoBehaviour
/// </summary>
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>("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<RivetSettings>(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
Expand Down