Advertisement
szymski

Untitled

Oct 18th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. /* --- --------------------------------------------------------------------------------
  2. @: Default Exceptions
  3. --- */
  4.  
  5. EXPADV.AddException( nil, "invoke" )
  6. EXPADV.AddException( nil, "cast" )
  7.  
  8. /* --- --------------------------------------------------------------------------------
  9. @: Default Classes
  10. --- */
  11.  
  12. local Class_Boolean = EXPADV.AddClass( nil, "boolean", "b" )
  13. local Class_Function = EXPADV.AddClass( nil, "function", "f" )
  14. local Class_Delgate = EXPADV.AddClass( nil, "delegate", "d" )
  15. local Class_Exception = EXPADV.AddClass( nil, "exception", "ex" )
  16.  
  17. Class_Boolean:AddAlias( "bool" )
  18. Class_Boolean:DefaultAsLua( false )
  19. Class_Function:DefaultAsLua( "function( ) end" )
  20.  
  21. if WireLib then
  22. Class_Boolean:WireInput( "NUMBER", function( Context, MemoryRef )
  23. return Context.Memory[ MemoryRef ] and 1 or 0
  24. end )
  25.  
  26. Class_Boolean:WireOutput( "NUMBER", function( Context, MemoryRef, InValue )
  27. Context.Memory[ MemoryRef ] = (InValue ~= 0)
  28. end )
  29. end
  30.  
  31. /* --- --------------------------------------------------------------------------------
  32. @: Default Operators
  33. --- */
  34.  
  35. EXPADV.SharedOperators( )
  36.  
  37. Class_Boolean:AddVMOperator( "=", "n,b", "", function( Context, Trace, MemRef, Value )
  38. local Prev = Context.Memory[MemRef]
  39. Context.Memory[MemRef] = Value
  40. Context.Trigger[MemRef] = Context.Trigger[MemRef] or ( Prev ~= Value )
  41. end )
  42.  
  43. Class_Function:AddPreparedOperator( "=", "n,f", "", "Context.Memory[@value 1] = @value 2" )
  44. Class_Delgate:AddPreparedOperator( "=", "n,d", "", "Context.Memory[@value 1] = @value 2" )
  45. Class_Exception:AddPreparedOperator( "=", "n,ex", "", "Context.Memory[@value 1] = @value 2" )
  46.  
  47. EXPADV.AddInlineOperator( nil, "==", "b,b", "b", "(@value 1 == @value 2)" )
  48. EXPADV.AddInlineOperator( nil, "!=", "b,b", "b", "(@value 1 != @value 2)" )
  49.  
  50. EXPADV.AddInlineOperator( nil, "is", "b", "b", "@value 1" )
  51. EXPADV.AddInlineOperator( nil, "not", "b", "b", "!@value 1" )
  52.  
  53. EXPADV.AddInlineOperator( nil, "||", "b,b", "b", "(@value 1 or @value 2)" )
  54.  
  55. EXPADV.AddInlineOperator( nil, "&&", "b,b", "b", "(@value 1 and @value 2)" )
  56.  
  57. EXPADV.AddInlineOperator( nil, "delegate", "f", "d", "@value 1" )
  58.  
  59. EXPADV.AddInlineOperator( nil, "function", "d", "f", "@value 1" )
  60.  
  61. EXPADV.AddPreparedOperator( nil, "call", "f,s,...", "_vr", [[
  62. @define Return, Type = @value 1( Context, @...)
  63. if @value 2 and @Type ~= @value 2 and !(@value 2 == "void" and !@Return) then
  64. Context:Throw( @trace, "invoke", string.format( "Invalid return value, %s expected got %s", @value 2, @Type ) )
  65. end
  66. ]], "@Return" )
  67.  
  68. /* --- -------------------------------------------------------------------------------
  69. @: Loops
  70. --- */
  71.  
  72. EXPADV.AddPreparedOperator( nil, "while", "b,?", "", [[
  73. while( @value 1 ) do
  74. @prepare 2
  75. end
  76. ]] )
  77.  
  78. /* --- --------------------------------------------------------------------------------
  79. @: Performance
  80. --- */
  81.  
  82. local Component = EXPADV.AddComponent( "performance" , true )
  83.  
  84. Component.Author = "Rusketh"
  85. Component.Description = "Allows for monitoring performance and usage."
  86.  
  87. EXPADV.SharedOperators( )
  88.  
  89. Component:AddInlineFunction( "ops", "", "n", "math.Round(Context.Status.Perf)" )
  90.  
  91. Component:AddInlineFunction( "opCounter", "", "n", "math.ceil(Context.Status.Perf + Context.Status.Counter)" )
  92.  
  93. Component:AddInlineFunction( "cpuUsage", "", "n", "($SysTime( ) - Context.Status.BenchMark)" )
  94.  
  95. Component:AddInlineFunction( "cpuStopWatch", "", "n", "Context.Status.StopWatch" )
  96.  
  97. --------------------------------------------------------------
  98.  
  99. Component:AddVMFunction( "perf", "", "b",
  100. function( Context, Trace )
  101. if Context.Status.Perf + Context.Status.Counter >= expadv_hardquota - expadv_tickquota then
  102. return false
  103. elseif Context.Status.Perf >= expadv_softquota * 2 then
  104. return false
  105. end
  106.  
  107. return true
  108. end )
  109.  
  110. Component:AddVMFunction( "perf", "n", "b",
  111. function( Context, Trace, Value )
  112. Value = math.Clamp( Value, 0, 100 )
  113.  
  114. if Context.Status.Perf + Context.Status.Counter >= (expadv_hardquota - expadv_tickquota) * Value * 0.01 then
  115. return false
  116. elseif Value == 100 then
  117. if Context.Status.Perf >= cv_expadv_softquota * 2 then
  118. return false
  119. end
  120. elseif Context.Status.Perf >= cv_expadv_softquota * Value * 0.01 then
  121. return false
  122. end
  123.  
  124. return true
  125. end )
  126.  
  127. --------------------------------------------------------------
  128.  
  129. Component:AddVMFunction( "minquota", "", "n",
  130. function( Context, Trace )
  131. if self.prf < e2_softquota then
  132. return math.floor(expadv_softquota - Context.Status.Perf)
  133. else
  134. return 0
  135. end
  136. end )
  137.  
  138. Component:AddVMFunction( "maxquota", "", "n",
  139. function( Context, Trace )
  140. local Perf = Context.Status.Perf
  141.  
  142. if Perf >= cv_expadv_tickquota then return 0 end
  143.  
  144. local tickquota = expadv_tickquota - Perf
  145. local hardquota = expadv_hardquota - Context.Status.Counter - Perf + expadv_softquota
  146.  
  147. if hardquota < tickquota then return math.floor(hardquota) end
  148.  
  149. return math.floor(tickquota)
  150. end )
  151.  
  152. Component:AddVMFunction( "softQuota", "", "n",
  153. function( Context, Trace )
  154. return expadv_softquota
  155. end )
  156.  
  157. Component:AddVMFunction( "hardQuota", "", "n",
  158. function( Context, Trace )
  159. return expadv_hardquota
  160. end )
  161.  
  162. /* --- --------------------------------------------------------------------------------
  163. @: Printing
  164. --- */
  165.  
  166. local Component = EXPADV.AddComponent( "print" , true )
  167.  
  168. Component.Author = "Rusketh"
  169. Component.Description = "Prints stuff to your chat."
  170.  
  171. EXPADV.SharedOperators( )
  172.  
  173. Component:AddVMFunction( "printColor", "...", "",
  174. function( Context, Trace, ... )
  175. if CLIENT and Context.player ~= LocalPlayer( ) then return end
  176.  
  177. local Values = { ... }
  178.  
  179. for Key, Value in pairs( Values ) do
  180. if Value[2] == "c" then
  181. Values[Key] = Value[1]
  182. else
  183. Values[Key] = EXPADV.ToString( Value[2], Value[1] )
  184. end
  185. end
  186.  
  187. if SERVER then
  188. EXPADV.PrintColor( Context.player, Values )
  189. elseif CLIENT then
  190. chat.AddText( unpack( Values ) )
  191. end
  192. end )
  193.  
  194. Component:AddVMFunction( "print", "...", "",
  195. function( Context, Trace, ... )
  196.  
  197. if CLIENT and Context.player ~= LocalPlayer( ) then return end
  198.  
  199. local Values = { ... }
  200.  
  201. for Key, Value in pairs( Values ) do
  202. Values[Key] = EXPADV.ToString( Value[2], Value[1] )
  203. end
  204.  
  205. if SERVER then
  206. EXPADV.PrintColor( Context.player, Values )
  207. elseif CLIENT then
  208. chat.AddText( unpack( Values ) )
  209. end
  210. end )
  211.  
  212. Component:AddFunctionHelper( "print", "...", "Prints the contents of ( ... ) to chat seperated with a space." )
  213.  
  214. if SERVER then
  215. util.AddNetworkString( "expadv.printcolor" )
  216.  
  217. function EXPADV.PrintColor( Player, Tbl )
  218. net.Start( "expadv.printcolor" )
  219. net.WriteTable( Tbl )
  220. net.Send( Player )
  221. end
  222.  
  223. end
  224.  
  225. if CLIENT then
  226. net.Receive( "expadv.printcolor", function( )
  227. chat.AddText( unpack( net.ReadTable( ) ) )
  228. end )
  229. end
  230.  
  231. /* --- --------------------------------------------------------------------------------
  232. @: Console
  233. --- */
  234.  
  235. local Component = EXPADV.AddComponent( "console" , true )
  236.  
  237. EXPADV.SharedOperators( )
  238.  
  239. Component.Author = "Szymekk"
  240. Component.Description = "Allows you to execute console commands."
  241.  
  242. Component:AddInlineFunction( "concmd", "s", "", "Context.player:ConCommand(@value 1)" )
  243. Component:AddFunctionHelper( "concmd", "s", "Executes console command." )
  244.  
  245. /* --- -------------------------------------------------------------------------------
  246. @: Events
  247. --- */
  248.  
  249. EXPADV.SharedEvents( )
  250.  
  251. EXPADV.AddEvent( nil, "tick", "", "" )
  252. EXPADV.AddEvent( nil, "think", "", "" )
  253.  
  254. EXPADV.ServerEvents( )
  255. EXPADV.AddEvent( nil, "trigger", "s,s", "" )
  256. EXPADV.AddEvent( nil, "clientLoaded", "ply", "" )
  257. EXPADV.AddEvent( nil, "dupePasted" )
  258.  
  259. /* --- -------------------------------------------------------------------------------
  260. @: Shared Hooks
  261. --- */
  262.  
  263. hook.Add( "Tick", "Expav.Event", function( )
  264. EXPADV.CallEvent( "tick" )
  265. end )
  266.  
  267. hook.Add( "Think", "Expav.Event", function( )
  268. EXPADV.CallEvent( "think" )
  269. end )
  270.  
  271. /* --- --------------------------------------------------------------------------------
  272. @: Variants
  273. --- */
  274.  
  275. local Component = EXPADV.AddComponent( "variant" , true )
  276.  
  277. Component.Author = "Rusketh"
  278. Component.Description = "Adds an object that can pass around anything."
  279.  
  280. local Class_Variant = Component:AddClass( "variant", "vr" )
  281.  
  282. Class_Variant:DefaultAsLua( { false, "b" } )
  283.  
  284. Class_Variant:AddPreparedOperator( "=", "n,vr", "", "Context.Memory[@value 1] = @value 2" )
  285.  
  286. function Component:OnPostRegisterClass( Name, Class )
  287. if !Class.LoadOnClient then
  288. EXPADV.ServerOperators( )
  289. elseif !Class.LoadOnServer then
  290. EXPADV.ClientOperators( )
  291. else EXPADV.SharedOperators( ) end
  292.  
  293. self:AddInlineOperator( "variant", Class.Short, "vr", "{ @value 1, @type 1 }" )
  294.  
  295. self:AddInlineOperator( Name, "vr", Class.Short, string.format( "( @value 1[2] == %q and @value 1[1] or Context:Throw(@trace, %q, \"Attempt to cast value \" .. EXPADV.TypeName(@value 1[2]) .. \" to %s \") )", Class.Short, "cast", Name ) )
  296. end
  297.  
  298. Component:AddInlineFunction( "type", "vr:", "s", "EXPADV.TypeName(@value 1[2])" )
  299.  
  300. /* --- --------------------------------------------------------------------------------
  301. @: Debug
  302. --- */
  303.  
  304. local Component = EXPADV.AddComponent( "debug" , true )
  305.  
  306. Component.Author = "Rusketh"
  307. Component.Description = "Used to debug thrown exceptions in your code."
  308.  
  309. Component:AddInlineFunction( "type", "ex:", "s", "@value 1.Exception" )
  310. Component:AddFunctionHelper( "type", "_ex:", "Returns the true type of an Exception" )
  311.  
  312. Component:AddInlineFunction( "message", "ex:", "s", "@value 1.Message" )
  313. Component:AddFunctionHelper( "message", "_ex:", "Returns the current exceptions message." )
  314.  
  315. Component:AddInlineFunction( "root", "ex:", "ar", [[{@value 1.Trace[1] or 0, @value 1.Trace[2] or 0, __type = "n" } ]] )
  316.  
  317. Component:AddVMFunction( "stack", "ex:n", "ar",
  318. function( Context, Trace, Exception, Index )
  319. local Stack = Exception.Trace.Stack
  320.  
  321. if !Stack or !Stack[Index] then return {0, 0, __type = "n" } end
  322.  
  323. return {Stack[Index][1] or 0, Stack[Index][2] or 0, __type = "n" }
  324. end )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement