Пример #1
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);
        }
Пример #2
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);
        }