-
Notifications
You must be signed in to change notification settings - Fork 0
/
PublicOsuBotTransferPlugin.cs
135 lines (119 loc) · 4.98 KB
/
PublicOsuBotTransferPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Sync.Client;
using Sync.Command;
using Sync.Plugins;
using Sync.Tools;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PublicOsuBotTransfer.Gui;
using static Sync.Plugins.PluginEvents;
namespace PublicOsuBotTransfer
{
public class PublicOsuBotTransferPlugin : Plugin, IConfigurable
{
private PluginConfigurationManager config_manager;
OsuBotTransferClient client = new OsuBotTransferClient();
public const string VERSION = "1.3.0";
public string Token => client.Token;
public string Username => OsuBotTransferClient.Target_User_Name;
string temp_user_name;
public PublicOsuBotTransferPlugin() : base("PublicOsuBotTransferPlugin", "MikiraSora && KedamaOvO")
{
config_manager = new PluginConfigurationManager(this);
config_manager.AddItem(client);
EventBus.BindEvent<InitClientEvent>(evt => {
evt.Clients.AddAllClient(client);
});
EventBus.BindEvent<LoadCompleteEvent>(OnLoaded);
EventBus.BindEvent<InitCommandEvent>(e=> e.Commands.Dispatch.bind("osu_bot",OnCommand, "PublicOsuBotTransferPlugin"));
}
private bool OnCommand(Arguments arg)
{
switch (arg.FirstOrDefault()??string.Empty)
{
case "auto":
if (!string.IsNullOrWhiteSpace(temp_user_name))
OsuBotTransferClient.Target_User_Name = temp_user_name;
break;
case "name":
if (arg.Count == 1)
{
IO.CurrentIO.WriteColor("[PublicOsuBotTransferPlugin]please append your user name.for example :\"osu_bot name MikiraSora\"", ConsoleColor.Red);
return false;
}
else
{
var user_name = string.Join(" ", arg.Skip(1));
OsuBotTransferClient.Target_User_Name = user_name;
}
break;
default:
IO.CurrentIO.WriteColor("[PublicOsuBotTransferPlugin]\nosu_bot auto\t:Automatic to set Target_User_Name up by your osu config files.\n" +
"osu_bot name \"your_user_name\"\t:set Target_User_Name explicitly", ConsoleColor.Green);
return true;
}
IO.CurrentIO.WriteColor($"[PublicOsuBotTransferPlugin]Now Target_User_Name is \"{OsuBotTransferClient.Target_User_Name}\"", ConsoleColor.Green);
return true;
}
private void OnLoaded(LoadCompleteEvent e)
{
if (string.IsNullOrWhiteSpace(OsuBotTransferClient.Target_User_Name))
{
//尝试给用户提示
if (TryGetUserName(out var user_name)&&!string.IsNullOrWhiteSpace(user_name))
{
temp_user_name = user_name;
IO.CurrentIO.WriteColor($"[PublicOsuBotTransferPlugin]Are you \"{user_name}\"? Current PublicOsuBotTransferPlugin's option Target_User_Name isn't set." +
$"You can type 'osu_bot auto' to set up automatically.\nor you can type 'osu_bot name \"your_user_name\" ' to set name up explicitly",ConsoleColor.Yellow);
}
}
}
public static bool TryGetUserName(out string user_name)
{
user_name = "";
try
{
var processes = Process.GetProcessesByName(@"osu!");
if (processes.Length != 0)
{
string osu_path = processes[0].MainModule.FileName.Replace(@"osu!.exe", string.Empty);
string osu_config_file = Path.Combine(osu_path, $"osu!.{Environment.UserName}.cfg");
var lines = File.ReadLines(osu_config_file);
foreach (var line in lines)
{
if (line.StartsWith("Username"))
{
user_name = line.Split('=')[1].Trim();
}
}
}
}
catch (Exception e)
{
IO.CurrentIO.WriteColor("[PublicOsuBotTransferPlugin]Failed to get user name from osu! config files :" + e.Message,ConsoleColor.Yellow);
}
return !string.IsNullOrWhiteSpace(user_name);
}
public override void OnEnable()
{
Plugin guiPlugin = getHoster().EnumPluings().FirstOrDefault(p => p.Name == "ConfigGUI");
if (guiPlugin != null)
{
GuiRegisterHelper.RegisterCustomItem(guiPlugin, client);
}
}
public void onConfigurationLoad()
{
}
public void onConfigurationReload()
{
}
public void onConfigurationSave()
{
}
}
}