Skip to content

Commit aeb2898

Browse files
committed
fix: Build with correct Rivet config file (#10)
Closes UTY-20
1 parent 252c32b commit aeb2898

4 files changed

Lines changed: 141 additions & 24 deletions

File tree

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
1-
using UnityEditor;
21
using UnityEditor.Build;
32
using UnityEditor.Build.Reporting;
43
using UnityEngine;
4+
using System.IO;
55

66
public class BuildScript : IPreprocessBuildWithReport, IPostprocessBuildWithReport
77
{
88
public int callbackOrder { get { return 0; } }
99

1010
public void OnPreprocessBuild(BuildReport report)
1111
{
12+
// Check if StreamingAssets folder exists and create it if it doesn't
13+
string streamingAssetsPath = Application.streamingAssetsPath;
14+
if (!Directory.Exists(streamingAssetsPath))
15+
{
16+
Directory.CreateDirectory(streamingAssetsPath);
17+
}
18+
19+
// If either is null, add a build error
20+
if (string.IsNullOrEmpty(ExtensionData.ApiEndpoint))
21+
{
22+
Debug.LogError("Rivet API endpoint is not set. Please set the API endpoint in the Rivet settings.");
23+
}
24+
25+
if (string.IsNullOrEmpty(ExtensionData.RivetToken))
26+
{
27+
Debug.LogError("Rivet token is not set. Please set the Rivet token in the Rivet settings.");
28+
}
29+
1230
// Create the asset file before the build
13-
RivetSettings data = ScriptableObject.CreateInstance<RivetSettings>();
14-
data.ApiEndpoint = ExtensionData.ApiEndpoint;
15-
data.RivetToken = ExtensionData.RivetToken;
16-
AssetDatabase.CreateAsset(data, "Assets/rivet_export.asset");
17-
AssetDatabase.SaveAssets();
31+
RivetSettings data = new RivetSettings
32+
{
33+
ApiEndpoint = ExtensionData.ApiEndpoint,
34+
RivetToken = ExtensionData.RivetToken
35+
};
36+
37+
string json = JsonUtility.ToJson(data);
38+
string filePath = Path.Combine(Application.streamingAssetsPath, "rivet_export.json");
39+
File.WriteAllText(filePath, json);
1840
}
1941

2042
public void OnPostprocessBuild(BuildReport report)
2143
{
2244
// Delete the asset file after the build
23-
AssetDatabase.DeleteAsset("Assets/rivet_export.asset");
45+
string filePath = Path.Combine(Application.streamingAssetsPath, "rivet_export.json");
46+
if (File.Exists(filePath))
47+
{
48+
File.Delete(filePath);
49+
}
2450
}
25-
}
51+
}

Assets/Rivet/Editor/RivetPlugin.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
11
using UnityEngine;
22
using UnityEditor;
33

4+
/// <summary>
5+
/// Provides extension data for the Rivet plugin.
6+
/// </summary>
47
public static class ExtensionData
58
{
6-
public static string RivetToken { get; set; }
9+
private static string rivetToken;
710
private static string apiEndpoint = "https://api.rivet.gg";
811

12+
/// <summary>
13+
/// Gets or sets the Rivet token.
14+
/// </summary>
15+
/// <remarks>
16+
/// The Rivet token is used for authentication with the Rivet API.
17+
/// When the token is set, it is also stored in the PlayerPrefs for persistence.
18+
/// </remarks>
19+
public static string RivetToken
20+
{
21+
get { return rivetToken; }
22+
set
23+
{
24+
rivetToken = value;
25+
// This might not be called from the main thread, so we need to
26+
// delay the call to PlayerPrefs
27+
UnityEditor.EditorApplication.delayCall += () =>
28+
{
29+
PlayerPrefs.SetString("RivetToken", value);
30+
};
31+
}
32+
}
33+
34+
/// <summary>
35+
/// Gets or sets the API endpoint.
36+
/// </summary>
37+
/// <remarks>
38+
/// The API endpoint is the base URL for the Rivet API.
39+
/// When the endpoint is set, it is also stored in the PlayerPrefs for persistence.
40+
/// </remarks>
941
public static string ApiEndpoint
1042
{
1143
get { return apiEndpoint; }
12-
set { apiEndpoint = value; }
44+
set
45+
{
46+
apiEndpoint = value;
47+
// This might not be called from the main thread, so we need to
48+
// delay the call to PlayerPrefs
49+
UnityEditor.EditorApplication.delayCall += () =>
50+
{
51+
PlayerPrefs.SetString("ApiEndpoint", value);
52+
};
53+
}
1354
}
1455
}
1556

Assets/Rivet/Editor/Windows/Plugin.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net;
88
using System.Collections.Generic;
99
using System.Security.Cryptography;
10+
using System.IO;
1011

1112
public struct BootstrapData
1213
{
@@ -95,6 +96,7 @@ public void OnEnter(RivetPluginWindow pluginWindow)
9596
new System.Threading.Thread(() =>
9697
{
9798
GetBootstrapData();
99+
GetNamespaceToken();
98100
}).Start();
99101
}
100102

@@ -281,6 +283,13 @@ public void OnGUI()
281283
// Build the player
282284
var result = BuildPipeline.BuildPlayer(buildPlayerOptions);
283285

286+
// If the build failed, log an error, and don't continue
287+
if (result.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded)
288+
{
289+
Debug.LogError("Build failed: " + result.summary.result);
290+
return;
291+
}
292+
284293
// Run deploy with CLI
285294
new System.Threading.Thread(() =>
286295
{

Assets/Rivet/Runtime/RivetManager.cs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Newtonsoft.Json;
99
using Newtonsoft.Json.Converters;
1010
using Newtonsoft.Json.Linq;
11+
using System.IO;
1112

1213
[JsonConverter(typeof(StringEnumConverter))]
1314
public enum CreateLobbyRequestPublicity
@@ -61,21 +62,15 @@ public struct RivetPlayer
6162
[JsonProperty("token")] public string Token;
6263
}
6364

64-
[CreateAssetMenu(fileName = "RivetSettings", menuName = "ScriptableObjects/RivetSettings", order = 1)]
65-
public class RivetSettings : ScriptableObject
65+
[System.Serializable]
66+
public class RivetSettings
6667
{
6768
public string? RivetToken;
6869
public string? ApiEndpoint;
6970
}
7071

7172
public class RivetManager : MonoBehaviour
7273
{
73-
[HideInInspector]
74-
public string? RivetToken = null;
75-
76-
[HideInInspector]
77-
public string? ApiEndpoint = null;
78-
7974
[HideInInspector]
8075
public string? MatchmakerApiEndpoint => ApiEndpoint + "/matchmaker";
8176

@@ -85,15 +80,61 @@ public class RivetManager : MonoBehaviour
8580
/// </summary>
8681
public FindLobbyResponse? FindLobbyResponse { get; private set; }
8782

88-
private void Start()
83+
[HideInInspector]
84+
public string? RivetToken => GetRivetToken();
85+
86+
[HideInInspector]
87+
public string? ApiEndpoint => GetApiEndpoint();
88+
89+
private string? GetRivetToken()
8990
{
90-
// Try to load Rivet runtime settings
91-
var rivetSettings = Resources.Load<RivetSettings>("RivetSettings");
92-
if (rivetSettings != null)
91+
string? token = PlayerPrefs.GetString("RivetToken");
92+
if (string.IsNullOrEmpty(token))
9393
{
94-
RivetToken = rivetSettings.RivetToken;
95-
ApiEndpoint = rivetSettings.ApiEndpoint;
94+
var rivetSettings = LoadRivetSettings();
95+
if (rivetSettings != null)
96+
{
97+
token = rivetSettings.RivetToken;
98+
}
9699
}
100+
return token;
101+
}
102+
103+
private string? GetApiEndpoint()
104+
{
105+
string? endpoint = PlayerPrefs.GetString("ApiEndpoint");
106+
if (string.IsNullOrEmpty(endpoint))
107+
{
108+
var rivetSettings = LoadRivetSettings();
109+
if (rivetSettings != null)
110+
{
111+
endpoint = rivetSettings.ApiEndpoint;
112+
}
113+
}
114+
return endpoint;
115+
}
116+
117+
private RivetSettings? LoadRivetSettings()
118+
{
119+
string filePath = Path.Combine(Application.streamingAssetsPath, "rivet_export.json");
120+
if (File.Exists(filePath))
121+
{
122+
string json = File.ReadAllText(filePath);
123+
RivetSettings rivetSettings = JsonUtility.FromJson<RivetSettings>(json);
124+
return rivetSettings;
125+
}
126+
else
127+
{
128+
Debug.LogError("File not found: " + filePath);
129+
return null;
130+
}
131+
}
132+
133+
// Start function that debugs the Rivet token and API endpoint
134+
private void Start()
135+
{
136+
Debug.Log("Rivet Token: " + RivetToken);
137+
Debug.Log("API Endpoint: " + ApiEndpoint);
97138
}
98139

99140
#region API: Matchmaker.Lobbies

0 commit comments

Comments
 (0)