Holy87

Universal Module 1.7 - ITA

Nov 19th, 2016
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 63.04 KB | None | 0 0
  1. $imported = {} if $imported == nil
  2. $imported['H87_UniversalModule'] = 1.7
  3. =begin
  4.  ==============================================================================
  5.   ■ Utility universali di Holy87
  6.       versione 1.7.1
  7.       Difficoltà utente: ★★★
  8.       Licenza: CC-BY. Chiunque può scaricare, modificare, distribuire e utilizzare
  9.       lo script nei propri progetti, sia amatoriali che commerciali. Vietata
  10.       l'attribuzione impropria.
  11.  
  12.   ■ Questo modulo serve a espandere a nuovi livelli i vostri script e permetter-
  13.     vi di creare feature nel vostro gioco prima impensabili, nel modo più
  14.     semplice e immediato possibile. Cosa è possibile fare?
  15.     ● Salvare delle variabili indipendenti dal salvataggio
  16.     ● Ottenere informazioni dal sistema, quali la risoluzione dello schermo,
  17.       la lingua del sistema, la versione di Windows, il nome utente, il
  18.       percorso della cartella documenti ecc...
  19.     ● Scaricare un file e/o inviare una richiesta ed ottenere risposta da un web
  20.       server, anche in modo asincrono e nel modo più facile possibile
  21.     ● Ottenere informazioni sulla versione del gioco o impostarla
  22.     ● Codificare/decodificare una stringa in rot13 o Base64
  23.     ● Altro ancora!
  24.  ==============================================================================
  25.   ■ Compatibilità
  26.     DataManager -> alias load_normal_database, load_battle_test_database
  27.     Scene_Base  -> alias update
  28.     Window_Base -> alias update
  29.  ==============================================================================
  30.   ■ Installazione
  31.     Installare questo script sotto Materials e prima del Main. Metterlo sopra a
  32.     tutti gli altri script che fanno uso di questo modulo.
  33.  ==============================================================================
  34.   ■ Istruzioni
  35.     Usare da script o da chiama evento le seguenti chiamate: quando vedi delle
  36.     parentesi quadre [] a dei valori significa che è facoltativo.
  37.  
  38.   ▼ Valori universali di gioco
  39.     L'oggetto $game_settings è una classe universale che salva e conserva un
  40.     valore indipendentemente dal salvataggio. Può essere usato per memorizzare
  41.     impostazioni nella schermata del titolo, sbloccare gli extra o gestire gli
  42.     obiettivi, o qualsiasi cosa ti venga in mente.
  43.  
  44.     ● $game_settings[quellochevuoi] = ilvalorechevuoi
  45.       memorizza il valore che vuoi, e viene automaticamente salvato nel file
  46.       game_settings.rvdata, che si trova nella cartella del gioco. Il valore
  47.       viene salvato anche se la partita non è stata salvata.
  48.  
  49.     ● $game_settings[quellochevuoi]
  50.       restituisce il valore di quellochevuoi.
  51.  
  52.     ● Puoi impostare la versione del gioco creando un file nel progetto con il
  53.       nome "version.ini", e all'interno scriverci la versione come 1.0.2.5
  54.       Puoi richiamare la versione dal gioco da $game_system.game_version
  55.       Es. versione = $game_system.game_version
  56.       print versione.major => 1
  57.       print versione.minor => 0
  58.       print versione.build => 2
  59.       print versione.revision => 5
  60.       print versione => 1.0.2.5
  61.       print versione > "1.0.1.7" => false
  62.  
  63.   ▼ Chiamate di sistema
  64.  
  65.     ● Win.version:
  66.       restituisce un decimale con il numero di versione di Windows in uso.
  67.         5.0 -> Windows 2000
  68.         5.1 -> Windows Xp
  69.         5.2 -> Windows Xp (64 bit)
  70.         6.0 -> Windows Vista
  71.         6.1 -> Windows 7
  72.         6.2 -> Windows 8
  73.         6.3 -> Windows 8.1
  74.         10.0-> Windows 10
  75.  
  76.     ● Win.username
  77.       Restituisce una stringa con nome utente di Windows attualmente in uso
  78.  
  79.     ● Win.homepath
  80.       Restituisce il percorso utente del computer. Esempio:
  81.       C:/Users/nomeutente/                      in Windows Vista, 7 e 8
  82.       C:/Documents and Settings/nomeutente      in Windows 2000 e Xp
  83.  
  84.     ● Win.get_folder_path([simbolo])
  85.       Restituisce il percorso di una cartella definita dal simbolo:
  86.         :docs -> cartella documenti dell'utente
  87.         :imgs -> cartella immagini dell'utente
  88.         :dskt -> destkop dell'utente
  89.         :musc -> cartella musica dell'utente
  90.         :vdeo -> cartella video dell'utente
  91.         :prog -> cartella dei programmi installati (C:\Programmi (x86))
  92.         * se non è definito un simbolo, viene preso :docs
  93.  
  94.     ● Win.shutdown[(mode)]
  95.       Spegne il computer, a seconda di mode (se si omette è 0):
  96.         0: spegnimento normale
  97.         1: riavvio
  98.         2: ibernazione
  99.  
  100.     ● Win.open(filepath)
  101.       Apre una cartella o un file sul PC. Specificare il percorso in filepath
  102.       Esempi:
  103.         Win.open(C:/Windows/system32/calc.exe) avvierà la calcolatrice
  104.         Win.open(Win.homepath+"/Desktop") aprirà la cartella del desktop
  105.  
  106.     ● Win.temp_flush(nomefile)
  107.       elimina il file dai file temporanei di internet. Utilizzarlo prima di un
  108.       download di un file che viene aggiornato molto spesso.
  109.  
  110.     ● Win.datenow[(partition)]
  111.       Restituisce la data attuale in stringa in formato "gg/mm/aaaa". Se
  112.       viene inserito partition come valore:
  113.       0: restituisce il numero del giorno in intero
  114.       1: restituisce il numero del mese attuale in intero
  115.       2: restituisce il numero dell'anno attuale in intero
  116.  
  117.     ● Win.timenow[(partition)]
  118.       Restituisce l'ora attuale in formato "hh:mm" (h 0~24)
  119.       partition: 0 -> restituisce le ore in intero
  120.       partition: 1 -> restituisce il minuto attuale 0~59
  121.       partition: 2 -> restituisce i secondi 0~59
  122.  
  123.     ● Win.language
  124.       Restituisce un codice con la lingua del sistema in uso. Per l'elenco delle
  125.       lingue con i rispettivi codici vedere questo link:
  126.       http://support.microsoft.com/kb/193080
  127.  
  128.     ● Win.screen_resolution
  129.       restituisce un array di due interi contenenti la risoluzione in lunghezza
  130.       e altezza dello schermo (es. [1024,730]). Ricordati che non restituisce
  131.       la risoluzione completa, ma solo la parte di schermo utilizzabile (cioè
  132.       quella non nascosta dalla barra delle applicazioni)
  133.  
  134.     ● Screen.resize(lunghezza, larghezza)
  135.       ridimensiona la finestra di gioco. Ricordati che questa funzione non
  136.       aumenta la risoluzione del gioco, ma stretcha limmagine.
  137.  
  138.   ▼ Appunti
  139.     ● Clipboard.read_text
  140.       Restituisce una stringa di testo copiato dagli appunti (ad esempio,
  141.       quando si fa copia da un testo selezionato)
  142.  
  143.   ▼ Funzioni per le stringhe
  144.  
  145.     ● String.random([n_caratteri])
  146.       Restituisce una stringa casuale di n_caratteri. Se n_caratteri non è
  147.       definito, la stringa sarà di 4.
  148.       Esempi:
  149.       print String.random     #=> ajpf
  150.       print String.random(7)  #=> opetnpg
  151.  
  152.     ● crypt_rot13
  153.       Cripta una stringa in rot13 in modo da essere illeggibile (sposta ogni
  154.       carattere di 13 posti nell'alfabeto). Richiamare il metodo alla stringa
  155.       crittografata per farla tornare normale. Esempi:
  156.       print "Casa".crypt_rot13    #=> "Pnfn"
  157.       print "Pnfn".crypt_rot13    #=> "Casa"
  158.  
  159.     ● Base64.encode(stringa) e Base64.decode(stringa)
  160.       Restituisce una stringa in Base64 per l'interscambio di dati web
  161.       Per ulteriori informazioni: http://it.wikipedia.org/wiki/Base64
  162.  
  163.   ▼ Internet e HTTP
  164.  
  165.     ----------------------------------------------------------------------------
  166.     * Metodi semplificati per operazioni asincrone
  167.     ----------------------------------------------------------------------------
  168.     I metodi semplificati permettono una facile gestione dei download e delle
  169.     risposte su internet in modo da usare meno codice possibile ed essere più
  170.     efficienti. Questi metodi possono essere usati solo in una Scene, Window o
  171.     nelle classi dove viene incluso il modulo Async_Downloads.
  172.  
  173.     ● send_post_request(url, parametri)
  174.       Invia una richiesta POST all'url indicato inviando i parametri e ne
  175.       restituisce la risposta sottoforma di stringa.
  176.       Esempio:
  177.       parametri = {"user" => "Francesco", "password" => "banana"}
  178.       print send_post_request(www.miosito.com/login.php, parametri)
  179.  
  180.     ● download_async(url, metodo[, priorità, cartella])
  181.       Avvia il download di un file da url. Lancia automaticamente il metodo
  182.       nome_metodo quando il download è completato
  183.       (il nome del metodo dev'essere passato come simbolo)
  184.       Esempi:
  185.       download_async("www.miosito.com/immagine.jpg", method(:carica_immagine))
  186.       (bisogna anche definire il metodo carica_immagine)
  187.       Il download del file è molto lento per non alterare la fluidità del gioco.
  188.       Se vuoi velocizzare il download, metti true nel campo priorità.
  189.  
  190.     ● get_response_async(url, metodo, priorità)
  191.       Avvia la ricezione della risposta dall'indirizzo dell'url, ed avvia il
  192.       metodo definito da method_name nonappena la risposta è stata ricevuta.
  193.       A differenza del precedente, il metodo da richiamare deve avere un
  194.       parametro in ingresso, che è la stringa della risposta.
  195.       All'interno dell'URL si possono anche inserire i parametri per una
  196.       richiesta di tipo GET (ad esempio, login.php?user=francesco&password=banana)
  197.       Purtroppo RPG Maker non gestisce correttamente il multithreading, quindi
  198.       ci sarà sempre un blocco del gioco mentre invia la risposta, che dipende
  199.       dalle prestazioni del server.
  200.  
  201.     ● abort_download(nomefile)
  202.       Annulla il download di nomefile.
  203.  
  204.     ● download_status(filename)
  205.       Restituisce un numero intero da 0 a 100 rappresentante lo stato di download.
  206.  
  207.     ----------------------------------------------------------------------------
  208.     * Immagine del giorno di Bing
  209.     ----------------------------------------------------------------------------
  210.     Bing propone ogni giorno una nuova foto dal mondo. Puoi ottenere quest'im-
  211.     magine dal modulo Cache:
  212.  
  213.     ● Cache.bing_daily
  214.       ti restituirà una Bitmap pronta per l'utilizzo (restituisce nil se ci sono
  215.       problemi di connessione)
  216.       La risoluzione predefinita è 640x480, ma puoi ottenerne altre impostando
  217.       la risoluzione nel secondo parametro:
  218.       Cache.bing_daily('1920x1080')
  219.       Non tutti i formati sono disponibili. Alcune delle risoluzioni sono:
  220.         QVGA (320x240, 4:3)
  221.         VGA (640x480, 4:3)
  222.         SVGA (800x600, 4:3)
  223.         XGA (1024x768, 4:3)
  224.         WXGA (1280x720 o 1280x800, 16:9)
  225.         HD768 (1366x768, 16:9)
  226.         FULLHD (1920x1080,16:9)
  227.         WUXGA (1920x1200, 16:10)
  228.       Puoi anche selezionare la risoluzione da Resolution. esempio:
  229.         Cache.bing_daily(Resolution::SVGA)
  230.         Cache.bing_daily(Resolution::WXGA)
  231.  
  232.     ● Cache.bing_daily_copyright
  233.       Puoi ottenere l'origine dell'immagine (soggetto, autore e copyright)
  234.       Attenzione: da usare dopo aver preso l'immagine!
  235.  
  236.     ----------------------------------------------------------------------------
  237.     * Metodi più complessi per la gestione completa dei download
  238.     ----------------------------------------------------------------------------
  239.     Questi metodi possono essere usati ovunque e permettono una gestione più
  240.     complessa e funzionale dei download. Possono anche essere usati affiancati
  241.     ai metodi più semplici.
  242.  
  243.     ● await_response(url)
  244.       Avvia la ricezione della risposta e restituisce direttamente la stringa
  245.       senza attesa. Può bloccare l'esecuzione del gioco per qualche secondo,
  246.       perciò meglio metterlo durante caricamenti o cambi di schermata.
  247.       Esempio:
  248.       print await_response(www.miosito.com)
  249.       Una cosa del genere stamperà tutto il codice HTML della pagina.
  250.  
  251.     ● HTTP.domain
  252.       restituisce il dominio principale del tuo server (configura in basso).
  253.       usalo nelle locazioni internet in modo da cambiare velocemente dominio
  254.  
  255.     ● HTTP.download(percorsofile[,destinazione[,bassa priorità]])
  256.       Scarica il file dal percorso. Se il percorso di destinazione è omesso,
  257.       verrà scaricato nella cartella del gioco (cioè "./") Esempio:
  258.       HTTP.download("http://www.miosito.it/immagine.png","./Graphics/Pictures")
  259.         scaricherà immagine.png nella cartella Graphics/Pictures del gioco.
  260.       HTTP.download("http://www.miosito.it/immagine.png")
  261.         scaricherà immagine.png nella cartella del gioco.
  262.       HTTP.download(HTTP.domain+"/immagine.png", Win.getFolderPath(:dskt))
  263.         scaricherà immagine.png da http://www.miosito.it sul desktop.
  264.       Imposta true in bassa priorità per velocizzare il gioco durante il download
  265.       (il download sarà più lento)
  266.  
  267.     ● HTTP.get_server_response(url, response_name)
  268.       Ottiene la risposta o legge il contenuto di un indirizzo web e lo memorizza
  269.       nel proprio contenitore. url è l'indirizzo, mentre response_name è un
  270.       identificatore per ritrovarlo.
  271.  
  272.     ● HTTP.await_response(url)
  273.       come await_response senza HTTP
  274.  
  275.     ● HTTP.response(response_name)
  276.       Restituisce il testo letto da get_server_response. Usalo dopo aver
  277.       prima controllato che il download sia stato effettuato
  278.  
  279.     ● HTTP.downloaded?(nomefile)
  280.       Restituisce true se il file che si sta scaricando è stato scaricato
  281.       completamente. Restituisce false se non è stato neanche iniziato o se è
  282.       ancora in corso. Al posto di nomefile si può mettere il nome della risposta
  283.       per controllare se è stata ricevuta quando si usa get_server_response e
  284.       prima di utilizzare HTTP.response.
  285.  
  286.     ● HTTP.progress(nomefile)
  287.       Restituisce la percentuale di scaricamento del file in un float compreso
  288.       tra 0.0 e 100.0. Restituisce 0 se il download non è cominciato.
  289.  
  290.     ● HTTP.filesize(nomefile)
  291.       restituisce la grandezza del file in bit: dividi per 8 per ottenere i byte.
  292.  
  293.     ● HTTP.sizeloaded(nomefile)
  294.       restituisce il numero di bit scaricati del file: dividi per 8 per ottenere
  295.       i byte.
  296.  
  297.     ● HTTP.time_passed(nomefile)
  298.       restituisce il tempo in secondi del tempo che ha richiesto il download.
  299.       Restituisce 0 se il download non è completato o non esiste.
  300.  
  301.     ● HTTP.downloads
  302.       restituisce un hash contenente l'elenco dei download
  303.  
  304.     ● HTTP.completed
  305.       restituisce il numero dei download completati
  306.  
  307.     ● HTTP.get_file_size(url)
  308.       restituisce la grandezza del file senza scaricarlo
  309.  
  310.     ● HTTP.file_good?(nomefile)
  311.       controlla se il file è stato scaricato correttamente
  312.  
  313.     ● Browser.open(url)
  314.       apre il browser ad un sito scelto. Ricordati che se l'url è troppo grande,
  315.       potrebbe non avviare il sito. In questo caso ti consiglio di usare tinyurl
  316.       o goo.gl per rimpicciolire il link.
  317. =end
  318.  
  319. #==============================================================================
  320. module H87_ModConfig
  321.   HTTPDOMAIN = "http://holyres.altervista.org/demoscript"#"http://miosito.com/"
  322.   SETTINGNAME = "game_settings.rvdata2"
  323.   VERSIONFILE = "version.ini"
  324. end
  325. #==============================================================================
  326. # ** Win
  327. #------------------------------------------------------------------------------
  328. #  Questo modulo gestisce le chiamate di sistema e recupera informazioni sul
  329. #  computer
  330. #==============================================================================
  331. module Win
  332.   # Win32APIs
  333.   GetUserName = Win32API.new('advapi32','GetUserName','PP','I')
  334.   GetEnvironmentVariable = Win32API.new('kernel32', 'GetEnvironmentVariable', 'PPL', 'L')
  335.   SHGetFolderPath = Win32API.new('shell32', 'SHGetFolderPath', 'LLLLP', 'L')
  336.   GetSystemMetrics = Win32API.new("user32", "GetSystemMetrics", 'I','I')
  337.   GetVersionEx = Win32API.new( 'kernel32', 'GetVersionEx', 'P', 'I' )
  338.   #-----------------------------------------------------------------------------
  339.   # * Restituisce il nome utente di Windows
  340.   # @return [String]
  341.   #-----------------------------------------------------------------------------
  342.   def self.username
  343.     name = ' ' * 128
  344.     size = '128'
  345.     GetUserName.call(name,size)
  346.     username = name.unpack('A*')
  347.     return username[0]
  348.   end
  349.   # --------------------------------------------------------------------------
  350.   # * Restituisce la cartella utente di Windows
  351.   # @return [String]
  352.   # --------------------------------------------------------------------------
  353.   def self.homepath
  354.     name = ' ' * 128
  355.     size = '128'
  356.     username = "\0" * 256 #userprofile
  357.     GetEnvironmentVariable.call('userprofile', username, 256)
  358.     username.delete!("\0")
  359.     return username.gsub('\\','/')
  360.   end
  361.   # --------------------------------------------------------------------------
  362.   # * Restituisce il percorso di una cartella del computer
  363.   # @param [Symbol] symbol
  364.   # @return [String]
  365.   # --------------------------------------------------------------------------
  366.   def self.get_folder_path(symbol = :docs)
  367.     case symbol
  368.       when :user; index = 40
  369.       when :docs; index = 5
  370.       when :imgs; index = 39
  371.       when :musc; index = 13
  372.       when :vdeo; index = 14
  373.       when :strp; index = 2
  374.       when :prog; index = 38
  375.       when :appd; index = 28
  376.       else; index = 0
  377.     end
  378.     path = "\0" * 128
  379.     SHGetFolderPath.call(0, index, 0, 2, path)
  380.     return path.delete("\0").gsub("\\","/")
  381.   end
  382.   # --------------------------------------------------------------------------
  383.   # * Deprecated, left for compatibility
  384.   # @param [Symbol] symbol
  385.   # @return [String]
  386.   # @deprecated
  387.   # --------------------------------------------------------------------------
  388.   def self.getFolderPath(symbol = :docs)
  389.     get_folder_path(symbol)
  390.   end
  391.   # --------------------------------------------------------------------------
  392.   # * Restituisce la larghezza della cornice della finestra
  393.   # @return [String]
  394.   # --------------------------------------------------------------------------
  395.   def self.window_frame_width
  396.     return GetSystemMetrics.call(7)
  397.   end
  398.   # --------------------------------------------------------------------------
  399.   # * Restituisce l'altezza della barra del titolo
  400.   # @return [String]
  401.   # --------------------------------------------------------------------------
  402.   def self.window_title_height
  403.     return GetSystemMetrics.call(4)
  404.   end
  405.   #-----------------------------------------------------------------------------
  406.   # * Elimina il file temporaneo per aggiornarlo prima di un download.
  407.   #   inserire il nome del file.
  408.   # @param [String] filename
  409.   #-----------------------------------------------------------------------------
  410.   def self.temp_flush(filename)
  411.     if version < 6
  412.       path = homepath+"/Impostazioni locali/Temporary Internet Files"
  413.       unless File.directory?(path)
  414.         path = homepath+"/Local Settings/Temporary Internet Files"
  415.         return unless File.directory?(path)
  416.       end
  417.       fetch_folder_for_delete(path,filename)
  418.     else
  419.       path = homepath+"/AppData/Local/Microsoft/Windows/Temporary Internet Files/Content.IE5"
  420.       unless File.directory?(path)
  421.         path = cartella_utente_win+"/AppData/Local/Microsoft/Windows/INetCache/IE"
  422.       end
  423.       return unless File.directory?(path)
  424.       Dir.foreach(path) {|x|                        #per ogni file nel percorso
  425.         next if x == "." or x == ".."               #passa al prossimo se è ind.
  426.         if File.directory?(path+"/"+x)              #se il file è una cartella
  427.           folder = path+"/"+x                       #entra nella cartella
  428.           fetch_folder_for_delete(folder,filename)
  429.         end
  430.       }
  431.     end
  432.   end
  433.   # --------------------------------------------------------------------------
  434.   # * Cerca nella cartella il file da cancellare
  435.   #   path: directory
  436.   #   nomefile: file da cancellare
  437.   # --------------------------------------------------------------------------
  438.   def self.fetch_folder_for_delete(path,nomefile)
  439.     Dir.foreach(path) {|y|                  #per ogni file nella cartella
  440.       next if File.directory?(path+"/"+y)   #passa al prossimo se è una c.
  441.       if no_ext(nomefile) == y[0..no_ext(nomefile).size-1]#se l'inizio del nome corrisp.
  442.         begin
  443.           File.delete(path+"/"+y)             #eliminalo
  444.         rescue
  445.           next
  446.         end
  447.       end
  448.     }
  449.   end
  450.   # --------------------------------------------------------------------------
  451.   # * Restituisce la versione di Windows in uso
  452.   # @return [String]
  453.   # --------------------------------------------------------------------------
  454.   def self.version
  455.     s = [20+128, 0, 0, 0, 0, '' ].pack('LLLLLa128')
  456.     GetVersionEx.call( s )
  457.     a = s.unpack( 'LLLLLa128' )
  458.     indice = a[1].to_f;dec = a[2].to_f/10
  459.     return indice + dec
  460.   end
  461.   #-----------------------------------------------------------------------------
  462.   # * Restituisce il nome del file senza estensione.
  463.   # @return [String]
  464.   #-----------------------------------------------------------------------------
  465.   def self.no_ext(nomefile)
  466.     nome = nomefile.split(".")
  467.     return nome[0..nome.size-2]
  468.   end
  469.   #-----------------------------------------------------------------------------
  470.   # * Restituisce un array di larghezza e altezza della parte utilizzabile dello
  471.   #   schermo: non conta lo spazio della barra delle applicazioni.
  472.   #-----------------------------------------------------------------------------
  473.   def self.screen_resolution
  474.     x = GetSystemMetrics.call(16)
  475.     y = GetSystemMetrics.call(17)
  476.     return [x,y]
  477.   end
  478.   #-----------------------------------------------------------------------------
  479.   # * Restituisce un intero come codice della lingua del sistema
  480.   #-----------------------------------------------------------------------------
  481.   def self.language
  482.     return Win32API.new('kernel32', 'GetUserDefaultLCID', [], 'I').call
  483.   end
  484.   #-----------------------------------------------------------------------------
  485.   # * Restituisce la data attuale
  486.   #-----------------------------------------------------------------------------
  487.   def self.datenow(partition = -1)
  488.     date = Time.now
  489.     case partition
  490.       when -1
  491.         return sprintf('%d/%d/%d',date.day,date.month,date.year)
  492.       when 0
  493.         return date.day
  494.       when 1
  495.         return date.month
  496.       when 2
  497.         return date.year
  498.       else
  499.         return -1
  500.     end
  501.   end
  502.   #-----------------------------------------------------------------------------
  503.   # * Restituisce l'ora attuale
  504.   #-----------------------------------------------------------------------------
  505.   def self.timenow(partition = -1)
  506.     date = Time.now
  507.     case partition
  508.       when -1
  509.         return sprintf('%d:%d',date.hour,date.min)
  510.       when 0
  511.         return date.hour
  512.       when 1
  513.         return date.min
  514.       when 2
  515.         return date.sec
  516.       else
  517.         return -1
  518.     end
  519.   end
  520.   #-----------------------------------------------------------------------------
  521.   # * arresta il computer in modalità diverse.
  522.   #-----------------------------------------------------------------------------
  523.   def self.shutdown(mode = 0)
  524.     string = 'system '
  525.     case mode
  526.       when 0
  527.         string += '-s'
  528.       when 1
  529.         string += '-r'
  530.       when 2
  531.         string += '-h'
  532.       else
  533.         return -1
  534.     end
  535.     system(string)
  536.   end
  537.   #-----------------------------------------------------------------------------
  538.   # * Returns a copied text from Windows clipboard
  539.   # @return [String]
  540.   #-----------------------------------------------------------------------------
  541. end #win
  542.  
  543. #==============================================================================
  544. # ** Clipboard
  545. #------------------------------------------------------------------------------
  546. #  This module handles the Windows clibpoard.
  547. #==============================================================================
  548. module Clipboard
  549.   # Clipboard types
  550.   CF_TEXT = 1           #plain text
  551.   CF_BITMAP = 2         #bitmap
  552.   CF_METAFILEPICT = 3   #metafile
  553.   CF_SYLK = 4           #symbolic link
  554.   CF_DIF = 5            #data interchange format
  555.   CF_TIFF = 6           #tiff text format
  556.   CF_OEMTEXT = 7        #OEM text format. Not sure it's useful
  557.   CF_DIB = 8            #Device Independent Bitmap Format
  558.  
  559.   # Windows APIs
  560.   OpenClipboard = Win32API.new('user32', 'OpenClipboard', 'L', 'I')
  561.   GetClipboardData = Win32API.new('user32', 'GetClipboardData', 'I', 'I')
  562.   SetClipboardData = Win32API.new('user32', 'SetClipboardData', 'IP', 'p')
  563.   CloseClipboard = Win32API.new('user32', 'CloseClipboard', 'V', 'I')
  564.   GlobalLock = Win32API.new('kernel32', 'GlobalLock', 'L', 'L')
  565.   GlobalUnlock = Win32API.new('kernel32', 'GlobalUnlock', 'L', '')
  566.   GlobalSize = Win32API.new('kernel32', 'GlobalSize', 'L' , 'L')
  567.   MemCpy = Win32API.new('ntdll', 'memcpy', 'PPL', 'L')
  568.   IsClipboardFormatAvailable = Win32API.new('user32', 'IsClipboardFormatAvailable', 'I', 'I')
  569.   #-----------------------------------------------------------------------------
  570.   # * Returns a copied text from Windows clipboard. Can raise an error.
  571.   # @return [String]
  572.   # @raise [ClipboardDataAccessException]
  573.   #-----------------------------------------------------------------------------
  574.   def self.read_text
  575.     return '' unless open
  576.     begin
  577.       if format_avaiable?(CF_TEXT)
  578.         handle = get_clipboard_data(CF_TEXT)
  579.         buff_size = data_size(handle)
  580.         buffer = ' ' * (buff_size - 1)
  581.         copy_memory(buffer, handle, buff_size)
  582.         data = buffer.gsub('\0', '')
  583.       else
  584.         puts 'No text avaiable in clipboard'
  585.         data = ''
  586.       end
  587.     rescue
  588.       puts 'Read clipboard error!'
  589.       data = ''
  590.     ensure
  591.       CloseClipboard.call
  592.     end
  593.     data
  594.   end
  595.   #-----------------------------------------------------------------------------
  596.   # * Returns a copied text from Windows clipboard without exceptions.
  597.   # @return [String]
  598.   #-----------------------------------------------------------------------------
  599.   def self.read_text_safe; read_text rescue ''; end
  600.   # TODO: Make methods for other format handling
  601.   #-----------------------------------------------------------------------------
  602.   # Opens the clipboard
  603.   # @return [Integer]
  604.   #-----------------------------------------------------------------------------
  605.   def self.open; OpenClipboard.call(0); end
  606.   #-----------------------------------------------------------------------------
  607.   # Closes the clipboard
  608.   #-----------------------------------------------------------------------------
  609.   def self.close; CloseClipboard.call; end
  610.   #-----------------------------------------------------------------------------
  611.   # @param [Integer] format
  612.   # @return [Integer]
  613.   #-----------------------------------------------------------------------------
  614.   def self.format_avaiable?(type); IsClipboardFormatAvailable.call(type); end
  615.   #-----------------------------------------------------------------------------
  616.   # @param [Object] handle
  617.   # @return [Integer]
  618.   def self.data_size(handle); GlobalSize.call(handle); end
  619.   #-----------------------------------------------------------------------------
  620.   # @param [Integer] type
  621.   # @return [Object]
  622.   #-----------------------------------------------------------------------------
  623.   def self.get_clipboard_data(type); GetClipboardData.call(type); end
  624.   #-----------------------------------------------------------------------------
  625.   # @param [String] data
  626.   # @param [Object] handle
  627.   # @param [Integer] size
  628.   # @return [String]
  629.   #-----------------------------------------------------------------------------
  630.   def self.copy_memory(data, handle, size); MemCpy.call(data, handle, size); end
  631. end #clipboard
  632.  
  633. #==============================================================================
  634. # ** Screen
  635. #------------------------------------------------------------------------------
  636. #  Questo modulo gestisce il ridimensionamento della finestra di gioco
  637. #==============================================================================
  638. module Screen
  639.   MoveWindow = Win32API.new("user32","MoveWindow",'LIIIIL','L')
  640.   FindWindowEx = Win32API.new("user32","FindWindowEx",'LLPP','I')
  641.   #-----------------------------------------------------------------------------
  642.   # * ridimensiona la finestra e la centra
  643.   #   width = nuova larghezza
  644.   #   height = nuova altezza
  645.   #-----------------------------------------------------------------------------
  646.   def self.resize(width ,height)
  647.     #API
  648.     this_window = FindWindowEx.call(0,0,"RGSS Player",0)
  649.     res_x = Win.screen_resolution[0] #risoluzione x
  650.     res_y = Win.screen_resolution[1] #risoluzione y
  651.     width += Win.window_frame_width*2
  652.     height += Win.window_frame_width*2 + Win.window_title_height
  653.     new_x = [(res_x-width)/2, 0].max #ottiene la nuova coordinata, ma non
  654.     new_y = [(res_y-height)/2, 0].max#fa passare oltre il bordo
  655.     MoveWindow.call(this_window, new_x, new_y, width, height, 1)
  656.   end
  657. end #screen
  658.  
  659. #==============================================================================
  660. # ** HTTP
  661. #------------------------------------------------------------------------------
  662. #  Questo modulo permette di interfacciarsi ad internet e gestire i download.
  663. #  Ringraziamenti: Berka (il codice è ispirato al suo)
  664. #==============================================================================
  665. module HTTP
  666.   # Connection types
  667.   INTERNET_OPEN_TYPE_PRECONFIG = 0
  668.   INTERNET_OPEN_TYPE_DIRECT = 1
  669.   INTERNET_OPEN_TYPE_PROXY = 3
  670.  
  671.   # Internet service types
  672.   INTERNET_SERVICE_URL = 0
  673.   INTERNET_SERVICE_FTP = 1
  674.   INTERNET_SERVICE_GOPHER = 2
  675.   INTERNET_SERVICE_HTTP = 3
  676.  
  677.   # Internet ports
  678.   INTERNET_INVALID_PORT_NUMBER = 0
  679.   INTERNET_DEFAULT_FTP_PORT = 21
  680.   INTERNET_DEFAULT_GOPHER_PORT = 70
  681.   INTERNET_DEFAULT_HTTP_PORT = 80
  682.   INTERNET_DEFAULT_HTTPS_PORT = 443
  683.   INTERNET_DEFAULT_SOCKS_PORT = 1080
  684.  
  685.   # Internet flags
  686.   INTERNET_FLAG_RELOAD = 2147483648
  687.   INTERNET_FLAG_NO_CACHE_WRITE = 67108864
  688.   INTERNET_FLAG_RAW_DATA = 1073741824
  689.  
  690.   #API Calls
  691.   SetPrClass = Win32API.new('kernel32','SetPriorityClass','pi','i').call(-1,128)
  692.   InternetOpenA = Win32API.new("wininet",'InternetOpenA','plppl','l').call(
  693.       'RMVXA',INTERNET_OPEN_TYPE_PRECONFIG,'','',0)
  694.   InternetConnectA = Win32API.new("wininet",'InternetConnectA','lplpplll','l')
  695.   InternetOpenUrl = Win32API.new("wininet",'InternetOpenUrl','lppllp','l')
  696.   InternetReadFile = Win32API.new("wininet",'InternetReadFile','lpip','l')
  697.   InternetCloseHandle = Win32API.new('wininet','InternetCloseHandle','l','l')
  698.   HttpQueryInfo = Win32API.new('wininet','HttpQueryInfo','LLPPP','I')
  699.   HttpOpenRequest = Win32API.new('wininet','HttpOpenRequestA','pppppplp','l')
  700.   HttpSendRequest = Win32API.new('wininet','HttpSendRequestA','PPLPL','L')
  701.   #--------------------------------------------------------------------------
  702.   # Sends a post-type request.
  703.   # @param [String] url
  704.   # @param [Hash] params
  705.   # @param [Integer] method
  706.   # @return [String]
  707.   #--------------------------------------------------------------------------
  708.   def self.send_post_request(url, params)
  709.     #variable initialization
  710.     info = url_info(url)
  711.     server = info[:server]
  712.     root = info[:root]
  713.     accept = '*/*'
  714.     headers = 'Content-Type: application/x-www-form-urlencoded'
  715.     response = ''
  716.  
  717.     port = INTERNET_DEFAULT_HTTP_PORT
  718.     service_type = INTERNET_SERVICE_HTTP
  719.  
  720.     connection = InternetConnectA.call(InternetOpenA, server, port, '', '', service_type, 0, 1)
  721.     if connection.nil?
  722.       raise InternetConnectionException, 'Error while connecting to the server.'
  723.     end
  724.     request = prepare_request(params)
  725.  
  726.     h_file = HttpOpenRequest.call(connection, 'POST',
  727.                                   root, nil, nil, accept,
  728.                                   INTERNET_FLAG_RELOAD, 1
  729.     )
  730.     request_status = HttpSendRequest.call(h_file, headers, headers.size,
  731.                                           request, request.size
  732.     )
  733.  
  734.     unless request_status
  735.       raise InternetConnectionException, 'Error while sending request.'
  736.     end
  737.     loop do
  738.       buffer = ' '*1024
  739.       n = 0
  740.       r = InternetReadFile.call(h_file,buffer,1024,o=[n].pack('i!'))
  741.       n = o.unpack('i!')[0]
  742.       response << buffer[0,n]
  743.       if r&&n==0
  744.         break
  745.       end
  746.     end
  747.     response
  748.   end
  749.   #--------------------------------------------------------------------------
  750.   # * Gets the url info
  751.   # @param [String] uri
  752.   # @return [Hash]
  753.   #--------------------------------------------------------------------------
  754.   def self.url_info(uri)
  755.     address = uri.split('/')
  756.     {
  757.         server:   address[2],
  758.         root:     address[3..address.size].join('/'),
  759.         filename: address[-1],
  760.  
  761.     }
  762.   end
  763.   #--------------------------------------------------------------------------
  764.   # * Gets the request string
  765.   # @param [Hash] request_array
  766.   # @return String
  767.   #--------------------------------------------------------------------------
  768.   def self.prepare_request(request_array)
  769.     array = []
  770.     request_array.each_pair{|key, value|
  771.       array.push(sprintf('%s=%s', key.to_s, value.to_s.gsub(' ', '+')))
  772.     }
  773.     array * '&'
  774.   end
  775.   #--------------------------------------------------------------------------
  776.   # * Scarica un file da internet in un thread separato
  777.   # url = indirizzo completo del nome del file
  778.   # folder = cartella dove depositare il file scaricato
  779.   # @param [String] url
  780.   # @param [String] folder
  781.   # @param [Boolean] low_priority
  782.   # @param [String] filename
  783.   # @param [Boolean] save
  784.   #--------------------------------------------------------------------------
  785.   def self.download(url, folder = './', low_priority = false, filename = nil, save = true)
  786.     #inizializzazione
  787.     @downloaded = 0 if @downloaded.nil?
  788.     @downloads = {} if @downloads.nil?
  789.     @counter = -1 if @counter.nil?
  790.     @size = {} if @size.nil?
  791.     @received = {} if @received.nil?
  792.     @timepassed = {} if @timepassed.nil?
  793.     @message = {} if @message.nil?
  794.     @completed = {} if @completed.nil?
  795.  
  796.     #ottenimento dell'indirizzo
  797.     address = url.split('/')
  798.     server = address[2]
  799.     root = address[3..address.size].join('/')
  800.     filename = address[-1] if filename == nil
  801.  
  802.     #crezione del thread
  803.     @downloads[filename] = Thread.start(url,folder, save){|url,folder,save|
  804.       txt = ""
  805.       t = Time.now
  806.       status = InternetConnectA.call(InternetOpenA,server,80,'','',3,1,0)
  807.       if status == 0
  808.         #raise InternetConnectionException, 'Error while connecting to the server'
  809.         @received[filename] = 0
  810.         @size[filename] = 0
  811.         @timepassed[filename] = 0
  812.         puts 'connection error ' + server
  813.         return
  814.       end
  815.       file = InternetOpenUrl.call(InternetOpenA,url,nil,0,0,0)
  816.       HttpQueryInfo.call(file,5,k="\0"*1024,[k.size-1].pack('l'),nil)
  817.       @received[filename] = 0
  818.       @size[filename] = k.delete!("\0").to_i
  819.       loop do
  820.         buffer = ' ' * 1024
  821.         n = 0
  822.         r = InternetReadFile.call(file,buffer,1024,o=[n].pack('i!'))
  823.         n = o.unpack('i!')[0]
  824.         txt<<buffer[0,n]
  825.         @message[filename] = txt unless save
  826.         @received[filename] = txt.size
  827.         if r && n == 0
  828.           break
  829.         end
  830.         sleep(0.001) if low_priority
  831.       end
  832.       #creazione del file nel percorso
  833.       if save
  834.         if File.directory?(folder)
  835.           obj = File.open(folder + filename,'wb')<<txt
  836.           obj.close #chiusura del file
  837.         else
  838.           string = '%s non è un percorso valido, pertanto %s non verrà salvato.'
  839.           println sprintf(string, folder, filename)
  840.         end
  841.       end
  842.       @received[filename] = @size[filename]
  843.       @completed[filename] = true
  844.       @downloaded += @received[filename]
  845.       InternetCloseHandle.call(file)
  846.       sleep(0.01)
  847.       @timepassed[filename] = Time.now-t
  848.     }
  849.   end
  850.   #--------------------------------------------------------------------------
  851.   # * Ottiene la dimensione di un file remoto in modo asincrono
  852.   #--------------------------------------------------------------------------
  853.   def self.get_file_size_async(url)
  854.     @filesize = {} if @filesize.nil?
  855.     Thread.start(url){|url|
  856.       @filesize[url] = get_file_size(url)
  857.     }
  858.   end
  859.   #--------------------------------------------------------------------------
  860.   # * Restituisce true se la dimensione del file è stata ottenuta
  861.   #--------------------------------------------------------------------------
  862.   def self.size_get?(url)
  863.     return false if @filesize.nil?
  864.     return @filesize[url] != nil?
  865.   end
  866.   #--------------------------------------------------------------------------
  867.   # * Ottiene la dimensione di un file remoto
  868.   #--------------------------------------------------------------------------
  869.   def self.get_file_size(url)
  870.     file = InternetOpenUrl.call(InternetOpenA,url,nil,0,0,0)
  871.     HttpQueryInfo.call(file,5,k="\0"*1024,[k.size-1].pack('l'),nil)
  872.     InternetCloseHandle.call(file)
  873.     return k.delete!("\0").to_i
  874.   end
  875.   #--------------------------------------------------------------------------
  876.   # * Ottiene la risposta del server e la piazza nell'array delle rispose
  877.   #   url: indirizzo della richiesta
  878.   #   response_name: nome della risposta (per poterla leggere)
  879.   #   low_priority: priorità (false se normale, true se bassa)
  880.   #--------------------------------------------------------------------------
  881.   def self.get_server_response(url, response_name, low_priority = false)
  882.     download(url, "", low_priority, response_name, false)
  883.   end
  884.   #--------------------------------------------------------------------------
  885.   # * Restituisce direttamente il testo di risposta dal server, interrompendo
  886.   #   l'esecuzione del gioco fino a quando non arriva la risposta.
  887.   #   url: indirizzo della richiesta
  888.   #--------------------------------------------------------------------------
  889.   def self.await_get_server_response(url)
  890.     response = "response"
  891.     @message.delete(response) if @message != nil
  892.     @received.delete(response) if @received != nil
  893.     @downloads.delete(response) if @downloads != nil
  894.     download(url, "", false, response, false)
  895.     loop {break if downloaded?(response)}
  896.     return @message[response]
  897.   end
  898.   class << self; alias await_response await_get_server_response; end
  899.   #--------------------------------------------------------------------------
  900.   # * Restituisce true se il file è scaricato.
  901.   # filename: nome del file
  902.   #--------------------------------------------------------------------------
  903.   def self.downloaded?(filename)
  904.     return false if @received.nil?
  905.     return false if @received[filename].nil?
  906.     return false if @size[filename].nil?
  907.     return true if @received[filename] >= @size[filename]
  908.     return true if progress(filename) >= 100
  909.     return true if progress(filename).to_s == "NaN"
  910.     return true if @completed[filename]
  911.     false
  912.   end
  913.   #--------------------------------------------------------------------------
  914.   # * Restituisce la grandezza del file da scaricare in byte
  915.   # filename: nome del file
  916.   #--------------------------------------------------------------------------
  917.   def self.filesize(filename)
  918.     return 0 if @size.nil?
  919.     return 0 if @size[filename].nil?
  920.     return @size[filename]
  921.   end
  922.   #--------------------------------------------------------------------------
  923.   # * Restituisce la quantità di byte scaricati
  924.   # filename: nome del file
  925.   #--------------------------------------------------------------------------
  926.   def self.sizeloaded(filename)
  927.     return 0 if @received.nil?
  928.     return 0 if @received[filename].nil?
  929.     return @received[filename]
  930.   end
  931.   #--------------------------------------------------------------------------
  932.   # * Restituisce la percentuale di progresso del download
  933.   # filename: nome del file
  934.   #--------------------------------------------------------------------------
  935.   def self.progress(filename)
  936.     @received = {} if @received.nil?
  937.     return 0 if @received[filename].nil?
  938.     return @received[filename].to_f/@size[filename]*100
  939.   end
  940.   #--------------------------------------------------------------------------
  941.   # * Restituisce l'hash dei download
  942.   #--------------------------------------------------------------------------
  943.   def downloads
  944.     return {} if @downloads.nil?
  945.     @downloads
  946.   end
  947.   #--------------------------------------------------------------------------
  948.   # * Restituisce il numero di secondi che ci sono voluti per scaricare
  949.   #--------------------------------------------------------------------------
  950.   def self.time_passed(filename)
  951.     return 0 if @timepassed[filename] == nil
  952.     return @timepassed[filename]
  953.   end
  954.   #--------------------------------------------------------------------------
  955.   # * Restituisce il numero dei download completati
  956.   #--------------------------------------------------------------------------
  957.   def self.completed
  958.     return 0 if @downloaded.nil?
  959.     return @downloaded
  960.   end
  961.   #--------------------------------------------------------------------------
  962.   # * Restituisce la risposta dal server
  963.   #   response_name: id della risposta
  964.   #--------------------------------------------------------------------------
  965.   def self.response(response_name)
  966.     return 0 if @message.nil? || @message[response_name].nil?
  967.     return @message[response_name]
  968.   end
  969.   #--------------------------------------------------------------------------
  970.   # * Restituisce i download
  971.   #--------------------------------------------------------------------------
  972.   def self.downloads
  973.     @downloads ||= {}
  974.     return @downloads
  975.   end
  976.   #--------------------------------------------------------------------------
  977.   # * Restituisce il dominio principale
  978.   #--------------------------------------------------------------------------
  979.   def self.domain
  980.     H87_ModConfig::HTTPDOMAIN
  981.   end
  982.   #--------------------------------------------------------------------------
  983.   # * Controlla se il file scaricato è buono e restituisce true o false
  984.   #--------------------------------------------------------------------------
  985.   def self.file_good?(filename)
  986.     if File.exist? (filename) #apre il file in sola lett.
  987.       File.open(filename,"r") do |f|
  988.         f.lineno = 1 # imposta la riga n.1
  989.         txt = f.gets
  990.         return check_response(txt)
  991.       end
  992.     else
  993.       return false
  994.     end
  995.   end
  996.   #--------------------------------------------------------------------------
  997.   # * Restituisce true se il testo non è vuoto
  998.   #--------------------------------------------------------------------------
  999.   def self.check_response(text)
  1000.     return false if text == "" or text.nil?
  1001.     first = text[0].chr
  1002.     return false if first == "" or first == nil
  1003.     return true
  1004.   end
  1005. end #http
  1006.  
  1007. #==============================================================================
  1008. # ** Async_Downloads
  1009. #------------------------------------------------------------------------------
  1010. #  Dev'essere incluso nelle classi che fanno uso dei metodi download_async.
  1011. #==============================================================================
  1012. module Async_Downloads
  1013.   #--------------------------------------------------------------------------
  1014.   # * Scarica un file in modo asincrono lanciando automaticamente il metodo.
  1015.   #   url:          indirizzo del file
  1016.   #   method_name:  nome del metodo, in simbolo (ad es. :apri)
  1017.   #   low:          true se è a bassa incidenza, false altrimenti
  1018.   #   folder:       percorso del file di salvataggio
  1019.   #--------------------------------------------------------------------------
  1020.   def download_async(url, method, folder = "./", low = true)
  1021.     filename = url.split('/')[-1]
  1022.     if filename.downcase.include? ".php"
  1023.       println "Questo URL chiama un file PHP, pertanto non verrà salvato."
  1024.       println "Utilizzare invece il metodo get_repsonse_async."
  1025.       return
  1026.     end
  1027.     @async_downloads = {} if @async_downloads.nil?
  1028.     @async_downloads[filename] = method
  1029.     HTTP.download(url, folder, low)
  1030.   end
  1031.   #--------------------------------------------------------------------------
  1032.   # * Ottiene la risposta di un servizio web in modo asincrono, lanciando
  1033.   #   automaticamente il metodo associato.
  1034.   #   url:          indirizzo della richiesta
  1035.   #   method_name:  nome del metodo, in simbolo (ad es. :apri)
  1036.   #   low:          true se è a bassa incidenza, false altrimenti
  1037.   #   response_id:  id della risposta.
  1038.   # @param [String] url
  1039.   # @param [String] method
  1040.   # @param [Boolean] low
  1041.   # @param [String] response_id
  1042.   # @return [String]
  1043.   # @deprecated
  1044.   #--------------------------------------------------------------------------
  1045.   def get_response_async(url, method, low = true, response_id = String.random(20))
  1046.     @async_responses = {} if @async_responses.nil?
  1047.     @async_responses[response_id] = method
  1048.     HTTP.get_server_response(url, response_id, low)
  1049.   end
  1050.   #--------------------------------------------------------------------------
  1051.   # * Restituisce direttamente la stringa di risposta dal server
  1052.   #   url: indirizzo della richiesta
  1053.   # @param [String] url
  1054.   # @return [String]
  1055.   # @raise [InternetConnectionException]
  1056.   #--------------------------------------------------------------------------
  1057.   def await_response(url)
  1058.     HTTP.await_get_server_response(url)
  1059.   end
  1060.   #--------------------------------------------------------------------------
  1061.   # * Controlla i download e lancia il metodo associato se completato.
  1062.   #--------------------------------------------------------------------------
  1063.   def check_async_downloads
  1064.     return if @async_downloads.nil? || @async_downloads.size == 0
  1065.     @async_downloads.each_key do |key|
  1066.       if HTTP.downloaded?(key)
  1067.         @async_downloads[key].call
  1068.         @async_downloads.delete(key)
  1069.       end
  1070.     end
  1071.   end
  1072.   #--------------------------------------------------------------------------
  1073.   # * Controlla le risposte e lancia il metodo associato quando ricevuta.
  1074.   #--------------------------------------------------------------------------
  1075.   def check_async_requests
  1076.     return if @async_responses.nil? || @async_responses.size == 0
  1077.     @async_responses.each_key do |key|
  1078.       if HTTP.downloaded?(key) && HTTP.response(key) != nil
  1079.         @async_responses[key].call(HTTP.response(key))
  1080.         @async_responses.delete(key)
  1081.       end
  1082.     end
  1083.   end
  1084.   #--------------------------------------------------------------------------
  1085.   # * Cancella un download o l'attesa di una risposta
  1086.   #   filename: nome del file o id della risposta
  1087.   #--------------------------------------------------------------------------
  1088.   def abort_download(filename)
  1089.     Thread.kill(HTTP.downloads[filename])
  1090.   end
  1091.   #--------------------------------------------------------------------------
  1092.   # * Restituisce la percentuale di download da 0 a 100
  1093.   #   filename: nome del file
  1094.   # @param [String] filename
  1095.   # @return [Integer]
  1096.   #--------------------------------------------------------------------------
  1097.   def download_status(filename)
  1098.     status = HTTP.progress(filename)
  1099.     [[0, status].max, 100].min.to_i
  1100.   end
  1101. end
  1102.  
  1103. #==============================================================================
  1104. # ** Modulo Browser per aprire il browser predefinito del PC
  1105. #==============================================================================
  1106. module Browser
  1107.   #--------------------------------------------------------------------------
  1108.   # * apre il browser
  1109.   #   url: url impostato
  1110.   # @param [String] url
  1111.   #--------------------------------------------------------------------------
  1112.   def self.open(url)
  1113.     #controlla che ci siano prefissi
  1114.     if url[0..6] != "http://" and url[0..7] != "https://"
  1115.       open_url = "http://"+url
  1116.     else
  1117.       open_url = url
  1118.     end
  1119.     shell = Win32API.new("Shell32", "ShellExecute", ['L', 'P', 'P', 'P','P', 'L'], 'L')
  1120.     Thread.new{shell.call(0, "open", open_url, 0, 0, 1)}
  1121.     sleep(0.01)
  1122.     SceneManager.exit
  1123.   end
  1124. end #browser
  1125.  
  1126. #==============================================================================
  1127. # ** Modulo Browser per la codifica/decodifica di stringhe in Base64
  1128. #==============================================================================
  1129. module Base64
  1130.   #--------------------------------------------------------------------------
  1131.   # * Restituisce una stringa decodificata da Base64
  1132.   #     string: stringa da decodificare
  1133.   # @param [String] string
  1134.   # @return [String]
  1135.   #--------------------------------------------------------------------------
  1136.   def self.decode(string)
  1137.     return string.gsub(/\s+/, "").unpack("m")[0]
  1138.   end
  1139.     #--------------------------------------------------------------------------
  1140.     # * Restituisce una stringa codificata in Base64
  1141.     #     string: stringa da codificare
  1142.     # @param [String] string
  1143.     # @return [String]
  1144.     #--------------------------------------------------------------------------
  1145.   def self.encode(string)
  1146.     return [string].pack("m")
  1147.   end
  1148. end #base64
  1149.  
  1150. #==============================================================================
  1151. # ** Classe Settings (per le impostazioni comuni ai salvataggi)
  1152. #==============================================================================
  1153. class H87_Settings
  1154.   #--------------------------------------------------------------------------
  1155.   # * restituisce l'elemento corrispondente al parametro
  1156.   # @param [Object] param
  1157.   # @return [Object]
  1158.   #--------------------------------------------------------------------------
  1159.   def [](param)
  1160.     @settings[param]
  1161.   end
  1162.   #--------------------------------------------------------------------------
  1163.   # * inizializzazione
  1164.   #--------------------------------------------------------------------------
  1165.   def initialize
  1166.     @settings = {}
  1167.   end
  1168.   #--------------------------------------------------------------------------
  1169.   # * cambia o aggiunge un elemento dell'hash
  1170.   #--------------------------------------------------------------------------
  1171.   def []=(param_name,value)
  1172.     @settings[param_name] = value
  1173.     save
  1174.   end
  1175.   #--------------------------------------------------------------------------
  1176.   # * Registra i dati salvati
  1177.   #--------------------------------------------------------------------------
  1178.   def save
  1179.     save_data($game_settings,DataManager.settings_path)
  1180.   end
  1181. end #settings
  1182.  
  1183. #==============================================================================
  1184. # ** Game_Version
  1185. #------------------------------------------------------------------------------
  1186. # Questa classe è d'appoggio per gestire la versione del gioco.
  1187. #==============================================================================
  1188. class Game_Version
  1189.   include Comparable        #per la verifica delle versioni se maggiore o minore
  1190.   attr_accessor :major      #numero di major release
  1191.   attr_accessor :minor      #numero di minor release
  1192.   attr_accessor :build      #numero di versione build
  1193.   attr_accessor :revision   #numero di revisione
  1194.   #--------------------------------------------------------------------------
  1195.   # * Inizializzazione
  1196.   #     version_string: versione in stringa (ad es. 1.5.3.1)
  1197.   # @param [String] version_string
  1198.   # @param [Integer] starting_major
  1199.   #--------------------------------------------------------------------------
  1200.   def initialize(version_string, starting_major = 1)
  1201.     @major = starting_major
  1202.     @minor = 0
  1203.     @build = 0
  1204.     @revision = 0
  1205.     version_string.gsub!(/\s\n\r/,"")
  1206.     return unless version_string =~ /[\d]+([\.[\d]]*)/
  1207.     version_string = version_string.split('.')
  1208.     @major = version_string[0].to_i
  1209.     return if version_string[1].nil?
  1210.     @minor = version_string[1].to_i
  1211.     return if version_string[2].nil?
  1212.     @build = version_string[2].to_i
  1213.     return if version_string[3].nil?
  1214.     @revision = version_string[3].to_i
  1215.   end
  1216.   #--------------------------------------------------------------------------
  1217.   # * Restituisce la versione attuale del gioco
  1218.   # @return [Game_Version]
  1219.   #--------------------------------------------------------------------------
  1220.   def self.now
  1221.     if File.exist?(H87_ModConfig::VERSIONFILE)
  1222.       file = File.open(H87_ModConfig::VERSIONFILE,'r')
  1223.       str = file.read
  1224.       file.close
  1225.       Game_Version.new(str)
  1226.     else
  1227.       Game_Version.new('1.0.0.0')
  1228.     end
  1229.   end
  1230.   #--------------------------------------------------------------------------
  1231.   # * Compara una versione o una stringa con se stessa
  1232.   #--------------------------------------------------------------------------
  1233.   def <=> other
  1234.     return self <=> Game_Version.new(other) if other.is_a?(String)
  1235.     return self.major <=> other.major if self.major != other.major
  1236.     return self.minor <=> other.minor if self.minor != other.minor
  1237.     return self.build <=> other.build if self.build != other.build
  1238.     self.revision <=> other.revision
  1239.   end
  1240.   #--------------------------------------------------------------------------
  1241.   # * restituisce la versione in stringa
  1242.   #--------------------------------------------------------------------------
  1243.   def to_s
  1244.     sprintf('%d.%d.%d.%d', @major, @minor, @build, @revision)
  1245.   end
  1246. end #game_version
  1247.  
  1248. #==============================================================================
  1249. # ** RPG::System -> aggiunta del metodo per la versione del gioco
  1250. #==============================================================================
  1251. class Game_System
  1252.   #--------------------------------------------------------------------------
  1253.   # * Restituisce la versione del gioco attuale
  1254.   #--------------------------------------------------------------------------
  1255.   def game_version
  1256.     Game_Version.now
  1257.   end
  1258. end #rpg_system
  1259.  
  1260. #==============================================================================
  1261. # ** DataManager -> aggiunta dei metodi per caricare i settaggi
  1262. #==============================================================================
  1263. module DataManager
  1264.   #--------------------------------------------------------------------------
  1265.   # * alias
  1266.   #--------------------------------------------------------------------------
  1267.   class << self
  1268.     alias h87set_load_n_db load_normal_database
  1269.     alias h87set_load_b_db load_battle_test_database
  1270.   end
  1271.   # --------------------------------------------------------------------------
  1272.   # * caricamento nd
  1273.   # --------------------------------------------------------------------------
  1274.   def self.load_normal_database
  1275.     load_h87settings
  1276.     h87set_load_n_db
  1277.   end
  1278.   # --------------------------------------------------------------------------
  1279.   # * caricamento btd
  1280.   # --------------------------------------------------------------------------
  1281.   def self.load_battle_test_database
  1282.     load_h87settings
  1283.     h87set_load_b_db
  1284.   end
  1285.   # --------------------------------------------------------------------------
  1286.   # * restituisce il percorso delle impostazioni
  1287.   # --------------------------------------------------------------------------
  1288.   def self.settings_path
  1289.     H87_ModConfig::SETTINGNAME
  1290.   end
  1291.   #--------------------------------------------------------------------------
  1292.   # * carica le impostazioni universali
  1293.   #--------------------------------------------------------------------------
  1294.   def self.load_h87settings
  1295.     return if $game_settings
  1296.     if File.exist?(settings_path)
  1297.       $game_settings = load_data(settings_path)
  1298.     else
  1299.       $game_settings = H87_Settings.new
  1300.       save_data($game_settings,settings_path)
  1301.     end
  1302.   end
  1303. end #datamanager
  1304.  
  1305. #==============================================================================
  1306. # ** Resolution
  1307. #------------------------------------------------------------------------------
  1308. # Un contenitore di risoluzioni
  1309. #==============================================================================
  1310. module Resolution
  1311.   QVGA  = '320x240'   # 4:3
  1312.   VGA   = '640x480'   # 4:3
  1313.   WSVGA = '1024x600'  # 16:9
  1314.   SVGA  = '800x600'   # 4:3
  1315.   XGA   = '1024x768'  # 4:3
  1316.   SXGA  = '1280x1024' # 4:3
  1317.   WXGA  = '1280x720'  # 16:9
  1318.   HD768 = '1366x768'  # 16:9
  1319.   FULLHD= '1920x1080' # 16:9
  1320.   WUXGA = '1920x1200' # 16:10
  1321. end
  1322.  
  1323. #==============================================================================
  1324. # ** Cache
  1325. #------------------------------------------------------------------------------
  1326. # Aggiunta della possibilità di scaricare l'immagine del giorno.
  1327. #==============================================================================
  1328. module Cache
  1329.     #--------------------------------------------------------------------------
  1330.     # * Restituisce l'immagine del giorno di Bing come bitmap.
  1331.     #   È possibile specificare una risoluzione. Risoluzioni supportate:
  1332.     #   QVGA, VGA, SVGA, XGA, WXGA, HD768, FULLHD, WUXGA
  1333.     # @param [String] resolution
  1334.     # @return [Bitmap]
  1335.     #--------------------------------------------------------------------------
  1336.     def self.bing_daily(resolution = Resolution::VGA)
  1337.     if bing_daily_ready?(resolution)
  1338.       return @bing_daily[resolution]
  1339.     end
  1340.     @bing_info = get_bing_xml if @bing_info.nil?
  1341.     if @bing_info != nil
  1342.       @bing_daily_copyright = @bing_info[:copyright]
  1343.       image_name = @bing_info[:filename] + "_#{resolution}.jpg"
  1344.       url = "http://www.bing.com#{@bing_info[:url]}_#{resolution}.jpg"
  1345.       HTTP.download(url, bing_download_folder)
  1346.       loop{break if HTTP.downloaded?(image_name)}
  1347.       bitmap = load_bitmap(bing_download_folder, image_name)
  1348.       @bing_daily[resolution] = bitmap
  1349.       bitmap
  1350.     else
  1351.       nil
  1352.     end
  1353.   end
  1354.   #--------------------------------------------------------------------------
  1355.   # * Restituisce la cartella di download dell'immagine
  1356.   # @return [String]
  1357.   #--------------------------------------------------------------------------
  1358.   def self.bing_download_folder; './Graphics/Pictures/'; end
  1359.   #--------------------------------------------------------------------------
  1360.   # * Restituisce il copyright dell'immagine del giorno. usare solo se già
  1361.   #   scaricata l'immagine!
  1362.   # @return [String]
  1363.   #--------------------------------------------------------------------------
  1364.   def self.bing_daily_copyright
  1365.     if @bing_daily_copyright
  1366.       @bing_daily_copyright
  1367.     else
  1368.       @bing_info = get_bing_xml
  1369.       if @bing_info.nil?
  1370.         ''
  1371.       else
  1372.         @bing_daily_copyright = @bing_info[:copyright]
  1373.         @bing_daily_copyright
  1374.       end
  1375.     end
  1376.   end
  1377.   #--------------------------------------------------------------------------
  1378.   # * Ottiene il feed xml dell'immagine di Bing e ne restituisce un hash con
  1379.   #   informazioni, oppure nil se la connessione non è riuscita
  1380.   # @return [Hash]
  1381.   #--------------------------------------------------------------------------
  1382.   def self.get_bing_xml
  1383.     lang = $imported['H87_Localization'] ? H87Localization.system_language : 'en-US'
  1384.     url = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=#{lang}"
  1385.     response = HTTP.await_get_server_response(url)
  1386.     begin
  1387.       if response =~ /<urlBase>(.+)<\/urlBase>/i
  1388.         info = {}
  1389.         info[:url] = $1
  1390.         info[:filename] = File.basename(info[:url])
  1391.       else
  1392.         return nil
  1393.       end
  1394.     rescue
  1395.       return nil
  1396.     end
  1397.     if response =~ /<copyright>(.+)<\/copyright>/i
  1398.       info[:copyright] = $1
  1399.     else
  1400.       info[:copyright] = ''
  1401.     end
  1402.     info
  1403.   end
  1404.   #--------------------------------------------------------------------------
  1405.   # * Controlla se l'immagine del giorno è scaricata
  1406.   #--------------------------------------------------------------------------
  1407.   def self.bing_daily_ready?(resolution = Resolution::VGA)
  1408.     @bing_daily != nil && @bing_daily[Resolution::VGA.to_sym] != nil
  1409.   end
  1410. end
  1411.  
  1412. #==============================================================================
  1413. # ** Aggiunta di alcuni metodi utili per le stringhe
  1414. #==============================================================================
  1415. class String
  1416.   #--------------------------------------------------------------------------
  1417.   # * Metodo Random: restituisce una stringa a caso
  1418.   #   size: numero di caratteri della stringa
  1419.   # @param [Integer] size
  1420.   # @return [String]
  1421.   #--------------------------------------------------------------------------
  1422.   def self.random(size = 4); rand(36**size).to_s(36); end
  1423.   #--------------------------------------------------------------------------
  1424.   # * Restituisce la stessa stringa ma crittografata in ROT13
  1425.   #   http://it.wikipedia.org/wiki/ROT13
  1426.   # @return [String]
  1427.   #--------------------------------------------------------------------------
  1428.   def crypt_rot13; self.tr('A-Za-z', 'N-ZA-Mn-za-m'); end
  1429. end #fine della stringa
  1430.  
  1431. #==============================================================================
  1432. # ** Inclusione dei metodi asincroni in Scene_Base
  1433. #==============================================================================
  1434. class Scene_Base
  1435.   include Async_Downloads # inclusione del modulo
  1436.   #--------------------------------------------------------------------------
  1437.   # * Alias del metodo d'aggiornamento
  1438.   #--------------------------------------------------------------------------
  1439.   alias h87_module_update update unless $@
  1440.   def update
  1441.     h87_module_update
  1442.     check_async_downloads #controlla i download
  1443.     check_async_requests  #controlla le richieste
  1444.   end
  1445. end #scene_base
  1446.  
  1447. #==============================================================================
  1448. # ** Inclusione dei metodi asincroni in Window_Base
  1449. #==============================================================================
  1450. class Window_Base
  1451.   include Async_Downloads # inclusione del modulo
  1452.   #--------------------------------------------------------------------------
  1453.   # * Alias del metodo d'aggiornamento
  1454.   #--------------------------------------------------------------------------
  1455.   alias h87_module_update update unless $@
  1456.   def update
  1457.     h87_module_update
  1458.     check_async_downloads #controlla i download
  1459.     check_async_requests  #controlla le richieste
  1460.   end
  1461. end #scene_base
  1462.  
  1463. #==============================================================================
  1464. # ** Object
  1465. #------------------------------------------------------------------------------
  1466. # Metodi universali di gioco
  1467. #==============================================================================
  1468. class Object
  1469.   #--------------------------------------------------------------------------
  1470.   # * Metodo di stampa riga
  1471.   #--------------------------------------------------------------------------
  1472.   def println(*args); puts *args; end
  1473.   #--------------------------------------------------------------------------
  1474.   # * Metodi di conversione Base64
  1475.   #--------------------------------------------------------------------------
  1476.   # @param [String] string
  1477.   # @return [String]
  1478.   def base64_encode(string); Base64.encode(string); end
  1479.   # @param [String] string
  1480.   # @return [String]
  1481.   def base64_decode(string); Base64.decode(string); end
  1482.   #--------------------------------------------------------------------------
  1483.   # * Restituisce direttamente la stringa di risposta dal server
  1484.   #   url: indirizzo della richiesta
  1485.   # @param [String] url
  1486.   # @return [String]
  1487.   #--------------------------------------------------------------------------
  1488.   def await_response(url)
  1489.     HTTP.await_get_server_response(url) rescue ''
  1490.   end
  1491.   #--------------------------------------------------------------------------
  1492.   # * Restituisce direttamente la stringa di risposta dal server
  1493.   #   url: indirizzo della richiesta
  1494.   # @param [String] url
  1495.   # @param [Hash] params
  1496.   # @return [String]
  1497.   # @raise [InternetConnectionException]
  1498.   #--------------------------------------------------------------------------
  1499.   def submit_post_request(url, params)
  1500.     HTTP.send_post_request(url, params)
  1501.   end
  1502. end
  1503.  
  1504. # launched when can't connect with the server
  1505. class InternetConnectionException < Exception; end
  1506. # launched when can't read from the clipboard
  1507. class ClipboardDataAccessException < Exception; end
Add Comment
Please, Sign In to add comment