summaryrefslogblamecommitdiff
path: root/game/update.rpy
blob: 0e29fb4cd7933866db5230ab01530d8efeeabd69 (plain) (tree)
1
2
3
4
5
6
7
8







                                                                                 






                                      






















                                                                          


                                                        
                
                                                                        
                                                         
                                              







                                                                    
























                                                                                                                                                                 

                                                                                 
                             

                                                 




                                                                                 
                                                                   













                                                                                                                                                                                   
                                                                                  










                                                                          
                                                                                                 
                           
 


                                                          

                                                                  
                       

                                                                                       
 
                                                 
                                         
                                                         
                                                                                                                              

                                

                                         
 



                                                                                 
 






                                                         
                            








                                                 







                                                                                 

              
#################################################################################
#     This file is part of Mana Launcher.
#     Copyright (C) 2021  Jesusalva <jesusalva@tmw2.org>
#
#     Distributed under the MIT license.
#################################################################################

init python:
    def status_update(smsg="", pc=-1):
        global progress, statusmsg
        if pc > 0 and pc != progress:
            progress=min(pc, 100)
        if (smsg != ""):
            statusmsg=smsg

    def validate_server(srv):
        name="Unknown Server"; ok=False
        try:
            name=srv["Name"]
            stdout("Validating server \"%s\"..." % name)
            print("Host: %s" % srv["Host"])
            print("Port: %04d" % srv["Port"])
            print("Desc: %s" % srv["Desc"])
            print("Link: %s" % srv["Link"])
            print("News: %s" % srv["News"])
            print("Back: %s" % srv["Back"])
            print("UUID: %s" % srv["UUID"])
            print("Help: %s" % srv["Help"])
            print("Online List: %s" % srv["Online"])
            print("Policy: %s" % srv["Policy"])
            stdout("Server \"%s\" is valid! Downloading assets..." % name)
            ok=True
        except:
            traceback.print_exc()
            stdout("Validation for server \"%s\" FAILED!" % name)

        ## Fetch server background (optional) if successful
        if (ok):
            bgname="bg_%s.png" % (name.replace(" ", ""))
            if os.path.exists(get_path(bgname)):
                return ok
            try:
                stdout("Fetching background for server \"%s\"" % bgname)
                r=requests.get(srv["Back"], timeout=10.0)
                with open(bgname, 'wb') as fd:
                    for chunk in r.iter_content(chunk_size=128):
                        fd.write(chunk)
            except:
                traceback.print_exc()
                stdout("Background error for server \"%s\"." % name)

        return ok

    def update_serverlist(host):
        try:
            r=requests.get("%s/server_list.json" % host, timeout=10.0)

            if (r.status_code != 200):
                raise AssertionError("Mirror %s seems to be down!\nReturned error %03d\n" % (host.replace("https://", "").replace("http://", ""), r.status_code))

            j=json.loads(r.text)

            ## If we reached here, then everything is likely fine
            ## We can now build the persistent data
            status_update("Fetching server list and assets...", 20)
            persistent.serverlist = []
            slist=len(j)
            for server in j:
                if (validate_server(server)):
                    persistent.serverlist.append(server)
                status_update(pc=int(10+(70.0/slist)))

            ## If we were truly successful, save this host as our mirror
            persistent.host = host
            return True
        except:
            traceback.print_exc()
            return False

    #############################################################################
    def CONFIGURE_LAUNCHER():
        global progress, statusmsg, responsive
        statusmsg="Loading user configuration..."
        #########################################################################
        # If persistent data is not yet set, it must be created
        # This block is 1~60%
        # But first we check for updates
        if persistent.host is not None:
            stdout("Fetching data from %s" % persistent.host, True)
            retry=3
            while retry > 0:
                try:
                    retry-=1
                    rv=requests.get("%s/version.txt" % persistent.host, timeout=10.0)

                    if (rv.status_code != 200):
                        raise AssertionError("Mirror %s seems to be down!\nReturned error %03d" % (persistent.host.replace("https://", "").replace("http://", ""), rv.status_code))

                    # Everything went fine! Update server list if needed
                    if (persistent.version != rv.text):
                        if not update_serverlist(persistent.host):
                            raise Exception("Mirror serverlist error")
                        persistent.version=rv.text
                        stdout("Version updated to %s" % persistent.version, True)
                    retry=0
                except:
                    traceback.print_exc()
                    if retry < 1:
                        persistent.host=None
                        stdout("Too many failures, seeking a new mirror.")

        ## Either our original mirror died, or it was never a thing
        ## Maybe you're offline but in any case:
        ## Seek a new mirror and update server list accordingly.
        if persistent.host is None:
            status_update("Welcome to the Mana Launcher.\nBuilding user data, please wait...", 1)
            time.sleep(1.0)

            # Determine from where we should fetch updates
            for host in HOST_LIST:
                try:
                    if not update_serverlist(host):
                        raise Exception("Mirror serverlist error")
                except:
                    stdout("An error happened on mirror \"%s\"." % host.split("//")[1])
                    stdout("\nTrying next mirror...\n")

            ## If we don't have a host - DIE HARD
            if (persistent.host is None):
                stdout("No mirror could be found.", True)
                status_update("{color=#f00}{b}ERROR: No active mirror found.\nMana Launcher needs to be updated.{/b}{/color}")
                responsive=False
                return

            ## Everything was successful!


        #########################################################################
        # Check for clients, this block is 60~80%
        status_update("Checking for installed clients...", 60)

        ## Check for ManaPlus
        try:
            MANAPLUS=os.path.exists(get_path("manaplus"))
        except:
            traceback.printexc()
            MANAPLUS=False

        status_update(pc=70)
        ## Check for Mana
        try:
            MANA=os.path.exists(get_path("mana"))
        except:
            traceback.printexc()
            MANA=False

        time.sleep(0.1)

        #########################################################################
        # Before starting, we must check for Vault or Steam credentials
        # This block is 1~20%
        status_update("Verifying credentials...", 80)
        # TODO


        status_update("Complete!", 100)
        return