Skip to content

Commit

Permalink
Bugfix, improved context menu
Browse files Browse the repository at this point in the history
-Thread.sleep is found to be blocking the process. It is now removed.
-Context menu functionality improved
  • Loading branch information
Uchuu Tamashi committed Sep 17, 2014
1 parent 4065569 commit 2717d72
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 21 deletions.
47 changes: 33 additions & 14 deletions AZUSA/Internals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ static public void INIT()
//提示本體啟動成功, 待各引擎啟動完畢後會再有提示的
//MESSAGE(Localization.GetMessage("AZUSAREADY", "AZUSA is ready. Waiting for engines to initialize..."));


//每一個執行檔都添加為引擎
foreach (string exePath in EngList)
{
Expand All @@ -86,13 +85,7 @@ static public void INIT()
{
Internals.ERROR(Localization.GetMessage("ENGSTARTFAIL", "Unable to run {arg}. Please make sure it is in the correct folder.", exePath.Replace(EngPath + @"\", "").Replace(".exe", "").Trim()));
}

Thread.Sleep(160);
}




}

//一切就緒, 進行提示, 載入啟動腳本
Expand Down Expand Up @@ -210,11 +203,42 @@ static public void MESSAGE(string msg)
}

//增加右鍵選單的選項
static public void ADDMENUITEM(string name)
static public void ADDMENUITEM(string name,string command)
{
MenuItem itm = new MenuItem(Localization.GetMessage(name, name));
itm.Name = name;
itm.Click += new EventHandler(itm_Click);

if (command.Trim() != "")
{
//執行 command
itm.Click += new EventHandler(delegate(object sender, EventArgs e)
{
//先創建一個可運行物件, 用來儲存解析結果
MUTAN.IRunnable obj;


//然後用單行解析器
if (MUTAN.LineParser.TryParse(command, out obj))
{
//如果成功解析, 則運行物件, 獲取回傳碼
MUTAN.ReturnCode tmp = obj.Run();

//然後按回傳碼執行指令
if (tmp.Command != "")
{
Internals.Execute(tmp.Command, tmp.Argument);
}
}
});
}
else
{
//進行事件廣播
itm.Click += new EventHandler(delegate(object sender, EventArgs e)
{
ProcessManager.Broadcast(name + "Clicked");
});
}

if (notifyIcon.ContextMenu.MenuItems.Count == 5)
{
Expand All @@ -225,11 +249,6 @@ static public void ADDMENUITEM(string name)

}

static void itm_Click(object sender, EventArgs e)
{
MenuItem itm = (MenuItem)sender;
ProcessManager.Broadcast(itm.Text);
}

//這是用來暫存反應的
static string respCache = "";
Expand Down
50 changes: 43 additions & 7 deletions AZUSA/Process/IOPortedPrc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,24 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)
//activity log
ActivityLog.Add("From " + Name + ": " + e.Data);


//如果是詢問, 則調用 MUTAN 表達式解析器, 並返回結東
//詢問的語法是 "(表達式)?"
//First check if the engine is asking a question about value of an expression
if (e.Data.EndsWith("?"))
{
//首先保護進程以免受到 BROADCAST 干擾
NoBroadcast = true;

string result;

//去掉最後的問號, 就是表達式了
//如果格式有誤的話, 會返回 INVALIDEXPR (無效的表達式) 或 IMBALBRACKET (括號不平衡)
MUTAN.ExprParser.TryParse(e.Data.TrimEnd('?'), out result);
Engine.StandardInput.WriteLine(result);

//解除 BROADCAST 干擾保護
NoBroadcast = false;

//activity log
ActivityLog.Add("To " + Name + ": " + result);

Expand Down Expand Up @@ -374,13 +379,17 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)
return;
//這是用來讓進程取得 AZUSA 的 pid, 進程可以利用 pid 檢查 AZUSA 是否存活, 當 AZUSA 意外退出時, 進程可以檢查到並一併退出
case "GetAzusaPid":

//首先保護進程以免受到 BROADCAST 干擾
NoBroadcast = true;

Engine.StandardInput.WriteLine(Process.GetCurrentProcess().Id);
//activity log
ActivityLog.Add("To " + Name + ": " + Process.GetCurrentProcess().Id);

//解除 BROADCAST 干擾保護
NoBroadcast = false;

return;
//這是讓進程宣佈自己的身份的, 這指令應該是進程完成各種初始化之後才用的
case "RegisterAs":
Expand Down Expand Up @@ -424,21 +433,31 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)
return;
//這是讓進程取得當前可用所有端口
case "GetAllPorts":

//首先保護進程以免受到 BROADCAST 干擾
NoBroadcast = true;

string result = "";
foreach (KeyValuePair<string, PortType> port in ProcessManager.Ports)
{
result += port.Key + ",";
}

Engine.StandardInput.WriteLine(result.Trim(','));

//解除 BROADCAST 干擾保護
NoBroadcast = false;

//activity log
ActivityLog.Add("To " + Name + ": " + result.Trim(','));
NoBroadcast = false;

return;
//這是讓進程取得當前可用的AI 端口(AI引擎的接口)
case "GetAIPorts":

//首先保護進程以免受到 BROADCAST 干擾
NoBroadcast = true;

result = "";
foreach (KeyValuePair<string, PortType> port in ProcessManager.Ports)
{
Expand All @@ -452,13 +471,19 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)

Engine.StandardInput.WriteLine(result.Trim(','));

//解除 BROADCAST 干擾保護
NoBroadcast = false;

//activity log
ActivityLog.Add("To " + Name + ": " + result.Trim(','));
NoBroadcast = false;

return;
//這是讓進程取得當前可用的輸入端口(輸入引擎的接口)
case "GetInputPorts":

//首先保護進程以免受到 BROADCAST 干擾
NoBroadcast = true;

result = "";
foreach (KeyValuePair<string, PortType> port in ProcessManager.Ports)
{
Expand All @@ -472,13 +497,19 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)

Engine.StandardInput.WriteLine(result.Trim(','));

//解除 BROADCAST 干擾保護
NoBroadcast = false;

//activity log
ActivityLog.Add("To " + Name + ": " + result.Trim(','));
NoBroadcast = false;

return;
//這是讓進程取得當前可用的輸出端口(輸出引擎的接口)
case "GetOutputPorts":

//首先保護進程以免受到 BROADCAST 干擾
NoBroadcast = true;

result = "";
foreach (KeyValuePair<string, PortType> port in ProcessManager.Ports)
{
Expand All @@ -492,9 +523,12 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)

Engine.StandardInput.WriteLine(result.Trim(','));

//解除 BROADCAST 干擾保護
NoBroadcast = false;

//activity log
ActivityLog.Add("To " + Name + ": " + result.Trim(','));
NoBroadcast = false;

return;
//這是讓進程可以宣佈自己責負甚麼函式, AZUSA 在接收到這種函件就會轉發給進程
//函式接管不是唯一的, 可以同時有多個進程接管同一個函式, AZUSA 會每個宣告了接管的進程都轉發一遍
Expand All @@ -506,11 +540,13 @@ void Engine_OutputDataReceived(object sender, DataReceivedEventArgs e)
return;
//添加右鍵選單項目
case "AddMenuItem":
Internals.ADDMENUITEM(arg);
parsed = arg.Split(',');

Internals.ADDMENUITEM(parsed[0].Trim(),arg.Replace(parsed[0]+",","").Trim());
return;
default:
break;
}
}

//檢查整體架構是否完備, 完備或除錯模式下才執行指令
if (Internals.SysReady || Internals.Debugging)
Expand Down

0 comments on commit 2717d72

Please sign in to comment.