Advertisement
Double_X

DoubleX RMMV Object Properties v100d

Oct 27th, 2015 (edited)
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*============================================================================
  2.  *    ## Plugin Info                                                          
  3.  *----------------------------------------------------------------------------
  4.  *    # Plugin Name                                                          
  5.  *      DoubleX RMMV Object Properties                                        
  6.  *----------------------------------------------------------------------------
  7.  *    # Terms Of Use                                                          
  8.  *      You shall keep this plugin's Plugin Info part's contents intact      
  9.  *      You shalln't claim that this plugin's written by anyone other than    
  10.  *      DoubleX or his aliases                                                
  11.  *     None of the above applies to DoubleX or his aliases                    
  12.  *----------------------------------------------------------------------------
  13.  *    # Prerequisites                                                        
  14.  *      Abilities:                                                            
  15.  *      1. Basic knowledge of inspecting object properties in Javascript      
  16.  *      2. Some Javascript coding proficiency to fully utilize this plugin    
  17.  *----------------------------------------------------------------------------
  18.  *    # Links                                                                
  19.  *      This plugin:                                                          
  20.  *      1. http://pastebin.com/nZRhF8vc                                      
  21.  *      Mentioned Patreon Supporters:
  22.  *      https://www.patreon.com/posts/71738797
  23.  *----------------------------------------------------------------------------
  24.  *    # Author                                                                
  25.  *      DoubleX                                                              
  26.  *----------------------------------------------------------------------------
  27.  *    # Changelog                                                            
  28.  *      v1.00d(GMT 1500 27-1-2016):                                          
  29.  *      1. Fixed enumerating this plugin's functions via for in loops bug    
  30.  *      v1.00c(GMT 0100 13-11-2015):                                          
  31.  *      1. Fixed tracing properties from null or non object object bug        
  32.  *      v1.00b(GMT 1100 11-11-2015):                                          
  33.  *      1. Added descriptions that will be shown in the plugin manager        
  34.  *      v1.00a(GMT 1500 27-10-2015):                                          
  35.  *      1. 1st version of this plugin finished                                
  36.  *============================================================================*/
  37. /*:
  38.  * @plugindesc Traces all object properties meeting some conditions linked to
  39.  *             the queried object
  40.  *             Designed as a bug diagnosis tool used by Javascript coders with
  41.  *             debug experience
  42.  * @author DoubleX
  43.  *
  44.  * @help
  45.  *============================================================================
  46.  *    ## Plugin Call Info                                                    
  47.  *       A path in the object property trace will stop if it'd be cyclic      
  48.  *----------------------------------------------------------------------------
  49.  *    # Object manipulations                                                  
  50.  *      1. trace_obj_prop(cond, label)                                        
  51.  *         - Traces all object properties satisfying function cond linked to  
  52.  *           this object                                                      
  53.  *         - Labels all traced object properties with function label          
  54.  *         - cond and label are functions written in                          
  55.  *           Object Property Tracing Condition Function and                  
  56.  *           Object Property Tracing Label Function respectively              
  57.  *      2. _obj_prop_log[cond]                                                
  58.  *         - Returns the log of all traced object properties satisfying      
  59.  *           function cond linked to this object                              
  60.  *============================================================================
  61.  */
  62.  
  63. "use strict";
  64. var DoubleX_RMMV = DoubleX_RMMV || {};
  65. DoubleX_RMMV["Obj_Prop"] = "v1.00d";
  66.  
  67. DoubleX_RMMV.Obj_Prop = {
  68.  
  69.     /*------------------------------------------------------------------------
  70.      *    Object Property Tracing Condition Function                          
  71.      *    - Setups cond used by trace_obj_prop(cond, label)                  
  72.      *------------------------------------------------------------------------*/
  73.     /* cond must be a function taking the object property as the only argument
  74.        The below examples are added to help you setup your own cond functions */
  75.  
  76.     // Checks if the currently traced object's indeed an object
  77.     cond_obj: function(obj) {
  78.         return typeof obj === "object";
  79.     }, // substitute cond with "cond_obj" to use this function
  80.  
  81.     // Checks if the currently traced object's an array
  82.     cond_array: function(obj) {
  83.         return Array.isArray(obj);
  84.     }, // substitute cond with "cond_array" to use this function
  85.  
  86.     // Add your own cond functions here
  87.    
  88.  
  89.     /*------------------------------------------------------------------------
  90.      *    Object Property Tracing Label Function                              
  91.      *    - Setups label used by trace_obj_prop(cond, label)                  
  92.      *------------------------------------------------------------------------*/
  93.     /* label must be a function taking the object property as the only argument
  94.        All label functions must return a string
  95.        The below examples are added to help you setup your own label functions */
  96.  
  97.     // Always returns the entire object
  98.     label_obj: function(obj) {
  99.         return obj;
  100.     }, // substitute label with "label_obj" to use this function
  101.  
  102.     // Always returns the type(including Array) of each traced object property
  103.     label_array: function(obj) {
  104.         return Array.isArray(obj) ? "array" : typeof obj;
  105.     } // substitute label with "label_array" to use this function
  106.  
  107.     // Add your own label functions here
  108.    
  109.  
  110. }; // DoubleX_RMMV.Obj_Prop
  111.  
  112. /*============================================================================
  113.  *    ## Plugin Implementations                                              
  114.  *       You need not edit this part as it's about how this plugin works      
  115.  *----------------------------------------------------------------------------
  116.  *    # Plugin Support Info:                                                  
  117.  *      1. Prerequisites                                                      
  118.  *         - Solid understanding of inspecting object properties in Javascript
  119.  *         - Decent Javascript coding proficiency to fully comprehend this    
  120.  *           plugin                                                          
  121.  *      2. Function documentation                                            
  122.  *         - The 1st part describes why this function's rewritten/extended for
  123.  *           rewritten/extended functions or what the function does for new  
  124.  *           functions                                                        
  125.  *         - The 2nd part describes what the arguments of the function are    
  126.  *         - The 3rd part informs which version rewritten, extended or created
  127.  *           this function                                                    
  128.  *         - The 4th part informs whether the function's rewritten or new    
  129.  *         - The 5th part informs whether the function's a real or potential  
  130.  *           hotspot                                                          
  131.  *         - The 6th part describes how this function works for new functions
  132.  *           only, and describes the parts added, removed or rewritten for    
  133.  *           rewritten or extended functions only                            
  134.  *         Example:                                                          
  135.  * /*----------------------------------------------------------------------
  136.  *  * Why rewrite/extended/What this function does                        
  137.  *  *----------------------------------------------------------------------*/
  138. /* // arguments: What these arguments are                                    
  139.  * function function_name(arguments) // Version X+; Rewrite/New; Hotspot      
  140.  *     // Added/Removed/Rewritten to do something/How this function works    
  141.  *     function_name_code                                                    
  142.  *     //                                                                    
  143.  * end // function_name                                                      
  144.  *----------------------------------------------------------------------------*/
  145.  
  146. Object.defineProperties(Object.prototype, {
  147.  
  148.     "trace_obj_prop": {
  149.         /* cond: The function checking whether an object property will be traced
  150.          * label: The function returning the label of an traced object property
  151.          */
  152.         value: function(cond, label) { // New
  153.             if (!this._obj_prop_trace) {
  154.                 this._obj_prop_log = {};
  155.                 this._obj_prop_trace = {};
  156.             }
  157.             // Stop tracing the object property if the path would be cyclic
  158.             if (this._obj_prop_trace[cond]) { return; }
  159.             //
  160.             this._obj_prop_log[cond] = "";
  161.             this._obj_prop_trace[cond] = {};
  162.             this.log_obj_prop(cond, label);
  163.         }, // Object.prototype.trace_obj_prop
  164.         writable: true,
  165.         configurable: true
  166.     },
  167.  
  168.     "log_obj_prop": {
  169.         /* cond: The function checking whether an object property will be traced
  170.          * label: The function returning the label of an traced object property
  171.          */
  172.         value: function(cond, label) { // New
  173.             // Checks if currently traced object property has object properties
  174.             var has_obj_prop = false;
  175.             for (var prop in this) {
  176.                 if (this.hasOwnProperty(prop) && this.is_obj_prop(prop)) {
  177.                     has_obj_prop = true;
  178.                     break;
  179.                 }
  180.             }
  181.             //
  182.             if (!has_obj_prop) { return; }
  183.             this._obj_prop_log[cond] = "{";
  184.             this.traverse_obj_prop_tree(cond, label);
  185.             this._obj_prop_log[cond] += "}";
  186.         }, // Object.prototype.log_obj_prop
  187.         writable: true,
  188.         configurable: true
  189.     },
  190.  
  191.     /*------------------------------------------------------------------------
  192.      *    Label and use all nonempty subtrees to form the object property tree
  193.      *------------------------------------------------------------------------*/
  194.     "traverse_obj_prop_tree": {
  195.         /* cond: The function checking whether an object property will be traced
  196.          * label: The function returning the label of an traced object property
  197.          */
  198.         value: function(cond, label) { // New
  199.             var op = DoubleX_RMMV.Obj_Prop;
  200.             for (var prop in this) {
  201.                 if (this.hasOwnProperty(prop) && this.is_obj_prop(prop)) {
  202.                     var obj = this[prop];
  203.                     // Recursively traverse property tree via Depth First Search
  204.                     if (op[cond](obj)) {
  205.                         this._obj_prop_log[cond] += " " + prop + ": " +
  206.                         op[label](obj) + " ";
  207.                         this._obj_prop_trace[cond][obj] = [prop];
  208.                     }
  209.                     var temp_prop = prop;
  210.                     if (obj === null || typeof obj !== "object") { continue; }
  211.                     obj.trace_obj_prop(cond, label);
  212.                     if (Object.keys(obj._obj_prop_trace[cond]).length > 0) {
  213.                         if (this._obj_prop_trace[cond][obj] === undefined) {
  214.                             this._obj_prop_trace[cond][obj] = [];
  215.                         }
  216.                         this._obj_prop_log[cond] += " " + temp_prop + ": " +
  217.                         obj._obj_prop_log[cond];
  218.                         this._obj_prop_trace[cond][obj].push(
  219.                         obj._obj_prop_trace[cond]);
  220.                     }
  221.                     //
  222.                 }
  223.             }
  224.         }, // Object.prototype.traverse_obj_prop_tree
  225.         writable: true,
  226.         configurable: true
  227.     },
  228.  
  229.     "is_obj_prop": {
  230.         // prop: The current object property to be traced
  231.         value: function(prop) { // New
  232.             // Return false for all object properties added by this plugin
  233.             if (prop === "_obj_prop_log" || prop === "_obj_prop_trace") {
  234.                 return false;
  235.             } else if (prop === "trace_obj_prop" || prop === "log_obj_prop") {
  236.                 return false;
  237.             }
  238.             return prop !== "traverse_obj_prop_tree" && prop !== "is_obj_prop";
  239.             //
  240.         }, // Object.prototype.is_obj_prop
  241.         writable: true,
  242.         configurable: true
  243.     }
  244.  
  245. });
  246.  
  247. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement