-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASE2GMS.lua
796 lines (623 loc) · 20.4 KB
/
ASE2GMS.lua
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
-- Exports Sprite to Gamemaker
-- Author : Clément Espeute (@valdenthoranar)
-- Templating : this script use a custom templating to replace
-- content of gamemaker files. These templates are marked with <<template_name>>
-- and the contents of these templates are replaced using the FormatTable function
local json = dofile('.ase2gmsjson.lua');
-- Constants
local popupName = "GMS Exporter (Beta)"
local metaDataLayerName = "__ase2gms"
local hitboxLayerName = "Collision"
local pivotLayerName = "Origin"
local layersColor = Color{r = 0x03, g = 0x9d, b = 0x5b};
local sublayersColor = Color{r = 0x03, g = 0x9d, b = 0x5b, a = 127};
local cantOpenYYPMsg = "Couldn't open project at path %s."
local cantOpenSpriteYYMsg = "Couldn't open Gamemaker sprite file at path %s."
local cantParseMsg = "Couldn't parse GameMakerProject. Maybe the file is corrupted ?"
local gmsAnimFPS = 60.0
local origins = {
"Top Left",
"Top Center",
"Top Right",
"Middle Left",
"Middle Centre",
"Middle Right",
"Bottom Left",
"Bottom Centre",
"Bottom Right",
"Custom"
}
-- Empty GMS .yy file
local function local_assert(cond, msg)
if (not cond) then
print(msg)
error()
end
end
local emptyGMSyy = [[{
"bboxMode": 2,
"collisionKind": 1,
"type": 0,
"origin": <<exportorigin>>,
"preMultiplyAlpha": false,
"edgeFiltering": false,
"collisionTolerance": 0,
"swfPrecision": 2.525,
"bbox_left": <<bboxleft>>,
"bbox_right": <<bboxright>>,
"bbox_top": <<bboxtop>>,
"bbox_bottom": <<bboxbottom>>,
"HTile": false,
"VTile": false,
"For3D": false,
"width": <<sprwidth>>,
"height": <<sprheigth>>,
"textureGroupId": {
"name": "<<texturegroupname>>",
"path": "<<texturegrouppath>>",
},
"swatchColours": null,
"gridX": 0,
"gridY": 0,
"frames": [
],
"sequence": {
"spriteId": {"name":"<<spritename>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},
"timeUnits": 1,
"playback": 1,
"playbackSpeed": <<playbackspeed>>,
"playbackSpeedType": 0,
"autoRecord": true,
"volume": 1.0,
"length": <<sequencelenght>>,
"events": {"Keyframes":[],"resourceVersion":"1.0","resourceType":"KeyframeStore<MessageEventKeyframe>",},
"moments": {"Keyframes":[],"resourceVersion":"1.0","resourceType":"KeyframeStore<MomentsEventKeyframe>",},
"tracks": [
{"name":"frames","spriteId":null,"keyframes":{"Keyframes":[
],"resourceVersion":"1.0","resourceType":"KeyframeStore<SpriteFrameKeyframe>",},"trackColour":0,"inheritsTrackColour":true,"builtinName":0,"traits":0,"interpolation":1,"tracks":[],"events":[],"modifiers":[],"isCreationTrack":false,"resourceVersion":"1.0","tags":[],"resourceType":"GMSpriteFramesTrack",},
],
"visibleRange": null,
"lockOrigin": false,
"showBackdrop": true,
"showBackdropImage": false,
"backdropImagePath": "",
"backdropImageOpacity": 0.5,
"backdropWidth": 1366,
"backdropHeight": 768,
"backdropXOffset": 0.0,
"backdropYOffset": 0.0,
"xorigin": <<xorigin>>,
"yorigin": <<yorigin>>,
"eventToFunction": {},
"eventStubScript": null,
"parent": {"name":"<<spritename>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},
"resourceVersion": "1.3",
"name": "<<spritename>>",
"tags": [],
"resourceType": "GMSequence",
},
"layers": [
{"visible":true,"isLocked":false,"blendMode":0,"opacity":100.0,"displayName":"default","resourceVersion":"1.0","name":"<<layerid>>","tags":[],"resourceType":"GMImageLayer",},
],
"parent": {
"name": "<<parentname>>",
"path": "<<parentpath>>",
},
"resourceVersion": "1.0",
"name": "<<spritename>>",
"tags": [],
"resourceType": "GMSprite",
}]]
local emptyFrameData = [[
{"compositeImage":{"FrameId":{"name":"<<spriteguid>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},"LayerId":null,"resourceVersion":"1.0","name":"","tags":[],"resourceType":"GMSpriteBitmap",},"images":[
{"FrameId":{"name":"<<spriteguid>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},"LayerId":{"name":"<<layerguid>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},"resourceVersion":"1.0","name":"","tags":[],"resourceType":"GMSpriteBitmap",},
],"parent":{"name":"<<spritename>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},"resourceVersion":"1.0","name":"<<spriteguid>>","tags":[],"resourceType":"GMSpriteFrame",},
]]
local emptyKeyframeData = [[
{"id":"<<keyframeid>>","Key":<<keyid>>,"Length":<<keylength>>,"Stretch":false,"Disabled":false,"IsCreationKey":false,"Channels":{"0":{"Id":{"name":"<<spriteid>>","path":"sprites/<<spritename>>/<<spritename>>.yy",},"resourceVersion":"1.0","resourceType":"SpriteFrameKeyframe",},},"resourceVersion":"1.0","resourceType":"Keyframe<SpriteFrameKeyframe>",},
]]
-- Light serialization of settings
function StringToSettings(str, t)
str:gsub("([^;]*)=([^;]*)", function(k,v)
t[k] = v
end
)
return t
end
function SettingsToString(settings)
local s = ""
for k, v in pairs(settings) do
s = s .. k .."="..v..";"
end
return s
end
-- replaces occurrences of <<tag>> in str by t[tag]
function FormatTable(str, t)
return str:gsub("<<(%a*)>>",
function(s)
return t[s]
end
)
end
-- Utility functions
function GetCellCenter(cel)
return Point(math.floor(cel.bounds.x + cel.bounds.width/2), math.floor(cel.bounds.y + cel.bounds.width/2));
end
-- returns metadataLayer, wasLayerCreated
function GetOrCreateMetadataLayer(spr)
-- Check or create metadata layer
local metaLayer = nil
for _, layer in ipairs(spr.layers) do
if layer and layer.name == metaDataLayerName then
if not layer.isGroup then
spr:deleteLayer(layer)
else
metaLayer = layer
end
break;
end
end
local wasMetaLayerCreated = false
if not metaLayer then
metaLayer = spr:newGroup()
metaLayer.name = metaDataLayerName
wasMetaLayerCreated = true
end
metaLayer.color = layersColor
metaLayer.stackIndex = 9998
return metaLayer, wasMetaLayerCreated
end
-- returns a table with the metaInfos
function GetOrCreateMetaInfo(metaLayer, spr)
local infos = {}
for _, layer in ipairs(metaLayer.layers) do
if layer.name == hitboxLayerName then
infos.hitboxLayer = layer
end
if (layer.name == pivotLayerName) then
infos.pivotLayer = layer
end
end
if not infos.hitboxLayer then
infos.hitboxLayer = spr:newLayer()
infos.hitboxLayer.name = hitboxLayerName
infos.hitboxLayer.parent = metaLayer
infos.hitboxLayer.isContinuous = true
infos.hitboxLayer.opacity = 127
infos.hitboxLayer.color = sublayersColor
end
local savedRange = app.range
infos.hitboxCel = infos.hitboxLayer:cel(1)
if not infos.hitboxCel then
infos.hitboxCel = spr:newCel(infos.hitboxLayer, 1)
local image = infos.hitboxCel.image;
-- Fill cell with black (full hitbox)
for it in image:pixels() do
it(app.pixelColor.rgba(0,0,0))
end
end
if not infos.pivotLayer then
infos.pivotLayer = spr:newLayer()
infos.pivotLayer.name = pivotLayerName
infos.pivotLayer.parent = metaLayer
infos.pivotLayer.isContinuous = true
infos.pivotLayer.opacity = 127
infos.pivotLayer.color = sublayersColor
end
infos.pivotCel = infos.pivotLayer:cel(1)
if not infos.pivotCel then
infos.pivotCel = spr:newCel(infos.pivotLayer, 1, Image(5,5, spr.colorMode), Point(-2,-2))
-- Draw small cross
local color = app.pixelColor.rgba(255,0,0)
local img = infos.pivotCel.image;
img:drawPixel(0,2,color)
img:drawPixel(1,2,color)
img:drawPixel(3,2,color)
img:drawPixel(4,2,color)
img:drawPixel(1,1,color)
img:drawPixel(1,3,color)
img:drawPixel(3,1,color)
img:drawPixel(3,3,color)
img:drawPixel(2,0,color)
img:drawPixel(2,1,color)
img:drawPixel(2,3,color)
img:drawPixel(2,4,color)
end
return infos
end
function GetPivotId(spr, metaInfos)
local pivotPoint = GetCellCenter(metaInfos.pivotCel);
local spriteCenter = GetCellCenter(spr);
local pivotId = 0;
if (pivotPoint.x == 0) then
pivotId = pivotId + 0
elseif (pivotPoint.x == spriteCenter.x) then
pivotId = pivotId + 1
elseif (pivotPoint.x == spr.bounds.width) then
pivotId = pivotId + 2
else
-- Pivot is custom
pivotId = 9
end
if (pivotPoint.y == 0) then
pivotId = pivotId + 0
elseif (pivotPoint.y == spriteCenter.y) then
pivotId = pivotId + 3
elseif (pivotPoint.y == spr.bounds.height) then
pivotId = pivotId + 6
else
-- Pivot is custom
pivotId = 9
end
if pivotId >= 9 then
pivotId = 9
end
return pivotId
end
-- Find the bracket that closes the one at 'start' inside str
function MatchClosingBracket(str, start)
local depth = 0;
local len = str:len()
local posInString = start
while posInString <= len do
local token = str:sub(posInString, posInString)
if token == '[' then
depth = depth + 1;
elseif token == ']' then
depth = depth - 1;
if (depth == 0) then
break;
end
end
posInString = posInString + 1
end
return posInString;
end
function SetPivotPoint(pivotId, metaInfos, spr)
local spriteCenter = GetCellCenter(spr);
if (pivotId < 9) then
local xorigin, yorigin = 0, 0
if pivotId % 3 == 0 then
xorigin = 0
elseif pivotId % 3 == 1 then
xorigin = spriteCenter.x
else
xorigin = spr.bounds.width
end
if math.floor(pivotId/3) == 0 then
yorigin = 0
elseif math.floor(pivotId/3) == 1 then
yorigin = spriteCenter.y
else
yorigin = spr.bounds.height
end
metaInfos.pivotCel.position = Point(xorigin - math.floor(metaInfos.pivotCel.bounds.width/2), yorigin - math.floor(metaInfos.pivotCel.bounds.height/2));
end
end
function Main()
math.randomseed( os.time() )
local spr = app.activeSprite
if not spr or not app.isUIAvailable then
return
end
-- Check or create metadata layer
local metaLayer, wasMetaLayerCreated = GetOrCreateMetadataLayer(spr)
local metaInfos = GetOrCreateMetaInfo(metaLayer, spr)
-- Find pivot based on origin layer position
local pivotId = GetPivotId(spr, metaInfos);
app.refresh()
-- Open filedialog
local settings = {
exportProjectPath = nil,
exportSpriteName = app.fs.fileTitle(spr.filename),
exportTag = nil
}
settings = StringToSettings(metaLayer.data, settings);
-- Dialog creation
local tagStringList = {}
for _,tag in ipairs(spr.tags) do
table.insert(tagStringList,tag.name)
end
if #tagStringList > 0 then
table.insert(tagStringList,1, "All");
end
local dlg = Dialog(popupName)
local chosenButton = "cancel"
function onExportClicked()
chosenButton = "export"
dlg:close();
end
function onSaveClicked()
chosenButton = "save"
dlg:close();
end
function onCancelClicked()
dlg:close();
end
function onDebugClicked()
local cel = metaInfos.pivotLayer:cel(1)
if cel then
print(string.format("x:%d y:%d w:%d h:%d", cel.bounds.x, cel.bounds.y, cel.bounds.width, cel.bounds.height))
end
dlg:close();
end
function onCheckClicked()
local useTags = dlg.data.exportTag
dlg:modify{id="exportTagList", visible=useTags};
end
function onPivotChange()
for k,v in ipairs(origins) do
if v == dlg.data.origin then
pivotId = k - 1
break;
end
end
SetPivotPoint(pivotId, metaInfos, spr);
app.refresh()
end
dlg:separator{id="sep1",text="Export settings"}
dlg:file{
id="exportProjectPath",
label="GMS Project",
open = true,
filename=settings.exportProjectPath,
filetypes={"yyp"},
entry=false,
}
dlg:entry{
id="exportSpriteName",
label="Sprite Name",
text=settings.exportSpriteName
}
dlg:check{
id="exportTag",
label="Export Tags",
selected= settings.exportTag and #spr.tags > 0,
onclick=onCheckClicked
}
dlg:combobox{
id="exportTagList",
label="",
option=settings.exportTag or "All",
options=tagStringList,
}
if #spr.tags == 0 then
dlg:modify{id="exportTag", enabled=false};
end
dlg:separator{id="sep2",text="Gamemaker settings"}
dlg:combobox{
id="origin",
label="Origin",
option=origins[pivotId+1],
options=origins,
onchange=onPivotChange
}
local bbox = metaInfos.hitboxCel.bounds
dlg:label{
id="collision",
label="Collision : ",
text=string.format("left: %d right: %d top: %d bot: %d", bbox.x, bbox.x + bbox.width-1, bbox.y, bbox.y + bbox.height-1)
}
dlg:separator{id="sep3"}
dlg:button{
text = "Export",
onclick = onExportClicked,
focus = true
}:button{
text = "Save",
onclick = onSaveClicked
}:button{
text = "Cancel",
onclick = onCancelClicked
}
onCheckClicked();
dlg:show{wait = true}
if chosenButton == "cancel" then
if (wasMetaLayerCreated) then
spr:deleteLayer(metaLayer)
end
return
end
settings.exportProjectPath = dlg.data.exportProjectPath
settings.exportSpriteName = dlg.data.exportSpriteName
settings.exportTag = nil
if dlg.data.exportTag and dlg.data.exportTagList then
settings.exportTag = dlg.data.exportTagList;
end
onPivotChange()
local width = spr.width;
local height = spr.height;
metaLayer.data = SettingsToString(settings)
if spr.filename and spr.filename:len() > 0 then
spr:saveAs(spr.filename);
end
if chosenButton == "save" then
return
end
-- Write files to disk
local wasHitboxLayerVisible, wasPivotLayerVisible = metaInfos.hitboxLayer.isVisible, metaInfos.pivotLayer.isVisible
metaInfos.hitboxLayer.isVisible, metaInfos.pivotLayer.isVisible = false, false
local random = math.random
local function uuid()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
local commands = ""
function QueueCommand(cmd)
if commands:len() > 0 then
commands = commands .. " & "
end
commands = commands .. cmd
end
-- Not 100% safe but good enough
function IsValidPath(str)
return str:find('([^%*%?"<>|\r\n])\'') == nil
end
function WindowsPathEscape(str)
local_assert(IsValidPath(str), string.format("Error, invalid path %s", str))
return '"'..str:gsub("/","\\")..'"'
end
-- Create directory structure
function ExportTag(spriteToExport, from, to, exportName)
-- Security to avoid creating an empty path for dirPath
if not exportName or exportName:len() < 1 then
return
end
local layerUuid = uuid();
local gmsRootDir = app.fs.filePath(settings.exportProjectPath);
local dirPath = app.fs.joinPath(app.fs.joinPath(gmsRootDir, "sprites"), exportName);
local spriteFilepath = app.fs.joinPath(dirPath, exportName .. ".yy");
-- backup previous sprite properties
local prevFile = io.open(spriteFilepath, "r");
local parentName = "Sprites";
local parentPath = "folders/Sprites.yy"
local texturegroupname = "Default"
local texturegrouppath = "texturegroups/Default"
if prevFile then
local prevFileTxt = prevFile:read("*all");
prevFileJson = json.parse(prevFileTxt);
parentPath = prevFileJson.parent.path;
parentName = prevFileJson.parent.name;
texturegroupname = prevFileJson.textureGroupId.name;
texturegrouppath = prevFileJson.textureGroupId.path;
end
local_assert(dirPath:len() > 10, "Fatal error, dirPath was too short, aborting to avoid wiping your computer")
os.execute("rmdir /s /q " .. WindowsPathEscape(dirPath) .. " & mkdir " .. WindowsPathEscape(dirPath));
-- Copy current sprite and save each frame as a independent frame
local spritestring = ""
local keyframestring = ""
local image = Image(spriteToExport);
local animTime = 0
local count = to-from+1
for i=1,count do
image:clear()
image:drawSprite(spriteToExport, from+i-1);
local curSpriteUuid = uuid();
local curSpriteOutPath = dirPath .. "\\" .. curSpriteUuid .. ".png"
local layerOutDir = dirPath .. "\\layers\\" .. curSpriteUuid
local layerOutName = layerOutDir .. "\\" .. layerUuid .. ".png"
image:saveAs(curSpriteOutPath)
QueueCommand("mkdir " .. WindowsPathEscape(layerOutDir));
QueueCommand("copy /Y " .. WindowsPathEscape(curSpriteOutPath) .. " " .. WindowsPathEscape(layerOutName));
local data = {spriteguid = curSpriteUuid, spritename = exportName, layerguid = layerUuid}
spritestring = spritestring .. FormatTable(emptyFrameData, data)
local length = math.floor(gmsAnimFPS * spriteToExport.frames[from+i-1].duration + 0.5)
local keyframedata = {keyframeid = uuid(),
spriteid = curSpriteUuid,
spritename = exportName,
keyid = string.format("%.1f", animTime),
keylength = string.format("%.1f", length),
}
animTime = animTime + length
keyframestring = keyframestring .. FormatTable(emptyKeyframeData, keyframedata)
end
local spriteFile = io.open(spriteFilepath, "r")
local originalSpriteContent = "";
local center = GetCellCenter(metaInfos.pivotCel)
local bbox = metaInfos.hitboxCel.bounds
if false and spriteFile then
originalSpriteContent = spriteFile:read("*all");
spriteFile:close();
spriteFile = nil;
else
originalSpriteContent = FormatTable(emptyGMSyy,{
spritename = exportName,
layerid = layerUuid,
sequencelenght = animTime,
sprwidth = width,
sprheigth = height,
exportorigin = pivotId,
xorigin = center.x,
yorigin = center.y,
bboxleft = bbox.x,
bboxright = bbox.x + bbox.width-1,
bboxtop = bbox.y,
bboxbottom = bbox.y + bbox.height-1,
playbackspeed = string.format("%.0f",gmsAnimFPS),
parentname = parentName,
parentpath = parentPath,
texturegroupname = texturegroupname,
texturegrouppath = texturegrouppath,
}
)
end
local _, frameRangeStart = string.find(originalSpriteContent, '"frames":%s*%[')
local frameRangeEnd = MatchClosingBracket(originalSpriteContent, frameRangeStart)
newSpriteContent = originalSpriteContent:sub(1, frameRangeStart) .. "\n"
newSpriteContent = newSpriteContent .. spritestring .. originalSpriteContent:sub(frameRangeEnd, -1)
originalSpriteContent = newSpriteContent;
_, tracksStart = originalSpriteContent:find('"tracks"%s*:%s*%[')
_, keyframesStart = originalSpriteContent:find('"Keyframes"%s*:%s*%[', tracksStart)
keyframesEnd = MatchClosingBracket(originalSpriteContent, keyframesStart)
newSpriteContent = originalSpriteContent:sub(1, keyframesStart) .. "\n" .. keyframestring .. originalSpriteContent:sub(keyframesEnd, -1)
spriteFile = io.open(spriteFilepath, "w+")
if not spriteFile then
local errMsg = string.format(cantOpenSpriteYYMsg, spriteFilepath)
app.alert{title=popupName, text = errMsg}
return
end
spriteFile:write(newSpriteContent);
spriteFile:close()
end
yoyoProject = io.open(settings.exportProjectPath, "r");
if not yoyoProject then
local errMsg = string.format(cantOpenYYPMsg, settings.exportProjectPath)
app.alert{title=popupName, text = errMsg}
return
end
local yoyoProjectText = yoyoProject:read("*all");
yoyoProject:close();
local filesString = ""
function sanitize(str)
return str:gsub("[^%w%-]", "_");
end
if #spr.tags > 0 and settings.exportTag then
for i, tag in ipairs(spr.tags) do
if settings.exportTag == "All" or settings.exportTag == tag.name then
local tagName = sanitize(tag.name);
local exportTagName = settings.exportSpriteName .. tagName
ExportTag(spr, tag.fromFrame.frameNumber, tag.toFrame.frameNumber, exportTagName)
local exists = string.find(yoyoProjectText, exportTagName .. ".yy")
if not exists then
local toInsert = '\n {"id":{"name":"%s","path":"sprites/%s/%s.yy",},"order":0,},'
local toInsertFormated = string.format(toInsert, exportTagName,exportTagName,exportTagName);
filesString = filesString .. toInsertFormated
end
end
end
else
local exportTagName = settings.exportSpriteName
ExportTag(spr, 1, #spr.frames, exportTagName)
local exists = string.find(yoyoProjectText, exportTagName .. ".yy")
if not exists then
local toInsert = '\n {"id":{"name":"%s","path":"sprites/%s/%s.yy",},"order":0,},'
local toInsertFormated = string.format(toInsert, exportTagName,exportTagName,exportTagName);
filesString = filesString .. toInsertFormated
end
end
os.execute(commands)
-- Open .yyp
do
local searchString = '"resources":%s*%['
local _,searchIndex = string.find(yoyoProjectText, searchString);
if not searchIndex then
local errMsg = cantParseMsg
app.alert{title=popupName, text = errMsg}
return
end
local exists = string.find(yoyoProjectText, settings.exportSpriteName .. ".yy")
if filesString:len() > 0 then
testFile = io.open(settings.exportProjectPath, "w+");
local outText = string.sub(yoyoProjectText, 1, searchIndex) .. filesString .. string.sub(yoyoProjectText, searchIndex+1, -1);
testFile:write(outText);
testFile:close();
end
end
metaInfos.hitboxLayer.isVisible, metaInfos.pivotLayer.isVisible = wasHitboxLayerVisible, wasPivotLayerVisible
app.alert{title=popupName, text = "Export done !"}
end
--Main()
app.transaction(Main)