Skip to content
Closed
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

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

14 changes: 4 additions & 10 deletions Assets/Rivet/Editor/PluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,12 @@ private static void SetAndSave(ref string field, string value, string prefKey)
/// </summary>
public static void UpdateFromPlugin()
{
if (Dock.Singleton is { } plugin)
if (RivetGlobal.Singleton is { } plugin)
{
BackendEndpoint = plugin.EnvironmentType switch
{
EnvironmentType.Local => $"http://localhost:{plugin.LocalBackendPort}",
EnvironmentType.Remote => plugin.RemoteEnvironmentBackend?.Endpoint ?? "http://localhost:6420",
_ => throw new System.NotImplementedException(),
};

GameVersion = plugin.GameVersion ?? "unknown";
BackendEndpoint = plugin.BackendEndpoint;
GameVersion = plugin.CurrentBuildSlug;

RivetLogger.Log($"Update Shared Settings [BackendEndpoint={BackendEndpoint} GameVersion={GameVersion}]");
RivetLogger.Log($"Update Shared Settings: BackendEndpoint={BackendEndpoint} GameVersion={GameVersion}");
}
}
}
Expand Down
200 changes: 200 additions & 0 deletions Assets/Rivet/Editor/RivetGlobal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using UnityEditor.Build;
using System.Threading.Tasks;
using UnityEditor.Build.Reporting;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Rivet.Editor.Types;
using Rivet.Editor.UI.Dock;

namespace Rivet.Editor
{
public enum EnvironmentType
{
Local = 0, Remote = 1,
}

public class RivetGlobal
{
public static RivetGlobal? Singleton;

// MARK: Bootstrap
// Null if not bootstrapped yet
public BootstrapData? BootstrapData;

// Null if not authenticated
public CloudData? CloudData
{
get
{
return BootstrapData?.Cloud;
}
}

// If the user has the credentials required to connect to Rivet Cloud.
public bool IsAuthenticated
{
get
{
return CloudData != null;
}
}

// MARK: Environment
public EnvironmentType EnvironmentType
{
get { return PluginSettings.EnvironmentType; }
set
{
PluginSettings.EnvironmentType = value;
SharedSettings.UpdateFromPlugin();
}
}
public string? RemoteEnvironmentId
{
get { return PluginSettings.RemoteEnvironmentId; }
set
{
PluginSettings.RemoteEnvironmentId = value;
SharedSettings.UpdateFromPlugin();
}
}
public RivetEnvironment? RemoteEnvironment
{
get
{
return RemoteEnvironmentIndex != null ? CloudData?.Environments[RemoteEnvironmentIndex.Value] : null;
}
}
public int? RemoteEnvironmentIndex
{
get
{
if (CloudData is { } data)
{
var idx = data.Environments.FindIndex(x => x.Id == RemoteEnvironmentId);
return idx >= 0 ? idx : 0;
}
else
{
return 0;
}
}
set
{
if (value != null && value >= 0 && value < CloudData?.Environments.Count)
{
RemoteEnvironmentId = CloudData?.Environments[value.Value].Id;
}
}
}

// MARK: Port
public int LocalBackendPort = 6420;

public string LocalBackendEndpoint
{
get
{
return $"http://127.0.0.1:{LocalBackendPort}";
}
}

public int LocalEditorPort = 6421;

public string LocalEditorEndpoint
{
get
{
return $"http://127.0.0.1:{LocalEditorPort}";
}
}

// Endpoint to connect to
public string BackendEndpoint
{
get
{
switch (EnvironmentType)
{
case EnvironmentType.Local:
return LocalBackendEndpoint;
case EnvironmentType.Remote:
if (CloudData is { } cloudData && RemoteEnvironmentId is { } remoteEnvId)
{
return cloudData.Backends[remoteEnvId].Endpoint;
}
else
{
RivetLogger.Error("BackendEndpoint: unreachable");
return "";
}
default:
RivetLogger.Error("BackendEndpoint: unreachable");
return "";
}
}
}

// If the Rivet SDK has been generated.
public bool BackendSdkExists = false;

// The current deployed build slug.
public string CurrentBuildSlug
{
get
{
switch (EnvironmentType)
{
case EnvironmentType.Local:
return "local";
case EnvironmentType.Remote:
if (CloudData is { } cloudData && RemoteEnvironmentId is { } remoteEnvId)
{
if (cloudData.CurrentBuilds.TryGetValue(remoteEnvId, out var build) && build.Tags.TryGetValue("version", out var versionTag))
{
return versionTag;
}
else
{
RivetLogger.Error("CurrentBuildSlug: no current build or version in build");
return "";
}
}
else
{
RivetLogger.Error("CurrentBuildSlug: not authenticated");
return "";
}
default:
RivetLogger.Error("BackendEndpoint: unreachable");
return "";
}
}
}

// MARK: Local Game Server
public string? LocalGameServerExecutablePath;

// MARK: Bootstrap
public async Task Bootstrap()
{
var result = await new RivetTask("get_bootstrap_data", new JObject()).RunAsync();
if (result is ResultErr<JObject> err)
{
return;
}

// Save data
var data = result.Data.ToObject<BootstrapData>(); ;
BootstrapData = data;

// Update configuration
SharedSettings.UpdateFromPlugin();

// Emit event
Dock.Singleton?.OnBootstrap();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Rivet/Editor/RivetGlobal.cs.meta

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

8 changes: 4 additions & 4 deletions Assets/Rivet/Editor/RivetToolchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ public static class RivetToolchain
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void EventCallback(ulong taskId, IntPtr eventJson);

[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "run_task")]
[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "rivet_run_task")]
private static extern ulong run_task(
[MarshalAs(UnmanagedType.LPStr)] string name,
[MarshalAs(UnmanagedType.LPStr)] string inputJson,
EventCallback callback
);

[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "abort_task")]
[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "rivet_abort_task")]
private static extern bool abort_task(ulong taskId);

[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "shutdown")]
[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "rivet_shutdown")]
private static extern void shutdown();

[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "free_rust_string")]
[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "rivet_free_rust_string")]
private static extern void free_rust_string(IntPtr str);

public static ulong RunTask(string name, string inputJson, EventCallback callback)
Expand Down
17 changes: 15 additions & 2 deletions Assets/Rivet/Editor/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public RivetTask(string name, JObject input)

private void Run(string name, string inputJson)
{
RivetLogger.Log($"starting run");
_taskId = RivetToolchain.RunTask(name, inputJson, OnOutputEvent);
RivetLogger.Log($"run finished");
RivetLogger.Log($"running {_taskId}");
}

private void OnOutputEvent(ulong taskId, IntPtr eventJsonPtr)
Expand All @@ -56,14 +59,19 @@ private void OnOutputEvent(ulong taskId, IntPtr eventJsonPtr)

private void HandleOnOutputEvent(string eventJson)
{
RivetLogger.Log($"got event {eventJson}");

var eventObj = JObject.Parse(eventJson);
if (eventObj.ContainsKey("log"))
{
OnLogEvent(eventObj);
}
else if (eventObj.ContainsKey("result"))
{
_logResult = eventObj["result"] as JObject;
RivetLogger.Log("got result");
// 1st result = event enum type
// 2nd result = result enum type
_logResult = eventObj["result"]["result"] as JObject;
OnFinish();
}
else if (eventObj.ContainsKey("port_update"))
Expand Down Expand Up @@ -113,16 +121,21 @@ private void OnFinish()
outputResult = _logResult;
}

RivetLogger.Log($"[{_name}] Response: {outputResult.ToString(Newtonsoft.Json.Formatting.None)}");;

TaskOutput?.Invoke(outputResult);
if (outputResult.ContainsKey("Ok"))
{
RivetLogger.Log($"[{_name}] Success: {outputResult["Ok"]}");
TaskOk?.Invoke(outputResult["Ok"] as JObject);
}
else
else if (outputResult.ContainsKey("Err"))
{
RivetLogger.Error($"[{_name}] Error: {outputResult["Err"]}");
TaskError?.Invoke(outputResult["Err"].ToString());
} else {
RivetLogger.Error($"[{_name}] Missing Err or Ok in result: {outputResult.ToString(Newtonsoft.Json.Formatting.None)}");

}
}

Expand Down
22 changes: 17 additions & 5 deletions Assets/Rivet/Editor/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@
namespace Rivet.Editor.Types
{
public struct BootstrapData
{
[JsonProperty("cloud")] public CloudData? Cloud;
}

public struct CloudData
{
[JsonProperty("token")] public string Token;
[JsonProperty("api_endpoint")] public string ApiEndpoint;
[JsonProperty("game_id")] public string GameId;
[JsonProperty("envs")] public List<RivetEnvironment> Environments;
[JsonProperty("backends")] public Dictionary<string, EnvironmentBackend> Backends;
[JsonProperty("current_builds")] public Dictionary<string, ServersBuild> CurrentBuilds;
}

public struct RivetEnvironment
{
[JsonProperty("id")] public string Id;
[JsonProperty("created_at")] public string CreatedAt;
[JsonProperty("slug")] public string Slug;
[JsonProperty("name")] public string Name;
}

public struct EnvironmentBackend
Expand All @@ -21,12 +35,10 @@ public struct EnvironmentBackend
[JsonProperty("tier")] public string Tier;
}

public struct RivetEnvironment
public struct ServersBuild
{
[JsonProperty("id")] public string Id;
[JsonProperty("created_at")] public string CreatedAt;
[JsonProperty("slug")] public string Slug;
[JsonProperty("id")] public string id;
[JsonProperty("name")] public string Name;
[JsonProperty("tags")] public Dictionary<string, string> Tags;
}

}
Loading