Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ReadOnlySpan to make the alloction free #479

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions src/Starward.Core/Gacha/GachaLogClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ public abstract class GachaLogClient
protected const string WEB_CACHE_PATH_YS_CN = @"YuanShen_Data\webCaches\Cache\Cache_Data\data_2";
protected const string WEB_CACHE_PATH_YS_OS = @"GenshinImpact_Data\webCaches\Cache\Cache_Data\data_2";

protected const string WEB_PREFIX_YS_CN = "https://webstatic.mihoyo.com/hk4e/event/e20190909gacha-v2/index.html";
protected const string WEB_PREFIX_YS_OS = "https://webstatic-sea.hoyoverse.com/genshin/event/e20190909gacha-v2/index.html";

protected const string API_PREFIX_YS_CN = "https://hk4e-api.mihoyo.com/event/gacha_info/api/getGachaLog";
protected const string API_PREFIX_YS_OS = "https://hk4e-api-os.hoyoverse.com/event/gacha_info/api/getGachaLog";

protected static readonly ReadOnlyMemory<byte> MEMORY_WEB_PREFIX_YS_CN = new(Encoding.UTF8.GetBytes(WEB_PREFIX_YS_CN));
protected static readonly ReadOnlyMemory<byte> MEMORY_WEB_PREFIX_YS_OS = new(Encoding.UTF8.GetBytes(WEB_PREFIX_YS_OS));
protected static ReadOnlySpan<byte> SPAN_WEB_PREFIX_YS_CN => "https://webstatic.mihoyo.com/hk4e/event/e20190909gacha-v2/index.html"u8;
protected static ReadOnlySpan<byte> SPAN_WEB_PREFIX_YS_OS => "https://webstatic-sea.hoyoverse.com/genshin/event/e20190909gacha-v2/index.html"u8;



Expand All @@ -35,14 +32,11 @@ public abstract class GachaLogClient

protected const string WEB_CACHE_SR_PATH = @"StarRail_Data\webCaches\Cache\Cache_Data\data_2";

protected const string WEB_PREFIX_SR_CN = "https://webstatic.mihoyo.com/hkrpg/event/e20211215gacha-v2/index.html";
protected const string WEB_PREFIX_SR_OS = "https://gs.hoyoverse.com/hkrpg/event/e20211215gacha-v2/index.html";

protected const string API_PREFIX_SR_CN = "https://api-takumi.mihoyo.com/common/gacha_record/api/getGachaLog";
protected const string API_PREFIX_SR_OS = "https://api-os-takumi.mihoyo.com/common/gacha_record/api/getGachaLog";

protected static readonly ReadOnlyMemory<byte> MEMORY_WEB_PREFIX_SR_CN = new(Encoding.UTF8.GetBytes(WEB_PREFIX_SR_CN));
protected static readonly ReadOnlyMemory<byte> MEMORY_WEB_PREFIX_SR_OS = new(Encoding.UTF8.GetBytes(WEB_PREFIX_SR_OS));
protected static ReadOnlySpan<byte> SPAN_WEB_PREFIX_SR_CN => "https://webstatic.mihoyo.com/hkrpg/event/e20211215gacha-v2/index.html"u8;
protected static ReadOnlySpan<byte> SPAN_WEB_PREFIX_SR_OS => "https://gs.hoyoverse.com/hkrpg/event/e20211215gacha-v2/index.html"u8;



Expand Down Expand Up @@ -187,14 +181,14 @@ public static string GetGachaCacheFilePath(GameBiz gameBiz, string? installPath)



private static ReadOnlyMemory<byte> GetGachaUrlPattern(GameBiz gameBiz)
private static ReadOnlySpan<byte> GetGachaUrlPattern(GameBiz gameBiz)
{
return gameBiz switch
{
GameBiz.hk4e_cn or GameBiz.hk4e_cloud => MEMORY_WEB_PREFIX_YS_CN,
GameBiz.hk4e_global => MEMORY_WEB_PREFIX_YS_OS,
GameBiz.hkrpg_cn => MEMORY_WEB_PREFIX_SR_CN,
GameBiz.hkrpg_global => MEMORY_WEB_PREFIX_SR_OS,
GameBiz.hk4e_cn or GameBiz.hk4e_cloud => SPAN_WEB_PREFIX_YS_CN,
GameBiz.hk4e_global => SPAN_WEB_PREFIX_YS_OS,
GameBiz.hkrpg_cn => SPAN_WEB_PREFIX_SR_CN,
GameBiz.hkrpg_global => SPAN_WEB_PREFIX_SR_OS,
_ => throw new ArgumentOutOfRangeException($"Unknown region {gameBiz}"),
};
}
Expand Down Expand Up @@ -321,21 +315,19 @@ private async Task<List<T>> GetGachaLogByTypeAsync<T>(string prefix, GachaType g
}


protected static string? FindMatchStringFromFile(string path, params ReadOnlyMemory<byte>[] prefixes)
protected static string? FindMatchStringFromFile(string path, ReadOnlySpan<byte> prefix)
{
using var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
var ms = new MemoryStream();
fs.CopyTo(ms);
var span = ms.ToArray().AsSpan();
foreach (var prefix in prefixes)
var index = span.LastIndexOf(prefix);
if (index >= 0)
{
var index = span.LastIndexOf(prefix.Span);
if (index >= 0)
{
var length = span[index..].IndexOfAny("\0\""u8);
return Encoding.UTF8.GetString(span.Slice(index, length));
}
var length = span[index..].IndexOfAny("\0\""u8);
return Encoding.UTF8.GetString(span.Slice(index, length));
}

return null;
}

Expand Down
Loading