Files
cuetools.net/CUETools/Program.cs

161 lines
4.3 KiB
C#
Raw Normal View History

2008-10-13 19:25:11 +00:00
using System;
using System.Collections.Generic;
2009-03-23 15:46:26 +00:00
using System.Globalization;
using System.IO;
2009-03-22 16:59:05 +00:00
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Ipc;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CUETools.Processor;
using CUETools.Processor.Settings;
2008-10-13 19:25:11 +00:00
namespace JDP
{
2008-10-13 19:25:11 +00:00
static class Program {
[STAThread]
static void Main(string[] args)
{
2009-08-06 13:03:02 +00:00
if (args.Length > 1 && args[0].Length > 1 && args[0][0] == '/')
2008-10-13 19:25:11 +00:00
{
2009-03-22 16:59:05 +00:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2008-10-13 19:25:11 +00:00
frmBatch batch = new frmBatch();
2009-08-06 13:03:02 +00:00
batch.Profile = args[0].Substring(1);
2008-12-01 20:24:16 +00:00
if (args.Length == 2 && args[1][0] != '@')
batch.InputPath = args[1];
else for (int i = 1; i < args.Length; i++)
{
if (args[i][0] == '@')
{
string lineStr;
StreamReader sr;
try
{
sr = new StreamReader(args[i].Substring(1), Encoding.Default);
while ((lineStr = sr.ReadLine()) != null)
batch.AddInputPath(lineStr);
}
catch
{
batch.AddInputPath(args[i]);
}
} else
batch.AddInputPath(args[i]);
}
Application.Run(batch);
2008-10-13 19:25:11 +00:00
return;
}
2009-03-22 16:59:05 +00:00
string myId = "BZ92759C-63Q7-444e-ADA6-E495634A493D";
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2009-05-01 15:16:26 +00:00
2009-03-22 16:59:05 +00:00
CUEConfig config = new CUEConfig();
2009-05-01 15:16:26 +00:00
config.Load(new SettingsReader("CUE Tools", "settings.txt", Application.ExecutablePath));
2009-03-28 03:57:49 +00:00
try { Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(config.language); }
catch { }
2009-03-23 15:46:26 +00:00
frmCUETools form = new frmCUETools();
2009-03-22 16:59:05 +00:00
if (!config.oneInstance || SingletonController.IamFirst(myId, new SingletonController.ReceiveDelegate(form.OnSecondCall)))
{
if (args.Length == 1)
form.InputPath = args[0];
Application.Run(form);
}
else
{
List<string> newArgs = new List<string>();
foreach (string arg in args)
newArgs.Add(Path.GetFullPath(arg));
SingletonController.Send(myId, newArgs.ToArray());
}
SingletonController.Cleanup();
2008-10-13 19:25:11 +00:00
}
}
2009-03-22 16:59:05 +00:00
[Serializable]
class SingletonController : MarshalByRefObject
{
private static IpcChannel m_IPCChannel = null;
2009-05-01 15:16:26 +00:00
public delegate bool ReceiveDelegate(string[] args);
2009-03-22 16:59:05 +00:00
static private ReceiveDelegate m_Receive = null;
static public ReceiveDelegate Receiver
{
get
{
return m_Receive;
}
set
{
m_Receive = value;
}
}
public static bool IamFirst(string id, ReceiveDelegate r)
{
if (IamFirst(id))
{
Receiver += r;
return true;
}
else
{
return false;
}
}
public static bool IamFirst(string id)
{
try
{
m_IPCChannel = new IpcChannel(id);
}
catch
{
return false;
}
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(m_IPCChannel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SingletonController),
"SingletonController",
WellKnownObjectMode.SingleCall);
return true;
}
public static void Cleanup()
{
if (m_IPCChannel != null)
m_IPCChannel.StopListening(null);
m_IPCChannel = null;
}
public static void Send(string id, string[] s)
{
SingletonController ctrl;
IpcChannel channel = new IpcChannel();
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel, false);
2009-05-01 15:16:26 +00:00
bool result = false;
2009-03-22 16:59:05 +00:00
try
{
ctrl = (SingletonController)Activator.GetObject(typeof(SingletonController), "ipc://" + id + "/SingletonController");
2009-05-01 15:16:26 +00:00
result = ctrl.Receive(s);
2009-03-22 16:59:05 +00:00
}
catch
{
2009-05-01 15:16:26 +00:00
}
if (!result)
2009-03-22 16:59:05 +00:00
MessageBox.Show("Another instance of the application seems to be running, but not responding.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
2009-05-01 15:16:26 +00:00
}
2009-03-22 16:59:05 +00:00
2009-05-01 15:16:26 +00:00
public bool Receive(string[] s)
2009-03-22 16:59:05 +00:00
{
2009-05-01 15:16:26 +00:00
if (m_Receive == null)
return false;
return m_Receive(s);
2009-03-22 16:59:05 +00:00
}
}
2008-10-13 19:25:11 +00:00
}