public static List <T> LoadJsonListFromDirectory <T>(String DirectoryPath, Object Context, Func <T, String> Name) { var result = new Dictionary <String, T>(); foreach (var resolvedAssetPath in AssetManager.EnumerateAllFiles(DirectoryPath)) { try { var item = LoadJsonFromAbsolutePath <T>(resolvedAssetPath, Context); var name = Name(item); if (!result.ContainsKey(name)) { result.Add(name, item); } } catch (Exception e) { DwarfGame.LogSentryBreadcrumb("AssetManager", String.Format("Could not load json: {0} msg: {1}", resolvedAssetPath, e.Message), SharpRaven.Data.BreadcrumbLevel.Error); Console.WriteLine("Error loading asset {0}: {1}", resolvedAssetPath, e.Message); } } return(new List <T>(result.Values)); }
private Texture2D CreateScreenshot() { DwarfGame.LogSentryBreadcrumb("Saving", String.Format("User saving an overworld with size {0} x {1}", MetaData.Overworld.Width, MetaData.Overworld.Height), SharpRaven.Data.BreadcrumbLevel.Info); Texture2D toReturn = new Texture2D(GameState.Game.GraphicsDevice, MetaData.Overworld.Width, MetaData.Overworld.Height); var colorData = new Color[MetaData.Overworld.Width * MetaData.Overworld.Height]; MetaData.Overworld.Map.CreateTexture(null, 1, colorData, MetaData.Overworld.GenerationSettings.SeaLevel); MetaData.Overworld.Map.ShadeHeight(1, colorData); toReturn.SetData(colorData); return(toReturn); }
public static RawPrimitive GetContentMesh(string _asset) { if (String.IsNullOrEmpty(_asset)) { DwarfGame.LogSentryBreadcrumb("AssetManager", "Attempt to load mesh asset from empty string.", SharpRaven.Data.BreadcrumbLevel.Warning); return(null); } string asset = FileUtils.NormalizePath(_asset); if (asset == null) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Asset {0} was null.", _asset), SharpRaven.Data.BreadcrumbLevel.Warning); return(null); } try { var filename = ResolveContentPath(asset, ".obj"); if (Path.GetExtension(filename) == ".xnb") { //var toReturn = Content.Load<ModelMesh>(filename.Substring(0, filename.Length - 4)); //TextureCache[asset] = toReturn; //return toReturn; // Todo: Convert model mesh to raw prim? return(null); } else if (Path.GetExtension(filename) == ".obj") { return(ObjLoader.LoadObject(File.ReadAllLines(FileUtils.NormalizePath(filename)))); } else { return(null); } } catch (Exception exception) { Console.Error.WriteLine(exception.ToString()); try { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Failed to load asset {0} : {1}", asset, exception.ToString()), SharpRaven.Data.BreadcrumbLevel.Warning); return(null); } catch (Exception innerException) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Everything is broken! {0}", innerException.ToString()), SharpRaven.Data.BreadcrumbLevel.Error); return(null); } } }
private void LoadFromFile() { SetLoadingMessage("Creating Sky..."); Renderer.Sky = new SkyRenderer(); #region Reading game file SetLoadingMessage("Loading " + Overworld.InstanceSettings.ExistingFile); var gameFile = SaveGame.LoadMetaFromDirectory(Overworld.InstanceSettings.ExistingFile); if (gameFile == null) { throw new InvalidOperationException("Game File does not exist."); } if (gameFile.Metadata.Version != Program.Version && !Program.CompatibleVersions.Contains(gameFile.Metadata.Version)) { throw new InvalidOperationException(String.Format("Game file is from version {0}. Compatible versions are {1}.", gameFile.Metadata.Version, TextGenerator.GetListString(Program.CompatibleVersions))); } Renderer.Sky.TimeOfDay = gameFile.Metadata.TimeOfDay; Renderer.PersistentSettings = gameFile.Metadata.RendererSettings; Time = gameFile.Metadata.Time; WorldSizeInChunks = new Point3(Overworld.InstanceSettings.Cell.Bounds.Width, Overworld.zLevels, Overworld.InstanceSettings.Cell.Bounds.Height); #endregion #region Initialize static data bool actionComplete = false; Game.DoLazyAction(new Action(() => { Renderer.InstanceRenderer = new InstanceRenderer(); Renderer.bloom = new BloomComponent(Game) { Settings = BloomSettings.PresetSettings[5] }; Renderer.bloom.Initialize(); SoundManager.Content = Content; if (PlanService != null) { PlanService.Restart(); } MonsterSpawner = new MonsterSpawner(this); EntityFactory.Initialize(this); }), () => { actionComplete = true; return(true); }); while (!actionComplete) { Thread.Sleep(10); } #endregion PlanService = new PlanService(); SetLoadingMessage("Creating Liquids ..."); #region liquids Renderer.WaterRenderer = new WaterRenderer(GraphicsDevice); #endregion #region Load Components // Create updateable systems. foreach (var updateSystemFactory in AssetManager.EnumerateModHooks(typeof(UpdateSystemFactoryAttribute), typeof(EngineModule), new Type[] { typeof(WorldManager) })) { UpdateSystems.Add(updateSystemFactory.Invoke(null, new Object[] { this }) as EngineModule); } ChunkManager = new ChunkManager(Content, this); Splasher = new Splasher(ChunkManager); Renderer.ChunkRenderer = new ChunkRenderer(ChunkManager); SetLoadingMessage("Loading Terrain..."); ChunkManager.LoadChunks(gameFile.LoadChunks(), ChunkManager); SetLoadingMessage("Loading Entities..."); gameFile.LoadPlayData(Overworld.InstanceSettings.ExistingFile, this); PersistentData = gameFile.PlayData.PersistentData; Renderer.Camera = gameFile.PlayData.Camera; if (gameFile.PlayData.Stats != null) { Stats = gameFile.PlayData.Stats; } ComponentManager = new ComponentManager(gameFile.PlayData.Components, this); foreach (var component in gameFile.PlayData.Components.SaveableComponents) { if (!ComponentManager.HasComponent(component.GlobalID) && ComponentManager.HasComponent(component.Parent.GlobalID)) { // Logically impossible. throw new InvalidOperationException("Component exists in save data but not in manager."); } } ConversationMemory = gameFile.PlayData.ConversationMemory; Factions = gameFile.PlayData.Factions; ComponentManager.World = this; Renderer.Sky.TimeOfDay = gameFile.Metadata.TimeOfDay; Time = gameFile.Metadata.Time; PlayerFaction = Factions.Factions["Player"]; EventScheduler = new Events.Scheduler(); TutorialManager = new Tutorial.TutorialManager(); TutorialManager.SetFromSaveData(gameFile.PlayData.TutorialSaveData); Renderer.Camera.World = this; #endregion SetLoadingMessage("Creating Particles ..."); Game.DoLazyAction(new Action(() => ParticleManager = new ParticleManager(ComponentManager))); SetLoadingMessage("Creating GameMaster ..."); TaskManager = new TaskManager(); TaskManager.World = this; Time.NewDay += (time) => PayEmployees(); DwarfGame.LogSentryBreadcrumb("Loading", "Started new game with an existing file."); if (gameFile.PlayData.Tasks != null) { TaskManager = gameFile.PlayData.Tasks; TaskManager.World = this; } if (PlayerFaction.Economy.Information == null) { throw new InvalidProgramException(); } if (MathFunctions.RandEvent(0.01f)) { SetLoadingMessage("Reticulating Splines..."); } ChunkManager.StartThreads(); SetLoadingMessage("Presimulating ..."); ShowingWorld = false; OnLoadedEvent(); Thread.Sleep(1000); ShowingWorld = true; SetLoadingMessage("Complete."); // GameFile is no longer needed. gameFile = null; LoadStatus = LoadingStatus.Success; }
public static Texture2D GetContentTexture(string _asset) { if (String.IsNullOrEmpty(_asset)) { DwarfGame.LogSentryBreadcrumb("AssetManager", "Attempt to load texture asset from empty string.", SharpRaven.Data.BreadcrumbLevel.Warning); return(Content.Load <Texture2D>("Content/newgui/error")); } #if DEBUG if (!DwarfGame.IsMainThread) { // This completely breaks loading... //throw new InvalidOperationException("Can't load an asset outside of the main thread."); } #endif string asset = FileUtils.NormalizePath(_asset); if (asset == null) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Asset {0} was null.", _asset), SharpRaven.Data.BreadcrumbLevel.Warning); var r = Content.Load <Texture2D>(ContentPaths.Error); return(r); } if (TextureCache.ContainsKey(asset)) { var existing = TextureCache[asset]; if (existing != null && !existing.IsDisposed && existing.GraphicsDevice != null && !existing.GraphicsDevice.IsDisposed) { return(existing); } else { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Asset {0} was invalid.", asset), SharpRaven.Data.BreadcrumbLevel.Warning); TextureCache.Remove(asset); } } try { var filename = ResolveContentPath(asset, ".png", ".bmp"); if (Path.GetExtension(filename) == ".xnb") { var toReturn = Content.Load <Texture2D>(filename.Substring(0, filename.Length - 4)); TextureCache[asset] = toReturn; return(toReturn); } else { var toReturn = LoadUnbuiltTextureFromAbsolutePath(filename); TextureCache[asset] = toReturn; return(toReturn); } } catch (Exception exception) { Console.Error.WriteLine(exception.ToString()); try { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Failed to load asset {0} : {1}", asset, exception.ToString()), SharpRaven.Data.BreadcrumbLevel.Warning); var r = Content.Load <Texture2D>(ContentPaths.Error); TextureCache[asset] = r; return(r); } catch (Exception innerException) { DwarfGame.LogSentryBreadcrumb("AssetManager", string.Format("Everything is broken! {0}", innerException.ToString()), SharpRaven.Data.BreadcrumbLevel.Error); return(null); } } }