Exemple #1
0
        public static Stat GetStat(byte[] dat, Pointer p, bool withInfoText, StatBag bag)
        {
            Stat s = new Stat();

            s.StatID = GetInt(dat, p);
            float curVal = GetSingle(dat, p);

            s.MinValue = GetSingle(dat, p);
            s.MaxValue = GetSingle(dat, p);
            s.ForceValue(curVal);

            s.Owner = bag;
            if (withInfoText)
            {
                s.DisplayName = GetString(dat, p);
                s.Description = GetString(dat, p);
                s.Group       = GetString(dat, p);
            }

            return(s);
        }
Exemple #2
0
        /// <summary>
        /// Read an item from the database.  if @lockedBy is set to null or empty string it is assumed that you are not trying to take ownership of the loaded object and are opening it for read-only 
        /// purposes.  Setting the @lockedBy parameter to a server ID indicates that that server has ownership of the object.  Objects cannot be saved except by the lock's owner.  
        /// </summary>
        /// <param name="db"></param>
        /// <param name="item"></param>
        /// <param name="ItemId"></param>
        /// <param name="includeDeleted"></param>
        /// <param name="loadedBy"></param>
        /// <param name="tran"></param>
        /// <param name="con"></param>
        /// <returns></returns>
        public static bool Item_Load(this DB db, out ServerGameObject item, Guid ItemId, bool includeDeleted, string lockedBy, out SqlTransaction tran, out SqlConnection con)
        {
            tran = null;
            con = null;

            bool result = true;

            con = DB.GameDataConnection;
            SqlCommand cmd = DB.GetCommand(con, "Items_Get", true);

            cmd.Parameters.Add(new SqlParameter("@ItemId", ItemId));
            cmd.Parameters.Add(new SqlParameter("@includeDeleted", includeDeleted));
            cmd.Parameters.Add(new SqlParameter("@lockedBy", lockedBy));

            SqlParameter pout = new SqlParameter("@resultCode", 0);
            pout.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(pout);

            SqlDataReader reader = null;
            Guid context = Guid.Empty;
            item = null;

            try
            {
                int idColumn = 1;
                int valueColumn = 2;
                int nameColumn = 3;
                con.Open();

                tran = con.BeginTransaction(IsolationLevel.Serializable);
                cmd.Connection = con;
                cmd.Transaction = tran;

                reader = cmd.ExecuteReader();
                int count = 0;
                bool isStatic = false;

                if (reader.HasRows) // any items to read?
                {
                    ServerGameObject loadedItem = null;
                    if (reader.Read()) // read item data from Item Master Rable
                    {
                        string templateNum = reader.GetString(1);
                        loadedItem = ItemUtil.Instance.CreateItemShell(templateNum);

                        bool isDeleted = reader.GetByte(2) == 1;
                        loadedItem.IsDeleted = isDeleted;
                        // int GOT = reader.GetInt32(3); // set by template in CreateItemShell
                        loadedItem.UID = reader.GetGuid(0);
                        //loadedItem.ItemTemplate = templateNum; // set by template in CreateItemShell
                        // loadedItem.GameObjectType = (GOT)GOT;// set by template in CreateItemShell
                        loadedItem.CreatedOn = reader.GetDateTimeUtc(4);
                        //isStatic = reader.GetByte(9) == 1; // set by template in CreateItemShell
                        loadedItem.StackCount = reader.GetInt32(9);
                        loadedItem.Owner = reader.GetGuid(10);

                        if (!reader.IsDBNull(5))
                        {
                            loadedItem.OwningServer = reader.GetString(5);
                        }

                        loadedItem.Context = reader.GetGuid(6);
                        if (loadedItem.TypeHash != (uint)reader.GetInt64(7))
                        {
                            Log1.Logger("Server").Error("Tried loading game object [" + loadedItem.UID.ToString() + "]. Type hash was inconsistent with database.)");
                        }

                        if (!isStatic && !reader.IsDBNull(8))
                        {
                            byte[] binData = (byte[])reader["BinData"]; // works good if there's not a ton of data
                            loadedItem.Deserialize(binData, new Pointer());

                            /*
                            long dataSize = reader.GetBytes(8, 0, null, 0, 0);

                            byte[] buffer = new byte[1024];
                            var dataRemaining = dataSize;
                            while (dataRemaining > 0)
                            {
                                int bytesToRead = (int)(buffer.Length < dataRemaining ? buffer.Length : dataRemaining);
                                //fill the buffer
                                reader.GetBytes(1, dataSize - dataRemaining, buffer, 0, bytesToRead);
                                Util.Copy(buffer, 0, binData, (int)dataSize - (int)dataRemaining, bytesToRead);
                                dataRemaining -= bytesToRead;
                            }
                            */
                        }
                    }

                    //if (!isStatic)
                    {
                        reader.NextResult(); // grab item properties
                        bool attribsDone = false;

                        while (!attribsDone)
                        {
                            //reader.NextResult();
                            count++;

                            if (count == 5)
                            {
                                // Finished reading item attributes.  Go on to the next item.
                                attribsDone = true;
                            }

                            if (!reader.HasRows)
                            {
                                reader.NextResult();
                                continue;
                            }

                            IGameObject ci = loadedItem;
                            switch (count)
                            {
                                case 1: // Float
                                    while (reader.Read())
                                    {
                                        string name = reader.IsDBNull(nameColumn) ? "" : reader.GetString(nameColumn);
                                        ci.Properties.SetProperty(name, (int)reader.GetInt32(idColumn), (float)reader.GetDouble(valueColumn));
                                    }
                                    break;
                                case 2: // Int
                                    while (reader.Read())
                                    {
                                        string name = reader.IsDBNull(nameColumn) ? "" : reader.GetString(nameColumn);
                                        ci.Properties.SetProperty(name, (int)reader.GetInt32(idColumn), reader.GetInt32(valueColumn));
                                    }
                                    break;
                                case 3: // Long
                                    while (reader.Read())
                                    {
                                        string name = reader.IsDBNull(nameColumn) ? "" : reader.GetString(nameColumn);
                                        ci.Properties.SetProperty(name, (int)reader.GetInt32(idColumn), reader.GetInt64(valueColumn));
                                    }
                                    break;
                                case 4: // String
                                    while (reader.Read())
                                    {
                                        string name = reader.IsDBNull(nameColumn) ? "" : reader.GetString(nameColumn);
                                        ci.Properties.SetProperty(name, (int)reader.GetInt32(idColumn), reader.GetString(valueColumn));
                                    }
                                    break;
                                case 5: // Stats
                                    //reader.NextResult(); // read past the Item id
                                    while (reader.Read())
                                    {
                                        Stat s = new Stat();
                                        s.StatID = reader.GetInt32(reader.GetOrdinal("StatID"));

                                        double cValue = reader.GetDouble(reader.GetOrdinal("CurrentValue"));
                                        double mValue = reader.GetDouble(reader.GetOrdinal("MaxValue"));
                                        double mnValue = reader.GetDouble(reader.GetOrdinal("MinValue"));

                                        Stat proto = StatManager.Instance[s.StatID];
                                        if (proto == null)
                                        {
                                            Log1.Logger("Server.Stats").Error("Item_Load attempted to read stat ID [" + s.StatID + "] which was not defined in the Stats.xml config file. Stat not added to Item.");
                                            continue;
                                        }

                                        s.MinValue = (float)mnValue;
                                        s.MaxValue = (float)mValue;
                                        s.ForceValue((float)cValue);

                                        s.Description = proto.Description;
                                        s.DisplayName = proto.DisplayName;
                                        s.Group = proto.Group;

                                        ci.Stats.AddStat(s);
                                    }
                                    break;
                            }

                            reader.NextResult();
                        } // while reader has next result for item properties
                    } // if not static item
                    item = loadedItem;
                } // while reader has rows
            }
            catch (Exception e)
            {
                Log1.Logger("Server").Error("[DATABASE ERROR Item_Load] : " + e.Message);
                result = false;
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                {
                    reader.Close();
                }
            }
            return result;
        }
Exemple #3
0
        public static bool LoadTemplate(XPathDocument docNav)
        {
            Template t = new Template();
            bool     isValidTemplate = false;

            try
            {
                // Create a navigator to query with XPath.
                XPathNavigator nav = docNav.CreateNavigator();

                XPathNavigator root = nav.SelectSingleNode("//Item");

                t.Description = root.GetAttribute("Name", "");
                t.Class       = root.GetAttribute("Class", "");

                bool isStatic = true;
                if (!bool.TryParse(root.GetAttribute("IsStatic", ""), out isStatic))
                {
                    isStatic = true; // default to static item
                }
                t.IsStatic = isStatic;

                bool isTransient = true;
                if (!bool.TryParse(root.GetAttribute("IsTransient", ""), out isTransient))
                {
                    isTransient = true; // default to Transient, non-persisting item
                }
                t.IsTransient = isTransient;

                int stackCount = 1;
                if (!int.TryParse(root.GetAttribute("StackCount", ""), out stackCount))
                {
                    stackCount = 1; // default to Transient, non-persisting item
                }
                t.StackCount = stackCount;

                if (t.Class.Length < 1)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Class (fully qualified type name) could not be determined. Make sure that the Class attribute is defined.]");
                    goto bailout;
                }

                GOT    got    = GOT.None;
                string gotStr = root.GetAttribute("GOT", "");
                try
                {
                    got = (GOT)Enum.Parse(typeof(GOT), gotStr);
                }
                catch { }

                if (got == GOT.None)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [GOT (Game object type) could not be determined. Make sure that GOT value exists in GOT enum cs file.]");
                    goto bailout;
                }

                t.GameObjectType = got;

                string            strExpression = "//Item/Properties/*";
                XPathNodeIterator NodeIter      = nav.Select(strExpression);

                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if (name.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Property Name attribute is missing.]");
                        goto bailout;
                    }

                    string stringValue = NodeIter.Current.GetAttribute("StringValue", "");
                    string intValue    = NodeIter.Current.GetAttribute("IntValue", "");
                    string floatValue  = NodeIter.Current.GetAttribute("FloatValue", "");
                    string longValue   = NodeIter.Current.GetAttribute("LongValue", "");

                    if (stringValue.Length < 1 && intValue.Length < 1 && floatValue.Length < 1 && longValue.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must set either StringValue, IntValue, FloatValue or LongValue for property " + name + ".]");
                        goto bailout;
                    }

                    if (stringValue.Length > 0)
                    {
                        t.Properties.SetProperty(name, stringValue);
                    }
                    else if (floatValue.Length > 1)
                    {
                        try
                        {
                            float val = float.Parse(floatValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for FloatValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (intValue.Length > 0)
                    {
                        try
                        {
                            int val = int.Parse(intValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for IntValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (longValue.Length > 0)
                    {
                        try
                        {
                            long val = long.Parse(longValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for LongValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                }

                strExpression = "//Item/Stats/*";
                NodeIter      = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string id = NodeIter.Current.GetAttribute("StatID", "");

                    string value = NodeIter.Current.GetAttribute("StartValue", "");
                    if (value.Length < 1)
                    {
                        value = "0";
                    }

                    float val       = float.Parse(value);
                    Stat  prototype = StatManager.Instance.AllStats.GetStat(id);
                    if (prototype == null)
                    {
                        Log.LogMsg("Failed to load stat [" + id + "] for template [" + t.Description + "]. Make sure stat is defiend in Stats.xml.");
                        continue;
                    }

                    Stat ns = new Stat(
                        prototype.StatID,
                        prototype.DisplayName,
                        prototype.Description,
                        prototype.Group,
                        prototype.CurrentValue,
                        prototype.MinValue,
                        prototype.MaxValue);
                    ns.ForceValue(val);
                    t.Stats.AddStat(ns);
                }

                strExpression = "//Item/Scripts/*";
                NodeIter      = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if (!name.StartsWith("Scripts."))
                    {
                        name = "Scripts." + name;
                    }

                    if (name.Length > 0)
                    {
                        GameObjectScript s = GameObjectScript.GetScript(Factory.GetStableHash(name));
                        if (s == null)
                        {
                            Log.LogMsg("Unable to instantiate script [" + name + "] on template [" + docNav.ToString() + "]. Factory couldn't create script. Was it registered using Factor.Register()?");
                        }
                        else
                        {
                            t.Scripts.Add(Factory.GetStableHash(name));
                        }
                    }
                }

                isValidTemplate = true;
            }
            catch (Exception e)
            {
                isValidTemplate = false;
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [" + e.Message + "]");
            }

bailout:
            if (isValidTemplate)
            {
                t.Name = t.Description.ToLower();// Path.GetFileNameWithoutExtension(template).ToLower();
                Templates.Add(t.Name, t);
                //Log1.Logger("Server").Info("Loaded template [" + Path.GetFileNameWithoutExtension(template) + "]");
            }
            else
            {
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "]");
            }

            return(isValidTemplate);
        }
Exemple #4
0
        public static bool LoadTemplate(XPathDocument docNav)
        {
            Template t = new Template();
            bool isValidTemplate = false;
            try
            {
                // Create a navigator to query with XPath.
                XPathNavigator nav = docNav.CreateNavigator();

                XPathNavigator root = nav.SelectSingleNode("//Item");

                t.Description = root.GetAttribute("Name", "");
                t.Class = root.GetAttribute("Class", "");

                bool isStatic = true;
                if (!bool.TryParse(root.GetAttribute("IsStatic", ""), out isStatic))
                {
                    isStatic = true; // default to static item
                }
                t.IsStatic = isStatic;

                bool isTransient = true;
                if (!bool.TryParse(root.GetAttribute("IsTransient", ""), out isTransient))
                {
                    isTransient = true; // default to Transient, non-persisting item
                }
                t.IsTransient = isTransient;

                int stackCount = 1;
                if (!int.TryParse(root.GetAttribute("StackCount", ""), out stackCount))
                {
                    stackCount = 1; // default to Transient, non-persisting item
                }
                t.StackCount = stackCount;

                if (t.Class.Length < 1)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Class (fully qualified type name) could not be determined. Make sure that the Class attribute is defined.]");
                    goto bailout;
                }

                GOT got = GOT.None;
                string gotStr = root.GetAttribute("GOT", "");
                try
                {
                    got = (GOT)Enum.Parse(typeof(GOT), gotStr);
                }
                catch { }

                if (got == GOT.None)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [GOT (Game object type) could not be determined. Make sure that GOT value exists in GOT enum cs file.]");
                    goto bailout;
                }

                t.GameObjectType = got;

                string strExpression = "//Item/Properties/*";
                XPathNodeIterator NodeIter = nav.Select(strExpression);

                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if (name.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Property Name attribute is missing.]");
                        goto bailout;
                    }

                    string stringValue = NodeIter.Current.GetAttribute("StringValue", "");
                    string intValue = NodeIter.Current.GetAttribute("IntValue", "");
                    string floatValue = NodeIter.Current.GetAttribute("FloatValue", "");
                    string longValue = NodeIter.Current.GetAttribute("LongValue", "");

                    if (stringValue.Length < 1 && intValue.Length < 1 && floatValue.Length < 1 && longValue.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must set either StringValue, IntValue, FloatValue or LongValue for property " + name + ".]");
                        goto bailout;
                    }

                    if (stringValue.Length > 0)
                    {
                        t.Properties.SetProperty(name, stringValue);
                    }
                    else if (floatValue.Length > 1)
                    {
                        try
                        {
                            float val = float.Parse(floatValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for FloatValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (intValue.Length > 0)
                    {
                        try
                        {
                            int val = int.Parse(intValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for IntValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (longValue.Length > 0)
                    {
                        try
                        {
                            long val = long.Parse(longValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for LongValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                }

                strExpression = "//Item/Stats/*";
                NodeIter = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string id = NodeIter.Current.GetAttribute("StatID", "");

                    string value = NodeIter.Current.GetAttribute("StartValue", "");
                    if (value.Length < 1)
                    {
                        value = "0";
                    }

                    float val = float.Parse(value);
                    Stat prototype = StatManager.Instance.AllStats.GetStat(id);
                    if (prototype == null)
                    {
                        Log.LogMsg("Failed to load stat [" + id + "] for template [" + t.Description + "]. Make sure stat is defiend in Stats.xml.");
                        continue;
                    }

                    Stat ns = new Stat(
                        prototype.StatID,
                        prototype.DisplayName,
                        prototype.Description,
                        prototype.Group,
                        prototype.CurrentValue,
                        prototype.MinValue,
                        prototype.MaxValue);
                    ns.ForceValue(val);
                    t.Stats.AddStat(ns);
                }

                strExpression = "//Item/Scripts/*";
                NodeIter = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if(!name.StartsWith("Scripts."))
                    {
                        name = "Scripts." + name;
                    }

                    if (name.Length > 0)
                    {
                        GameObjectScript s = GameObjectScript.GetScript(Factory.GetStableHash(name));
                        if (s == null)
                        {
                            Log.LogMsg("Unable to instantiate script [" + name + "] on template [" + docNav.ToString() + "]. Factory couldn't create script. Was it registered using Factor.Register()?");
                        }
                        else
                        {
                            t.Scripts.Add(Factory.GetStableHash(name));
                        }
                    }
                }

                isValidTemplate = true;
            }
            catch (Exception e)
            {
                isValidTemplate = false;
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [" + e.Message + "]");
            }

            bailout:
            if (isValidTemplate)
            {
                t.Name = t.Description.ToLower();// Path.GetFileNameWithoutExtension(template).ToLower();
                Templates.Add(t.Name, t);
                //Log1.Logger("Server").Info("Loaded template [" + Path.GetFileNameWithoutExtension(template) + "]");
            }
            else
            {
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "]");
            }

            return isValidTemplate;
        }
Exemple #5
0
        public static Stat GetStat(byte[] dat, Pointer p, bool withInfoText, StatBag bag)
        {
            Stat s = new Stat();
            s.StatID = GetInt(dat, p);
            float curVal = GetSingle(dat, p);
            s.MinValue = GetSingle(dat, p);
            s.MaxValue = GetSingle(dat, p);
            s.ForceValue(curVal);

            s.Owner = bag;
            if (withInfoText)
            {
                s.DisplayName = GetString(dat, p);
                s.Description = GetString(dat, p);
                s.Group = GetString(dat, p);
            }

            return s;
        }