-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommandsparserold.pas
More file actions
1151 lines (1036 loc) · 37.9 KB
/
Copy pathcommandsparserold.pas
File metadata and controls
1151 lines (1036 loc) · 37.9 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
unit CommandsParser;
{$mode objfpc}{$H+}
interface
uses
ogf_parser, basedefs;
type
TSlotsContainer = class;
TSlotId = integer;
TTempBufferType = (TTempTypeNone, TTempTypeString);
{ TTempBuffer }
TTempBuffer = class
_cur_type:TTempBufferType;
_buffer_string:string;
public
constructor Create;
destructor Destroy; override;
procedure Clear();
function GetCurrentType():TTempBufferType;
function GetString(var outstr:string):boolean;
procedure SetString(s:string);
end;
{ TModelSlot }
TModelSlot = class
_data:TOgfParser;
_id:TSlotId;
_container:TSlotsContainer;
function _CmdInfo():string;
function _CmdLoadFromFile(path:string):string;
function _CmdSaveToFile(path:string):string;
function _CmdUnload():string;
function _CmdPasteMeshFromTempBuf():string;
function _CmdRemoveCollapsedMeshes():string;
function _CmdSkeletonUniformScale(cmd:string):string;
function _CmdPropChildMesh(cmd:string):string;
function _CmdPropSkeleton(cmd:string):string;
function _ProcessMeshesCommands(child_id:integer; cmd:string):string;
function _CmdMeshInfo(child_id:integer):string;
function _CmdMeshSetTexture(child_id:integer; args:string):string;
function _CmdMeshSetShader(child_id:integer; args:string):string;
function _CmdMeshCopy(child_id:integer):string;
function _CmdMeshInsert(child_id:integer):string;
function _CmdMeshRemove(child_id:integer):string;
function _CmdMeshMove(child_id:integer; cmd:string):string;
function _CmdMeshScale(child_id:integer; cmd:string):string;
function _CmdMeshRebind(child_id:integer; cmd:string):string;
function _CmdMeshBonestats(child_id:integer):string;
function _CmdMeshFilterBone(child_id:integer; cmd:string):string;
function _ProcessBonesCommands(bone_id:integer; cmd:string):string;
function _CmdBoneInfo(bone_id:integer):string;
function _ProcessIKDataCommands(bone_id:integer; cmd:string):string;
function _CmdIKDataInfo(bone_id:integer):string;
function _CmdIKDataCopy(bone_id:integer):string;
function _CmdIKDataPaste(bone_id:integer):string;
public
constructor Create(id:TSlotId; container:TSlotsContainer);
destructor Destroy; override;
function Id():TSlotId;
function ExecuteCmd(cmd:string):string;
function ExtractBoneIdFromString(var inoutstr:string; var boneid:TBoneId):boolean;
function GetBoneNameById(boneid:TBoneId):string;
end;
{ TSlotsContainer }
TSlotsContainer = class
_model_slots:array of TModelSlot;
_temp_buffer:TTempBuffer;
public
constructor Create();
destructor Destroy(); override;
function GetModelSlotById(id:TSlotId):TModelSlot;
function GetTempBuffer():TTempBuffer;
function TryGetSlotRefByString(in_string:string; var rest_string:string):TModelSlot;
end;
implementation
uses sysutils, strutils, CommandsHelpers;
const
OPCODE_CALL:char=':';
OPCODE_INDEX:char='.';
{ TTempBuffer }
constructor TTempBuffer.Create;
begin
Clear();
end;
destructor TTempBuffer.Destroy;
begin
inherited Destroy;
end;
procedure TTempBuffer.Clear();
begin
_cur_type:=TTempTypeNone;
end;
function TTempBuffer.GetCurrentType(): TTempBufferType;
begin
result:=_cur_type;
end;
function TTempBuffer.GetString(var outstr: string): boolean;
begin
if _cur_type = TTempTypeString then begin
result:=true;
outstr:=_buffer_string;
end else begin
result:=false;
end;
end;
procedure TTempBuffer.SetString(s: string);
begin
_cur_type:=TTempTypeString;
_buffer_string:=s;
end;
{ TModelSlot }
/////////////////////////////////// COMMON /////////////////////////////////////
function TModelSlot._CmdInfo(): string;
begin
if not _data.Loaded then begin
result:='slot doesn''t contain loaded data';
end else begin
result:='slot is in use';
end;
end;
function TModelSlot._CmdLoadFromFile(path: string): string;
begin
if _data.Loaded() then begin
result:='!Slot is not empty. Unload data first';
exit;
end;
if _data.LoadFromFile(path) then begin
result:='';
end else begin
result:='!Can''t load model from file "'+path+'"';
end;
end;
function TModelSlot._CmdSaveToFile(path: string): string;
begin
result:='';
if not _data.Loaded() then begin
result:='!Slot is empty';
exit;
end;
if not _data.SaveToFile(path) then begin
result:='!Can''t save model to "'+path+'"';
end;
end;
function TModelSlot._CmdUnload(): string;
begin
if not _data.Loaded() then begin
result:='!Slot is empty';
exit;
end;
_data.Reset;
result:='';
end;
/////////////////////////////////// MESHES /////////////////////////////////////
function TModelSlot._ProcessMeshesCommands(child_id: integer; cmd: string): string;
var
opcode:char;
args:string;
proccode:string;
const
PROC_INFO:string='info';
PROC_SETTEXTURE:string='settexture';
PROC_SETSHADER:string='setshader';
PROC_REMOVE:string='remove';
PROC_COPY:string='copy';
PROC_PASTE:string='paste';
PROC_MOVE:string='move';
PROC_SCALE:string='scale'; //mirror if negative
PROC_REBIND:string='rebind';
PROC_BONESTATS:string='bonestats';
PROC_FILTERBONE:string='filterbone';
PROC_CHANGELINK:string='changelinktype';
PROC_GETOPTIMALLINKTYPE:string='getoptimallinktype';
begin
if (not _data.Loaded()) or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if abs(child_id) >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
if child_id < 0 then begin
child_id:=_data.Meshes().Count() - child_id;
end;
if length(trim(cmd))=0 then begin
result:=_CmdMeshInfo(child_id);
end else begin
args:='';
opcode:=cmd[1];
cmd:=rightstr(cmd, length(cmd)-1);
if opcode = OPCODE_CALL then begin
proccode:=ExtractAlphabeticString(cmd);
if not ExtractProcArgs(cmd, args) then begin
result:='!can''t parse arguments to call procedure "'+proccode+'"';
end else if lowercase(proccode)=PROC_INFO then begin
result:=_CmdMeshInfo(child_id);
end else if lowercase(proccode)=PROC_SETTEXTURE then begin
result:=_CmdMeshSetTexture(child_id, args);
end else if lowercase(proccode)=PROC_SETSHADER then begin
result:=_CmdMeshSetShader(child_id, args);
end else if lowercase(proccode)=PROC_REMOVE then begin
result:=_CmdMeshRemove(child_id);
end else if lowercase(proccode)=PROC_COPY then begin
result:=_CmdMeshCopy(child_id);
end else if lowercase(proccode)=PROC_PASTE then begin
result:=_CmdMeshInsert(child_id);
end else if lowercase(proccode)=PROC_MOVE then begin
result:=_CmdMeshMove(child_id, args);
end else if lowercase(proccode)=PROC_SCALE then begin
result:=_CmdMeshScale(child_id, args);
end else if lowercase(proccode)=PROC_REBIND then begin
result:=_CmdMeshRebind(child_id, args);
end else if lowercase(proccode)=PROC_BONESTATS then begin
result:=_CmdMeshBonestats(child_id);
end else if lowercase(proccode)=PROC_FILTERBONE then begin
result:=_CmdMeshFilterBone(child_id, args);
end else begin
result:='!unknown procedure "'+proccode+'"';
end;
end else begin
result:='!unsupported opcode "'+opcode+'"';
end;
end;
end;
end;
function TModelSlot._CmdMeshInfo(child_id: integer): string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
result:='Info for child mesh #'+inttostr(child_id)+':'+chr($0d)+chr($0a);
result:=result+'- Texture: '+_data.Meshes.Get(child_id).GetTextureData().texture+chr($0d)+chr($0a);
result:=result+'- Shader: '+_data.Meshes.Get(child_id).GetTextureData().shader+chr($0d)+chr($0a);
result:=result+'- Vertices count:'+inttostr(_data.Meshes.Get(child_id).GetVerticesCount())+chr($0d)+chr($0a);
result:=result+'- Tris count:'+inttostr(_data.Meshes.Get(child_id).GetTrisCountTotal())+chr($0d)+chr($0a);
result:=result+'- Current link type:'+inttostr(_data.Meshes.Get(child_id).GetCurrentLinkType())+chr($0d)+chr($0a);
end;
end;
function TModelSlot._CmdMeshSetTexture(child_id: integer; args: string): string;
var
texdata:TOgfTextureData;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
texdata:=_data.Meshes().Get(child_id).GetTextureData();
texdata.texture:=trim(args);
if _data.Meshes().Get(child_id).SetTextureData(texdata) then begin
result:='texture successfully updated for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end else begin
result:='!can''t update texture for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end;
end;
end;
function TModelSlot._CmdMeshSetShader(child_id: integer; args: string): string;
var
texdata:TOgfTextureData;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
texdata:=_data.Meshes().Get(child_id).GetTextureData();
texdata.shader:=trim(args);
if _data.Meshes().Get(child_id).SetTextureData(texdata) then begin
result:='shader successfully updated for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end else begin
result:='!can''t update shader for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end;
end;
end;
function TModelSlot._CmdMeshCopy(child_id: integer): string;
var
shader, texture:string;
s:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
s:=_data.Meshes().Get(child_id).Serialize();
if length(s) = 0 then begin
result:='!cannot serialize mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+'), buffer cleared';
_container.GetTempBuffer().Clear();
end else begin
_container.GetTempBuffer().SetString(s);
result:='mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') successfully saved to temp buffer';
end;
end;
end;
function TModelSlot._CmdMeshInsert(child_id: integer): string;
var
s:string;
meshid:integer;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count()+1 then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
s:='';
if _container.GetTempBuffer().GetString(s) then begin
meshid:=_data.Meshes().Insert(s, child_id);
if (meshid < 0) or (meshid<>child_id) then begin
result:='!unable to insert data from temp buffer as a mesh';
end else begin
shader:=_data.Meshes().Get(meshid).GetTextureData().shader;
texture:=_data.Meshes().Get(meshid).GetTextureData().texture;
result:='mesh #'+inttostr(meshid)+' ('+texture+' : '+shader+') successfully inserted';
end;
end else begin
result:='!can''t extract data from temp buffer, unsupported format?';
end;
end;
end;
function TModelSlot._CmdPasteMeshFromTempBuf(): string;
var
s:string;
meshid:integer;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else begin
s:='';
if _container.GetTempBuffer().GetString(s) then begin
meshid:=_data.Meshes().Append(s);
if meshid < 0 then begin
result:='!unable to append data from temp buffer as a mesh';
end else begin
shader:=_data.Meshes().Get(meshid).GetTextureData().shader;
texture:=_data.Meshes().Get(meshid).GetTextureData().texture;
result:='mesh #'+inttostr(meshid)+' ('+texture+' : '+shader+') successfully appended';
end;
end else begin
result:='!can''t extract data from temp buffer, unsupported format?';
end;
end;
end;
function TModelSlot._CmdRemoveCollapsedMeshes(): string;
var
i:integer;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else begin
result:='';
for i:=_data.Meshes().Count()-1 downto 0 do begin
shader:=_data.Meshes().Get(i).GetTextureData().shader;
texture:=_data.Meshes().Get(i).GetTextureData().texture;
if _data.Meshes().Get(i).GetVerticesCount() = 0 then begin;
if not _data.Meshes().Remove(i) then begin
result:='!'+result+'Failed to remove mesh #'+inttostr(i)+' ('+texture+' : '+shader+')'+chr($0d)+chr($0a);
end else begin
result:=result+'Removed collapses mesh #'+inttostr(i)+' ('+texture+' : '+shader+')'+chr($0d)+chr($0a);
end;
end;
end;
end;
end;
function TModelSlot._CmdMeshRemove(child_id: integer): string;
var
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
if not _data.Meshes().Remove(child_id) then begin
result:='!remove operation failed for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end else begin
result:='successfully removed mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end;
end;
end;
function TModelSlot._CmdMeshMove(child_id: integer; cmd: string): string;
var
v:FVector3;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
set_zero(v{%H-});
if not ExtractFVector3(cmd, v) then begin
result:='!cannot extract vector argument';
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
cmd:=TrimLeft(cmd);
if length(cmd)>0 then begin
result:='!invalid arguments count, expected 3 floats';
end else begin
if not _data.Meshes().Get(child_id).Move(v) then begin
result:='!move operation failed for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end else begin
result:='mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') successfully moved';
end;
end;
end;
end;
end;
function TModelSlot._CmdMeshScale(child_id: integer; cmd: string): string;
var
v:FVector3;
shader, texture:string;
begin
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
set_zero(v{%H-});
if not ExtractFVector3(cmd, v) then begin
result:='!cannot extract vector argument';
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
cmd:=TrimLeft(cmd);
if length(cmd)>0 then begin
result:='!invalid arguments count, expected 3 floats';
end else begin
if not _data.Meshes().Get(child_id).Scale(v) then begin
result:='!scale operation failed for mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+')';
end else begin
result:='mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') successfully scaled';
end;
end;
end;
end;
end;
function TModelSlot._CmdMeshRebind(child_id: integer; cmd: string): string;
var
dest_boneid, src_boneid:TBoneID;
shader, texture:string;
flag:boolean;
vcnt:integer;
begin
result:='';
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
src_boneid:=INVALID_BONE_ID;
dest_boneid:=INVALID_BONE_ID;
if not ExtractBoneIdFromString(cmd, dest_boneid) then begin
result:='!can''t extract target bone id';
end else begin
cmd:=TrimLeft(cmd);
if (length(cmd) > 0) and (cmd[1]=COMMANDS_ARGUMENTS_SEPARATOR) then begin
cmd:=TrimLeft(rightstr(cmd, length(cmd)-1));
flag:=ExtractBoneIdFromString(cmd, src_boneid);
cmd:=TrimLeft(cmd);
if (not flag) then begin
result:='!can''t extract source bone id';
end else if (length(cmd)>0) then begin
result:='!the procedure expects 1 or 2 argument(s)';
end;
end;
if (length(result) = 0) then begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
vcnt:= _data.Meshes().Get(child_id).GetVerticesCountForBoneID(src_boneid);
if vcnt = 0 then begin
result:='#mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') contains no vertices binded with '+GetBoneNameById(src_boneid);
end else if not _data.Meshes().Get(child_id).RebindVertices(dest_boneid, src_boneid) then begin
result:='!failed to rebind vertices of mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') from '+GetBoneNameById(src_boneid)+' to '+GetBoneNameById(dest_boneid);
end else begin
result:=inttostr(vcnt)+' vertices of mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') are successfully rebinded from '+GetBoneNameById(src_boneid)+' to '+GetBoneNameById(dest_boneid);
end;
end;
end;
end;
end;
function TModelSlot._CmdMeshBonestats(child_id: integer): string;
var
i, vcnt:integer;
shader, texture:string;
found:boolean;
s:TOgfSkeleton;
begin
result:='';
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
s:=_data.Skeleton();
if s = nil then begin
result:='!model has no skeleton';
end else begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
result:='mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') is assigned to the following bones:'+chr($0d)+chr($0a);
found:=false;
for i:=0 to s.GetBonesCount()-1 do begin
vcnt:= _data.Meshes().Get(child_id).GetVerticesCountForBoneID(i);
if vcnt > 0 then begin
found:=true;
result:=result+'- '+GetBoneNameById(i)+' (vertices: '+inttostr(vcnt)+')'+chr($0d)+chr($0a);
end;
end;
if not found then begin
result:='#mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') is NOT assigned to any valid bone';
end;
end;
end;
end;
function TModelSlot._CmdMeshFilterBone(child_id: integer; cmd: string): string;
var
boneid:TBoneID;
shader, texture:string;
inverse:boolean;
begin
result:='';
if not _data.Loaded() or (_data.Meshes()=nil) then begin
result:='!please load model first';
end else if child_id >= _data.Meshes().Count() then begin
result:='!child id #'+inttostr(child_id)+' out of bounds, total children count: '+inttostr(_data.Meshes().Count());
end else begin
boneid:=INVALID_BONE_ID;
inverse:=false;
cmd:=trimleft(cmd);
if (length(cmd)>0) and (cmd[1]=COMMANDS_ARGUMENT_INVERSE) then begin
inverse:=true;
cmd:=trimleft(rightstr(cmd, length(cmd)-1));
end;
if ExtractBoneIdFromString(cmd, boneid) then begin
shader:=_data.Meshes().Get(child_id).GetTextureData().shader;
texture:=_data.Meshes().Get(child_id).GetTextureData().texture;
if length(trimleft(cmd))>0 then begin
result:='!procedure expects 1 argument';
end else begin
if not inverse and (_data.Meshes().Get(child_id).GetVerticesCountForBoneID(boneid) = 0) then begin
result:='#no vertices of mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') are assigned to bone '+GetBoneNameById(boneid);
end;
if _data.Meshes().Get(child_id).RemoveVerticesForBoneId(boneid, inverse) then begin
if _data.Meshes().Get(child_id).GetVerticesCount() = 0 then begin
result:='#mesh is fully collapsed (no vertices found), please remove it'+chr($0d)+chr($0a);
end;
result:=result+'successfully removed vertices of mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') assigned to bone '+GetBoneNameById(boneid);
end else begin
result:='!error filtering vertices of mesh #'+inttostr(child_id)+' ('+texture+' : '+shader+') assigned to bone '+GetBoneNameById(boneid);
end;
end;
end else begin
result:='!can''t extract bone id';
end;
end;
end;
/////////////////////////////////// BONES //////////////////////////////////////
function TModelSlot.ExtractBoneIdFromString(var inoutstr:string; var boneid:TBoneId):boolean;
var
tmpid, tmpstr:string;
tmp_num, i:integer;
begin
result:=false;
if not result then begin
// Пробуем извлечь ID по имени кости
tmpstr:=inoutstr;
tmpid:=ExtractABNString(tmpstr);
tmpstr:=TrimLeft(tmpstr);
tmpid:=trim(tmpid);
for i:=0 to _data.Skeleton().GetBonesCount()-1 do begin
if _data.Skeleton().GetBoneName(i) = tmpid then begin
result:=true;
tmp_num:=i;
break;
end;
end;
end;
if not result then begin
// Пробуем извлечь ID напрямую
tmpstr:=inoutstr;
tmpid:=ExtractNumericString(tmpstr, true);
tmpstr:=TrimLeft(tmpstr);
tmp_num:=strtointdef(tmpid, -2);
if (tmp_num <> -2) then begin
result:=(_data.Skeleton().GetBonesCount() > tmp_num);
end;
end;
if result then begin
inoutstr:=tmpstr;
boneid:=tmp_num;
end;
end;
function TModelSlot.GetBoneNameById(boneid: TBoneId): string;
var
s:TOgfSkeleton;
begin
result:='[none]';
if not _data.Loaded() then exit;
s:=_data.Skeleton();
if s = nil then exit;
if boneid = INVALID_BONE_ID then begin
result:='[all]';
exit;
end;
if (boneid<s.GetBonesCount()) then begin
result:=s.GetBoneName(boneid);
end;
end;
function TModelSlot._ProcessBonesCommands(bone_id: integer; cmd: string): string;
var
opcode:char;
args:string;
proccode:string;
propname:string;
const
PROC_INFO:string='info';
PROP_IKDATA:string='ikdata';
begin
if (not _data.Loaded()) or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else if abs(bone_id) >= _data.Skeleton().GetBonesCount() then begin
result:='!bone id #'+inttostr(bone_id)+' out of bounds, total bones count: '+inttostr(_data.Meshes().Count());
end else begin
if bone_id < 0 then begin
bone_id:=_data.Skeleton().GetBonesCount() - bone_id;
end;
if length(trim(cmd))=0 then begin
result:=_CmdBoneInfo(bone_id);
end else begin
args:='';
opcode:=cmd[1];
cmd:=rightstr(cmd, length(cmd)-1);
if opcode = OPCODE_CALL then begin
proccode:=ExtractAlphabeticString(cmd);
if not ExtractProcArgs(cmd, args) then begin
result:='!can''t parse arguments to call procedure "'+proccode+'"';
end else if lowercase(proccode)=PROC_INFO then begin
result:=_CmdBoneInfo(bone_id);
end else begin
result:='!unknown procedure "'+proccode+'"';
end;
end else if opcode = OPCODE_INDEX then begin
propname:=ExtractAlphabeticString(cmd);
if not _data.Loaded() then begin
result:='!please load model first';
end else if propname = PROP_IKDATA then begin
result:=_ProcessIKDataCommands(bone_id, cmd)
end else begin
result:='!unknown property "'+propname+'"';
end;
end else begin
result:='!unsupported opcode "'+opcode+'"';
end;
end;
end;
end;
function TModelSlot._CmdBoneInfo(bone_id: integer): string;
begin
if not _data.Loaded() or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else if bone_id >= _data.Skeleton().GetBonesCount() then begin
result:='!bone id #'+inttostr(bone_id)+' out of bounds, total bones count: '+inttostr(_data.Skeleton().GetBonesCount());
end else begin
result:='Info for bone #'+inttostr(bone_id)+':'+chr($0d)+chr($0a);
result:=result+'- Name: '+_data.Skeleton().GetBoneName(bone_id)+chr($0d)+chr($0a);
result:=result+'- Parent: '+_data.Skeleton().GetParentBoneName(bone_id);
end;
end;
function TModelSlot._ProcessIKDataCommands(bone_id: integer; cmd: string): string;
var
opcode:char;
args:string;
proccode:string;
const
PROC_INFO:string='info';
PROC_COPYIKDATA:string='copy';
PROC_PASTEIKDATA:string='paste';
begin
if not _data.Loaded() or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else if bone_id >= _data.Skeleton().GetBonesCount() then begin
result:='!bone id #'+inttostr(bone_id)+' out of bounds, total bones count: '+inttostr(_data.Skeleton().GetBonesCount());
end else begin
if bone_id < 0 then begin
bone_id:=_data.Skeleton().GetBonesCount() - bone_id;
end;
if length(trim(cmd))=0 then begin
result:=_CmdIKDataInfo(bone_id);
end else begin
args:='';
opcode:=cmd[1];
cmd:=rightstr(cmd, length(cmd)-1);
if opcode = OPCODE_CALL then begin
proccode:=ExtractAlphabeticString(cmd);
if not ExtractProcArgs(cmd, args) then begin
result:='!can''t parse arguments to call procedure "'+proccode+'"';
end else if lowercase(proccode)=PROC_INFO then begin
result:=_CmdIKDataInfo(bone_id);
end else if lowercase(proccode)=PROC_COPYIKDATA then begin
result:=_CmdIKDataCopy(bone_id);
end else if lowercase(proccode)=PROC_PASTEIKDATA then begin
result:=_CmdIKDataPaste(bone_id);
end else begin
result:='!unknown procedure "'+proccode+'"';
end;
end else begin
result:='!unsupported opcode "'+opcode+'"';
end;
end;
end;
end;
function ShapeTypeById(shape:word):string;
begin
if shape = OGF_SHAPE_TYPE_BOX then begin
result:='BOX';
end else if shape = OGF_SHAPE_TYPE_CYLINDER then begin
result:='CYLINDER';
end else if shape = OGF_SHAPE_TYPE_INVALID then begin
result:='INVALID';
end else if shape = OGF_SHAPE_TYPE_SPHERE then begin
result:='SPHERE';
end else if shape = OGF_SHAPE_TYPE_NONE then begin
result:='NONE';
end else begin
result:='[unknown]';
end;
end;
function TModelSlot._CmdIKDataInfo(bone_id: integer): string;
begin
if not _data.Loaded() or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else if bone_id >= _data.Skeleton().GetBonesCount() then begin
result:='!bone id #'+inttostr(bone_id)+' out of bounds, total bones count: '+inttostr(_data.Skeleton().GetBonesCount());
end else begin
result:='IKData info for bone #'+inttostr(bone_id)+' ('+_data.Skeleton().GetBoneName(bone_id)+'):'+chr($0d)+chr($0a);
result:=result+'- Shape type: '+ShapeTypeById(_data.Skeleton().GetOgfShape(bone_id).shape_type);
end;
end;
function TModelSlot._CmdIKDataCopy(bone_id: integer): string;
var
s:string;
begin
if not _data.Loaded() or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else if bone_id >= _data.Skeleton().GetBonesCount() then begin
result:='!bone id #'+inttostr(bone_id)+' out of bounds, total bones count: '+inttostr(_data.Skeleton().GetBonesCount());
end else begin
s:=_data.Skeleton().CopySerializedBoneIKData(bone_id);
if length(s)=0 then begin
result:='!failed to serialize data';
end else begin
_container.GetTempBuffer().SetString(s);
result:='data successfully copied to temp buffer';
end;
end;
end;
function TModelSlot._CmdIKDataPaste(bone_id: integer): string;
var
s:string;
begin
if not _data.Loaded() or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else if bone_id >= _data.Skeleton().GetBonesCount() then begin
result:='!bone id #'+inttostr(bone_id)+' out of bounds, total bones count: '+inttostr(_data.Skeleton().GetBonesCount());
end else begin
if not _container.GetTempBuffer().GetString(s) then begin
result:='!failed to extract data from temp buffer';
end else if not _data.Skeleton().PasteSerializedBoneIKData(bone_id, s) then begin
result:='!failed to paste serialized data';
end else begin
result:='data successfully pasted';
end;
end;
end;
function TModelSlot._CmdSkeletonUniformScale(cmd: string): string;
var
k:single;
begin
if not ExtractFloatFromString(cmd, k) then begin
result:='!procedure expects a floating-point argument';
end else if length(trim(cmd)) > 0 then begin
result:='!procedure expects 1 argument';
end else if not _data.Loaded() or (_data.Skeleton()=nil) then begin
result:='!please load model first';
end else begin
if not _data.Skeleton().UniformScale(k) then begin
result:='!error scaling skeleton';
end else begin
result:='skeleton successfully scaled';
end;
end;
end;
/////////////////////////////////// CORE ///////////////////////////////////////
constructor TModelSlot.Create(id: TSlotId; container: TSlotsContainer);
begin
_id:=id;
_data:=TOgfParser.Create();
_container:=container;
end;
destructor TModelSlot.Destroy;
begin
_data.Free();
inherited Destroy;
end;
function TModelSlot.Id(): TSlotId;
begin
result:=_id;
end;
/////////////////////////// COMMAND PROCESSORS /////////////////////////////////
function TModelSlot._CmdPropChildMesh(cmd: string): string;
var
filters:TIndexFilters;
i:integer;
tmpstr:string;
args:string;
const
FILTER_TEXTURE_NAME='texture';
FILTER_SHADER_NAME='shader';
begin
result:='';
InitFilters(filters{%H-});
PushFilter(filters, FILTER_TEXTURE_NAME);
PushFilter(filters, FILTER_SHADER_NAME);
i:=0;
if ExtractIndexFilter(cmd,filters,i) then begin
if i < 0 then begin
result:='!invalid filter rule';
end else begin
result:='';
// We use reverse order because current child could disappear or new child in the end could appear while executing command
for i:=_data.Meshes().Count()-1 downto 0 do begin
if IsMatchFilter(_data.Meshes().Get(i).GetTextureData().texture, filters[0], FILTER_MODE_EXACT) and IsMatchFilter(_data.Meshes().Get(i).GetTextureData().shader, filters[1], FILTER_MODE_EXACT) then begin
tmpstr:=_ProcessMeshesCommands(i, cmd);
if (length(tmpstr)>0) then begin
if tmpstr[1]='!' then begin
result:=result+'!mesh'+inttostr(i)+': '+tmpstr+chr($0d)+chr($0a);
end else if tmpstr[1]='#' then begin
result:=result+'#mesh'+inttostr(i)+': '+tmpstr+chr($0d)+chr($0a);
end else begin
result:=result+'mesh'+inttostr(i)+': '+tmpstr+chr($0d)+chr($0a);
end;
end;
end;
end;
if length(result) = 0 then begin
result:='#the specified filter doesn''t match any item, no action performed';
end;
end;
end else begin
args:=ExtractNumericString(cmd, false);
i:=strtointdef(args, -1);
if i<0 then begin
result:='!invalid child id "'+args+'"';
end else begin
result:=_ProcessMeshesCommands(i, cmd);
end;
end;
ClearFilters(filters);
end;
function TModelSlot._CmdPropSkeleton(cmd: string): string;
var
filters:TIndexFilters;
i:integer;
tmpstr:string;
args:string;
const
FILTER_BONE_NAME='bonename';
FILTER_BONE_ID='boneid';
begin
result:='';
InitFilters(filters{%H-});
PushFilter(filters, FILTER_BONE_NAME);
PushFilter(filters, FILTER_BONE_ID);
i:=0;
if ExtractIndexFilter(cmd,filters,i) then begin
if i < 0 then begin
result:='!invalid filter rule';
end else begin
result:='';
// We use reverse order because current child could disappear or new child in the end could appear while executing command
for i:=_data.Skeleton().GetBonesCount()-1 downto 0 do begin
if IsMatchFilter(_data.Skeleton().GetBoneName(i), filters[0], FILTER_MODE_EXACT) and IsMatchFilter(inttostr(i), filters[1], FILTER_MODE_EXACT) then begin
tmpstr:=_ProcessBonesCommands(i, cmd);
if (length(tmpstr)>0) then begin
if tmpstr[1]='!' then begin
result:=result+'!bone'+inttostr(i)+': '+tmpstr+chr($0d)+chr($0a);
end else if tmpstr[1]='#' then begin
result:=result+'bone'+inttostr(i)+': '+tmpstr+chr($0d)+chr($0a);
end else begin
result:=result+'bone'+inttostr(i)+': '+tmpstr+chr($0d)+chr($0a);
end;
end;