From f8c507b546af533a0a6e4086f0f9e80fc14bc665 Mon Sep 17 00:00:00 2001 From: Tom O'Sullivan Date: Sat, 5 Mar 2022 23:36:30 +0000 Subject: [PATCH 1/3] chore: update copyright year --- class_loader.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class_loader.lua b/class_loader.lua index 713d429..65a546f 100644 --- a/class_loader.lua +++ b/class_loader.lua @@ -1,5 +1,5 @@ --[[----------------------------------------------------------------------------- - Copyright 2021 Thomas (Tom.bat) O'Sullivan + Copyright 2022 Thomas (Tom.bat) O'Sullivan Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From c26d2aed3a4db7badb39f9f1a7219c7b9060aa82 Mon Sep 17 00:00:00 2001 From: Tom O'Sullivan Date: Sat, 5 Mar 2022 23:37:50 +0000 Subject: [PATCH 2/3] fix: metadata checks --- class_loader.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/class_loader.lua b/class_loader.lua index 65a546f..eb57d9d 100644 --- a/class_loader.lua +++ b/class_loader.lua @@ -64,7 +64,7 @@ do local objects = {} for objectName, object in pairs(packages[packageName]) do - if objectName:sub(2) ~= "__" then + if objectName:sub(0, 2) ~= "__" then currentFile[objectName] = object table.insert(objects, object) end @@ -285,7 +285,7 @@ do packages[packageName] = nil for key, _ in pairs(package) do - if key:sub(2) ~= "__" then --If our package only contains metadata it's safe to delete + if key:sub(0, 2) ~= "__" then --If our package only contains metadata it's safe to delete packages[packageName] = package break end From 97a99efba8507505dfd3bce14e30cc8389ed79e4 Mon Sep 17 00:00:00 2001 From: Tom O'Sullivan Date: Sat, 5 Mar 2022 23:38:26 +0000 Subject: [PATCH 3/3] feat: improved enums --- class_loader.lua | 100 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/class_loader.lua b/class_loader.lua index eb57d9d..6de2ec7 100644 --- a/class_loader.lua +++ b/class_loader.lua @@ -113,6 +113,22 @@ do return setmetatable({}, class) end + local Enum = {} + Enum.__type = "Enum" + Enum.__isEnum = true + Enum.__index = Enum + + function Enum:GetValues() + return self._values + end + + function Enum:GetValueOf(name) + local value = self[name] + if istable(value) and value.__type == "EnumValue" then + return value + end + end + local baseEnvironment = { Import = ClassLoader.ImportObject, Class = function() @@ -134,11 +150,7 @@ do return singleton end, Enum = function() - local enum = getCurrentFileObject() - enum.__type = "Enum" - enum.__isEnum = true - - return enum + return setmetatable(getCurrentFileObject(), Enum) end } @@ -205,6 +217,80 @@ do return currentFile end + local EnumValue = {} + EnumValue.__type = "EnumValue" + EnumValue.__isEnumValue = true + EnumValue.__index = EnumValue + + local function newEnumValue(name, value, declaringClass) + local tbl = setmetatable({}, EnumValue) + + tbl._name = name + tbl._value = value + tbl._declaringClass = declaringClass + + return tbl + end + + function EnumValue:GetName() + return self._name + end + + function EnumValue:GetValue() + return self._value + end + + function EnumValue:GetDeclaringClass() + return self._declaringClass + end + + function EnumValue:__add(other) + return self._value + other._value + end + + function EnumValue:__sub(other) + return self._value - other._value + end + + function EnumValue:__mul(other) + return self._value * other._value + end + + function EnumValue:__div(other) + return self._value / other._value + end + + function EnumValue:__mod(other) + return self._value % other._value + end + + function EnumValue:__eq(other) + return self._value == other._value + end + + function EnumValue:__lt(other) + return self._value < other._value + end + + function EnumValue:__le(other) + return self._value <= other._value + end + + local function setupEnum(enum) + local values = {} + + for name, value in pairs(enum) do + if name:sub(0, 2) ~= "__" then --Ignore type, isEnum, etc + local enumValue = newEnumValue(name, value, enum) + + enum[name] = enumValue + table.insert(values, enumValue) + end + end + + enum._values = values + end + local function registerObject(packageName, objectName, object) assert(istable(object), "Invalid object returned by: " .. packageName .. "." .. objectName) @@ -215,6 +301,10 @@ do object.Extends = nil end + if object.__type == "Enum" then + setupEnum(object) + end + currentPackage[objectName] = object end