Beispiel #1
0
        public static DBConnect GetInstance()
        {
            if (instance == null)
            {
                instance = new DBConnect();
                instance.OpenConnection();
            }

            return instance;
        }
        // @TODO: to rewrite ASAP!! just for test
        // I know, it it TOTAL MESS
        public void LoadFromDB(int entryorguid)
        {
            this.entryorguid = entryorguid;
            DBConnect connect = new DBConnect();
            bool opened = connect.OpenConnection();

            if (!opened)
            {
                return;
            }

            events.Clear();

            MySql.Data.MySqlClient.MySqlCommand cmd = connect.Query("SELECT * FROM conditions WHERE sourceentry = " + entryorguid + " and sourceid=0 and sourcetypeorreferenceid=22");
            Dictionary<int, List<SmartCondition>> conditions = new Dictionary<int, List<SmartCondition>>();
            
            using (MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader())
            {
                int prevelsegroup = 0;
                while (reader.Read())
                {
                    int id = Convert.ToInt32(reader["sourcegroup"]) - 1;

                    if (!conditions.ContainsKey(id))
                        conditions.Add(id, new List<SmartCondition>());

                    if (Convert.ToInt32(reader["ElseGroup"]) != prevelsegroup)
                        conditions[id].Add(new CONDITION_LOGICAL_OR());

                    SmartCondition cond = SmartFactory.GetInstance().ConditionFactory(Convert.ToInt32(reader["ConditionTypeOrReference"]));
                    cond.UpdateParams(0,(Convert.ToInt32(reader["ConditionValue1"])));
                    cond.UpdateParams(1,(Convert.ToInt32(reader["ConditionValue2"])));
                    cond.UpdateParams(2,(Convert.ToInt32(reader["ConditionValue3"])));
                    cond.invert = (Convert.ToInt32(reader["NegativeCondition"]) == 1);


                    conditions[id].Add(cond);
                    prevelsegroup = Convert.ToInt32(reader["ElseGroup"]);
                }
            }

            cmd = connect.Query("SELECT * FROM smart_scripts WHERE source_type = "+(int)Type+" and entryorguid = "+entryorguid + " order by id");
            SmartEvent prev = null;
            bool keep_legacy_comments = false;
            bool keep_lagacy_comments_asked = false;
            using (MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader())
            {
                int next_link = -1;
                while (reader.Read())
                {
                    //(`entryorguid`,`source_type`,`id`,`link`,`event_type`,`event_phase_mask`,`event_chance`,`event_flags`,`event_param1`,`event_param2`,`event_param3`,`event_param4`,`action_type`,`action_param1`,`action_param2`,`action_param3`,`action_param4`,`action_param5`,`action_param6`,`target_type`,`target_param1`,`target_param2`,`target_param3`,`target_x`,`target_y`,`target_z`,`target_o`,`comment`)
                    int id = Convert.ToInt32(reader["id"]);
                    int entry = Convert.ToInt32(reader["entryorguid"]);
                    string comment = Convert.ToString(reader["comment"]);
                    SmartAction a = SmartFactory.GetInstance().ActionFactory(Convert.ToInt32(reader["action_type"]));
                    SmartTarget target = SmartFactory.GetInstance().TargetFactory(Convert.ToInt32(reader["target_type"]));

                    for (int i = 0; i < 6; i++)
                        a.UpdateParams(i, Convert.ToInt32(reader["action_param" + (i + 1)]));

                    for (int i = 0; i < 3; i++)
                        target.UpdateParams(i, Convert.ToInt32(reader["target_param" + (i + 1)]));

                    target.position[0] = (float)Convert.ToDouble(reader["target_x"]);
                    target.position[1] = (float)Convert.ToDouble(reader["target_y"]);
                    target.position[2] = (float)Convert.ToDouble(reader["target_z"]);
                    target.position[3] = (float)Convert.ToDouble(reader["target_o"]);

                    a.Target = target;


                    if (comment.IndexOf(" // ") > -1)
                        a.Comment = comment.Substring(comment.IndexOf(" // ") + 4);
                    else if (!Properties.Settings.Default.DiscardLegacyComments)
                    {
                        if (!keep_lagacy_comments_asked)
                        {
                            DialogResult res =
                              PSTaskDialog.cTaskDialog.ShowTaskDialogBox("Legacy comments",
                                                        "Legacy comments",
                                                        "Visual SAI Studio has detected script you loaded doesn't have comments created with SAI Studio.\n",
                                                        "",
                                                        "",
                                                        "Never propose keeping legacy comments",
                                                        "",
                                                        "Keep legacy comments|Discard legacy comments",
                                                        PSTaskDialog.eTaskDialogButtons.Cancel,
                                                        PSTaskDialog.eSysIcons.Question, PSTaskDialog.eSysIcons.Information);
                            if (PSTaskDialog.cTaskDialog.VerificationChecked)
                                Properties.Settings.Default.DiscardLegacyComments = true;
                            if (PSTaskDialog.cTaskDialog.CommandButtonResult == 0)
                                keep_legacy_comments = true;
                            keep_lagacy_comments_asked = true;
                        }

                        if (keep_legacy_comments)
                            a.Comment = comment;
                    }

                    if (id == next_link)
                    {
                        prev.AddAction(a);
                    }
                    else
                    {
                        SmartEvent ev = SmartFactory.GetInstance().EventFactory(Convert.ToInt32(reader["event_type"]));
                        ev.chance = Convert.ToInt32(reader["event_chance"]);
                        ev.flags = (SmartEventFlag)Convert.ToInt32(reader["event_flags"]);
                        ev.phasemask = (SmartPhaseMask)Convert.ToInt32(reader["event_phase_mask"]);
                        ev.UpdateParams(0, Convert.ToInt32(reader["event_param1"]));
                        ev.UpdateParams(1, Convert.ToInt32(reader["event_param2"]));
                        ev.UpdateParams(2, Convert.ToInt32(reader["event_param3"]));
                        ev.UpdateParams(3, Convert.ToInt32(reader["event_param4"]));
                        if (conditions.ContainsKey(id))
                        {
                            foreach(SmartCondition cond in conditions[id])
                            {
                                ev.AddCondition(cond);
                            }
                        }

                        ev.AddAction(a);
                        events.Add(ev);
                        prev = ev;
                    }


                    next_link = Convert.ToInt32(reader["link"]);
                }
            }
            connect.CloseConnection();
            scratch1.Refresh();
        }
Beispiel #3
0
        public void LoadAll()
        {
            CurrentAction(this, new LoadingEventArgs("actions"));
            Load(SmartScripts.SmartType.SMART_ACTION, "data/actions.json");

            CurrentAction(this, new LoadingEventArgs("events"));
            Load(SmartScripts.SmartType.SMART_EVENT, "data/events.json");

            CurrentAction(this, new LoadingEventArgs("targets"));
            Load(SmartScripts.SmartType.SMART_TARGET, "data/targets.json");

            CurrentAction(this, new LoadingEventArgs("conditions"));
            Load(SmartScripts.SmartType.SMART_CONDITION, "data/conditions.json");

            CurrentAction(this, new LoadingEventArgs("quests"));
            dbString.Add(StorageType.Quest, new ClientDataDB <string>("title", "quest_template"));

            CurrentAction(this, new LoadingEventArgs("creatures"));
            dbString.Add(StorageType.Creature, new ClientDataDB <string>("entry", "name", "creature_template"));
            dbString.Add(StorageType.CreatureEntryWithAI, new ClientDataDB <string>("entry", "ScriptName", "creature_template", "AIName = \"SmartAI\""));

            //dbInt.Add(StorageType.CreatureGuid, new ClientDataDB<int>("guid", "entry", "creature"));



            MySql.Data.MySqlClient.MySqlCommand cmd = DBConnect.GetInstance().Query(String.Format("SELECT guid, concat(name, ' ', creature.id) as _name, count(source_type) as smartAI FROM creature left join smart_scripts on source_type=0 and entryorguid=(-guid) join creature_template on creature_template.entry=creature.id group by guid "));
            dbString.Add(StorageType.CreatureGuid, new ClientData <String>());
            dbString.Add(StorageType.CreatureGuidWithSAI, new ClientData <String>());
            if (cmd != null)
            {
                using (MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        dbString[StorageType.CreatureGuid].Set(Convert.ToInt32(reader["guid"]), Convert.ToString(reader["_name"]));
                        if (Convert.ToInt32(reader["smartAI"]) > 0)
                        {
                            dbString[StorageType.CreatureGuidWithSAI].Set(Convert.ToInt32(reader["guid"]), Convert.ToString(reader["_name"]));
                        }
                    }
                }
            }


            CurrentAction(this, new LoadingEventArgs("gameobjects"));
            dbString.Add(StorageType.GameObject, new ClientDataDB <string>("entry", "name", "gameobject_template"));
            dbString.Add(StorageType.GameObjectEntryWithAI, new ClientDataDB <string>("entry", "ScriptName", "gameobject_template", "AIName = \"SmartGameObjectAI\""));


            cmd = DBConnect.GetInstance().Query(String.Format("SELECT guid, concat(name, ' ', gameobject.id) as _name, count(source_type) as smartAI FROM gameobject left join smart_scripts on source_type=1 and entryorguid=(-guid) join gameobject_template on gameobject_template.entry=gameobject.id group by guid "));
            dbString.Add(StorageType.GameObjectGuid, new ClientData <String>());
            dbString.Add(StorageType.GameObjectGuidWithAI, new ClientData <String>());
            if (cmd != null)
            {
                using (MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        dbString[StorageType.GameObjectGuid].Set(Convert.ToInt32(reader["guid"]), Convert.ToString(reader["_name"]));
                        if (Convert.ToInt32(reader["smartAI"]) > 0)
                        {
                            dbString[StorageType.GameObjectGuidWithAI].Set(Convert.ToInt32(reader["guid"]), Convert.ToString(reader["_name"]));
                        }
                    }
                }
            }


            cmd = DBConnect.GetInstance().Query(String.Format("SELECT entryorguid FROM smart_scripts where source_type=9 group by entryorguid"));
            dbString.Add(StorageType.TimedActionList, new ClientData <String>());
            if (cmd != null)
            {
                using (MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        dbString[StorageType.TimedActionList].Set(Convert.ToInt32(reader["entryorguid"]), null);
                    }
                }
            }

            CurrentAction(this, new LoadingEventArgs("area triggers"));
            dbString.Add(StorageType.AreaTrigger, new ClientDataDBC("AreaTrigger.dbc",
                                                                    delegate(BinaryReader br, Dictionary <int, string> strings)
            {
                StringBuilder sb = new StringBuilder();
                br.ReadInt32();
                sb.Append("(");
                sb.Append(br.ReadSingle());
                sb.Append(", ");
                sb.Append(br.ReadSingle());
                sb.Append(", ");
                sb.Append(br.ReadSingle());
                sb.Append(")");
                return(sb.ToString());
            }
                                                                    ));
            dbString.Add(StorageType.AreaTriggerWithSAI, new ClientDataDB <string>("entry", "ScriptName", "areatrigger_scripts", "ScriptName = \"SmartTrigger\""));

            CurrentAction(this, new LoadingEventArgs("game events"));
            dbString.Add(StorageType.GameEvent, new ClientDataDB <string>("EventEntry", "description", "game_event"));



            Dictionary <String, DBCConfig> dbc_config = JsonConvert.DeserializeObject <Dictionary <String, DBCConfig> >(File.ReadAllText(@"data\dbc.json"));

            if (string.IsNullOrEmpty(Properties.Settings.Default.DBCVersion))
            {
                Properties.Settings.Default.DBCVersion = GuessDBCVersion(dbc_config);
                Properties.Settings.Default.Save();
            }

            if (!string.IsNullOrEmpty(Properties.Settings.Default.DBCVersion))
            {
                foreach (StorageType type in dbc_config[Properties.Settings.Default.DBCVersion].offsets.Keys)
                {
                    if (dbc_config[Properties.Settings.Default.DBCVersion].offsets[type].unsupported)
                    {
                        continue;
                    }
                    CurrentAction(this, new LoadingEventArgs(type.ToString()));
                    dbString.Add(type, new ClientDataDBC(dbc_config[Properties.Settings.Default.DBCVersion].offsets[type].file, dbc_config[Properties.Settings.Default.DBCVersion].offsets[type].offset));
                }
            }



            FinishedLoading(this, new EventArgs());
        }