-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbivalues.lua
More file actions
747 lines (632 loc) · 17.5 KB
/
bivalues.lua
File metadata and controls
747 lines (632 loc) · 17.5 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
--[[
The MIT License (MIT)
Copyright (c) 2014 Antton Hytönen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
--]]
local VERSION = "022";
if _G["BiValuesV" .. VERSION] then
return;
end
local SyncNetID = "BiValuesSync" .. VERSION;
local CallNetID = "BiValuesCall" .. VERSION;
---
-- BiValues table definition
---
local BV = {};
BV.BindTypes = {};
BV.Containers = {};
BV.__index = function(table, key)
if type(key) == "string" and string.sub(key, 1, 1) == "_" then
local val = rawget(table, key);
if not val then
val = getmetatable(table)[key];
end
return val;
else
local val = rawget(table._NewValues, key);
if not val then
val = rawget(table._Values, key);
end
if not val and table._RequireValue then
error(key .. " does not exist");
end
return val;
end
end
BV.__newindex = function(table, key, value)
if type(key) == "string" and string.sub(key, 1, 1) == "_" then
rawset(table, key, value);
else
table._NewValues[key] = value;
if table._AutoApply then
table:_Apply();
end
end
end
function BV.New(players, id, settings, defaultValues)
-- On clientside, we don't need the players parameter, so we shift the parameters.
if CLIENT then
defaultValues = settings;
settings = id;
id = players;
players = nil;
end
-- Check parameters
if type(players) == "Player" then
players = {players};
end
if settings.IsPrivate and not game.SinglePlayer() and ((players and #players == 1) or CLIENT) then
local pl;
if CLIENT then
pl = LocalPlayer();
else
pl = players[1];
end
id = id .. pl:SteamID64();
end
local bv = setmetatable({}, BV);
bv._ID = id;
bv._Players = players;
settings = settings or {};
bv._ReadOnly = settings.ReadOnly or false;
bv._AutoApply = settings.AutoApply or false;
bv._UseSync = settings.UseSync or false;
bv._SyncIgnore = settings.SyncIgnore or {};
bv._RequireValue = settings.RequireValue or false;
bv._Values = {};
bv._NewValues = {};
if defaultValues then
for key, value in pairs(defaultValues) do
bv._Values[key] = value;
end
end
bv._Bindings = {};
bv._Listeners = {};
BV.Containers[id] = bv;
return bv;
end
function BV.RegisterBindType(name, meta)
meta.__index = meta;
BV.BindTypes[name] = meta;
end
function BV.BindToConVar(container, key, convar, settings)
container:_BindToConVar(key, convar, settings);
end
function BV.UnBindConVar(container, key, convar)
container:_UnBindConVar(key, convar);
end
function BV:_GetPlayers()
if CLIENT then
return {LocalPlayer()};
end
return self._Players or player.GetAll();
end
-- Convenience method for returning the first player in the owner list.
function BV:_GetPlayer()
if CLIENT then
return LocalPlayer();
end
if not self._Players or #self._Players == 0 then
return nil;
end
return self._Players[1];
end
function BV:_Bind(entity, key, type, settings)
entity.Bindings = entity.Bindings or {};
for _, binding in pairs(self._Bindings) do
if binding.Entity == entity and binding.Key == key then
ErrorNoHalt("Binding " .. entity .. " -> " .. self._ID .. " -> " .. key .. " already exists");
return;
end
end
if not BV.BindTypes[type] then
error("Invalid bind type");
return;
end
local bindMeta = BV.BindTypes[type];
local b = setmetatable({}, bindMeta);
b.Container = self;
b.Entity = entity;
b.Key = key;
b.Settings = settings or {};
table.insert(entity.Bindings, b);
table.insert(self._Bindings, b);
b:Init();
local value = self[key];
if value ~= nil then
b:SetValue(value);
end
end
function BV:_BindToConVar(key, convar, settings)
for _, binding in pairs(self._Bindings) do
if type(binding.Entity) == "ConVar" and binding.Entity:GetName() == convar:GetName() and binding.Key == key then
ErrorNoHalt("Binding " .. convar:GetName() .. " -> " .. self._ID .. " -> " .. key .. " already exists");
return;
end
end
local bindMeta = BV.BindTypes["ConVar"];
local b = setmetatable({}, bindMeta);
b.Container = self;
b.Entity = convar;
b.Key = key;
b.Settings = settings or {};
table.insert(self._Bindings, b);
b:Init();
local value = self[key];
if value ~= nil then
b:SetValue(value);
end
end
function BV:_UnBind(entity, key)
for _, binding in pairs(self._Bindings) do
if binding.Entity == entity and binding.Key == key then
binding:Remove();
table.RemoveByValue(self._Bindings, binding);
table.RemoveByValue(entity.Bindings, binding);
return;
end
end
end
function BV:_UnBindConVar(key, convar)
for _, binding in pairs(self._Bindings) do
if type(binding.Entity) == "ConVar" and binding.Entity:GetName() == convar:GetName() and binding.Key == key then
binding:Remove();
table.RemoveByValue(self._Bindings, binding);
return;
end
end
end
function BV:_Listen(key, func)
local l = {};
l.Key = key;
l.Function = func;
table.insert(self._Listeners, l);
return l;
end
function BV:_DontListen(listener)
table.RemoveByValue(self._Listeners, listener);
end
-- Call __newindex so that the given key is placed on _NewValues, marking it as changed.
function BV:_MarkChanged(key)
self[key] = self[key];
end
function BV:_ApplyFromBind(binding, key, value)
self._NewValues[key] = value;
self:_Apply(binding, false);
end
function BV:_ApplySynced(newValues)
self._NewValues = newValues;
self:_Apply(nil, true);
end
function BV:_Apply(fromBinding, fromSync)
local values = self._NewValues;
self._NewValues = {};
for key, value in pairs(values) do
self._Values[key] = value;
end
for _, binding in pairs(self._Bindings) do
local value = values[binding.Key];
if fromBinding ~= binding and value ~= nil then
binding:SetValue(value);
end
end
for _, listener in pairs(self._Listeners) do
local value = values[listener.Key];
if value ~= nil then
listener.Function(self, listener.Key, value);
end
end
if not fromSync and self._UseSync then
net.Start(SyncNetID);
net.WriteString(self._ID);
local syncValues = {};
for key, value in pairs(values) do
local typeid = TypeID(value);
if not table.HasValue(self._SyncIgnore, key) and net.WriteVars[typeid] then
syncValues[key] = value;
end
end
net.WriteTable(syncValues);
if SERVER then
net.Send(self:_GetPlayers());
else
net.SendToServer();
end
end
end
-- Call the given function, and apply changes afterwards. Good for changing many values while AutoApply is enabled
function BV:_ApplyBulk(func)
self._AutoApply = false;
func(self);
self:_Apply();
self._AutoApply = true;
end
-- Call a function from _Values and if sync is enabled, call it remotely as well
function BV:_Call(key, fromSync)
local func = self[key];
if func and type(func) == "function" then
func(self, key);
end
for _, listener in pairs(self._Listeners) do
if listener.Key == key then
listener.Function(self, listener.Key);
end
end
if not fromSync and self._UseSync then
net.Start(CallNetID);
net.WriteString(self._ID);
net.WriteString(key);
if SERVER then
net.Send(self:_GetPlayers());
else
net.SendToServer();
end
end
end
function BV._RecvSync(length, player)
if CLIENT then
player = LocalPlayer();
end
local id = net.ReadString();
local newValues = net.ReadTable();
local container = BV.Containers[id];
if not container then
ErrorNoHalt("Failed to synchronize; BiValue container not found!");
return;
end
if SERVER then
if container._ReadOnly then
Msg("Received call request to read-only container " .. container._ID .. " from player " .. player);
return;
end
local players = table.Copy(container:_GetPlayers());
if players and not table.HasValue(players, player) then
Msg("Unauthorized access to container " .. container._ID .. " from player " .. player);
return;
end
-- Broadcast call to any other players owning the container
table.RemoveByValue(players, player);
if #players > 0 then
net.Start(SyncNetID);
net.WriteString(container._ID);
net.WriteTable(newValues);
net.Send(players);
end
end
container:_ApplySynced(newValues);
end
function BV._RecvCall(length, player)
if CLIENT then
player = LocalPlayer();
end
local id = net.ReadString();
local key = net.ReadString();
local container = BV.Containers[id];
if not container then
ErrorNoHalt("Failed to synchronize; BiValue container not found!");
return;
end
if SERVER then
if container._ReadOnly then
Msg("Received call request to read-only container " .. container._ID .. " from player " .. player);
return;
end
local players = table.Copy(container:_GetPlayers());
if players and not table.HasValue(players, player) then
Msg("Unauthorized access to container " .. container._ID .. " from player " .. player);
return;
end
-- Broadcast call to any other players owning the container
table.RemoveByValue(players, player);
if #players > 0 then
net.Start(CallNetID);
net.WriteString(container._ID);
net.WriteString(key);
net.Send(players);
end
end
container:_Call(key, true);
end
if SERVER then
util.AddNetworkString(SyncNetID);
util.AddNetworkString(CallNetID);
end
net.Receive(SyncNetID, BV._RecvSync);
net.Receive(CallNetID, BV._RecvCall);
---
-- Default bind types
---
local VALUE = {};
function VALUE:Init()
local control = self.Entity;
local callback = self.Settings.Callback or "OnValueChanged";
control[callback] = function(control, value)
self:OnValueChanged(value);
end
end
function VALUE:Remove()
local control = self.Entity;
local callback = self.Settings.Callback or "OnValueChanged";
control[callback] = function() end
end
function VALUE:OnValueChanged(value)
local container = self.Container;
local key = self.Key;
container:_ApplyFromBind(self, key, value);
end
function VALUE:SetValue(value)
local control = self.Entity;
local callback = self.Settings.Callback or "OnValueChanged";
local valueFunc = self.Settings.ValueFunction or "SetValue";
local func = control[callback];
control[callback] = function() end
control[valueFunc](control, value);
control[callback] = func;
end
BV.RegisterBindType("Value", VALUE);
local NUMBER = setmetatable({}, VALUE);
function NUMBER:Init()
self.Settings.Callback = self.Settings.Callback or "OnValueChanged";
self.Settings.ValueFunction = self.Settings.ValueFunction or "SetValue";
VALUE.Init(self);
end
function NUMBER:OnValueChanged(value)
value = tonumber(value);
if not value then
value = 0;
end
VALUE.OnValueChanged(self, value);
end
BV.RegisterBindType("Number", NUMBER);
local TEXT = setmetatable({}, VALUE);
function TEXT:Init()
self.Settings.Callback = self.Settings.Callback or "OnChange";
self.Settings.ValueFunction = self.Settings.ValueFunction or "SetValue";
local control = self.Entity;
control[self.Settings.Callback] = function(control)
self:OnValueChanged(control:GetValue());
end
end
BV.RegisterBindType("TextEntry", TEXT);
local BUTTON = {};
function BUTTON:Init()
local control = self.Entity;
local callback = self.Settings.Callback or "DoClick";
control[callback] = function()
self:OnPress();
end
end
function BUTTON:Remove()
local control = self.Entity;
local callback = self.Settings.Callback or "DoClick";
control[callback] = function() end
end
function BUTTON:OnPress()
local container = self.Container;
local key = self.Key;
container:_Call(key);
end
function BUTTON:SetValue(value)
-- Do nothing
end
BV.RegisterBindType("Button", BUTTON);
local LABEL = {};
function LABEL:Init()
-- Do nothing
end
function LABEL:Remove()
-- Do nothing
end
function LABEL:SetValue(value)
local control = self.Entity;
control:SetText(value);
end
BV.RegisterBindType("Label", LABEL);
local VISIBILITY = setmetatable({}, VALUE);
function VISIBILITY:Init()
local control = self.Entity;
if not control.OldSetVisible then
control.OldSetVisible = control.SetVisible;
control.SetVisible = function(control, value)
control.OldSetVisible(control, value);
self:OnValueChanged(value);
end
end
self.Settings.ValueFunction = "SetVisible";
end
BV.RegisterBindType("Visibility", VISIBILITY);
local CHECKBOX = setmetatable({}, VALUE);
function CHECKBOX:Init()
self.Settings.Callback = self.Settings.Callback or "OnChange";
self.Settings.ValueFunction = self.Settings.ValueFunction or "SetValue";
VALUE.Init(self);
end
BV.RegisterBindType("CheckBox", CHECKBOX);
local LISTVIEW = {};
function LISTVIEW:Init()
-- Do nothing
end
function LISTVIEW:Remove()
-- Do nothing
end
function LISTVIEW:SetValue(value)
local control = self.Entity;
control:ClearSelection();
control:Clear();
if value == nil then
return;
end
for k, v in pairs(value) do
if type(v) == "table" then
control:AddLine(unpack(v));
elseif type(v) == "string" then
control:AddLine(v);
end
end
end
BV.RegisterBindType("ListView", LISTVIEW);
local LVSELECT = setmetatable({}, VALUE);
function LVSELECT:Init()
self.Settings.ColumnID = self.Settings.ColumnID or 1;
local control = self.Entity;
control.OnRowSelected = function(control, lineID, line)
local value = line:GetValue(self.Settings.ColumnID);
self:OnValueChanged(value);
end
end
function LVSELECT:Remove()
local control = self.Entity;
control.OnRowSelected = function() end
end
function LVSELECT:SetValue(value)
local control = self.Entity;
control:ClearSelection();
local func = control.OnRowSelected;
control.OnRowSelected = function() end
for i, line in pairs(control:GetLines()) do
local v = line:GetValue(self.Settings.ColumnID);
if v == value then
control:SelectItem(line);
break;
end
end
control.OnRowSelected = func;
end
BV.RegisterBindType("ListViewSelect", LVSELECT);
local COMBOBOX = {};
function COMBOBOX:Init()
-- Do nothing
end
function COMBOBOX:Remove()
-- Do nothing
end
function COMBOBOX:SetValue(value)
local control = self.Entity;
control:Clear();
if value == nil then
return;
end
for k, v in pairs(value) do
control:AddChoice(k, v);
end
end
BV.RegisterBindType("ComboBox", COMBOBOX);
local CBSELECT = setmetatable({}, VALUE);
function CBSELECT:Init()
local control = self.Entity;
control.OnSelect = function(control, index, text, data)
self:OnValueChanged(data);
end
end
function CBSELECT:Remove()
local control = self.Entity;
control.OnSelect = function() end
end
function CBSELECT:SetValue(value)
local control = self.Entity;
local func = control.OnSelect;
control.OnSelect = function() end
for idx, data in ipairs(control.Data) do
if data == value then
control:ChooseOptionID(idx);
break;
end
end
control.OnSelect = func;
end
BV.RegisterBindType("ComboBoxSelect", CBSELECT);
local CONVAR = setmetatable({}, VALUE);
function CONVAR:Init()
local convar = self.Entity;
if type(convar) ~= "ConVar" then
error("ConVar bind not bound to a convar!");
end
self:OnValueChanged(convar:GetString());
self.CallbackIdentifier = convar:GetName() .. math.random(1, 10000);
cvars.AddChangeCallback(convar:GetName(), function(name, oldValue, newValue)
self:OnValueChanged(newValue);
end, self.CallbackIdentifier);
end
function CONVAR:Remove()
local convar = self.Entity;
cvars.RemoveChangeCallback(convar:GetName(), self.CallbackIdentifier);
end
function CONVAR:OnValueChanged(value)
local container = self.Container;
local key = self.Key;
local valueType = self.Settings.ValueType or "string";
if valueType == "number" then
value = tonumber(value);
elseif valueType == "boolean" then
value = tobool(value);
else
value = tostring(value);
end
if value == nil then
return;
end
container:_ApplyFromBind(self, key, value);
end
function CONVAR:SetValue(value)
local convar = self.Entity;
cvars.RemoveChangeCallback(convar:GetName(), self.CallbackIdentifier);
local valueType = self.Settings.ValueType or "string";
if value ~= nil then
RunConsoleCommand(convar:GetName(), tostring(value));
end
cvars.AddChangeCallback(convar:GetName(), function(name, oldValue, newValue)
self:OnValueChanged(newValue);
end, self.CallbackIdentifier);
end
BV.RegisterBindType("ConVar", CONVAR);
BV.ValueBind = VALUE;
BV.NumberBind = NUMBER;
BV.TextBind = TEXT;
BV.ButtonBind = BUTTON;
BV.LabelBind = LABEL;
BV.VisibilityBind = VISIBILITY;
BV.CheckBoxBind = CHECKBOX;
BV.ListViewBind = LISTVIEW;
BV.ListViewSelectBind = LVSELECT;
BV.ComboBoxBind = COMBOBOX;
BV.ComboBoxSelectBind = CBSELECT;
BV.ConVarType = CONVAR;
---
-- Meta table setup
---
local ENT = FindMetaTable("Entity");
function ENT:Bind(container, key, type)
container:_Bind(self, key, type);
end
function ENT:UnBind(container, key)
container:_UnBind(self, key);
end
if CLIENT then
local PANEL = FindMetaTable("Panel");
function PANEL:Bind(container, key, type, settings)
container:_Bind(self, key, type, settings);
end
function PANEL:UnBind(container, key)
container:_UnBind(self, key);
end
end
---
-- Finally, put the table to the global scope, and make sure the clients download this script
---
AddCSLuaFile();
_G["BiValuesV" .. VERSION] = BV;
_G.BiValues = BV;