Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################################################################
- # multi_loader.gd #
- #########################################################################
- # This file is part of: #
- # WILD WITCH PROJECT #
- # http://www.wildwitchproject/ #
- #########################################################################
- # CC-BY-SA #
- # Wild Witch Project by Marcos Augusto Bitetti is licensed under a #
- # Creative Commons Attribution-ShareAlike 4.0 International License. #
- # Based on a work at http://www.wildwitchproject.com/. #
- # Permissions beyond the scope of this license may be available at #
- # http://www.wildwitchproject.com/p/direitos-da-obra.html. #
- #########################################################################
- # 'load_complete'
- # 'load_progress'
- # 'load_error'
- var server = 'localhost'
- var protocol = 'HTTP'
- var port = 80
- var get_string = '/'
- var mime_type = 'none'
- var data = null
- var headers = [
- "User-Agent: Custom Brownser/1.0 (Godot)",
- "Accept: */*"
- ]
- var response_headers = null
- var response_code = 0
- var _is_working = false
- var _http = HTTPClient.new()
- var _thread = Thread.new()
- var _is_connected = false
- var _old_server = ''
- var _redirection_level = 0
- func _init():
- add_user_signal('load_complete')
- add_user_signal('load_progress')
- add_user_signal('load_error')
- func _parse_url(url):
- var err = 0
- var http = HTTPClient.new()
- var tks = url.split('/',false)
- protocol = 'HTTP'
- if tks[0]=='https:':
- self.protocol = 'HTTPS'
- # remove protocol info
- var i = 0
- if tks[0]=='http:' or tks[0]=='https:' :
- i = 1
- server = tks[i]
- i = i + 1
- if server.find(':')>-1:
- var p1 = server.substr(0,server.find(':'))
- var p2 = server.substr(server.find(':')+1,server.length())
- server = p1
- port = int(p2)
- get_string = ''
- while i<tks.size():
- get_string += '/' + tks[i]
- i += 1
- ###
- #
- static func _bg_load_data(s):
- var err = 0
- var rb = RawArray()
- print(s.protocol,'://',s.server,':',s.port,'/',s.get_string)
- if not s._is_connected or s._old_server!=s.server:
- if s.protocol=='HTTP':
- err = s._http.connect(s.server,s.port,false)
- else:
- print('sim sim')
- err = s._http.connect(s.server,s.port,true)
- print('salabim')
- if err != OK:
- s._is_working = false
- print('err 00')
- return err
- print('quartman')
- #while( s._http.get_status()==HTTPClient.STATUS_CONNECTING or s._http.get_status()==HTTPClient.STATUS_RESOLVING):
- while s._http.get_status()!=HTTPClient.STATUS_CONNECTED:
- print('po')
- s._http.poll()
- print('te')
- OS.delay_msec(200)
- print(s._http.get_status())
- if s._http.get_status() != HTTPClient.STATUS_CONNECTED:
- s._is_working = false
- s._is_connected = false
- print('err 1')
- #return 1
- s._is_connected = true
- err = s._http.request(HTTPClient.METHOD_GET,s.get_string,s.headers)
- if err != OK:
- s._is_working = false
- s._is_connected = false
- print('err 2')
- return err
- print('bb')
- while (s._http.get_status() == HTTPClient.STATUS_REQUESTING):
- s._http.poll()
- OS.delay_msec(200)
- assert( s._http.get_status() == HTTPClient.STATUS_BODY or s._http.get_status() == HTTPClient.STATUS_CONNECTED)
- # s._is_working = false
- # s._is_connected = false
- # print('err 3')
- # return err
- print('0')
- if s._http.has_response():
- var headers = s._http.get_response_headers_as_dictionary()
- s.response_headers = headers
- print('a')
- # ignore is chuncked
- # s._http.is_response_chunked()
- if s._http.get_response_code() != HTTPClient.RESPONSE_MOVED_PERMANENTLY:
- while( s._http.get_status()==HTTPClient.STATUS_BODY):
- s._http.poll()
- var chunk = s._http.read_response_body_chunk()
- if chunk.size()==0:
- # got nothing, wait for buffers to fill a bit
- OS.delay_usec(1000)
- else:
- rb = rb + chunk
- #print('moved ',s._http.get_response_code() == HTTPClient.RESPONSE_MOVED_PERMANENTLY)
- #print('moved')
- #print(s._http.get_response_code())
- print('b')
- if s._http.get_response_code() == HTTPClient.RESPONSE_MOVED_PERMANENTLY:
- print('BAIXAR ',s.response_headers.Location)
- var nl = get_script().new()
- nl._redirection_level = s._redirection_level + 1
- if nl._redirection_level<2:
- nl.connect('load_complete',s,'_secundary_load')
- nl.load_data(s.response_headers.Location)
- print('segundo')
- else:
- s.emit_signal('load_error',s)
- else:
- s.data = rb
- print('c')
- s.emit_signal('load_complete', s)
- ###
- func _secundary_load(loader):
- data = loader.data
- emit_signal('load_complete',self)
- ####
- # return true if can load
- # return false if the object is already loading data
- ##
- func load_data(url):
- if _is_working or _thread.is_active():
- return false
- # define protocol
- if url.find('http')==0:
- _parse_url(url)
- _is_working = true
- _thread.start(self,"_bg_load_data",self)
- else:
- if url.find('file://')==0:
- url = url.substr(7,url.length())
- print(url)
- var f = File.new()
- var rb = RawArray()
- if f.file_exists(url):
- if f.open(url,File.READ)==OK:
- while not f.eof_reached():
- rb = rb + f.get_buffer(1024)
- data = rb
- f.close()
- emit_signal('load_complete',self)
- else:
- emit_signal('load_error',self)
- else:
- emit_signal('load_error',self)
Advertisement
Add Comment
Please, Sign In to add comment