-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSampleBehaviour.cs
More file actions
66 lines (57 loc) · 2.17 KB
/
SampleBehaviour.cs
File metadata and controls
66 lines (57 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.IO;
using System.Threading.Tasks;
using ChibiRuby;
using ChibiRuby.Compiler;
using ChibiRuby.Debugger.Dap;
using UnityEngine;
public class SampleBehaviour : MonoBehaviour
{
void Start()
{
var mrb = MRubyState.Create(x =>
{
x.UseFiberScheduler();
});
mrb.DefineMethod(mrb.ObjectClass, mrb.Intern("log"), (x, self) =>
{
var message = x.GetArgumentAsStringAt(0);
Debug.Log(message.ToString());
return MRubyValue.Nil;
});
var compiler = MRubyCompiler.Create(mrb);
var dapServer = new MRubyDapServer(mrb, compiler, log: (logLevel, message, ex) =>
{
var line = ex is null ? message : $"{message}\n{ex}";
switch (logLevel)
{
case LogLevel.Warning:
Debug.LogWarning(line);
break;
case LogLevel.Error or LogLevel.Critical:
Debug.LogError(line);
break;
default:
Debug.Log(line);
break;
}
});
Task.Run(async () =>
{
await dapServer.StartAsync(destroyCancellationToken);
});
destroyCancellationToken.Register(dapServer.Dispose);
// Pass the absolute disk path of the .rb file so the bytecode's DBG section
// records a real filename — otherwise mruby's compiler defaults to "-e" (the
// `mruby -e` CLI mode marker), which the DAP stackTrace response then surfaces
// to VSCode as `Source.path = "-e"`, and VSCode tries to open a file called "-e".
// In a real game build (no asset on disk) you'd want a synthetic but stable
// path like "ruby/sample.rb" instead.
var scriptPath = $"{Application.dataPath}/ruby/sample.rb";
using var compilation = compiler.CompileFile(scriptPath);
var fiber = mrb.ParseBytecodeAsFiber(compilation.AsBytecode());
fiber.Resume(Array.Empty<MRubyValue>());
// fiber.WaitForTerminateAsync(destroyCancellationToken);
destroyCancellationToken.Register(mrb.Dispose);
}
}