Esempio n. 1
0
    /// <summary>
    /// Reads configuration data from a file. If the file read fails, the constructor contains
    /// default values for each kind of configuration data
    /// REFACTOR: Break this into three different constructors
    /// </summary>
    public ConfigData(ConfigDataType type)
    {
        // Read and save configuration data from file
        StreamReader input = null;

        // Three kinds of ConfigData
        switch (type)
        {
        case ConfigDataType.EnemyData:
            #region EnemyData

            try
            {
                // Create stream reader input
                input = File.OpenText(Path.Combine(
                                          Application.streamingAssetsPath, EnemyDataFileName));

                // Populate StatNames from header row
                string            currentLine = input.ReadLine();
                string[]          names       = currentLine.Split(',');
                BattleStatNames[] statHeader  = new BattleStatNames[names.Length];

                // Validate the header row before importing
                BattleStats validate = new BattleStats();

                for (int i = 1; i < names.Length; i++)
                {
                    statHeader[i] = (BattleStatNames)Enum.Parse(typeof(BattleStatNames), names[i]);
                    if (!validate.ValidateOrder(i - 1, statHeader[i]))
                    {
                        errorMessage = "Headers do not match BattleStats.\nUsing default settings.";
                        Debug.Log(errorMessage);
                        SetEnemyStatDataDefaultValues();
                        break;
                    }
                }

                // Only procede forward if there is no error with the headers
                if (errorMessage == null)
                {
                    // Populate values for enemyData
                    currentLine = input.ReadLine();
                    while (currentLine != null)
                    {
                        // Parse current line into name and stat values
                        string[]  tokens    = currentLine.Split(',');
                        EnemyName enemyName =
                            (EnemyName)Enum.Parse(
                                typeof(EnemyName), tokens[0]);

                        int[] intTokens = new int[tokens.Length - 1];

                        for (int i = 0; i < tokens.Length - 1; i++)
                        {
                            int bStat;
                            intTokens[i] = int.TryParse(tokens[i + 1], out bStat) ? bStat : 0;
                        }

                        // import the array of ints into a new BattleStats
                        BattleStats battleStats = new BattleStats();
                        battleStats.Import(intTokens);

                        // add to enemyStatData
                        enemyStatData.Add(enemyName, battleStats);

                        // queue next line in csv
                        currentLine = input.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                // send the error to the console for debugging
                Debug.Log(e);
                errorMessage = "Problem loading file. \nUsing default settings.\n";
                Debug.Log(errorMessage);

                SetEnemyStatDataDefaultValues();
            }
            finally
            {
                // close files that were opened
                if (input != null)
                {
                    input.Close();
                }
            }

            break;     // end of EnemyData
            #endregion EnemyData

        case ConfigDataType.InvData:
            #region InvData

            try
            {
                // create stream reader input
                input = File.OpenText(Path.Combine(
                                          Application.streamingAssetsPath, InvDataFileName));

                // populate StatNames from header row
                string            currentLine = input.ReadLine();
                string[]          headings    = currentLine.Split(',');
                BattleStatNames[] statHeader  = new BattleStatNames[headings.Length];

                // Validate the header row before importing
                // 0 name, 1 fullname, 2 description, 3 id, 4 price, 5 type, 6 subtype, 7 slot,
                // 8 rank, 9+ Battlestats
                BattleStats validate = new BattleStats();

                for (int i = 9; i < headings.Length; i++)
                {
                    statHeader[i] = (BattleStatNames)Enum.Parse(typeof(BattleStatNames), headings[i]);
                    if (!validate.ValidateOrder(i - 9, statHeader[i]))
                    {
                        errorMessage = "Headers do not match BattleStats.\nUsing default settings.";
                        Debug.Log(errorMessage);
                        SetInvDataDefaultValues();
                        break;
                    }
                }

                // only procede forward if there is no error with the headers
                if (errorMessage == null)
                {
                    // populate values for enemyData
                    currentLine = input.ReadLine();
                    while (currentLine != null)
                    {
                        // parse current line into name and stat values
                        // 0 name, 1 fullname, 2 description, 3 id, 4 price, 5 type, 6 subtype, 7 slot, 8 rank

                        // 0 name
                        string[] tokens  = currentLine.Split(',');
                        InvNames invName =
                            (InvNames)Enum.Parse(
                                typeof(InvNames), tokens[0]);

                        // 1 fullname
                        string fullName = tokens[1];

                        // 2 description
                        string description = tokens[2];

                        // 3 id
                        int id = int.Parse(tokens[3]);

                        // 4 price
                        int price = int.Parse(tokens[4]);

                        // 5 type
                        InvType invType = (InvType)Enum.Parse(
                            typeof(InvType), tokens[5]);

                        // 6 subtype
                        InvSubtype invSubtype = (InvSubtype)Enum.Parse(
                            typeof(InvSubtype), tokens[6]);

                        Sprite sprite = Resources.Load <Sprite>(@"Sprites\InvItems\"
                                                                + invType.ToString() + @"\"
                                                                + invSubtype.ToString() + @"\"
                                                                + invName.ToString());

                        // 7 slot
                        EquipSlots slot = (EquipSlots)Enum.Parse(
                            typeof(EquipSlots), tokens[7]);

                        // 8 rank
                        int rank = int.Parse(tokens[8]);


                        // BattleStats is only included for InvEqItems (equipment and tomes)
                        // This is an InvEqItem
                        if (invType == InvType.Tome || invType == InvType.Weapon || invType == InvType.Armor)
                        {
                            // 9+ BattleStats

                            int[] intTokens = new int[tokens.Length - 9];

                            for (int i = 0; i < tokens.Length - 9; i++)
                            {
                                int bStat;
                                intTokens[i] = int.TryParse(tokens[i + 9], out bStat) ? bStat : 0;
                            }

                            // import the array of ints into a new BattleStats
                            BattleStats battleStats = new BattleStats();
                            battleStats.Import(intTokens);

                            // create a new InvEqItem
                            InvEqItem item = new InvEqItem(invName, fullName, description, sprite,
                                                           id, price, 0, invType, invSubtype, slot, rank, battleStats);

                            // add to invStatData
                            invStatData.Add(invName, item);
                        }
                        // else it is a potion, so add a regular InvItem
                        else
                        {
                            // create a new InvEqItem
                            InvItem item = new InvItem(invName, fullName, description, sprite,
                                                       id, price, 0, invType, invSubtype, slot, rank);

                            // add to invStatData
                            invStatData.Add(invName, item);
                        }

                        // queue next line in csv
                        currentLine = input.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                // send the error to the console for debugging
                Debug.Log(e);
                errorMessage = "Problem loading file. \nUsing default settings.\n";
                Debug.Log(errorMessage);

                SetInvDataDefaultValues();
            }
            finally
            {
                // close files that were opened
                if (input != null)
                {
                    input.Close();
                }
            }

            break;
            #endregion InvData

        case ConfigDataType.AbilityData:
            #region AbilityData
            try
            {
                // create stream reader input
                input = File.OpenText(Path.Combine(
                                          Application.streamingAssetsPath, AbilityDataFileName));

                // populate StatNames from header row
                string   currentLine = input.ReadLine();
                string[] headings    = currentLine.Split(',');

                // ignoring the header row

                // populate values for abilityData
                currentLine = input.ReadLine();
                while (currentLine != null)
                {
                    // parse current line into name and stat values
                    // 0 name, 1 isPhysical, 2 mp, 3 noReduction, 4 modifier,
                    // 5 noMiss, 6 hitOverride, 7 noCrit

                    // 0 name
                    string[]   tokens = currentLine.Split(',');
                    BattleMode name   =
                        (BattleMode)Enum.Parse(
                            typeof(BattleMode), tokens[0]);

                    // 1 isPhyical
                    bool isPhysical = bool.Parse(tokens[1].ToLower());

                    // 2 mp
                    int?mp = null;
                    if (int.TryParse(tokens[2], out int num))
                    {
                        mp = num;
                    }
                    else
                    {
                        mp = null;
                    }

                    // 3 noReduction
                    bool noReduction = bool.Parse(tokens[3].ToLower());

                    // 4 modifier
                    float modifier = float.Parse(tokens[4]);

                    // 5 noMiss
                    bool noMiss = bool.Parse(tokens[5].ToLower());

                    // 6 hitOverride
                    int?hitOverride = null;
                    if (int.TryParse(tokens[6], out int num2))
                    {
                        hitOverride = num2;
                    }
                    else
                    {
                        hitOverride = null;
                    }

                    // 7 noCrit
                    bool noCrit = bool.Parse(tokens[7].ToLower());


                    // create a new battleAbility
                    BattleAbility ability = new BattleAbility(name, isPhysical, mp, noReduction, modifier, noMiss, hitOverride, noCrit);

                    // add to invStatData
                    abilityStatData.Add(name, ability);

                    // queue next line in csv
                    currentLine = input.ReadLine();
                }
            }
            catch (Exception e)
            {
                // send the error to the console for debugging
                Debug.Log(e);
                errorMessage = "Problem loading file. \nUsing default settings.\n";
                Debug.Log(errorMessage);

                SetAbilityDataDefaultValues();
            }
            finally
            {
                // close files that were opened
                if (input != null)
                {
                    input.Close();
                }
            }

            break;
            #endregion AbilityData

        default:
            break;
        }
    }