diff --git a/GLMod/Services/IntegrityService.cs b/GLMod/Services/IntegrityService.cs index 5220c84..08e4a40 100644 --- a/GLMod/Services/IntegrityService.cs +++ b/GLMod/Services/IntegrityService.cs @@ -7,6 +7,8 @@ using System.IO; using System.Reflection; using System.Security.Cryptography; +using System.Threading.Tasks; +using System.Net.Http; namespace GLMod.Services { @@ -149,5 +151,102 @@ public IEnumerator VerifyGLMod(System.Action onComplete) onComplete?.Invoke(result); } + + #region Alternative Methods Without Coroutines (Async/Await) + + /// + /// Alternative version of GetApiData using async/await instead of coroutines + /// + public async Task GetApiDataAsync(string id) + { + var form = new Dictionary + { + { "id", id } + }; + + try + { + var content = new FormUrlEncodedContent(form); + var response = await HttpHelper.Client.PostAsync(_apiEndpoint + "/data", content).ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + return System.Text.RegularExpressions.Regex.Unescape(responseString); + } + catch (Exception ex) + { + Log($"GetApiDataAsync failed for {id}, error: {ex.Message}"); + throw; + } + } + + /// + /// Alternative version of GetChecksum using async/await instead of coroutines + /// + public async Task GetChecksumAsync(string checksumId) + { + Log("getChecksumAsync:" + checksumId); + + try + { + string result = await GetApiDataAsync("checksum_" + checksumId).ConfigureAwait(false); + return result; + } + catch (Exception ex) + { + Log($"getChecksumAsync failed for {checksumId}, error: {ex.Message}"); + throw; + } + } + + /// + /// Alternative version of VerifyDll using async/await instead of coroutines + /// + public async Task VerifyDllAsync(string checksumId, string dllPath) + { + try + { + string localChecksum = CalculateFileSHA512(dllPath); + Log("Local checksum: " + localChecksum); + + string remoteChecksum = await GetChecksumAsync(checksumId).ConfigureAwait(false); + Log("Remote checksum: " + remoteChecksum); + + bool result = localChecksum == remoteChecksum; + Log(result ? "Valid checksum" : "Invalid checksum"); + + return result; + } + catch (Exception ex) + { + Log($"VerifyDllAsync failed: {ex.Message}"); + return false; + } + } + + /// + /// Alternative version of VerifyGLMod using async/await instead of coroutines + /// + public async Task VerifyGLModAsync() + { + try + { + var pluginAttribute = typeof(GLMod).GetCustomAttribute(); + // Format version as Major.Minor.Build (3 components) to match server expectations + string version = pluginAttribute?.Version != null + ? $"{pluginAttribute.Version.Major}.{pluginAttribute.Version.Minor}.{pluginAttribute.Version.Build}" + : null; + Log(version); + + bool result = await VerifyDllAsync("glmod" + version, "BepInEx/plugins/glmod.dll").ConfigureAwait(false); + return result; + } + catch (Exception ex) + { + Log($"VerifyGLModAsync failed: {ex.Message}"); + return false; + } + } + + #endregion } }