Skip to content

Commit

Permalink
Merge pull request #142 from Lady-Binary/r-1-2-1-0
Browse files Browse the repository at this point in the history
R 1 2 1 0
  • Loading branch information
Lady-Binary authored Jan 11, 2021
2 parents 144ce99 + 54e06e2 commit f161f03
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 30 deletions.
3 changes: 1 addition & 2 deletions EasyLoU/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

273 changes: 263 additions & 10 deletions EasyLoU/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace EasyLoU
Expand Down Expand Up @@ -384,7 +386,8 @@ private void DoOpen()
DoNew();
ScriptsTab.SelectedTab.Text = fileName;
TextEditorControlEx ScriptTextArea = ((TextEditorControlEx)ScriptsTab.SelectedTab.Controls.Find("ScriptTextArea", true)[0]);
ScriptTextArea.Tag = filePath;
string sha1 = CalcSha1(filePath);
ScriptTextArea.Tag = new Tuple<string, string>(filePath, sha1);
ScriptTextArea.LoadFile(filePath);
}
}
Expand All @@ -395,25 +398,215 @@ private void DoOpen()

}

private static string CalcSha1(string filePath)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
using (BufferedStream bs = new BufferedStream(fs))
{
using (SHA1Managed sha1 = new SHA1Managed())
{
byte[] hash = sha1.ComputeHash(bs);
StringBuilder formatted = new StringBuilder(2 * hash.Length);
foreach (byte b in hash)
{
formatted.AppendFormat("{0:X2}", b);
}
return formatted.ToString();
}
}
} catch (Exception ex)
{
return "";
}
}

private bool IsFileChanged(int TabIndex)
{
TabPage ScriptPage = ScriptsTab.TabPages[TabIndex];
TextEditorControlEx ScriptTextArea = ((TextEditorControlEx)ScriptPage.Controls.Find("ScriptTextArea", true)[0]);

if (ScriptTextArea.Tag != null && ScriptTextArea.Tag.ToString() != "new")
{
if (ScriptTextArea.Tag is Tuple<string, string> Tag && Tag.Item1 != "" && File.Exists(Tag.Item1))
{
string filePath = Tag.Item1;
string oldSha1 = Tag.Item2;
string newSha1 = CalcSha1(filePath);
if (oldSha1 != newSha1)
{
return true;
}
}
}
return false;
}

private void AskForReload(int TabIndex)
{
TabPage ScriptPage = ScriptsTab.TabPages[TabIndex];
TextEditorControlEx ScriptTextArea = ((TextEditorControlEx)ScriptPage.Controls.Find("ScriptTextArea", true)[0]);
if (ScriptTextArea.Tag != null && ScriptTextArea.Tag.ToString() != "new")
{
if (ScriptTextArea.Tag is Tuple<string, string> Tag && Tag.Item1 != "" && File.Exists(Tag.Item1))
{
string filePath = Tag.Item1;
string oldSha1 = Tag.Item2;
string newSha1 = CalcSha1(filePath);
if (oldSha1 != newSha1)
{
ScriptDebuggers.TryGetValue(ScriptPage.Tag.ToString(), out ScriptDebugger Debugger);
DialogResult confirmResult;
if (Debugger != null && (Debugger.Status == ScriptDebugger.DebuggerStatus.Paused || Debugger.Status == ScriptDebugger.DebuggerStatus.Running))
{
confirmResult = MessageBoxEx.Show(MainForm.TheMainForm, $"{ScriptTextArea.FileName} changed on disk. Stop it and reload it from disk?", "Confirm stop and reload", MessageBoxButtons.YesNo);
}
else
{
confirmResult = MessageBoxEx.Show(MainForm.TheMainForm, $"{ScriptTextArea.FileName} changed on disk. Reload it from disk?", "Confirm reload", MessageBoxButtons.YesNo);
}
if (confirmResult == DialogResult.Yes)
{
if (Debugger != null)
{
Debugger.Stop();
}
RefreshToolStripStatus();

ScriptTextArea.LoadFile(ScriptTextArea.FileName);
ScriptTextArea.Tag = new Tuple<string, string>(filePath, newSha1);
}
}
}
}
}

private bool CheckAnyFileChanged()
{
bool FileChanged = false;

int SelectedTabIndex = ScriptsTab.SelectedIndex;
for (int TabIndex = 0; TabIndex < ScriptsTab.TabCount - 1; TabIndex++)
{
try
{
if (IsFileChanged(TabIndex))
{
ScriptsTab.SelectedIndex = TabIndex;
AskForReload(TabIndex);
}
} catch (Exception ex)
{

}
}
ScriptsTab.SelectedIndex = SelectedTabIndex;

return FileChanged;
}

private delegate void LoadFileThreadSafeDelegate(TextEditorControlEx control, string propertyName, string propertyValue);
public static void LoadFileThreadSafe(TextEditorControlEx control, string propertyName, string propertyValue)
{
if (control.InvokeRequired)
{
control.Invoke(new LoadFileThreadSafeDelegate(LoadFileThreadSafe), new object[] { control, propertyName, propertyValue });
}
else
{
do
{
Thread.Sleep(100);
}
while (IsFileLocked(control.FileName));

control.LoadFile(control.FileName);
}
}

private static bool IsFileLocked(String filePath)
{
FileInfo file = new FileInfo(filePath);
FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
if (stream != null)
stream.Close();
}

return false;
}

private void DoReopen()
{
MessageBoxEx.Show(MainForm.TheMainForm, "Not implemented!");
int SelectedTabIndex = ScriptsTab.SelectedIndex;
TabPage ScriptPage = ScriptsTab.TabPages[SelectedTabIndex];
TextEditorControlEx ScriptTextArea = ((TextEditorControlEx)ScriptPage.Controls.Find("ScriptTextArea", true)[0]);

if (ScriptTextArea.Tag != null && ScriptTextArea.Tag.ToString() != "new")
{
if (ScriptTextArea.Tag is Tuple<string, string> Tag && Tag.Item1 != "" && File.Exists(Tag.Item1))
{
ScriptDebuggers.TryGetValue(ScriptPage.Tag.ToString(), out ScriptDebugger Debugger);
DialogResult confirmResult;
if (Debugger != null && (Debugger.Status == ScriptDebugger.DebuggerStatus.Paused || Debugger.Status == ScriptDebugger.DebuggerStatus.Running))
{
confirmResult = MessageBoxEx.Show(MainForm.TheMainForm, $"Are you sure you want to stop and reload {ScriptTextArea.FileName}?", "Confirm stop and reload", MessageBoxButtons.YesNo);
}
else
{
confirmResult = MessageBoxEx.Show(MainForm.TheMainForm, $"Are you sure you want to reload {ScriptTextArea.FileName}?", "Confirm reload", MessageBoxButtons.YesNo);
}
if (confirmResult == DialogResult.Yes)
{
if (Debugger != null)
{
Debugger.Stop();
}
RefreshToolStripStatus();

string filePath = Tag.Item1;
string sha1 = CalcSha1(filePath);
ScriptTextArea.Tag = new Tuple<string, string>(filePath, sha1);
ScriptTextArea.LoadFile(ScriptTextArea.FileName);
ScriptTextArea.Document.UndoStack.ClearAll();
RefreshChangedStatus();
}
}
}
}

private void DoSave()
{
TextEditorControlEx ScriptTextArea = ((TextEditorControlEx)ScriptsTab.SelectedTab.Controls.Find("ScriptTextArea", true)[0]);
if ("new" == ScriptTextArea.Tag.ToString())
if (ScriptTextArea.Tag != null && "new" == ScriptTextArea.Tag.ToString())
{
DoSaveAs();
}
else
{
try
{
ScriptTextArea.SaveFile(ScriptTextArea.Tag.ToString());
ScriptTextArea.Document.UndoStack.ClearAll();
RefreshChangedStatus();
if (ScriptTextArea.Tag is Tuple<string, string> Tag && Tag.Item1 != "" && File.Exists(Tag.Item1))
{
string filePath = Tag.Item1;
string oldSha1 = Tag.Item2;
ScriptTextArea.SaveFile(Tag.Item1);
string newSha1 = CalcSha1(Tag.Item1);
ScriptTextArea.Tag = new Tuple<string, string>(Tag.Item1, newSha1);
ScriptTextArea.Document.UndoStack.ClearAll();
RefreshChangedStatus();
}
}
catch (Exception ex)
{
Expand All @@ -432,11 +625,15 @@ private void DoSaveAs()
saveFileDialog.FileNames.Length > 0)
{
String filePath = saveFileDialog.FileName;
String directryName = Path.GetDirectoryName(filePath);
String fileName = Path.GetFileName(filePath);
TextEditorControlEx ScriptTextArea = ((TextEditorControlEx)ScriptsTab.SelectedTab.Controls.Find("ScriptTextArea", true)[0]);
ScriptTextArea.Tag = filePath;
ScriptTextArea.SaveFile(filePath);
string newSha1 = CalcSha1(filePath);
ScriptTextArea.Tag = new Tuple<string, string>(filePath, newSha1);
ScriptsTab.SelectedTab.Text = fileName;
ScriptTextArea.Document.UndoStack.ClearAll();
RefreshChangedStatus();
}
}
catch (Exception ex)
Expand Down Expand Up @@ -1406,14 +1603,21 @@ private void closeAllScriptsButThisToolStripMenuItem_Click(object sender, EventA

private void ScriptsTab_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
for (int TabIndex = 0; TabIndex < ScriptsTab.TabCount - 1; ++TabIndex)
{
for (int TabIndex = 0; TabIndex < ScriptsTab.TabCount - 1; ++TabIndex)
if (ScriptsTab.GetTabRect(TabIndex).Contains(e.Location))
{
if (ScriptsTab.GetTabRect(TabIndex).Contains(e.Location))
if (e.Button == MouseButtons.Right)
{
ScriptsTab.SelectedIndex = TabIndex;
ScriptsTabContextMenu.Show(Cursor.Position);
} else if (e.Button == MouseButtons.Left)
{
if (IsFileChanged(TabIndex))
{
ScriptsTab.SelectedIndex = TabIndex;
AskForReload(TabIndex);
}
}
}
}
Expand Down Expand Up @@ -1482,6 +1686,43 @@ public void PrintOutput(String guid, String s)
}
}

public delegate void PrintGlobalOutputDelegate(String s);
public void PrintGlobalOutput(String s)
{
Control[] ScriptOutputs = Controls.Find("ScriptOutput", true);
foreach (TextBox ScriptOutput in ScriptOutputs)
{
if (ScriptOutput != null)
{
if (ScriptOutput.Text.Length > 1024 * 1024 * 100)
{
PrintGlobalThreadSafe(ScriptOutput, "Text", ScriptOutput.Text.Remove(0, 1024 * 1024 * 1));
}
PrintGlobalThreadSafe(ScriptOutput, "AppendText", s + Environment.NewLine);
}
}
}

private delegate void PrintGlobalThreadSafeDelegate(TextBox control, string propertyName, string propertyValue);
public static void PrintGlobalThreadSafe(TextBox control, string propertyName, string propertyValue)
{
if (control.InvokeRequired)
{
control.Invoke(new PrintGlobalThreadSafeDelegate(PrintGlobalThreadSafe), new object[] { control, propertyName, propertyValue });
}
else
{
if(propertyName == "Text")
{
control.Text = propertyValue;
}
if (propertyName == "AppendText")
{
control.AppendText(propertyValue);
}
}
}

public delegate void WriteOutputDelegate(String guid, String s);
public void WriteOutput(String guid, String s)
{
Expand Down Expand Up @@ -1588,5 +1829,17 @@ private void pinToolStripButton_Click(object sender, EventArgs e)
this.onTopToggle = false;
}
}

public delegate void SetTimerReadClientStatusIntervalDelegate(int value);
public void SetTimerReadClientStatusInterval(int value)
{
this.TimerReadClientStatus.Interval = value;
MainForm.TheMainForm.Invoke(new MainForm.ResetTimerReadClientStatusDelegate(MainForm.TheMainForm.ResetTimerReadClientStatus));
}

private void MainForm_Activated(object sender, EventArgs e)
{
CheckAnyFileChanged();
}
}
}
18 changes: 9 additions & 9 deletions EasyLoU/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,15 @@
<data name="DebugLogsToolStripButton.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHdSURBVDhPhZFRT9pQGIb9D/sH3pj9lt2YeGGWhbmFEKls
QEXUFhyzroziHC2looYW7AaKlhGCgspQsxiXmOji2PYbmhgvll3t6t2xsMyLI3uT5+bkfE/e852BVGRy
VHzOXMkci21NQt2Q+1JdS/5KR8Ohgb8R/RNXC8wTCN4x1HMp/Diu/Je6nkLc57vnCNQI23k1/hhy2IeK
lsC3QwuXrTIumkUqnfY2Dt5pEALMfUcghQNDMhfcVDj2qKjEf3faWzgur6C2IlE52zGxt67+E9zO+mLM
vmxtOpdOqwaVi70SGgWFLigkY/Z5s+Q0aBgpKp9rBeyQHQgBN11wVjdxWMpiV39L5eSDjtraGwgMRZBP
zNmn1TxpsIqP7zUqJxUd1WySLtDjEfuTlSNVZViqSKVdXIaVSdAF2diUfbSxihb5pmZeodImzysrIqI0
gUYEB6YGKy3CfM07WGocN2e3KS0JROCiCOZC9q4uk2X1x0y+QNRNEah80C6IPHLz0zCEGRgLs1SyURbi
M/dWjHk62BvtRprw2NMPh8G7RpyFfdnfuJPzRhEa5//aG+0mEfR+z3B+LEdY6KTBvpnpS4YPXfdGu5n3
PHoghZjrpUkvbkjPBu5kMTz+86XHNfYHaJMx1Ji5LP8AAAAASUVORK5CYII=
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHaSURBVDhPhZFRT9pQGIb9D/6D3Sz+lt0s2cWyGOYWwkbF
CZXhLOCYdWWlmaOlFNTQglVQtIwQFFSGmsW4xEQXx7bf0MR4sexqV++OBTMvDuxNnpuT8z15z3eGkpGp
h8IEcylzLLa0BOqGPJDqsvQ7FQ0Fh24ivBi/nGeegPeOoZ5L4udR5b/U9STiPt+wI1AjbOft88eQQz5U
NBHfDyxctMo4bxapdNpb2F/VwPuZEUeQCPnvylxgQ+HYwzUl/qfT3sRReRG1xQSV020TuyvqP8HtFKSY
fdHacC6dVA0q57slNApKf8FZs+Q0aBhJKl9qBWyTHfB+N11wWjdxUMpiR/9A5fijjtrye/AMRZAXZ+2T
ap40WMKnNY3KcUVHNSvRBXo8Yn+2cqSqDEsVqLSLGVhpkS7IxF7ah+tLaJFvauYVKm3yvLIiIEoTaESw
b2qwUgLMd2EHS43j+uw2pQWeCFwUwWzQ3tFlsqzBmNJrRN0UgRoO2AUhjNzcNAz+FYz5GSrZKAthwr0Z
Y57e6Y12k2A89vSj+wi7HjgL+7q33pezRhEaN/mtN9qNGPD+SHOTyERY6KTBnpkeSJoLXvVGu5nzjN4T
g8zVwpQX16Rm/H2RQs9+vfG4xv4CVI4xyTu2SV4AAAAASUVORK5CYII=
</value>
</data>
<metadata name="TimerReadClientStatus.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
Expand Down
Loading

0 comments on commit f161f03

Please sign in to comment.