Esempio n. 1
0
        private static T CheckForNullAndReturn <T>(T checkingObject, string errorMessage)
        {
            if (checkingObject == null)
            {
                UnityLogger.Error(errorMessage);
            }

            return(checkingObject);
        }
Esempio n. 2
0
        public static T LoadSerializedObjectFromFile <T>(string pathToFile)
        {
            var applicationPathToFile = GetApplicationPathToFile(pathToFile);

            try
            {
                using (var reader = new StreamReader(applicationPathToFile))
                {
                    return(JsonUtility.FromJson <T>(reader.ReadToEnd()));
                }
            }
            catch (Exception)
            {
                var errorMessage = "Failed to load data from file " + applicationPathToFile;
                UnityLogger.Error(errorMessage);
                throw;
            }
        }
Esempio n. 3
0
        public static byte[] LoadBytesFromFile(FileSourceType fileSourceType, string pathToFile)
        {
            try
            {
                var applicationPathToFile = GetApplicationPathToFile(fileSourceType, pathToFile);
                using (var reader = new BinaryReader(File.Open(applicationPathToFile, FileMode.Open)))
                {
                    return(reader.ReadAllBytes());
                }
            }
            catch (Exception)
            {
                var errorMessage = $"Failed to load byte data from source {fileSourceType}, path {pathToFile}";
                UnityLogger.Error(errorMessage);
                throw;
            }

            return(null);
        }
Esempio n. 4
0
        public static void SaveSerializedObjectToFile(string pathToFile, object data)
        {
            var applicationPathToFile = GetApplicationPathToFile(pathToFile);

            CreateDirectoryIfNotExistsWithActualPath(Path.GetDirectoryName(applicationPathToFile));

            try
            {
                using (var writer = new StreamWriter(applicationPathToFile))
                {
                    var json = JsonUtility.ToJson(data);
                    writer.Write(json);
                }
            }
            catch (Exception)
            {
                var errorMessage = "Failed to save data to file " + applicationPathToFile;
                UnityLogger.Error(errorMessage);
                throw;
            }
        }
Esempio n. 5
0
        public static void SaveBytesToFile(FileSourceType fileSourceType, string pathToFile, byte[] bytes)
        {
            var applicationPathToFile = GetApplicationPathToFile(fileSourceType, pathToFile);

            CreateDirectoryIfNotExistsWithActualPath(Path.GetDirectoryName(applicationPathToFile));

            try
            {
                File.Delete(applicationPathToFile);
                using (var writer = new BinaryWriter(File.OpenWrite(applicationPathToFile)))
                {
                    writer.Write(bytes);
                    writer.Flush();
                }
            }
            catch (Exception)
            {
                var errorMessage = "Failed to save bytes data to file " + applicationPathToFile;
                UnityLogger.Error(errorMessage);
                throw;
            }
        }
Esempio n. 6
0
        private static void LoadIfUnresolved(Type type)
        {
            if (DEPENDENCIES.ContainsKey(type))
            {
                return;
            }

            if (CURRENTLY_LOADING_DEPENDENCIES.Contains(type))
            {
                throw new AccessViolationException(
                          $"An attempt to load class that is being loaded. Please check that you don't have any dependency usage of class {type}" + " in its constructor");
            }

            if (type.IsSubclassOf(typeof(MonoBehaviour)) && type.GetCustomAttribute <InjectAttribute>() != null)
            {
                var instance = Object.FindObjectOfType(type);
                if (instance != null)
                {
                    Add(type, instance);
                }
                else
                {
                    UnityLogger.Error($"Failed to instantiate MonoBehaviour object of type {type}: no object found in the scene");
                }

                return;
            }

            if (Application.isEditor && !Application.isPlaying)
            {
                if (!DEPENDENCIES.ContainsKey(type))
                {
                    UnityLogger.Debug($"Initializing dependency {type} inside the editor");
                    InstantiateObject(type);
                }

                return;
            }

            if (!IS_INITIALIZED)
            {
                throw new AccessViolationException(
                          "Tried to resolve without Initializing " + typeof(DependencyProvider));
            }

            CURRENTLY_LOADING_DEPENDENCIES.Add(type);

            foreach (var injectableType in INJECTABLE_TYPES)
            {
                if (injectableType == type)
                {
                    try
                    {
                        InstantiateObject(type);
                    }
                    catch (Exception ex)
                    {
                        UnityLogger.Error("Tried to instantiate class [" + type + "], but failed because of: " + ex);
                    }
                }
                else if (type.IsAssignableFrom(injectableType))
                {
                    if (!DEPENDENCIES.ContainsKey(injectableType))
                    {
                        LoadIfUnresolved(injectableType);
                    }

                    foreach (var injectable in DEPENDENCIES[injectableType])
                    {
                        Add(type, injectable);
                    }
                }
            }

            CURRENTLY_LOADING_DEPENDENCIES.Remove(type);
        }