-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.luau
More file actions
167 lines (135 loc) · 4.34 KB
/
Source.luau
File metadata and controls
167 lines (135 loc) · 4.34 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
--// Author: 8ch99
--// Date written: 19/01/2025
--// A module that merges all of roblox's enum instances into a custom and modifyable enum environment
--// This can be used for the smooth creation of custom enums, etc.
--[[
MIT License
Copyright (c) 2025 8ch99
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
--!strict
local RobloxEnum = Enum --// because we are overriding it later
--// Typedefs
type Metatable = typeof(setmetatable({}, {}))
type RobloxEnum = Enum
export type Enum = Metatable & {
Name: string,
Value: number,
EnumType: EnumType,
}
export type EnumType = Metatable & {
[string]: Enum | RobloxEnum,
AddEnumItem: (EnumType, Enum) -> (),
GetEnumItems: (EnumType) -> { Enum },
}
--// @class Enum
local Enum = {}
Enum.__index = Enum
Enum.__meta = "Enum"
--[[
@TODO Create a new Enum instance
@param EnumType: The EnumType instance that this Enum should belong to
@param Name: The name of the enum
@param Value: The numerical value of the enum
@returns Enum
]]
function Enum.new(EnumType: EnumType, Name: string, Value: number): Enum
local self = setmetatable({} :: Enum, Enum)
self.EnumType = EnumType
self.Name = Name
self.Value = Value or 0
EnumType:AddEnumItem(self)
return self
end
--// @class EnumType
local EnumType = {}
EnumType.__index = EnumType
EnumType.__meta = "EnumType"
--[[
@TODO Create a new EnumType instance
@returns EnumType
]]
function EnumType.new(): EnumType
return setmetatable({} :: EnumType, EnumType)
end
--[[
@TODO Adds an Enum to the EnumType
@param Enum: The enum to be added
]]
function EnumType:AddEnumItem(Enum: Enum)
self[Enum.Name] = Enum
end
--[[
@TODO Gets an array of all Enum's within this EnumType
@returns The enum items found
]]
function EnumType:GetEnumItems(): {}
local EnumItems = {}
for _, Enum in self do
if Enum.__meta == "Enum" then
table.insert(EnumItems, Enum)
end
end
return EnumItems
end
--// @module Enums
local Enums = {}
--[[
@TODO Creates a new Enum, if a custom/roblox one doesn't exist already, and adds it to an EnumType if it exists or creates the new EnumType if it doesn't exist
@param GivenEnumType: The name of the enum type to add the new Enum to
@param Name: The name of the enum
@param Value: The numerical value of the enum
@returns The new Enum
]]
function Enums:Create(GivenEnumType: string, Name: string, Value: number)
if GivenEnumType == "Create" or GivenEnumType == "GetEnums" then
return warn(`Cannot name enum type {GivenEnumType}!`)
end
local RealEnumType = self[GivenEnumType]
if typeof(RealEnumType) == "Enum" then
return warn(`Cannot add custom enum to Roblox EnumType! ({GivenEnumType})`)
end
if not RealEnumType then
RealEnumType = EnumType.new()
end
if RealEnumType[Name] then
return warn(`Enum '{Name}' already exists in '{GivenEnumType}'!`)
end
return Enum.new(RealEnumType, Name, Value)
end
--[[
@TODO Fetches a list of every existing EnumType within the Enums module, including roblox enums
@returns The compiled array of EnumTypes
]]
function Enums:GetEnums()
local Enums = {}
for _, EnumType in self do
if typeof(EnumType) == "Enum" then
table.insert(Enums, EnumType)
continue
end
if EnumType.__meta == "EnumType" then
table.insert(Enums, EnumType)
end
end
return Enums
end
--// load in roblox enums
for _, RobloxEnumType in ipairs(RobloxEnum:GetEnums()) do
Enums[tostring(RobloxEnumType)] = RobloxEnumType
end
return Enums :: typeof(Enums)