Advertisement
sinkir

WIP list all functions in game

Jul 22nd, 2023 (edited)
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Kitten game, try to list functions recursivly from "game"
  3. */
  4.  
  5.  
  6. /*
  7. https://stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable
  8. */
  9. var toType = function(obj) {
  10.   return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
  11. }
  12.  
  13. /*
  14. https://stackoverflow.com/questions/30158515/list-all-prototype-properties-of-a-javascript-object
  15. */
  16. function props(obj) {
  17.     var p = [];
  18.     for (; obj != null; obj = Object.getPrototypeOf(obj)) {
  19.         var op = Object.getOwnPropertyNames(obj);
  20.         for (var i=0; i<op.length; i++)
  21.             if (p.indexOf(op[i]) == -1)
  22.                  p.push(op[i]);
  23.     }
  24.     return p;
  25. }
  26. //console.log(props(game));
  27.  
  28. ////////////////////////////////////////////////////////
  29.  
  30. if(!game.mods)
  31. {
  32.     game.mods = {};
  33. }
  34. if(!game.mods.listFunction)
  35. {
  36.     game.mods.listFunction = {};
  37. }
  38.  
  39.  
  40. game.mods.listFunction.iDeepLevelMax = 12;
  41. game.mods.listFunction.sBlackList = `
  42. game.mods
  43. game
  44. toString
  45. toLocaleString
  46. valueOf
  47. hasOwnProperty
  48. isPrototypeOf
  49. propertyIsEnumerable
  50. __defineGetter__
  51. __defineSetter__
  52. __lookupGetter__
  53. __lookupSetter__
  54. __proto__
  55. constructor
  56. `;
  57.  
  58. game.mods.listFunction.list = [];
  59.  
  60.  
  61.  
  62.  
  63. game.mods.listFunction.genListRec = function (sKey, iDeepLevel = 0)
  64. {
  65.     if (iDeepLevel >= game.mods.listFunction.iDeepLevelMax)
  66.     {
  67.         console.log("iDeepLevel:"+iDeepLevel+" sKey:"+sKey+" Too deep");
  68.         return false;
  69.     }
  70.     sKey = sKey.replaceAll("[", "\[");
  71.     sKey = sKey.replaceAll("]", "\]");
  72.     sKey = sKey.replaceAll(".", "\.");
  73.     sReg = "^"+sKey+"$";
  74.    
  75.     if(sKey == "game" && iDeepLevel == 0)
  76.     {
  77.         console.log("First run, check game content");
  78.     }  
  79.     else if (game.mods.listFunction.sBlackList.match(RegExp(sReg, "m") ) )
  80.     {
  81.         console.log("iDeepLevel:"+iDeepLevel+" sKey:"+sKey+" BlackListed");
  82.         return false;
  83.     }
  84.     if(sKey.includes("-"))
  85.     {
  86.         return false;
  87.     }
  88.    
  89.     var sType = eval('toType('+sKey+')');
  90. //  console.log(sType+"  "+sKey);
  91.     if(sType == "array")
  92.     {
  93.         aList = eval(sKey);
  94.         for(var i=0; i<aList.length; i++)
  95.         {
  96.             // detect type, recursiv
  97.             if(sType == "object")
  98.             {
  99.                 this.genListRec(sKey+"["+i+"]", iDeepLevel+1);
  100.             }
  101.             if(sType == "function")
  102.             {
  103.                 game.mods.listFunction.list.push(sKey+"["+i+"]" );
  104.                 //game.mods.listFunction.list.push(sKey+"["+i+"]"+eval(sKey+"["+i+"].toString().match(/\(.*\)/)[0];") );
  105.             }
  106.             if(sType == "array")
  107.             {
  108.                 this.genListRec(sKey+"["+i+"]", iDeepLevel+1);
  109.             }          
  110.         }
  111.     }
  112.     else if (sType == "object")
  113.     {
  114.  
  115.         //var aKeys =  eval("Object.keys("+sKey+")");
  116.         //console.log(sKey);
  117.         var aKeys =  eval("props("+sKey+")");
  118.         //console.log(aKeys);
  119.         var aKeysFiltered = aKeys.filter(n => !game.mods.listFunction.sBlackList.split("\n").includes(n));
  120.         //console.log(aKeysFiltered);
  121.  
  122.         for(var i=0; i<aKeysFiltered.length; i++)
  123.         {
  124.            
  125.             if(aKeysFiltered[i].includes("-"))
  126.             {
  127.                 continue;
  128.             }
  129.            
  130.             //console.log('toType('+sKey+'.'+aKeysFiltered[i]+')');
  131.             var sType = eval('toType('+sKey+'.'+aKeysFiltered[i]+')');
  132. //          console.log(sType+"  "+sKey+'.'+aKeysFiltered[i]);
  133.  
  134.             if(sType == "object")
  135.             {
  136.                 this.genListRec(sKey+"."+aKeysFiltered[i], iDeepLevel+1);
  137.             }
  138.             if(sType == "function")
  139.             {
  140.         //      game.mods.listFunction.list.push(sKey+"."+aKeysFiltered[i] );
  141.         //      game.mods.listFunction.list.push(sKey+"."+aKeysFiltered[i] + eval(sKey+"."+aKeysFiltered[i]+".toString().match(/\(.*\)/)[0];") );
  142.                 var sArgs = eval(sKey+"."+aKeysFiltered[i]+".toString().match(/^.*?\\((.*?)\\)/)[1];");
  143. //              console.log("sArgs : "+sArgs);
  144.                 game.mods.listFunction.list.push(sKey+"."+aKeysFiltered[i] + "(" + sArgs + ")" );
  145.             }
  146.             if(sType == "array")
  147.             {
  148.                 this.genListRec(sKey+"."+aKeysFiltered[i], iDeepLevel+1);
  149.             }
  150.            
  151.         }
  152.     }
  153.     else
  154.     {
  155.     //  console.log(sKey);
  156.     }
  157.  
  158. }
  159.  
  160. game.mods.listFunction.genListRec("game");
  161.  
  162. console.log(game.mods.listFunction.list);
Tags: Kittensgame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement