Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new, empty addon and encapsulates it within a realtime instance.
        /// </summary>
        /// <param name="filename">The path of the addon file to create.</param>
        /// <returns>A RealtimeAddon instance.</returns>
        /// <exception cref="UnauthorizedAccessException">The specified file already exists on the local filesystem.</exception>
        /// <exception cref="IOException">There was an error creating a specified file.</exception>
        public static RealtimeAddon New(string filename)
        {
            if (File.Exists(filename))
            {
                throw new UnauthorizedAccessException("The file already exists.");
            }

            if (Path.GetExtension(filename) != "gma")
            {
                filename  = Path.GetFileNameWithoutExtension(filename);
                filename += ".gma";
            }

            FileStream fs;

            try
            {
                fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                throw;
            }

            Addon addon = new Addon();

            RealtimeAddon realtime = new RealtimeAddon(addon, fs);

            return(realtime);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new, empty addon and encapsulates it within a realtime instance.
        /// </summary>
        /// <param name="filename">The path of the addon file to create.</param>
        /// <returns>A RealtimeAddon instance.</returns>
        /// <exception cref="UnauthorizedAccessException">The specified file already exists on the local filesystem.</exception>
        /// <exception cref="IOException">There was an error creating a specified file.</exception>
        public static RealtimeAddon New(string filename)
        {
            if (File.Exists(filename))
            {
                throw new UnauthorizedAccessException("The file already exists.");
            }

            if (Path.GetExtension(filename) != "gma")
            {
                filename = Path.GetFileNameWithoutExtension(filename);
                filename += ".gma";
            }

            FileStream fs;
            try
            {
                fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                throw;
            }

            Addon addon = new Addon();

            RealtimeAddon realtime = new RealtimeAddon(addon, fs);
            return realtime;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads the specified addon from the local filesystem and encapsulates it within a realtime instance.
        /// </summary>
        /// <param name="filename">The path to the file on the local filesystem.</param>
        /// <returns>A RealtimeAddon instance.</returns>
        /// <exception cref="FileNotFoundException">Happens if the specified file does not exist.</exception>
        /// <exception cref="IOException">Thrown if there is a problem opening the specified file.</exception>
        /// <exception cref="ReaderException">Thrown if the addon reader and parser encounters an error.</exception>
        /// <exception cref="ArgumentException">Happens if a file with the same path is already added.</exception>
        /// <exception cref="WhitelistException">There is a file prohibited from storing by the global whitelist.</exception>
        /// <exception cref="IgnoredException">There is a file prohibited from storing by the addon's ignore list.</exception>
        public static RealtimeAddon Load(string filename)
        {
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("The specified file " + filename + " does not exist.");
            }

            FileStream fs;
            try
            {
                fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                throw;
            }

            Reader r;
            try
            {
                r = new Reader(fs);
            }
            catch (IOException)
            {
                throw;
            }
            catch (ReaderException)
            {
                throw;
            }

            Addon addon;
            try
            {
                addon = new Addon(r);
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (WhitelistException)
            {
                throw;
            }
            catch (IgnoredException)
            {
                throw;
            }

            RealtimeAddon realtime = new RealtimeAddon(addon, fs);
            realtime.AddonReader = r;
            return realtime;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new addon.
        /// </summary>
        /// <param name="filename">The filename where the addon should be saved to.</param>
        static void NewAddon(string filename)
        {
            if (AddonHandle is RealtimeAddon)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("An addon is already open. Please close it first.");
                Console.ResetColor();
                return;
            }

            try
            {
                AddonHandle = RealtimeAddon.New(filename);
            }
            catch (UnauthorizedAccessException)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("The file already exists. You might want to load it instead.");
                Console.ResetColor();
                return;
            }
            catch (IOException e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("There was a problem creating the file.");
                Console.ResetColor();
                Console.WriteLine(e.Message);
                CloseAddon();
                return;
            }

            Console.WriteLine("Successfully created the new addon...");

            SetTitle();
            SetDescription();
            //SetAuthor(); // Author name setting is not yet implemented.
            SetType();
            SetTags();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Addon metadata setup finished.");
            Console.ResetColor();

            // Write initial content
            Push();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Loads an addon from the filesystem.
        /// </summary>
        /// <param name="filename">The path of the addon to load.</param>
        private static void LoadAddon(string filename)
        {
            if (AddonHandle is RealtimeAddon)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("An addon is already open. Please close it first.");
                Console.ResetColor();
                return;
            }

            Console.WriteLine("Loading file...");
            try
            {
                AddonHandle = RealtimeAddon.Load(filename);
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("There was a problem opening the file.");
                Console.ResetColor();
                Console.WriteLine(e.Message);
                CloseAddon();
                return;
            }

            foreach (ContentFile f in AddonHandle.OpenAddon.Files)
            {
                Console.WriteLine("\t" + f.Path + " [" + ((int)f.Size).HumanReadableSize() + "]");
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Closes the currently open addon connection.
        /// </summary>
        private static void CloseAddon()
        {
            if (AddonHandle == null)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("No addon is open.");
                Console.ResetColor();
                return;
            }

            AddonHandle.Close();
            AddonHandle = null;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Loads the specified addon from the local filesystem and encapsulates it within a realtime instance.
        /// </summary>
        /// <param name="filename">The path to the file on the local filesystem.</param>
        /// <param name="readOnly">True if the file is to be opened read-only, false otherwise</param>
        /// <returns>A RealtimeAddon instance.</returns>
        /// <exception cref="FileNotFoundException">Happens if the specified file does not exist.</exception>
        /// <exception cref="IOException">Thrown if there is a problem opening the specified file.</exception>
        /// <exception cref="ReaderException">Thrown if the addon reader and parser encounters an error.</exception>
        /// <exception cref="ArgumentException">Happens if a file with the same path is already added.</exception>
        /// <exception cref="WhitelistException">There is a file prohibited from storing by the global whitelist.</exception>
        /// <exception cref="IgnoredException">There is a file prohibited from storing by the addon's ignore list.</exception>
        public static RealtimeAddon Load(string filename, bool readOnly = false)
        {
            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("The specified file " + filename + " does not exist.");
            }

            FileStream fs = null;

            try
            {
                if (!readOnly)
                {
                    fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                }
                else
                {
                    fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                }
            }
            catch (IOException)
            {
                if (fs != null)
                {
                    fs.Dispose();
                }

                throw;
            }

            Reader r;

            try
            {
                r = new Reader(fs);
            }
            catch (IOException)
            {
                fs.Dispose();
                throw;
            }
            catch (ReaderException)
            {
                fs.Dispose();
                throw;
            }

            Addon addon;

            try
            {
                addon = new Addon(r);
            }
            catch (ArgumentException)
            {
                fs.Dispose();
                throw;
            }
            catch (WhitelistException)
            {
                fs.Dispose();
                throw;
            }
            catch (IgnoredException)
            {
                fs.Dispose();
                throw;
            }

            RealtimeAddon realtime = new RealtimeAddon(addon, fs);

            realtime.AddonReader = r;
            return(realtime);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Loads an addon from the filesystem.
        /// </summary>
        /// <param name="filename">The path of the addon to load.</param>
        private void LoadAddon(string path)
        {
            try
            {
                AddonHandle = RealtimeAddon.Load(path);
            }
            catch (FileNotFoundException)
            {
                return;
            }
            catch (IgnoredException e)
            {
                MessageBox.Show(e.Message, "Addon is corrupted", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (WhitelistException e)
            {
                MessageBox.Show(e.Message, "Addon is corrupted", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            catch (ArgumentException e)
            {
                MessageBox.Show(e.Message, "Addon is corrupted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            catch (IOException ex)
            {
                MessageBox.Show("Unable to load the addon.\nAn exception happened.\n\n" + ex.Message, "Addon reading error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (ReaderException ex)
            {
                MessageBox.Show("There was an error parsing the file.\n\n" + ex.Message, "Addon reading error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (AddonHandle is RealtimeAddon)
            {
                UpdateMetadataPanel();
                UpdateFileList();
                UpdateModified();
                UpdateStatus("Loaded the addon");

                tsbAddFile.Enabled = true;
                tsbUpdateMetadata.Enabled = true;
            }
        }
Ejemplo n.º 9
0
        private void tsbCreateAddon_Click(object sender, EventArgs e)
        {
            DialogResult dropChanges = new DialogResult();
            if (AddonHandle is RealtimeAddon && AddonHandle.Modified)
            {
                dropChanges = MessageBox.Show("Open an another addon without saving the current one?\nYou'll lose the changes.",
                    "Addon already open", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }

            if (dropChanges == DialogResult.Yes || AddonHandle == null ||
                (AddonHandle is RealtimeAddon && !AddonHandle.Modified))
            {
                UnloadAddon();
                DialogResult file = sfdAddon.ShowDialog();

                if (file == DialogResult.OK)
                {
                    try
                    {
                        AddonHandle = RealtimeAddon.New(sfdAddon.FileName);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("There was a problem creating the addon.", "Create new addon",
                            MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        return;
                    }

                    AddonHandle.OpenAddon.Title = Path.GetFileNameWithoutExtension(sfdAddon.FileName);
                    AddonHandle.OpenAddon.Author = "Author Name"; // This is currently not changable
                    AddonHandle.OpenAddon.Description = String.Empty;
                    AddonHandle.OpenAddon.Type = String.Empty;
                    AddonHandle.OpenAddon.Tags = new List<string>();
                    tsbUpdateMetadata_Click(sender, e); // This will make the metadata change enabled to set the initial values

                    // But the "Discard" button must be disabled so that the user cannot leave the metadata blank
                    tsbDiscardMetadataChanges.Enabled = false;
                    tsbDiscardMetadataChanges.Visible = false;

                    UpdateModified();
                    UpdateMetadataPanel();
                    UpdateFileList();

                    tsbAddFile.Enabled = true;
                    tsbUpdateMetadata.Enabled = true;
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Closes the currently open addon connection.
        /// </summary>
        private void UnloadAddon()
        {
            txtMetadataTitle.Text = String.Empty;
            txtMetadataAuthor.Text = String.Empty;
            cmbMetadataType.Items.Clear();
            cmbMetadataTag1.Items.Clear();
            cmbMetadataTag2.Items.Clear();
            txtMetadataDescription.Text = String.Empty;

            lstFiles.Items.Clear();
            lstFiles.Groups.Clear();

            this.Text = "SharpGMad";
            tsbSaveAddon.Enabled = false;

            if (AddonHandle != null)
                AddonHandle.Close();
            AddonHandle = null;

            tsbUpdateMetadata.Enabled = false;
            tsbDiscardMetadataChanges_Click(null, new EventArgs());
            tsbAddFile.Enabled = false;
        }