Skip to content

Commit

Permalink
Added visual lockdown for CLI read-only mode
Browse files Browse the repository at this point in the history
Merge pull request #9 from JohnnyCrazy/issue8 (closes #8)
  • Loading branch information
whisperity committed Dec 21, 2013
1 parent b0cc251 commit 05390f1
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 21 deletions.
11 changes: 6 additions & 5 deletions SharpGMad/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ private void LoadAddon(string path)

if (!AddonHandle.CanWrite)
{
DialogResult openReadOnly = MessageBox.Show("This addon is locked by another process.\n\n" +
"Would you like to open it in read-only mode?", "Addon locked",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
DialogResult openReadOnly = MessageBox.Show("This addon is locked by another process, " +
"and cannot be written.\n\n" +
"Would you like to open it in read-only mode?\nAll modification options will be disabled.",
"Addon locked", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (openReadOnly != DialogResult.Yes)
{
Expand Down Expand Up @@ -120,7 +121,7 @@ private void LoadAddon(string path)
UpdateMetadataPanel();
UpdateFileList();
UpdateModified();
UpdateStatus("Loaded the addon");
UpdateStatus("Loaded the addon" + (AddonHandle.CanWrite ? null : " (read-only mode)"));

tsbAddFile.Enabled = AddonHandle.CanWrite;
tsbUpdateMetadata.Enabled = AddonHandle.CanWrite;
Expand All @@ -143,7 +144,7 @@ private void UpdateModified()
}
else
{
this.Text = Path.GetFileName(AddonHandle.AddonPath) + (AddonHandle.CanWrite ? null : " (readonly)") +
this.Text = Path.GetFileName(AddonHandle.AddonPath) + (AddonHandle.CanWrite ? null : " (read-only)") +
(AddonHandle.Modified ? "*" : null) + " - SharpGMad";

tsbSaveAddon.Enabled = AddonHandle.CanWrite && AddonHandle.Modified;
Expand Down
4 changes: 2 additions & 2 deletions SharpGMad/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1")]
[assembly: AssemblyFileVersion("1.1")]
[assembly: AssemblyVersion("1.1.1")]
[assembly: AssemblyFileVersion("1.1.1")]
163 changes: 149 additions & 14 deletions SharpGMad/RealtimeCommandline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static int Main(string[] args)
}
else if (AddonHandle is RealtimeAddon)
{
Console.Write(Path.GetFileName(AddonHandle.AddonPath) +
Console.Write(Path.GetFileName(AddonHandle.AddonPath) + (AddonHandle.CanWrite ? null : " (read-only)") +
(AddonHandle.Modified ? "*" : null) + (AddonHandle.Pullable ? "#" : null) + "> ");
}

Expand Down Expand Up @@ -217,6 +217,14 @@ public static int Main(string[] args)

break;
case "export":
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Addon read-only. Use `extract` to unpack files from it.");
Console.ResetColor();
break;
}

string exportPath = String.Empty;
try
{
Expand Down Expand Up @@ -253,6 +261,14 @@ public static int Main(string[] args)

break;
case "pull":
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
break;
}

try
{
PullFile(command[1]);
Expand Down Expand Up @@ -330,6 +346,14 @@ public static int Main(string[] args)

break;
case "set":
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
break;
}

string Sparameter;
try
{
Expand Down Expand Up @@ -560,20 +584,32 @@ public static int Main(string[] args)

if (AddonHandle is RealtimeAddon)
{
Console.WriteLine("add <filename> Adds <filename> to the archive");
Console.WriteLine("addfolder <folder> Adds all files from <folder> to the archive");
if (AddonHandle.CanWrite)
{
Console.WriteLine("add <filename> Adds <filename> to the archive");
Console.WriteLine("addfolder <folder> Adds all files from <folder> to the archive");
}
Console.WriteLine("list Lists the files in the memory");
Console.WriteLine("remove <filename> Removes <filename> from the archive");
if (AddonHandle.CanWrite)
{
Console.WriteLine("remove <filename> Removes <filename> from the archive");
}
Console.WriteLine("extract <filename> [path] Extract <filename> (to [path] if specified)");
Console.WriteLine("mget <folder> <f1> [f2...] Extract all specified files to <folder>");
Console.WriteLine("export View the list of exported files");
Console.WriteLine("export <filename> [path] Export <filename> for editing (to [path] if specified)");
Console.WriteLine("pull Pull changes from all exported files");
Console.WriteLine("pull <filename> Pull the changes of exported <filename>");
Console.WriteLine("drop <filename> Drops the export for <filename>");
if (AddonHandle.CanWrite)
{
Console.WriteLine("export View the list of exported files");
Console.WriteLine("export <filename> [path] Export <filename> for editing (to [path] if specified)");
Console.WriteLine("pull Pull changes from all exported files");
Console.WriteLine("pull <filename> Pull the changes of exported <filename>");
Console.WriteLine("drop <filename> Drops the export for <filename>");
}
Console.WriteLine("get <parameter> Prints the value of metadata <parameter>");
Console.WriteLine("set <parameter> [value] Sets metadata <parameter> to the specified [value]");
Console.WriteLine("push Writes the changes to the disk");
if (AddonHandle.CanWrite)
{
Console.WriteLine("set <parameter> [value] Sets metadata <parameter> to the specified [value]");
Console.WriteLine("push Writes the changes to the disk");
}
Console.WriteLine("shellexec <path> Execute the specified file");
Console.WriteLine("close Closes the addon (dropping all changes)");
Console.WriteLine("path Prints the full path of the current addon.");
Expand Down Expand Up @@ -629,9 +665,13 @@ public static int Main(string[] args)
return 0;
//break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unknown operation.");
Console.ResetColor();
if (!String.IsNullOrWhiteSpace(command[0]))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unknown operation.");
Console.ResetColor();
}

break;
}
}
Expand Down Expand Up @@ -694,6 +734,14 @@ static void NewAddon(string filename)
/// <param name="title">Optional. The new title the addon should have.</param>
private static void SetTitle(string title = null)
{
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

if (title == String.Empty || title == null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Expand All @@ -712,6 +760,14 @@ private static void SetTitle(string title = null)
/// <param name="description">Optional. The new description the addon should have.</param>
private static void SetDescription(string description = null)
{
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

if (description == String.Empty || description == null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Expand All @@ -730,6 +786,14 @@ private static void SetDescription(string description = null)
/// <param name="author">Optional. The new author the addon should have.</param>
private static void SetAuthor(string author = null)
{
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}
if (author == String.Empty || author == null)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Expand All @@ -750,6 +814,14 @@ private static void SetAuthor(string author = null)
/// <param name="type">Optional. The new type the addon should have.</param>
private static void SetType(string type = null)
{
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

if (type == String.Empty || type == null)
{
while (!Tags.TypeExists(type))
Expand Down Expand Up @@ -790,6 +862,14 @@ private static void SetType(string type = null)
/// <param name="tagsInput">Optional. The new tags the addon should have.</param>
private static void SetTags(string[] tagsInput = null)
{
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

List<string> tags = new List<string>(2);
if (tagsInput == null || tagsInput.Length == 0 || tagsInput[0] == String.Empty)
{
Expand Down Expand Up @@ -926,6 +1006,13 @@ private static void LoadAddon(string filename)
{
Console.WriteLine("\t" + f.Path + " [" + ((int)f.Size).HumanReadableSize() + "]");
}

if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("File can not be written. Addon opened read-only.");
Console.ResetColor();
}
}

/// <summary>
Expand All @@ -942,6 +1029,14 @@ private static void AddFile(string filename)
return;
}

if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

Console.WriteLine(filename + " as ");
Console.WriteLine("\t" + Whitelist.GetMatchingString(filename));

Expand Down Expand Up @@ -993,6 +1088,14 @@ private static void AddFile(string filename)
/// <param name="folder">The folder containing the files to be added.</param>
private static void AddFolder(string folder)
{
if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

if (folder == String.Empty)
{
folder = Directory.GetCurrentDirectory();
Expand Down Expand Up @@ -1041,6 +1144,14 @@ private static void RemoveFile(string filename)
return;
}

if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

try
{
AddonHandle.RemoveFile(filename);
Expand Down Expand Up @@ -1119,6 +1230,14 @@ private static void ExportFile(string filename, string exportPath = null)
return;
}

if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

try
{
AddonHandle.ExportFile(filename, exportPath);
Expand Down Expand Up @@ -1228,6 +1347,14 @@ private static void PullFile(string filename)
return;
}

if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

try
{
AddonHandle.Pull(filename);
Expand Down Expand Up @@ -1264,6 +1391,14 @@ private static void Push()
return;
}

if (!AddonHandle.CanWrite)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Cannot modify a read-only addon.");
Console.ResetColor();
return;
}

foreach (ContentFile f in AddonHandle.OpenAddon.Files)
{
Console.WriteLine("File index: " + f.Path.TrimStart('/') + " [CRC: " + f.CRC + "] [Size:" + ((int)f.Size).HumanReadableSize() + "]");
Expand Down

0 comments on commit 05390f1

Please sign in to comment.