UltimateZoneBuilder/Source/General/General.cs

941 lines
27 KiB
C#
Raw Normal View History

2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
2007-06-13 19:39:38 +00:00
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Namespaces
2007-06-13 19:39:38 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using CodeImp.DoomBuilder.Interface;
using CodeImp.DoomBuilder.IO;
2007-06-14 23:31:57 +00:00
using CodeImp.DoomBuilder.Map;
2007-06-15 22:38:42 +00:00
using CodeImp.DoomBuilder.Geometry;
2007-06-24 18:56:43 +00:00
using System.Runtime.InteropServices;
2007-06-24 22:53:41 +00:00
using CodeImp.DoomBuilder.Controls;
using System.Diagnostics;
using CodeImp.DoomBuilder.Rendering;
using CodeImp.DoomBuilder.Config;
2007-06-14 23:31:57 +00:00
#endregion
2007-06-13 19:39:38 +00:00
namespace CodeImp.DoomBuilder
{
2007-10-24 17:25:03 +00:00
public static class General
2007-06-13 19:39:38 +00:00
{
2007-06-24 18:56:43 +00:00
#region ================== API Declarations
//[DllImport("user32.dll")]
//public static extern bool LockWindowUpdate(IntPtr hwnd);
2007-06-24 18:56:43 +00:00
[DllImport("kernel32.dll", EntryPoint="RtlZeroMemory", SetLastError=false)]
public static extern void ZeroMemory(IntPtr dest, int size);
[DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", SetLastError = false)]
2007-10-24 17:25:03 +00:00
public static extern unsafe void CopyMemory(void* dst, void* src, UIntPtr length);
[DllImport("kernel32.dll", SetLastError = true)]
2007-10-24 17:25:03 +00:00
public static unsafe extern void* VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
2007-10-24 17:25:03 +00:00
public static unsafe extern bool VirtualFree(void* lpAddress, UIntPtr dwSize, uint dwFreeType);
2007-06-24 18:56:43 +00:00
#endregion
2007-06-13 19:39:38 +00:00
#region ================== Constants
// Memory APIs
public const uint MEM_COMMIT = 0x1000;
public const uint MEM_RESERVE = 0x2000;
public const uint MEM_DECOMMIT = 0x4000;
public const uint MEM_RELEASE = 0x8000;
public const uint MEM_RESET = 0x80000;
public const uint MEM_TOP_DOWN = 0x100000;
public const uint MEM_PHYSICAL = 0x400000;
public const uint PAGE_NOACCESS = 0x01;
public const uint PAGE_READONLY = 0x02;
public const uint PAGE_READWRITE = 0x04;
public const uint PAGE_WRITECOPY = 0x08;
public const uint PAGE_EXECUTE = 0x10;
public const uint PAGE_EXECUTE_READ = 0x20;
public const uint PAGE_EXECUTE_READWRITE = 0x40;
public const uint PAGE_EXECUTE_WRITECOPY = 0x80;
public const uint PAGE_GUARD = 0x100;
public const uint PAGE_NOCACHE = 0x200;
public const uint PAGE_WRITECOMBINE = 0x400;
2007-06-13 19:39:38 +00:00
// Files and Folders
private const string SETTINGS_FILE = "Builder.cfg";
private const string SETTINGS_DIR = "Doom Builder";
private const string LOG_FILE = "Builder.log";
private const string GAME_CONFIGS_DIR = "Configurations";
2007-09-28 08:56:18 +00:00
private const string COMPILERS_DIR = "Compilers";
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Variables
// Files and Folders
private static string apppath;
private static string settingspath;
private static string logfile;
2007-06-13 19:39:38 +00:00
private static string temppath;
private static string configspath;
2007-09-28 08:56:18 +00:00
private static string compilerspath;
2007-06-13 19:39:38 +00:00
// Main objects
2007-06-15 18:30:55 +00:00
private static Assembly thisasm;
2007-06-13 19:39:38 +00:00
private static MainForm mainwindow;
private static Configuration settings;
2007-06-14 23:31:57 +00:00
private static MapManager map;
2007-06-24 22:53:41 +00:00
private static ActionManager actions;
private static ColorCollection colors;
2007-06-14 23:31:57 +00:00
// Configurations
2007-06-14 23:31:57 +00:00
private static List<ConfigurationInfo> configs;
private static List<CompilerInfo> compilers;
2007-09-28 08:56:18 +00:00
private static List<NodebuilderInfo> nodebuilders;
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Properties
2007-06-15 18:30:55 +00:00
public static Assembly ThisAssembly { get { return thisasm; } }
2007-06-13 19:39:38 +00:00
public static string AppPath { get { return apppath; } }
public static string TempPath { get { return temppath; } }
public static string ConfigsPath { get { return configspath; } }
2007-09-28 08:56:18 +00:00
public static string CompilersPath { get { return compilerspath; } }
2007-06-13 19:39:38 +00:00
public static MainForm MainWindow { get { return mainwindow; } }
public static Configuration Settings { get { return settings; } }
public static ColorCollection Colors { get { return colors; } }
2007-06-14 23:31:57 +00:00
public static List<ConfigurationInfo> Configs { get { return configs; } }
2007-09-28 08:56:18 +00:00
public static List<NodebuilderInfo> Nodebuilders { get { return nodebuilders; } }
public static List<CompilerInfo> Compilers { get { return compilers; } }
2007-06-14 23:31:57 +00:00
public static MapManager Map { get { return map; } }
2007-06-24 22:53:41 +00:00
public static ActionManager Actions { get { return actions; } }
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Configurations
2007-09-27 22:55:03 +00:00
// This returns the game configuration info by filename
public static ConfigurationInfo GetConfigurationInfo(string filename)
{
// Go for all config infos
foreach(ConfigurationInfo ci in configs)
{
// Check if filename matches
if(string.Compare(Path.GetFileNameWithoutExtension(ci.Filename),
Path.GetFileNameWithoutExtension(filename), true) == 0)
{
// Return this info
return ci;
}
}
// None found
return null;
}
2007-06-14 23:31:57 +00:00
// This loads and returns a game configuration
public static Configuration LoadGameConfiguration(string filename)
{
Configuration cfg;
// Make the full filepathname
string filepathname = Path.Combine(configspath, filename);
// Load configuration
try
{
// Try loading the configuration
cfg = new Configuration(filepathname, true);
// Check for erors
if(cfg.ErrorResult != 0)
{
// Error in configuration
ShowErrorMessage("Unable to load the game configuration file \"" + filename + "\".\n" +
"Error near line " + cfg.ErrorLine + ": " + cfg.ErrorDescription, MessageBoxButtons.OK);
2007-06-14 23:31:57 +00:00
return null;
}
2007-09-27 22:55:03 +00:00
// Check if this is a Doom Builder 2 config
2007-06-16 19:53:51 +00:00
else if(cfg.ReadSetting("type", "") != "Doom Builder 2 Game Configuration")
{
// Old configuration
ShowErrorMessage("Unable to load the game configuration file \"" + filename + "\".\n" +
"This configuration is not a Doom Builder 2 game configuration.", MessageBoxButtons.OK);
2007-06-16 19:53:51 +00:00
return null;
}
2007-06-14 23:31:57 +00:00
else
{
// Return config
return cfg;
}
}
catch(Exception)
{
// Unable to load configuration
ShowErrorMessage("Unable to load the game configuration file \"" + filename + "\".", MessageBoxButtons.OK);
2007-06-14 23:31:57 +00:00
return null;
}
}
// This loads all game configurations
private static void LoadAllGameConfigurations()
2007-06-14 23:31:57 +00:00
{
Configuration cfg;
string[] filenames;
string name, fullfilename;
// Display status
mainwindow.DisplayStatus("Loading game configurations...");
2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
// Make array
configs = new List<ConfigurationInfo>();
2007-09-28 08:56:18 +00:00
// Go for all cfg files in the configurations directory
2007-06-14 23:31:57 +00:00
filenames = Directory.GetFiles(configspath, "*.cfg", SearchOption.TopDirectoryOnly);
foreach(string filepath in filenames)
{
// Check if it can be loaded
2007-06-16 19:53:51 +00:00
cfg = LoadGameConfiguration(Path.GetFileName(filepath));
2007-06-14 23:31:57 +00:00
if(cfg != null)
{
// Get name and filename
name = cfg.ReadSetting("game", "<unnamed game>");
fullfilename = Path.GetFileName(filepath);
// Add to lists
2007-10-05 10:00:15 +00:00
General.WriteLogLine("Registered game configuration '" + name + "' from '" + fullfilename + "'");
2007-06-14 23:31:57 +00:00
configs.Add(new ConfigurationInfo(name, fullfilename));
}
}
// Sort the list
2007-06-14 23:31:57 +00:00
configs.Sort();
}
// This loads all nodebuilder configurations
private static void LoadAllNodebuilderConfigurations()
2007-09-28 08:56:18 +00:00
{
Configuration cfg;
IDictionary builderslist;
2007-09-28 08:56:18 +00:00
string[] filenames;
2007-09-28 08:56:18 +00:00
// Display status
mainwindow.DisplayStatus("Loading nodebuilder configurations...");
// Make array
nodebuilders = new List<NodebuilderInfo>();
// Go for all cfg files in the compilers directory
filenames = Directory.GetFiles(compilerspath, "*.cfg", SearchOption.TopDirectoryOnly);
foreach(string filepath in filenames)
{
try
{
// Try loading the configuration
cfg = new Configuration(filepath, true);
// Check for erors
if(cfg.ErrorResult != 0)
{
// Error in configuration
ShowErrorMessage("Unable to load the compiler configuration file \"" + Path.GetFileName(filepath) + "\".\n" +
"Error near line " + cfg.ErrorLine + ": " + cfg.ErrorDescription, MessageBoxButtons.OK);
}
else
{
// Get structures
builderslist = cfg.ReadSetting("nodebuilders", new Hashtable());
foreach(DictionaryEntry de in builderslist)
{
// Check if this is a structure
if(de.Value is IDictionary)
{
try
{
// Make nodebuilder info
nodebuilders.Add(new NodebuilderInfo(Path.GetFileName(filepath), de.Key.ToString(), cfg));
}
catch(Exception e)
{
// Unable to load configuration
ShowErrorMessage("Unable to load the nodebuilder configuration '" + de.Key.ToString() + "' from \"" + Path.GetFileName(filepath) + "\". Error: " + e.Message, MessageBoxButtons.OK);
}
}
}
}
}
catch(Exception)
{
// Unable to load configuration
ShowErrorMessage("Unable to load the compiler configuration file \"" + Path.GetFileName(filepath) + "\".", MessageBoxButtons.OK);
}
}
// Sort the list
nodebuilders.Sort();
}
// This loads all compiler configurations
private static void LoadAllCompilerConfigurations()
{
Configuration cfg;
IDictionary compilerslist;
string[] filenames;
// Display status
mainwindow.DisplayStatus("Loading compiler configurations...");
// Make array
compilers = new List<CompilerInfo>();
// Go for all cfg files in the compilers directory
filenames = Directory.GetFiles(compilerspath, "*.cfg", SearchOption.TopDirectoryOnly);
foreach(string filepath in filenames)
{
try
{
// Try loading the configuration
cfg = new Configuration(filepath, true);
// Check for erors
if(cfg.ErrorResult != 0)
{
// Error in configuration
ShowErrorMessage("Unable to load the compiler configuration file \"" + Path.GetFileName(filepath) + "\".\n" +
"Error near line " + cfg.ErrorLine + ": " + cfg.ErrorDescription, MessageBoxButtons.OK);
2007-09-28 08:56:18 +00:00
}
else
{
// Get structures
compilerslist = cfg.ReadSetting("compilers", new Hashtable());
foreach(DictionaryEntry de in compilerslist)
{
// Check if this is a structure
if(de.Value is IDictionary)
{
// Make compiler info
compilers.Add(new CompilerInfo(Path.GetFileName(filepath), de.Key.ToString(), cfg));
}
}
2007-09-28 08:56:18 +00:00
}
}
catch(Exception)
{
// Unable to load configuration
ShowErrorMessage("Unable to load the compiler configuration file \"" + Path.GetFileName(filepath) + "\".", MessageBoxButtons.OK);
2007-09-28 08:56:18 +00:00
}
}
}
// This returns a nodebuilder by name
public static NodebuilderInfo GetNodebuilderByName(string name)
{
// Go for all nodebuilders
foreach(NodebuilderInfo n in nodebuilders)
{
// Name matches?
if(n.Name == name) return n;
}
2007-09-28 08:56:18 +00:00
// Cannot find that nodebuilder
return null;
2007-09-28 08:56:18 +00:00
}
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Startup
2007-06-13 19:39:38 +00:00
// Main program entry
2007-06-15 10:18:03 +00:00
[STAThread]
2007-06-13 19:39:38 +00:00
public static void Main(string[] args)
{
2007-06-15 18:30:55 +00:00
Uri localpath;
Version thisversion;
2007-06-15 18:30:55 +00:00
// Get a reference to this assembly
thisasm = Assembly.GetExecutingAssembly();
thisversion = thisasm.GetName().Version;
2007-06-15 18:30:55 +00:00
2007-06-13 19:39:38 +00:00
// Find application path
2007-06-15 18:30:55 +00:00
localpath = new Uri(Path.GetDirectoryName(thisasm.GetName().CodeBase));
2007-06-13 19:39:38 +00:00
apppath = Uri.UnescapeDataString(localpath.AbsolutePath);
2007-09-28 08:56:18 +00:00
// Setup directories
2007-06-13 19:39:38 +00:00
temppath = Path.GetTempPath();
settingspath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), SETTINGS_DIR);
configspath = Path.Combine(apppath, GAME_CONFIGS_DIR);
2007-09-28 08:56:18 +00:00
compilerspath = Path.Combine(apppath, COMPILERS_DIR);
2007-10-05 07:19:57 +00:00
logfile = Path.Combine(settingspath, LOG_FILE);
2007-10-05 08:00:29 +00:00
// Make program settings directory if missing
if(!Directory.Exists(settingspath)) Directory.CreateDirectory(settingspath);
// Remove the previous log file and start logging
2007-10-05 08:00:29 +00:00
if(File.Exists(logfile)) File.Delete(logfile);
General.WriteLogLine("Doom Builder " + thisversion.Major + "." + thisversion.Minor + " startup");
General.WriteLogLine("Application path: " + apppath);
General.WriteLogLine("Temporary path: " + temppath);
General.WriteLogLine("Local settings path: " + settingspath);
General.WriteLogLine("Configurations path: " + configspath);
General.WriteLogLine("Compilers path: " + compilerspath);
2007-06-13 19:39:38 +00:00
// Load configuration
General.WriteLogLine("Loading program configuration...");
if(LoadProgramConfiguration())
{
// Create action manager
actions = new ActionManager();
// Bind static methods to actions
ActionAttribute.BindMethods(typeof(General));
// Create main window
General.WriteLogLine("Loading main interface window...");
mainwindow = new MainForm();
2007-10-14 21:31:45 +00:00
mainwindow.UpdateInterface();
// Show main window
General.WriteLogLine("Showing main interface window...");
mainwindow.Show();
mainwindow.Update();
// Load game configurations
General.WriteLogLine("Loading game configurations...");
LoadAllGameConfigurations();
// Load compiler configurations
General.WriteLogLine("Loading compiler configurations...");
LoadAllCompilerConfigurations();
// Load nodebuilder configurations
General.WriteLogLine("Loading nodebuilder configurations...");
LoadAllNodebuilderConfigurations();
// Load color settings
General.WriteLogLine("Loading color settings...");
colors = new ColorCollection(settings);
// Run application from the main window
General.WriteLogLine("Startup done");
mainwindow.DisplayReady();
Application.Run(mainwindow);
}
else
{
// Terminate
Terminate(false);
}
}
// Program configuration
private static bool LoadProgramConfiguration()
{
DialogResult result;
2007-06-13 19:39:38 +00:00
// Check if no config for this user exists yet
if(!File.Exists(Path.Combine(settingspath, SETTINGS_FILE)))
{
// Copy new configuration
General.WriteLogLine("Local user program configuration is missing!");
File.Copy(Path.Combine(apppath, SETTINGS_FILE), Path.Combine(settingspath, SETTINGS_FILE));
General.WriteLogLine("New program configuration copied for local user");
}
// Load it
settings = new Configuration(Path.Combine(settingspath, SETTINGS_FILE), true);
if(settings.ErrorResult != 0)
{
// Error in configuration
// Ask user for a new copy
result = ShowErrorMessage("Error in program configuration near line " + settings.ErrorLine + ": " + settings.ErrorDescription, MessageBoxButtons.YesNoCancel);
if(result == DialogResult.Yes)
{
// Remove old configuration and make a new copy
General.WriteLogLine("User requested a new copy of the program configuration");
File.Delete(Path.Combine(settingspath, SETTINGS_FILE));
File.Copy(Path.Combine(apppath, SETTINGS_FILE), Path.Combine(settingspath, SETTINGS_FILE));
General.WriteLogLine("New program configuration copied for local user");
// Load it
settings = new Configuration(Path.Combine(settingspath, SETTINGS_FILE), true);
if(settings.ErrorResult != 0)
{
// Error in configuration
General.WriteLogLine("Error in program configuration near line " + settings.ErrorLine + ": " + settings.ErrorDescription);
ShowErrorMessage("Default program configuration is corrupted. Please re-install Doom Builder.", MessageBoxButtons.OK);
return false;
}
}
else if(result == DialogResult.Cancel)
{
// User requested to cancel startup
General.WriteLogLine("User cancelled startup");
return false;
}
}
// Done
return true;
2007-06-13 19:39:38 +00:00
}
#endregion
#region ================== Terminate
// This terminates the program
public static void Terminate(bool properexit)
{
// Terminate properly?
if(properexit)
{
General.WriteLogLine("Termination requested");
// Unbind static methods from actions
ActionAttribute.UnbindMethods(typeof(General));
// Clean up
if(map != null) map.Dispose();
mainwindow.Dispose();
actions.Dispose();
// Save colors
colors.SaveColors(settings);
// Save action controls
actions.SaveSettings();
// Save game configuration settings
foreach(ConfigurationInfo ci in configs) ci.SaveSettings();
// Save settings configuration
General.WriteLogLine("Saving program configuration...");
settings.SaveConfiguration(Path.Combine(settingspath, SETTINGS_FILE));
// Application ends here and now
General.WriteLogLine("Termination done");
Application.Exit();
}
else
{
// Just end now
General.WriteLogLine("Immediate program termination");
Application.Exit();
}
}
#endregion
2007-06-14 23:31:57 +00:00
#region ================== Management
// This creates a new map
[Action(Action.NEWMAP)]
public static void NewMap()
2007-06-14 23:31:57 +00:00
{
MapOptions newoptions = new MapOptions();
2007-06-14 23:31:57 +00:00
MapOptionsForm optionswindow;
// Ask the user to save changes (if any)
if(General.AskSaveMap())
2007-06-16 19:53:51 +00:00
{
// Open map options dialog
optionswindow = new MapOptionsForm(newoptions);
if(optionswindow.ShowDialog(mainwindow) == DialogResult.OK)
2007-06-14 23:31:57 +00:00
{
2007-06-15 10:18:03 +00:00
// Display status
mainwindow.DisplayStatus("Creating new map...");
2007-10-14 18:11:03 +00:00
Cursor.Current = Cursors.WaitCursor;
2007-06-16 19:53:51 +00:00
2007-06-15 10:18:03 +00:00
// Clear the display
mainwindow.ClearDisplay();
2007-06-16 19:53:51 +00:00
2007-06-15 10:18:03 +00:00
// Trash the current map, if any
if(map != null) map.Dispose();
// Create map manager with given options
2007-06-15 18:30:55 +00:00
map = new MapManager();
if(map.InitializeNewMap(newoptions))
{
// Done
}
else
2007-06-15 18:30:55 +00:00
{
// Unable to create map manager
map.Dispose();
map = null;
// Show splash logo on display
mainwindow.ShowSplashDisplay();
}
2007-10-14 18:11:03 +00:00
// All done
2007-10-14 21:31:45 +00:00
mainwindow.UpdateInterface();
2007-10-14 18:11:03 +00:00
mainwindow.DisplayReady();
Cursor.Current = Cursors.Default;
2007-06-14 23:31:57 +00:00
}
2007-06-15 10:18:03 +00:00
}
}
2007-06-16 19:53:51 +00:00
// This closes the current map
[Action(Action.CLOSEMAP)]
public static void CloseMap()
{
// Ask the user to save changes (if any)
if(General.AskSaveMap())
{
// Display status
mainwindow.DisplayStatus("Closing map...");
2007-10-05 10:00:15 +00:00
General.WriteLogLine("Unloading map...");
2007-10-14 18:11:03 +00:00
Cursor.Current = Cursors.WaitCursor;
2007-10-05 10:00:15 +00:00
// Trash the current map
if(map != null) map.Dispose();
map = null;
// Show splash logo on display
mainwindow.ShowSplashDisplay();
// Done
2007-10-14 18:11:03 +00:00
Cursor.Current = Cursors.Default;
2007-10-14 21:31:45 +00:00
mainwindow.UpdateInterface();
mainwindow.DisplayReady();
2007-10-05 10:00:15 +00:00
General.WriteLogLine("Map unload done");
}
}
// This loads a map from file
[Action(Action.OPENMAP)]
public static void OpenMap()
{
OpenFileDialog openfile;
// Open map file dialog
openfile = new OpenFileDialog();
openfile.Filter = "Doom WAD Files (*.wad)|*.wad";
openfile.Title = "Open Map";
2007-10-14 15:44:55 +00:00
openfile.AddExtension = false;
openfile.CheckFileExists = true;
openfile.Multiselect = false;
openfile.ValidateNames = true;
if(openfile.ShowDialog(mainwindow) == DialogResult.OK)
{
// Update main window
mainwindow.Update();
// Open map file
OpenMapFile(openfile.FileName);
}
}
// This opens the specified file
public static void OpenMapFile(string filename)
{
OpenMapOptionsForm openmapwindow;
// Ask the user to save changes (if any)
if(General.AskSaveMap())
{
// Open map options dialog
openmapwindow = new OpenMapOptionsForm(filename);
if(openmapwindow.ShowDialog(mainwindow) == DialogResult.OK)
{
// Display status
mainwindow.DisplayStatus("Opening map file...");
2007-10-14 18:11:03 +00:00
Cursor.Current = Cursors.WaitCursor;
// Clear the display
mainwindow.ClearDisplay();
// Trash the current map, if any
if(map != null) map.Dispose();
// Create map manager with given options
map = new MapManager();
if(map.InitializeOpenMap(filename, openmapwindow.Options))
{
2007-10-15 07:50:28 +00:00
// Add recent file
mainwindow.AddRecentFile(filename);
}
else
{
// Unable to create map manager
map.Dispose();
map = null;
// Show splash logo on display
mainwindow.ShowSplashDisplay();
}
2007-10-14 18:11:03 +00:00
// All done
2007-10-14 21:31:45 +00:00
mainwindow.UpdateInterface();
2007-10-14 18:11:03 +00:00
mainwindow.DisplayReady();
Cursor.Current = Cursors.Default;
}
}
2007-06-15 10:18:03 +00:00
}
// This saves the current map
[Action(Action.SAVEMAP)]
public static void SaveMap()
{
// Check if a wad file is known
if(map.FilePathName == "")
{
2007-10-14 15:44:55 +00:00
// Call to SaveMapAs
SaveMapAs();
}
else
{
2007-10-14 18:11:03 +00:00
// Display status
mainwindow.DisplayStatus("Saving map file...");
Cursor.Current = Cursors.WaitCursor;
// Save the map
2007-10-15 07:50:28 +00:00
if(map.SaveMap(map.FilePathName, MapManager.SAVE_NORMAL))
{
// Add recent file
mainwindow.AddRecentFile(map.FilePathName);
}
2007-10-14 18:11:03 +00:00
// All done
2007-10-14 21:31:45 +00:00
mainwindow.UpdateInterface();
2007-10-14 18:11:03 +00:00
mainwindow.DisplayReady();
Cursor.Current = Cursors.Default;
}
}
2007-10-14 15:44:55 +00:00
// This saves the current map as a different file
[Action(Action.SAVEMAPAS)]
public static void SaveMapAs()
{
SaveFileDialog savefile;
// Show save as dialog
savefile = new SaveFileDialog();
savefile.Filter = "Doom WAD Files (*.wad)|*.wad";
savefile.Title = "Save Map As";
savefile.AddExtension = true;
savefile.CheckPathExists = true;
savefile.OverwritePrompt = true;
savefile.ValidateNames = true;
if(savefile.ShowDialog(mainwindow) == DialogResult.OK)
{
2007-10-14 18:11:03 +00:00
// Display status
mainwindow.DisplayStatus("Saving map file...");
Cursor.Current = Cursors.WaitCursor;
2007-10-14 15:44:55 +00:00
// Save the map
2007-10-15 07:50:28 +00:00
if(map.SaveMap(savefile.FileName, MapManager.SAVE_AS))
{
// Add recent file
mainwindow.AddRecentFile(map.FilePathName);
}
2007-10-14 18:11:03 +00:00
// All done
2007-10-14 21:31:45 +00:00
mainwindow.UpdateInterface();
2007-10-14 18:11:03 +00:00
mainwindow.DisplayReady();
Cursor.Current = Cursors.Default;
2007-10-14 15:44:55 +00:00
}
}
2007-06-15 10:18:03 +00:00
// This asks to save the map if needed
// Returns false when action was cancelled
2007-06-16 19:53:51 +00:00
public static bool AskSaveMap()
2007-06-15 10:18:03 +00:00
{
DialogResult result;
// Map open and not saved?
if((map != null) && map.IsChanged)
{
// Ask to save changes
result = MessageBox.Show(mainwindow, "Do you want to save changes to " + map.FileTitle + " (" + map.Options.CurrentName + ")?", Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if(result == DialogResult.Yes)
{
// TODO: Save map
2007-06-14 23:31:57 +00:00
2007-06-15 10:18:03 +00:00
}
else if(result == DialogResult.Cancel)
{
// Abort
return false;
}
}
// Continue
return true;
}
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Debug
2007-06-24 18:56:43 +00:00
// This outputs log information
public static void WriteLogLine(string line)
{
// Output to console
Console.WriteLine(line);
// Write to log file
File.AppendAllText(logfile, line + Environment.NewLine);
}
// This outputs log information
public static void WriteLog(string text)
{
// Output to console
Console.Write(text);
// Write to log file
File.AppendAllText(logfile, text);
}
#endregion
2007-06-24 18:56:43 +00:00
#region ================== Tools
// This returns the next power of 2
public static int NextPowerOf2(int v)
{
int p = 0;
// Continue increasing until higher than v
while(Math.Pow(2, p) < v) p++;
// Return power
return (int)Math.Pow(2, p);
}
// Convert bool to integer
public static int Bool2Int(bool v)
{
if(v) return 1; else return 0;
}
// Convert integer to bool
public static bool Int2Bool(int v)
{
return (v != 0);
}
// This shows a message and logs the message
public static DialogResult ShowErrorMessage(string message, MessageBoxButtons buttons)
{
2007-10-14 18:11:03 +00:00
Cursor oldcursor;
DialogResult result;
// Log the message
WriteLogLine(message);
2007-10-14 18:11:03 +00:00
// Use normal cursor
oldcursor = Cursor.Current;
Cursor.Current = Cursors.Default;
// Show message
2007-10-14 18:11:03 +00:00
result = MessageBox.Show(Form.ActiveForm, message, Application.ProductName, buttons, MessageBoxIcon.Error);
// Restore old cursor
Cursor.Current = oldcursor;
// Return result
return result;
}
// This shows a message and logs the message
public static DialogResult ShowWarningMessage(string message, MessageBoxButtons buttons)
2007-10-14 21:31:45 +00:00
{
return ShowWarningMessage(message, buttons, MessageBoxDefaultButton.Button1);
}
// This shows a message and logs the message
public static DialogResult ShowWarningMessage(string message, MessageBoxButtons buttons, MessageBoxDefaultButton defaultbutton)
{
2007-10-14 18:11:03 +00:00
Cursor oldcursor;
DialogResult result;
// Log the message
WriteLogLine(message);
2007-06-24 18:56:43 +00:00
2007-10-14 18:11:03 +00:00
// Use normal cursor
oldcursor = Cursor.Current;
Cursor.Current = Cursors.Default;
// Show message
2007-10-14 21:31:45 +00:00
result = MessageBox.Show(Form.ActiveForm, message, Application.ProductName, buttons, MessageBoxIcon.Warning, defaultbutton);
2007-10-14 18:11:03 +00:00
// Restore old cursor
Cursor.Current = oldcursor;
// Return result
return result;
}
2007-06-24 18:56:43 +00:00
// This returns a unique temp filename
public static string MakeTempFilename(string tempdir)
2007-06-24 18:56:43 +00:00
{
string filename;
string chars = "abcdefghijklmnopqrstuvwxyz1234567890";
Random rnd = new Random();
int i;
do
{
// Generate a filename
filename = "";
for(i = 0; i < 8; i++) filename += chars[rnd.Next(chars.Length)];
filename = Path.Combine(tempdir, filename + ".tmp");
2007-06-24 18:56:43 +00:00
}
// Continue while file is not unique
while(File.Exists(filename) || Directory.Exists(filename));
2007-06-24 18:56:43 +00:00
// Return the filename
return filename;
}
// This returns a unique temp directory name
public static string MakeTempDirname()
{
string dirname;
string chars = "abcdefghijklmnopqrstuvwxyz1234567890";
Random rnd = new Random();
int i;
do
{
// Generate a filename
dirname = "";
for(i = 0; i < 8; i++) dirname += chars[rnd.Next(chars.Length)];
dirname = Path.Combine(temppath, dirname);
}
// Continue while file is not unique
while(File.Exists(dirname) || Directory.Exists(dirname));
// Return the filename
return dirname;
}
2007-06-24 18:56:43 +00:00
#endregion
2007-06-13 19:39:38 +00:00
}
}
2007-09-28 08:56:18 +00:00