Skip to content

Commit 7a4df09

Browse files
authored
merge Guava 0.7.1
merge Guava 0.7.1
2 parents 5f266a1 + 4af4fe7 commit 7a4df09

22 files changed

Lines changed: 261 additions & 123 deletions

File tree

.luarc.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
"invisible"
2525
],
2626
"Lua.workspace.checkThirdParty": false,
27-
"diagnostics.globals": [
28-
"atomic",
29-
"git",
30-
"mysqloo",
31-
"todo",
32-
]
27+
"diagnostics.globals": [
28+
"atomic",
29+
"git",
30+
"mysqloo",
31+
"todo"
32+
]
3333
}

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,29 @@
1313
Atomic is a flexible, OOP-driven framework for building Garry’s Mod addons and gamemodes with a clean, modular architecture.
1414
Each **package** is a self-contained unit - just like an addon - with built-in dependency management and powerful libraries for writing structured, maintainable Lua code.
1515

16+
```lua
17+
local package = current()
18+
local libui = package:getDependency("com.developername.libui")
19+
20+
package:listen(function(self)
21+
libui:drawText(self:getPhrase("en", "hello_world"), libui.textSize.small, ScrW() / 2, ScrH()/2, color_white, TEXT_ALIGN_CENTER)
22+
end, "HUDPaint")
23+
24+
package:listen(function(self)
25+
self.logger:info("package successfully enabled")
26+
end, "onEnable")
27+
```
28+
###### Real example of addon based on Atomic
29+
1630
---
1731

1832
## Installation
1933
Simply place the framework in your `addons/` folder.
2034
Atomic will automatically load available packages.
2135

2236
Optional dependencies:
23-
- For `MySQL` support → install [MySQLOO](https://github.com/FredyH/MySQLOO)
24-
- For `git` support → install [gm_git](https://github.com/TeamMeadows/gm_git)
37+
- `MySQL` support / [MySQLOO](https://github.com/FredyH/MySQLOO)
38+
- `git` support / [gm_git](https://github.com/TeamMeadows/gm_git)
2539

2640
---
2741

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
---@class ExamplePackagePing: Atomic.Package
22
local package = current()
33

4+
-- Asynchronous variant
5+
46
--- this function will send
57
--- an asynchronous network message to the server
68
--- and wait for a response
7-
function package:ping()
9+
function package:pingAsync()
810
local instant = Instant()
911

1012
async(function()
1113
local message = self:sendNetworkMessageAsync("Ping", {})
1214

1315
local ms = instant:elapsed():as_millis()
1416

15-
package.logger:debug("Server ping took %sms", ms)
16-
package.logger:debug("Server response: %s", message.content)
17+
self.logger:debug("Server ping took %sms", ms)
18+
self.logger:debug("Server response: %s", message.content)
1719
end)
1820
end
1921

20-
--- Removing console command
21-
--- when package will disabled
22-
package:listen(function()
23-
concommand.Remove("do_ping")
24-
end, "onDisable")
22+
concommand.Add("ping_async", function()
23+
package:pingAsync()
24+
end)
25+
26+
-- Synchronous variant
27+
28+
---@type table<string, Atomic.Time.Instant>
29+
package.syncMessagesId = {}
30+
31+
function package:ping()
32+
local instant = Instant()
33+
34+
-- every network message have id
35+
local messageId = self:sendNetworkMessage("Ping", {})
36+
37+
-- saving network packet sending time
38+
self.syncMessagesId[messageId] = instant
39+
end
40+
41+
package:onNetworkMessage(function(self, message)
42+
local response = message.content
43+
44+
local time = self.syncMessagesId[message:getId()]
45+
46+
if (time) then
47+
self.logger:debug("Server ping took %sms", time:elapsed():as_millis())
48+
end
49+
50+
self.logger:debug("Server response: %s", response)
51+
end, "Ping")
2552

26-
concommand.Add("do_ping", function()
27-
package:ping()
53+
concommand.Add("ping", function()
54+
package:ping()
2855
end)

examples/lua/atomic/packages/ping/core/shared.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ local package = current()
66
package:networkSchema("Ping")
77
-- server will send to client
88
-- field "content" with type of string
9-
:clientField("content", "string")
9+
:client("content", "string")
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
---@type Atomic.Package.Metadata
22
return {
33
id = "team.meadows.example_ping",
4-
title = "Example package - Ping",
4+
title = "Example: Ping",
55
version = "1.0.0",
66
files = {
77
client = { "core/client.lua" },
88
shared = { "core/shared.lua" },
99
server = { "core/server.lua" }
10+
},
11+
dependencies = {
12+
shared = {
13+
atomic = "~0.7.1"
14+
}
1015
}
1116
}

lua/atomic/libraries/class.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ atomic.class = atomic.class or {
1717

1818
---@class Atomic.Class
1919
---@field init fun(self: Atomic.Class, ...: any)?
20-
---@field private _name string?
20+
---@field private _classname string?
2121
local classMt = {}
2222
classMt.__index = classMt
2323

2424
function classMt:__tostring()
25-
return "class " .. tostring(self._name)
25+
return "class " .. self:__classname()
26+
end
27+
28+
---@return string
29+
function classMt:__classname()
30+
return tostring(self._classname)
2631
end
2732

2833
--- Creates new class
@@ -31,14 +36,10 @@ end
3136
---@return Atomic.Class
3237
function atomic.class.create(name, parent)
3338
-- new class inherited from classMt or parent (if provided)
34-
local class = setmetatable({ _name = name }, { __index = parent or classMt })
39+
local class = setmetatable({ _classname = name }, { __index = parent or classMt })
3540
class.__index = class
3641
class.__tostring = function(self)
37-
return "instance of " .. tostring(self._name)
38-
end
39-
40-
class.__classname = function(self)
41-
return name
42+
return "instance of " .. tostring(self._classname)
4243
end
4344

4445
return class

lua/atomic/libraries/command/class.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---@alias Atomic.Command.ArgumentKind "number" | "string" | "boolean" | "time" | "player"
2-
---@alias Atomic.Command.ExecuteFunc fun(executor: Player, arguments: table<string, Atomic.Command.ArgumentKind>): string?
2+
---@alias Atomic.Command.ArgumentTypes number | string | boolean | Player
3+
---@alias Atomic.Command.ExecuteFunc fun(executor: Player, arguments: table<string, Atomic.Command.ArgumentTypes>): string?
34
---@alias Atomic.Command.Argument { name: string, kind: Atomic.Command.ArgumentKind, isOptional: boolean }
45

56
---@class Atomic.Command: Atomic.Class
@@ -124,4 +125,9 @@ function Command:setEnabled(b)
124125
self._enabled = b
125126

126127
return self
128+
end
129+
130+
---@return boolean
131+
function Command:isEnabled()
132+
return self._enabled
127133
end

lua/atomic/libraries/git.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
if (not util.IsBinaryModuleInstalled("git")) then
2-
return
2+
return atomic.log:trace("gm_git is not installed")
33
end
44

55
---@class Atomic.Git.Folder

lua/atomic/libraries/i18n.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ end
125125
---@return string
126126
function atomic.i18n.getPhrase(language, phraseIndex, ...)
127127
if (isentity(language)) then
128-
language = atomic.i18n.getPlayerLanguage(player)
128+
---@diagnostic disable-next-line
129+
language = atomic.i18n.getPlayerLanguage(language)
129130
end
130131

131132
local langTable = atomic.i18n._storage[language]

lua/atomic/libraries/logger.lua

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ local function getcurrenttime()
3131
return os.date("%H:%M:%S")
3232
end
3333

34-
local iswin = jit.os == "Windows"
35-
3634
-- colors
37-
local white = iswin and Color(255, 255, 255) or "\27[37m"
38-
local trace = iswin and Color(128, 128, 128) or "\27[90m"
39-
local debug = iswin and Color(0, 255, 255) or "\27[36m"
40-
local info = iswin and Color(0, 255, 0) or "\27[32m"
41-
local warn = iswin and Color(255, 255, 0) or "\27[33m"
42-
local err = iswin and Color(255, 0, 0) or "\27[31m"
35+
-- *client game console doesn't support the ANSI escape codes
36+
local white = CLIENT and Color(255, 255, 255) or "\27[37m"
37+
local trace = CLIENT and Color(128, 128, 128) or "\27[90m"
38+
local debug = CLIENT and Color(0, 255, 255) or "\27[36m"
39+
local info = CLIENT and Color(0, 255, 0) or "\27[32m"
40+
local warn = CLIENT and Color(255, 255, 0) or "\27[33m"
41+
local err = CLIENT and Color(255, 0, 0) or "\27[31m"
4342

4443
local levels = {
4544
TRACE = 1,
@@ -49,13 +48,16 @@ local levels = {
4948
ERR = 5,
5049
}
5150

52-
local logvar = CreateConVar("atomic_log", "INFO", {FCVAR_ARCHIVE, FCVAR_PROTECTED}, "Minimum log level (INFO, DEBUG, WARN, ERR)")
51+
local logvar = CreateConVar("atomic_log", "INFO", {FCVAR_ARCHIVE, FCVAR_PROTECTED}, "Minimum log level (TRACE/INFO/DEBUG/WARN/ERR)")
5352

5453
---@param prefix string
5554
function Logger:init(prefix)
5655
self.prefix = prefix
5756
end
5857

58+
-- not a magic number
59+
local MAX_LEVEL_LENGTH = 5
60+
5961
---@protected
6062
---@param color Color | string
6163
---@param level string
@@ -66,9 +68,11 @@ function Logger:log(color, level, message, ...)
6668
local currentIdx = levels[currentLevel] or 1
6769
local msgIdx = levels[level] or 1
6870

69-
if msgIdx < currentIdx then return end
71+
if (msgIdx < currentIdx) then
72+
return
73+
end
7074

71-
MsgC(white, "[", getcurrenttime(), " ", color, level, " ", white, self.prefix, "]", " ", string.format(message, ...))
75+
MsgC(white, "[", getcurrenttime(), " ", color, level .. (" "):rep(MAX_LEVEL_LENGTH - #level), " ", white, self.prefix, "]", " ", string.format(message, ...))
7276
MsgN()
7377
end
7478

0 commit comments

Comments
 (0)