-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby_generic.odin
63 lines (46 loc) · 1.44 KB
/
ruby_generic.odin
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
package main
import "core:fmt"
import "core:os"
import "core:path/filepath"
import "core:strings"
import mrb "./mruby"
import rl "vendor:raylib"
setup_require :: proc(state: ^mrb.State) {
krn := mrb.state_get_kernel_module(state)
mrb.define_method(state, krn, "require", require_fn, mrb.args_req(1))
}
find_ruby_file :: proc(as: ^AssetSystem, path: string) -> (string, bool) {
rel_path, err := filepath.rel("assets/", path, context.temp_allocator)
assert(err == .None)
ruby_path := strings.concatenate({rel_path, ".rb"}, context.temp_allocator)
full_path := filepath.join({as.asset_dir, ruby_path}, context.temp_allocator)
if !os.exists(full_path) {
return string{}, false
}
return full_path, true
}
require_fn :: proc "c" (state: ^mrb.State, self: mrb.Value) -> mrb.Value {
context = load_context(state)
filename: mrb.Value
mrb.get_args(state, "o", &filename)
path, success := mrb.string_from_value(state, filename, context.temp_allocator)
if !success {
return mrb.false_value()
}
as := &g.assets
if !os.exists(as.asset_dir) {
mrb.raise_exception(state, "%s is not a valid asset directory", as.asset_dir)
}
rbf, rbf_found := find_ruby_file(as, path)
if rbf_found {
handle, rbf_loaded := as_load_ruby(as, rbf)
if !success {
rl.TraceLog(.WARNING, "Failed to Load Ruby Script: %s", rbf)
return mrb.false_value()
}
if (as_should_rerun_ruby(as, handle)) {
game_run_code(g, handle)
}
}
return mrb.true_value()
}