コード例 #1
0
    public static string InitializeDatabase(AS_MySQLField[] additionalFields = null)
    {
        float timeLeft = timeOut;

        string msg = "";

        Debug.Log("AccountSystem: Initializing Database..");

        if (AS_Credentials.phpScriptsLocation == "")
        {
            Debug.LogError("AccountSystem: Host URL not set..! Please load the Account System Setup window!");
            return("Host URL not set..! Please load the Setup scene located in ../AccountSystem/Setup/");
        }

        // Location of the initialization script
        string url = AS_Credentials.phpScriptsLocation + createDBPhp;

        // Create a new form
        WWWForm form = new WWWForm();

        // Pass the AS_Preferences
        form.AddPreferences();

        List <AS_MySQLField> fields = new List <AS_MySQLField> ();

        // Add the username and password fields
        fields.Add(new AS_MySQLField("username", AS_MySQLFieldType.Varchar, true, true, "Unique - Used for loging in"));
        fields.Add(new AS_MySQLField("password", AS_MySQLFieldType.Varchar, false, true, "This is a hashed password, should be case invariant"));

        // Check if we should add an email
        if (AS_Preferences.askUserForEmail)
        {
            bool emailMustBeUnique = false;
            bool emailIsRequired   = false;

            if (AS_Preferences.enablePasswordRecovery | AS_Preferences.requireEmailActivation)
            {
                emailMustBeUnique = true;
                emailIsRequired   = true;
            }

            fields.Add(new AS_MySQLField("email", AS_MySQLFieldType.Varchar, emailMustBeUnique, emailIsRequired));

            // Add the field required for account activation
            if (AS_Preferences.requireEmailActivation)
            {
                fields.Add(new AS_MySQLField("isactive", AS_MySQLFieldType.Bool, false, true));
            }
        }

        // Add any extra fields
        if (additionalFields != null)
        {
            fields.AddRange(additionalFields);
        }


        // Stores XML data (potentially needs a lot of space, TEXT will do)
        fields.Add(new AS_MySQLField("custominfo", AS_MySQLFieldType.LongText, false, false,
                                     "This is used by [Upload|Download]InfoToDb (C#) to store the XML representation of custom serializable classes."));

        // Add the fields to the form
        msg = form.AddMySQLFields("fields", fields.ToArray());

        if (msg.ToLower().Contains("error"))
        {
            return("Make sure all field names have values");
        }

        // Connect to the script
        WWW www = new WWW(url, form);

        // Wait for it to respond
        Debug.Log("AccountSystem: Connecting to " + url);

        while (!www.isDone && timeLeft > 0)
        {
            Debug.Log("Time Left " + timeLeft.ToString("#.00"));
            timeLeft -= Time.fixedDeltaTime;

            if (timeLeft / timeOut > 0)
            {
                EditorUtility.DisplayProgressBar(
                    "Connecting to " + url + "..",
                    "Timing out in " + timeLeft.ToString("#.") + " seconds",
                    timeLeft / timeOut);
            }
            else
            {
                EditorUtility.ClearProgressBar();
            }
        }

        if (timeLeft <= 0)
        {
            return("Connection timed out!\nCheck your credentials, internet connection and firewall and try again.");
        }


        EditorUtility.ClearProgressBar();

        // Check for errors
        if (www.error != null)
        {
            Debug.LogError("AccountSystem: WWW Error:\n" + www.error);
            if (www.error.ToLower().Contains("404"))
            {
                return("Could not find CREATEDATABASE.PHP at " + AS_Credentials.phpScriptsLocation.ToUpper() + "\nHave you uploaded all the scripts from the folder 'Server-Side Scripts'?" + "\nCheck Debug.Log for more info.");
            }

            return("WWW Error\nCheck Debug.Log for more info");
        }
        if (www.text.ToLower().Contains("error"))
        {
            Debug.LogError("AccountSystem: PHP/MySQL Error:\n" + www.text);
            if (www.text.ToLower().Contains("cannot connect"))
            {
                return("Could not connect to the database.\nPlease check your credentials.\nCheck Debug.Log for more info.");
            }

            if (www.text.ToLower().Contains("404"))
            {
                return("Could not find CREATEDATABASE.PHP at " + AS_Credentials.phpScriptsLocation.ToUpper() + "\nHave you uploaded all the scripts from the folder 'Server-Side Scripts'?" + "\nCheck Debug.Log for more info.");
            }

            return("PHP/MySQL Error\nCheck Debug.Log for more info");
        }

        if (www.text.ToLower().Contains("success"))
        {
            Debug.Log("AccountSystem: Database Initialized successfully! You can now load the Demo scene!\n" + www.text);
            return("");
        }
        else
        {
            Debug.LogWarning("AccountSystem: Database could not be Initialized - Check Message:\n" + www.text);
            return("Database could not be Initialized- check Debug.Log for more info");;
        }
    }