Advertisement
aporokizzu

main.ahk (1/3)

Mar 2nd, 2023
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 4.79 KB | Source Code | 0 0
  1. ; main.ahk
  2. ; by Aporokizzu
  3. ; 2023.03.02
  4.  
  5. #Include "Yaml.ahk"
  6.  
  7. strStripCrlf(str) {
  8.  
  9.     str := RegExReplace(str, "`n")
  10.     str := RegExReplace(str, "`r")
  11.  
  12.     return str
  13.  
  14. }
  15.  
  16. writeFile(data, fn) {
  17.  
  18.     fn := strStripCrlf(fn)
  19.  
  20.     f := FileOpen(fn, "w")
  21.     f.Write(data)
  22.     f.Close()
  23.  
  24.  
  25. }
  26.  
  27. class JsonShine {
  28.  
  29.     __New(jsonFile) {
  30.    
  31.         this._ready := false
  32.         this._needsExtraFudging := false
  33.        
  34.         this.__setup(jsonFile)
  35.    
  36.     }
  37.    
  38.     __STR_JSON_HLPROBJ_NAME() {
  39.    
  40.         retVal := "funky_cold_medina"
  41.         return retVal
  42.    
  43.     }
  44.    
  45.     __STR_TRUE(addQuotes := true) {
  46.    
  47.         retVal := "-*-vrai-*-"
  48.         if (addQuotes) {
  49.  
  50.             retVal := '"' retVal '"'
  51.  
  52.         }
  53.         return retVal
  54.  
  55.     }
  56.    
  57.         __STR_FALSE(addQuotes := true) {
  58.    
  59.         retVal := "-*-faux-*-"
  60.         if (addQuotes) {
  61.  
  62.             retVal := '"' retVal '"'
  63.  
  64.         }
  65.         return retVal
  66.  
  67.     }
  68.    
  69.     __STR_NULL(addQuotes := true) {
  70.    
  71.         retVal := "-*-null-*-"
  72.         if (addQuotes) {
  73.  
  74.             retVal := '"' retVal '"'
  75.  
  76.         }
  77.         return retVal
  78.  
  79.     }
  80.    
  81.     __setup(jsonFile) {
  82.    
  83.         checkJson := FileExist(jsonFile)
  84.        
  85.         if (!checkJson) {
  86.             Return
  87.         }
  88.        
  89.         this._fullPath := jsonFile
  90.        
  91.         SplitPath(this._fullPath, &justFn, &justDir)
  92.        
  93.         this._fileName := justFn
  94.         this._folderName := justDir
  95.        
  96.         rawData := FileRead(this._fullPath)
  97.        
  98.         this._needsExtraFudging := this.__isArrayJson(rawData)
  99.        
  100.         rawData := this.__fudgeJson(rawData)
  101.        
  102.         this._data := Yaml(rawData)
  103.        
  104.         this._ready := true
  105.    
  106.     }
  107.    
  108.     __isArrayJson(data) {
  109.    
  110.         firstChar := SubStr(data, 1, 1)
  111.  
  112.         if (firstChar = "[") {
  113.             return True
  114.         }
  115.        
  116.         return False
  117.        
  118.     }
  119.    
  120.     __fudgeJson(data) {
  121.    
  122.         if (this._needsExtraFudging) {
  123.        
  124.             data := '{"' this.__STR_JSON_HLPROBJ_NAME() '":' data  '}'
  125.        
  126.         }
  127.        
  128.         data := StrReplace(data, "true", this.__STR_TRUE())
  129.         data := StrReplace(data, "false", this.__STR_FALSE())
  130.        
  131.         data := StrReplace(data, "null", this.__STR_NULL())
  132.        
  133.         return data
  134.    
  135.     }
  136.    
  137.     __unfudgeJson(data) {
  138.    
  139.         if (this._needsExtraFudging) {
  140.        
  141.             startPos := (InStr(data, this.__STR_NULL()) - 2)
  142.             dataLen := (StrLen(data) - (startPos + 1))
  143.             data := SubStr(data, startPos, dataLen)
  144.        
  145.         }
  146.        
  147.         data := StrReplace(data, this.__STR_TRUE(), "true")
  148.         data := StrReplace(data, this.__STR_FALSE(), "false")
  149.        
  150.         data := StrReplace(data, this.__STR_NULL(), "null")
  151.        
  152.         return data
  153.        
  154.     }
  155.        
  156.     __writeFullPath(data, fullPath := this._fullPath) {
  157.    
  158.         if (this._needsExtraFudging) {
  159.    
  160.             data := "[`n" data "`n]"
  161.    
  162.         }
  163.    
  164.         writeFile(data, fullPath)
  165.    
  166.     }
  167.    
  168.     data() {
  169.  
  170.         if (this._needsExtraFudging) {
  171.  
  172.             return (this._data[this.__STR_JSON_HLPROBJ_NAME()])
  173.  
  174.         }
  175.  
  176.         return (this._data)
  177.  
  178.     }
  179.    
  180.    
  181.     dump() {
  182.    
  183.         if (this._ready) {
  184.        
  185.             this.__polish()
  186.            
  187.             data := Yaml(this.data(), -69)
  188.            
  189.             data := this.__unfudgeJson(data)
  190.            
  191.             this.__writeFullPath(data)
  192.        
  193.         }
  194.    
  195.     }
  196.    
  197.     __polish() {
  198.    
  199.         ; TO BE OVERWRITTEN
  200.    
  201.     }
  202.    
  203. }
  204.  
  205. class ClassesJson extends JsonShine {
  206.  
  207.     dataParams(idx := 2) {
  208.    
  209.         return this.data()[idx]["params"]
  210.    
  211.     }
  212.  
  213.     __genParam(leadSeqArr := [], pttrnArr := [], loopLen := 99) {
  214.  
  215.         retVal := []
  216.  
  217.         leadSeqArrLen := leadSeqArr.Length
  218.         pttrnArrLen := pttrnArr.Length
  219.  
  220.         if ((leadSeqArrLen != 0) and (pttrnArrLen != 0)) {
  221.  
  222.             retVal := leadSeqArr
  223.  
  224.             remainingCharsInParamSeq := ((loopLen - leadSeqArrLen) - 1)
  225.  
  226.             count := 0
  227.  
  228.             currentChar := Ceil(leadSeqArr[leadSeqArrLen])
  229.  
  230.             Loop remainingCharsInParamSeq {
  231.  
  232.                 count += 1
  233.                 additive := Ceil(pttrnArr[count])
  234.                 currentChar += additive
  235.  
  236.                 retVal.Push((Ceil(currentChar) + 0))
  237.  
  238.                 if (count = pttrnArrLen) {
  239.  
  240.                     count := 0
  241.  
  242.                 }
  243.  
  244.             }
  245.  
  246.         }
  247.  
  248.         return retVal
  249.  
  250.     }
  251.    
  252.     __polish() {
  253.    
  254.         this.data().Capacity := 2
  255.        
  256.         setDataParam(idx, leadSeq, pttrn) {
  257.        
  258.             paramLen := this.dataParams(2)[idx].Length
  259.             param := this.__genParam(leadSeq, pttrn, paramLen)
  260.             paramLen := param.Length
  261.            
  262.             this.dataParams(2)[idx] := []
  263.            
  264.             Loop paramLen {
  265.                        
  266.                 currentItem := (param[A_Index] + 0)
  267.                 this.dataParams(2)[idx].push(currentItem)
  268.            
  269.             }
  270.        
  271.         }
  272.        
  273.         setDataParam(1, [1, 8], [8])
  274.         setDataParam(2, [0, 128], [0])
  275.         setDataParam(3, [1, 16], [2, 1, 1, 1])
  276.         setDataParam(4, [0, 32], [3, 2])
  277.        
  278.     }
  279.  
  280. }
  281.  
  282. class Main {
  283.  
  284.     __New(jsonFile) {
  285.    
  286.         this._jsonSrc := jsonFile
  287.    
  288.     }
  289.  
  290.     run() {
  291.    
  292.         clsssJson := ClassesJson(this._jsonSrc)
  293.         clsssJson.dump()
  294.    
  295.     }
  296.  
  297. }
  298.  
  299. jsonFn := A_ScriptDir "\" "Classes.json"
  300. App := Main(jsonFn)
  301. App.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement