version '0.0.7'

This commit is contained in:
MrNewb committed 2025-01-08 06:33:31 -05:00
1 parent 89a9a371bd
commit 47977ce701
105 files changed
+5036 -1

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
local Tables = Require('use/tables.lua')
local Ids = Require("use/ids.lua")
local AllInstances = {}
---@class Class
Class = {}
Class.ClassName = "Class"
Class.__call = function(this, ...)
return this:new(...)
end
Builders = {}
function Class.SetInstance(instance)
AllInstances[instance.id] = instance
end
function Class.GetInstance(id)
return AllInstances[id]
end
--- Default (empty) constructor
--- @param self Class
--- @vararg any
function Class:init(...) end
--- Creates a subclass of the current class.
--- This function copies the properties and methods of the current class to a new object.
--- It also sets up metatables to allow for object-oriented behavior, such as method calls and property access.
--- @param obj object | nil: The table to extend. If nil, a new table will be created.
--- @return obj object: The extended object with properties and methods from the given class obj.
function Class:extend(obj)
local className = obj.ClassName or "Class"
local old = Tables.DeepClone(self, {}, {components = true})
local new = Tables.DeepClone(obj, old, {ClassName = true})
new.components = self.components or {}
new.components[className] = obj
if not new.id then new.id = Ids.CreateUniqueId(new.components) end
-- create new objects directly, like o = Object()
self = setmetatable(new, Class)
if not Class.GetInstance(new.id) then Class.SetInstance(self) end
return self
end
--- Sets a property on the object. NOTE: This is mostly for internal purposes.
--- @param prop string|table: The property name or a table of properties to set.
--- @param value any: The value to set the property to.
function Class:set(prop, value)
if not value and type(prop) == "table" then
for k, v in pairs(prop) do
rawset(self._, k, v)
end
else
rawset(self._, prop, value)
end
end
--- This function is used to create a new instance of the class.
--- It calls the init function if it exists.
--- @vararg any: The arguments to pass to the init
--- @return object: The new instance of the class.
function Class:new(...)
local obj = self:extend({})
if obj.init then obj:init(...) end
return obj
end
--- This function is used to create a new instance of the class.
--- It calls the struct function if it exists.
--- @vararg object(s): List of objects to extend the class with.
--- @return object: The new instance of the class with the data and functions from the objects passed.
function class(className, ...)
local newObj = Class:extend({})
local components = table.pack(...)
newObj.ClassName = className
if components then
for k, v in pairs(components) do
local clName = v.ClassName
if not Builders[clName] then -- builders are used to reconstruct an object usually when pulled from server to client or vice versa
Builders[clName] = Tables.DeepClone(newObj)
end
end
end
if components.n > 0 then
for k, v in pairs(components) do
if k ~= "n" then
local newNewObject = nil
if v.Struct and type(v.Struct) == "function" then
newNewObject = v:Struct()
end
newObj = newObj:extend(newNewObject or v)
end
end
else
return newObj
end
return newObj
end
function reconstruct(obj)
local newTable = {}
local tbl = obj.components or {}
for k, v in pairs(tbl) do
local builder = Builders[k]
if builder then
newTable[k] = builder
end
end
return class(table.unpack(newTable))
end
--- Overwrites the type(self) value to return "Class".
--- @return string: The string "Class".
function Class:__type()
return self.ClassName or "Class"
end
--- Overwrites the call operator to call the new function. Ex: Class()
--- @vararg any: The arguments to pass to the new function.
--- @return object: The new instance of the class.
function Class:__call(...)
return self:new(...)
end
--- Checks to see if the class is an instance of the given class.
--- @param cls Class: The class to check against.
--- @return boolean: True if the class is an instance of the given class, false otherwise.
function Class:IsA(cls)
local p = promise.new()
CreateThread(function()
local m = getmetatable(self)
while m do
if m == cls then
return p:resolve(true)
end
m = getmetatable(m)
Wait(0)
end
return p:resolve(false)
end)
return Citizen.Await(p)
end
return {Class, class}
+38
View File
@@ -0,0 +1,38 @@
local classModule = Require("components/class.lua")
local _, class = table.unpack(classModule)
-- Registry to store singletons for classes
local SingletonRegistry = {}
--- @class Singleton
local Singleton = class()
Singleton.ClassName = "Singleton"
--- Ensures the class instance is a singleton.
--- If an instance already exists, it returns the existing one.
--- If not, it initializes and registers a new singleton.
function Singleton:Struct()
local className = self.ClassName or "UnnamedClass"
-- Check if the singleton already exists
if SingletonRegistry[className] then
self = SingletonRegistry[className]
return self
end
-- Register this instance as the singleton for its class
SingletonRegistry[className] = self
return self
end
--- Clears the singleton instance for this class (if needed).
function Singleton:ClearSingleton()
local destroy = self.Destroy
if destroy and type(destroy) == "function" then self:Destroy() end
local className = self.ClassName or "UnnamedClass"
SingletonRegistry[className] = nil
end
--- Returns the existing singleton for a class.
--- @param className string: The name of the class to check.
--- @return any: The singleton instance, or nil if none exists.
function Singleton.Get(className)
return SingletonRegistry[className]
end
return Singleton
+103
View File
@@ -0,0 +1,103 @@
local classModule = Require("components/class.lua")
local _, class = table.unpack(classModule)
local ids = Require("use/ids.lua")
local SyncObjects = {}
function GetSyncObject(syndId)
return SyncObjects[syndId]
end
function GetSyncObjects()
return SyncObjects
end
function SetSyncObject(syncId, sync)
SyncObjects[syncId] = sync
end
--- @class Sync
local Sync = class("Sync")
--- Initializes the Sync object.
function Sync:Struct()
self.syncId = ids.CreateUniqueId(SyncObjects)
self.sync = {}
self.updateWaitTime = 500
SyncObjects[self.syncId] = self
return self
end
--- Retrieves the value associated with the given key in the sync table.
--- @param key any: The key to retrieve the value for.
--- @return any: The value associated with the key.
function Sync:GetSyncKey(key)
return self.sync[key]
end
--- Called when a sync key is initialized.
--- @param key any: The key being initialized.
--- @param value any: The value being set.
function Sync:OnSyncKeyInit(key, value) end
--- Called when a sync key is updated.
--- @param key any: The key being updated.
--- @param oldValue any: The old value of the key.
--- @param newValue any: The new value of the key.
function Sync:OnSyncKeyUpdate(key, oldValue, newValue) end
--- Called when a sync key is removed.
--- @param key any: The key being removed.
function Sync:OnSyncKeyRemove(key) end
-- Server
if not IsDuplicityVersion() then goto client end
--- Sets a sync key and triggers a client event to update the sync table.
--- @param key any: The key to set.
--- @param value any: The value to set for the key.
function Sync:SetSynckey(key, value)
self.sync[key] = value
CreateThread(function()
SetTimeout(self.updateWaitTime, function()
TriggerClientEvent("Sync:Setup", -1, self.syncId, self.sync)
end)
end)
end
-- Client
if IsDuplicityVersion() then return Sync end
::client::
--- Compares the current sync table with a new table and updates accordingly.
--- @param newTable table: The new sync table to compare with.
function Sync:_CompareAndUpdate(newTable)
local oldTable = self.sync
for key, oldValue in pairs(oldTable) do
local newValue = newTable[key]
if newValue == nil then
self:OnSyncKeyRemove(key)
elseif oldValue ~= newValue then
self:OnSyncKeyUpdate(key, oldValue, newValue)
end
end
for key, newValue in pairs(newTable) do
if oldTable[key] == nil then
self:OnSyncKeyInit(key, newValue)
end
end
self.sync = newTable
end
RegisterNetEvent("Sync:Setup", function(syncId, sync)
local syncObj = GetSyncObject(syncId)
if not syncObj then
syncObj = reconstruct(Sync)
assert(syncObj, "Failed to sync: ", syncId)
syncObj.syncId = syncId
SetSyncObject(syncId, syncObj)
end
syncObj:_CompareAndUpdate(sync)
end)
return table.pack({Sync})