Ejemplo n.º 1
0
        /// <summary>
        /// Called when the Deployment Serializer was asked to save an object to a binary file but the object is not tagged as serializable.
        /// </summary>
        internal static void logNonSerializableObjectCall(object t)
        {
            string message = "Deployment Serializer was called to saveObject() on an Object that was not tagged as serializable by the programmer " +
                             "and cannot be saved to file. Please include the [System.Serializable] attribute on the object. \n" +
                             "Class Name: " + SystemTools.getObjectName(t);

            logMessageToUnityConsole(message, SerializerLogType.Warning);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves the object to a binary file in the PersistentPath for post deployment saves
        /// </summary>
        /// <typeparam name="T">Object type to be serialized</typeparam>
        /// <param name="objectToSave">Object to be serialized</param>
        /// <param name="fileName">Name to save the file under</param>
        private static void serializeObject_Build(object objectToSave, string fileName)
        {
            //Initialize objects for use
            BinaryFormatter binaryFormatter;
            FileStream      fileStream;

            //Build file name and save reference
            fileName = filenamePrefix + fileName;
            string filePath = UnityTools.getPersistentFilePath() + "/" + filenamePrefix + fileName + fileNameExtension;

            //Serialize the new Object provided by the Dev
            try
            {
                //Create stream and formatter
                binaryFormatter = new BinaryFormatter();
                fileStream      = new FileStream(filePath, FileMode.Create);
                //Serialize file and close stream
                binaryFormatter.Serialize(fileStream, objectToSave);
                fileStream.Close();
            }
            catch (IOException)
            {
                DS_MessageLogger.logMessageToUnityConsole("An unexpected IO failure occured when trying to serialize the provided object to the" +
                                                          " Persistent File Path in build. " +
                                                          "Process is being aborted! \n" +
                                                          "Class Name: " + SystemTools.getObjectName(objectToSave), SerializerLogType.Error);
                return;
            }
            catch (Exception)
            {
                DS_MessageLogger.logMessageToUnityConsole("An unknown failure occured when trying to serialize the provided object to the " +
                                                          " Persistent File Path in build." +
                                                          "Process is being aborted! \n" +
                                                          "Class Name: " + SystemTools.getObjectName(objectToSave), SerializerLogType.Error);
                return;
            }

            //Log the save if messages are enabled
            DS_MessageLogger.logMessageToUnityConsole(SystemTools.getObjectName(objectToSave) + " was saved successfully", SerializerLogType.Standard);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="objectToSave"></param>
        /// <param name="fileName"></param>
        private static void serializeObject_Persistent(object objectToSave, string fileName, int attemptsRemaining)
        {
            //Initialize objects for use
            BinaryFormatter binaryFormatter;
            FileStream      fileStream;
            FileNameTracker fileNameTracker;

            ////Get a valid copy of the FileNameTracker
            //If the file tracker exists we deserialize it
            if (SystemTools.fileExists(fileNamesTrackerPath))
            {
                try
                {
                    binaryFormatter = new BinaryFormatter();
                    fileStream      = new FileStream(fileNamesTrackerPath, FileMode.Open);
                    fileNameTracker = binaryFormatter.Deserialize(fileStream) as FileNameTracker;
                    fileStream.Close();
                }
                catch (IOException)
                {
                    //DS_MessageLogger.logMessageToUnityConsole("An unexpected IO failure occured when trying to open the FileNameTracker" +
                    //    " binary stored in the Resources Folder. Object saving process is being aborted!", SerializerLogType.Error);
                    Debug.Log("File already open, waiting then trying again");
                    //System.Threading.Thread.Sleep(500);
                    //serializeObject_Persistent(objectToSave, fileName);
                    return;
                }
                catch (Exception)
                {
                    DS_MessageLogger.logMessageToUnityConsole("An unknown failure occured when trying to open the FileNameTracker" +
                                                              " binary stored in the Resources Folder. Onject saving process is being aborted!", SerializerLogType.Error);
                    return;
                }
            }
            //Otherwise we need to make it
            else
            {
                fileNameTracker = new FileNameTracker();
            }

            //Build file name and save reference
            fileName = filenamePrefix + fileName;
            string filePath = resourcesFilePath + fileName + fileNameExtension;

            //Serialize the new Object provided by the Dev
            try
            {
                //Create stream and formatter
                binaryFormatter = new BinaryFormatter();
                fileStream      = new FileStream(filePath, FileMode.Create);
                //Serialize file and close stream
                binaryFormatter.Serialize(fileStream, objectToSave);
                fileStream.Close();
            }
            catch (IOException)
            {
                DS_MessageLogger.logMessageToUnityConsole("An unexpected IO failure occured when trying to serialize the provided object to the " +
                                                          "Resources Folder for persistance. " +
                                                          "Process is being aborted! \n" +
                                                          "Class Name: " + SystemTools.getObjectName(objectToSave), SerializerLogType.Error);
                return;
            }
            catch (Exception)
            {
                DS_MessageLogger.logMessageToUnityConsole("An unknown failure occured when trying to serialize the provided object to the " +
                                                          "Resources Folder for persistance." +
                                                          "Process is being aborted! \n" +
                                                          "Class Name: " + SystemTools.getObjectName(objectToSave), SerializerLogType.Error);
                return;
            }

            //Serialize the FileNameTracker for persistence
            try
            {
                //Add the new file name
                fileNameTracker.fileNames.Add(fileName);
                //Create stream and formatter
                binaryFormatter = new BinaryFormatter();
                fileStream      = new FileStream(fileNamesTrackerPath, FileMode.Create);
                //Serialize the file and close the stream
                binaryFormatter.Serialize(fileStream, fileNameTracker);
            }
            catch (IOException)
            {
                DS_MessageLogger.logMessageToUnityConsole("An unexpected IO failure occured when trying to serialize the FileNameTracker object. " +
                                                          "Process is being aborted!", SerializerLogType.Error);
                return;
            }
            catch (Exception)
            {
                DS_MessageLogger.logMessageToUnityConsole("An unknown failure occured when trying to serialize the FileNameTracker object. " +
                                                          "Process is being aborted!", SerializerLogType.Error);
                return;
            }

            //Log the save if messages are enabled
            DS_MessageLogger.logMessageToUnityConsole(SystemTools.getObjectName(objectToSave) + " was saved successfully", SerializerLogType.Standard);
        }