Skip to content

Commit

Permalink
添加了控制台清空的按钮, 一些小问题修复
Browse files Browse the repository at this point in the history
  • Loading branch information
Megghy committed Aug 26, 2021
1 parent 8723a3f commit 9938d14
Show file tree
Hide file tree
Showing 24 changed files with 346 additions and 502 deletions.
96 changes: 0 additions & 96 deletions TSManager/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,100 +52,4 @@ protected override void OnStartup(StartupEventArgs e)
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
[SuppressUnmanagedCodeSecurity]
public static class ConsoleManager
{
private const string Kernel32_DllName = "kernel32.dll";

[DllImport(Kernel32_DllName)]
private static extern bool AllocConsole();

[DllImport(Kernel32_DllName)]
private static extern bool FreeConsole();

[DllImport(Kernel32_DllName)]
private static extern IntPtr GetConsoleWindow();

[DllImport(Kernel32_DllName)]
private static extern int GetConsoleOutputCP();

public static bool HasConsole
{
get { return GetConsoleWindow() != IntPtr.Zero; }
}

/// <summary>
/// Creates a new console instance if the process is not attached to a console already.
/// </summary>
public static void Show()
{
#if DEBUG
if (!HasConsole)
{
AllocConsole();
InvalidateOutAndError();
}
#endif
}

/// <summary>
/// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown.
/// </summary>
public static void Hide()
{
#if DEBUG
if (HasConsole)
{
SetOutAndErrorNull();
FreeConsole();
}
#endif
}

public static void Toggle()
{
if (HasConsole)
{
Hide();
}
else
{
Show();
}
}

static void InvalidateOutAndError()
{
Type type = typeof(System.Console);

System.Reflection.FieldInfo _out = type.GetField("_out",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

System.Reflection.FieldInfo _error = type.GetField("_error",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

System.Reflection.MethodInfo _InitializeStdOutError = type.GetMethod("InitializeStdOutError",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);

Debug.Assert(_out != null);
Debug.Assert(_error != null);

Debug.Assert(_InitializeStdOutError != null);

_out.SetValue(null, null);
_error.SetValue(null, null);

_InitializeStdOutError.Invoke(null, new object[] { true });
}

static void SetOutAndErrorNull()
{
Console.SetOut(TextWriter.Null);
Console.SetError(TextWriter.Null);
}
static void SetOut(string A)
{

}
}
}
5 changes: 3 additions & 2 deletions TSManager/Data/ConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class ConfigData
public static List<ConfigData> ReadAllConfig()
{
var list = new List<ConfigData>();
if (!Directory.Exists(Info.ConfigPath)) Directory.CreateDirectory(Info.ConfigPath);
if (!Directory.Exists(Info.ConfigPath))
Directory.CreateDirectory(Info.ConfigPath);
var files = Directory.GetFiles(Info.ConfigPath, "*.json");
if (files.Any())
{
Expand All @@ -29,7 +30,7 @@ public static List<ConfigData> ReadAllConfig()
var text = File.ReadAllText(filename);
if (Utils.TryParseJson(text, out var jobj))
{
if (jobj.TryGetValue("Settings", out var temp) && jobj.Count == 1) list.Add(new ConfigData(shortName, filename, text, (JObject)jobj["Settings"]));
if (jobj.TryGetValue("Settings", out var temp) && jobj.Count == 1) list.Add(new(shortName, filename, text, (JObject)jobj["Settings"]));
else list.Add(new ConfigData(shortName, filename, text, jobj));
}
else
Expand Down
6 changes: 3 additions & 3 deletions TSManager/Data/GroupData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static List<PermissionData> GetAllPermissions()
if (c.Permissions.Count > 1) c.Permissions.ForEach(p => { if (p != "") permissions.Add(new PermissionData(p, "/" + string.Join(",", c.Names))); });
else if (c.Permissions.Any() && c.Permissions[0] != "") permissions.Add(new PermissionData(c.Permissions[0], "/" + string.Join(",", c.Names)));
});
TShock.Groups.ForEach(g => g.permissions.ForEach(p => { if (!permissions.Where(_p => _p.Name == p).Any()) permissions.Add(new PermissionData(p, "未知")); }));
TShock.Groups.ForEach(g => g.permissions.ForEach(p => { if (!permissions.Any(_p => _p.Name == p)) permissions.Add(new PermissionData(p, "未知")); }));
return permissions;
}
public async static Task<List<PermissionData>> GetPermissionsAsync(string groupName)
Expand Down Expand Up @@ -70,13 +70,13 @@ public GroupData(bool noParent = false)
public string Prefix { get; set; }
public string Suffix { get; set; }
public Color ChatColor { get; set; }
public Brush DisplayColor
public SolidColorBrush DisplayColor
{
get => ChatColor.ToBrush();
set
{
if (ChatColor == null) return;
Color color = ((SolidColorBrush)value).Color;
var color = value.Color;
ChatColor = Color.FromRgb(color.R, color.G, color.B);
}
}
Expand Down
18 changes: 11 additions & 7 deletions TSManager/Data/ItemData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ public ItemData(PlayerInfo info, Item item, int slot)
public int Slot = -1;
public Item Item { get; set; } = new();

public string Name { get { return Item != null ? Item.Name : "未知"; } set { } }
public string Name { get => Item != null ? Item.Name : "未知"; set { } }

public int ID { get { return Item != null ? Item.netID : 0; } set { var temp = new Item();
temp.SetDefaults(value);
temp.stack = Stack;
temp.prefix = (byte)Prefix;
Item = temp;
} }
public int ID
{
get => Item != null ? Item.netID : 0;
set
{
Item.SetDefaults(value);
Item.stack = Stack;
Item.prefix = (byte)Prefix;
}
}

public int Stack { get { return Item != null ? Item.stack : -1; } set { Item.stack = value; } }
public int MaxStack { get { return Item != null ? Item.maxStack : 30; } set { } }
Expand Down
35 changes: 19 additions & 16 deletions TSManager/Data/PlayerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,27 @@ public static List<PlayerInfo> GetAllPlayerInfo()
}
public void Update()
{
try
{
var tempPlayer = TShock.Players.FirstOrDefault(p => p != null && p.Name == Name);
Ban = TShock.Bans.Bans.Any(b => b.Value.Identifier == "uuid:" + Account?.UUID) || TShock.Bans.Bans.Any(b => b.Value.Identifier == "acc:" + Name) || (Online && TShock.Bans.Bans.Any(b => b.Value.Identifier == "ip:" + Player.IP));
if (tempPlayer == null)
{
if (Online) Online = false;
}
else
Task.Run(() => {
try
{
Player = tempPlayer;
Data = tempPlayer.PlayerData;
Account = tempPlayer.Account;
PlayTime += TSMMain.UpdateTime;
Online = true;
var tempPlayer = TShock.Players.FirstOrDefault(p => p != null && p.Name == Name);
Ban = TShock.Bans.Bans.Any(b => b.Value.Identifier == "uuid:" + Account?.UUID) || TShock.Bans.Bans.Any(b => b.Value.Identifier == "acc:" + Name) || (Online && TShock.Bans.Bans.Any(b => b.Value.Identifier == "ip:" + Player.IP));
if (tempPlayer == null)
{
if (Online)
Online = false;
}
else
{
Player = tempPlayer;
Data = tempPlayer.PlayerData;
Account = tempPlayer.Account;
PlayTime += TSMMain.UpdateTime;
Online = true;
}
}
}
catch { }
catch { }
});
}
public void Save()
{
Expand Down
10 changes: 3 additions & 7 deletions TSManager/Data/ScriptData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class ScriptData : Class
{
public static List<ScriptData> GetAllScripts()
{
if (!Directory.Exists(Info.Path + "Scripts")) Directory.CreateDirectory(Info.Path + "Scripts");
if (!Directory.Exists(Info.Path + "Scripts"))
Directory.CreateDirectory(Info.Path + "Scripts");
var list = new List<ScriptData>();
var files = Directory.GetFiles(Info.Path + "Scripts", "*.tsms");
if (files.Any())
Expand Down Expand Up @@ -53,10 +54,6 @@ public static ScriptData Read(string path)
return null;
}
}
/*public ScriptData(string name, string author, string description, Version version, Guid id)
{
}*/
public ScriptData(string name)
{
Name = name;
Expand Down Expand Up @@ -93,7 +90,6 @@ public enum Triggers
PlayerLeave,
PlayerChat,
PlayerDead,

}
public bool Enable { get; set; }
public Triggers TriggerCondition { get; set; }
Expand Down Expand Up @@ -128,7 +124,7 @@ public List<ScratchNet.EventHandler> Handlers
get;
set;
}
public List<ScratchNet.Expression> Expressions
public List<Expression> Expressions
{
get;
set;
Expand Down
2 changes: 1 addition & 1 deletion TSManager/Data/ServerStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void LoadInfo()
public string TShockVersion { get; set; }
[AlsoNotifyFor("RunTime_Text", new string[] { "CPUUsed", "MemoryUsed", "PlayerCount" })]
public long RunTime { get; set; }
public string RunTime_Text { get { TimeSpan ts = new TimeSpan(0, 0, (int)(RunTime / 1000)); return $"{ts.Days}{ts.Hours}{ts.Minutes}{ts.Seconds}秒"; } set { } }
public string RunTime_Text { get { TimeSpan ts = new(0, 0, (int)(RunTime / 1000)); return $"{ts.Days}{ts.Hours}{ts.Minutes}{ts.Seconds}秒"; } set { } }
public struct PluginInfo
{
public PluginInfo(string n, string v, string a, string d)
Expand Down
6 changes: 3 additions & 3 deletions TSManager/Expansion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public static void Add(this RichTextBox t, string content, bool playerInfo, Colo
{
t.Document.Blocks.Add(new Paragraph());
}
Run run = new Run(content);
Run run = new(content);
run.Foreground = color.ToBrush();
run.FontFamily = new FontFamily("Consolas");
if (playerInfo)
{
Hyperlink hl = new Hyperlink(run);
Hyperlink hl = new(run);
hl.Foreground = color.ToBrush();
hl.MouseLeftButtonDown += PlayerClickEvent;
hl.Cursor = Cursors.Hand;
Expand All @@ -69,7 +69,7 @@ static void PlayerClickEvent(object sender, RoutedEventArgs args)

public static SolidColorBrush ToBrush(this Color color)
{
SolidColorBrush solidColorBrush = new SolidColorBrush(color);
SolidColorBrush solidColorBrush = new(color);
solidColorBrush.Freeze();
return solidColorBrush;
}
Expand Down
23 changes: 11 additions & 12 deletions TSManager/Modules/BagManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Windows.Input;
using System.Windows.Media;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using TShockAPI;
using TSManager.Data;
Expand Down Expand Up @@ -135,10 +134,10 @@ public static void ClearAllItem()
((Label)TSMMain.GUI.PlayerBagBox.Children[i]).Background = null;
}
}
public async static void ChangeItemList(List<ItemData> list)
public static void ChangeItemList(List<ItemData> list)
{
await Task.Run(() =>
{
Task.Run(() =>
{
lock (TSMMain.GUI.PlayerBagBox)
{
if (list == null) return;
Expand All @@ -160,15 +159,15 @@ await Task.Run(() =>
var item = Utils.GetTexture($"{list[i].ID}.png").Result;
if (item == null) continue;

//十分憨批的图片缩放
var width = item.Width;
//十分憨批的图片缩放
var width = item.Width;
var height = item.Height;
double top = 0;
double left = 0;
int size = 32;
int blocksize = 60;
if (height > size && width > size) //高宽都超过
{
{
if (width >= height)
{
height = size / width * height;
Expand All @@ -185,14 +184,14 @@ await Task.Run(() =>
}
}
else if (height > size && width <= size) //贴图高度超过
{
{
width = size / height * width;
height = size;
top = (blocksize - height) / 2;
left = (blocksize - width) / 2;
}
else if (height <= size && width > size) //贴图宽度超过
{
{
height = size / width * height;
width = size;
top = (blocksize - height) / 2;
Expand All @@ -203,12 +202,12 @@ await Task.Run(() =>
top = (blocksize - height) / 2;
left = (blocksize - width) / 2;
}
ImageDrawing smallKiwi1 = new ImageDrawing
ImageDrawing smallKiwi1 = new()
{
Rect = new Rect(left, top, width, height),
ImageSource = item
}; //物品贴图位置
imageDrawings.Children.Add(smallKiwi1);
imageDrawings.Children.Add(smallKiwi1);
DrawingImage drawingImageSource = new(imageDrawings);
drawingImageSource.Freeze();
TSMMain.GUIInvoke(() =>
Expand Down Expand Up @@ -345,7 +344,7 @@ public static void SelectItem(object sender, RoutedEventArgs e)
var item = (ItemData)label.DataContext;
TSMMain.GUI.Bag_ItemInfo.DataContext = item;
}
catch (Exception ex) { Utils.Notice(ex, HandyControl.Data.InfoType.Error); }
catch (Exception ex) { ex.ShowError(); }
}
public static void ChangePrefix(ItemData item, int prefix)
{
Expand Down
7 changes: 3 additions & 4 deletions TSManager/Modules/ConfigEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static void OnTextEntering(object sender, TextCompositionEventArgs e)
public static void OnTextEntered(object sender, TextCompositionEventArgs e)
{
var editor = TSMMain.GUI.ConfigEditor;
var data = editor.DataContext as Data.ConfigData;
switch (e.Text)
{
/*case "t":
Expand Down Expand Up @@ -91,12 +90,12 @@ public static void Save(ConfigData data, bool force = false)
if (Utils.TryParseJson(data.Text, out var json)) data.JsonData = json; //首先转换一下文本
if (force || !data.Error)
{
using (StreamWriter sw = new StreamWriter(data.Path))
using (StreamWriter sw = new(data.Path))
{
sw.WriteLine(data.Text);
sw.Flush();
}
if (Info.IsServerRunning) ReloadCfg(data.JsonData, data.Name == "config.json" || data.Name == "sscconfig.json");
if (Info.IsServerRunning) ReloadCfg();
Utils.Notice($"已保存该配置文件.{(Info.IsServerRunning ? "\r\n仅有部分插件支持重载, 一些设置项修改后可能需要重启后才能生效." : "")}", HandyControl.Data.InfoType.Success);
}
else Growl.Ask("Json格式存在错误, 确定要保存吗?", b =>
Expand All @@ -113,7 +112,7 @@ public static void Save(ConfigData data, bool force = false)
/// <summary>
/// 不加载程序集直接保存会报错
/// </summary>
static void ReloadCfg(JObject jobj, bool istsconfig) => GeneralHooks.OnReloadEvent(TSPlayer.Server);
static void ReloadCfg() => GeneralHooks.OnReloadEvent(TSPlayer.Server);
public static void OpenFile(ConfigData data)
{
Process.Start("notepad.exe", data.Path);
Expand Down
Loading

0 comments on commit 9938d14

Please sign in to comment.