示例#1
0
 public CachedData(int offset, ICacheableObject data)
 {
     Offset   = offset;
     Size     = data.Size;
     Checksum = data.GetHashCode();
     Object   = data;
 }
示例#2
0
 public CachedData(int offset, int size, int checksum)
 {
     Offset   = offset;
     Size     = size;
     Checksum = checksum;
     Object   = null;
 }
示例#3
0
        public static CachedData GetData(ICacheableObject data)
        {
            var key = data.GetHashCode();

            if (m_buffers.ContainsKey(key))
            {
                return(m_buffers[key]);
            }

            return(CachedData.Empty);
        }
示例#4
0
        /// <summary>
        /// Delete an object from the filesystem
        /// </summary>
        /// <param name="obj">The object to delete</param>
        /// <returns>Whether the save action succeeded</returns>
        internal static bool DeleteObject(ICacheableObject obj)
        {
            try
            {
                var fileName = obj.Prototype.ToString() + ".json";
                var delPath  = string.Format("{0}\\{1}\\", PersistencePath, obj.GetType().ToString());

                File.Delete(delPath + fileName);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#5
0
        /// <summary>
        /// Saves an object to the filesystem in json format
        /// </summary>
        /// <param name="obj">The object to serialize and save</param>
        /// <param name="rootFolderPath">The path of the root world folder</param>
        /// <param name="objName">Optional name to use for saving the file in the format of objName.json</param>
        /// <returns>Whether the save action succeeded</returns>
        /// <remarks>The object will be saved under the appropriate folder given its type and the master ID store will be updated.</remarks>
        public static bool SaveObject(ICacheableObject obj)
        {
            try
            {
                var fileName   = obj.Prototype.ToString() + ".json";
                var savePath   = string.Format("{0}\\{1}\\", PersistencePath, obj.GetType().ToString());
                var serialized = JsonConvert.SerializeObject(obj, Formatting.Indented);

                if (!Directory.Exists(savePath))
                {
                    Directory.CreateDirectory(savePath);
                }

                File.WriteAllText(savePath + fileName, serialized);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#6
0
        /// <summary>
        /// Adds an object to the cache. You MUST first check to see if the cache contains a duplicate key.
        /// </summary>
        /// <param name="entity">The object to add.</param>
        /// <param name="specificID">The specific ID to add. If -1, find first available ID.</param>
        /// <returns>The ID of the added object</returns>
        public uint Add(ICacheableObject entity, CacheType cacheType, uint?specificID = null, bool persist = false)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(Add));
            }

            uint id;

            // Consume the appropriate ID
            if (specificID == null)
            {
                id = ConsumeFirstAvailableID();
            }
            else
            {
                id = ConsumeSpecifiedID((uint)specificID);
            }

            _cached_objects.Add(id, entity);

            if (cacheType == CacheType.Instance)
            {
                entity.Instance = id;
            }
            else
            {
                entity.Prototype = id;
            }

            entity.CacheType = cacheType;

            if (persist)
            {
                DataPersistence.SaveObject(entity);
            }

            return(id);
        }
示例#7
0
        public static void Cache(int offset, ICacheableObject data)
        {
            var entry = new CachedData(offset, data);

            m_buffers.Add(entry.Checksum, entry);
        }
示例#8
0
        public static bool IsCached(ICacheableObject data)
        {
            var key = data.GetHashCode();

            return(m_buffers.ContainsKey(key));
        }