Advertisement
Narzew

NRGSS FUNCTION LIBRARY 2.5

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