Skip to content

Commit

Permalink
update CmlLibWinFormSample
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Nov 13, 2021
1 parent 9524a13 commit 28e1b08
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 36 deletions.
9 changes: 9 additions & 0 deletions CmlLibWinFormSample/CmlLibWinFormSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util.cs" />
<Compile Include="VersionSortOptionForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="VersionSortOptionForm.Designer.cs">
<DependentUpon>VersionSortOptionForm.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="ChangeLog.resx">
<DependentUpon>ChangeLog.cs</DependentUpon>
</EmbeddedResource>
Expand Down Expand Up @@ -144,6 +150,9 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<EmbeddedResource Include="VersionSortOptionForm.resx">
<DependentUpon>VersionSortOptionForm.cs</DependentUpon>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
20 changes: 14 additions & 6 deletions CmlLibWinFormSample/GameLog.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Windows.Forms;

namespace CmlLibWinFormSample
{
public partial class GameLog : Form
{
public GameLog()
public GameLog(Process process)
{
InitializeComponent();

process.ErrorDataReceived += Process_DataReceived;
process.OutputDataReceived += Process_DataReceived;
output(process.StartInfo.Arguments);
}

private readonly ConcurrentQueue<string> logQueue = new ConcurrentQueue<string>();

static ConcurrentQueue<string> logQueue = new ConcurrentQueue<string>();

public static void AddLog(string msg)
private void Process_DataReceived(object sender, DataReceivedEventArgs e)
{
logQueue.Enqueue(msg);
if (!string.IsNullOrEmpty(e.Data))
logQueue.Enqueue(e.Data);
}


private void output(string msg) => logQueue.Enqueue(msg);

private void timer1_Tick(object sender, EventArgs e)
{
string msg;
Expand Down
19 changes: 17 additions & 2 deletions CmlLibWinFormSample/MainForm.Designer.cs

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

41 changes: 13 additions & 28 deletions CmlLibWinFormSample/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using CmlLib.Core.Downloader;
using CmlLib.Core.Version;

namespace CmlLibWinFormSample
{
Expand All @@ -26,8 +27,6 @@ public MainForm(MSession session)
readonly MSession session;
MinecraftPath gamePath;
string javaPath;

GameLog logForm;

private async void MainForm_Shown(object sender, EventArgs e)
{
Expand All @@ -40,6 +39,7 @@ private async void MainForm_Shown(object sender, EventArgs e)

private async Task initializeLauncher(MinecraftPath path)
{
lbUsername.Text = session.Username;
txtPath.Text = path.BasePath;
this.gamePath = path;

Expand Down Expand Up @@ -78,6 +78,13 @@ private void btnSetLastVersion_Click(object sender, EventArgs e)
{
cbVersion.Text = launcher.Versions?.LatestReleaseVersion?.Name;
}

private void btnSortFilter_Click(object sender, EventArgs e)
{
var form = new VersionSortOptionForm(launcher, new MVersionSortOption());
form.ShowDialog();

}

// Start Game
private async void Btn_Launch_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -160,6 +167,9 @@ private async void Btn_Launch_Click(object sender, EventArgs e)

// process.Start(); // Just start game, or
StartProcess(process); // Start Process with debug options

var gameLog = new GameLog(process);
gameLog.Show();
}
catch (FormatException fex) // int.Parse exception
{
Expand All @@ -184,13 +194,6 @@ private async void Btn_Launch_Click(object sender, EventArgs e)
}
finally
{
// re open log form
if (logForm != null)
logForm.Close();

logForm = new GameLog();
logForm.Show();

// enable ui
setUIEnabled(true);
}
Expand Down Expand Up @@ -293,37 +296,19 @@ private void btnForgeInstall_Click(object sender, EventArgs e)
private void StartProcess(Process process)
{
File.WriteAllText("launcher.txt", process.StartInfo.Arguments);
output(process.StartInfo.Arguments);


// process options to display game log

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.OutputDataReceived += Process_OutputDataReceived;

process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
}

private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
output(e.Data);
}

private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
output(e.Data);
}

void output(string msg)
{
GameLog.AddLog(msg);
}

private void btnChangelog_Click(object sender, EventArgs e)
{
// Game Changelog
Expand Down

0 comments on commit 28e1b08

Please sign in to comment.