-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
convar.lua
187 lines (152 loc) · 3.65 KB
/
convar.lua
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Convar class
--
--==========================================================================--
require( "class" )
class( "convar" )
convar._config = convar._config or {}
convar._convars = convar._convars or {}
function convar.getConfig( name )
return convar._config[ name ]
end
function convar.getConvar( name )
return convar._convars[ name ]
end
function convar.setConvar( name, value )
local convar = convar.getConvar( name )
if ( convar ) then
convar:setValue( value )
return true
else
return false
end
end
function convar.readConfig()
if ( love.filesystem.getInfo( "cfg/config.cfg" ) == nil ) then
return
end
for line in love.filesystem.lines( "cfg/config.cfg" ) do
for k, v in string.gmatch( line, "(.+)%s(.+)" ) do
convar._config[ k ] = v
end
end
end
function convar.saveConfig()
local config = {}
for k, v in pairs( convar._convars ) do
if ( v:isFlagSet( "archive" ) ) then
table.insert( config, k .. " " .. tostring( v:getValue() ) )
end
end
table.sort( config )
table.insert( config, "" )
config = table.concat( config, "\r\n" )
love.filesystem.createDirectory( "cfg" )
if ( love.filesystem.write( "cfg/config.cfg", config ) ) then
print( "Saved configuration." )
else
print( "Failed to save configuration!" )
end
end
function convar:convar(
name,
default,
min,
max,
helpString,
onValueChange,
flags
)
local value = convar._config[ name ] or default
if ( convar._convars[ name ] ) then
value = convar._convars[ name ]:getValue()
end
self.name = name
self.default = default
self.value = value
self.min = min
self.max = max
self.helpString = helpString
self.onValueChange = onValueChange
self.flags = flags
convar._convars[ name ] = self
end
function convar:getBoolean()
local n = self:getNumber()
return n ~= nil and n ~= 0
end
function convar:getDefault()
return self.default
end
function convar:getFlags()
return self.flags
end
function convar:getHelpString()
return self.helpString
end
function convar:getMin()
return self.min
end
function convar:getMax()
return self.max
end
function convar:getName()
return self.name
end
function convar:getNumber()
return tonumber( self.value )
end
function convar:getValue()
return self.value
end
function convar:isFlagSet( flag )
local flags = self:getFlags()
if ( flags ) then
return table.hasvalue( flags, flag ) ~= nil
end
return false
end
function convar:onValueChange( oldValue, newValue )
end
function convar:remove()
convar._convars[ self:getName() ] = nil
end
function convar:setDefault( default )
self.default = default
end
function convar:setFlags( flags )
self.flags = flags
end
function convar:setMin( min )
self.min = min
end
function convar:setMax( max )
self.max = max
end
function convar:setHelpString( helpString )
self.helpString = helpString
end
function convar:setValue( value )
local oldValue = self.value
local numValue = tonumber( value )
local isNumber = type( value ) == "number" or numValue
local min, max = self:getMin(), self:getMax()
if ( isNumber and min and max ) then
self.value = math.clamp( numValue, min, max )
else
self.value = value
end
self:onValueChange( oldValue, self.value )
if ( _SERVER ) then
local shouldNotify = self:isFlagSet( "notify" )
if ( shouldNotify ) then
local name = self:getName()
local text = "Server cvar " .. name .. " changed to " .. self.value
player.sendTextAll( text )
end
end
end
function convar:__tostring()
return "convar: " .. self.name .. " = \"" .. self.value .. "\""
end