Advertisement
Narzew

NRGSS LATEST

Apr 8th, 2013
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 33.71 KB | None | 0 0
  1. #===========================================================
  2. #**NRGSS (Narzew RGSS Module)
  3. #**Narzew
  4. #**Version 2.6
  5. #===========================================================
  6. #**History:
  7. #**11.05.12 - 1.0
  8. #**20.05.12 - 1.1
  9. #**1.06.12 - 1.2
  10. #**3.06.12 - 1.3
  11. #**10.06.12 - 1.4
  12. #**22.06.12 - 1.5
  13. #**13.07.12 - 1.6
  14. #**27.07.12 - 1.7
  15. #**09.08.12 - 1.8
  16. #**24.08.12 - 1.9
  17. #**26.08.12 - 2.0
  18. #**29.08.12 - 2.1
  19. #**4.09.12 - 2.2
  20. #**8.09.12 - 2.3
  21. #**18.09.12 - 2.4
  22. #**06.10.12 - 2.5
  23. #**19.01.13 - 2.6
  24. #===========================================================
  25.  
  26. #===========================================================
  27. #**Module Authors:
  28. #**Narzew
  29. #**Peter O.
  30. #**A Crying Minister
  31. #**Forever Zer0
  32. #**KGC
  33. #===========================================================
  34.  
  35. #===========================================================
  36. #**Start of Library
  37. #**Początek biblioteki
  38. #===========================================================
  39.  
  40. #===========================================================
  41. #**NRGSS Class
  42. #**Klasa główna NRGSS
  43. #===========================================================
  44.  
  45. class NRGSS
  46.  
  47.   #===========================================================
  48.   #**initialize
  49.   #**Defines a initialisation of class
  50.   #**Definiuje inicjalizację klasy
  51.   #**Narzew
  52.   #===========================================================
  53.  
  54.   def initialize
  55.     $nrgss_version = 2.6
  56.   end
  57.  
  58.   #===========================================================
  59.   #**version
  60.   #**Defines a library Version
  61.   #**Definiuje wersję biblioteki
  62.   #**Narzew
  63.   #===========================================================
  64.  
  65.   def version
  66.     return $nrgss_version
  67.   end
  68.  
  69.   #===========================================================
  70.   #**Variable definitions
  71.   #**Definicje zmiennych
  72.   #**Narzew
  73.   #===========================================================
  74.  
  75.   $error_section_num = (/^(?:Section)?{?(\d+)}?:/)
  76.   $error_section = (/^(?:Section)?{?\d+}?:/)
  77.   $double_crlf = (/\n\n/)
  78.   $line = "\n"
  79.   $doubleline = "\n\n"
  80.  
  81.   $messagebox = Win32API.new('user32', 'MessageBoxA', %w(p p p i), 'i')
  82.   $msgbox = Win32API.new('user32', 'MessageBoxA', %w(p p p i), 'i')
  83.   $getprivateprofilestring = Win32API.new('kernel32', 'GetPrivateProfileStringA',%w(p p p p l p),'l')
  84.   $mcisendstring = Win32API.new('winmm', 'mciSendString', 'PPLL', 'L')
  85.   $midioutsetvolume = Win32API.new('winmm', 'midiOutSetVolume', 'LL', 'L')
  86.  
  87.   $halfbyte = 0xF
  88.   $byte = 0xFF
  89.   $doublebyte = 0xFFFF
  90.   $threebyte = 0xFFFFFF
  91.   $long = 0xFFFFFF
  92.   $doublelong = 0xFFFFFFFFFFFFFFFF
  93.   $hex = $halfbyte
  94.   $hex2 = $byte
  95.   $hex4 = $doublebyte
  96.   $hex6 = $threebyte
  97.   $hex8 = $long
  98.   $hex16 = $doublelong
  99.   $hex32 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  100.   $hex48 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  101.   $hex64 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  102.   $hexes = [16**0,16**1,16**2,16**3,16**4,16**5,16**6,16**7,16**8,16**9,16**10,16**11,16**12,16**13,16**14,16**15,16**16]
  103.  
  104.   $registered_names = []
  105.   $registered_authors = []
  106.   $registered_scripts = {}
  107.  
  108.   #===========================================================
  109.   #**register
  110.   #**Registers a component
  111.   #**Rejestruje komponent
  112.   #**Narzew
  113.   #===========================================================
  114.  
  115.   def register(product, author, version)
  116.     $registered_scripts[product] =  [author, version]
  117.     $registered_authors << author
  118.     $registered_names << product
  119.   end
  120.  
  121.   #===========================================================
  122.   #**registered?(product)
  123.   #**Checks the component is registered
  124.   #**Sprawdza czy komponent jest zarejestrowany
  125.   #**Narzew
  126.   #===========================================================
  127.  
  128.   def registered?(product, rd=false)
  129.     if $registered_scripts.include?(product)
  130.       if rd
  131.         return $registered_scripts[product]
  132.       else
  133.         return true
  134.       end
  135.     else
  136.       return false
  137.     end
  138.   end
  139.  
  140.   #===========================================================
  141.   #**check_version(product, version)
  142.   #**Checks the component is in valid version
  143.   #**Sprawdza czy komponent jest w poprawnej wersji
  144.   #===========================================================
  145.  
  146.   def check_version(product, version, raise=0)
  147.     return false unless registered?(product)
  148.     if $registered_scripts[product][1] >= version
  149.       return true
  150.     else
  151.       raise("Bad version of product : #{product}.\nVersion : #{version} or later is required.") if raise == 1
  152.       return false
  153.     end
  154.   end
  155.  
  156.   #===========================================================
  157.   #**in_range?
  158.   #**Checks the value is between x and y
  159.   #**Sprawdza czy wartość jest pomiedzy x a y
  160.   #**Narzew
  161.   #===========================================================
  162.  
  163.   def in_range?( x, y, value)
  164.     value = self if value ==  nil
  165.     return true if value.to_i > x.to_i and value < y.to_i
  166.   end
  167.  
  168.   #===========================================================
  169.   #**round_range
  170.   #**Rounds the value to range
  171.   #**Zaokrągla wartość do zasięgu
  172.   #**Narzew
  173.   #===========================================================
  174.  
  175.   def round_range(min, max, value)
  176.     value = self if value == nil
  177.     if value > max
  178.       value = max
  179.     elsif value < min
  180.       value = min
  181.     end
  182.     return value
  183.   end
  184.  
  185.   #===========================================================
  186.   #**unpack_byte
  187.   #**Unpacks byte to letter
  188.   #**Odpakowuje bajt do litery
  189.   #**Narzew
  190.   #===========================================================
  191.  
  192.   def unpack_byte(byte)
  193.     result  = []
  194.     case byte
  195.     when 0 then result << '$NULL$'
  196.     when 1 then result << '$SOH$'
  197.     when 2 then result << '$STX$'
  198.     when 3 then result << '$ETX$'
  199.     when 4 then result << '$EOT$'
  200.     when 5 then result << '$ENQ$'
  201.     when 6 then result << '$ACK$'
  202.     when 7 then result << '$BEL$'
  203.     when 8 then result << '$BS$'
  204.     when 9 then result << '$TAB$'
  205.     when 10 then result << '$LF$'
  206.     when 11 then result << '$VT$'
  207.     when 12 then result << '$FF$'
  208.     when 13 then result << '$CR$'
  209.     when 14 then result << '$SO$'
  210.     when 15 then result << '$SI$'
  211.     when 16 then result << '$DLE$'
  212.     when 17 then result << '$DC1$'
  213.     when 18 then result << '$DC2$'
  214.     when 19 then result << '$DC3$'
  215.     when 20 then result << '$DC4$'
  216.     when 21 then result << '$NAK$'
  217.     when 22 then result << '$SYN$'
  218.     when 23 then result << '$ETB$'
  219.     when 24 then result << '$CAN$'
  220.     when 25 then result << '$EN$'
  221.     when 26 then result << '$SUB$'
  222.     when 27 then result << '$ESC$'
  223.     when 28 then result << '$FS$'
  224.     when 29 then result << '$GS$'
  225.     when 30 then result << '$RS$'
  226.     when 31 then result << '$US$'
  227.     when 32 then result << ' '
  228.     when 33 then result << '!'
  229.     when 34 then result << '\"'
  230.     when 35 then result << '#'
  231.     when 36 then result << '$'
  232.     when 37 then result << '%'
  233.     when 38 then result << '&'
  234.     when 39 then result << '\''
  235.     when 40 then result << '('
  236.     when 41 then result << ')'
  237.     when 42 then result << '*'
  238.     when 43 then result << '+'
  239.     when 44 then result << ','
  240.     when 45 then result << '-'
  241.     when 46 then result << '.'
  242.     when 47 then result << '/'
  243.     when 48 then result << '0'
  244.     when 49 then result << '1'
  245.     when 50 then result << '2'
  246.     when 51 then result << '3'
  247.     when 52 then result << '4'
  248.     when 53 then result << '5'
  249.     when 54 then result << '6'
  250.     when 55 then result << '7'
  251.     when 56 then result << '8'
  252.     when 57 then result << '9'
  253.     when 58 then result << ':'
  254.     when 59 then result << ';'
  255.     when 60 then result << '<'
  256.     when 61 then result << '='
  257.     when 62 then result << '>'
  258.     when 63 then result << '?'
  259.     when 64 then result << '@'
  260.     when 65 then result << 'A'
  261.     when 66 then result << 'B'
  262.     when 67 then result << 'C'
  263.     when 68 then result << 'D'
  264.     when 69 then result << 'E'
  265.     when 70 then result << 'F'
  266.     when 71 then result << 'G'
  267.     when 72 then result << 'H'
  268.     when 73 then result << 'I'
  269.     when 74 then result << 'J'
  270.     when 75 then result << 'K'
  271.     when 76 then result << 'L'
  272.     when 77 then result << 'M'
  273.     when 78 then result << 'N'
  274.     when 79 then result << 'O'
  275.     when 80 then result << 'P'
  276.     when 81 then result << 'Q'
  277.     when 82 then result << 'R'
  278.     when 83 then result << 'S'
  279.     when 84 then result << 'T'
  280.     when 85 then result << 'U'
  281.     when 86 then result << 'V'
  282.     when 87 then result << 'W'
  283.     when 88 then result << 'X'
  284.     when 89 then result << 'Y'
  285.     when 90 then result << 'Z'
  286.     when 91 then result << '['
  287.     when 92 then result << '\\'
  288.     when 93 then result << ']'
  289.     when 94 then result << '^'
  290.     when 95 then result << '_'
  291.     when 96 then result << '`'
  292.     when 97 then result << 'a'
  293.     when 98 then result << 'b'
  294.     when 99 then result << 'c'
  295.     when 100 then result << 'd'
  296.     when 101 then result << 'e'
  297.     when 102 then result << 'f'
  298.     when 103 then result << 'g'
  299.     when 104 then result << 'h'
  300.     when 105 then result << 'i'
  301.     when 106 then result << 'j'
  302.     when 107 then result << 'k'
  303.     when 108 then result << 'l'
  304.     when 109 then result << 'm'
  305.     when 110 then result << 'n'
  306.     when 111 then result << 'o'
  307.     when 112 then result << 'p'
  308.     when 113 then result << 'q'
  309.     when 114 then result << 'r'
  310.     when 115 then result << 's'
  311.     when 116 then result << 't'
  312.     when 117 then result << 'u'
  313.     when 118 then result << 'v'
  314.     when 119 then result << 'w'
  315.     when 120 then result << 'x'
  316.     when 121 then result << 'y'
  317.     when 122 then result << 'z'
  318.     when 123 then result << '{'
  319.     when 124 then result << '|'
  320.     when 125 then result << '}'
  321.     when 126 then result << '~'
  322.     when 127 then result << '$DEL$'
  323.     else
  324.       result << '$NOT$'
  325.     end
  326.     return result
  327.   end
  328.  
  329.   #===========================================================
  330.   #**unpack_clear_byte
  331.   #**Unpacks clear (>32) byte
  332.   #**Depakowuje czysty (>32) bajt
  333.   #**Narzew
  334.   #===========================================================
  335.  
  336.   def unpack_clear_byte(byte)
  337.     if byte <= 31
  338.       byte = 32
  339.     end
  340.     $nrgss.unpack_byte(byte)
  341.   end
  342.  
  343.   #===========================================================
  344.   #**unpack_text
  345.   #**Unpacks byte packed text (array)
  346.   #**Odpakowuje tekst zapisany w tablicy bajtów
  347.   #**Narzew
  348.   #===========================================================
  349.  
  350.   def unpack_text(texttabl)
  351.     result = []
  352.     texttabl.each{|byte|
  353.     result << $nrgss.unpack_byte(byte)
  354.     }
  355.     return result.to_s
  356.   end
  357.  
  358.   #===========================================================
  359.   #**pack_bytes
  360.   #**Packs bytes to array from text
  361.   #**Pakuje bajty do tablicy z tekstu
  362.   #**Narzew
  363.   #===========================================================
  364.  
  365.   def pack_bytes(text)
  366.     result = []
  367.     text.each_byte{|byte| result << byte }
  368.     return result
  369.   end
  370.  
  371.   #===========================================================
  372.   #**pack_byte
  373.   #**Packs one byte to array from text
  374.   #**Pakuje pojedyńczy bajt do tablicy z tekstu
  375.   #**Narzew
  376.   #===========================================================
  377.  
  378.   def pack_byte(letter)
  379.     result = []
  380.     letter.each_byte{|byte| result << letter }
  381.     return result.at(0)
  382.   end
  383.  
  384.   #===========================================================
  385.   #**encrypt_int
  386.   #**Encrypts an int
  387.   #**Koduje liczbę
  388.   #**Narzew
  389.   #===========================================================
  390.  
  391.   def encrypt_int(int, key=17)
  392.     srand(key * 7 + 3)
  393.     return rand(9999) + int
  394.   end
  395.  
  396.   #===========================================================
  397.   #**decrypt_int
  398.   #**Decrypts an int
  399.   #**Dekoduje liczbę
  400.   #**Narzew
  401.   #===========================================================
  402.  
  403.   def decrypt_int(int, key=17)
  404.     srand(key * 7 + 3)
  405.     return int - rand(9999)
  406.   end
  407.  
  408.   #===========================================================
  409.   #**file_read
  410.   #**Reads file
  411.   #**Odczytuje plik
  412.   #**Narzew
  413.   #===========================================================
  414.  
  415.   def file_read(file2)
  416.     file = File.open(file2, 'rb')
  417.     return file.read
  418.   end
  419.  
  420.   #===========================================================
  421.   #**file_write
  422.   #**Writes file
  423.   #**Zapisuje plik
  424.   #**Narzew
  425.   #===========================================================
  426.  
  427.   def file_write(var, file2)
  428.     file = File.open(file2, 'wb')
  429.     file.write(var)
  430.     file.close
  431.   end
  432.  
  433.   #===========================================================
  434.   #**file_dump
  435.   #**Dumps to file
  436.   #**Zapisuje dane binarne do pliku
  437.   #**Narzew
  438.   #===========================================================
  439.  
  440.   def file_dump(var, file2)
  441.     file = File.open(file2, 'wb')
  442.     Marshal.dump(var, file)
  443.     file.close
  444.   end
  445.  
  446.   #===========================================================
  447.   #**file_link
  448.   #**Links two files
  449.   #**Łączy dwa pliki
  450.   #**Narzew
  451.   #===========================================================
  452.  
  453.   def file_link(afile, bfile, cfile)
  454.     file1 = File.open(afile, 'rb')
  455.     file2 = File.open(bfile, 'rb')
  456.     file3 = File.open(cfile, 'wb')
  457.     file3.write(file1.read)
  458.     file3.write(file2.read)
  459.     file1.close
  460.     file2.close
  461.     file3.close
  462.   end
  463.  
  464.   #===========================================================
  465.   #**cbrt
  466.   #**3rd root
  467.   #**Pierwiastek sześcienny
  468.   #**Narzew
  469.   #===========================================================
  470.  
  471.   def cbrt(x)
  472.     result = x**(1.0/3.0)
  473.     return result
  474.   end
  475.  
  476.   #===========================================================
  477.   #**root
  478.   #**Root
  479.   #**Pierwiastek
  480.   #**Narzew
  481.   #===========================================================
  482.  
  483.   def root(nr, st)
  484.     result = nr**(1.0/st.to_f)
  485.     return result
  486.   end
  487.  
  488.   #===========================================================
  489.   #**oppose
  490.   #**Oppose
  491.   #**Przeciwność
  492.   #**Narzew
  493.   #===========================================================
  494.  
  495.   def oppose(nr)
  496.     result = nr - (nr*2)
  497.     return result
  498.   end
  499.  
  500.   #===========================================================
  501.   #**randomize
  502.   #**Randomize a int with range
  503.   #**Losuje liczbę z zakresu
  504.   #**Narzew
  505.   #===========================================================
  506.  
  507.   def randomize(min, max)
  508.     return rand(max - min + 1)
  509.   end
  510.  
  511.   #===========================================================
  512.   #**save_script
  513.   #**Saves a RGSS scripts to text
  514.   #**Zapisuje skrypty RGSS do tekstu
  515.   #**Narzew
  516.   #===========================================================
  517.  
  518.   def save_script(filename='script.txt', scriptfile='Data/Scripts.rxdata')
  519.     file = File.open(filename, 'wb')
  520.     script = load_data(scriptfile)
  521.     script.each {|s|
  522.     file.write(Zlib::Inflate.inflate(s.at(2)))
  523.     file.write("\n\n\n")
  524.     }
  525.     file.close
  526.   end
  527.  
  528.   #===========================================================
  529.   #**Messagebox
  530.   #**Messgebox
  531.   #**Okienko z wiadomością
  532.   #**A Crying Minister
  533.   #===========================================================
  534.  
  535.   def messagebox(message, title, type)
  536.     $msgbox.call(0, message, title, type)
  537.   end
  538.  
  539.   #===========================================================
  540.   #**b64_encode
  541.   #**Encodes string using Base64 algorithm
  542.   #**Koduje ciąg używając algorytmu Base64
  543.   #**Narzew
  544.   #===========================================================
  545.  
  546.   def b64_encode(x)
  547.     return [x].pack('m')
  548.   end
  549.  
  550.   #===========================================================
  551.   #**b64_decode
  552.   #**Decodes string using Base64 algorithm
  553.   #**Dekoduje ciąg używając algorytmu Base64
  554.   #**Narzew
  555.   #===========================================================
  556.  
  557.   def b64_decode(x)
  558.     return x.unpack('m').first
  559.   end
  560.  
  561.   #===========================================================
  562.   #**uu_encode
  563.   #**Encodes string using UU algorithm
  564.   #**Koduje ciąg używając algorytmu UU
  565.   #**Narzew
  566.   #===========================================================
  567.  
  568.   def uu_encode(x)
  569.     return [x].pack('u')
  570.   end
  571.  
  572.   #===========================================================
  573.   #**uu_decode
  574.   #**Decodes string using UU algorithm
  575.   #**Dekoduje ciąg używając algorytmu UU
  576.   #**Narzew
  577.   #===========================================================
  578.  
  579.   def uu_decode(x)
  580.     return x.unpack('u')
  581.   end
  582.  
  583.   #===========================================================
  584.   #**b64_encode_file
  585.   #**Encodes file using Base64 algorithm
  586.   #**Koduje plik używając algorytmu Base64
  587.   #**Narzew
  588.   #===========================================================
  589.  
  590.   def b64_encode_file(file, result)
  591.     readfile = File.open(file, 'rb')
  592.     writefile = File.open(result, 'wb')
  593.     data = readfile.read
  594.     writefile.write($nrgss.b64_encode(data))
  595.     readfile.close
  596.     writefile.close
  597.   end
  598.  
  599.   #===========================================================
  600.   #**b64_decode_file
  601.   #**Decodes file using Base64 algorithm
  602.   #**Dekoduje plik używając algorytmu Base64
  603.   #**Narzew
  604.   #===========================================================
  605.  
  606.   def b64_decode_file(file, result)
  607.     readfile = File.open(file, 'rb')
  608.     writefile = File.open(result, 'wb')
  609.     data = readfile.read
  610.     writefile.write($nrgss.b64_decode(data))
  611.     readfile.close
  612.     writefile.close
  613.   end
  614.  
  615.   #===========================================================
  616.   #**uu_encode_file
  617.   #**Encodes file using UU algorithm
  618.   #**Koduje plik używając algorytmu UU
  619.   #**Narzew
  620.   #===========================================================
  621.  
  622.   def uu_encode_file(file, result)
  623.     readfile = File.open(file, 'rb')
  624.     writefile = File.open(result, 'wb')
  625.     data = readfile.read
  626.     writefile.write($nrgss.uu_encode(data))
  627.     readfile.close
  628.     writefile.close
  629.   end
  630.  
  631.   #===========================================================
  632.   #**uu_decode_file(file, result)
  633.   #**Decodes file using UU algorithm
  634.   #**Dekoduje plik używając algorytmu UU
  635.   #**Narzew
  636.   #===========================================================
  637.  
  638.   def uu_decode_file(file, result)
  639.     readfile = File.open(file, 'rb')
  640.     writefile = File.open(result, 'wb')
  641.     data = readfile.read
  642.     writefile.write($nrgss.uu_decode(data))
  643.     readfile.close
  644.     writefile.close
  645.   end
  646.  
  647.   #===========================================================
  648.   #**uri_download
  649.   #**Downloads file using open-uri algorithm. Clear Ruby only.
  650.   #**Pobiera plik używając algorytmu open-uri. Tylko czyste ruby.
  651.   #**Narzew
  652.   #===========================================================
  653.  
  654.   def uri_download(x, nam)
  655.     require 'open-uri'
  656.     open(x) {|f|
  657.     file = File.open(nam, 'wb')
  658.     file.write(f.read)
  659.     file.close
  660.     }
  661.   end
  662.  
  663.   #===========================================================
  664.   #**location_table
  665.   #**Gets location file list from location array
  666.   #**Zwraca wszystkie nazwy plików z lokacji określonych w tablicy
  667.   #**Narzew
  668.   #===========================================================
  669.  
  670.   def location_table(locations_ary)
  671.     result = []
  672.     locations_ary.each{|location|
  673.     Dir.foreach(location){|x|
  674.     if x != '.'
  675.       if x != '..'
  676.         result << "#{location}/#{x}"
  677.       end
  678.     end
  679.     }
  680.     }
  681.     return result
  682.   end
  683.  
  684.   #===========================================================
  685.   #**xorify_crypt
  686.   #**Encrypts script using xorify algorithm. Only TEXT are supported.
  687.   #**Koduje skrypt używając algorytmu xorify. Tylko TEXT jest obsługiwany.
  688.   #**Narzew
  689.   #===========================================================
  690.  
  691.   def xorify_crypt(source, destination, key=0x0AEEF6)
  692.     file = File.open(source, "rb")
  693.     data = file.read
  694.     file.close
  695.     s = []
  696.     xorval = key
  697.     data.each_byte{|byte|
  698.     r = byte.to_i ^ xorval
  699.     s << r
  700.     xorval = xorval * 2 + 113 & 0xFFFFFFFF
  701.     }
  702.     $data = s
  703.     file = File.open(destination, 'wb')
  704.     Marshal.dump($data, file)
  705.     file.close
  706.   end
  707.  
  708.   #===========================================================
  709.   #**xorify_eval
  710.   #**Evals a xorify encrypted script
  711.   #**Wykonuje skrypt zakodowany algorytmem xorify
  712.   #**Narzew
  713.   #===========================================================
  714.  
  715.   def xorify_eval(packed, key=0x0AEEF6, raiseonfailure=0)
  716.     file = File.open(packed, 'rb')
  717.     $data = Marshal.load(file)
  718.     s = []
  719.     xorval = key
  720.     $data.each{|x|
  721.     r = x ^ xorval
  722.     s << r
  723.     xorval = xorval * 2 + 113 & 0xFFFFFFFF
  724.     }
  725.     a = []
  726.     s.each{|x|
  727.     a << $nrgss.unpack_byte_clear(x)
  728.     }
  729.     script = a.to_s
  730.     begin
  731.       eval(script)
  732.     rescue
  733.       raise("Failed to load script") if raiseonfailure == 1
  734.       print("Failed to load script") if raiseonfailure == 0
  735.     end
  736.   end
  737.  
  738.   #===========================================================
  739.   #**mci_eval
  740.   #**Gets a command from MCI DLL
  741.   #**Wykonuje komendę na bibliotece MCI
  742.   #**ForeverZer0
  743.   #===========================================================
  744.  
  745.   def mci_eval(command)
  746.     data = "\0" * 256
  747.     $mcisendstring.call(command, data, 256, 0)
  748.     return data.delete("\0")
  749.   end
  750.  
  751.   #===========================================================
  752.   #**open_cd_drive
  753.   #**Opens CD drive
  754.   #**Otwiera napęd CD
  755.   #**ForeverZer0
  756.   #===========================================================
  757.  
  758.   def open_cd_drive
  759.     $nrgss.mci_eval('set CDAudio door open')
  760.   end
  761.  
  762.   #===========================================================
  763.   #**close_cd_drive
  764.   #**Closes CD drive
  765.   #**Zamyka napęd CD
  766.   #**ForeverZer0
  767.   #===========================================================
  768.  
  769.   def close_cd_drive
  770.     $nrgss.mci_eval('set CDAudio door closed')
  771.   end
  772.  
  773.   #===========================================================
  774.   #**string_int
  775.   #**Converts string to integer
  776.   #**Konwertuje ciąg na liczbę
  777.   #**Narzew
  778.   #===========================================================
  779.  
  780.   def string_int(string)
  781.     return string.to_i(36)
  782.   end
  783.  
  784.   #===========================================================
  785.   #**int_string
  786.   #**Converts int to string
  787.   #**Konwertuje liczbę na ciąg
  788.   #**Narzew
  789.   #===========================================================
  790.  
  791.   def int_string(int)
  792.     return int.to_s(36)
  793.   end
  794.  
  795.   #===========================================================
  796.   #**tea97_hash
  797.   #**Hashs int using TEA97 hashing algorithm. Ruby 1.8 only
  798.   #**Hashuje liczbę używając algorytmu TEA97. Tylko Ruby 1.8
  799.   #**Narzew
  800.   #===========================================================
  801.  
  802.   def tea97_hash(x,y=133,z=413,k=817)
  803.     $k = k.to_i + 113
  804.     $y = y.to_i + 103
  805.     $z = z.to_i + 404
  806.     $result = []
  807.     x = x.crypt(($k * $z + $y).to_s)
  808.     x.each_byte{|b|
  809.     s = b.to_i
  810.     a = s.to_i ^ y.to_i + 4
  811.     b = a.to_i ^ y.to_i + 7
  812.     c = b ^ y.to_i + $k.to_i
  813.     d = c ^ (y.to_i + 2) * $k.to_i
  814.     e = d ^ (z.to_i + 7) * $k.to_i
  815.     f = e ^ (z.to_i + k.to_i) * 3
  816.     g = f ^ (y.to_i + z.to_i + 330) * 3
  817.     h = g ^ $k.to_i
  818.     $result << (h.to_i ^ k.to_i + 7)
  819.     $k = $k.to_i * 2 + 5 & 0xFFFFFF
  820.     }
  821.     $result = $result.to_s.to_i / ($k * 348 + $y + 329429378 + $z * 117 + $k * 1113244)
  822.     $result = $result.to_s
  823.     return $result
  824.   end
  825.  
  826.   #===========================================================
  827.   #**xt_unpack
  828.   #**Depacks xt packed array
  829.   #**Depakowuje tablicę zapakowaną xt
  830.   #**Narzew
  831.   #===========================================================
  832.  
  833.   def xt_unpack(table, key=0)
  834.     $key = key
  835.     $xt = []
  836.     table.each{|x|
  837.     $xt << ((x - $key).to_s(36))
  838.     $key = $key * 2 + 6
  839.     }
  840.     return $xt
  841.   end
  842.  
  843.   #===========================================================
  844.   #**xt_pack
  845.   #**Packs array using xt algorithm
  846.   #**Pakuje tablicę używając algorytmu xt
  847.   #**Narzew
  848.   #===========================================================
  849.  
  850.   def xt_pack(table, key=0)
  851.     $key = key
  852.     $xt = []
  853.     table.each{|x|
  854.     $xt << (x.to_i(36) + $key)
  855.     $key = $key * 2 + 6
  856.     }
  857.     return $xt
  858.   end
  859.  
  860.   #===========================================================
  861.   #**make_rbl
  862.   #**Makes a rbl from data array
  863.   #**Tworzy rbl z tablicy
  864.   #**Narzew
  865.   #===========================================================
  866.  
  867.   def make_rbl(archivedata, file2)
  868.     $result = {}
  869.     $archive = archivedata
  870.     $archive.each{|x,y|
  871.     $result[$nrgss.uu_encode(x)] = Zlib::Deflate.deflate(y)
  872.     }
  873.     file = File.open(file2, 'wb')
  874.     Marshal.dump($result, file)
  875.     file.close
  876.   end
  877.  
  878.   #===========================================================
  879.   #**execute_rbl
  880.   #**Executes a rbl section
  881.   #**Ładuje sekcję rbl
  882.   #**Narzew
  883.   #===========================================================
  884.  
  885.   def execute_rbl(archivesection, archive, args=[])
  886.     $rbl_args = args
  887.     file = File.open(archive, 'rb')
  888.     $result = Marshal.load(file)
  889.     file.close
  890.     $data = {}
  891.     $result.each{|x,y|
  892.     $data[x] = Zlib::Inflate.inflate(y)
  893.     }
  894.     eval($data[$nrgss.uu_decode(x)])
  895.   end
  896.  
  897.   #===========================================================
  898.   #**eval_all_rbl
  899.   #**Executes all rbl sections. Will crash if it's function-caller library.
  900.   #**Ładuje wszystkie sekcje rbl. Zwróci błąd jeśli to function-caller.
  901.   #**Narzew
  902.   #===========================================================
  903.  
  904.   def eval_all_rbl(rbl)
  905.     file = File.open(rbl, 'rb')
  906.     $result = Marshal.load(file)
  907.     file.close
  908.     $data = {}
  909.     $result.each{|x,y|
  910.     $data[x] = Zlib::Inflate.inflate(y)
  911.     }
  912.     $data.each{|x,y|
  913.     eval(y)
  914.     }
  915.   end
  916.  
  917.   #===========================================================
  918.   #**encrypt_int2
  919.   #**Encrypts an int (Method 2)
  920.   #**Koduje liczbę (Metoda 2)
  921.   #**Narzew
  922.   #===========================================================
  923.  
  924.   def encrypt_int2(int, key)
  925.     $int = int * 17 + 113
  926.     $key = (key + (114 * 19 - 724) * key)
  927.     srand(key) rescue srand(9200)
  928.     $r = $int
  929.     rand(2000).times{
  930.     srand($key + 3)
  931.     $r = $r ^ ($key + 7)
  932.     $r = $r + rand(200 + $key)
  933.     $key = $key + rand(3999)
  934.     }
  935.     return $r
  936.   end
  937.  
  938.   #===========================================================
  939.   #**fgetb
  940.   #**???
  941.   #**???
  942.   #**Peter O.
  943.   #===========================================================
  944.  
  945.   def fgetb
  946.     x=0
  947.     ret=0
  948.     each_byte do |i|
  949.       ret=i || 0
  950.       break
  951.     end
  952.     return ret
  953.   end
  954.  
  955.   #===========================================================
  956.   #**fgetw
  957.   #**???
  958.   #**???
  959.   #**Peter O.
  960.   #===========================================================
  961.  
  962.   def fgetw
  963.     x=0
  964.     ret=0
  965.     each_byte do |i|
  966.       break if !i
  967.       ret|=(i<<x)
  968.       x+=8
  969.       break if x==16
  970.     end
  971.     return ret
  972.   end
  973.  
  974.   #===========================================================
  975.   #**fgetdw
  976.   #**???
  977.   #**???
  978.   #**Peter O.
  979.   #===========================================================
  980.  
  981.   def fgetdw
  982.     x=0
  983.     ret=0
  984.     each_byte do |i|
  985.       break if !i
  986.       ret|=(i<<x)
  987.       x+=8
  988.       break if x==32
  989.     end
  990.     return ret
  991.   end
  992.  
  993.   #===========================================================
  994.   #**fgetsb
  995.   #**???
  996.   #**???
  997.   #**Peter O.
  998.   #===========================================================
  999.  
  1000.   def fgetsb
  1001.     ret=fgetb
  1002.     if (ret&0x80)!=0
  1003.       return ret-256
  1004.     else
  1005.       return ret
  1006.     end
  1007.   end
  1008.  
  1009.   #===========================================================
  1010.   #**xfgetb
  1011.   #**???
  1012.   #**???
  1013.   #**Peter O.
  1014.   #===========================================================
  1015.  
  1016.   def xfgetb(offset)
  1017.     self.pos=offset
  1018.     return fgetb
  1019.   end
  1020.  
  1021.   #===========================================================
  1022.   #**xfgetw
  1023.   #**???
  1024.   #**???
  1025.   #**Peter O.
  1026.   #===========================================================
  1027.  
  1028.   def xfgetw(offset)
  1029.     self.pos=offset
  1030.     return fgetw
  1031.   end
  1032.  
  1033.   #===========================================================
  1034.   #**xfgetdw
  1035.   #**???
  1036.   #**???
  1037.   #**Peter O.
  1038.   #===========================================================
  1039.  
  1040.   def xfgetdw(offset)
  1041.     self.pos=offset
  1042.     return fgetdw
  1043.   end
  1044.  
  1045.   #===========================================================
  1046.   #**getoffset
  1047.   #**???
  1048.   #**???
  1049.   #**Peter O.
  1050.   #===========================================================
  1051.  
  1052.   def getoffset(index)
  1053.     self.nil
  1054.     self.pos=0
  1055.     offset=fgetdw>>3
  1056.     return 0 if index>=offset
  1057.     self.pos=index*8
  1058.     return fgetdw
  1059.   end
  1060.  
  1061.   #===========================================================
  1062.   #**getlength
  1063.   #**???
  1064.   #**???
  1065.   #**PeterO.
  1066.   #===========================================================
  1067.  
  1068.   def getlength(index)
  1069.     self.nil
  1070.     self.pos=0
  1071.     offset=fgetdw>>3
  1072.     return 0 if index>=offset
  1073.     self.pos=index*8+4
  1074.     return fgetdw
  1075.   end
  1076.  
  1077.   #===========================================================
  1078.   #**readname
  1079.   #**???
  1080.   #**???
  1081.   #**Peter O.
  1082.   #===========================================================
  1083.  
  1084.   def readname(index)
  1085.     self.nil
  1086.     self.pos=0
  1087.     offset=fgetdw>>3
  1088.     return "" if index>=offset
  1089.     self.pos=index<<3
  1090.     offset=fgetdw
  1091.     length=fgetdw
  1092.     return "" if length==0
  1093.     self.pos=offset
  1094.     return read(length)
  1095.   end
  1096.  
  1097.   #===========================================================
  1098.   #**fputb
  1099.   #**???
  1100.   #**???
  1101.   #**Peter O.
  1102.   #===========================================================
  1103.  
  1104.   def fputb(b)
  1105.     b=b&0xFF
  1106.     write(b.chr)
  1107.   end
  1108.  
  1109.   #===========================================================
  1110.   #**fputw
  1111.   #**???
  1112.   #**???
  1113.   #**Peter O.
  1114.   #===========================================================
  1115.  
  1116.   def fputw(w)
  1117.     2.times do
  1118.       b=w&0xFF
  1119.       write(b.chr)
  1120.       w>>=8
  1121.     end
  1122.   end
  1123.  
  1124.   #===========================================================
  1125.   #**fputdw
  1126.   #**???
  1127.   #**???
  1128.   #**Peter O.
  1129.   #===========================================================
  1130.  
  1131.   def fputdw(w)
  1132.     4.times do
  1133.       b=w&0xFF
  1134.       write(b.chr)
  1135.       w>>=8
  1136.     end
  1137.   end
  1138.  
  1139.   #===========================================================
  1140.   #**pos=
  1141.   #**???
  1142.   #**???
  1143.   #**Peter O.
  1144.   #===========================================================
  1145.  
  1146.   def pos=(value)
  1147.     seek(value)
  1148.   end
  1149.  
  1150.   #===========================================================
  1151.   #**swap32
  1152.   #**Swaps bytes
  1153.   #**Zamienia bity
  1154.   #**Peter O.
  1155.   #===========================================================
  1156.  
  1157.   def swap32(x)
  1158.     return ((x>>24)&0x000000FF)|((x>>8)&0x0000FF00)|((x<<8)&0x00FF0000)|((x<<24)&0xFF000000)
  1159.   end
  1160.  
  1161.   #===========================================================
  1162.   #**gcd
  1163.   #**The greatest common divisor
  1164.   #**Największy wspólny dzielnik
  1165.   #**KGC
  1166.   #===========================================================
  1167.  
  1168.   def gcd(x)
  1169.     ary = x.find_all { |i| i.is_a?(Integer) && i != 0 }
  1170.     ary.sort! { |a, b| b - a }
  1171.     return 0 if ary.size < 2
  1172.     g = ary[0].abs
  1173.     (1...ary.size).each { |i|
  1174.       n = ary[i].abs
  1175.       g = gcd_r(g, n)
  1176.     }
  1177.     return g
  1178.   end
  1179.  
  1180.   #===========================================================
  1181.   #**gcd_r
  1182.   #**???
  1183.   #**???
  1184.   #**Required function.
  1185.   #**KGC
  1186.   #===========================================================
  1187.  
  1188.   def gcd_r(a, b)
  1189.     while b != 0
  1190.       c = a
  1191.       a = b
  1192.       b = c % b
  1193.     end
  1194.     return a
  1195.   end
  1196.  
  1197.   #===========================================================
  1198.   #**lcm
  1199.   #**The smallest common multiple
  1200.   #**Najmniejsza wspólna wielokrotność
  1201.   #**KGC
  1202.   #===========================================================
  1203.  
  1204.   def lcm(x)
  1205.     ary = x.find_all { |i| i.is_a?(Integer) && i != 0 }
  1206.     return 0 if ary.size < 2
  1207.     l = ary[0].abs
  1208.     (1...ary.size).each { |i|
  1209.       n = ary[i].abs
  1210.       l = l * n / [l, n].gcd
  1211.     }
  1212.     return l
  1213.   end
  1214.  
  1215.   #===========================================================
  1216.   #**average
  1217.   #**The average number from array
  1218.   #**Średnia z numerów tablicy
  1219.   #**KGC
  1220.   #===========================================================
  1221.  
  1222.   def average(value)
  1223.     n = 0.0
  1224.     value.each {|i| n += i}
  1225.     return n / value.size
  1226.   end
  1227.  
  1228.   #===========================================================
  1229.   #**devsq
  1230.   #**Sum of squared deviations
  1231.   #**Suma kwadratów odchyleń
  1232.   #**KGC
  1233.   #===========================================================
  1234.  
  1235.   def devsq(value)
  1236.     n, v = 0.0, average(value)
  1237.     value.each {|i| n += (i - v) ** 2}
  1238.     return n
  1239.   end
  1240.  
  1241.   #===========================================================
  1242.   #**gmt
  1243.   #**Geometric mean (synergistic)
  1244.   #**Średnia geometryczna (synergetsyczna)
  1245.   #**KGC
  1246.   #===========================================================
  1247.  
  1248.   def gmt(value)
  1249.     n = 1.0
  1250.     value.each {|i| n *= i}
  1251.     return n ** (1.0 / value.size)
  1252.   end
  1253.  
  1254.   #===========================================================
  1255.   #**stdevp
  1256.   #**Standard deviation
  1257.   #**Odchylenie standardowe
  1258.   #**KGC
  1259.   #===========================================================
  1260.  
  1261.   def stdevp(value)
  1262.     return sqrt(var(value))
  1263.   end
  1264.  
  1265.   #===========================================================
  1266.   #**var
  1267.   #**Unbiased variance
  1268.   #**Bezstronna wariancja
  1269.   #**KGC
  1270.   #===========================================================
  1271.  
  1272.   def var(value)
  1273.     return 0.0 if value.size < 2
  1274.     return devsq(value) / (value.size - 1)
  1275.   end
  1276.  
  1277.   #===========================================================
  1278.   #**Ends of NRGSS class
  1279.   #**Koniec klasy NRGSS
  1280.   #===========================================================
  1281.  
  1282. end
  1283.  
  1284. #===========================================================
  1285. #**NRGSS Class Definition
  1286. #**Definicja klas NRGSS
  1287. #===========================================================
  1288.  
  1289. $nrgss = NRGSS.new
  1290.  
  1291. #===========================================================
  1292. #**End of Library
  1293. #**Koniec biblioteki
  1294. #===========================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement