cb-lib.lua
216 lines
if _G.CBLib ~= nil then return _G.CBLib end --Prevent Lua refresh.
if _G.CBLib ~= nil then return _G.CBLib end -- Prevent Lua refresh.
local CBLib = {}
local CBLib = {}
--[[-------------------------------------------------------------------------
--[[-------------------------------------------------------------------------
Helper functions
Helper Functions
---------------------------------------------------------------------------]]
---------------------------------------------------------------------------]]
CBLib.Helper = {}
CBLib.Helper = {}
--Formats a number as a string with commas inserted
-- Formats a number as a string with commas inserted
function CBLib.Helper.CommaFormatNumber(amount)
function CBLib.Helper.CommaFormatNumber(amount)
local formatted = amount
local formatted = amount
while true do
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
if (k == 0) then
break
break
end
end
end
end
return formatted
return formatted
end
end
--Credit to facepunch (Didnt see guys name)
-- Credit to Facepunch (author unknown)
--Retursn the text in a table for, an index for each new line
-- Returns the text in a table form, an index for each new line
function CBLib.Helper.WrapText(Str,font,width)
function CBLib.Helper.WrapText(Str, font, width)
if( font ) then --Dr Magnusson's much less prone to failure and more optimized version
if font then -- Dr. Magnusson's more optimized version
surface.SetFont( font )
surface.SetFont(font)
end
end
local tbl, len, Start, End = {}, string.len( Str ), 1, 1
local tbl, len, Start, End = {}, string.len(Str), 1, 1
while ( End < len ) do
while (End < len) do
End = End + 1
End = End + 1
if ( surface.GetTextSize( string.sub( Str, Start, End ) ) > width ) then
if (surface.GetTextSize(string.sub(Str, Start, End)) > width) then
local n = string.sub( Str, End, End )
local n = string.sub(Str, End, End)
local I = 0
local I = 0
for i = 1, 15 do
for i = 1, 15 do
I = i
I = i
if( n != " " and n != "," and n != "." and n != "\n" ) then
if (n ~= " " and n ~= "," and n ~= "." and n ~= "\n") then
End = End - 1
End = End - 1
n = string.sub( Str, End, End )
n = string.sub(Str, End, End)
else
else
break
break
end
end
end
end
if( I == 15 ) then
if (I == 15) then
End = End + 14
End = End + 14
end
end
local FnlStr = string.Trim( string.sub( Str, Start, End ) )
local FnlStr = string.Trim(string.sub(Str, Start, End))
table.insert( tbl, FnlStr )
table.insert(tbl, FnlStr)
Start = End + 1
Start = End + 1
end
end
end
end
table.insert( tbl, string.sub( Str, Start, End ) )
table.insert(tbl, string.sub(Str, Start, End))
return tbl
return tbl
end
end
-----------------------------
-----------------------------
-- MODULES --
-- MODULES --
-----------------------------
-----------------------------
--Modules are basicly instances of a table. This handles loading and destroying modules.
-- Modules are basically instances of a table. This handles loading and destroying modules.
--Or anouther way to look at it is like a static class in an oop language.
-- Another way to look at it is like a static class in an OOP language.
--A module can also contain three event functions, OnLoaded, OnUnloaded, and OnReloaded
-- A module can also contain three event functions: OnLoaded, OnUnloaded, and OnReloaded.
--A Table for all modules loaded.
-- A table for all modules loaded.
CBLib.Modules = {}
CBLib.Modules = {}
--loads a module script and stores a reference, if a module is loaded of the same name then
-- Loads a module script and stores a reference. If a module is already loaded with the same name,
--the function will instead return the already loaded module to prevent reloading of them.
-- it returns the already loaded module to prevent reloading unless 'reload' is true.
--Unless you pass true for reload the module will not be reloaded but instead return the current instance.
-- 'subfolder' is a known folder location of the module, for example, lua/codebluemodules
--Subfolder is a folder you know its located in, even if its in anouther folder. For example store you modules in lua/codebluemodules then supply lua/codebluemodules as the subfolder
function CBLib.LoadModule(modulePath, reload)
function CBLib.LoadModule(modulePath, reload)
reload = reload or false
reload = reload or false
--Check if a module was found
-- Check if a module was found
if modulePath == nil then
if modulePath == nil then
CBLib.Debug.Error("Failed to load module '"..modulePath.."'. Module not found...")
CBLib.Debug.Error("Failed to load module '"..modulePath.."'. Module not found...")
return
return
end
end
--We must re-load the module
-- Re-load the module if needed
if reload then
if reload then
CBLib.Modules[modulePath] = nil --Destroy the old reference
CBLib.Modules[modulePath] = nil -- Destroy the old reference
end
end
--This will either return the already created module, or it will create a new one
-- This will either return the already created module or create a new one
if CBLib.Modules[modulePath] == nil then
if CBLib.Modules[modulePath] == nil then
-- Load the module code.
--Load the module code.
local moduleContents = file.Read(modulePath, "lsv")
local moduleContents = file.Read(modulePath, "lsv")
local module = nil
local module = nil
include(modulePath)
include(modulePath)
if CBLIB_MODULE ~= nil then
if CBLIB_MODULE ~= nil then
module = CBLIB_MODULE
module = CBLIB_MODULE
CBLIB_MODULE = nil
CBLIB_MODULE = nil
else
else
CBLib.Debug.Error("Tried to locate module @"..modulePath.." but the module returned nothing, it either does not exist or has produced an error!")
CBLib.Debug.Error("Tried to locate module @"..modulePath.." but the module returned nothing; it either does not exist or has produced an error!")
return
return
end
end
CBLib.Debug.Info("Loaded Module : "..modulePath)
CBLib.Debug.Info("Loaded Module: "..modulePath)
--Did compile string return an error?
-- Check if module loading resulted in an error
if isstring(module) then
if isstring(module) then
CBLib.Debug.Error("Failed to load module. Error : "..module)
CBLib.Debug.Error("Failed to load module. Error: "..module)
else
else
--Execute the module
-- Execute the module
CBLib.Modules[modulePath] = module
CBLib.Modules[modulePath] = module
if CBLib.Modules[modulePath].OnLoaded then
if CBLib.Modules[modulePath].OnLoaded then
CBLib.Modules[modulePath].OnLoaded()
CBLib.Modules[modulePath].OnLoaded()
end
end
if reload and CBLib.Modules[modulePath].OnReloaded then
if reload and CBLib.Modules[modulePath].OnReloaded then
CBLib.Modules[modulePath].OnReloaded()
CBLib.Modules[modulePath].OnReloaded()
end
end
end
end
end
end
--Return the reference.
-- Return the reference.
return CBLib.Modules[modulePath]
return CBLib.Modules[modulePath]
end
end
-- Although it does not destroy 'copies' of the module, it removes the stored reference here.
--Altough it does not destroy 'copies' of the module it does remove the reference stored here.
function CBLib.UnloadModule(modulePath)
function CBLib.UnloadModule(modulePath)
if CBLib.Modules[modulePath].OnUnloaded then
if CBLib.Modules[modulePath].OnUnloaded then
CBLib.Modules[modulePath].OnUnloaded()
CBLib.Modules[modulePath].OnUnloaded()
end
end
CBLib.Modules[modulePath] = nil
CBLib.Modules[modulePath] = nil
end
end
--Scans for all modules and sends any client/shared ones to the server using AddCSLuaFile()
-- Scans for all modules and sends any client/shared ones to the server using AddCSLuaFile()
function CBLib.NetworkModules()
function CBLib.NetworkModules()
local base = ""
local base = ""
local function ScanForClientSideModules(first, currentDirectory, currentFiles, path)
local function ScanForClientSideModules(first, currentDirectory, currentFiles, path)
if first then
if first then
currentFiles, currentDirectory = file.Find("*", "lsv")
currentFiles, currentDirectory = file.Find("*", "lsv")
path = base
path = base
first = false
first = false
else
else
currentFiles, currentDirectory = file.Find(path.."/*", "lsv")
currentFiles, currentDirectory = file.Find(path.."/*", "lsv")
end
end
for k ,v in pairs(currentFiles) do
for k, v in pairs(currentFiles) do
--Client
-- Client
if string.find( v, "bmcl_" ) then
if string.find(v, "bmcl_") then
local modulePath = path.."/"..v --Found it!
local modulePath = path.."/"..v -- Found it!
AddCSLuaFile(modulePath)
AddCSLuaFile(modulePath)
CBLib.Debug.Info("Added client side file '"..modulePath.."'")
CBLib.Debug.Info("Added client-side file '"..modulePath.."'")
end
end
--Shared
-- Shared
if string.find( v, "bmsh_" ) then
if string.find(v, "bmsh_") then
local modulePath = path.."/"..v --Found it!
local modulePath = path.."/"..v -- Found it!
AddCSLuaFile(modulePath)
AddCSLuaFile(modulePath)
CBLib.Debug.Info("Added client side file '"..modulePath.."'")
CBLib.Debug.Info("Added shared file '"..modulePath.."'")
end
end
end
end
for k , v in pairs(currentDirectory) do
for k, v in pairs(currentDirectory) do
local newPath = ""
local newPath = ""
if path == "" then
if path == "" then
newPath = v
newPath = v
else
else
newPath = path.."/"..v
newPath = path.."/"..v
end
end
--Scan again and append directory.
-- Scan again and append directory.
if ScanForClientSideModules(first, currentDirectory, currentFiles, newPath) then return true end --Cancle scan
if ScanForClientSideModules(first, currentDirectory, currentFiles, newPath) then return true end -- Cancel scan
end
end
end
end
ScanForClientSideModules(true)
ScanForClientSideModules(true)
end
end
-----------------------------
-----------------------------
-- DEBUG --
-- DEBUG --
-----------------------------
-----------------------------
CBLib.Debug = {} --A table with a bunch of debug functions
CBLib.Debug = {} -- A table with a bunch of debug functions
function CBLib.Debug.Error(message)
function CBLib.Debug.Error(message)
MsgC(Color(255,120,120), "[CB-LIB][ERROR] ", message, "\n")
MsgC(Color(255,120,120), "[CB-LIB][ERROR] ", message, "\n")
end
end
function CBLib.Debug.Warning(message)
function CBLib.Debug.Warning(message)
MsgC(Color(255,255,0), "[CB-LIB][WARNING] ", message, "\n")
MsgC(Color(255,255,0), "[CB-LIB][WARNING] ", message, "\n")
end
end
function CBLib.Debug.Info(message)
function CBLib.Debug.Info(message)
MsgC(Color(0,191,255), "[CB-LIB][INFO] ", message, "\n")
MsgC(Color(0,191,255), "[CB-LIB][INFO] ", message, "\n")
end
end
--Add global reference
-- Add global reference
_G.CBLib = CBLib
_G.CBLib = CBLib
if CLIENT then
if CLIENT then
--Done!
-- Done!
CBLib.Debug.Info("Finished loading CB-LIB client-side")
CBLib.Debug.Info("Finished loading CB-LIB client-side")
else
else
--Add Clientside modules
-- Add client-side modules
CBLib.NetworkModules()
CBLib.NetworkModules()
--Done!
-- Done!
CBLib.Debug.Info("Finished loading CB-LIB server-side")
CBLib.Debug.Info("Finished loading CB-LIB server-side")
end
end