Example #1
0
        private static void LoadTypes()
        {
            Debug.Log("Loading Types...");
            Dictionary <Pokemon.Types, Pokemon.Type> types = new Dictionary <Pokemon.Types, Pokemon.Type>();
            string types_path = Settings.TYPES_FILE_PATH;

            string[] type_files = Directory.GetFiles(types_path, "*.asset");
            foreach (string file in type_files)
            {
                string        local_path = Path.Combine(Path.GetFileName(Path.GetDirectoryName(types_path)), Path.GetFileName(types_path));
                string        file_path  = Path.Combine(local_path, Path.GetFileNameWithoutExtension(file));
                Pokemon.Type  type_obj   = Resources.Load <Pokemon.Type>(file_path);
                string        type_name  = FileNameToEnumEntry(type_obj.name);
                Pokemon.Types type       = (Pokemon.Types)Enum.Parse(typeof(Pokemon.Types), type_name, true);
                types[type] = type_obj;
            }

            Pokemon.Type.types = types;
            Debug.Log(Pokemon.Type.types.Count.ToString() + " Type files loaded.");
        }
        public void TypesImport()
        {
            string file_path = types_input.GetComponentsInChildren <Text>()[1].text;
            string full_path = Path.Combine(import_path, file_path);

            imported_types = new List <Pokemon.Type>();

            try
            {
                StreamReader input_stream = new StreamReader(full_path);
                Pokemon.Type current_type = null;

                while (!input_stream.EndOfStream)
                {
                    string line = Regex.Replace(input_stream.ReadLine(), @"\s+", "");

                    // Line is a comment
                    if (line[0] == '#')
                    {
                    }
                    // Line is start of a new type
                    else if (line[0] == '[')
                    {
                        if (current_type != null)
                        {
                            imported_types.Add(current_type);
                        }

                        current_type = ScriptableObject.CreateInstance <Pokemon.Type>();
                    }
                    // Line contains data field
                    else
                    {
                        string[] parts = line.Split('=');
                        string   key   = parts[0];
                        string   value = parts[1];

                        // Part is name
                        if (key == "Name")
                        {
                            current_type.name = value;
                        }
                        // Part is weaknesses list
                        else if (key == "Weaknesses")
                        {
                            string[]        values         = value.Split(',');
                            int             num_weaknesses = values.Length;
                            Pokemon.Types[] weaknesses     = new Pokemon.Types[num_weaknesses];
                            for (int i = 0; i < num_weaknesses; i++)
                            {
                                weaknesses[i] = (Pokemon.Types)Enum.Parse(typeof(Pokemon.Types), values[i], true);
                            }
                            current_type.weaknesses = weaknesses;
                        }
                        // Part is resistances list
                        else if (key == "Resistances")
                        {
                            string[]        values          = value.Split(',');
                            int             num_resistances = values.Length;
                            Pokemon.Types[] resistances     = new Pokemon.Types[num_resistances];
                            for (int i = 0; i < num_resistances; i++)
                            {
                                resistances[i] = (Pokemon.Types)Enum.Parse(typeof(Pokemon.Types), values[i], true);
                            }
                            current_type.resistances = resistances;
                        }
                        // Part is immunities list
                        else if (key == "Immunities")
                        {
                            string[]        values         = value.Split(',');
                            int             num_immunities = values.Length;
                            Pokemon.Types[] immunities     = new Pokemon.Types[num_immunities];
                            for (int i = 0; i < num_immunities; i++)
                            {
                                immunities[i] = (Pokemon.Types)Enum.Parse(typeof(Pokemon.Types), values[i], true);
                            }
                            current_type.immunities = immunities;
                        }
                    }
                }

                if (current_type != null)
                {
                    imported_types.Add(current_type);
                }
                input_stream.Close();
            }
            catch (Exception e)
            {
                Debug.Log(e);
                return;
            }

            string types_path = Settings.TYPES_FILE_PATH;

            for (int i = 0; i < imported_types.Count; i++)
            {
                Pokemon.Type new_type = imported_types[i];

                AssetDatabase.CreateAsset(new_type, Path.Combine(types_path, new_type.name) + ".asset");
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                EditorUtility.FocusProjectWindow();
                Selection.activeObject = new_type;
            }
        }