Overwrite.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. class Overwrite{
  2. _func = {};
  3. func(passage,args){
  4. passage = passage.toLowerCase();
  5. if(this._func[passage] === undefined)
  6. return undefined;
  7. if(typeof this._func[passage] == "function")
  8. return this._func[passage](args);
  9. return this._func[passage];
  10. }
  11. funcRegister(passage,code){
  12. this._func[passage.toLowerCase()] = code;
  13. }
  14. _gs = {};
  15. gs(passage,args){
  16. passage = passage.toLowerCase();
  17. if(this._gs[passage] === undefined)
  18. return undefined;
  19. if(typeof this._gs[passage] == "function")
  20. return this._gs[passage](args);
  21. return this._gs[passage];
  22. }
  23. gsRegister(passage,code){
  24. this._gs[passage.toLowerCase()] = code;
  25. }
  26. _var = {};
  27. _varByPrefix = {};
  28. varGet(id,index){
  29. let overwrite = null;
  30. for(const [prefix, ow] of Object.entries(this._varByPrefix)){
  31. if(id.startsWith(prefix)){
  32. overwrite = ow;
  33. if(typeof overwrite.get == "function")
  34. return overwrite.get(id.substring(prefix.length),index);
  35. break;
  36. }else if(id.startsWith("$"+prefix)){
  37. overwrite = ow;
  38. if(typeof overwrite.get == "function")
  39. return overwrite.get(id.substring(prefix.length+1),index);
  40. break;
  41. }
  42. }
  43. if(this._var[id] !== undefined && this._var[id].get !== undefined)
  44. overwrite = this._var[id];
  45. if(!overwrite)
  46. return undefined;
  47. if(typeof overwrite.get == "function")
  48. return overwrite.get(index);
  49. if(Array.isArray(overwrite.get) && typeof index == "number")
  50. return overwrite.get[index];
  51. return overwrite.get;
  52. }
  53. varSet(id,index,val){
  54. let overwrite = null;
  55. for(const [prefix, ow] of Object.entries(this._varByPrefix)){
  56. if(id.startsWith(prefix)){
  57. overwrite = ow;
  58. if(typeof overwrite.set == "function")
  59. return overwrite.set(id.substring(prefix.length),index,val);
  60. break;
  61. }
  62. else if(id.startsWith("$"+prefix)){
  63. overwrite = ow;
  64. if(typeof overwrite.set == "function")
  65. return overwrite.set(id.substring(prefix.length+1),index,val);
  66. break;
  67. }
  68. }
  69. if(this._var[id] === undefined || this._var[id].set === undefined)
  70. return undefined;
  71. if(typeof this._var[id].set == "function")
  72. return this._var[id].set(index,val);
  73. console.error("Unsupported type for Overwrite.var.set (id,val)",id,val);
  74. }
  75. varRegister(id,get,set){
  76. this._var[id.toLowerCase()] = {get:get, set:set};
  77. }
  78. varRegisterByPrefix(prefix,get,set){
  79. this._varByPrefix[prefix.toLowerCase()] = {get:get, set:set};
  80. }
  81. }
  82. setup.Overwrite = new Overwrite();