Skip to content

Commit

Permalink
修改控制台显示逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
dawn-lc committed Jul 6, 2021
1 parent 6710101 commit 174da0e
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 370 deletions.
13 changes: 9 additions & 4 deletions ArchivePasswordTestTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@
<NeutralLanguage>zh-Hans</NeutralLanguage>
<RepositoryUrl>https://github.com/dawn-lc/ArchivePasswordTestTool</RepositoryUrl>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>1.0.10.0</AssemblyVersion>
<FileVersion>1.0.10.0</FileVersion>
<Version>1.0.10</Version>
<AssemblyVersion>1.0.12.0</AssemblyVersion>
<FileVersion>1.0.12.0</FileVersion>
<Version>1.0.12</Version>
<PackageIcon></PackageIcon>
<SignAssembly>false</SignAssembly>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<PackageLicenseExpression></PackageLicenseExpression>
<RunPostBuildEvent>Always</RunPostBuildEvent>
<ApplicationIcon>favicon-20190111022740169.ico</ApplicationIcon>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<WarningLevel>5</WarningLevel>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup>
Expand Down
234 changes: 136 additions & 98 deletions ConsoleExpandModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace module.dawnlc.me
{
Expand All @@ -9,96 +13,151 @@ namespace module.dawnlc.me
/// </summary>
class ConsoleExpand : IDisposable
{
private bool isDisposed = false;
private bool isDisposing = false;
private bool RenderCacheLock = false;
/// <summary>
/// Console 高
/// </summary>
public static int High = Console.WindowHeight;
/// <summary>
/// Console 宽
/// </summary>
public static int Width = Console.WindowWidth;

public static Thread RenderThread;

public static List<Content> RenderCache1 = new List<Content>();
public static List<Content> RenderCache2 = new List<Content>();

public class Content
public class Render : IDisposable
{
public int Row { get; set; }
public int Col { get; set; }
public string ContentString { get; set; }
public Content(int row, int col, string content)
public class Content
{
Row = row;
Col = col;
ContentString = content;
}
}

public ConsoleExpand()
{
Console.SetCursorPosition(0,0);
Console.Clear();
RenderThread = new Thread(OutputCanvas)
{
IsBackground = true
};
RenderThread.Start();
}

public void OutputCanvas()
{
while (!isDisposing)
{
RenderCacheLock = true;
List<Content> Rendering = new List<Content>(RenderCache1);
foreach (var item in Rendering)
public int Row { get; set; }
public int Col { get; set; }
public bool Cover { get; set; }
public string ContentString { get; set; }
public Content(int row, int col, string content,bool cover = true)
{
if (item != null)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}

Row = row;
Col = col;
Cover = cover;
ContentString = content;
}
Rendering.Clear();
RenderCache1.Clear();
}
public class RenderCache : ConcurrentQueue<Content>
{
public ConcurrentQueue<Content> Contents { get; set; }
public bool CacheLock { get; set; }

RenderCache1.AddRange(RenderCache2);
RenderCacheLock = false;
public object SyncRoot => ((ICollection)Contents).SyncRoot;

RenderCache2.Clear();
public bool IsSynchronized => ((ICollection)Contents).IsSynchronized;

Thread.Sleep(32);
public RenderCache()
{
CacheLock = false;
Contents = new ConcurrentQueue<Content>();
}
public void Lock()
{
CacheLock = true;
}
public void Unlock()
{
CacheLock = false;
}
}
}

public void Print(int Col, int Row, string Content)
{
try
private bool isDisposed = false;
public int High = Console.WindowHeight;
public int Width = Console.WindowWidth;
public int Left = Console.CursorLeft;
public int Top = Console.CursorTop;
public Task RenderTask { get; set; }
public CancellationTokenSource RenderTaskTokenSource { get; set; }
public List<RenderCache> Pool { get; set; }
public int RenderInterval { get; set; }
public void Push(Content content)
{
if (RenderCacheLock)
try
{
RenderCache2.Add(new Content(Row, Col, Content));
Pool.Where(p => !p.CacheLock).First().Enqueue(content);
}
else
catch (Exception ex)
{
RenderCache1.Add(new Content(Row, Col, Content));
throw new Exception("缺少可用的输出缓存区!"+ex.ToString());
}
}
catch (Exception)
public Render(int FPS = 10,int PoolCount = 2)
{
//这里如果报错,肯定是破解速度过快.(跑完了7zip test并返回了结果只花了不到10毫秒)
//不过不影响,漏显示一行不算什么大问题(笑
//摸了
RenderInterval = 1000 / FPS;
Pool = new List<RenderCache>();
for (int i = 0; i < PoolCount; i++)
{
Pool.Add(new RenderCache());
}
RenderTaskTokenSource = new CancellationTokenSource();
RenderTask = Task.Factory.StartNew(() => {
try
{
while (true)
{
foreach (var item in Pool.Where(p => p.Count != 0))
{
item.Lock();
for (int i = 0; i < item.Count; i++)
{
if (item.TryDequeue(out Content content))
{
if (content.Cover)
{
Console.SetCursorPosition(0, content.Row);
Console.Write(new string(' ', Width));
}
Console.SetCursorPosition(content.Col, content.Row);
Console.Write(content.ContentString);
}
}
item.Unlock();
}
RenderTaskTokenSource.Token.ThrowIfCancellationRequested();
Thread.Sleep(RenderInterval);
}
}
catch (OperationCanceledException)
{
//外部取消
return;
}
catch (Exception ex)
{
throw ex;
}
}, RenderTaskTokenSource.Token);
}
public void Close()
{
Dispose(true);
}
~Render()
{
Dispose(false);
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
//释放
RenderTaskTokenSource.Cancel();
RenderTask.Wait();
Console.SetCursorPosition(Left,Top);
}
}
isDisposed = true;
}
}

private bool isDisposed = false;
private Render RenderTask { get; set; }
public ConsoleExpand()
{
RenderTask = new Render(25);
}
public void Print(int row, int col, string content)
{
RenderTask.Push(new Render.Content(col, row, content));
}
public void Close()
{
Dispose(true);
Expand All @@ -118,33 +177,12 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
RenderThread.Abort();
isDisposing = true;
foreach (var item in RenderCache1)
{
if (item != null)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}
}
foreach (var item in RenderCache2)
{
if (item != null)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}
}

isDisposing = false;
//释放
RenderTask.Close();
}
}
isDisposed = true; // 标识此对象已释放
isDisposed = true;
}

}
}
Loading

0 comments on commit 174da0e

Please sign in to comment.