lune-std-roblox 0.2.4

Lune standard library - Roblox
Documentation
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
export type DatabaseScriptability = "None" | "Custom" | "Read" | "ReadWrite" | "Write"

export type DatabasePropertyTag =
	"Deprecated"
	| "Hidden"
	| "NotBrowsable"
	| "NotReplicated"
	| "NotScriptable"
	| "ReadOnly"
	| "WriteOnly"

export type DatabaseClassTag =
	"Deprecated"
	| "NotBrowsable"
	| "NotCreatable"
	| "NotReplicated"
	| "PlayerReplicated"
	| "Service"
	| "Settings"
	| "UserSettings"

export type DatabaseProperty = {
	--[=[
		The name of the property.
	]=]
	Name: string,
	--[=[
		The datatype of the property.

		For normal datatypes this will be a string such as `string`, `Color3`, ...

		For enums this will be a string formatted as `Enum.EnumName`.
	]=]
	Datatype: string,
	--[=[
		The scriptability of this property, meaning if it can be written / read at runtime.

		All properties are writable and readable in Lune even if scriptability is not.
	]=]
	Scriptability: DatabaseScriptability,
	--[=[
		Tags describing the property.

		These include information such as if the property can be replicated to players
		at runtime, if the property should be hidden in Roblox Studio, and more.
	]=]
	Tags: { DatabasePropertyTag },
}

export type DatabaseClass = {
	--[=[
		The name of the class.
	]=]
	Name: string,
	--[=[
		The superclass (parent class) of this class.

		May be nil if no parent class exists.
	]=]
	Superclass: string?,
	--[=[
		Known properties for this class.
	]=]
	Properties: { [string]: DatabaseProperty },
	--[=[
		Default values for properties of this class.

		Note that these default properties use Lune's built-in datatype
		userdatas, and that if there is a new datatype that Lune does
		not yet know about, it may be missing from this table.
	]=]
	DefaultProperties: { [string]: any },
	--[=[
		Tags describing the class.

		These include information such as if the class can be replicated
		to players at runtime, and top-level class categories.
	]=]
	Tags: { DatabaseClassTag },
}

export type DatabaseEnum = {
	--[=[
		The name of this enum, for example `PartType` or `UserInputState`.
	]=]
	Name: string,
	--[=[
		Members of this enum.

		Note that this is a direct map of name -> enum values,
		and does not actually use the EnumItem datatype itself.
	]=]
	Items: { [string]: number },
}

export type Database = {
	--[=[
		The current version of the reflection database.

		This will follow the format `x.y.z.w`, which most commonly looks something like `0.567.0.123456789`
	]=]
	Version: string,
	--[=[
		Retrieves a list of all currently known class names.
	]=]
	GetClassNames: (self: Database) -> { string },
	--[=[
		Retrieves a list of all currently known enum names.
	]=]
	GetEnumNames: (self: Database) -> { string },
	--[=[
		Gets a class with the exact given name, if one exists.
	]=]
	GetClass: (self: Database, name: string) -> DatabaseClass?,
	--[=[
		Gets an enum with the exact given name, if one exists.
	]=]
	GetEnum: (self: Database, name: string) -> DatabaseEnum?,
	--[=[
		Finds a class with the given name.

		This will use case-insensitive matching and ignore leading and trailing whitespace.
	]=]
	FindClass: (self: Database, name: string) -> DatabaseClass?,
	--[=[
		Finds an enum with the given name.

		This will use case-insensitive matching and ignore leading and trailing whitespace.
	]=]
	FindEnum: (self: Database, name: string) -> DatabaseEnum?,
}

type InstanceProperties = {
	Parent: Instance?,
	ClassName: string,
	Name: string,
	-- FIXME: This breaks intellisense, but we need some way to access
	-- instance properties without casting the entire instance to any...
	-- [string]: any,
}

type InstanceMetatable = {
	Clone: (self: Instance) -> Instance,
	Destroy: (self: Instance) -> (),
	ClearAllChildren: (self: Instance) -> (),

	GetChildren: (self: Instance) -> { Instance },
	GetDebugId: (self: Instance) -> string,
	GetDescendants: (self: Instance) -> { Instance },
	GetFullName: (self: Instance) -> string,

	FindFirstAncestor: (self: Instance, name: string) -> Instance?,
	FindFirstAncestorOfClass: (self: Instance, className: string) -> Instance?,
	FindFirstAncestorWhichIsA: (self: Instance, className: string) -> Instance?,
	FindFirstChild: (self: Instance, name: string, recursive: boolean?) -> Instance?,
	FindFirstChildOfClass: (self: Instance, className: string, recursive: boolean?) -> Instance?,
	FindFirstChildWhichIsA: (self: Instance, className: string, recursive: boolean?) -> Instance?,

	IsA: (self: Instance, className: string) -> boolean,
	IsAncestorOf: (self: Instance, descendant: Instance) -> boolean,
	IsDescendantOf: (self: Instance, ancestor: Instance) -> boolean,

	GetAttribute: (self: Instance, name: string) -> any,
	GetAttributes: (self: Instance) -> { [string]: any },
	SetAttribute: (self: Instance, name: string, value: any) -> (),

	GetTags: (self: Instance) -> { string },
	HasTag: (self: Instance, name: string) -> boolean,
	AddTag: (self: Instance, name: string) -> (),
	RemoveTag: (self: Instance, name: string) -> (),
}

export type Instance = typeof(setmetatable(
	(nil :: any) :: InstanceProperties,
	(nil :: any) :: { __index: InstanceMetatable }
))

export type DataModelProperties = {}
export type DataModelMetatable = {
	GetService: (self: DataModel, name: string) -> Instance,
	FindService: (self: DataModel, name: string) -> Instance?,
}

export type DataModel =
	Instance
	& typeof(setmetatable((nil :: any) :: DataModelProperties, (nil :: any) :: { __index: DataModelMetatable }))

--[=[
	@class Roblox

	Built-in library for manipulating Roblox place & model files

	### Example usage

	```lua
	local fs = require("@lune/fs")
	local roblox = require("@lune/roblox")

	-- Reading a place file
	local placeFile = fs.readFile("myPlaceFile.rbxl")
	local game = roblox.deserializePlace(placeFile)

	-- Manipulating and reading instances - just like in Roblox!
	local workspace = game:GetService("Workspace")
	for _, child in workspace:GetChildren() do
		print("Found child " .. child.Name .. " of class " .. child.ClassName)
	end

	-- Writing a place file
	local newPlaceFile = roblox.serializePlace(game)
	fs.writeFile("myPlaceFile.rbxl", newPlaceFile)
	```
]=]
local roblox = {}

--[=[
	@within Roblox
	@tag must_use

	Deserializes a place into a DataModel instance.

	This function accepts a string of contents, *not* a file path.
	If reading a place file from a file path is desired, `fs.readFile`
	can be used and the resulting string may be passed to this function.

	### Example usage

	```lua
	local fs = require("@lune/fs")
	local roblox = require("@lune/roblox")

	local placeFile = fs.readFile("filePath.rbxl")
	local game = roblox.deserializePlace(placeFile)
	```

	@param contents The contents of the place to read
]=]
function roblox.deserializePlace(contents: string): DataModel
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Deserializes a model into an array of instances.

	This function accepts a string of contents, *not* a file path.
	If reading a model file from a file path is desired, `fs.readFile`
	can be used and the resulting string may be passed to this function.

	### Example usage

	```lua
	local fs = require("@lune/fs")
	local roblox = require("@lune/roblox")

	local modelFile = fs.readFile("filePath.rbxm")
	local instances = roblox.deserializeModel(modelFile)
	```

	@param contents The contents of the model to read
]=]
function roblox.deserializeModel(contents: string): { Instance }
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Serializes a place from a DataModel instance.

	This string can then be written to a file, or sent over the network.

	### Example usage

	```lua
	local fs = require("@lune/fs")
	local roblox = require("@lune/roblox")

	local placeFile = roblox.serializePlace(game)
	fs.writeFile("filePath.rbxl", placeFile)
	```

	@param dataModel The DataModel for the place to serialize
	@param xml If the place should be serialized as xml or not. Defaults to `false`, meaning the place gets serialized using the binary format and not xml.
]=]
function roblox.serializePlace(dataModel: DataModel, xml: boolean?): string
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Serializes one or more instances as a model.

	This string can then be written to a file, or sent over the network.

	### Example usage

	```lua
	local fs = require("@lune/fs")
	local roblox = require("@lune/roblox")

	local modelFile = roblox.serializeModel({ instance1, instance2, ... })
	fs.writeFile("filePath.rbxm", modelFile)
	```

	@param instances The array of instances to serialize
	@param xml If the model should be serialized as xml or not. Defaults to `false`, meaning the model gets serialized using the binary format and not xml.
]=]
function roblox.serializeModel(instances: { Instance }, xml: boolean?): string
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Gets the current auth cookie, for usage with Roblox web APIs.

	Note that this auth cookie is formatted for use as a "Cookie" header,
	and that it contains restrictions so that it may only be used for
	official Roblox endpoints. To get the raw cookie value without any
	additional formatting, you can pass `true` as the first and only parameter.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")
	local serde = require("@lune/serde")
	local net = require("@lune/net")

	local cookie = roblox.getAuthCookie()
	assert(cookie ~= nil, "Failed to get roblox auth cookie")

	local myPrivatePlaceId = 1234567890

	local response = net.request({
		url = "https://assetdelivery.roblox.com/v2/assetId/" .. tostring(myPrivatePlaceId),
		headers = {
			Cookie = cookie,
		},
	})

	local responseTable = serde.decode("json", response.body)
	local responseLocation = responseTable.locations[1].location
	print("Download link to place: " .. responseLocation)
	```

	@param raw If the cookie should be returned as a pure value or not. Defaults to false
]=]
function roblox.getAuthCookie(raw: boolean?): string?
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Gets the bundled reflection database.

	This database contains information about Roblox enums, classes, and their properties.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local db = roblox.getReflectionDatabase()

	print("There are", #db:GetClassNames(), "classes in the reflection database")

	print("All base instance properties:")

	local class = db:GetClass("Instance")
	for name, prop in class.Properties do
		print(string.format(
			"- %s with datatype %s and default value %s",
			prop.Name,
			prop.Datatype,
			tostring(class.DefaultProperties[prop.Name])
		))
	end
	```
]=]
function roblox.getReflectionDatabase(): Database
	return nil :: any
end

--[=[
	@within Roblox

	Implements a property for all instances of the given `className`.

	This takes into account class hierarchies, so implementing a property
	for the `BasePart` class will also implement it for `Part` and others,
	unless a more specific implementation is added to the `Part` class directly.

	### Behavior

	The given `getter` callback will be called each time the property is
	indexed, with the instance as its one and only argument. The `setter`
	callback, if given, will be called each time the property should be set,
	with the instance as the first argument and the property value as second.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local part = roblox.Instance.new("Part")

	local propertyValues = {}
	roblox.implementProperty(
		"BasePart",
		"CoolProp",
		function(instance)
			if propertyValues[instance] == nil then
				propertyValues[instance] = 0
			end
			propertyValues[instance] += 1
			return propertyValues[instance]
		end,
		function(instance, value)
			propertyValues[instance] = value
		end
	)

	print(part.CoolProp) --> 1
	print(part.CoolProp) --> 2
	print(part.CoolProp) --> 3

	part.CoolProp = 10

	print(part.CoolProp) --> 11
	print(part.CoolProp) --> 12
	print(part.CoolProp) --> 13
	```

	@param className The class to implement the property for.
	@param propertyName The name of the property to implement.
	@param getter The function which will be called to get the property value when indexed.
	@param setter The function which will be called to set the property value when indexed. Defaults to a function that will error with a message saying the property is read-only.
]=]
function roblox.implementProperty<T>(
	className: string,
	propertyName: string,
	getter: (instance: Instance) -> T,
	setter: ((instance: Instance, value: T) -> ())?
)
	return nil :: any
end

--[=[
	@within Roblox

	Implements a method for all instances of the given `className`.

	This takes into account class hierarchies, so implementing a method
	for the `BasePart` class will also implement it for `Part` and others,
	unless a more specific implementation is added to the `Part` class directly.

	### Behavior

	The given `callback` will be called every time the method is called,
	and will receive the instance it was called on as its first argument.
	The remaining arguments will be what the caller passed to the method, and
	all values returned from the callback will then be returned to the caller.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local part = roblox.Instance.new("Part")

	roblox.implementMethod("BasePart", "TestMethod", function(instance, ...)
	    print("Called TestMethod on instance", instance, "with", ...)
	end)

	part:TestMethod("Hello", "world!")
	--> Called TestMethod on instance Part with Hello, world!
	```

	@param className The class to implement the method for.
	@param methodName The name of the method to implement.
	@param callback The function which will be called when the method is called.
]=]
function roblox.implementMethod(className: string, methodName: string, callback: (instance: Instance, ...any) -> ...any)
	return nil :: any
end

-- TODO: Make typedefs for all of the datatypes as well...
roblox.Instance = (nil :: any) :: {
	new: ((className: "DataModel") -> DataModel) & ((className: string) -> Instance),
}

--[=[
	@within Roblox
	@tag must_use

	Returns the path to the system's Roblox Studio executable.

	There is no guarantee that this will exist, but if Studio is installed this
	is where it will be.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local pathToStudio = roblox.studioApplicationPath()
	print("Studio is located at:", pathToStudio)
	```
]=]
function roblox.studioApplicationPath(): string
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Returns the path to the `Content` folder of the system's current Studio
	install.

	This folder will always exist if Studio is installed.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local pathToContent = roblox.studioContentPath()
	print("Studio's content folder is located at:", pathToContent)
	```
]=]
function roblox.studioContentPath(): string
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Returns the path to the `plugin` folder of the system's current Studio
	install. This is the path where local plugins are installed.

	This folder may not exist if the user has never installed a local plugin.
	It will also not necessarily take into account custom plugin directories
	set from Studio.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local pathToPluginFolder = roblox.studioPluginPath()
	print("Studio's plugin folder is located at:", pathToPluginFolder)
	```
]=]
function roblox.studioPluginPath(): string
	return nil :: any
end

--[=[
	@within Roblox
	@tag must_use

	Returns the path to the `BuiltInPlugin` folder of the system's current
	Studio install. This is the path where built-in plugins like the ToolBox
	are installed.

	This folder will always exist if Studio is installed.

	### Example usage

	```lua
	local roblox = require("@lune/roblox")

	local pathToPluginFolder = roblox.studioBuiltinPluginPath()
	print("Studio's built-in plugin folder is located at:", pathToPluginFolder)
	```
]=]
function roblox.studioBuiltinPluginPath(): string
	return nil :: any
end

return roblox