Ejemplo n.º 1
0
        /// <summary>
        /// Tries to serialize the object fully to whichever storage medium is needed.
        /// </summary>
        /// <param name="objectKey">The data key to write i.e. character/</param>
        /// <param name="dataToSerialize">object data to write</param>
        /// <returns>true for success, false for failure</returns>
        public bool writeObjectToDestination(string objectKey, GameDataStorageObject dataToSerialize)
        {
            bool successfulWrite = false;

            switch (this.dataAccessType)
            {
            case GameDataStorageLayerUtils.DataStorageAreas.Network:

                successfulWrite = true;
                break;

            case GameDataStorageLayerUtils.DataStorageAreas.FileSystem:
                try
                {
                    XmlSerializer x = new System.Xml.Serialization.XmlSerializer(dataToSerialize.GetType());
                    x.Serialize(new StreamWriter(this.locationString), dataToSerialize);
                    successfulWrite = true;
                } catch (Exception e)
                {
                    BaseGameDataStorageLayer.logData("Unable to serialize object to destinaton {0} due to {1}" + this.locationString + e.Message, GameDataStorageLayerUtils.LogLevels.Error);
                }

                break;

            default:
                return(false);
            }

            return(successfulWrite);
        }
 /// <summary>
 /// Try to fetch the tuple at the index specified, return null if nothing was found or we ran into an exception.
 /// </summary>
 /// <param name="index">Index of an attribute</param>
 /// <returns>Tuple<string,int> if no exception is reached, null otherwise.</returns>
 public Tuple <DataType1, DataType2> getValueAt(int index)
 {
     try
     {
         return(dataList[index]);
     }
     catch (Exception ex)
     {
         BaseGameDataStorageLayer.logData("Error accessing attributeList {0}" + ex.Message, GameDataStorageLayerUtils.LogLevels.Error);
     }
     return(null);
 }
 /// <summary>
 /// Empty the list.
 /// </summary>
 /// <returns>True if successful, otherwise false.</returns>
 public bool removeAllElements()
 {
     try
     {
         dataList.RemoveRange(0, dataList.Count);
         return(true);
     } catch (Exception ex)
     {
         BaseGameDataStorageLayer.logData("Failed to clear out all elements in the list due to {0}" + ex.Message, GameDataStorageLayerUtils.LogLevels.Error);
         return(false);
     }
 }
 /// <summary>
 /// Try to remove an item from the list at index
 /// </summary>
 /// <param name="index">Index to remove the element at.</param>
 /// <returns>The removed element. Or null if no element.</returns>
 public Tuple <DataType1, DataType2> removeItemAt(int index)
 {
     try
     {
         Tuple <DataType1, DataType2> removedElement = dataList[index];
         dataList.RemoveAt(index);
         return(removedElement);
     } catch (Exception ex)
     {
         BaseGameDataStorageLayer.logData("Unable to remove element due to {0}" + ex.Message, GameDataStorageLayerUtils.LogLevels.Error);
     }
     return(null);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Prepare to store the object, we may have to synchonize before we write
 /// We will first check for locks and then write data as needed.
 /// </summary>
 /// <param name="objectToWrite">All Game objects to write in the dictionary.</param>
 public void prepareToStoreObject(ConcurrentDictionary <string, GameDataStorageObject> objectToWrite)
 {
     try
     {
         foreach (KeyValuePair <string, GameDataStorageObject> kvpair in objectToWrite)
         {
             writeObjectToDestination(kvpair.Key, kvpair.Value);
         }
     }
     catch (Exception e)
     {
         string exception = "Error unable to try to store object due to {0}" + e.Message;
         BaseGameDataStorageLayer.logData(exception, GameDataStorageLayerUtils.LogLevels.Error);
     }
 }
        /// <summary>
        /// Adds a Tuple object to the end of the list.
        /// </summary>
        /// <param name="insertedData">Tuple of attribute data.</param>
        /// <returns>Return success on successful insert, else false and log an error</returns>
        public bool addTupleToList(Tuple <DataType1, DataType2> insertedData)
        {
            bool insertSuccessful = false;

            try
            {
                dataList.Add(insertedData);
                insertSuccessful = true;
            }
            catch (Exception ex)
            {
                BaseGameDataStorageLayer.logData("Unable to insert into the list due to {0}" + ex.Message, GameDataStorageLayerUtils.LogLevels.Error);
            }
            return(insertSuccessful);
        }
        /// <summary>
        /// Insert a tuple at the index specified, if we don't want to replace something we'll add it to the end instead.
        /// </summary>
        /// <param name="tupleData">Data we want to add to the list.</param>
        /// <param name="index">Index where we want to add it at.</param>
        /// <param name="replace">Whether we want to replace the object at index, defaultly true.</param>
        /// <returns>Returns boolean saying whether we were succesful or not.</returns>
        public bool insertTupleAt(Tuple <DataType1, DataType2> tupleData, int index, bool replace = true)
        {
            bool insertSuccessful = false;

            if (!replace)
            {
                return(addTupleToList(tupleData));
            }

            try
            {
                dataList.Insert(index, tupleData);
                insertSuccessful = true;
            } catch (Exception ex)
            {
                BaseGameDataStorageLayer.logData("Unable to insert data into the list due to {0}" + ex.Message, GameDataStorageLayerUtils.LogLevels.Error);
            }
            return(insertSuccessful);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Open the data on the specified area and location (file name/hostname:uri)
        /// </summary>
        /// <returns>Boolean: true on successful opening and retrieving of storage data, false on failure.</returns>
        public bool openData()
        {
            if (dataAccessType == GameDataStorageLayerUtils.DataStorageAreas.None || dataType == null)
            {
                BaseGameDataStorageLayer.logData("Unable to find location or dataType.", GameDataStorageLayerUtils.LogLevels.Fatal);
                throw new System.ArgumentException("Storage location and dataTypes need to be defined.");
            }

            if (dataAccessType == GameDataStorageLayerUtils.DataStorageAreas.Network)
            {
                serializedGameData = getNetworkData(this.locationString);
                return(true);
            }

            if (dataAccessType == GameDataStorageLayerUtils.DataStorageAreas.FileSystem)
            {
                serializedGameData = getSerializedDataFromDisk(this.locationString);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Retrieve all serialized data from the disk, minimal processing needed.
        /// </summary>
        /// <param name="dataLocation">Location on disk to load.</param>
        /// <returns>Array of bytes to deserialize into usable data.</returns>
        public List <Tuple <string, byte[]> > getSerializedDataFromDisk(string dataLocation)
        {
            List <Tuple <string, byte[]> > data = null;

            //Perhaps we should replace this with a LINQ statement
            //as noted here: http://stackoverflow.com/questions/13301053/directory-getfiles-of-certain-extension
            string[] fileNames = System.IO.Directory.GetFiles(dataLocation, "*.xml");
            foreach (var file in fileNames)
            {
                try
                {
                    if (File.Exists(dataLocation + "/" + file))
                    {
                        data.Add(new Tuple <string, byte[]>(file, File.ReadAllBytes(dataLocation + "/" + file)));
                    }
                }
                catch (Exception ex)
                {
                    BaseGameDataStorageLayer.logData("Unable to open and read file due to {0}" + ex.Message, GameDataStorageLayerUtils.LogLevels.Error);
                }
            }
            return(data);
        }