Skip to content

Commit 28bed64

Browse files
NathanFlurryAngelOnFira
authored andcommitted
chore: switch to toolchain (#35)
1 parent 1a7f883 commit 28bed64

8 files changed

Lines changed: 228 additions & 70 deletions

File tree

Assets/Rivet/.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
Editor/PluginSettings.asset*
1+
# Don't save default settings
2+
Editor/PluginSettings.asset*
3+
4+
# Don't save dylibs
5+
Editor/Native/*.dll
6+
Editor/Native/*.dylib
7+
Editor/Native/*.so

Assets/Rivet/Editor/Native.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Rivet/Editor/Native/librivet_toolchain_ffi.dylib.meta

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// namespace Rivet.Editor
2+
// {
3+
// public static class RivetToolchain
4+
// {
5+
// public const string NativeLib = "rivet_toolchain";
6+
7+
// [DIlImport(NativeLib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "run_task")]
8+
// public static extern uint run_task();
9+
// }
10+
// }
11+
12+
using System;
13+
using System.Runtime.InteropServices;
14+
15+
namespace Rivet.Editor
16+
{
17+
public class RivetToolchain
18+
{
19+
const string RustLibrary = "__Internal";
20+
21+
22+
[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "run_task")]
23+
private static extern IntPtr run_task(
24+
[MarshalAs(UnmanagedType.LPStr)] string run_config,
25+
[MarshalAs(UnmanagedType.LPStr)] string name,
26+
[MarshalAs(UnmanagedType.LPStr)] string input_json
27+
);
28+
29+
[DllImport(RustLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "free_rust_string")]
30+
private static extern void free_rust_string(IntPtr str);
31+
32+
public static string RunTaskRaw(string runConfig, string name, string inputJson)
33+
{
34+
IntPtr resultPtr = run_task(runConfig, name, inputJson);
35+
string result = Marshal.PtrToStringAnsi(resultPtr);
36+
free_rust_string(resultPtr);
37+
return result;
38+
}
39+
}
40+
}

Assets/Rivet/Editor/RivetToolchain.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Rivet/Editor/Task.cs

Lines changed: 95 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -100,85 +100,114 @@ private async Task StartLogPolling(CancellationToken cancellationToken)
100100

101101
private Result<JObject> RunTask(string name, JObject input, JObject runConfig)
102102
{
103-
var output = RunRivetCLI("task", "run", "--run-config", runConfig.ToString(Formatting.None), "--name", name, "--input", input.ToString(Formatting.None));
104-
switch (output)
105-
{
106-
case ResultOk<JObject> ok:
107-
if (ok.Data.GetValue("Ok")?.Value<JObject>() is { } okInner)
108-
{
109-
RivetLogger.Log($"Task {name} Success: {okInner.ToString(Formatting.None)}");
110-
return new ResultOk<JObject>(okInner);
111-
}
112-
else if (ok.Data.GetValue("Err") is { } errInner)
113-
{
114-
RivetLogger.Log($"Task {name} Err: {errInner}");
115-
return new ResultErr<JObject>(errInner.ToString(Formatting.None));
116-
}
117-
else
118-
{
119-
throw new Exception("unreachable");
120-
}
121-
case ResultErr<JObject> err:
122-
RivetLogger.Log($"Task {name} Command Err: {err.Message}");
123-
return err;
124-
default:
125-
throw new Exception("unreachable");
126-
}
127-
}
128-
129-
public static string GetRivetCLIPath()
130-
{
131-
// TODO: Update this path as needed
132-
return "/Users/nathan/rivet/cli/target/debug/rivet-cli";
133-
}
134-
135-
private static Result<JObject> RunRivetCLI(params string[] args)
136-
{
137-
// TODO: Turn this on if debug is enabled
138-
RivetLogger.Log($"Running Rivet CLI: {GetRivetCLIPath()} {string.Join(" ", args)}");
139-
140-
if (!File.Exists(GetRivetCLIPath()))
141-
{
142-
return new ResultErr<JObject>("File does not exist: " + GetRivetCLIPath());
143-
}
144-
145-
var startInfo = new ProcessStartInfo
146-
{
147-
FileName = GetRivetCLIPath(),
148-
RedirectStandardOutput = true,
149-
RedirectStandardError = true,
150-
UseShellExecute = false,
151-
CreateNoWindow = true,
152-
};
153-
foreach (var arg in args) {
154-
startInfo.ArgumentList.Add(arg);
155-
}
156-
157103
try
158104
{
159-
using var process = Process.Start(startInfo);
160-
var stdout = process.StandardOutput.ReadToEnd();
161-
var stderr = process.StandardError.ReadToEnd();
162-
process.WaitForExit();
163-
164-
RivetLogger.Log($"Process output:\n\n{stdout}\n\n{stderr}");
165-
166-
if (process.ExitCode == 0)
105+
var outputRaw = RivetToolchain.RunTaskRaw(runConfig.ToString(Formatting.None), name, input.ToString(Formatting.None));
106+
var output = JObject.Parse(outputRaw);
107+
if (output.GetValue("Ok")?.Value<JObject>() is { } okInner)
167108
{
168-
return new ResultOk<JObject>(JObject.Parse(stdout));
109+
RivetLogger.Log($"Task {name} Success: {okInner.ToString(Formatting.None)}");
110+
return new ResultOk<JObject>(okInner);
111+
}
112+
else if (output.GetValue("Err") is { } errInner)
113+
{
114+
RivetLogger.Log($"Task {name} Err: {errInner}");
115+
return new ResultErr<JObject>(errInner.ToString(Formatting.None));
169116
}
170117
else
171118
{
172-
return new ResultErr<JObject>($"Process failed with exit code {process.ExitCode}\n\nstdout:\n{stdout}\n\nstderr:\n{stderr}");
119+
throw new Exception("unreachable");
173120
}
174121
}
175-
catch (System.Exception ex)
122+
catch (Exception error)
176123
{
177-
return new ResultErr<JObject>("Failed to start process: " + ex.Message);
124+
UnityEngine.Debug.LogError($"Error running task: {error}");
125+
return new ResultErr<JObject>(error.ToString());
178126
}
127+
}
179128

129+
// private Result<JObject> RunTask(string name, JObject input, JObject runConfig)
130+
// {
131+
// var output = RunRivetCLI("task", "run", "--run-config", runConfig.ToString(Formatting.None), "--name", name, "--input", input.ToString(Formatting.None));
132+
// switch (output)
133+
// {
134+
// case ResultOk<JObject> ok:
135+
// if (ok.Data.GetValue("Ok")?.Value<JObject>() is { } okInner)
136+
// {
137+
// RivetLogger.Log($"Task {name} Success: {okInner.ToString(Formatting.None)}");
138+
// return new ResultOk<JObject>(okInner);
139+
// }
140+
// else if (ok.Data.GetValue("Err") is { } errInner)
141+
// {
142+
// RivetLogger.Log($"Task {name} Err: {errInner}");
143+
// return new ResultErr<JObject>(errInner.ToString(Formatting.None));
144+
// }
145+
// else
146+
// {
147+
// throw new Exception("unreachable");
148+
// }
149+
// case ResultErr<JObject> err:
150+
// RivetLogger.Log($"Task {name} Command Err: {err.Message}");
151+
// return err;
152+
// default:
153+
// throw new Exception("unreachable");
154+
// }
155+
// }
156+
157+
// TODO: Remove this
158+
public static string GetRivetCLIPath()
159+
{
160+
// TODO: Update this path as needed
161+
return "/Users/nathan/rivet/cli/target/debug/rivet-cli";
180162
}
181163

164+
// private static Result<JObject> RunRivetCLI(params string[] args)
165+
// {
166+
// // TODO: Turn this on if debug is enabled
167+
// RivetLogger.Log($"Running Rivet CLI: {GetRivetCLIPath()} {string.Join(" ", args)}");
168+
169+
// if (!File.Exists(GetRivetCLIPath()))
170+
// {
171+
// return new ResultErr<JObject>("File does not exist: " + GetRivetCLIPath());
172+
// }
173+
174+
// var startInfo = new ProcessStartInfo
175+
// {
176+
// FileName = GetRivetCLIPath(),
177+
// RedirectStandardOutput = true,
178+
// RedirectStandardError = true,
179+
// UseShellExecute = false,
180+
// CreateNoWindow = true,
181+
// };
182+
// foreach (var arg in args) {
183+
// startInfo.ArgumentList.Add(arg);
184+
// }
185+
186+
// try
187+
// {
188+
// using var process = Process.Start(startInfo);
189+
// var stdout = process.StandardOutput.ReadToEnd();
190+
// var stderr = process.StandardError.ReadToEnd();
191+
// process.WaitForExit();
192+
193+
// RivetLogger.Log($"Process output:\n\n{stdout}\n\n{stderr}");
194+
195+
// if (process.ExitCode == 0)
196+
// {
197+
// return new ResultOk<JObject>(JObject.Parse(stdout));
198+
// }
199+
// else
200+
// {
201+
// return new ResultErr<JObject>($"Process failed with exit code {process.ExitCode}\n\nstdout:\n{stdout}\n\nstderr:\n{stderr}");
202+
// }
203+
// }
204+
// catch (System.Exception ex)
205+
// {
206+
// return new ResultErr<JObject>("Failed to start process: " + ex.Message);
207+
// }
208+
209+
// }
210+
182211
public void Kill()
183212
{
184213
if (!IsRunning) return;

Assets/Rivet/Editor/UI/Tabs/Develop.uxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<AttributeOverrides element-name="Label" text="Restart" />
2828
</ui:Instance>
2929
</ui:VisualElement>
30-
<ui:Label tabindex="-1" text="Show Logs" parse-escape-sequences="true" display-tooltip-when-elided="true" name="LogsButton" />
30+
<ui:Label tabindex="-1" text="&lt;u&gt;Show Logs&lt;/u&gt;" parse-escape-sequences="true" display-tooltip-when-elided="true" name="LogsButton" style="-unity-text-align: upper-center;" />
3131
</ui:VisualElement>
3232
<ui:Instance template="Separator" name="Separator" />
3333
<ui:Instance template="Header" name="BackendHeader">

Assets/Rivet/Editor/UI/Tabs/Settings.uxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<ui:Button text="Edit User" parse-escape-sequences="true" display-tooltip-when-elided="true" name="EditUserButton" style="flex-shrink: 1; flex-grow: 1;" />
1717
<ui:Button text="Edit Project" parse-escape-sequences="true" display-tooltip-when-elided="true" name="EditProjectButton" style="flex-shrink: 1; flex-grow: 1;" />
1818
</ui:VisualElement>
19-
<ui:Label tabindex="-1" text="Documentation" parse-escape-sequences="true" display-tooltip-when-elided="true" style="-unity-text-align: upper-center;" />
19+
<ui:Label tabindex="-1" text="&lt;u&gt;Documentation&lt;/u&gt;" parse-escape-sequences="true" display-tooltip-when-elided="true" style="-unity-text-align: upper-center;" />
2020
</ui:VisualElement>
2121
<ui:Instance template="Separator" name="Separator" />
2222
<ui:Instance template="Header" name="BackendHeader">
@@ -29,6 +29,6 @@
2929
<ui:Button text="Stop" parse-escape-sequences="true" display-tooltip-when-elided="true" name="StopButton" style="flex-shrink: 1; flex-grow: 1;" />
3030
<ui:Button text="Restart" parse-escape-sequences="true" display-tooltip-when-elided="true" name="RestartButton" style="flex-shrink: 1; flex-grow: 1;" />
3131
</ui:VisualElement>
32-
<ui:Label tabindex="-1" text="Show Logs" parse-escape-sequences="true" display-tooltip-when-elided="true" style="-unity-text-align: upper-center;" />
32+
<ui:Label tabindex="-1" text="&lt;u&gt;Show Logs&lt;/u&gt;" parse-escape-sequences="true" display-tooltip-when-elided="true" style="-unity-text-align: upper-center;" />
3333
</ui:VisualElement>
3434
</ui:UXML>

0 commit comments

Comments
 (0)