Advertisement
Narzew

NRGSS 2.5

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