Advertisement
DayDun

[CoasterTown] Eval command

Dec 30th, 2018
5,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Handy eval command by DayDun.
  4.  
  5. */
  6.  
  7. def string::slice(start) {
  8.     var out = "";
  9.    
  10.     for (var i=start; i<this.size(); ++i) {
  11.         out += to_string(this[i]);
  12.     }
  13.    
  14.     return out;
  15. }
  16.  
  17. def broadcast(message) {
  18.     for (var i=0; i<CountPlayers(); ++i) {
  19.         var player = GetPlayerAtIndex(i);
  20.         ConsoleMessage(player, message);
  21.     }
  22. }
  23.  
  24. def stringify(value) {
  25.     var numColor = "^6";
  26.     var boolColor = "^2";
  27.     var strColor = "^9";
  28.     var voidColor = "^0";
  29.     var vectorColor = "^7";
  30.     var mapColor = "^7";
  31.     var mapPairColor = "^3";
  32.     var errorColor = "^1";
  33.     var unknownColor = "^7";
  34.    
  35.     var type = type_name(value);
  36.     var out;
  37.    
  38.     if (type == "double") {
  39.         var str = to_string(value);
  40.         if (str.find(".") == -1) {
  41.             str += ".0";
  42.         }
  43.         out = "${numColor}${str}";
  44.     } else if (type == "float") {
  45.         var str = to_string(value);
  46.         if (str.find(".") == -1) {
  47.             str += ".0";
  48.         }
  49.         out = "${numColor}${str}f";
  50.     } else if (type == "long_double") {
  51.         var str = to_string(value);
  52.         if (str.find(".") == -1) {
  53.             str += ".0";
  54.         }
  55.         out = "${numColor}${str}l";
  56.     } else if (type == "int") {
  57.         out = "${numColor}${to_string(value)}";
  58.     } else if (type == "size_t") {
  59.         out = "${numColor}${to_string(value)}u";
  60.     } else if (type == "unsigned_long") {
  61.         out = "${numColor}${to_string(value)}ul";
  62.     } else if (type == "uint64_t") {
  63.         out = "${numColor}${to_string(value)}ull";
  64.     } else if (type == "bool") {
  65.         out = "${boolColor}${to_string(value)}";
  66.     } else if (type == "string") {
  67.         out = "${strColor}\"${value}\"";
  68.     } else if (type == "void") {
  69.         out = "${voidColor}void";
  70.     } else if (type == "Vector") {
  71.         out = "${vectorColor}[";
  72.         for (var i=0; i<value.size(); ++i) {
  73.             out += stringify(value[i]);
  74.             if (i != value.size() - 1) {
  75.                 out += "${vectorColor}, ";
  76.             }
  77.         }
  78.         out += "${vectorColor}]";
  79.     } else if (type == "Map") {
  80.         out = "${mapColor}[";
  81.         var range = value.range();
  82.         while(true) {
  83.             out += stringify(range.front());
  84.             range.pop_front();
  85.            
  86.             if (range.empty()) {
  87.                 break;
  88.             }
  89.            
  90.             out += "${mapColor}, ";
  91.         }
  92.        
  93.         out += "${mapColor}]";
  94.     } else if (type == "Map_Pair") {
  95.         out = "${mapPairColor}<";
  96.         out += stringify(value.first());
  97.         out += "${mapPairColor}: ";
  98.         out += stringify(value.second());
  99.         out += "${mapPairColor}>";
  100.     } else if (type == "eval_error") {
  101.         out = "${errorColor}${value.reason}";
  102.     } else {
  103.         out = "${unknownColor}[${type}]";
  104.     }
  105.    
  106.     return out;
  107. }
  108.  
  109. def OnPlayerChat(player, text) {
  110.     if (text.find("!eval ") != 0) {
  111.         return;
  112.     }
  113.    
  114.     text = text.slice(6);
  115.    
  116.    
  117.     var value;
  118.     try {
  119.         value := eval(text);
  120.     } catch(e) {
  121.         value := e;
  122.     }
  123.    
  124.     broadcast("> " + stringify(value));
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement