-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathgpulib.lua
More file actions
869 lines (734 loc) · 24.5 KB
/
gpulib.lua
File metadata and controls
869 lines (734 loc) · 24.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
--------------------------------------------------------------------------------
-- WireGPU class
--------------------------------------------------------------------------------
-- Usage:
-- Initialize:
-- self.GPU = WireGPU(self.Entity)
--
-- OnRemove:
-- self.GPU:Finalize()
--
-- Draw (if something changes):
-- self.GPU:RenderToGPU(function()
-- ...code...
-- end)
--
-- Draw (every frame):
-- self.GPU:Render()
--------------------------------------------------------------------------------
GPULib = {}
local GPU = {}
GPU.__index = GPU
GPULib.GPU = GPU
function GPULib.WireGPU(ent, ...)
local self = {
entindex = ent and ent:EntIndex() or 0,
Entity = ent or NULL,
}
setmetatable(self, GPU)
self:Initialize(...)
return self
end
WireGPU = GPULib.WireGPU
function GPU:SetTranslucentOverride(bool)
self.translucent = bool;
end
function GPU:GetInfo()
local ent = self.Entity
if not ent:IsValid() then ent = self.actualEntity end
if not ent then return end
local model = ent:GetModel()
local monitor = WireGPU_Monitors[model]
local pos = ent:LocalToWorld(monitor.offset)
local ang = ent:LocalToWorldAngles(monitor.rot)
return monitor, pos, ang
end
if CLIENT then
local materialCache = {}
function GPULib.Material(name)
if not materialCache[name] then
local protoMaterial = Material(name)
local textureName = protoMaterial:GetString("$basetexture")
local imageName = protoMaterial:GetName()
local materialParameters = {
["$basetexture"] = textureName,
["$vertexcolor"] = 1,
["$vertexalpha"] = 1,
}
materialCache[name] = CreateMaterial(imageName.."_DImage", "UnlitGeneric", materialParameters)
end
return materialCache[name]
end
-- Handles rendertarget caching
local RT_CACHE_SIZE = 64
local RenderTargetCache = { }
-- Todo: Just dynamically create table elements instead of having them pre-defined?
for i = 1, RT_CACHE_SIZE do
local Target = {
false, -- Is rendertarget in use
nil -- The rendertarget
}
table.insert(RenderTargetCache, Target)
end
-- Returns a render target from the cache pool and marks it as used
local function GetRT()
for i, RT in ipairs(RenderTargetCache) do
if not RT[1] then -- not used
local rendertarget = RT[2]
if rendertarget then
RT[1] = true -- Mark as used
return rendertarget
else
local rendertarget = GetRenderTargetEx("WireGPU_RT_" .. i, 1024, 1024, RT_SIZE_NO_CHANGE, MATERIAL_RT_DEPTH_SEPARATE, 256, 0, 12)
if rendertarget then
RT[1] = true -- Mark as used
RT[2] = rendertarget -- Assign the RT
return rendertarget
else
RT[1] = true -- Mark as used since we couldn't create it
ErrorNoHalt("Wiremod: Render target WireGPU_RT_" .. i .. " could not be created!\n")
end
end
end
end
ErrorNoHalt("All render targets are in use, some wire screens may not draw!\n")
return nil
end
-- Frees an used RT
local function FreeRT(rt)
for i, RT in pairs( RenderTargetCache ) do
if RT[2] == rt then
RT[1] = false
return
end
end
ErrorNoHalt("RT Screen ",rt," could not be freed (not found)\n")
end
//
// Create basic fonts
//
local fontData =
{
font="lucida console",
size=40,
weight=800,
antialias= true,
additive = false,
}
surface.CreateFont("WireGPU_ConsoleFont", fontData)
surface.CreateFont("LCDFontBlur", {
font = "Alphanumeric LCD",
size = 26,
antialias = false,
blursize = 1
})
surface.CreateFont("LCDFont", {
font = "Alphanumeric LCD",
size = 26,
antialias = false
})
//
// Create screen textures and materials
//
WireGPU_matScreen = CreateMaterial("sprites/GPURT","UnlitGeneric",{
["$vertexcolor"] = 1,
["$vertexalpha"] = 1,
["$translucent"] = 1,
["$ignorez"] = 1,
["$nolod"] = 1,
})
WireGPU_matBuffer = CreateMaterial("sprites/GPUBUF","UnlitGeneric",{
["$vertexcolor"] = 1,
["$vertexalpha"] = 1,
["$translucent"] = 1,
["$ignorez"] = 1,
["$nolod"] = 1,
})
function GPU:Initialize(no_rendertarget)
if no_rendertarget then return nil end
-- Rendertarget cache management
-- This should not even happen.
if self.RT then
ErrorNoHalt("Warning: GPU:Initialize called, but an RT still existed. Maybe you are not killing it properly?")
FreeRT(self.RT)
end
-- find a free one
self.RT = GetRT()
if not self.RT then
return nil
end
-- clear the new RT
self.ForceClear = true
return self.RT
end
function GPULib.WireGPU(ent, ...)
local self = {
entindex = ent and ent:EntIndex() or 0,
Entity = ent or NULL,
}
setmetatable(self, GPU)
self:Initialize(...)
return self
end
function GPU:Finalize()
if not self.RT then return end
timer.Simple(0.2, function() -- This is to test if the entity has truly been removed. If you really know you need to remove the RT, call FreeRT()
if IsValid(self.Entity) then
--MsgN(self,"Entity still exists, exiting.")
return
end
self:FreeRT()
end)
end
function GPU:FreeRT()
FreeRT( self.RT )
self.RT = nil
end
function GPU:Clear(color)
if not self.RT then return end
render.ClearRenderTarget(self.RT, color or Color(0, 0, 0, 0))
end
local texcoords = {
[0] = {
{ u = 0, v = 0 },
{ u = 1, v = 0 },
{ u = 1, v = 1 },
{ u = 0, v = 1 },
},
{
{ u = 0, v = 1 },
{ u = 0, v = 0 },
{ u = 1, v = 0 },
{ u = 1, v = 1 },
},
{
{ u = 1, v = 1 },
{ u = 0, v = 1 },
{ u = 0, v = 0 },
{ u = 1, v = 0 },
},
{
{ u = 1, v = 0 },
{ u = 1, v = 1 },
{ u = 0, v = 1 },
{ u = 0, v = 0 },
},
}
-- helper function for GPU:Render
function GPU.DrawScreen(x, y, w, h, rotation, scale, uvclipx, uvclipy)
-- generate vertex data
local vertices = {
--[[
Vector(x , y ),
Vector(x+w, y ),
Vector(x+w, y+h),
Vector(x , y+h),
]]
{ x = x , y = y },
{ x = x+w, y = y },
{ x = x+w, y = y+h },
{ x = x , y = y+h },
}
-- rotation and scaling
local rotated_texcoords = texcoords[rotation] or texcoords[0]
for index,vertex in ipairs(vertices) do
local tex = rotated_texcoords[index]
if tex.u == 0 then
vertex.u = tex.u-scale
else
vertex.u = tex.u+scale+uvclipx
end
if tex.v == 0 then
vertex.v = tex.v-scale
else
vertex.v = tex.v+scale+uvclipy
end
end
surface.DrawPoly(vertices)
--render.DrawQuad(unpack(vertices))
end
function GPU:RenderToGPU(renderfunction)
if not self.RT then return end
if self.ForceClear then
self:Clear()
self.ForceClear = nil
end
local oldw = ScrW()
local oldh = ScrH()
local NewRT = self.RT
local OldRT = render.GetRenderTarget()
render.SetRenderTarget(NewRT)
render.SetViewPort(0, 0, 1024, 1024)
cam.Start2D()
local ok, err = xpcall(renderfunction, debug.traceback)
if not ok then WireLib.ErrorNoHalt(err) end
cam.End2D()
render.SetViewPort(0, 0, oldw, oldh)
render.SetRenderTarget(OldRT)
end
-- If width is specified, height is ignored. if neither is specified, a height of 512 is used.
function GPU:RenderToWorld(width, height, renderfunction, zoffset, emulateRT)
local monitor, pos, ang = self:GetInfo()
if zoffset then
pos = pos + ang:Up()*zoffset
end
if emulateRT then
pos = pos - ang:Right()*(monitor.y2-monitor.y1)/2
pos = pos - ang:Forward()*(monitor.x2-monitor.x1)/2
end
local h = width and width*monitor.RatioX or height or 1024
local w = width or h/monitor.RatioX
local x = -w/2
local y = -h/2
local res = monitor.RS*1024/h
cam.Start3D2D(pos, ang, res)
local ok, err = xpcall(renderfunction, debug.traceback, x, y, w, h, monitor, pos, ang, res)
if not ok then WireLib.ErrorNoHalt(err) end
cam.End3D2D()
end
function GPU:Render(rotation, scale, width, height, postrenderfunction, uvclipx, uvclipy)
if not self.RT then return end
local monitor, pos, ang = self:GetInfo()
local OldTex = WireGPU_matScreen:GetTexture("$basetexture")
WireGPU_matScreen:SetTexture("$basetexture", self.RT)
local res = monitor.RS
cam.Start3D2D(pos, ang, res)
local ok, err = xpcall(function()
local aspect = 1/monitor.RatioX
local w = (width or 1024)*aspect
local h = (height or 1024)
local x = -w/2
local y = -h/2
local translucent = self.translucent;
if translucent == nil then
translucent = monitor.translucent
end
if not translucent then
surface.SetDrawColor(0,0,0,255)
surface.DrawRect(-512*aspect,-512,1024*aspect,1024)
end
surface.SetDrawColor(255,255,255,255)
surface.SetMaterial(WireGPU_matScreen)
render.PushFilterMag(self.texture_filtering or TEXFILTER.POINT)
render.PushFilterMin(self.texture_filtering or TEXFILTER.POINT)
self.DrawScreen(x, y, w, h, rotation or 0, scale or 0, uvclipx or 0, uvclipy or 0)
render.PopFilterMin()
render.PopFilterMag()
if postrenderfunction then postrenderfunction(pos, ang, res, aspect, monitor) end
end, debug.traceback)
if not ok then WireLib.ErrorNoHalt(err) end
cam.End3D2D()
WireGPU_matScreen:SetTexture("$basetexture", OldTex)
end
-- compatibility
local GPUs = {}
function WireGPU_NeedRenderTarget(entindex)
if not GPUs[entindex] then GPUs[entindex] = GPULib.WireGPU(Entity(entindex)) end
return GPUs[entindex].RT
end
function WireGPU_GetMyRenderTarget(entindex)
local self = GPUs[entindex]
if self.RT then return self.RT end
return self:Initialize()
end
function WireGPU_ReturnRenderTarget(entindex)
return GPUs[entindex]:Finalize()
end
function WireGPU_DrawScreen(x, y, w, h, rotation, scale)
return GPU.DrawScreen(x, y, w, h, rotation, scale)
end
end
-- GPULib switcher functionality
if CLIENT then
usermessage.Hook("wire_gpulib_setent", function(um)
local screen = Entity(um:ReadShort())
if not screen:IsValid() then return end
if not screen.GPU then return end
local ent = Entity(um:ReadShort())
if not ent:IsValid() then return end
screen.GPU.Entity = ent
screen.GPU.entindex = ent:EntIndex()
if screen == ent then return end
screen.GPU.actualEntity = screen
local model = ent:GetModel()
local monitor = WireGPU_Monitors[model]
local h = 1024*monitor.RS
local w = h/monitor.RatioX
local x = -w/2
local y = -h/2
local corners = {
{ x , y },
{ x , y+h },
{ x+w, y },
{ x+w, y+h },
}
local mins, maxs = screen:OBBMins(), screen:OBBMaxs()
local timerid = "wire_gpulib_updatebounds"..screen:EntIndex()
local function setbounds()
if not screen:IsValid() then
timer.Remove(timerid)
return
end
if not ent:IsValid() then
timer.Remove(timerid)
screen.ExtraRBoxPoints[1001] = nil
screen.ExtraRBoxPoints[1002] = nil
screen.ExtraRBoxPoints[1003] = nil
screen.ExtraRBoxPoints[1004] = nil
Wire_UpdateRenderBounds(screen)
screen.GPU.Entity = screen.GPU.actualEntity
screen.GPU.entindex = screen.GPU.actualEntity:EntIndex()
screen.GPU.actualEntity = nil
return
end
local ang = ent:LocalToWorldAngles(monitor.rot)
local pos = ent:LocalToWorld(monitor.offset)
screen.ExtraRBoxPoints = screen.ExtraRBoxPoints or {}
for i,x,y in ipairs_map(corners, unpack) do
local p = Vector(x, y, 0)
p:Rotate(ang)
p = screen:WorldToLocal(p+pos)
screen.ExtraRBoxPoints[i+1000] = p
end
Wire_UpdateRenderBounds(screen)
end
timer.Create(timerid, 5, 0, setbounds)
setbounds()
end) -- usermessage.Hook
elseif SERVER then
function GPULib.switchscreen(screen, ent)
screen.GPUEntity = ent
umsg.Start("wire_gpulib_setent")
umsg.Short(screen:EntIndex())
umsg.Short(ent:EntIndex())
umsg.End()
end
end
-- GPULib caching functionality
if CLIENT then
------------------------------------------------------------------------------
-- Attach cache receiver to this entity
------------------------------------------------------------------------------
local writeHandler = {}
function GPULib.ClientCacheCallback(ent, writeFunction)
writeHandler[ent and ent:EntIndex() or 0] = writeFunction
end
------------------------------------------------------------------------------
-- RLE-decompress incoming message
------------------------------------------------------------------------------
--[[local blockText = {
{ "[no offset]", "[1-offset]", "[2-offset]", "[4-offset]" },
{ "[no rep]", nil, "[rep 2]", "[rep 4]" },
{ "[cnt 1]", nil, "[cnt 2]", "[cnt 3]" },
{ "[1-byte]", "[2-byte]", "[4-byte]", "[marker]" },
} ]]--
local function GPULib_MemorySync(um)
-- Find the referenced entity
local GPUIdx = um:ReadLong()
local GPU = ents.GetByIndex(GPUIdx)
if not GPU then return end
if not GPU:IsValid() then return end
if not writeHandler[GPUIdx] then return end
-- Start reading blocks
local blockCount = 0
local currentOffset = 0
while true do
-- Read next block
blockCount = blockCount + 1
if blockCount > 256 then error("GPULib usermessage read error") return end
-- Read block flags
local dataFlags = um:ReadChar()+128
if dataFlags == 240 then return end
local offsetSize = dataFlags % 4
local repeatCount = math.floor(dataFlags/4) % 4
local dataCount = math.floor(dataFlags/16) % 4
local valueSize = math.floor(dataFlags/64) % 4
local Repeat = 0
local Count = 0
if offsetSize > 0 then
local deltaOffset = 0
if offsetSize == 1 then deltaOffset = um:ReadChar () end
if offsetSize == 2 then deltaOffset = um:ReadShort() end
if offsetSize == 3 then deltaOffset = um:ReadFloat() end
currentOffset = currentOffset + deltaOffset
--print(" dOffset = "..deltaOffset..", offset = "..currentOffset)
end
if dataCount == 0 then Count = 1 end
if dataCount == 1 then Count = um:ReadChar()+130 end
if dataCount == 2 then Count = 2 end
if dataCount == 3 then Count = 3 end
if repeatCount == 0 then Repeat = 1 end
if repeatCount == 1 then Repeat = um:ReadChar()+130 end
if repeatCount == 2 then Repeat = 2 end
if repeatCount == 3 then Repeat = 4 end
--[[print(" Block ",
blockText[1][offsetSize+1],
blockText[2][repeatCount+1] or ("[rep "..Repeat.."* ]"),
blockText[3][dataCount+1] or ("[cnt "..Count.."* ]"),
blockText[4][valueSize+1])]]--
for i=1,Count do
local Value = 0
if valueSize == 0 then Value = um:ReadChar() end
if valueSize == 1 then Value = um:ReadShort() end
if valueSize == 2 then Value = um:ReadLong() end
if valueSize == 3 then Value = um:ReadFloat() end
for j=1,Repeat do
--print(" ["..currentOffset.."] = "..Value)
writeHandler[GPUIdx](currentOffset,Value)
currentOffset = currentOffset + 1
end
end
end
end
usermessage.Hook("wire_memsync", GPULib_MemorySync)
elseif SERVER then
local CACHEMGR = {}
CACHEMGR.__index = CACHEMGR
GPULib.CACHEMGR = CACHEMGR
------------------------------------------------------------------------------
-- Create new cache manager (serverside)
------------------------------------------------------------------------------
function GPULib.GPUCacheManager(ent, orderMatters, ...)
local self = {
EntIndex = ent and ent:EntIndex() or 0,
Entity = ent or NULL,
}
setmetatable(self, CACHEMGR)
self.ValueOrderMatters = orderMatters
self.Enabled = true
self:Reset()
return self
end
GPUCacheManager = GPULib.GPUCacheManager
------------------------------------------------------------------------------
-- Get size of the value to write
------------------------------------------------------------------------------
local function getSize(value)
if (value >= -128) and (value <= 127) and (math.floor(value) == value) then return 1,false end
if (value >= -32768) and (value <= 32767) and (math.floor(value) == value) then return 2,false end
if (value >= -2147483648) and (value <= 2147483647) and (math.floor(value) == value) then return 4,false end
return 4,true
end
------------------------------------------------------------------------------
-- Initialize cache manager
------------------------------------------------------------------------------
function CACHEMGR:Reset()
self.Cache = {}
self.CacheBytes = 0
end
------------------------------------------------------------------------------
-- Write a single value to cache
------------------------------------------------------------------------------
function CACHEMGR:Write(Address,Value)
local valueSize,valueFloat
if Value then
valueSize,valueFloat = getSize(Value)
self.CacheBytes = self.CacheBytes + valueSize
end
table.insert(self.Cache,{ Address, Value, valueSize, valueFloat })
--if #self.Cache > 2048 then self:Flush() end
end
------------------------------------------------------------------------------
-- Send value right away
------------------------------------------------------------------------------
function CACHEMGR:WriteNow(Address,Value,forcePlayer)
umsg.Start("wire_memsync", forcePlayer)
umsg.Long(self.EntIndex)
umsg.Char(195-128)
umsg.Float(Address)
umsg.Float(Value)
umsg.Char(240-128)
umsg.End()
end
------------------------------------------------------------------------------
-- RLE-compress cache and send it
------------------------------------------------------------------------------
function CACHEMGR:Flush(forcePlayer)
-- Don't flush if nothing cached
if #self.Cache == 0 then return end
self.CacheBytes = 0
-- Sort cache so all addresses are continiously layed out
-- Do not sort if order at which values are written matters
if not self.ValueOrderMatters then
table.sort(self.Cache,function(A,B)
return A[1] < B[1]
end)
end
-- RLE-encode the data
local compressInfo = {}
for _,data in ipairs(self.Cache) do
local address,value,size,isfloat = data[1],(data[2] or 0),(data[3] or 1),(data[4] or false)
local compressBlock = compressInfo[#compressInfo]
local sequentialBlock
local previousBlockEnd
if compressBlock then
previousBlockEnd = compressBlock.Offset+#compressBlock.Data*compressBlock.Repeat
sequentialBlock = previousBlockEnd == address
end
if not compressBlock then
-- New block of data
compressBlock = {
Data = { value },
Offset = address,
SetOffset = address,
Repeat = 1,
Size = size,
IsFloat = isfloat,
}
table.insert(compressInfo,compressBlock)
elseif sequentialBlock and
(compressBlock.Size == size) then
-- Add to previous block of data
if (#compressBlock.Data == 1) and (compressBlock.Data[1] == value) and (sequentialBlock) and (compressBlock.Repeat < 256) then
-- RLE compression
compressBlock.Repeat = compressBlock.Repeat + 1
elseif compressBlock.Repeat > 1 then
-- Cant add to a repeating block, make new
compressBlock = {
Data = { value },
Offset = address,
Repeat = 1,
Size = size,
IsFloat = isfloat,
}
if not sequentialBlock then compressBlock.SetOffset = address-previousBlockEnd end
table.insert(compressInfo,compressBlock)
else
-- Append to a group of values, unless the block is too big
if #compressBlock.Data*compressBlock.Repeat*compressBlock.Size < 196 then
table.insert(compressBlock.Data,value)
else
-- Add it to a new block instead
compressBlock = {
Data = { value },
Offset = address,
Repeat = 1,
Size = size,
IsFloat = isfloat,
}
if not sequentialBlock then compressBlock.SetOffset = address-previousBlockEnd end
table.insert(compressInfo,compressBlock)
end
end
else
-- Create new block
compressBlock = {
Data = { value },
Offset = address,
Repeat = 1,
Size = size,
IsFloat = isfloat,
}
if not sequentialBlock then compressBlock.SetOffset = address-previousBlockEnd end
table.insert(compressInfo,compressBlock)
end
end
--PrintTable(compressInfo)
-- Start the message
local messageSize = 4
umsg.Start("wire_memsync", forcePlayer)
umsg.Long(self.EntIndex)
-- Start sending all compressed blocks
for k,v in ipairs(compressInfo) do
--======================================================================--
-- Generate flags for sending the data
--======================================================================--
-- [0..1] Delta offset
-- 0: no offset
-- 1: 1-byte offset
-- 2: 2-byte offset
-- 3: 4-byte offset
-- [2..3] Repeat count
-- 0: none
-- 1: repeat count 1-byte follows
-- 2: repeat 2 times
-- 3: repeat 4 times
-- [4..5] Data count
-- 0: 1 element
-- 1: data size 1-byte follows
-- 2: 2 elements
-- 3: 3 elements (but not floats)
-- [6..7] Size
-- 0: 1-byte
-- 1: 2-byte
-- 2: 4-byte int
-- 3: 4-byte float
--
-- If it's a special data marker, then bitmap is:
-- [0..1] Marker type
-- [2..5] Marker data
-- [6] 1
-- [7] 1
local dataFlags = 0
if v.SetOffset then
local offsetSize = getSize(v.SetOffset)
if offsetSize == 1 then dataFlags = dataFlags + 1 end
if offsetSize == 2 then dataFlags = dataFlags + 2 end
if offsetSize == 4 then dataFlags = dataFlags + 3 end
end
if v.Repeat > 1 then
if v.Repeat == 2 then dataFlags = dataFlags + 8
elseif v.Repeat == 4 then dataFlags = dataFlags + 12
else dataFlags = dataFlags + 4
end
end
if #v.Data > 1 then
if #v.Data == 2 then
dataFlags = dataFlags + 32
elseif (#v.Data == 3) and (not v.IsFloat) then
dataFlags = dataFlags + 48
else
dataFlags = dataFlags + 16
end
end
if v.Size == 1 then dataFlags = dataFlags + 0 end
if v.Size == 2 then dataFlags = dataFlags + 64 end
if (v.Size == 4) and (not v.IsFloat) then dataFlags = dataFlags + 128 end
if (v.Size == 4) and ( v.IsFloat) then dataFlags = dataFlags + 192 end
umsg.Char(dataFlags-128)
messageSize = messageSize + 4
--======================================================================--
-- Send the data
--======================================================================--
if v.SetOffset then
local offsetSize = getSize(v.SetOffset)
if offsetSize == 1 then umsg.Char (v.SetOffset) messageSize = messageSize + 1 end
if offsetSize == 2 then umsg.Short(v.SetOffset) messageSize = messageSize + 2 end
if offsetSize == 4 then umsg.Float(v.SetOffset) messageSize = messageSize + 4 end
end
if (#v.Data > 2) then
if (#v.Data ~= 3) or (v.IsFloat) then
umsg.Char(#v.Data-130) messageSize = messageSize + 1
end
end
if (v.Repeat > 1) and
(v.Repeat ~= 2) and
(v.Repeat ~= 4) then umsg.Char(v.Repeat-130) messageSize = messageSize + 1 end
for _,value in ipairs(v.Data) do
if v.Size == 1 then umsg.Char (value) messageSize = messageSize + 1 end
if v.Size == 2 then umsg.Short(value) messageSize = messageSize + 2 end
if (v.Size == 4) and (not v.IsFloat) then umsg.Long(value) messageSize = messageSize + 4 end
if (v.Size == 4) and ( v.IsFloat) then umsg.Float(value) messageSize = messageSize + 4 end
end
--======================================================================--
-- Check size of next data block. If it fits into usermessage, continue.
-- Otherwise just create new message
--======================================================================--
if compressInfo[k+1] then
local nextSize = #compressInfo[k+1].Data*compressInfo[k+1].Repeat*compressInfo[k+1].Size
if nextSize + messageSize > 248 then
umsg.Char(240-128)
umsg.End()
messageSize = 4
umsg.Start("wire_memsync", forcePlayer)
umsg.Long(self.EntIndex)
compressInfo[k+1].SetOffset = compressInfo[k+1].Offset -- Force set offset
end
else
umsg.Char(240-128)
umsg.End()
end
end
self.Cache = {}
end
end