/// <summary> /// Saves the game to the specified game name. /// </summary> /// <param name="fileName">Name of the file.</param> public void SaveGame(string fileName) { XmlDocument doc = new XmlDocument(); SaveGame(doc); using (var stream = VirtualPathProvider.CreateFile(VirtualPathProvider.EnsureModVirtualPath("save/" + fileName + ".xml", ModName))) doc.Save(stream); }
/// <summary> /// Loads the texture. /// </summary> /// <param name="forced">if set to <c>true</c> the texture is reloaded even if it is already loaded.</param> public void LoadTexture(bool forced) { if (!forced && this.textureId != 0) { return; } Dispose(); using (Stream stream = VirtualPathProvider.GetFile(VirtualPathProvider.EnsureModVirtualPath(FileName, manager.Game.ModName))) { Image buf = Image.FromStream(stream); Bitmap bmp; if (buf is Bitmap) { bmp = buf as Bitmap; } else { bmp = new Bitmap(buf.Width, buf.Height, PixelFormat.Format32bppArgb); using (Graphics gr = Graphics.FromImage(bmp)) { gr.DrawImage(buf, 0, 0); } buf.Dispose(); } if (IsAnimation) { for (int i = 0; i < Animation.AnimationFrames; ++i) { // Lock the bitmap data of the texture. int width = bmp.Width / Animation.AnimationFrames; BitmapData data = bmp.LockBits(new Rectangle(width * i, 0, width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); LoadTexture(data, i); bmp.UnlockBits(data); } bmp.Dispose(); } else { // Lock the bitmap data of the texture. BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); LoadTexture(data); bmp.UnlockBits(data); bmp.Dispose(); } } }
/// <summary> /// Loads the specified save game. /// </summary> /// <param name="fileName">The game file name.</param> public void LoadGame(string fileName) { XmlDocument doc = new XmlDocument(); using (var stream = VirtualPathProvider.GetFile(VirtualPathProvider.EnsureModVirtualPath("save/" + fileName + ".xml", ModName))) { try { doc.Load(stream); } catch (XmlException ex) { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatException, fileName), ex); } } LoadGame(doc); if (GameLoaded != null) { GameLoaded(this, EventArgs.Empty); } }
/// <summary> /// Loads the texture definitions for the currently selected mod. /// </summary> public void Load() { Load(VirtualPathProvider.GetFile(VirtualPathProvider.EnsureModVirtualPath(FileName, Game.ModName))); }
/// <summary> /// Loads the component data from the specified stream. /// </summary> /// <param name="stream">The input stream.</param> public void Load(Stream stream) { XmlDocument doc = new XmlDocument(); try { doc.Load(stream); } catch (XmlException ex) { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatException, FileName), ex); } finally { stream.Close(); } XmlElement root = doc.SelectSingleNode("/FDRFile") as XmlElement; if (root == null || root.Attributes["type"].Value != "fonts") { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatException, FileName)); } foreach (XmlElement font in root.SelectNodes("FontList/*")) { string id = string.Empty; string fontName = string.Empty; decimal fontSize = 0; bool systemFont = false; Styles fontStyles = Styles.None; foreach (XmlElement param in font.ChildNodes) { try { switch (param.Name) { case "Id": id = param.InnerText; break; case "FontName": fontName = param.InnerText; break; case "FontSize": fontSize = decimal.Parse(param.InnerText, CultureInfo.InvariantCulture); break; case "SystemFont": systemFont = XmlHelper.ParseBool(param.InnerText); break; case "FontStyles": fontStyles = (Styles)XmlHelper.ParseEnum <Styles>(param.InnerText); break; } } catch (FormatException ex) { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatSpecificException, FileName, param.InnerText, param.Name), ex); } } if (string.IsNullOrEmpty(id)) { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatSpecificException, FileName, "null", "Id")); } if (string.IsNullOrEmpty(fontName)) { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatSpecificException, FileName, "null", "FontName")); } if (fontSize == 0) { throw new BlackbirdException(string.Format(Resources.ModFileIsInIncorrectFormatSpecificException, FileName, "null", "FontSize")); } if (innerData.ContainsKey(id)) { throw new BlackbirdException(string.Format(Resources.FontAlreadyRegistered, id)); } Font pom; if (systemFont) { pom = new Font(fontName, (int)fontSize); } else { byte[] data; using (Stream fileStream = VirtualPathProvider.GetFile(VirtualPathProvider.EnsureModVirtualPath(fontName, Game.ModName))) { int length = (int)fileStream.Length; data = new byte[length]; if (fileStream.Read(data, 0, length) != length) { return; } } pom = new Font(data, (int)fontSize); pom.Style = fontStyles; } innerData.Add(id, pom); } }
/// <summary> /// Finds all the files in the specified directory based on the specified filter. /// </summary> /// <param name="path">The path.</param> /// <param name="filter">The filter.</param> /// <returns></returns> public static string[] FindFiles(string path, string filter) { // TODO: Implement finding files in packed mods, implement everything in a bit more clean way switch (GetPathKind(path)) { case VirtualPathKind.ModFile: { string[] fragments = path.Split('/'); string modName = fragments[1]; string modPath = Path.Combine(Path.Combine(Environment.CurrentDirectory, BaseModPath), modName); string directoryPath = Path.Combine(modPath, string.Join(Path.DirectorySeparatorChar.ToString(), fragments, 2, fragments.Length - 2)); // Unpacked mod. if (Directory.Exists(directoryPath)) { return(Directory.GetFiles(directoryPath, filter, SearchOption.AllDirectories).Select(i => VirtualPathProvider.EnsureModVirtualPath(i, modName)).ToArray()); } // Packed mod. else if (File.Exists(Path.ChangeExtension(modPath, ModArchiveExtension))) { throw new NotImplementedException("Packed mods not implemented yet."); } // Mod not found. else { throw new BlackbirdException(string.Format(Resources.ModNotFoundException, modName)); } } default: throw new ArgumentException(string.Format(Resources.InvalidVirtualPathException, path)); } }