-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsettings.rb
More file actions
103 lines (103 loc) · 3.16 KB
/
libsettings.rb
File metadata and controls
103 lines (103 loc) · 3.16 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class Settings
@@hash ||= {}
@@auto ||= false
@@stamp ||= Hash.new
def Settings.auto=(val)
@@auto = val
end
def Settings.auto
@@auto
end
def Settings.save
if script = Script.self
if File.exists?($script_dir + script.to_s + ".sav")
File.rename($script_dir + script.to_s + ".sav",
$data_dir + script.to_s + ".sav")
end
file = File.open($data_dir + script.to_s + '.sav', 'wb')
file.write(Marshal.dump(if @@hash[script.to_s] then @@hash[script.to_s] else {} end))
file.close
else
raise Exception.exception("SettingsError"), "The script trying to save its data cannot be identified!"
end
end
def Settings.autoload
if File.exists?($script_dir + Script.self.to_s + ".sav")
File.rename($script_dir + Script.self.to_s + ".sav",
$data_dir + Script.self.to_s + ".sav")
end
fname = $data_dir + Script.self.to_s + '.sav'
if File.mtime(fname) > @@stamp[Script.self.to_s]
Settings.load
true
else
false
end
end
def Settings.load(who = nil)
@@stamp[Script.self.to_s] = Time.now
if !who.nil?
unless who.include?(".")
who += ".sav"
end
begin
if File.exists?($script_dir + who)
File.rename($script_dir + who, $data_dir + who)
end
file = File.open($data_dir + who, 'rb')
@@hash[who.sub(/\..*/, '')] = Marshal.load(file.read)
rescue
$stderr.puts $!
$stderr.puts $!.backtrace
ensure
file.close unless file.closed?
end
return
end
if script = Script.self
if File.exists?($script_dir + script.to_s + '.sav')
File.rename($script_dir + script.to_s + ".sav",
$data_dir + script.to_s + ".sav")
end
if File.exists?($data_dir + script.to_s + ".sav")
begin
file = File.open($data_dir + script.to_s + '.sav', 'rb')
data = Marshal.load(file.read)
file.close
@@hash[script.to_s] = data
rescue
puts $!
ensure
file.close unless file.closed?
end
else
nil
end
else
raise Exception.exception("SettingsError"), "The script trying to load data cannot be identified!"
end
end
def Settings.clear
unless script = Script.self then raise Exception.exception("SettingsError"), "The script trying to access settings cannot be identified!" end
unless @@hash[script.to_s] then @@hash[script.to_s] = {} end
@@hash[script.to_s].clear
end
def Settings.[](val)
Settings.autoload if @@auto
unless script = Script.self then raise Exception.exception("SettingsError"), "The script trying to access settings cannot be identified!" end
unless @@hash[script.to_s] then @@hash[script.to_s] = {} end
@@hash[script.to_s][val]
end
def Settings.[]=(setting, val)
unless script = Script.self then raise Exception.exception("SettingsError"), "The script trying to access settings cannot be identified!" end
unless @@hash[script.to_s] then @@hash[script.to_s] = {} end
@@hash[script.to_s][setting] = val
Settings.save if @@auto
@@hash[script.to_s][setting]
end
def Settings.to_hash
unless script = Script.self then raise Exception.exception("SettingsError"), "The script trying to access settings cannot be identified!" end
unless @@hash[script.to_s] then @@hash[script.to_s] = {} end
@@hash[script.to_s]
end
end