InTesting

Wrap Instance Function

Nov 9th, 2020 (edited)
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. CoolCredits = '                                                                               \n'..
  2.     'Create wrapped instances easily for scripters who really love Object-Oriented Programming! '..
  3.     '                                                                                       \n\n'..
  4.     ' - SoftlockedUnderZero                                                                     '
  5.  
  6. Documentation = {
  7.     Notes = {
  8.         ' - Auto-suggest does not work here.'
  9.     };
  10.     Constructor_Function = {
  11.         ParameterInformation = {
  12.             Real_Instance = [[ M (Instance)
  13.                 Targeted Instance to be wrapped.
  14.             ]];
  15.             Properties =    [[ M (table (dictionary))
  16.                 A dictionary where the index is the property name and a value which is a function or array
  17.                 that has the function as the first value.
  18.                
  19.                 So it should/can look like this:
  20.                
  21.                 {
  22.                     Property1Name = function(a,b,c)end;
  23.                    
  24.                     Property2Name = {
  25.                         function(a,b,c)end;
  26.                         <variant default value>
  27.                         'readonly';
  28.                         -- ... other property tags/restrictions
  29.                     }
  30.                 }
  31.                
  32.                 The parmeters for the function are as follows:
  33.                     Instance: Real_Instance
  34.                     string: Property Name
  35.                     variant: New Value
  36.                
  37.                 If you're using an array instead a function for a value, the extra values are for restrictions
  38.                 you would like to put on your properties.
  39.                
  40.                 Currently there are:
  41.                     'readonly' Disables the ability to overwrite a property.
  42.             ]];
  43.             Functions =     [[ M (function)
  44.                 Should look like this:
  45.                
  46.                 {
  47.                     FunctionNameIndex = <function>;
  48.                 }
  49.                
  50.                 where the function is:
  51.                
  52.                 function(Instance: RealInstance, ... tuple)
  53.                
  54.                 end
  55.             ]];
  56.             Events =        [[ M (function)
  57.                 Still polishing this section.
  58.             ]]
  59.         }
  60.     }
  61. }
  62.  
  63. local spawn = function(a)
  64.     coroutine.wrap(a)()
  65. end
  66. local wait = require(6120706882)
  67.  
  68. local PropertyTags = {
  69.     readonly = function(_,_,_,a)
  70.         assert(not a,'This property is read-only')
  71.     end
  72.    
  73. }
  74.  
  75. local Wrap = {
  76.     new = function(Real_Instance,Properties,Functions,Events,OtherSettings)
  77.         local Indexed = {}
  78.         local NewIndexed = {}
  79.        
  80.         local StoredPropertyValues = {}
  81.        
  82.         for a,b in next,Properties do
  83.             local Func1 = type(b)=='table'and b[1]or b
  84.            
  85.             local PropertyTags1 = {}
  86.            
  87.             if type(b)=='table'then
  88.                 StoredPropertyValues[a] = b[2]
  89.                 for _,c in next,{table.unpack(b,3)}do
  90.                     local d = PropertyTags[c]
  91.                     assert(d,'non-existant property tag')
  92.                     table.insert(PropertyTags1,d)
  93.                 end
  94.             end
  95.            
  96.             Indexed[a] = function(c)
  97.                 local d = {Real_Instance,c,StoredPropertyValues[a],false}
  98.                 if#PropertyTags1>0 then
  99.                     for _,a in next,PropertyTags1 do
  100.                         a(unpack(d))
  101.                     end
  102.                 end
  103.                 return Func1(unpack(d))
  104.             end
  105.             NewIndexed[a] = function(c,d)
  106.                 local d = {Real_Instance,c,d,true}
  107.                 if#PropertyTags1>0 then
  108.                     for _,a in next,PropertyTags1 do
  109.                         a(unpack(d))
  110.                     end
  111.                 end
  112.                 StoredPropertyValues[a] = Func1(unpack(d))
  113.             end
  114.         end
  115.        
  116.         for a,b in next,Functions do
  117.             Indexed[a] = function()
  118.                 return function(_,...)
  119.                     return b(Real_Instance,...)
  120.                 end
  121.             end
  122.         end
  123.        
  124.         for a,b in next,Events do
  125.             local Eventlist = {}
  126.             spawn(function()
  127.                 repeat
  128.                     local c = {b()}
  129.                     if c[1]then
  130.                         for _,d in next,Eventlist do
  131.                             if not d then continue;end;
  132.                             d(table.unpack(c,2))
  133.                         end
  134.                     end
  135.                     wait()
  136.                 until nil
  137.             end)
  138.             local RBXEventSet = {
  139.                 Connect = function(_,c)
  140.                     table.insert(Eventlist,c)
  141.                     return{
  142.                         Disconnect = function()
  143.                             local a = table.find(Eventlist,c)
  144.                             if not a then return;end;
  145.                             table.remove(Eventlist,a)
  146.                         end
  147.                     }
  148.                 end;
  149.                 Wait = function()
  150.                     local c
  151.                     repeat
  152.                         c = {b()}
  153.                         wait()
  154.                     until c[1]
  155.                     return table.unpack(c,2)
  156.                 end
  157.             }
  158.             Indexed[a] = function()
  159.                 return RBXEventSet
  160.             end
  161.         end
  162.        
  163.         local MT = setmetatable({},{
  164.             __index = function(_,ind)
  165.                 local a = Indexed[ind]
  166.                 assert(a,'Bad index'..ind)
  167.                 return a(ind)
  168.             end;
  169.             __newindex = function(_,ind,nv)
  170.                 local a = NewIndexed[ind]
  171.                 assert(a,'No property')
  172.                 a(ind,nv)
  173.             end
  174.         })
  175.        
  176.         return MT
  177.     end
  178. }
  179.  
  180. return Wrap
Add Comment
Please, Sign In to add comment