MoonTools.NETPhysFS/Test/Program.cs

401 lines
9.5 KiB
C#
Raw Normal View History

2016-01-27 22:04:37 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test
{
class Program
{
2016-01-28 17:49:32 +00:00
static void PrintSupportedArchives()
{
Console.Write("Supported archive types: ");
bool any = false;
foreach (var archive in PhysFS.PhysFS.SupportedArchiveTypes())
{
any = true;
Console.WriteLine("\n - {0}: {1}", archive.extension, archive.description);
Console.WriteLine(" Written by {0}", archive.author);
Console.Write(" {0}", archive.url);
}
if (!any)
{
Console.WriteLine("NONE.");
}
else
{
Console.WriteLine();
}
}
2016-01-28 18:22:46 +00:00
static IEnumerable<string> ParseInput(string input)
{
var sb = new StringBuilder();
bool openString = false;
foreach (var c in input)
{
if (char.IsWhiteSpace(c))
{
if (!openString)
{
if (sb.ToString() != "")
{
yield return sb.ToString();
}
sb.Clear();
continue;
}
else
{
sb.Append(c);
}
}
if (c == '"')
{
if (sb.ToString() != "")
{
yield return sb.ToString();
}
sb.Clear();
openString = !openString;
}
else
{
sb.Append(c);
}
}
if (sb.ToString() != "")
{
yield return sb.ToString();
}
}
2016-01-29 14:20:20 +00:00
static Dictionary<string, Func<string[], bool>> commands = new Dictionary<string, Func<string[], bool>>();
2016-01-28 18:22:46 +00:00
#region Commands
2016-01-29 14:20:20 +00:00
static bool Help(string[] args)
2016-01-28 18:22:46 +00:00
{
Console.WriteLine("Commands:");
foreach (var kvp in commands)
{
Console.WriteLine(" - {0}", kvp.Key);
}
2016-01-29 14:20:20 +00:00
return true;
2016-01-28 18:22:46 +00:00
}
2016-01-29 14:20:20 +00:00
static bool Mount(string[] args)
2016-01-28 18:22:46 +00:00
{
if (args.Length < 3)
{
Console.WriteLine("Usage: mount <archive> <mntpoint> <append>");
2016-01-29 14:20:20 +00:00
return false;
2016-01-28 18:22:46 +00:00
}
bool append;
if (!bool.TryParse(args[2], out append))
{
Console.WriteLine("append can only be true or false");
}
PhysFS.PhysFS.Mount(args[0], args[1], append);
2016-01-29 14:20:20 +00:00
return true;
2016-01-28 18:22:46 +00:00
}
2016-01-29 14:20:20 +00:00
static bool Enumerate(string[] args)
2016-01-28 18:22:46 +00:00
{
if (args.Length < 1)
{
Console.WriteLine("Usage: enumerate/ls <dir>");
2016-01-29 14:20:20 +00:00
return false;
2016-01-28 18:22:46 +00:00
}
foreach (var f in PhysFS.PhysFS.EnumerateFiles(args[0]))
{
Console.WriteLine(" - {0}", f);
}
2016-01-29 14:20:20 +00:00
return true;
2016-01-28 18:22:46 +00:00
}
2016-01-29 14:20:20 +00:00
static bool GetLastError(string[] args)
2016-01-28 18:22:46 +00:00
{
Console.WriteLine(PhysFS.PhysFS.GetLastError());
2016-01-29 14:20:20 +00:00
return true;
2016-01-28 18:22:46 +00:00
}
2016-01-28 21:02:22 +00:00
2016-01-29 14:20:20 +00:00
static bool GetDirSeparator(string[] args)
2016-01-28 21:02:22 +00:00
{
Console.WriteLine(PhysFS.PhysFS.GetDirSeparator());
2016-01-29 14:20:20 +00:00
return true;
}
static bool GetCdRomDirectories(string[] args)
{
foreach(var d in PhysFS.PhysFS.GetCdRomDirs())
{
Console.WriteLine(" - {0}", d);
}
return true;
}
static bool GetSearchPath(string[] args)
{
foreach (var d in PhysFS.PhysFS.GetSearchPath())
{
Console.WriteLine(" - {0}", d);
}
return true;
}
static bool GetBaseDirectory(string[] args)
{
Console.WriteLine(PhysFS.PhysFS.GetBaseDir());
return true;
}
static bool GetUserDirectory(string[] args)
{
Console.WriteLine(PhysFS.PhysFS.GetUserDir());
return true;
}
static bool GetWriteDirectory(string[] args)
{
Console.WriteLine(PhysFS.PhysFS.GetWriteDir());
return true;
}
static bool SetWriteDirectory(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: setwritedir <dir>");
return false;
}
PhysFS.PhysFS.SetWriteDir(args[0]);
return true;
}
static bool PermitSymlinks(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: permitsymlinks <true/false>");
return false;
}
bool permit;
if (!bool.TryParse(args[0], out permit))
{
Console.WriteLine("Usage: permitsymlinks <true/false>");
}
PhysFS.PhysFS.PermitSymbolicLinks(permit);
return true;
}
static bool SetSaneConfig(string[] args)
{
if(args.Length < 5)
{
Console.WriteLine("Usage: setsaneconfig <org> <appName> <arcExt> <includeCdRoms> <archivesFirst>");
return false;
}
bool includeCdRoms, archivesFirst;
if(bool.TryParse(args[3], out includeCdRoms) && bool.TryParse(args[4], out archivesFirst))
{
PhysFS.PhysFS.SetSaneConfig(args[0], args[1], args[2], includeCdRoms, archivesFirst);
}
else
{
Console.WriteLine("Usage: setsaneconfig <org> <appName> <arcExt> <includeCdRoms> <archivesFirst>");
}
return true;
}
static bool MkDir(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: mkdir <dir>");
return false;
}
PhysFS.PhysFS.Mkdir(args[0]);
return true;
2016-01-28 21:02:22 +00:00
}
2016-01-29 14:20:20 +00:00
static bool Delete(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: delete <dir>");
return false;
}
PhysFS.PhysFS.Delete(args[0]);
return true;
}
static bool GetRealDir(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: getrealdir <dir>");
return false;
}
Console.WriteLine(PhysFS.PhysFS.GetRealDir(args[0]));
return true;
}
static bool Exists(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: exists <file>");
return false;
}
Console.WriteLine(PhysFS.PhysFS.Exists(args[0]));
return true;
}
static bool IsDir(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: isdir <path>");
return false;
}
Console.WriteLine(PhysFS.PhysFS.IsDirectory(args[0]));
return true;
}
static bool IsSymlink(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: issymlink <path>");
return false;
}
Console.WriteLine(PhysFS.PhysFS.IsSymbolicLink(args[0]));
return true;
}
static bool Cat(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: cat <file>");
return false;
}
using (var reader = new System.IO.StreamReader(new PhysFS.PhysFSStream(args[0], PhysFS.OpenMode.Read)))
{
Console.WriteLine(reader.ReadToEnd());
}
return true;
}
static bool FileLength(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: filelength <file>");
return false;
}
using (var stream = new PhysFS.PhysFSStream(args[0], PhysFS.OpenMode.Read))
{
Console.WriteLine(stream.Length);
}
return true;
}
2016-01-28 18:22:46 +00:00
#endregion
2016-01-27 22:04:37 +00:00
static void Main(string[] args)
{
2016-01-28 17:49:32 +00:00
try
{
PhysFS.PhysFS.InitializeCallbacks();
}
catch (PhysFS.PhysFSLibNotFound)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("ERROR: PhysFS could not be loaded. Are you sure it is installed or a suitable module is in your working directory?");
return;
}
2016-01-27 22:04:37 +00:00
PhysFS.PhysFS.Init("");
2016-01-28 17:49:32 +00:00
var version = PhysFS.PhysFS.GetLinkedVersion();
Console.WriteLine("SharpPhysFS Test console");
Console.WriteLine("Loaded PhysFS version: {0}.{1}.{2}", version.major, version.minor, version.patch);
PrintSupportedArchives();
Console.WriteLine("Type 'help' for a list of commands");
2016-01-28 18:22:46 +00:00
commands.Add("help", Help);
commands.Add("mount", Mount);
commands.Add("enumerate", Enumerate);
commands.Add("ls", Enumerate);
2016-01-28 21:02:22 +00:00
commands.Add("getdirsep", GetDirSeparator);
2016-01-29 14:20:20 +00:00
commands.Add("getcdromdirs", GetCdRomDirectories);
commands.Add("getsearchpath", GetSearchPath);
commands.Add("getbasedir", GetBaseDirectory);
commands.Add("getuserdir", GetUserDirectory);
commands.Add("getwritedir", GetWriteDirectory);
commands.Add("setwritedir", SetWriteDirectory);
commands.Add("permitsymlinks", PermitSymlinks);
commands.Add("setsaneconfig", SetSaneConfig);
commands.Add("mkdir", MkDir);
commands.Add("delete", Delete);
commands.Add("getrealdir", GetRealDir);
commands.Add("exists", Exists);
commands.Add("isdir", IsDir);
commands.Add("issymlink", IsSymlink);
commands.Add("cat", Cat);
commands.Add("filelength", FileLength);
2016-01-28 18:22:46 +00:00
while (true)
2016-01-28 17:49:32 +00:00
{
Console.Write("> ");
var input = Console.ReadLine();
2016-01-28 18:22:46 +00:00
var split = ParseInput(input);
if (split.Count() == 0)
{
continue;
}
if (split.First() == "quit")
{
break;
}
else
{
2016-01-29 14:20:20 +00:00
Func<string[], bool> cmd;
2016-01-28 18:22:46 +00:00
if (commands.TryGetValue(split.First(), out cmd))
{
try
{
2016-01-29 14:20:20 +00:00
if(cmd(split.Skip(1).ToArray()))
{
Console.WriteLine("Done.");
}
2016-01-28 18:22:46 +00:00
}
catch (PhysFS.PhysFSException e)
{
2016-01-29 14:20:20 +00:00
Console.ForegroundColor = ConsoleColor.Red;
2016-01-28 18:22:46 +00:00
Console.Error.WriteLine("ERROR: {0}", e.Message);
2016-01-29 14:20:20 +00:00
Console.ForegroundColor = ConsoleColor.Gray;
2016-01-28 18:22:46 +00:00
}
}
else
{
Console.Error.WriteLine("Invalid command");
}
}
2016-01-28 17:49:32 +00:00
}
2016-01-27 22:04:37 +00:00
PhysFS.PhysFS.Deinit();
}
}
}