-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropColorCompiler.cpp
More file actions
1595 lines (1330 loc) · 56.3 KB
/
PropColorCompiler.cpp
File metadata and controls
1595 lines (1330 loc) · 56.3 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
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
//========= Copyright Satjo Interactive 2025, All rights reserved. ============//
//
// Оригинальный код написан: Tirmiks
// Отдельные благодарности: ugo_zapad, Ambiabstract, MyCbEH
//
//=============================================================================//
#include "PropColorCompiler.h"
#include "colored_cout.h"
namespace fs = std::filesystem;
bool MDLFile::Load(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (!file)
{
std::cout << "Error: Cannot open file " << filename << std::endl;
return false;
}
file.seekg(0, std::ios::end);
size_t size = file.tellg();
file.seekg(0, std::ios::beg);
fileData.resize(size);
file.read(reinterpret_cast<char*>(fileData.data()), size);
file.close();
header = reinterpret_cast<StudioHdr*>(fileData.data());
int headerId = header->id;
if (headerId != 'TSDI' && headerId != 'IDST')
{
std::cout << "Error: Invalid MDL file signature!" << std::endl;
return false;
}
ParseTextures();
ParseTextureDirs();
ParseSkinFamilies();
ParseSurfaceProp();
ParseKeyValues();
return true;
}
bool MDLFile::Save(const std::string& filename)
{
if (newFileData.empty())
{
std::cout << "Error: No changes to save" << std::endl;
return false;
}
std::ofstream file(filename, std::ios::binary);
if (!file)
{
std::cout << "Error: Cannot create file " << filename << std::endl;
return false;
}
file.write(reinterpret_cast<char*>(newFileData.data()), newFileData.size());
file.close();
std::cout << "Successfully saved: " << filename << std::endl;
return true;
}
std::string MDLFile::ReadString(int offset)
{
if (offset < 0 || offset >= fileData.size())
return "";
const char* str = reinterpret_cast<const char*>(fileData.data() + offset);
return std::string(str);
}
int MDLFile::FindAlreadyEditedMarker()
{
return 0;
}
void MDLFile::ParseTextures()
{
textureNames.clear();
if (header->numtextures > 0 && header->textureindex > 0)
{
Texture* textures = reinterpret_cast<Texture*>(fileData.data() + header->textureindex);
for (int i = 0; i < header->numtextures; i++)
{
std::string name = ReadString(header->textureindex + i * sizeof(Texture) + textures[i].sznameindex);
textureNames.push_back(name);
}
}
}
void MDLFile::ParseTextureDirs()
{
textureDirs.clear();
if (header->numcdtextures > 0 && header->cdtextureindex > 0)
{
int* dirOffsets = reinterpret_cast<int*>(fileData.data() + header->cdtextureindex);
for (int i = 0; i < header->numcdtextures; i++)
{
std::string dir = ReadString(dirOffsets[i]);
textureDirs.push_back(dir);
}
}
}
void MDLFile::ParseSkinFamilies()
{
skinFamilies.clear();
if (header->numskinfamilies > 0 && header->skinindex > 0)
{
short* skins = reinterpret_cast<short*>(fileData.data() + header->skinindex);
int index = 0;
for (int i = 0; i < header->numskinfamilies; i++)
{
std::vector<short> family;
for (int j = 0; j < header->numskinref; j++)
{
family.push_back(skins[index++]);
}
skinFamilies.push_back(family);
}
}
}
void MDLFile::ParseSurfaceProp()
{
if (header->surfacepropindex > 0)
{
surfaceProp = ReadString(header->surfacepropindex);
}
}
void MDLFile::ParseKeyValues()
{
if (header->keyvalueindex > 0 && header->keyvaluesize > 0)
{
keyValues = std::string(reinterpret_cast<char*>(fileData.data() + header->keyvalueindex), header->keyvaluesize);
}
}
bool MDLFile::AddMaterial(const std::string& materialPath)
{
std::vector<std::string> newTextureNames = textureNames;
newTextureNames.push_back(materialPath);
std::vector<std::string> newTextureDirs = textureDirs;
size_t lastSlash = materialPath.find_last_of("/\\");
if (lastSlash != std::string::npos)
{
std::string dir = materialPath.substr(0, lastSlash);
if (std::find(newTextureDirs.begin(), newTextureDirs.end(), dir) == newTextureDirs.end())
{
newTextureDirs.push_back(dir);
}
}
RebuildMDLFile(newTextureNames, skinFamilies, newTextureDirs);
return true;
}
bool MDLFile::AddMaterialWithSkin(const std::string& materialPath)
{
std::vector<std::string> newTextureNames = textureNames;
newTextureNames.push_back(materialPath);
short newMaterialIndex = static_cast<short>(newTextureNames.size() - 1);
std::vector<std::string> newTextureDirs = textureDirs;
size_t lastSlash = materialPath.find_last_of("/\\");
if (lastSlash != std::string::npos)
{
std::string dir = materialPath.substr(0, lastSlash);
if (std::find(newTextureDirs.begin(), newTextureDirs.end(), dir) == newTextureDirs.end())
{
newTextureDirs.push_back(dir);
}
}
std::vector<std::vector<short>> newSkinFamilies = skinFamilies;
std::vector<short> newSkinFamily;
for (int i = 0; i < header->numskinref; i++)
{
newSkinFamily.push_back(newMaterialIndex);
}
newSkinFamilies.push_back(newSkinFamily);
RebuildMDLFile(newTextureNames, newSkinFamilies, newTextureDirs);
return true;
}
bool MDLFile::AddMultipleMaterialsWithSkins(const std::vector<std::string>& materialPaths)
{
std::vector<std::string> newTextureNames = textureNames;
std::vector<std::string> newTextureDirs = textureDirs;
for (const auto& materialPath : materialPaths)
{
newTextureNames.push_back(materialPath);
size_t lastSlash = materialPath.find_last_of("/\\");
if (lastSlash != std::string::npos)
{
std::string dir = materialPath.substr(0, lastSlash);
if (std::find(newTextureDirs.begin(), newTextureDirs.end(), dir) == newTextureDirs.end())
{
newTextureDirs.push_back(dir);
}
}
}
std::vector<std::vector<short>> newSkinFamilies = skinFamilies;
for (size_t i = 0; i < materialPaths.size(); i++)
{
short newMaterialIndex = static_cast<short>(textureNames.size() + i);
std::vector<short> newSkinFamily;
for (int j = 0; j < header->numskinref; j++)
{
newSkinFamily.push_back(newMaterialIndex);
}
newSkinFamilies.push_back(newSkinFamily);
}
RebuildMDLFile(newTextureNames, newSkinFamilies, newTextureDirs);
return true;
}
void MDLFile::RebuildMDLFile(const std::vector<std::string>& newTextureNames,
const std::vector<std::vector<short>>& newSkinFamilies,
const std::vector<std::string>& newTextureDirs)
{
int startOffset = static_cast<int>(fileData.size());
int newSize = startOffset;
newSize += static_cast<int>(newTextureNames.size() * sizeof(Texture));
for (const auto& name : newTextureNames)
{
newSize += static_cast<int>(name.length() + 1);
}
newSize += static_cast<int>(newTextureDirs.size() * sizeof(int));
for (const auto& dir : newTextureDirs)
{
newSize += static_cast<int>(dir.length() + 1);
}
int totalSkinIndices = 0;
for (const auto& family : newSkinFamilies)
{
totalSkinIndices += static_cast<int>(family.size());
}
newSize += totalSkinIndices * static_cast<int>(sizeof(short));
newSize += static_cast<int>(surfaceProp.length() + 1);
newSize += static_cast<int>(keyValues.length() + 1);
newFileData.resize(newSize);
memcpy(newFileData.data(), fileData.data(), fileData.size());
int currentOffset = startOffset;
int textureStructOffset = currentOffset;
int textureNameOffset = textureStructOffset + static_cast<int>(newTextureNames.size() * sizeof(Texture));
int textureDirOffset = textureNameOffset;
for (const auto& name : newTextureNames)
{
textureDirOffset += static_cast<int>(name.length() + 1);
}
int skinDataOffset = textureDirOffset + static_cast<int>(newTextureDirs.size() * sizeof(int));
for (const auto& dir : newTextureDirs)
{
skinDataOffset += static_cast<int>(dir.length() + 1);
}
int surfacePropOffset = skinDataOffset + totalSkinIndices * static_cast<int>(sizeof(short));
int keyValuesOffset = surfacePropOffset + static_cast<int>(surfaceProp.length() + 1);
currentOffset = textureStructOffset;
int nameCurrentOffset = 0;
for (size_t i = 0; i < newTextureNames.size(); i++)
{
Texture tex;
memset(&tex, 0, sizeof(Texture));
tex.sznameindex = textureNameOffset + nameCurrentOffset - (textureStructOffset + static_cast<int>(i * sizeof(Texture)));
tex.flags = 0;
tex.used = 1;
memcpy(newFileData.data() + currentOffset, &tex, sizeof(Texture));
currentOffset += static_cast<int>(sizeof(Texture));
nameCurrentOffset += static_cast<int>(newTextureNames[i].length() + 1);
}
currentOffset = textureNameOffset;
for (const auto& name : newTextureNames)
{
memcpy(newFileData.data() + currentOffset, name.c_str(), name.length());
currentOffset += static_cast<int>(name.length() + 1);
}
int dirNameOffset = textureDirOffset + static_cast<int>(newTextureDirs.size() * sizeof(int));
currentOffset = textureDirOffset;
int dirNameCurrentOffset = 0;
for (size_t i = 0; i < newTextureDirs.size(); i++)
{
int dirOffset = dirNameOffset + dirNameCurrentOffset;
memcpy(newFileData.data() + currentOffset, &dirOffset, sizeof(int));
currentOffset += static_cast<int>(sizeof(int));
dirNameCurrentOffset += static_cast<int>(newTextureDirs[i].length() + 1);
}
currentOffset = dirNameOffset;
for (const auto& dir : newTextureDirs)
{
memcpy(newFileData.data() + currentOffset, dir.c_str(), dir.length());
currentOffset += static_cast<int>(dir.length() + 1);
}
currentOffset = skinDataOffset;
for (const auto& family : newSkinFamilies)
{
for (short texIndex : family)
{
memcpy(newFileData.data() + currentOffset, &texIndex, sizeof(short));
currentOffset += static_cast<int>(sizeof(short));
}
}
currentOffset = surfacePropOffset;
memcpy(newFileData.data() + currentOffset, surfaceProp.c_str(), surfaceProp.length());
currentOffset += static_cast<int>(surfaceProp.length() + 1);
currentOffset = keyValuesOffset;
memcpy(newFileData.data() + currentOffset, keyValues.c_str(), keyValues.length());
currentOffset += static_cast<int>(keyValues.length() + 1);
StudioHdr* newHeader = reinterpret_cast<StudioHdr*>(newFileData.data());
newHeader->length = newSize;
newHeader->numtextures = static_cast<int>(newTextureNames.size());
newHeader->textureindex = textureStructOffset;
newHeader->numcdtextures = static_cast<int>(newTextureDirs.size());
newHeader->cdtextureindex = textureDirOffset;
newHeader->skinindex = skinDataOffset;
newHeader->numskinfamilies = static_cast<int>(newSkinFamilies.size());
if (!newSkinFamilies.empty())
{
newHeader->numskinref = static_cast<int>(newSkinFamilies[0].size());
}
newHeader->surfacepropindex = surfacePropOffset;
newHeader->keyvalueindex = keyValuesOffset;
newHeader->keyvaluesize = static_cast<int>(keyValues.length());
}
HammerCompiler::HammerCompiler(const std::string& vmfPath, const std::string& gameDir)
: vmfPath(vmfPath), gameDir(gameDir) {
}
bool HammerCompiler::ProcessVMF()
{
std::cout << "Processing VMF file: " << vmfPath << std::endl;
if (!ParseVMF())
{
std::cout << "Failed to parse VMF file" << std::endl;
return false;
}
if (!ProcessModels())
{
std::cout << "Failed to process models" << std::endl;
return false;
}
if (!UpdateVMF())
{
std::cout << "Failed to update VMF file" << std::endl;
return false;
}
std::cout << "VMF processing completed successfully!" << std::endl;
return true;
}
bool HammerCompiler::ParseVMF()
{
std::string content = ReadFileContent(vmfPath);
if (content.empty())
{
return false;
}
size_t pos = 0;
while (pos < content.length())
{
size_t entityStart = content.find("entity", pos);
if (entityStart == std::string::npos)
break;
size_t braceStart = content.find("{", entityStart);
if (braceStart == std::string::npos)
break;
int braceCount = 1;
size_t currentPos = braceStart + 1;
while (braceCount > 0 && currentPos < content.length())
{
if (content[currentPos] == '{')
braceCount++;
else if (content[currentPos] == '}')
braceCount--;
currentPos++;
}
if (braceCount == 0)
{
std::string entityContent = content.substr(entityStart, currentPos - entityStart);
EntityInfo entity;
entity.startPos = entityStart;
entity.endPos = currentPos;
size_t classnamePos = entityContent.find("\"classname\"");
if (classnamePos != std::string::npos)
{
size_t valueStart = entityContent.find("\"", classnamePos + 12);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
if (valueStart != std::string::npos && valueEnd != std::string::npos)
{
std::string classname = entityContent.substr(valueStart + 1, valueEnd - valueStart - 1);
if (classname == "prop_static")
{
size_t modelPos = entityContent.find("\"model\"");
if (modelPos != std::string::npos)
{
size_t valueStart = entityContent.find("\"", modelPos + 8);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
if (valueStart != std::string::npos && valueEnd != std::string::npos)
{
entity.model = entityContent.substr(valueStart + 1, valueEnd - valueStart - 1);
}
}
size_t colorPos = entityContent.find("\"rendercolor\"");
if (colorPos != std::string::npos)
{
size_t valueStart = entityContent.find("\"", colorPos + 14);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
if (valueStart != std::string::npos && valueEnd != std::string::npos)
{
entity.rendercolor = entityContent.substr(valueStart + 1, valueEnd - valueStart - 1);
}
}
size_t skinPos = entityContent.find("\"skin\"");
if (skinPos != std::string::npos) {
size_t valueStart = entityContent.find("\"", skinPos + 6);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
if (valueStart != std::string::npos && valueEnd != std::string::npos) {
entity.skin = std::stoi(entityContent.substr(valueStart + 1, valueEnd - valueStart - 1));
}
}
if (!entity.model.empty() && !entity.rendercolor.empty() && entity.rendercolor != "255 255 255")
{
entities.push_back(entity);
modelData[entity.model].colors.insert(entity.rendercolor);
modelData[entity.model].entities.push_back(&entities.back());
}
}
}
}
pos = currentPos;
}
else
{
break;
}
}
std::cout << "Found " << modelData.size() << " models with custom colors" << std::endl;
return true;
}
bool HammerCompiler::ProcessModels()
{
std::map<std::string, std::map<std::string, std::set<std::string>>> textureUsage;
for (const auto& [modelPath, colorInfo] : modelData)
{
std::string fullModelPath = gameDir + "/" + modelPath;
if (!CopyModelFiles(modelPath))
{
std::cout << "Failed to copy model files for: " << modelPath << std::endl;
continue;
}
MDLFile mdl;
if (mdl.Load(fullModelPath))
{
std::vector<std::string> textureNames = mdl.GetTextureNames();
if (!textureNames.empty())
{
std::string baseTexture = textureNames[0];
textureUsage[baseTexture][modelPath] = colorInfo.colors;
}
}
}
for (const auto& [texturePath, modelColors] : textureUsage)
{
std::cout << "Processing texture: " << texturePath << " used by " << modelColors.size() << " models" << std::endl;
// Читаем оригинальный VMT файл
std::string baseTexturePath = texturePath;
size_t textureDotPos = baseTexturePath.find_last_of('.');
if (textureDotPos != std::string::npos)
{
baseTexturePath = baseTexturePath.substr(0, textureDotPos);
}
std::string originalVmtPath = gameDir + "/materials/" + baseTexturePath + ".vmt";
std::string originalVmtContent = ReadFileContent(originalVmtPath);
if (originalVmtContent.empty())
{
std::cout << "Warning: Original VMT file not found or empty: " << originalVmtPath << std::endl;
continue;
}
for (const auto& [modelPath, colors] : modelColors)
{
fs::path modelFilePath(modelPath);
std::string modelName = modelFilePath.stem().string();
std::string uniqueSuffix = "_" + modelName;
std::cout << "Creating materials for model: " << modelPath << " with " << colors.size() << " colors" << std::endl;
std::vector<std::string> materialPaths;
std::vector<std::string> orderedColors(colors.begin(), colors.end());
for (size_t i = 0; i < orderedColors.size(); i++)
{
std::string newBaseTexture = baseTexturePath + uniqueSuffix + "_color" + std::to_string(i + 1);
std::string vmtContent = CreateVMTContent(originalVmtContent, newBaseTexture, orderedColors[i]);
std::string vmtPath = gameDir + "/materials/" + newBaseTexture + ".vmt";
std::cout << "Creating VMT: " << vmtPath << " with color " << orderedColors[i] << std::endl;
if (!WriteFileContent(vmtPath, vmtContent))
{
std::cout << "Failed to write VMT file: " << vmtPath << std::endl;
continue;
}
materialPaths.push_back(newBaseTexture);
AddCreatedFile(vmtPath);
}
std::string fullModelPath = gameDir + "/" + modelPath;
fs::path modelFilePathObj(fullModelPath);
std::string coloredModelPath = (modelFilePathObj.parent_path() / (modelFilePathObj.stem().string() + "_colored.mdl")).string();
MDLFile mdl;
if (mdl.Load(coloredModelPath))
{
if (!mdl.AddMultipleMaterialsWithSkins(materialPaths))
{
std::cout << "Failed to add materials to model: " << coloredModelPath << std::endl;
}
else
{
mdl.Save(coloredModelPath);
}
}
}
}
return true;
}
bool HammerCompiler::CopyModelFiles(const std::string& originalModelPath)
{
std::string fullModelPath = gameDir + "/" + originalModelPath;
std::cout << "Looking for model: " << fullModelPath << std::endl;
if (!fs::exists(fullModelPath))
{
std::cout << "Model file not found: " << fullModelPath << std::endl;
return false;
}
fs::path modelPath(fullModelPath);
std::string baseName = modelPath.stem().string();
std::string extension = modelPath.extension().string();
fs::path parentDir = modelPath.parent_path();
std::string coloredBaseName = baseName + "_colored";
fs::path coloredModelPath = parentDir / (coloredBaseName + extension);
std::cout << "Base name: " << baseName << std::endl;
std::cout << "Colored base name: " << coloredBaseName << std::endl;
std::cout << "Colored model path: " << coloredModelPath << std::endl;
if (fs::exists(coloredModelPath))
{
std::cout << "Colored model already exists: " << coloredModelPath << std::endl;
AddCreatedFile(coloredModelPath.string());
return true;
}
try
{
fs::copy_file(fullModelPath, coloredModelPath, fs::copy_options::overwrite_existing);
std::cout << "Copied MDL file to: " << coloredModelPath << std::endl;
AddCreatedFile(coloredModelPath.string());
}
catch (const std::exception& e)
{
std::cout << "Failed to copy MDL file: " << e.what() << std::endl;
return false;
}
std::vector<std::string> extensions = { ".dx90.vtx", ".vvd", ".phy" }; // добавить остальные, если потребуется (пропы из CS:GO имеют лишь это)
for (const auto& ext : extensions)
{
fs::path originalFile = parentDir / (baseName + ext);
fs::path coloredFile = parentDir / (coloredBaseName + ext);
std::cout << "Looking for: " << originalFile << std::endl;
if (fs::exists(originalFile))
{
try
{
fs::copy_file(originalFile, coloredFile, fs::copy_options::overwrite_existing);
std::cout << "Copied file: " << originalFile << " to " << coloredFile << std::endl;
AddCreatedFile(coloredFile.string());
}
catch (const std::exception& e)
{
std::cout << "Failed to copy file: " << originalFile << " - " << e.what() << std::endl;
}
}
else
{
std::cout << "File not found (this may be normal): " << originalFile << std::endl;
}
}
return true;
}
std::string HammerCompiler::CreateVMTContent(const std::string& originalContent, const std::string& newBaseTexture, const std::string& color)
{
std::istringstream iss(originalContent);
std::ostringstream oss;
std::string line;
bool hasColor2 = false;
bool hasBlendTint = false;
bool hasBaseTexture = false;
// Извлекаем оригинальное имя текстуры из оригинального VMT
std::string originalBaseTexture;
std::istringstream issTemp(originalContent);
std::string tempLine;
while (std::getline(issTemp, tempLine)) {
std::string trimmedLine = tempLine;
trimmedLine.erase(0, trimmedLine.find_first_not_of(" \t"));
trimmedLine.erase(trimmedLine.find_last_not_of(" \t") + 1);
if (trimmedLine.find("\"$basetexture\"") == 0) {
size_t firstQuote = trimmedLine.find('"', 14);
size_t secondQuote = trimmedLine.find('"', firstQuote + 1);
if (firstQuote != std::string::npos && secondQuote != std::string::npos) {
originalBaseTexture = trimmedLine.substr(firstQuote + 1, secondQuote - firstQuote - 1);
break;
}
}
}
if (originalBaseTexture.empty()) {
originalBaseTexture = newBaseTexture;
size_t colorPos = originalBaseTexture.find("_color");
if (colorPos != std::string::npos) {
originalBaseTexture = originalBaseTexture.substr(0, colorPos);
}
size_t modelSuffixPos = originalBaseTexture.find_last_of('_');
if (modelSuffixPos != std::string::npos) {
std::string possibleSuffix = originalBaseTexture.substr(modelSuffixPos);
if (possibleSuffix.find("hr_") == std::string::npos &&
possibleSuffix.find("lr_") == std::string::npos) {
originalBaseTexture = originalBaseTexture.substr(0, modelSuffixPos);
}
}
}
iss.clear();
iss.str(originalContent);
while (std::getline(iss, line))
{
std::string trimmedLine = line;
trimmedLine.erase(0, trimmedLine.find_first_not_of(" \t"));
trimmedLine.erase(trimmedLine.find_last_not_of(" \t") + 1);
if (trimmedLine.find("\"$basetexture\"") == 0) {
size_t firstQuote = trimmedLine.find('"', 14);
size_t secondQuote = trimmedLine.find('"', firstQuote + 1);
if (firstQuote != std::string::npos && secondQuote != std::string::npos) {
line = "\t\"$basetexture\" \"" + originalBaseTexture + "\"";
}
hasBaseTexture = true;
}
else if (trimmedLine.find("\"$color2\"") == 0)
{
size_t firstBrace = trimmedLine.find('{');
size_t secondBrace = trimmedLine.find('}');
if (firstBrace != std::string::npos && secondBrace != std::string::npos)
{
line = "\t\"$color2\" \"{" + color + "}\"";
}
hasColor2 = true;
}
else if (trimmedLine.find("\"$blendtintbybasealpha\"") == 0)
{
size_t firstQuote = trimmedLine.find('"', 24);
size_t secondQuote = trimmedLine.find('"', firstQuote + 1);
if (firstQuote != std::string::npos && secondQuote != std::string::npos)
{
line = "\t\"$blendtintbybasealpha\" \"1\"";
}
hasBlendTint = true;
}
oss << line << "\n";
}
std::string result = oss.str();
if (!hasBaseTexture) {
size_t lastBrace = result.rfind('}');
if (lastBrace != std::string::npos) {
result.insert(lastBrace, "\n\t\"$basetexture\" \"" + originalBaseTexture + "\"");
}
}
if (!hasColor2)
{
size_t lastBrace = result.rfind('}');
if (lastBrace != std::string::npos)
{
result.insert(lastBrace, "\n\t\"$color2\" \"{" + color + "}\"");
}
}
if (!hasBlendTint)
{
size_t lastBrace = result.rfind('}');
if (lastBrace != std::string::npos)
{
result.insert(lastBrace, "\n\t\"$blendtintbybasealpha\" \"1\"");
}
}
return result;
}
bool HammerCompiler::UpdateVMF() {
std::cout << "Updating VMF file..." << std::endl;
std::string content = ReadFileContent(vmfPath);
if (content.empty())
{
return false;
}
std::cout << "Original VMF content size: " << content.length() << " bytes" << std::endl;
std::string tempOutputPath = vmfPath + ".tmp_colored";
std::vector<EntityInfo*> sortedEntities;
for (auto& entity : entities)
{
sortedEntities.push_back(&entity);
}
std::sort(sortedEntities.begin(), sortedEntities.end(),
[](const EntityInfo* a, const EntityInfo* b) { return a->startPos > b->startPos; });
std::cout << "Processing " << sortedEntities.size() << " entities for update" << std::endl;
bool hasChanges = false;
for (auto* entity : sortedEntities)
{
if (modelData.find(entity->model) == modelData.end())
continue;
const auto& colorInfo = modelData[entity->model];
std::string entityContent = content.substr(entity->startPos, entity->endPos - entity->startPos);
std::cout << "Updating entity with model: " << entity->model << " and color: " << entity->rendercolor << std::endl;
std::vector<std::string> orderedColors(colorInfo.colors.begin(), colorInfo.colors.end());
int colorIndex = -1;
for (size_t i = 0; i < orderedColors.size(); i++)
{
if (orderedColors[i] == entity->rendercolor)
{
colorIndex = static_cast<int>(i) + 1;
break;
}
}
if (colorIndex == -1)
{
std::cout << "Error: Could not find color index for: " << entity->rendercolor << std::endl;
continue;
}
std::cout << "Color index: " << colorIndex << std::endl;
size_t modelPos = entityContent.find("\"model\"");
if (modelPos != std::string::npos)
{
size_t valueStart = entityContent.find("\"", modelPos + 8);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
if (valueStart != std::string::npos && valueEnd != std::string::npos)
{
std::string newModelPath = entity->model;
size_t dotPos = newModelPath.find_last_of('.');
newModelPath = newModelPath.substr(0, dotPos) + "_colored.mdl";
entityContent.replace(valueStart + 1, valueEnd - valueStart - 1, newModelPath);
std::cout << "Updated model path to: " << newModelPath << std::endl;
}
}
size_t skinPos = entityContent.find("\"skin\"");
if (skinPos != std::string::npos)
{
size_t valueStart = entityContent.find("\"", skinPos + 7);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
if (valueStart != std::string::npos && valueEnd != std::string::npos)
{
entityContent.replace(valueStart + 1, valueEnd - valueStart - 1, std::to_string(colorIndex));
std::cout << "Updated skin to: " << colorIndex << std::endl;
}
}
else
{
size_t insertPos = entityContent.find("\"model\"");
if (insertPos != std::string::npos)
{
size_t lineEnd = entityContent.find("\n", insertPos);
if (lineEnd != std::string::npos)
{
entityContent.insert(lineEnd + 1, "\t\"skin\" \"" + std::to_string(colorIndex) + "\"\n");
std::cout << "Added skin parameter: " << colorIndex << std::endl;
}
}
}
size_t colorPos = entityContent.find("\"rendercolor\"");
if (colorPos != std::string::npos)
{
size_t valueStart = entityContent.find("\"", colorPos + 14);
size_t valueEnd = entityContent.find("\"", valueStart + 1);
std::string originalColor;
if (valueStart != std::string::npos && valueEnd != std::string::npos) {
originalColor = entityContent.substr(valueStart + 1, valueEnd - valueStart - 1);
}
size_t lineStart = entityContent.rfind("\n", colorPos);
if (lineStart == std::string::npos) lineStart = 0;
size_t lineEnd = entityContent.find("\n", colorPos);
if (lineEnd != std::string::npos)
{
entityContent.erase(lineStart, lineEnd - lineStart + 1);
std::cout << "Removed rendercolor parameter" << std::endl;
}
if (!originalColor.empty()) {
size_t insertPos = entityContent.find("\"model\"");
if (insertPos != std::string::npos)
{
size_t lineEnd = entityContent.find("\n", insertPos);
if (lineEnd != std::string::npos)
{
entityContent.insert(lineEnd + 1, "\t\"$color\" \"" + originalColor + "\"\n");
std::cout << "Added $color marker for post-compile: " << originalColor << std::endl;
}
}
}
}
content.replace(entity->startPos, entity->endPos - entity->startPos, entityContent);
hasChanges = true;
}
if (!hasChanges)
{
std::cout << "No changes to save in VMF" << std::endl;
return true;
}
std::cout << "Writing updated VMF content..." << std::endl;
if (!WriteFileContent(tempOutputPath, content))
{
std::cout << "Failed to write temporary VMF file" << std::endl;
return false;
}
try
{
fs::remove(vmfPath);
fs::rename(tempOutputPath, vmfPath);
std::cout << "Successfully updated VMF file: " << vmfPath << std::endl;
}
catch (const std::exception& e)
{
std::cout << "Failed to replace original VMF file: " << e.what() << std::endl;
return false;
}
return true;
}
std::string HammerCompiler::ReadFileContent(const std::string& path)
{
std::ifstream file(path);
if (!file.is_open())
{
std::cout << "Failed to open file: " << path << std::endl;
return "";
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return content;
}
bool HammerCompiler::WriteFileContent(const std::string& path, const std::string& content)
{
fs::path filePath(path);
fs::create_directories(filePath.parent_path());
std::ofstream file(path);
if (!file.is_open())
{
std::cout << "Failed to create file: " << path << std::endl;
return false;
}
file << content;
file.close();
std::cout << "Successfully wrote file: " << path << " (" << content.length() << " bytes)" << std::endl;
return true;
}
void HammerCompiler::AddCreatedFile(const std::string& filePath)
{
if (!filePath.empty() && fs::exists(filePath)) {
createdFiles.push_back(filePath);
}
}
bool HammerCompiler::SaveCreatedFilesList()
{
std::string listPath = this->vmfPath + ".created_files.txt";
std::ofstream file(listPath);
if (!file.is_open()) {
std::cout << "Failed to create files list: " << listPath << std::endl;
return false;
}