Skip to content

Commit

Permalink
update ref
Browse files Browse the repository at this point in the history
  • Loading branch information
cc004 committed Mar 12, 2022
1 parent a086071 commit 0f5639b
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 81 deletions.
12 changes: 7 additions & 5 deletions BandoriBotCore/Apis/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public async Task<string> Pcrd(Request request)
var json = new JObject
{
["def"] = new JArray(request.def.Distinct()),
["language"] = 0,
["nonce"] = nonce,
["page"] = request.page,
["region"] = request.region,
Expand All @@ -108,15 +109,14 @@ public async Task<string> Pcrd(Request request)
{
["_sign"] = sign,
["def"] = json["def"],
["language"] = json["language"],
["nonce"] = json["nonce"],
["page"] = json["page"],
["region"] = json["region"],
["sort"] = json["sort"],
["ts"] = json["ts"]
};

JObject raw = null;


return client.PostAsync($"https://api.pcrdfans.com/x/v1/search",
new StringContent(json.ToString(Formatting.None)
, Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync().Result;
Expand All @@ -136,12 +136,14 @@ public async Task<ActionResult<string>> Execute(string message)
{
var source = new Source { FromGroup = 0, FromQQ = GetUID(), Session = MessageHandler.session };
var result = new StringBuilder();
await MessageHandler.OnMessage(new HandlerArgs
var args = new HandlerArgs
{
Sender = source,
Callback = async s => result.AppendLine(s),
message = message
});
};
await MessageHandler.OnMessage(args);
await args.finishedTask;
return result.ToString();
}
/*
Expand Down
2 changes: 1 addition & 1 deletion BandoriBotCore/BandoriBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disable</Nullable>
</PropertyGroup>

Expand Down
16 changes: 14 additions & 2 deletions BandoriBotCore/Commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@

namespace BandoriBot.Commands
{
public struct CommandArgs
public class CommandArgs
{
public Func<string, Task> Callback;
public string Arg;
public string Arg, Trigger;
public Source Source;
private readonly HandlerArgs parent;

public Task finishedTask
{
get => parent.finishedTask;
set => parent.finishedTask = value;
}

public CommandArgs(HandlerArgs args)
{
parent = args;
}
}

public interface ICommand
Expand Down
3 changes: 2 additions & 1 deletion BandoriBotCore/IMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace BandoriBot
{
public struct HandlerArgs
public class HandlerArgs
{
public string message;
public Source Sender;
public Func<string, Task> Callback;
public Task finishedTask = Task.CompletedTask;
}

public interface IMessageHandler
Expand Down
10 changes: 7 additions & 3 deletions BandoriBotCore/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static long HashGroupCache(long guild, long channel)
private class State
{
public State[] next = new State[256];
public string trigger;
public BlockingDelegate<CommandArgs> cmd;
}

Expand Down Expand Up @@ -108,6 +109,7 @@ public static void Register(ICommand t)
node = node.next[b];
}
node.cmd = @delegate;
node.trigger = alias;
}
}

Expand Down Expand Up @@ -190,7 +192,8 @@ public static async Task OnMessage(SoraApi session, string message, Source Sende

RecordDatabaseManager.AddRecord(Sender.FromQQ, Sender.FromGroup, DateTime.Now, message);

if (Configuration.GetConfig<GroupBlacklist>().InBlacklist(Sender.FromGroup))
if (Configuration.GetConfig<GroupBlacklist>().InBlacklist(Sender.FromGroup) ||
Configuration.GetConfig<GroupBlacklist>().InBlacklist(Sender.FromQQ))
{
Utils.Log(LoggerLevel.Debug, $"[{Sender.FromGroup}::{Sender.FromQQ}]ignored msg: " + message);
return;
Expand Down Expand Up @@ -235,11 +238,12 @@ public static async Task<bool> OnMessage(HandlerArgs args)
{
try
{
await node.cmd.Run(new CommandArgs
await node.cmd.Run(new CommandArgs(args)
{
Arg = args.message.Substring(Encoding.UTF8.GetString(bytes.Take(i).ToArray()).Length),
Source = args.Sender,
Callback = args.Callback
Callback = args.Callback,
Trigger = node.trigger
});
cmdhandle = true;
}
Expand Down
8 changes: 4 additions & 4 deletions BandoriBotCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public static void Main(string[] args)
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

PluginInitialize();

new Thread(() => Apis.Program.Main2(args)).Start();
var tasks = new List<Task>();


PluginInitialize();

foreach (var line in File.ReadAllLines("cqservers.txt"))
{
var s = line.Split(":");
Expand Down Expand Up @@ -165,7 +165,7 @@ private static async ValueTask Event_OnClientConnect(string type, Sora.EventArgs
{
lock (MessageHandler.bots)
{
if (!MessageHandler.bots.ContainsKey(eventArgs.LoginUid))
if (MessageHandler.bots.ContainsKey(eventArgs.LoginUid))
MessageHandler.bots.Remove(eventArgs.LoginUid);
MessageHandler.bots.Add(eventArgs.LoginUid, eventArgs.SoraApi);
MessageHandler.selfids.Add(eventArgs.LoginUid);
Expand Down
7 changes: 0 additions & 7 deletions BandoriBotCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"BandoriBot": {
"commandName": "Project",
"launchBrowser": true,
Expand Down
82 changes: 56 additions & 26 deletions BandoriBotCore/Services/JJCManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,37 @@ private static string Normalize(string text)

public JJCManager(string root)
{
textures = new Dictionary<string, Image>();
nicknames = new Dictionary<string, int>();
font = new Font(FontFamily.GenericMonospace, 15);
try
{
textures = new Dictionary<string, Image>();
nicknames = new Dictionary<string, int>();
font = new Font(FontFamily.GenericMonospace, 15);

foreach (var file in Directory.GetFiles(root))
if (file.EndsWith(".png"))
textures.Add(Path.GetFileNameWithoutExtension(file), Image.FromFile(file));
foreach (var file in Directory.GetFiles(root))
if (file.EndsWith(".png"))
textures.Add(Path.GetFileNameWithoutExtension(file), Image.FromFile(file));

trie = new Trie<int>();
trie = new Trie<int>();

foreach (Match match in Regex.Matches(File.ReadAllText(Path.Combine(root, "chara_names.py")),
@"(\d\d\d\d): \[(.*?)\],"))
{
var id = 100 * int.Parse(match.Groups[1].Value) + 1;// characters.SingleOrDefault(c => c.name.EndsWith(splits[2]));
foreach (var text in match.Groups[2].Value.Split(','))
foreach (Match match in Regex.Matches(File.ReadAllText(Path.Combine(root, "chara_names.py")),
@"(\d\d\d\d): \[(.*?)\],"))
{
var nickname = text.Trim(' ').Trim('"');
if (!nicknames.ContainsKey(nickname))
var id = 100 * int.Parse(match.Groups[1].Value) + 1;// characters.SingleOrDefault(c => c.name.EndsWith(splits[2]));
foreach (var text in match.Groups[2].Value.Split(','))
{
trie.AddWord(Normalize(nickname), id);
nicknames.Add(nickname, id);
var nickname = text.Trim(' ').Trim('"');
if (!nicknames.ContainsKey(nickname))
{
trie.AddWord(Normalize(nickname), id);
nicknames.Add(nickname, id);
}
}
}
}
catch
{

}

client = new HttpClient();

Expand All @@ -138,15 +145,6 @@ public JJCManager(string root)
/*
pool.GetProxysFromAPIs();
*/
wrapper = new GoWrapper(Module.ReadFromBinary("pcrd.wasm"));

wrapper.Global["myhash"] = new Func<string, double>(myHash);
wrapper.Global["location"] = new JsObject
{
["host"] = "pcrdfans.com",
["hostname"] = "pcrdfans.com",

};
}

private Image GetTexture(Character c)
Expand Down Expand Up @@ -228,7 +226,35 @@ private static double myHash(string str)
_0x473e93 = 0x72 * _0x473e93 ^ text[--_0x5d587e];
return _0x473e93 >> 0x3;
}


private string version;

private async Task UpdateVersion()
{
var cur = JObject.Parse(await client.GetStringAsync("https://api.pcrdfans.com/x/v1/search")).Value<string>("version");
if (cur != version)
{
version = cur;

await File.WriteAllBytesAsync("pcrd.wasm", await client.GetByteArrayAsync("https://pcrdfans.com/pcrd.wasm"));

wrapper = new GoWrapper(Module.ReadFromBinary("pcrd.wasm"))
{
Global =
{
["myhash"] = new Func<string, double>(myHash),
["location"] = new JsObject
{
["host"] = "pcrdfans.com",
["hostname"] = "pcrdfans.com",

}
}
};
}

}

public async Task<string> Callapi(string text)
{
string prefix = "";
Expand All @@ -239,11 +265,14 @@ public async Task<string> Callapi(string text)
prefix = "**角色数少于五个**\n" +
(indexes.Item2.Length > 0 ? $"未能识别的名字:{string.Join(',', indexes.Item2.Where(s => !string.IsNullOrWhiteSpace(s)))}\n" : "");

await UpdateVersion();
this.Log(Models.LoggerLevel.Debug, $"chara id = {string.Join(",", indexes.Item1)}");

var nonce = GenNonce();
var json = new JObject
{
["def"] = new JArray(indexes.Item1),
["language"] = 0,
["nonce"] = nonce,
["page"] = 1,
["region"] = 1,
Expand All @@ -262,6 +291,7 @@ public async Task<string> Callapi(string text)
{
["_sign"] = sign,
["def"] = json["def"],
["language"] = json["language"],
["nonce"] = json["nonce"],
["page"] = json["page"],
["region"] = json["region"],
Expand Down
2 changes: 1 addition & 1 deletion BandoriBotCore/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static MessageBody GetMessageChain(string msg)
case "mirai:at": result.Add(SoraSegment.At(long.Parse(val))); break;
case "mirai:imageid": result.Add(SoraSegment.Image(val.Decode().FixImage(), false)); break;
case "mirai:imageurl": result.Add(SoraSegment.Image(val.Decode(), false)); break;
case "mirai:imagepath": result.Add(SoraSegment.Image(val.Decode(), false)); break;
case "mirai:imagepath": result.Add(SoraSegment.Image(Path.GetFullPath(val.Decode()), false)); break;
case "mirai:imagenew": result.Add(SoraSegment.Image(val.Decode(), false)); break;
case "mirai:atall": result.Add(SoraSegment.AtAll()); break;
case "mirai:json": result.Add(SoraSegment.Json(val.Decode())); break;
Expand Down
Empty file added BandoriBotCore/cqservers.txt
Empty file.
Empty file added BandoriBotCore/index.dat
Empty file.
Binary file added BandoriBotCore/pcrd.wasm
Binary file not shown.
Empty file added BandoriBotCore/record.dat
Empty file.
1 change: 1 addition & 0 deletions BandoriBotCore/selfid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
2 changes: 1 addition & 1 deletion BasePlugin/BasePlugin.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 10 additions & 9 deletions BasePlugin/Commands/QACommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AssetsTools;
using BandoriBot.Services;
Expand All @@ -15,29 +16,29 @@ namespace BandoriBot.Commands
{
public class QACommand : ICommand
{
private AssetManager mgr = new ();

public List<string> Alias => new List<string> { "/qa", "QA版本更新 Manifest:" };


public async Task Run(CommandArgs args)
{
var mgr = AssetController.manager;
var a = args.Arg.Trim().Split(' ');
if (long.TryParse(a[0], out var val)) a = new[] {"update_query", a[0]};
if (a[0].IndexOf("update") >= 0)
{
var client = new AssetController.PCRClient();
client.urlroot = "http://l3-qa2-all-gs-gzlj.bilibiligame.net/";
var manifest = client.Callapi("source_ini/get_resource_info", new JObject { ["viewer_id"] = "0" });

var md5hash = mgr.registries.ToDictionary(p => p.Key, p => p.Value.md5);
await mgr.Initialize(a[1],
(string)manifest["movie_ver"],
(string)manifest["sound_ver"], manifest["resource"][0].ToString());
var ab = await mgr.ResolveAssetsBundle("a/masterdata_master.unity3d", "master_data.unity3d");
var af = ab.Files[0].ToAssetsFile();
await File.WriteAllBytesAsync("Data/master.db", af.Objects[0].Data.Skip(16).ToArray());
(string)manifest["sound_ver"], manifest["resource"][0].ToString(), false);
masterContextCache.instance = new masterContext();
await args.Callback($"manifest updated to {a[1]}");
await args.Callback($"manifest updated to {a[1]}\n");
await args.Callback(string.Join("\n",
mgr.registries.Where(r => !md5hash.TryGetValue(r.Key, out var val) || val != r.Value.md5)
.Select(r => r.Key).Select((s, i) => (s, i)).GroupBy(s => s.i / 3)
.Select(g => string.Join(" ", g.Select(t => t.s)))).ToImageText());
}
if (a[0].IndexOf("query") >= 0)
{
Expand All @@ -56,7 +57,7 @@ await mgr.Initialize(a[1],
.Where(s => DateTime.Parse(s.StartTime) > now && s.Enabled)
.Select(s => ((
$"{DateTime.Parse(s.StartTime).ToShortDateString()}-{DateTime.Parse(s.EndTime).ToShortDateString()}",
DateTime.Parse(s.StartTime), s.Description)))
DateTime.Parse(s.StartTime), s.GetDescription())))
.GroupBy(t => t.Item1)
.OrderBy(g => g.First().Item2)
.Select(g => $"{g.Key}\n{string.Join("\n", g.Select(s => $" {s.Item3}"))}"));
Expand Down
Loading

0 comments on commit 0f5639b

Please sign in to comment.