forked from F3XTeam/RBX-Building-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportPlugin.lua
585 lines (508 loc) · 15.7 KB
/
ImportPlugin.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
------------------------------------------
-- Make references to important items
------------------------------------------
Services = {
HttpService = Game:GetService( 'HttpService' );
Workspace = Game:GetService( 'Workspace' );
ContentProvider = Game:GetService( 'ContentProvider' );
CoreGui = Game:GetService( 'CoreGui' );
Selection = Game:GetService( 'Selection' );
Players = Game:GetService 'Players';
};
bt_logo = 'http://www.roblox.com/asset/?id=129747824';
plugin_icon = 'http://www.roblox.com/asset/?id=144549739';
export_base_url = 'http://www.f3xteam.com/bt/export/%s';
-- Create the plugin
Plugin = plugin;
Toolbar = Plugin:CreateToolbar( 'Building Tools by F3X' );
Launcher = Toolbar:CreateButton( '', 'Import from Building Tools by F3X', plugin_icon );
local GUI;
------------------------------------------
-- Load external dependencies
------------------------------------------
RbxUtility = LoadLibrary( 'RbxUtility' );
Services.ContentProvider:Preload( bt_logo );
------------------------------------------
-- Define functions that are depended-upon
------------------------------------------
function _findTableOccurrences( haystack, needle )
-- Returns the positions of instances of `needle` in table `haystack`
local positions = {};
-- Add any indexes from `haystack` that have `needle`
for index, value in pairs( haystack ) do
if value == needle then
table.insert( positions, index );
end;
end;
return positions;
end;
------------------------------------------
-- Provide actual functionality
------------------------------------------
function showGUI( message, ok_text )
-- Make sure the UI isn't already open
if GUI then
hideGUI();
end;
local Create = RbxUtility.Create;
GUI = Create 'ScreenGui' {
Name = 'BTImportUI';
Parent = Services.CoreGui;
};
local Container = Create 'Frame' {
Name = 'Container';
Parent = GUI;
BackgroundTransparency = 1;
Size = UDim2.new( 1, 0, 0, 0 );
BorderSizePixel = 0;
ClipsDescendants = true;
};
local Highlight = Create 'Frame' {
Name = 'Highlight';
Parent = Container;
ZIndex = 2;
BackgroundColor3 = Color3.new( 1, 1, 1 );
BackgroundTransparency = 0.95;
BorderSizePixel = 0;
Position = UDim2.new( 0, 0, 0, 270 );
Size = UDim2.new( 1, 0, 0, 180 );
ZIndex = 2;
};
if message then
local OkButton = Create 'TextButton' {
Name = 'OkButton';
Parent = Highlight;
BackgroundColor3 = Color3.new( 1, 162 / 255, 47 / 255 );
BorderSizePixel = 0;
Position = UDim2.new( 0.3, 0, 1, -60 );
Size = UDim2.new( 0.4, 0, 0, 40 );
ZIndex = 2;
Font = Enum.Font.SourceSansBold;
FontSize = Enum.FontSize.Size24;
Text = ok_text;
TextColor3 = Color3.new( 1, 1, 1 );
TextStrokeColor3 = Color3.new( 0, 0, 0 );
TextStrokeTransparency = 0.85;
TextWrapped = true;
[Create.E 'MouseButton1Up'] = function ()
hideGUI();
end;
};
Create 'Frame' {
Name = 'Bevel';
Parent = OkButton;
BorderSizePixel = 0;
BackgroundColor3 = Color3.new( 184 / 255, 116 / 255, 33 / 255 );
Position = UDim2.new( 0, 0, 1, -2 );
Size = UDim2.new( 1, 0, 0, 2 );
ZIndex = 2;
};
local Message = Create 'TextLabel' {
Name = 'Message';
Parent = Highlight;
BackgroundTransparency = 1;
BorderSizePixel = 0;
Position = UDim2.new( 0.1, 0, 0, 0 );
Size = UDim2.new( 0.8, 0, 1, -30 );
Font = Enum.Font.Arial;
FontSize = Enum.FontSize.Size24;
Text = message;
TextColor3 = Color3.new( 1, 1, 1 );
TextWrapped = true;
};
end;
local Interface = Create 'Frame' {
Name = 'Interface';
Parent = Container;
ZIndex = 2;
Size = UDim2.new( 0.6, 0, 0, 110 );
Position = UDim2.new( 0.2, 0, 0, 300 );
BorderSizePixel = 0;
BackgroundTransparency = 1;
Visible = not message;
};
local CreationID = Create 'Frame' {
Name = 'CreationID';
Parent = Interface;
BackgroundTransparency = 1;
BorderSizePixel = 0;
Position = UDim2.new( 0, 0, 0, 10 );
Size = UDim2.new( 1, 0, 0, 30 );
ZIndex = 2;
};
Create 'Frame' {
Name = 'BottomBorder';
Parent = CreationID;
BorderSizePixel = 0;
BackgroundColor3 = Color3.new( 1, 162 / 255, 47 / 255 );
Position = UDim2.new( 0, 0, 1, -1 );
Size = UDim2.new( 1, 0, 0, 1 );
ZIndex = 2;
};
Create 'Frame' {
Name = 'LeftBorder';
Parent = CreationID;
BorderSizePixel = 0;
BackgroundColor3 = Color3.new( 1, 162 / 255, 47 / 255 );
Position = UDim2.new( 0, 0, 0.8, 0 );
Size = UDim2.new( 0, 1, 0.2, 0 );
ZIndex = 2;
};
Create 'Frame' {
Name = 'RightBorder';
Parent = CreationID;
BorderSizePixel = 0;
BackgroundColor3 = Color3.new( 1, 162 / 255, 47 / 255 );
Position = UDim2.new( 1, -1, 0.8, 0 );
Size = UDim2.new( 0, 1, 0.2, 0 );
ZIndex = 2;
};
local TextBox;
TextBox = Create 'TextBox' {
Parent = CreationID;
BackgroundTransparency = 1;
BorderSizePixel = 0;
ClearTextOnFocus = true;
Position = UDim2.new( 0, 10, 0, -10 );
Size = UDim2.new( 1, -10, 1, 10 );
ZIndex = 2;
Font = Enum.Font.SourceSans;
FontSize = Enum.FontSize.Size24;
Text = "What's your creation's ID?";
TextColor3 = Color3.new( 1, 1, 1 );
TextTransparency = 0.3;
TextXAlignment = Enum.TextXAlignment.Left;
[Create.E 'FocusLost'] = function ( enter_pressed )
if not enter_pressed then
return;
end;
import( TextBox.Text:lower() );
end;
};
local CancelButton = Create 'TextButton' {
Parent = Interface;
BackgroundColor3 = Color3.new( 1, 162 / 255, 47 / 255 );
BackgroundTransparency = 0.3;
BorderSizePixel = 0;
Name = 'CancelButton';
Position = UDim2.new( 0.55, 0, 0, 65 );
Size = UDim2.new( 0.45, 0, 0, 40 );
ZIndex = 2;
Font = Enum.Font.SourceSansBold;
FontSize = Enum.FontSize.Size24;
Text = 'Cancel';
TextColor3 = Color3.new( 1, 1, 1 );
TextStrokeColor3 = Color3.new( 0, 0, 0 );
TextStrokeTransparency = 0.85;
TextTransparency = 0.3;
[Create.E 'MouseButton1Up'] = function ()
hideGUI();
end;
};
Create 'Frame' {
Parent = CancelButton;
BackgroundColor3 = Color3.new( 131 / 255, 83 / 255, 23 / 255 );
BorderSizePixel = 0;
Name = 'Bevel';
Position = UDim2.new( 0, 0, 1, -2 );
Size = UDim2.new( 1, 0, 0, 2 );
ZIndex = 2;
};
local ImportButton = Create 'TextButton' {
Parent = Interface;
BackgroundColor3 = Color3.new( 1, 162 / 255, 47 / 255 );
BorderSizePixel = 0;
Name = 'ImportButton';
Position = UDim2.new( 0, 0, 0, 65 );
Size = UDim2.new( 0.45, 0, 0, 40 );
ZIndex = 2;
Font = Enum.Font.SourceSansBold;
FontSize = Enum.FontSize.Size24;
Text = 'Import';
TextColor3 = Color3.new( 1, 1, 1 );
TextStrokeColor3 = Color3.new( 0, 0, 0 );
TextStrokeTransparency = 0.85;
[Create.E 'MouseButton1Up'] = function ()
import( TextBox.Text:lower() );
end;
};
Create 'Frame' {
Parent = ImportButton;
BackgroundColor3 = Color3.new( 204 / 255, 129 / 255, 37 / 255 );
BorderSizePixel = 0;
Name = 'Bevel';
Position = UDim2.new( 0, 0, 1, -2 );
Size = UDim2.new( 1, 0, 0, 2 );
ZIndex = 2;
};
Create 'Frame' {
Name = 'Overlay';
Parent = Container;
BackgroundColor3 = Color3.new( 0, 0, 0 );
BackgroundTransparency = 0.25;
BorderSizePixel = 0;
Size = UDim2.new( 1, 0, 1, 0 );
};
local Logo = Create 'ImageLabel' {
Name = 'Logo';
Parent = Container;
BackgroundTransparency = 1;
BorderSizePixel = 0;
Image = bt_logo;
Position = UDim2.new( 0, GUI.AbsoluteSize.x / 2 - 128, 0, 0 );
Size = UDim2.new( 0, 256, 0, 256 );
ZIndex = 2;
};
GUI.Changed:connect( function ( property )
if property == 'AbsoluteSize' then
Logo.Position = UDim2.new( 0, GUI.AbsoluteSize.x / 2 - 128, 0, 0 );
end;
end );
Container:TweenSize( UDim2.new( 1, 0, 1, 0 ), Enum.EasingDirection.Out, Enum.EasingStyle.Bounce, 0.5, true );
end;
function hideGUI()
-- Hide the current GUI
if GUI then
GUI:Destroy();
end;
end;
Launcher.Click:connect( function ()
if SoloMode then
showGUI( 'Use regular Studio to import.', 'Sure' );
else
showGUI();
end;
end );
function import( creation_id )
hideGUI();
-- Try to download the creation
local creation_data;
local download_attempt, download_error = ypcall( function ()
creation_data = Services.HttpService:GetAsync( export_base_url:format( creation_id ) );
end );
-- Fail graciously
if not download_attempt and download_error == 'Http requests are not enabled' then
print 'Import from Building Tools by F3X: Please enable HTTP requests (see http://wiki.roblox.com/index.php?title=Sending_HTTP_requests#Http_requests_are_not_enabled)';
showGUI( 'Please enable HTTP requests (see Output)', 'Got it' );
return false;
end;
if not download_attempt then
print( 'Import from Building Tools by F3X (download request error): ' .. tostring( download_error ) );
showGUI( "We couldn't get your creation", 'Oh' );
return false;
end;
if not ( creation_data and type( creation_data ) == 'string' and creation_data:len() > 0 ) then
showGUI( "We couldn't get your creation", ':(' );
return false;
end;
if not pcall( function () creation_data = RbxUtility.DecodeJSON( creation_data ); end ) then
showGUI( "We couldn't get your creation", ":'(" );
return false;
end;
-- Create a container to hold the creation
local Container = Instance.new( 'Model', Services.Workspace );
Container.Name = 'BTExport';
if creation_data.version == 1 then
local objects = {};
for part_id, part_data in pairs( creation_data.parts ) do
local Part;
local part_type = part_data[1];
if part_type == 1 then
Part = Instance.new( "Part" );
elseif part_type == 2 then
Part = Instance.new( "TrussPart" );
elseif part_type == 3 then
Part = Instance.new( "WedgePart" );
elseif part_type == 4 then
Part = Instance.new( "CornerWedgePart" );
elseif part_type == 5 then
Part = Instance.new( "Part" );
Part.Shape = "Cylinder";
elseif part_type == 6 then
Part = Instance.new( "Part" );
Part.Shape = "Ball";
elseif part_type == 7 then
Part = Instance.new( "Seat" );
elseif part_type == 8 then
Part = Instance.new( "VehicleSeat" );
elseif part_type == 9 then
Part = Instance.new( "SpawnLocation" );
end;
objects[part_id] = Part;
if ( pcall( function () local test = Part.FormFactor; end ) ) then
Part.FormFactor = "Custom";
end;
Part.Size = Vector3.new( unpack( part_data[2] ) );
Part.CFrame = CFrame.new( unpack( part_data[3] ) );
Part.BrickColor = BrickColor.new( part_data[4] );
Part.Material = part_data[5];
Part.Anchored = part_data[6];
Part.CanCollide = part_data[7];
Part.Reflectance = part_data[8];
Part.Transparency = part_data[9];
Part.TopSurface = part_data[10];
Part.BottomSurface = part_data[11];
Part.LeftSurface = part_data[12];
Part.RightSurface = part_data[13];
Part.FrontSurface = part_data[14];
Part.BackSurface = part_data[15];
Part.Parent = Container;
-- Add the part ID if it's referenced somewhere else
if creation_data.welds then
for _, Weld in pairs( creation_data.welds ) do
if Weld[1] == part_id or Weld[2] == part_id then
RbxUtility.Create 'StringValue' {
Name = 'BTID';
Parent = Part;
Value = part_id;
};
break;
end;
end;
end;
end;
if creation_data.welds then
local weld_count = 0;
for _, __ in pairs( creation_data.welds ) do
weld_count = weld_count + 1;
end;
if weld_count > 0 then
local WeldScript = Instance.new( 'Script' );
WeldScript.Name = 'BTWelder';
WeldScript.Source = [[-- This script creates the welds between parts imported by the Building Tools by F3X plugin.
local create = LoadLibrary( 'RbxUtility' ).Create;
function _getAllDescendants( Parent )
-- Recursively gets all the descendants of `Parent` and returns them
local descendants = {};
for _, Child in pairs( Parent:GetChildren() ) do
-- Add the direct descendants of `Parent`
table.insert( descendants, Child );
-- Add the descendants of each child
for _, Subchild in pairs( _getAllDescendants( Child ) ) do
table.insert( descendants, Subchild );
end;
end;
return descendants;
end;
function findExportedPart( part_id )
for _, Object in pairs( _getAllDescendants( Game:GetService( 'Workspace' ) ) ) do
if Object:IsA( 'StringValue' ) then
if Object.Name == 'BTID' and Object.Value == part_id then
return Object.Parent;
end;
end;
end;
end;
]];
for weld_id, weld_data in pairs( creation_data.welds ) do
WeldScript.Source = WeldScript.Source .. [[
( function ()
local Part0 = findExportedPart( ']] .. weld_data[1] .. [[' );
local Part1 = findExportedPart( ']] .. weld_data[2] .. [[' );
if not Part0 or not Part1 then
return;
end;
create 'Weld' {
Name = 'BTWeld';
Parent = Game.JointsService;
Archivable = false;
Part0 = Part0;
Part1 = Part1;
C1 = CFrame.new( ]] .. table.concat( weld_data[3], ', ' ) .. [[ );
};
end )();
]];
end;
WeldScript.Parent = Container;
end;
end;
if creation_data.meshes then
for mesh_id, mesh_data in pairs( creation_data.meshes ) do
-- Create, place, and register the mesh
local Mesh = Instance.new( "SpecialMesh", objects[mesh_data[1]] );
objects[mesh_id] = Mesh;
-- Set the mesh's properties
Mesh.MeshType = mesh_data[2];
Mesh.Scale = Vector3.new( unpack( mesh_data[3] ) );
Mesh.MeshId = mesh_data[4];
Mesh.TextureId = mesh_data[5];
Mesh.VertexColor = Vector3.new( unpack( mesh_data[6] ) );
end;
end;
if creation_data.textures then
for texture_id, texture_data in pairs( creation_data.textures ) do
-- Create, place, and register the texture
local texture_class;
if texture_data[2] == 1 then
texture_class = 'Decal';
elseif texture_data[2] == 2 then
texture_class = 'Texture';
end;
local Texture = Instance.new( texture_class, objects[texture_data[1]] );
objects[texture_id] = Texture;
-- Set the texture's properties
Texture.Face = texture_data[3];
Texture.Texture = texture_data[4];
Texture.Transparency = texture_data[5];
if Texture:IsA( "Texture" ) then
Texture.StudsPerTileU = texture_data[6];
Texture.StudsPerTileV = texture_data[7];
end;
end;
end;
if creation_data.lights then
for light_id, light_data in pairs( creation_data.lights ) do
-- Create, place, and register the light
local light_class;
if light_data[2] == 1 then
light_class = 'PointLight';
elseif light_data[2] == 2 then
light_class = 'SpotLight';
end;
local Light = Instance.new( light_class, objects[light_data[1]] )
objects[light_id] = Light;
-- Set the light's properties
Light.Color = Color3.new( unpack( light_data[3] ) );
Light.Brightness = light_data[4];
Light.Range = light_data[5];
Light.Shadows = light_data[6];
if Light:IsA( 'SpotLight' ) then
Light.Angle = light_data[7];
Light.Face = light_data[8];
end;
end;
end;
if creation_data.decorations then
for decoration_id, decoration_data in pairs( creation_data.decorations ) do
-- Create and register the decoration
if decoration_data[2] == 1 then
local Smoke = RbxUtility.Create 'Smoke' {
Parent = objects[decoration_data[1]];
Color = Color3.new( unpack( decoration_data[3] ) );
Opacity = decoration_data[4];
RiseVelocity = decoration_data[5];
Size = decoration_data[6];
};
objects[decoration_id] = Smoke;
elseif decoration_data[2] == 2 then
local Fire = RbxUtility.Create 'Fire' {
Parent = objects[decoration_data[1]];
Color = Color3.new( unpack( decoration_data[3] ) );
SecondaryColor = Color3.new( unpack( decoration_data[4] ) );
Heat = decoration_data[5];
Size = decoration_data[6];
};
objects[decoration_id] = Fire;
elseif decoration_data[2] == 3 then
local Sparkles = RbxUtility.Create 'Sparkles' {
Parent = objects[decoration_data[1]];
SparkleColor = Color3.new( unpack( decoration_data[3] ) );
};
objects[decoration_id] = Sparkles;
end;
end;
end;
Container:MakeJoints();
Services.Selection:Set( { Container } );
end;
end;