diff --git a/.luarc.json b/.luarc.json index c86e93f..3e37ce2 100644 --- a/.luarc.json +++ b/.luarc.json @@ -32,6 +32,7 @@ "new", "current", "async", - "super" + "super", + "isInstanceOf" ] } \ No newline at end of file diff --git a/README.md b/README.md index dc07de6..f5f7405 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ Atomic will automatically load available packages. Optional dependencies: - `MySQL` support / [MySQLOO](https://github.com/FredyH/MySQLOO) -- `git` support / [gm_git](https://github.com/TeamMeadows/gm_git) --- diff --git a/lua/atomic/libraries/class.lua b/lua/atomic/libraries/class.lua index 9d42337..e138393 100644 --- a/lua/atomic/libraries/class.lua +++ b/lua/atomic/libraries/class.lua @@ -33,6 +33,48 @@ function classMt:__classname() return tostring(self._classname) end +--- Calls a current method of the parent class +--- +--- # Example +--- ```lua +--- local Player = package:getClass("Player") +--- local User = package:class("User", Player) -- creating new class `User` that inherits class `Player` +--- +--- function User:init(userData) +--- self:super(userData) -- calling `Player:init` with argument `userData` +--- end +--- +--- function User:say(message) +--- self:super(message) -- calling `Player:say` with argument `message` +--- end +--- +--- function User:die(reason) +--- -- you can also use the global `super` function +--- -- essentially, it does the same thing as `Class.super` +--- -- this function is needed to maintain compatibility with older versions +--- super(self, reason) +--- end +--- ``` +---@protected +---@vararg any +function classMt:super(...) + local stack = debug.getinfo(2) + local methodName = stack.name + + if (not methodName) then + return + end + + local parent = atomic.class.getParent(self) + local method = parent and parent[methodName] + + if (not parent or not method) then + return + end + + method(self, ...) +end + --- Creates new class ---@param name string ---@param parent Atomic.Class? @@ -173,24 +215,11 @@ function atomic.class.getParent(class) return parent and parent.__index end ---- Calls the `init` method in the parent class ---- ---- # Example ---- ```lua ---- local Player = package:getClass("Player") ---- local User = package:class("User", Player) -- creating new class `User` that inherits class `Player` ---- ---- function User:init() ---- super(self) -- calling Player:init() method ---- end ---- ``` ----@param instance Atomic.Class ----@vararg ... -function atomic.class.super(instance, ...) - local parent = atomic.class.getParent(instance) - - assert(parent, "class has no parent") - assert(isfunction(parent.init), "parent class has no init method") +-- just compatibility for older versions +super = classMt.super - parent.init(instance, ...) +---@param instance any +---@param class Atomic.Class +function atomic.class.isInstanceOf(instance, class) + return getmetatable(instance) == class end \ No newline at end of file diff --git a/lua/atomic/libraries/git.lua b/lua/atomic/libraries/git.lua deleted file mode 100644 index d8a53ed..0000000 --- a/lua/atomic/libraries/git.lua +++ /dev/null @@ -1,68 +0,0 @@ -if (not util.IsBinaryModuleInstalled("git")) then - return atomic.log:trace("gm_git is not installed") -end - ----@class Atomic.Git.Folder ----@field folder string ----@field get_branch fun(self): string ----@field get_commit_id fun(self): string ----@field fetch fun(self): string? Error - -if (not git) then - require("git") -end - -atomic.git = atomic.git or { - logger = atomic.logger.new("git") -} - ----@type table -local openers = { - root = function() - return git.open("garrysmod") - end, - - gamemode = function(name) - return git.open("garrysmod/gamemodes/" .. name) - end, - - addon = function(name) - return git.open("garrysmod/addons/" .. name) - end -} - -atomic.git._storage = atomic.git._storage or { - root = {}, - gamemode = {}, - addon = {} -} - ----@param kind "root" | "gamemode" | "addon" ----@param name string? ----@return Atomic.Git.Folder? -function atomic.git.from(kind, name) - name = name or "" - - local opener = openers[kind] - - if (not opener) then - error("no opener with name `" .. tostring(kind) .. "`") - end - - local cache = atomic.git._storage[kind][name] - - if (cache) then - return cache - end - - local folder, err = opener(name) - - if (not folder) then - local who = name and kind .. " " .. name or kind - return atomic.git.logger:err("failed to open git repository for %s: %s", who, tostring(err)) - end - - atomic.git._storage[kind][name] = folder - - return folder -end \ No newline at end of file diff --git a/lua/atomic/utils/aliases.lua b/lua/atomic/utils/aliases.lua index 34c8af9..d31be04 100644 --- a/lua/atomic/utils/aliases.lua +++ b/lua/atomic/utils/aliases.lua @@ -2,6 +2,7 @@ new = atomic.class.new super = atomic.class.super current = atomic.package.current async = coroutine.start +isInstanceOf = atomic.class.isInstanceOf Instant = atomic.time.newInstant Duration = atomic.time.newDuration diff --git a/lua/autorun/atomic_autorun.lua b/lua/autorun/atomic_autorun.lua index e7387eb..617919d 100644 --- a/lua/autorun/atomic_autorun.lua +++ b/lua/autorun/atomic_autorun.lua @@ -2,7 +2,7 @@ atomic = { meta = { author = "smokingplaya", versionName = "Cherry", - version = "1.0.0-alpha.4", + version = "1.0.0-alpha.5", }, _config = {} } @@ -45,7 +45,6 @@ client("atomic/libraries/webview/class.lua") client("atomic/libraries/webview/common.lua") client("atomic/libraries/webview/basicfuncs.lua") server("atomic/libraries/command/common.lua") -server("atomic/libraries/git.lua") -- package loading should be one of the latest shared("atomic/libraries/package/common.lua") server("atomic/libraries/mysql.lua")