-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
72 lines (59 loc) · 1.52 KB
/
main.rb
File metadata and controls
72 lines (59 loc) · 1.52 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
67
68
69
70
71
72
# This code is MIT licensed, so enjoy!
#
# Author: Joseph Austin
require 'yaml'
# Open a yaml file if it exists, otherwise create it with blank contents
def open_create_yaml(file)
file_structure = nil
begin
file_structure = YAML.load_file file
rescue
File.open(file, 'w') {|f| f.write '{}' }
file_structure = YAML.load_file file
end
file_structure
end
# This handles the 'global data' such as largest max id number
$global_mutex = Mutex.new
$max_id = 0
begin
file_structure = open_create_yaml "Data/global.data"
if file_structure[:max_id].nil?
file_structure[:max_id] = 0
else
$max_id = file_structure[:max_id]
end
end
require "./Node.rb"
require "./Component.rb"
# Here is a test component with its very own field
class Test < Component
NAME = :test
def initialize(data = nil)
super NAME, {
test_field: ""
}, data
end
def setTestField(strng)
_set :test_field, strng
end
end
# Make a node
n = Node.new "TestNode"
n.description = "This is a test"
n.name = "test"
# Make a component
n.addComponent Test.new
c = n.getComponent Test # Proof you can get components back without referencing
# Use the component's own functionality
c.setTestField "Victory is ours"
# Proof of result
Concept.dump
# Now save to binary
Concept.save
# Also save the globals - next time it all just exists
$global_mutex.synchronize do
file_structure = open_create_yaml "Data/global.data"
file_structure[:max_id] = $max_id
File.open("Data/global.data", 'w') {|f| f.write file_structure.to_yaml}
end