-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceInfo.cpp
More file actions
906 lines (783 loc) · 27.1 KB
/
Copy pathDeviceInfo.cpp
File metadata and controls
906 lines (783 loc) · 27.1 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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//======================================================================================================================
// Project: OpenRGB - C++ SDK
//----------------------------------------------------------------------------------------------------------------------
// Author: Jan Broz (Youda008)
// Description: data structures containing device information
//======================================================================================================================
#include <OpenRGB/DeviceInfo.hpp>
#include <CppUtils-Essential/Essential.hpp>
#include "ProtocolCommon.hpp"
#include "MiscUtils.hpp"
#include <CppUtils-Essential/BinaryStream.hpp>
using own::BinaryOutputStream;
using own::BinaryInputStream;
#include <CppUtils-Essential/LangUtils.hpp>
using own::unconst;
#include <string>
using std::string;
#include <sstream>
using std::ostringstream; // flags to string
namespace orgb {
//======================================================================================================================
// enum strings
const char * enumString( DeviceType type ) noexcept
{
static const char * const DeviceTypeStr [] =
{
"Motherboard",
"DRAM",
"GPU",
"Cooler",
"LedStrip",
"Keyboard",
"Mouse",
"MouseMat",
"Headset",
"HeadsetStand",
"Gamepad",
"Light",
"Speaker",
"Virtual",
"Unknown",
};
static_assert( size_t(DeviceType::Unknown) + 1 == fut::size(DeviceTypeStr), "update the DeviceTypeStr" );
// Collapse everything else than known classes to Unknown
// in case the server adds some classes without increasing protocol version.
if (size_t( type ) < size_t( DeviceType::Unknown ))
return DeviceTypeStr[ size_t( type ) ];
else
return DeviceTypeStr[ size_t( DeviceType::Unknown ) ];
}
string modeFlagsToString( uint32_t flags )
{
ostringstream oss;
bool isFirst = true;
auto addFlag = [ &isFirst, &oss ]( const char * flagStr )
{
if (isFirst) {
isFirst = false;
} else {
oss << " | ";
}
oss << flagStr;
};
if (flags & ModeFlags::HasSpeed)
addFlag( "HasSpeed" );
if (flags & ModeFlags::HasDirectionLR)
addFlag( "HasDirectionLR" );
if (flags & ModeFlags::HasDirectionUD)
addFlag( "HasDirectionUD" );
if (flags & ModeFlags::HasDirectionHV)
addFlag( "HasDirectionHV" );
if (flags & ModeFlags::HasBrightness)
addFlag( "HasBrightness" );
if (flags & ModeFlags::HasPerLedColor)
addFlag( "HasPerLedColor" );
if (flags & ModeFlags::HasModeSpecificColor)
addFlag( "HasModeSpecificColor" );
if (flags & ModeFlags::HasRandomColor)
addFlag( "HasRandomColor" );
return oss.str();
}
const char * enumString( Direction dir ) noexcept
{
static const char * const DirectionStr [] =
{
"Left",
"Right",
"Up",
"Down",
"Horizontal",
"Vertical",
};
static_assert( size_t(Direction::Vertical) + 1 == fut::size(DirectionStr), "update the DirectionStr" );
if (size_t( dir ) <= size_t( Direction::Vertical ))
return DirectionStr[ size_t( dir ) ];
else
return "<invalid>";
}
const char * enumString( ColorMode mode ) noexcept
{
static const char * const ColorModeStr [] =
{
"None",
"PerLed",
"ModeSpecific",
"Random",
};
static_assert( size_t(ColorMode::Random) + 1 == fut::size(ColorModeStr), "update the ColorModeStr" );
if (size_t( mode ) <= size_t( ColorMode::Random ) )
return ColorModeStr[ size_t( mode ) ];
else
return "<invalid>";
}
const char * enumString( ZoneType type ) noexcept
{
static const char * const ZoneTypeStr [] =
{
"Single",
"Linear",
"Matrix",
};
static_assert( size_t(ZoneType::Matrix) + 1 == fut::size(ZoneTypeStr), "update the ZoneTypeStr" );
if (size_t( type ) <= size_t( ZoneType::Matrix ))
return ZoneTypeStr[ size_t( type ) ];
else
return "<invalid>";
}
//======================================================================================================================
// enum validation
/*static bool isValidDeviceType( DeviceType type )
{
return size_t( type ) <= size_t( DeviceType::Unknown );
}*/
static bool isValidDirection( Direction dir, uint32_t modeFlags )
{
bool allowedDirections [ size_t( Direction::Vertical ) + 1 ] = {0};
bool hasAnyDirections = false;
if (modeFlags & ModeFlags::HasDirectionLR)
{
hasAnyDirections = true;
allowedDirections[ size_t( Direction::Left ) ] = true;
allowedDirections[ size_t( Direction::Right ) ] = true;
}
if (modeFlags & ModeFlags::HasDirectionUD)
{
hasAnyDirections = true;
allowedDirections[ size_t( Direction::Up ) ] = true;
allowedDirections[ size_t( Direction::Down ) ] = true;
}
if (modeFlags & ModeFlags::HasDirectionHV)
{
hasAnyDirections = true;
allowedDirections[ size_t( Direction::Horizontal ) ] = true;
allowedDirections[ size_t( Direction::Vertical ) ] = true;
}
// in case no direction flag is active, direction will be uninitialized value, so it can be anything
if (!hasAnyDirections)
{
return true;
}
return allowedDirections[ size_t( dir ) ] == true;
}
static bool isValidColorMode( ColorMode mode )
{
return size_t( mode ) <= size_t( ColorMode::Random );
}
static bool isValidZoneType( ZoneType type )
{
return size_t( type ) <= size_t( ZoneType::Matrix );
}
//======================================================================================================================
// LED
LED::LED()
:
idx(),
parentIdx(),
name(),
value()
{}
size_t LED::calcSize( uint32_t /*protocolVersion*/ ) const noexcept
{
size_t size = 0;
size += protocol::sizeofString( name );
size += sizeof( value );
return size;
}
void LED::serialize( BinaryOutputStream & stream, uint32_t /*protocolVersion*/ ) const
{
/* Original Implementation
protocol::writeString( stream, name );
stream << value;*/
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
protocol::writeString( stream, name ); // keep existing string helper
stream.writeLittleEndian( value ); // replace '<< value'
//end patch
}
bool LED::deserialize( BinaryInputStream & stream, uint32_t /*protocolVersion*/, uint32_t idx, uint32_t parentIdx ) noexcept
{
// This hack with const casts allows us to restrict the user from changing attributes that are a static description
// and allow him to change only the parameters that are meant to be changed.
// fill in our metadata
unconst( this->idx ) = idx;
unconst( this->parentIdx ) = parentIdx;
/* Original code
protocol::readString( stream, unconst( name ) );
stream >> unconst( value ); */
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
protocol::readString(stream, unconst(name)); // read the string
stream.readLittleEndian(unconst(value)); // read the integer value
//end patch
return !stream.failed();
}
void print( const LED & led, unsigned int indentLevel )
{
indent( indentLevel ); printf( "[%u] = { name = \"%s\"; value = %u },\n", led.idx, led.name.c_str(), led.value );
}
void print( std::ostream & os, const LED & led, unsigned int indentLevel )
{
indent( os, indentLevel ); os << "["<<led.idx<<"] = { name = \""<<led.name<<"\"; value = "<<led.value<<" },\n";
}
//======================================================================================================================
// Zone
Zone::Zone()
:
idx(),
parentIdx(),
name(),
type(),
leds_min(),
leds_max(),
leds_count(),
matrix_height(),
matrix_width(),
matrix_values()
{}
size_t Zone::calcSize( uint32_t /*protocolVersion*/ ) const noexcept
{
size_t size = 0;
size += protocol::sizeofString( name );
size += sizeof( type );
size += sizeof( leds_min );
size += sizeof( leds_max );
size += sizeof( leds_count );
size += sizeof( uint16_t ); // length of the optional matrix block
if (matrix_values.size() > 0)
{
size += sizeof( matrix_height );
size += sizeof( matrix_width );
size += own::sizeofVector( matrix_values );
}
return size;
}
void Zone::serialize( BinaryOutputStream & stream, uint32_t /*protocolVersion*/ ) const
{
/* Original code
protocol::writeString( stream, name );
stream << type;
stream << leds_min;
stream << leds_max;
stream << leds_count;
uint16_t matrix_length = uint16_t(
matrix_values.size() > 0
? sizeof( matrix_height ) + sizeof( matrix_width ) + own::sizeofVector( matrix_values )
: 0
);
stream << matrix_length; // length of the optional matrix block
if (matrix_length > 0)
{
stream << matrix_height;
stream << matrix_width;
for (auto val : matrix_values)
{
stream << val;
}
} */
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
protocol::writeString(stream, name); // write the zone name
// write basic integer fields
stream.writeLittleEndian(type);
stream.writeLittleEndian(leds_min);
stream.writeLittleEndian(leds_max);
stream.writeLittleEndian(leds_count);
// compute length of optional matrix block
uint16_t matrix_length = uint16_t(
matrix_values.size() > 0
? sizeof(matrix_height) + sizeof(matrix_width) + matrix_values.size() * sizeof(matrix_values[0])
: 0
);
stream.writeLittleEndian(matrix_length);
// write optional matrix block if it exists
if (matrix_length > 0)
{
stream.writeLittleEndian(matrix_height);
stream.writeLittleEndian(matrix_width);
// write matrix values as raw bytes
stream.writeTrivialArray(matrix_values);
}
//end patch
}
bool Zone::deserialize( BinaryInputStream & stream, uint32_t /*protocolVersion*/, uint32_t idx, uint32_t parentIdx ) noexcept
{
// This hack with const casts allows us to restrict the user from changing attributes that are a static description
// and allow him to change only the parameters that are meant to be changed.
/* Original Implementation
// fill in our metadata
unconst( this->idx ) = idx;
unconst( this->parentIdx ) = parentIdx;
protocol::readString( stream, unconst( name ) );
stream >> unconst( type );
stream >> unconst( leds_min );
stream >> unconst( leds_max );
stream >> unconst( leds_count );
uint16_t matrix_length;
stream >> matrix_length;
if (matrix_length > 0)
{
stream >> unconst( matrix_height );
stream >> unconst( matrix_width );
size_t matrixSize = matrix_height * matrix_width;
unconst( matrix_values ).resize( matrixSize );
for (size_t i = 0; i < matrixSize; ++i)
{
stream >> unconst( matrix_values )[i];
}
} */
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
// fill in metadata (const_cast hack)
unconst(this->idx) = idx;
unconst(this->parentIdx) = parentIdx;
// read zone name
protocol::readString(stream, unconst(name));
// read integer fields
stream.readLittleEndian(unconst(type));
stream.readLittleEndian(unconst(leds_min));
stream.readLittleEndian(unconst(leds_max));
stream.readLittleEndian(unconst(leds_count));
// read optional matrix block length
uint16_t matrix_length = 0;
stream.readLittleEndian(matrix_length);
if (matrix_length > 0)
{
stream.readLittleEndian(unconst(matrix_height));
stream.readLittleEndian(unconst(matrix_width));
size_t matrixSize = matrix_height * matrix_width;
unconst(matrix_values).resize(matrixSize);
// read all matrix values as a contiguous array
stream.readResizableTrivialArray(unconst(matrix_values), matrixSize);
}
//end patch
if (!isValidZoneType( type ))
stream.setFailed();
return !stream.failed();
}
void print( const Zone & zone, unsigned int indentLevel )
{
indent( indentLevel ); printf( "[%u] = {\n", zone.idx );
indent( indentLevel + 1 ); printf( "name = \"%s\";\n", zone.name.c_str() );
indent( indentLevel + 1 ); printf( "type = %s;\n", enumString( zone.type ) );
indent( indentLevel + 1 ); printf( "leds_min = %u;\n", zone.leds_min );
indent( indentLevel + 1 ); printf( "leds_max = %u;\n", zone.leds_max );
indent( indentLevel + 1 ); printf( "leds_count = %u;\n", zone.leds_count );
indent( indentLevel + 1 ); printf( "matrix_height = %u;\n", zone.matrix_height );
indent( indentLevel + 1 ); printf( "matrix_width = %u;\n", zone.matrix_width );
indent( indentLevel ); printf( "},\n" );
}
void print( std::ostream & os, const Zone & zone, unsigned int indentLevel )
{
indent( os, indentLevel ); os << "["<<zone.idx<<"] = {\n";
indent( os, indentLevel + 1 ); os << "name = \""<<zone.name.c_str()<<"\";\n";
indent( os, indentLevel + 1 ); os << "type = "<<enumString( zone.type )<<";\n";
indent( os, indentLevel + 1 ); os << "leds_min = "<<zone.leds_min<<";\n";
indent( os, indentLevel + 1 ); os << "leds_max = "<<zone.leds_max<<";\n";
indent( os, indentLevel + 1 ); os << "leds_count = "<<zone.leds_count<<";\n";
indent( os, indentLevel + 1 ); os << "matrix_height = "<<zone.matrix_height<<";\n";
indent( os, indentLevel + 1 ); os << "matrix_width = "<<zone.matrix_width<<";\n";
indent( os, indentLevel ); os << "},\n";
}
//======================================================================================================================
// Mode
Mode::Mode()
:
idx(),
parentIdx(),
name(),
value(),
flags(),
speed_min(),
speed_max(),
brightness_min(),
brightness_max(),
colors_min(),
colors_max(),
speed(),
brightness(),
direction(),
color_mode(),
colors()
{}
size_t Mode::calcSize( uint32_t protocolVersion ) const noexcept
{
size_t size = 0;
size += protocol::sizeofString( name );
size += sizeof( value );
size += sizeof( flags );
size += sizeof( speed_min );
size += sizeof( speed_max );
if (protocolVersion >= 3)
{
size += sizeof( brightness_min );
size += sizeof( brightness_max );
}
size += sizeof( colors_min );
size += sizeof( colors_max );
size += sizeof( speed );
if (protocolVersion >= 3)
{
size += sizeof( brightness );
}
size += sizeof( direction );
size += sizeof( color_mode );
size += protocol::sizeofArray( colors );
return size;
}
void Mode::serialize( BinaryOutputStream & stream, uint32_t protocolVersion ) const
{
protocol::writeString( stream, name );
/* Original Implementation
stream << value;
stream << flags;
stream << speed_min;
stream << speed_max;
if (protocolVersion >= 3)
{
stream << brightness_min;
stream << brightness_max;
}
stream << colors_min;
stream << colors_max;
stream << speed;
if (protocolVersion >= 3)
{
stream << brightness;
}
stream << direction;
stream << color_mode;
*/
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
// write integer fields in little-endian
stream.writeLittleEndian(value);
stream.writeLittleEndian(flags);
stream.writeLittleEndian(speed_min);
stream.writeLittleEndian(speed_max);
if (protocolVersion >= 3)
{
stream.writeLittleEndian(brightness_min);
stream.writeLittleEndian(brightness_max);
}
stream.writeLittleEndian(colors_min);
stream.writeLittleEndian(colors_max);
stream.writeLittleEndian(speed);
if (protocolVersion >= 3)
{
stream.writeLittleEndian(brightness);
}
stream.writeLittleEndian(direction);
stream.writeLittleEndian(color_mode);
//end patch
protocol::writeArray( stream, colors );
}
bool Mode::deserialize( BinaryInputStream & stream, uint32_t protocolVersion, uint32_t idx, uint32_t parentIdx ) noexcept
{
// This hack with const casts allows us to restrict the user from changing attributes that are a static description
// and allow him to change only the parameters that are meant to be changed.
// fill in our metadata
unconst( this->idx ) = idx;
unconst( this->parentIdx ) = parentIdx;
protocol::readString( stream, unconst( name ) );
/* Original code
stream >> unconst( value );
stream >> unconst( flags );
stream >> unconst( speed_min );
stream >> unconst( speed_max );
if (protocolVersion >= 3)
{
stream >> unconst( brightness_min );
stream >> unconst( brightness_max );
}
stream >> unconst( colors_min );
stream >> unconst( colors_max );
stream >> speed;
if (protocolVersion >= 3)
{
stream >> unconst( brightness );
}
stream >> direction;
stream >> unconst( color_mode ); */
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
// read integer fields in little-endian
stream.readLittleEndian(unconst(value));
stream.readLittleEndian(unconst(flags));
stream.readLittleEndian(unconst(speed_min));
stream.readLittleEndian(unconst(speed_max));
if (protocolVersion >= 3)
{
stream.readLittleEndian(unconst(brightness_min));
stream.readLittleEndian(unconst(brightness_max));
}
stream.readLittleEndian(unconst(colors_min));
stream.readLittleEndian(unconst(colors_max));
stream.readLittleEndian(speed);
if (protocolVersion >= 3)
{
stream.readLittleEndian(unconst(brightness));
}
stream.readLittleEndian(direction);
stream.readLittleEndian(unconst(color_mode));
//end patch
protocol::readArray( stream, colors );
if (!isValidDirection( direction, flags ))
stream.setFailed();
if (!isValidColorMode( color_mode ))
stream.setFailed();
return !stream.failed();
}
void print( const Mode & mode, unsigned int indentLevel )
{
indent( indentLevel ); printf( "[%u] = {\n", mode.idx );
indent( indentLevel + 1 ); printf( "name = \"%s\";\n", mode.name.c_str() );
indent( indentLevel + 1 ); printf( "value = %u;\n", mode.value );
indent( indentLevel + 1 ); printf( "flags = %s;\n", modeFlagsToString( mode.flags ).c_str() );
indent( indentLevel + 1 ); printf( "direction = %s;\n", enumString( mode.direction ) );
indent( indentLevel + 1 ); printf( "speed_min = %u;\n", mode.speed_min );
indent( indentLevel + 1 ); printf( "speed_max = %u;\n", mode.speed_max );
indent( indentLevel + 1 ); printf( "speed = %u;\n", mode.speed );
indent( indentLevel + 1 ); printf( "brightness_min = %u;\n", mode.brightness_min );
indent( indentLevel + 1 ); printf( "brightness_max = %u;\n", mode.brightness_max );
indent( indentLevel + 1 ); printf( "brightness = %u;\n", mode.brightness );
indent( indentLevel + 1 ); printf( "colors_min = %u;\n", mode.colors_min );
indent( indentLevel + 1 ); printf( "colors_max = %u;\n", mode.colors_max );
indent( indentLevel + 1 ); printf( "color_mode = %s;\n", enumString( mode.color_mode ) );
indent( indentLevel + 1 ); printf( "colors = {\n" );
for (Color color : mode.colors)
{
indent( indentLevel + 2 ); print( color ); puts(",");
}
indent( indentLevel + 1 ); printf( "};\n" );
indent( indentLevel ); printf( "},\n" );
}
void print( std::ostream & os, const Mode & mode, unsigned int indentLevel )
{
indent( os, indentLevel ); os << "["<<mode.idx<<"] = {\n";
indent( os, indentLevel + 1 ); os << "name = \""<<mode.name<<"\";\n";
indent( os, indentLevel + 1 ); os << "value = "<<mode.value<<";\n";
indent( os, indentLevel + 1 ); os << "flags = "<<modeFlagsToString( mode.flags )<<";\n";
indent( os, indentLevel + 1 ); os << "direction = "<<enumString( mode.direction )<<";\n";
indent( os, indentLevel + 1 ); os << "speed_min = "<<mode.speed_min<<";\n";
indent( os, indentLevel + 1 ); os << "speed_max = "<<mode.speed_max<<";\n";
indent( os, indentLevel + 1 ); os << "speed = "<<mode.speed<<";\n";
indent( os, indentLevel + 1 ); os << "brightness_min = "<<mode.brightness_min<<";\n";
indent( os, indentLevel + 1 ); os << "brightness_max = "<<mode.brightness_max<<";\n";
indent( os, indentLevel + 1 ); os << "brightness = "<<mode.brightness<<";\n";
indent( os, indentLevel + 1 ); os << "colors_min = "<<mode.colors_min<<";\n";
indent( os, indentLevel + 1 ); os << "colors_max = "<<mode.colors_max<<";\n";
indent( os, indentLevel + 1 ); os << "color_mode = "<<enumString( mode.color_mode )<<";\n";
indent( os, indentLevel + 1 ); os << "colors = {\n";
for (Color color : mode.colors)
{
indent( os, indentLevel + 2 ); os << color << ",\n";
}
indent( os, indentLevel + 1 ); os << "};\n";
indent( os, indentLevel ); os << "},\n";
}
//======================================================================================================================
// Device
Device::Device()
:
idx(),
type(),
name(),
vendor(),
description(),
version(),
serial(),
location(),
active_mode(),
modes(),
zones(),
leds(),
colors()
{}
size_t Device::calcSize( uint32_t protocolVersion ) const noexcept
{
size_t size = 0;
size += sizeof( type );
size += protocol::sizeofString( name );
size += protocol::sizeofString( vendor );
size += protocol::sizeofString( description );
size += protocol::sizeofString( version );
size += protocol::sizeofString( serial );
size += protocol::sizeofString( location );
size += sizeof( active_mode );
size += protocol::sizeofArray( modes, protocolVersion );
size += protocol::sizeofArray( zones, protocolVersion );
size += protocol::sizeofArray( leds, protocolVersion );
size += protocol::sizeofArray( colors );
return size;
}
void Device::serialize( BinaryOutputStream & stream, uint32_t protocolVersion ) const
{
/*Original code
stream << type; */
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
stream.writeLittleEndian(type);
//end patch
protocol::writeString( stream, name );
protocol::writeString( stream, vendor );
protocol::writeString( stream, description );
protocol::writeString( stream, version );
protocol::writeString( stream, serial );
protocol::writeString( stream, location );
/* Original code
stream << uint16_t( modes.size() ); // the size is not directly before the array, so it must be written manually
stream << active_mode;*/
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
stream.writeLittleEndian(uint16_t(modes.size())); // size manually
stream.writeLittleEndian(active_mode);
//end patch
for (const Mode & mode : modes)
{
mode.serialize( stream, protocolVersion );
}
protocol::writeArray( stream, zones, protocolVersion );
protocol::writeArray( stream, leds, protocolVersion );
protocol::writeArray( stream, colors );
}
bool Device::deserialize( BinaryInputStream & stream, uint32_t protocolVersion, uint32_t deviceIdx ) noexcept
{
// This hack with const casts allows us to restrict the user from changing attributes that are a static description
// and allow him to change only the parameters that are meant to be changed.
// fill in our metadata
unconst( idx ) = deviceIdx;
/* Original code
stream >> unconst( type ); */
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
stream.readLittleEndian(unconst(type));
//end patch
protocol::readString( stream, unconst( name ) );
protocol::readString( stream, unconst( vendor ) );
protocol::readString( stream, unconst( description ) );
protocol::readString( stream, unconst( version ) );
protocol::readString( stream, unconst( serial ) );
protocol::readString( stream, unconst( location ) );
/* Original code
uint16_t num_modes;
stream >> num_modes; // the size is not directly before the array, so it must be read manually
stream >> unconst( active_mode );*/
// [GDGameLights Patch] Patch update to use new BinaryStream.hpp
// Patch License Info
// MIT License
// Copyright (c) 2025 itslebi
uint16_t num_modes = 0;
stream.readLittleEndian(num_modes); // size manually
stream.readLittleEndian(unconst(active_mode));
//end patch
unconst( modes ).reserve( num_modes );
for (uint32_t modeIdx = 0; modeIdx < num_modes; ++modeIdx)
{
Mode mode;
if (!mode.deserialize( stream, protocolVersion, modeIdx, deviceIdx ))
return false;
unconst( modes ).emplace_back( move(mode) );
}
protocol::readArray( stream, unconst( zones ), protocolVersion, deviceIdx );
protocol::readArray( stream, unconst( leds ), protocolVersion, deviceIdx );
protocol::readArray( stream, unconst( colors ) );
// Let's tolerate invalid device classes in case the server adds some without increasing protocol version
//if (!isValidDeviceType( type ))
// stream.setFailed();
return !stream.failed();
}
void print( const Device & device, unsigned int indentLevel )
{
indent( indentLevel ); printf( "[%u] = {\n", device.idx );
indent( indentLevel + 1 ); printf( "name = \"%s\";\n", device.name.c_str() );
indent( indentLevel + 1 ); printf( "type = %s;\n", enumString( device.type ) );
indent( indentLevel + 1 ); printf( "vendor = \"%s\";\n", device.vendor.c_str() );
indent( indentLevel + 1 ); printf( "description = \"%s\";\n", device.description.c_str() );
indent( indentLevel + 1 ); printf( "version = \"%s\";\n", device.version.c_str() );
indent( indentLevel + 1 ); printf( "serial = \"%s\";\n", device.serial.c_str() );
indent( indentLevel + 1 ); printf( "location = \"%s\";\n", device.location.c_str() );
indent( indentLevel + 1 ); printf( "active_mode = %u;\n", device.active_mode );
indent( indentLevel + 1 ); printf( "modes = {\n" );
for (const Mode & mode : device.modes)
{
print( mode, indentLevel + 2 );
}
indent( indentLevel + 1 ); printf( "};\n" );
indent( indentLevel + 1 ); printf( "zones = {\n" );
for (const Zone & zone : device.zones)
{
print( zone, indentLevel + 2 );
}
indent( indentLevel + 1 ); printf( "};\n" );
indent( indentLevel + 1 ); printf( "leds = {\n" );
for (const LED & led : device.leds)
{
print( led, indentLevel + 2 );
}
indent( indentLevel + 1 ); printf( "};\n" );
indent( indentLevel + 1 ); printf( "colors = {\n" );
for (Color color : device.colors)
{
indent( indentLevel + 2 ); print( color ); puts(",");
}
indent( indentLevel + 1 ); printf( "};\n" );
indent( indentLevel ); printf( "},\n" );
}
void print( std::ostream & os, const Device & device, unsigned int indentLevel )
{
indent( os, indentLevel ); os << "["<<device.idx<<"] = {\n";
indent( os, indentLevel + 1 ); os << "name = \""<<device.name<<"\";\n";
indent( os, indentLevel + 1 ); os << "type = "<<enumString( device.type )<<";\n";
indent( os, indentLevel + 1 ); os << "vendor = \""<<device.vendor<<"\";\n";
indent( os, indentLevel + 1 ); os << "description = \""<<device.description<<"\";\n";
indent( os, indentLevel + 1 ); os << "version = \""<<device.version<<"\";\n";
indent( os, indentLevel + 1 ); os << "serial = \""<<device.serial<<"\";\n";
indent( os, indentLevel + 1 ); os << "location = \""<<device.location<<"\";\n";
indent( os, indentLevel + 1 ); os << "active_mode = "<<device.active_mode<<";\n";
indent( os, indentLevel + 1 ); os << "modes = {\n";
for (const Mode & mode : device.modes)
{
print( os, mode, indentLevel + 2 );
}
indent( os, indentLevel + 1 ); os << "};\n";
indent( os, indentLevel + 1 ); os << "zones = {\n";
for (const Zone & zone : device.zones)
{
print( os, zone, indentLevel + 2 );
}
indent( os, indentLevel + 1 ); os << "};\n";
indent( os, indentLevel + 1 ); os << "leds = {\n";
for (const LED & led : device.leds)
{
print( os, led, indentLevel + 2 );
}
indent( os, indentLevel + 1 ); os << "};\n";
indent( os, indentLevel + 1 ); os << "colors = {\n";
for (Color color : device.colors)
{
indent( os, indentLevel + 2 ); os << color << ",\n";
}
indent( os, indentLevel + 1 ); os << "};\n";
indent( os, indentLevel ); os << "},\n";
}
//======================================================================================================================
} // namespace orgb