Exemple #1
0
 public ContentLoadResult(ContentHandle handle, bool _loadedFromFile)
 {
     content        = handle.content;
     contentType    = handle.contentType;
     isSuccess      = true;
     loadedFromFile = _loadedFromFile;
 }
Exemple #2
0
        private static bool LoadContentFromReference(ContentReference contentRef, out ContentHandle outHandle)
        {
            // Verify parameters and basic validity of the content reference:
            outHandle = null;
            if (contentRef == null)
            {
                Debug.LogError("[ContentLoader] Error! Cannot load content from null reference!");
                return(false);
            }
            if (!contentRef.IsValid())
            {
                Debug.LogError($"[ContentLoader] Error! Content reference '{contentRef}' is invalid, cannot load content!");
                return(false);
            }

            // Load the content from whichever source it is located in:
            switch (contentRef.source)
            {
            case ContentLoadSource.File:
                // Content object was serialized to file, read and deserialize it:
                return(ContentFileLoader.LoadContent(contentRef, out outHandle));

            case ContentLoadSource.Database:
                // Content was stored in a database (SQL or other), retrieve it:
                return(LoadContentFromDatabase(contentRef, out outHandle));

            default:
                // Unidentified source, log an error message and return failure:
                Debug.LogError($"[ContentLoader] Error! Content source '{contentRef.source}' is unknown, cannot load content for reference '{contentRef}'!");
                break;
            }
            return(false);
        }
Exemple #3
0
        public static bool SaveContent(string nameKey, object content, out ContentRefAndHandle refAndHandle, ContentLoadSource saveLocation = ContentLoadSource.File)
        {
            refAndHandle = null;
            if (string.IsNullOrWhiteSpace(nameKey))
            {
                Debug.LogError("[ContentLoader] Error! Content name may not be null, empty, or blank!");
                return(false);
            }
            if (content == null)
            {
                Debug.LogError("[ContentLoader] Error! Cannot save null content!");
                return(false);
            }

            // Check if a piece of content with this same name has been created before:
            bool exists = contentLoadedDict.TryGetValue(nameKey, out ContentHandle handle);

            exists |= contentDict.TryGetValue(nameKey, out ContentReference reference);

            // If we're dealing with new content, create and register new reference and handle objects:
            string sourcePath = reference?.sourcePath;

            if (!exists)
            {
                reference = new ContentReference(nameKey, content.GetType(), saveLocation, sourcePath);
                contentDict.Add(nameKey, reference);
            }
            if (handle == null)
            {
                handle = new ContentHandle(nameKey, content);
                contentLoadedDict.Add(nameKey, handle);
            }

            // Save content to file: (this will overwrite any previous version of the content)
            bool wasSaved = false;

            switch (saveLocation)
            {
            case ContentLoadSource.File:
                if (string.IsNullOrWhiteSpace(reference.sourcePath))
                {
                    reference.sourcePath = ContentFileLoader.CreateSourcePath(handle);
                }
                wasSaved = ContentFileLoader.SaveContent(reference, handle);
                break;

            case ContentLoadSource.Database:
                // TODO
                break;

            default:
                break;
            }

            // Output both content identifiers and return success:
            refAndHandle = new ContentRefAndHandle(reference, handle);
            return(true);
        }
        public static bool LoadContent(ContentReference contentRef, out ContentHandle outHandle)
        {
            outHandle = null;
            if (contentRef == null)
            {
                return(false);
            }

            if (!VerifyContentReference(contentRef, out FileFormat sourceFileFormat))
            {
                return(false);
            }

            // Get the content's actual data type:
            Assembly assembly    = Assembly.GetCallingAssembly();
            Type     contentType = assembly.GetType(contentRef.contentTypeName, false, true);

            if (contentType == null)
            {
                Debug.LogError($"[ContentFileLoader] Error! Could not find any type of the name '{contentRef.contentTypeName}' for content reference '{contentRef}'!");
                return(false);
            }

            // Read and deserialize content from whatever format:
            bool   success = false;
            object content = null;

            switch (sourceFileFormat)
            {
            case FileFormat.Json:
                if (ContentSerializer.ReadTextFile(contentRef.sourcePath, out string contentJson))
                {
                    success = ContentSerializer.DeserializeJson(contentJson, contentType, out content);
                }
                break;

            case FileFormat.Xml:
                if (ContentSerializer.ReadTextFile(contentRef.sourcePath, out string contentXml))
                {
                    success = ContentSerializer.DeserializeXml(contentXml, contentType, out content);
                }
                break;

            case FileFormat.Csv:
                // TODO
                break;

            case FileFormat.Binary:
                if (ContentSerializer.ReadBinaryFile(contentRef.sourcePath, out byte[] contentBytes))
Exemple #5
0
        private static bool LoadContentFromDatabase(ContentReference contentRef, out ContentHandle outHandle)
        {
            outHandle = null;
            if (contentRef == null)
            {
                return(false);
            }

            // TODO

            object content = null;

            outHandle = new ContentHandle(contentRef.nameKey, content);
            return(true);
        }
        public static string CreateSourcePath(ContentHandle handle)
        {
            if (handle == null || !handle.IsValid())
            {
                Debug.LogError("[ContentFileLoader] Error! Cannot create file source path from null or invalid content handle!");
                return(string.Empty);
            }

            string rootPath         = ProjectManager.ActiveProject?.rootPath ?? string.Empty;
            string subPath          = ContentHelper.GetContentCategorySubPath(handle.category);
            string contentDirectory = Path.Combine(rootPath, subPath);

            FileFormat fileFormat    = ProjectManager.ActiveProject.preferredFileFormat;
            string     fileExtension = (from ext in fileExtensions where ext.format == fileFormat select ext.extension).FirstOrDefault();
            string     fileName      = $"{handle.nameKey}{fileExtension ?? string.Empty}";

            return(Path.Combine(contentDirectory, fileName));
        }
Exemple #7
0
        public static ContentRefAndHandle[] GetAllContentReferences()
        {
            if (contentDict == null)
            {
                return(null);
            }
            if (contentLoadedDict == null)
            {
                contentLoadedDict = new Dictionary <string, ContentHandle>();
            }

            ContentRefAndHandle[] crh           = new ContentRefAndHandle[ContentCount];
            ContentReference[]    allReferences = contentDict.Values.ToArray();
            for (int i = 0; i < crh.Length; ++i)
            {
                ContentReference reference = allReferences[i];
                ContentHandle    handle    = (from h in contentLoadedDict where string.CompareOrdinal(h.Key, reference.nameKey) == 0 select h)?.FirstOrDefault().Value;
                crh[i] = new ContentRefAndHandle(reference, handle);
            }
            return(crh);
        }