public ContentReference(string _nameKey, string _contentTypeName, ContentLoadSource _source, string _sourcePath) { nameKey = _nameKey?.ToLowerInvariant(); contentTypeName = !string.IsNullOrEmpty(_contentTypeName) ? _contentTypeName : "Object"; source = _source; sourcePath = _sourcePath; }
public ContentReference(string _nameKey, Type _contentType, ContentLoadSource _source, string _sourcePath) { nameKey = _nameKey?.ToLowerInvariant(); contentTypeName = _contentType != null ? _contentType.Name : "Object"; source = _source; sourcePath = _sourcePath; }
public ContentReference(object content, ContentLoadSource _source, string _sourcePath) { Type contentType = content?.GetType(); if (contentType != null) { object[] attributes = contentType.GetCustomAttributes(typeof(ContentElementAttribute), true); ContentElementAttribute contentAttr = attributes != null && attributes.Length != 0 ? attributes[0] as ContentElementAttribute : null; if (contentAttr != null && contentAttr.GetNameKey(content, out nameKey)) { contentTypeName = contentAttr.GetContentType()?.Name ?? string.Empty; } } source = _source; sourcePath = _sourcePath; }
public static bool CreateNewProject(string campaignName, string parentDirectory, ContentLoadSource defaultSaveLocation, DateTime ingameStartTime, out Campaign outCampaign) { // Create the campaign directories and folder structure: if (!CreateProjectDirectories(campaignName, parentDirectory, out string projectRootDir)) { outCampaign = null; return(false); } // Create a project object, for later serialization to file: Project project = new Project() { name = campaignName, rootPath = projectRootDir, }; // Set the new project as active, thus updating content root directories to the new project's: if (!ProjectManager.LoadProject(project)) { Debug.LogError("[ProjectCreator] Error! Failed to load and activate newly created project!"); outCampaign = null; return(false); } // Create basic contents for the project and its main campaign: string storylineName = $"{campaignName} Timeline"; StoryEvent startEvent = new StoryEvent() { name = $"{campaignName} beginning", description = "The player characters first meet in the taproom of their hostel, attracted by a scream of terror. " + "Seconds later, the woman at the origin of the scream collapses; blood running from her eyes, she dies on the spot. " + "On her hand, a strange marking has appeared, strangely similar to the symbol Baal, the god of death.", startTime = ingameStartTime, storyline = new ContentAccessor(storylineName), }; ContentAccessor startEventCA = new ContentAccessor(startEvent.name, startEvent); ContentLoader.SaveContent(startEventCA, out ContentRefAndHandle startEventRaH, defaultSaveLocation); StoryLine storyline = new StoryLine() { name = storylineName, description = $"This is the main storyline of the '{campaignName}' campaign, telling the tale of a group " + "of unlikely companions banding together to vanquish the great evil that is threatening their home.", startTime = ingameStartTime, endTime = ingameStartTime + new TimeSpan(1, 0, 0, 0), events = new List <ContentAccessor>(new ContentAccessor[] { startEventCA }), }; ContentAccessor storylineCA = new ContentAccessor(storylineName, storyline); ContentLoader.SaveContent(storylineCA, out ContentRefAndHandle storylineRaH, defaultSaveLocation); Campaign campaign = new Campaign() { name = campaignName, description = "A grand adventure involving great heroes, vile foes, and an excessive number of murder hobos.", mainStoryline = new ContentAccessor(storyline.name, storyline), }; ContentAccessor campaignCA = new ContentAccessor(campaignName, campaign); ContentLoader.SaveContent(campaignCA, out ContentRefAndHandle campaignRaH, defaultSaveLocation); WorldMap worldMap = new WorldMap() { name = $"{campaignName} World Map", description = "The home world of our great heroes, and the place they persistently haunt in quest for murder and loot.", }; ContentAccessor worldMapCA = new ContentAccessor(worldMap.name, worldMap); ContentLoader.SaveContent(worldMapCA, out ContentRefAndHandle worldMapRaH, defaultSaveLocation); // Link the above contents to the project: project.mainCampaign = campaignCA; project.mainWorldMap = worldMapCA; ContentAccessor projectCA = new ContentAccessor(project.name, project); // Save project to file: ContentLoader.SaveContent(projectCA, out ContentRefAndHandle projectRaH, ContentLoadSource.File); // Output the newly created and populated campaign object and return success: outCampaign = campaign; return(true); }
public static bool SaveContent(ContentAccessor contentCA, out ContentRefAndHandle refAndHandle, ContentLoadSource saveLocation = ContentLoadSource.File) { if (contentCA == null) { Debug.LogError("[ContentLoader] Error! Cannot save content from null accessor!"); refAndHandle = null; return(false); } return(SaveContent(contentCA.nameKey, contentCA.content, out refAndHandle, saveLocation)); }
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); }