-
Notifications
You must be signed in to change notification settings - Fork 2k
/
RuntimeAssetNatives.cpp
1541 lines (1210 loc) · 38.1 KB
/
RuntimeAssetNatives.cpp
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "StdInc.h"
#include <Hooking.h>
#include <Streaming.h>
#include <EntitySystem.h>
#include <scrBind.h>
#include <CrossBuildRuntime.h>
#define RAGE_FORMATS_GAME five
#define RAGE_FORMATS_GAME_FIVE
#define RAGE_FORMATS_IN_GAME
#include <gtaDrawable.h>
#include <Resource.h>
#include <fxScripting.h>
#include <VFSManager.h>
#include <VFSWin32.h>
#include <grcTexture.h>
#include <RageParser.h>
#include <wrl.h>
#include <wincodec.h>
#include <shlwapi.h>
#include <botan/base64.h>
#include <nutsnbolts.h>
#define WANT_CEF_INTERNALS
#include <CefOverlay.h>
#include <NetLibrary.h>
#include <ResourceCacheDevice.h>
#include <ResourceManager.h>
#include <json.hpp>
#include <atPool.h>
#include <skyr/url.hpp>
#include <concurrent_unordered_set.h>
using Microsoft::WRL::ComPtr;
static hook::cdecl_stub<rage::five::pgDictionary<rage::grcTexture>*(void*, int)> textureDictionaryCtor([]()
{
return hook::get_call(hook::get_pattern("E8 ? ? ? ? 48 8B F8 EB 02 33 FF 4C 8D 3D"));
});
class RuntimeTex
{
public:
RuntimeTex(const char* name, int width, int height);
RuntimeTex(rage::grcTexture* texture, const void* data, size_t size);
RuntimeTex(rage::grcTexture* texture, bool owned = true);
virtual ~RuntimeTex();
int GetWidth();
int GetHeight();
int GetPitch();
void SetPixel(int x, int y, int r, int g, int b, int a);
bool SetPixelData(const void* data, size_t length);
bool LoadImage(const char* fileName);
void Commit();
inline void SetReferenceData(fwRefContainer<fwRefCountable> reference)
{
m_reference = reference;
}
inline rage::grcTexture* GetTexture()
{
return m_texture;
}
void SetTexture(rage::grcTexture* texture);
private:
rage::grcTexture* m_texture = nullptr;
fwRefContainer<fwRefCountable> m_reference;
int m_pitch = 0;
std::vector<uint8_t> m_backingPixels;
bool m_owned = false;
};
class RuntimeTxd
{
public:
RuntimeTxd(const char* name);
RuntimeTex* CreateTexture(const char* name, int width, int height);
RuntimeTex* CreateTextureFromImage(const char* name, const char* fileName);
RuntimeTex* CreateTextureFromDui(const char* name, const char* duiHandle);
private:
void EnsureTxd();
private:
uint32_t m_txdIndex = -1;
std::string m_name;
std::unordered_map<std::string, std::shared_ptr<RuntimeTex>> m_textures;
rage::five::pgDictionary<rage::grcTexture>* m_txd = nullptr;
};
RuntimeTex::RuntimeTex(const char* name, int width, int height)
: m_owned(true)
{
rage::grcManualTextureDef textureDef;
memset(&textureDef, 0, sizeof(textureDef));
textureDef.isStaging = 1;
textureDef.usage = 1;
textureDef.arraySize = 1;
m_texture = rage::grcTextureFactory::getInstance()->createManualTexture(width, height, 2, nullptr, true, &textureDef);
rage::grcLockedTexture lockedTexture;
if (m_texture->Map(0, 0, &lockedTexture, rage::grcLockFlags::WriteDiscard))
{
memset(lockedTexture.pBits, 0, lockedTexture.pitch * lockedTexture.height);
m_backingPixels.resize(lockedTexture.pitch * lockedTexture.height);
m_pitch = lockedTexture.pitch;
m_texture->Unmap(&lockedTexture);
}
}
RuntimeTex::RuntimeTex(rage::grcTexture* texture, const void* data, size_t size)
: m_texture(texture), m_owned(true)
{
m_backingPixels.resize(size);
memcpy(&m_backingPixels[0], data, m_backingPixels.size());
}
RuntimeTex::RuntimeTex(rage::grcTexture* texture, bool owned)
: m_texture(texture), m_owned(owned)
{
m_backingPixels.resize(0);
}
RuntimeTex::~RuntimeTex()
{
if (m_owned)
{
delete m_texture;
m_texture = nullptr;
}
}
int RuntimeTex::GetWidth()
{
return m_texture ? m_texture->GetWidth() : 0;
}
int RuntimeTex::GetHeight()
{
return m_texture ? m_texture->GetHeight() : 0;
}
int RuntimeTex::GetPitch()
{
return m_pitch;
}
void RuntimeTex::SetTexture(rage::grcTexture* texture)
{
if (!m_texture)
{
m_texture = texture;
}
}
void RuntimeTex::SetPixel(int x, int y, int r, int g, int b, int a)
{
auto offset = (y * m_pitch) + (x * 4);
if (offset < 0 || offset >= m_backingPixels.size() - 4)
{
return;
}
auto start = &m_backingPixels[offset];
start[3] = a;
start[2] = r;
start[1] = g;
start[0] = b;
}
bool RuntimeTex::SetPixelData(const void* data, size_t length)
{
if (length != m_backingPixels.size())
{
return false;
}
if (!m_texture)
{
return false;
}
rage::grcLockedTexture lockedTexture;
if (m_texture->Map(0, 0, &lockedTexture, rage::grcLockFlags::WriteDiscard))
{
memcpy(lockedTexture.pBits, data, length);
memcpy(m_backingPixels.data(), data, length);
m_texture->Unmap(&lockedTexture);
}
return true;
}
void RuntimeTex::Commit()
{
rage::grcLockedTexture lockedTexture;
if (m_texture && m_texture->Map(0, 0, &lockedTexture, rage::grcLockFlags::WriteDiscard))
{
memcpy(lockedTexture.pBits, m_backingPixels.data(), m_backingPixels.size());
m_texture->Unmap(&lockedTexture);
}
}
RuntimeTxd::RuntimeTxd(const char* name)
: m_name(name)
{
EnsureTxd();
}
void RuntimeTxd::EnsureTxd()
{
streaming::Manager* streaming = streaming::Manager::GetInstance();
auto txdStore = streaming->moduleMgr.GetStreamingModule("ytd");
txdStore->FindSlotFromHashKey(&m_txdIndex, m_name.c_str());
if (m_txdIndex != 0xFFFFFFFF)
{
auto& entry = streaming->Entries[txdStore->baseIdx + m_txdIndex];
if (!entry.handle)
{
void* memoryStub = rage::GetAllocator()->Allocate(sizeof(rage::five::pgDictionary<rage::grcTexture>), 16, 0);
m_txd = textureDictionaryCtor(memoryStub, 1);
streaming::strAssetReference ref;
ref.asset = m_txd;
txdStore->SetResource(m_txdIndex, ref);
entry.flags = (512 << 8) | 1;
entry.flags |= (0x20000000); // SetDoNotDefrag
txdStore->AddRef(m_txdIndex);
}
}
}
RuntimeTex* RuntimeTxd::CreateTexture(const char* name, int width, int height)
{
if (!m_txd)
{
return nullptr;
}
if (m_textures.find(name) != m_textures.end())
{
return nullptr;
}
if (width <= 0 || width > 8192)
{
return nullptr;
}
if (height <= 0 || height > 8192)
{
return nullptr;
}
auto tex = std::make_shared<RuntimeTex>(name, width, height);
m_txd->Add(name, tex->GetTexture());
m_textures[name] = tex;
scrBindAddSafePointer(tex.get());
return tex.get();
}
extern void TextureReplacement_OnTextureCreate(const std::string& txd, const std::string& txn);
// TODO: we need a 'common' place for this
static std::mutex nextFrameLock;
static std::queue<std::function<void()>> nextFrameQueue;
static void OnNextMainFrame(std::function<void()>&& fn)
{
std::unique_lock _(nextFrameLock);
nextFrameQueue.push(std::move(fn));
}
RuntimeTex* RuntimeTxd::CreateTextureFromDui(const char* name, const char* duiHandle)
{
if (!m_txd)
{
return nullptr;
}
if (m_textures.find(name) != m_textures.end())
{
return nullptr;
}
auto texture = nui::GetWindowTexture(duiHandle);
auto tex = std::make_shared<RuntimeTex>(nullptr, false);
tex->SetReferenceData(texture);
texture->WithHostTexture([this, name = std::string{ name }, tex](void* hostTexture)
{
auto texture = (rage::grcTexture*)hostTexture;
tex->SetTexture(texture);
m_txd->Add(HashString(name), tex->GetTexture());
OnNextMainFrame([this, name = std::move(name)]()
{
TextureReplacement_OnTextureCreate(m_name, name);
});
});
m_textures[name] = tex;
scrBindAddSafePointer(tex.get());
return tex.get();
}
#pragma comment(lib, "windowscodecs.lib")
static ComPtr<IWICImagingFactory> g_imagingFactory;
static ComPtr<IWICBitmapSource> ImageToBitmapSource(std::string_view fileName)
{
if (!g_imagingFactory)
{
HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory1, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory), (void**)g_imagingFactory.GetAddressOf());
}
ComPtr<IWICBitmapSource> source;
ComPtr<IStream> stream;
std::string fileNameString(fileName);
if (fileNameString.find("data:") == 0)
{
auto f = fileNameString.find("base64,");
if (f == std::string::npos)
{
return {};
}
fileNameString = fileNameString.substr(f + 7);
std::string decodedURL;
UrlDecode(fileNameString, decodedURL, false);
decodedURL.erase(std::remove_if(decodedURL.begin(), decodedURL.end(), [](char c)
{
return std::isspace<char>(c, std::locale::classic());
}),
decodedURL.end());
size_t length = decodedURL.length();
size_t paddingNeeded = 4 - (length % 4);
if ((paddingNeeded == 1 || paddingNeeded == 2) && decodedURL[length - 1] != '=')
{
decodedURL.resize(length + paddingNeeded, '=');
}
auto imageData = Botan::base64_decode(decodedURL, false);
stream = SHCreateMemStream(imageData.data(), imageData.size());
}
else
{
fx::OMPtr<IScriptRuntime> runtime;
if (!FX_SUCCEEDED(fx::GetCurrentScriptRuntime(&runtime)))
{
return {};
}
fx::Resource* resource = reinterpret_cast<fx::Resource*>(runtime->GetParentObject());
stream = vfs::CreateComStream(vfs::OpenRead(fmt::sprintf("%s/%s", resource->GetPath(), fileName)));
}
ComPtr<IWICBitmapDecoder> decoder;
HRESULT hr = g_imagingFactory->CreateDecoderFromStream(stream.Get(), nullptr, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
if (SUCCEEDED(hr))
{
ComPtr<IWICBitmapFrameDecode> frame;
hr = decoder->GetFrame(0, frame.GetAddressOf());
if (SUCCEEDED(hr))
{
// try to convert to a pixel format we like
frame.As(&source);
}
}
return source;
}
RuntimeTex* RuntimeTxd::CreateTextureFromImage(const char* name, const char* fileName)
{
if (!m_txd)
{
return nullptr;
}
if (auto source = ImageToBitmapSource(fileName))
{
ComPtr<IWICBitmapSource> convertedSource;
UINT width = 0, height = 0;
source->GetSize(&width, &height);
// try to convert to a pixel format we like
HRESULT hr = WICConvertBitmapSource(GUID_WICPixelFormat32bppBGRA, source.Get(), convertedSource.GetAddressOf());
if (SUCCEEDED(hr))
{
source = convertedSource;
}
// create a pixel data buffer
std::unique_ptr<uint32_t[]> pixelData(new uint32_t[width * height]);
hr = source->CopyPixels(nullptr, width * 4, width * height * 4, reinterpret_cast<BYTE*>(pixelData.get()));
if (SUCCEEDED(hr))
{
rage::grcTextureReference reference;
memset(&reference, 0, sizeof(reference));
reference.width = width;
reference.height = height;
reference.depth = 1;
reference.stride = width * 4;
reference.format = 11; // should correspond to DXGI_FORMAT_B8G8R8A8_UNORM
reference.pixelData = (uint8_t*)pixelData.get();
auto tex = std::make_shared<RuntimeTex>(rage::grcTextureFactory::getInstance()->createImage(&reference, nullptr), pixelData.get(), width * height * 4);
m_txd->Add(name, tex->GetTexture());
m_textures[name] = tex;
scrBindAddSafePointer(tex.get());
return tex.get();
}
}
return nullptr;
}
bool RuntimeTex::LoadImage(const char* fileName)
{
if (!m_texture)
{
return false;
}
auto completion = [this](const ComPtr<IWICBitmapSource>& bitmapSource)
{
auto source = bitmapSource;
ComPtr<IWICBitmapSource> convertedSource;
// try to convert to a pixel format we like
HRESULT hr = WICConvertBitmapSource(GUID_WICPixelFormat32bppBGRA, source.Get(), convertedSource.GetAddressOf());
if (SUCCEEDED(hr))
{
source = convertedSource;
}
// set up the pixel data buffer
UINT width = 0, height = 0;
source->GetSize(&width, &height);
size_t length = (size_t(width) * size_t(height) * 4);
if (length == m_backingPixels.size())
{
if (SUCCEEDED(source->CopyPixels(nullptr, width * 4, width * height * 4, reinterpret_cast<BYTE*>(&m_backingPixels[0]))))
{
Commit();
return true;
}
}
return false;
};
if (auto source = ImageToBitmapSource(fileName))
{
UINT width = 0, height = 0;
source->GetSize(&width, &height);
if (width == m_texture->GetWidth() && height == m_texture->GetHeight())
{
return completion(source);
}
else
{
struct Item
{
ComPtr<IWICBitmapSource> bitmap;
int targetWidth;
int targetHeight;
decltype(completion) cb;
Item(ComPtr<IWICBitmapSource>&& bitmap, int targetWidth, int targetHeight, decltype(completion)&& cb)
: bitmap(std::move(bitmap)), targetWidth(targetWidth), targetHeight(targetHeight), cb(std::move(cb))
{
}
void Work()
{
ComPtr<IWICBitmapScaler> scaler;
if (SUCCEEDED(g_imagingFactory->CreateBitmapScaler(&scaler)))
{
if (SUCCEEDED(scaler->Initialize(
bitmap.Get(),
targetWidth,
targetHeight,
WICBitmapInterpolationModeFant)))
{
ComPtr<IWICBitmapSource> outBitmap;
scaler.As(&outBitmap);
OnNextMainFrame([cb = std::move(cb), bitmap = std::move(outBitmap)]()
{
cb(bitmap);
});
}
}
}
static DWORD WINAPI StaticWork(LPVOID arg)
{
auto self = static_cast<Item*>(arg);
self->Work();
delete self;
return 0;
}
};
auto workItem = new Item(std::move(source), m_texture->GetWidth(), m_texture->GetHeight(), std::move(completion));
QueueUserWorkItem(&Item::StaticWork, workItem, 0);
return true;
}
}
return false;
}
#define VFS_GET_RAGE_PAGE_FLAGS 0x20001
struct GetRagePageFlagsExtension
{
const char* fileName; // in
int version;
rage::ResourceFlags flags; // out
};
void DLL_IMPORT CfxCollection_AddStreamingFileByTag(const std::string& tag, const std::string& fileName, rage::ResourceFlags flags);
static hook::cdecl_stub<void(fwArchetype*)> registerArchetype([]()
{
return hook::get_pattern("48 8B D9 8A 49 60 80 F9", -11);
});
static void* MakeStructFromMsgPack(uint32_t hash, const std::map<std::string, msgpack::object>& data, void* old = nullptr, bool keep = false);
static bool FillStructure(rage::parStructure* structure, const std::function<bool(rage::parMember* member)>& fn)
{
if (structure->m_baseClass)
{
if (!FillStructure(structure->m_baseClass, fn))
{
return false;
}
}
for (auto& member : structure->m_members)
{
if (!fn(member))
{
return false;
}
}
return true;
}
template<typename T>
static bool SetValue(void* ptr, const msgpack::object& obj)
{
*(T*)ptr = obj.as<T>();
return true;
}
struct Vector2
{
float x;
float y;
Vector2(float x, float y, float z, float w)
: x(x), y(y)
{
}
};
struct Vector3f
{
float x;
float y;
float z;
Vector3f(float x, float y, float z, float w)
: x(x), y(y), z(z)
{
}
};
struct Vector4
{
float x;
float y;
float z;
float w;
Vector4(float x, float y, float z, float w)
: x(x), y(y), z(z), w(w)
{
}
};
template<typename T>
static bool SetVector(void* ptr, const msgpack::object& obj)
{
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
float w = 0.0f;
if (obj.type == msgpack::type::EXT)
{
if (obj.via.ext.type() == 20 && obj.via.ext.size == 8) // vector2
{
x = *(float*)(obj.via.ext.data() + 0);
y = *(float*)(obj.via.ext.data() + 4);
}
else if (obj.via.ext.type() == 21 && obj.via.ext.size == 12) // vector3
{
x = *(float*)(obj.via.ext.data() + 0);
y = *(float*)(obj.via.ext.data() + 4);
z = *(float*)(obj.via.ext.data() + 8);
}
else if ((obj.via.ext.type() == 22 || obj.via.ext.type() == 23) && obj.via.ext.size == 16) // vector4/quat
{
x = *(float*)(obj.via.ext.data() + 0);
y = *(float*)(obj.via.ext.data() + 4);
z = *(float*)(obj.via.ext.data() + 8);
w = *(float*)(obj.via.ext.data() + 12);
}
}
else if (obj.type == msgpack::type::ARRAY)
{
std::vector<float> floats = obj.as<std::vector<float>>();
if (floats.size() >= 2)
{
x = floats[0];
y = floats[1];
}
if (floats.size() >= 3)
{
z = floats[2];
}
if (floats.size() >= 4)
{
w = floats[3];
}
}
*(T*)ptr = T{ x, y, z, w };
return true;
}
static bool SetString(void* ptr, const msgpack::object& obj)
{
if (obj.type == msgpack::type::STR || obj.type == msgpack::type::BIN)
{
*(uint32_t*)ptr = HashString(obj.as<std::string>().c_str());
}
else if (obj.type == msgpack::type::POSITIVE_INTEGER || obj.type == msgpack::type::NEGATIVE_INTEGER)
{
*(uint32_t*)ptr = obj.as<int32_t>();
}
return true;
}
static bool SetValueEnum(void* ptr, const msgpack::object& obj, rage::parEnumDefinition* enum_)
{
uint32_t str = 0;
if (obj.type == msgpack::type::STR || obj.type == msgpack::type::BIN)
{
str = HashRageString(obj.as<std::string>().c_str());
}
else if (obj.type == msgpack::type::POSITIVE_INTEGER || obj.type == msgpack::type::NEGATIVE_INTEGER)
{
str = obj.as<int32_t>();
}
for (auto p = enum_->fields; p->index != 0xFFFFFFFF; p++)
{
if (p->hash == str)
{
*(uint32_t*)ptr = p->index;
return true;
}
}
return false;
}
static bool SetStruct(void* ptr, const msgpack::object& obj, rage::parMemberDefinition* member)
{
if (obj.type != msgpack::type::MAP)
{
return false;
}
auto structData = obj.as<std::map<std::string, msgpack::object>>();
if (member->structType == rage::parStructType::Inline)
{
MakeStructFromMsgPack(member->structure->m_nameHash, structData, ptr);
}
else
{
*(void**)ptr = MakeStructFromMsgPack(member->structure->m_nameHash, structData);
}
return true;
}
template<typename TIndex>
struct atArrayBase
{
void* offset;
TIndex count;
TIndex size;
atArrayBase(int count, int size)
{
offset = rage::GetAllocator()->allocate(count * size, 16, 0);
count = count;
size = count;
}
~atArrayBase()
{
if (offset)
{
rage::GetAllocator()->free(offset);
offset = nullptr;
}
}
};
using atArray_16 = atArrayBase<uint16_t>;
using atArray_32 = atArrayBase<uint32_t>;
static bool SetFromMsgPack(rage::parMember* memberBase, void* structVal, const msgpack::object& value);
static bool SetArray(void* ptr, const msgpack::object& value, rage::parMember* member)
{
if (value.type != msgpack::type::ARRAY)
{
return false;
}
// set up the array
auto arrayData = value.as<std::vector<msgpack::object>>();
char* arrayBase = nullptr;
uint32_t arrayElemSize = 0;
switch (member->m_definition->arrayType)
{
case rage::parArrayType::atArray:
*(atArray_16*)ptr = atArray_16{int(arrayData.size()), int(member->m_definition->arrayElemSize)};
arrayBase = (char*)(((atArray_16*)ptr)->offset);
arrayElemSize = member->m_definition->arrayElemSize;
break;
case rage::parArrayType::FixedTrailingCount:
trace("Unhandled array type: FixedTrailingCount\n");
return false;
case rage::parArrayType::Fixed:
arrayBase = (char*)ptr;
arrayElemSize = member->m_definition->arrayElemSize;
break;
case rage::parArrayType::FixedPointer:
trace("Unhandled array type: FixedPointer\n");
return false;
case rage::parArrayType::Fixed_2:
arrayBase = (char*)ptr;
arrayElemSize = member->m_definition->arrayElemSize;
break;
case rage::parArrayType::atArray32:
*(atArray_32*)ptr = atArray_32{ int(arrayData.size()), int(member->m_definition->arrayElemSize) };
arrayBase = (char*)(((atArray_32*)ptr)->offset);
arrayElemSize = member->m_definition->arrayElemSize;
break;
case rage::parArrayType::TrailerInt:
trace("Unhandled array type: TrailerInt\n");
return false;
case rage::parArrayType::TrailerByte:
trace("Unhandled array type: TrailerByte\n");
return false;
case rage::parArrayType::TrailerShort:
trace("Unhandled array type: TrailerShort\n");
return false;
default:
break;
}
size_t offset = 0;
for (const auto& entry : arrayData)
{
void* entryPtr = arrayBase + offset;
offset += arrayElemSize;
if (!SetFromMsgPack(member->m_arrayDefinition, entryPtr, entry))
{
return false;
}
}
return true;
}
void* MakeStructFromMsgPack(const char* structType, const std::map<std::string, msgpack::object>& data, void* old = nullptr, bool keep = false)
{
return MakeStructFromMsgPack(HashRageString(structType), data, old, keep);
}
static bool SetFromMsgPack(rage::parMember* memberBase, void* structVal, const msgpack::object& value)
{
auto member = memberBase->m_definition;
switch (member->type)
{
case rage::parMemberType::Float:
return SetValue<float>(structVal, value);
case rage::parMemberType::String:
return SetString(structVal, value);
case rage::parMemberType::Vector3:
return SetVector<Vector3f>(structVal, value);
case rage::parMemberType::Bool:
return SetValue<bool>(structVal, value);
case rage::parMemberType::Int8:
return SetValue<int8_t>(structVal, value);
case rage::parMemberType::UInt8:
return SetValue<uint8_t>(structVal, value);
case rage::parMemberType::Int16:
return SetValue<int16_t>(structVal, value);
case rage::parMemberType::UInt16:
return SetValue<uint16_t>(structVal, value);
case rage::parMemberType::Int32:
return SetValue<int32_t>(structVal, value);
case rage::parMemberType::UInt32:
return SetValue<uint32_t>(structVal, value);
case rage::parMemberType::Vector2:
return SetVector<Vector2>(structVal, value);
case rage::parMemberType::Vector4:
return SetVector<Vector4>(structVal, value);
case rage::parMemberType::Struct:
return SetStruct(structVal, value, member);
case rage::parMemberType::Array:
return SetArray(structVal, value, memberBase);
case rage::parMemberType::Enum:
return SetValueEnum(structVal, value, member->enumData);
case rage::parMemberType::Bitset:
break;
case rage::parMemberType::Map:
break;
case rage::parMemberType::Matrix4x3:
break;
case rage::parMemberType::Matrix4x4:
break;
case rage::parMemberType::Vector2_Padded:
return SetVector<Vector4>(structVal, value);
case rage::parMemberType::Vector3_Padded:
return SetVector<Vector4>(structVal, value);
case rage::parMemberType::Vector4_Padded:
return SetVector<Vector4>(structVal, value);
case rage::parMemberType::Matrix3x4:
break;
case rage::parMemberType::Matrix4x3_2:
break;
case rage::parMemberType::Matrix4x4_2:
break;
case rage::parMemberType::Vector1_Padded:
break;
case rage::parMemberType::Flag1_Padded:
break;
case rage::parMemberType::Flag4_Padded:
break;
case rage::parMemberType::Int32_64:
return SetValue<int64_t>(structVal, value);
case rage::parMemberType::Int32_U64:
return SetValue<uint64_t>(structVal, value);
case rage::parMemberType::Half:
trace("Unhandled parMemberType::Half\n");
break;
case rage::parMemberType::Int64:
return SetValue<int64_t>(structVal, value);
case rage::parMemberType::UInt64:
return SetValue<uint64_t>(structVal, value);
case rage::parMemberType::Double:
return SetValue<double>(structVal, value);
default:
break;
}
return true;
}
static void* MakeStructFromMsgPack(uint32_t hash, const std::map<std::string, msgpack::object>& data, void* old, bool keep)
{
std::string structTypeReal;
{
auto typeIt = data.find("__type");
if (typeIt != data.end())
{
structTypeReal = typeIt->second.as<std::string>();
}
}
auto structDef = rage::GetStructureDefinition(hash);