Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Kitten game, try to list functions recursivly from "game"
- */
- /*
- https://stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable
- */
- var toType = function(obj) {
- return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
- }
- /*
- https://stackoverflow.com/questions/30158515/list-all-prototype-properties-of-a-javascript-object
- */
- function props(obj) {
- var p = [];
- for (; obj != null; obj = Object.getPrototypeOf(obj)) {
- var op = Object.getOwnPropertyNames(obj);
- for (var i=0; i<op.length; i++)
- if (p.indexOf(op[i]) == -1)
- p.push(op[i]);
- }
- return p;
- }
- //console.log(props(game));
- ////////////////////////////////////////////////////////
- if(!game.mods)
- {
- game.mods = {};
- }
- if(!game.mods.listFunction)
- {
- game.mods.listFunction = {};
- }
- game.mods.listFunction.iDeepLevelMax = 12;
- game.mods.listFunction.sBlackList = `
- game.mods
- game
- toString
- toLocaleString
- valueOf
- hasOwnProperty
- isPrototypeOf
- propertyIsEnumerable
- __defineGetter__
- __defineSetter__
- __lookupGetter__
- __lookupSetter__
- __proto__
- constructor
- `;
- game.mods.listFunction.list = [];
- game.mods.listFunction.genListRec = function (sKey, iDeepLevel = 0)
- {
- if (iDeepLevel >= game.mods.listFunction.iDeepLevelMax)
- {
- console.log("iDeepLevel:"+iDeepLevel+" sKey:"+sKey+" Too deep");
- return false;
- }
- sKey = sKey.replaceAll("[", "\[");
- sKey = sKey.replaceAll("]", "\]");
- sKey = sKey.replaceAll(".", "\.");
- sReg = "^"+sKey+"$";
- if(sKey == "game" && iDeepLevel == 0)
- {
- console.log("First run, check game content");
- }
- else if (game.mods.listFunction.sBlackList.match(RegExp(sReg, "m") ) )
- {
- console.log("iDeepLevel:"+iDeepLevel+" sKey:"+sKey+" BlackListed");
- return false;
- }
- if(sKey.includes("-"))
- {
- return false;
- }
- var sType = eval('toType('+sKey+')');
- // console.log(sType+" "+sKey);
- if(sType == "array")
- {
- aList = eval(sKey);
- for(var i=0; i<aList.length; i++)
- {
- // detect type, recursiv
- if(sType == "object")
- {
- this.genListRec(sKey+"["+i+"]", iDeepLevel+1);
- }
- if(sType == "function")
- {
- game.mods.listFunction.list.push(sKey+"["+i+"]" );
- //game.mods.listFunction.list.push(sKey+"["+i+"]"+eval(sKey+"["+i+"].toString().match(/\(.*\)/)[0];") );
- }
- if(sType == "array")
- {
- this.genListRec(sKey+"["+i+"]", iDeepLevel+1);
- }
- }
- }
- else if (sType == "object")
- {
- //var aKeys = eval("Object.keys("+sKey+")");
- //console.log(sKey);
- var aKeys = eval("props("+sKey+")");
- //console.log(aKeys);
- var aKeysFiltered = aKeys.filter(n => !game.mods.listFunction.sBlackList.split("\n").includes(n));
- //console.log(aKeysFiltered);
- for(var i=0; i<aKeysFiltered.length; i++)
- {
- if(aKeysFiltered[i].includes("-"))
- {
- continue;
- }
- //console.log('toType('+sKey+'.'+aKeysFiltered[i]+')');
- var sType = eval('toType('+sKey+'.'+aKeysFiltered[i]+')');
- // console.log(sType+" "+sKey+'.'+aKeysFiltered[i]);
- if(sType == "object")
- {
- this.genListRec(sKey+"."+aKeysFiltered[i], iDeepLevel+1);
- }
- if(sType == "function")
- {
- // game.mods.listFunction.list.push(sKey+"."+aKeysFiltered[i] );
- // game.mods.listFunction.list.push(sKey+"."+aKeysFiltered[i] + eval(sKey+"."+aKeysFiltered[i]+".toString().match(/\(.*\)/)[0];") );
- var sArgs = eval(sKey+"."+aKeysFiltered[i]+".toString().match(/^.*?\\((.*?)\\)/)[1];");
- // console.log("sArgs : "+sArgs);
- game.mods.listFunction.list.push(sKey+"."+aKeysFiltered[i] + "(" + sArgs + ")" );
- }
- if(sType == "array")
- {
- this.genListRec(sKey+"."+aKeysFiltered[i], iDeepLevel+1);
- }
- }
- }
- else
- {
- // console.log(sKey);
- }
- }
- game.mods.listFunction.genListRec("game");
- console.log(game.mods.listFunction.list);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement