hoobonceagain

cl_lib

Aug 28th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1. if _G.CBLib ~= nil then return _G.CBLib end --Prevent Lua refresh.
  2.  
  3. local CBLib = {}
  4.  
  5. --[[-------------------------------------------------------------------------
  6. Helper functions
  7. ---------------------------------------------------------------------------]]
  8. CBLib.Helper = {}
  9.  
  10. --Formats a number as a string with commas inserted
  11. function CBLib.Helper.CommaFormatNumber(amount)
  12. local formatted = amount
  13. while true do
  14. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  15. if (k==0) then
  16. break
  17. end
  18. end
  19. return formatted
  20. end
  21.  
  22. --Credit to facepunch (Didnt see guys name)
  23. --Retursn the text in a table for, an index for each new line
  24. function CBLib.Helper.WrapText(Str,font,width)
  25. if( font ) then --Dr Magnusson's much less prone to failure and more optimized version
  26. surface.SetFont( font )
  27. end
  28.  
  29. local tbl, len, Start, End = {}, string.len( Str ), 1, 1
  30.  
  31. while ( End < len ) do
  32. End = End + 1
  33. if ( surface.GetTextSize( string.sub( Str, Start, End ) ) > width ) then
  34. local n = string.sub( Str, End, End )
  35. local I = 0
  36. for i = 1, 15 do
  37. I = i
  38. if( n != " " and n != "," and n != "." and n != "\n" ) then
  39. End = End - 1
  40. n = string.sub( Str, End, End )
  41. else
  42. break
  43. end
  44. end
  45. if( I == 15 ) then
  46. End = End + 14
  47. end
  48.  
  49. local FnlStr = string.Trim( string.sub( Str, Start, End ) )
  50. table.insert( tbl, FnlStr )
  51. Start = End + 1
  52. end
  53. end
  54. table.insert( tbl, string.sub( Str, Start, End ) )
  55. return tbl
  56. end
  57.  
  58.  
  59.  
  60. -----------------------------
  61. -- MODULES --
  62. -----------------------------
  63.  
  64. --Modules are basicly instances of a table. This handles loading and destroying modules.
  65. --Or anouther way to look at it is like a static class in an oop language.
  66.  
  67. --A module can also contain three event functions, OnLoaded, OnUnloaded, and OnReloaded
  68.  
  69. --A Table for all modules loaded.
  70. CBLib.Modules = {}
  71.  
  72. --loads a module script and stores a reference, if a module is loaded of the same name then
  73. --the function will instead return the already loaded module to prevent reloading of them.
  74. --Unless you pass true for reload the module will not be reloaded but instead return the current instance.
  75. --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
  76. function CBLib.LoadModule(modulePath, reload)
  77. reload = reload or false
  78.  
  79. --Check if a module was found
  80. if modulePath == nil then
  81. CBLib.Debug.Error("Failed to load module '"..modulePath.."'. Module not found...")
  82. return
  83. end
  84.  
  85. --We must re-load the module
  86. if reload then
  87. CBLib.Modules[modulePath] = nil --Destroy the old reference
  88. end
  89.  
  90. --This will either return the already created module, or it will create a new one
  91. if CBLib.Modules[modulePath] == nil then
  92.  
  93. --Load the module code.
  94. local moduleContents = file.Read(modulePath, "lsv")
  95.  
  96. local module = nil
  97. include(modulePath)
  98.  
  99. if CBLIB_MODULE ~= nil then
  100. module = CBLIB_MODULE
  101. CBLIB_MODULE = nil
  102. else
  103. CBLib.Debug.Error("Tried to locate module @"..modulePath.." but the module returned nothing, it either does not exist or has produced an error!")
  104. return
  105. end
  106.  
  107. CBLib.Debug.Info("Loaded Module : "..modulePath)
  108.  
  109. --Did compile string return an error?
  110. if isstring(module) then
  111. CBLib.Debug.Error("Failed to load module. Error : "..module)
  112. else
  113. --Execute the module
  114. CBLib.Modules[modulePath] = module
  115.  
  116. if CBLib.Modules[modulePath].OnLoaded then
  117. CBLib.Modules[modulePath].OnLoaded()
  118. end
  119.  
  120. if reload and CBLib.Modules[modulePath].OnReloaded then
  121. CBLib.Modules[modulePath].OnReloaded()
  122. end
  123. end
  124. end
  125.  
  126. --Return the reference.
  127. return CBLib.Modules[modulePath]
  128. end
  129.  
  130.  
  131. --Altough it does not destroy 'copies' of the module it does remove the reference stored here.
  132. function CBLib.UnloadModule(modulePath)
  133. if CBLib.Modules[modulePath].OnUnloaded then
  134. CBLib.Modules[modulePath].OnUnloaded()
  135. end
  136.  
  137. CBLib.Modules[modulePath] = nil
  138. end
  139.  
  140. --Scans for all modules and sends any client/shared ones to the server using AddCSLuaFile()
  141. function CBLib.NetworkModules()
  142. local base = ""
  143.  
  144. local function ScanForClientSideModules(first, currentDirectory, currentFiles, path)
  145. if first then
  146. currentFiles, currentDirectory = file.Find("*", "lsv")
  147. path = base
  148. first = false
  149. else
  150. currentFiles, currentDirectory = file.Find(path.."/*", "lsv")
  151. end
  152.  
  153. for k ,v in pairs(currentFiles) do
  154. --Client
  155. if string.find( v, "bmcl_" ) then
  156. local modulePath = path.."/"..v --Found it!
  157. AddCSLuaFile(modulePath)
  158. CBLib.Debug.Info("Added client side file '"..modulePath.."'")
  159. end
  160.  
  161. --Shared
  162. if string.find( v, "bmsh_" ) then
  163. local modulePath = path.."/"..v --Found it!
  164. AddCSLuaFile(modulePath)
  165. CBLib.Debug.Info("Added client side file '"..modulePath.."'")
  166. end
  167. end
  168.  
  169. for k , v in pairs(currentDirectory) do
  170. local newPath = ""
  171. if path == "" then
  172. newPath = v
  173. else
  174. newPath = path.."/"..v
  175. end
  176.  
  177. --Scan again and append directory.
  178. if ScanForClientSideModules(first, currentDirectory, currentFiles, newPath) then return true end --Cancle scan
  179. end
  180. end
  181.  
  182. ScanForClientSideModules(true)
  183. end
  184.  
  185. -----------------------------
  186. -- DEBUG --
  187. -----------------------------
  188.  
  189. CBLib.Debug = {} --A table with a bunch of debug functions
  190.  
  191. function CBLib.Debug.Error(message)
  192. MsgC(Color(255,120,120), "[CB-LIB][ERROR] ", message, "\n")
  193. end
  194.  
  195. function CBLib.Debug.Warning(message)
  196. MsgC(Color(255,255,0), "[CB-LIB][WARNING] ", message, "\n")
  197. end
  198.  
  199. function CBLib.Debug.Info(message)
  200. MsgC(Color(0,191,255), "[CB-LIB][INFO] ", message, "\n")
  201. end
  202.  
  203. --Add global reference
  204. _G.CBLib = CBLib
  205.  
  206. if CLIENT then
  207. --Done!
  208. CBLib.Debug.Info("Finished loading CB-LIB client-side")
  209. else
  210. --Add Clientside modules
  211. CBLib.NetworkModules()
  212.  
  213. --Done!
  214. CBLib.Debug.Info("Finished loading CB-LIB server-side")
  215. end
  216. if _G.CBLib ~= nil then return _G.CBLib end --Prevent Lua refresh.
  217.  
  218. local CBLib = {}
  219.  
  220. --[[-------------------------------------------------------------------------
  221. Helper functions
  222. ---------------------------------------------------------------------------]]
  223. CBLib.Helper = {}
  224.  
  225. --Formats a number as a string with commas inserted
  226. function CBLib.Helper.CommaFormatNumber(amount)
  227. local formatted = amount
  228. while true do
  229. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  230. if (k==0) then
  231. break
  232. end
  233. end
  234. return formatted
  235. end
  236.  
  237. --Credit to facepunch (Didnt see guys name)
  238. --Retursn the text in a table for, an index for each new line
  239. function CBLib.Helper.WrapText(Str,font,width)
  240. if( font ) then --Dr Magnusson's much less prone to failure and more optimized version
  241. surface.SetFont( font )
  242. end
  243.  
  244. local tbl, len, Start, End = {}, string.len( Str ), 1, 1
  245.  
  246. while ( End < len ) do
  247. End = End + 1
  248. if ( surface.GetTextSize( string.sub( Str, Start, End ) ) > width ) then
  249. local n = string.sub( Str, End, End )
  250. local I = 0
  251. for i = 1, 15 do
  252. I = i
  253. if( n != " " and n != "," and n != "." and n != "\n" ) then
  254. End = End - 1
  255. n = string.sub( Str, End, End )
  256. else
  257. break
  258. end
  259. end
  260. if( I == 15 ) then
  261. End = End + 14
  262. end
  263.  
  264. local FnlStr = string.Trim( string.sub( Str, Start, End ) )
  265. table.insert( tbl, FnlStr )
  266. Start = End + 1
  267. end
  268. end
  269. table.insert( tbl, string.sub( Str, Start, End ) )
  270. return tbl
  271. end
  272.  
  273.  
  274.  
  275. -----------------------------
  276. -- MODULES --
  277. -----------------------------
  278.  
  279. --Modules are basicly instances of a table. This handles loading and destroying modules.
  280. --Or anouther way to look at it is like a static class in an oop language.
  281.  
  282. --A module can also contain three event functions, OnLoaded, OnUnloaded, and OnReloaded
  283.  
  284. --A Table for all modules loaded.
  285. CBLib.Modules = {}
  286.  
  287. --loads a module script and stores a reference, if a module is loaded of the same name then
  288. --the function will instead return the already loaded module to prevent reloading of them.
  289. --Unless you pass true for reload the module will not be reloaded but instead return the current instance.
  290. --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
  291. function CBLib.LoadModule(modulePath, reload)
  292. reload = reload or false
  293.  
  294. --Check if a module was found
  295. if modulePath == nil then
  296. CBLib.Debug.Error("Failed to load module '"..modulePath.."'. Module not found...")
  297. return
  298. end
  299.  
  300. --We must re-load the module
  301. if reload then
  302. CBLib.Modules[modulePath] = nil --Destroy the old reference
  303. end
  304.  
  305. --This will either return the already created module, or it will create a new one
  306. if CBLib.Modules[modulePath] == nil then
  307.  
  308. --Load the module code.
  309. local moduleContents = file.Read(modulePath, "lsv")
  310.  
  311. local module = nil
  312. include(modulePath)
  313.  
  314. if CBLIB_MODULE ~= nil then
  315. module = CBLIB_MODULE
  316. CBLIB_MODULE = nil
  317. else
  318. CBLib.Debug.Error("Tried to locate module @"..modulePath.." but the module returned nothing, it either does not exist or has produced an error!")
  319. return
  320. end
  321.  
  322. CBLib.Debug.Info("Loaded Module : "..modulePath)
  323.  
  324. --Did compile string return an error?
  325. if isstring(module) then
  326. CBLib.Debug.Error("Failed to load module. Error : "..module)
  327. else
  328. --Execute the module
  329. CBLib.Modules[modulePath] = module
  330.  
  331. if CBLib.Modules[modulePath].OnLoaded then
  332. CBLib.Modules[modulePath].OnLoaded()
  333. end
  334.  
  335. if reload and CBLib.Modules[modulePath].OnReloaded then
  336. CBLib.Modules[modulePath].OnReloaded()
  337. end
  338. end
  339. end
  340.  
  341. --Return the reference.
  342. return CBLib.Modules[modulePath]
  343. end
  344.  
  345.  
  346. --Altough it does not destroy 'copies' of the module it does remove the reference stored here.
  347. function CBLib.UnloadModule(modulePath)
  348. if CBLib.Modules[modulePath].OnUnloaded then
  349. CBLib.Modules[modulePath].OnUnloaded()
  350. end
  351.  
  352. CBLib.Modules[modulePath] = nil
  353. end
  354.  
  355. --Scans for all modules and sends any client/shared ones to the server using AddCSLuaFile()
  356. function CBLib.NetworkModules()
  357. local base = ""
  358.  
  359. local function ScanForClientSideModules(first, currentDirectory, currentFiles, path)
  360. if first then
  361. currentFiles, currentDirectory = file.Find("*", "lsv")
  362. path = base
  363. first = false
  364. else
  365. currentFiles, currentDirectory = file.Find(path.."/*", "lsv")
  366. end
  367.  
  368. for k ,v in pairs(currentFiles) do
  369. --Client
  370. if string.find( v, "bmcl_" ) then
  371. local modulePath = path.."/"..v --Found it!
  372. AddCSLuaFile(modulePath)
  373. CBLib.Debug.Info("Added client side file '"..modulePath.."'")
  374. end
  375.  
  376. --Shared
  377. if string.find( v, "bmsh_" ) then
  378. local modulePath = path.."/"..v --Found it!
  379. AddCSLuaFile(modulePath)
  380. CBLib.Debug.Info("Added client side file '"..modulePath.."'")
  381. end
  382. end
  383.  
  384. for k , v in pairs(currentDirectory) do
  385. local newPath = ""
  386. if path == "" then
  387. newPath = v
  388. else
  389. newPath = path.."/"..v
  390. end
  391.  
  392. --Scan again and append directory.
  393. if ScanForClientSideModules(first, currentDirectory, currentFiles, newPath) then return true end --Cancle scan
  394. end
  395. end
  396.  
  397. ScanForClientSideModules(true)
  398. end
  399.  
  400. -----------------------------
  401. -- DEBUG --
  402. -----------------------------
  403.  
  404. CBLib.Debug = {} --A table with a bunch of debug functions
  405.  
  406. function CBLib.Debug.Error(message)
  407. MsgC(Color(255,120,120), "[CB-LIB][ERROR] ", message, "\n")
  408. end
  409.  
  410. function CBLib.Debug.Warning(message)
  411. MsgC(Color(255,255,0), "[CB-LIB][WARNING] ", message, "\n")
  412. end
  413.  
  414. function CBLib.Debug.Info(message)
  415. MsgC(Color(0,191,255), "[CB-LIB][INFO] ", message, "\n")
  416. end
  417.  
  418. --Add global reference
  419. _G.CBLib = CBLib
  420.  
  421. if CLIENT then
  422. --Done!
  423. CBLib.Debug.Info("Finished loading CB-LIB client-side")
  424. else
  425. --Add Clientside modules
  426. CBLib.NetworkModules()
  427.  
  428. --Done!
  429. CBLib.Debug.Info("Finished loading CB-LIB server-side")
  430. end
Advertisement
Add Comment
Please, Sign In to add comment