Skip to content

Commit

Permalink
v0.3 stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
neomoth committed Oct 22, 2024
1 parent 5f27c75 commit 358aee2
Show file tree
Hide file tree
Showing 22 changed files with 1,037 additions and 49 deletions.
30 changes: 30 additions & 0 deletions NeoQOLPack/FileDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// namespace NeoQOLPack;
//
// public class FileDownloader(Mod mod)
// {
// private static readonly HttpClient HttpClient = new();
//
// public async Task DownloadFileAsync(string url, string outputPath)
// {
// try
// {
// Directory.CreateDirectory(outputPath);
// string fileName = Path.GetFileName(url);
// string destinationPath = Path.Combine(outputPath, fileName);
//
// using var response = await HttpClient.GetAsync(url);
// response.EnsureSuccessStatusCode();
// byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();
// await File.WriteAllBytesAsync(destinationPath, fileBytes);
// mod.Logger.Information($"file {fileName} downloaded to {outputPath}");
// }
// catch (HttpRequestException e)
// {
// mod.Logger.Error(e.Message);
// }
// catch (Exception e)
// {
// mod.Logger.Error(e.Message);
// }
// }
// }
131 changes: 128 additions & 3 deletions NeoQOLPack/Mod.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,151 @@
using GDWeave;
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
using GDWeave;
using GDWeave.Godot;
using GDWeave.Godot.Variants;
using NeoQOLPack.Mods;
using Newtonsoft.Json.Linq;
using Serilog;
// using System.Runtime.InteropServices;

namespace NeoQOLPack;

public class Mod : IMod {
public class Mod : IMod
{
public IModInterface modInterface;
public Config Config;
public ILogger Logger;

private static readonly string versionTag = "Beta2";
private static readonly string repo = "neomoth/NeoQOLPack";

private bool injectUpdateNotice = false;

public Mod(IModInterface modInterface) {
this.modInterface = modInterface;
this.Config = modInterface.ReadConfig<Config>();
Logger = modInterface.Logger;
modInterface.Logger.Information("Hello, world!");
_ = GetVersion();
modInterface.RegisterScriptMod(new InventoryStackerItem(this));
modInterface.RegisterScriptMod(new InventoryStackerPlayerData(this));
modInterface.RegisterScriptMod(new InventoryStackerInventory(this));
modInterface.RegisterScriptMod(new InventoryStackerSelect(this));
modInterface.RegisterScriptMod(new CosmeticLoaderGlobals(this));
modInterface.RegisterScriptMod(new CosmeticLoaderPlayer(this));
modInterface.RegisterScriptMod(new CosmeticLoaderTitle());
// modInterface.RegisterScriptMod(new ModScriptPatcher(this, versionTag, injectUpdateNotice));
modInterface.RegisterScriptMod(new ShopPatcher());
modInterface.RegisterScriptMod(new PlayerHudPatcher(this));
modInterface.RegisterScriptMod(new ShopButtonPatcher(this));
modInterface.RegisterScriptMod(new MenuPatcher(this, versionTag));
if (injectUpdateNotice) ;
}

// [DllImport("user32.dll", SetLastError = true)]
// private static extern int MessageBox(IntPtr hWnd, string text, string caption, uint uType);
//
// private const uint MB_OK = 0x00000000;
// private const uint MB_OKCANCEL = 0x00000001;
// private const uint MB_YESNO = 0x00000004;
// private const uint MB_ICONINFORMATION = 0x00000040;
// private const uint MB_ICONWARNING = 0x00000030;
// private const uint MB_ICONERROR = 0x00000010;
// private const uint MB_ICONQUESTION = 0x00000020;
// private const uint MB_SYSTEMMODAL = 0x00001000; // Make it a system modal dialog
// private const uint MB_SETFOREGROUND = 0x00010000; // Bring the message box to the foreground
//
// private static int ShowMessage(string message, string title, uint buttonType, uint iconType)
// {
// uint options = buttonType | iconType | MB_SYSTEMMODAL | MB_SETFOREGROUND;
// return MessageBox(IntPtr.Zero, message, title, options);
// }

private async Task GetVersion()
{
modInterface.Logger.Information("Neo's QOL Pack loaded!! :3");
try
{
// modInterface.Logger.Information($"This should run before");
JsonDocument? githubInfoRaw = await GetLatestRelease(modInterface.Logger);
JsonElement? githubInfo = githubInfoRaw?.RootElement;
var latestVersion = githubInfo?.GetProperty("tag_name").GetString();
// modInterface.Logger.Information($"This should run after");
modInterface.Logger.Information($"Latest version: {latestVersion}");
if ((bool)latestVersion?.StartsWith("Beta"))
{
var newVer = Int32.Parse(Regex.Match(latestVersion, @"\d+").Value);
var currVer = Int32.Parse(Regex.Match(versionTag, @"\d+").Value);

injectUpdateNotice = newVer > currVer;
// int result = ShowMessage("The version of the mod you are using is out of date.\nWould you like to download the latest release?","Mod out of date!", MB_YESNO, MB_ICONWARNING);
// if (result == 0)
// {
// FileDownloader downloader = new FileDownloader(this);
// var url = githubInfo?.GetProperty("assets").GetProperty("0").GetProperty("browser_download_url").GetString();
// bool isWine = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WINEPREFIX"));
// string path = isWine ? "/home/neomoth/Downloads/gwagwa" : "C:\\users\\steamuser\\Downloads\\gwagwa";
// if (url is null)
// {
// modInterface.Logger.Warning("Failed to find release zip");
// return;
// }
// await downloader.DownloadFileAsync(url, path);
// var a = ShowMessage("New version downloaded. Please relaunch the game.","Success", MB_OK, MB_ICONINFORMATION);
// }
}
}
catch (Exception e)
{
modInterface.Logger.Error(e.Message);
}
}

private static async Task<JsonDocument?> GetLatestRelease(ILogger logger)
{
logger.Information("Getting latest release...");
string apiUrl = $"https://api.github.com/repos/neomoth/NeoQOLPatchMod/releases/latest";

// logger.Debug("Testing dns...");
await Dns.GetHostEntryAsync("api.github.com");
// logger.Debug("creating HTTP Client...");
try
{
using HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(10);

// logger.Debug("adding headers...");
client.DefaultRequestHeaders.Add("User-Agent", "C# App");
// logger.Debug("awaiting response...");
HttpResponseMessage response = await client.GetAsync(apiUrl).ConfigureAwait(false);
// logger.Debug("got response.");
response.EnsureSuccessStatusCode();

if (response.Headers.Contains("X-RateLimit-Remaining"))
{
string? remainingRequests = response.Headers.GetValues("X-RateLimit-Remaining").FirstOrDefault();
// logger.Debug($"GitHub API Rate Limit Remaining: {remainingRequests}");
}

if (!response.IsSuccessStatusCode){
logger.Error("Unable to connect to github.");
return null; // Unable to connect to github for some reason
}
// logger.Debug("valid. getting response data...");
string json = await response.Content.ReadAsStringAsync();

// logger.Debug("parsing json...");
return JsonDocument.Parse(json);
// logger.Debug("retrieved info.");
}
catch (HttpRequestException e)
{
throw new Exception($"Http error: {e.Message}");
}
catch (TaskCanceledException e)
{
throw new Exception($"Task cancelled: {e.Message}");
}
}

public IEnumerable<Token> GetMod()
Expand Down
4 changes: 2 additions & 2 deletions NeoQOLPack/Mods/CosmeticLoaderGlobals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CosmeticLoaderGlobals(Mod mod) : IScriptMod

public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
{
mod.Logger.Information("loaded globals.gdc");
// mod.Logger.Information("loaded globals.gdc");
MultiTokenWaiter extendsWaiter = new MultiTokenWaiter([
t=>t.Type == TokenType.PrExtends,
t => t.Type == TokenType.Newline,
Expand Down Expand Up @@ -50,7 +50,7 @@ public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
}
else if (readyWaiter.Check(token))
{
mod.Logger.Information("found ready func");
// mod.Logger.Information("found ready func");
yield return token;

yield return new Token(TokenType.Newline, 1);
Expand Down
13 changes: 10 additions & 3 deletions NeoQOLPack/Mods/CosmeticLoaderPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
t => t is IdentifierToken {Name: "_ready"},
t => t.Type == TokenType.Newline
],allowPartialMatch: true);



//"/iamweest": PlayerData._unlock_cosmetic("title_streamerman")
// "/colonthreetimeseight": PlayerData._unlock_cosmetic("title_colonthreetimeseight")
// "/hithisisaveryhardstringtotrytoguesslol": PlayerData._unlock_cosmetic("title_seventvowner")

foreach (Token token in tokens)
{
if (readyWaiter.Check(token))
Expand All @@ -27,10 +33,11 @@ public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
yield return new Token(TokenType.ParenthesisOpen);
yield return new IdentifierToken("title");
yield return new Token(TokenType.ParenthesisClose);

yield return new Token(TokenType.Newline, 1);

} else yield return token;

}
else yield return token;
}
}
}
24 changes: 9 additions & 15 deletions NeoQOLPack/Mods/InventoryStackerInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,22 @@ public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
{
yield return token;

var t1 = new Token(TokenType.Period);
var t2 = new IdentifierToken("_stack_items");
var t3 = new Token(TokenType.ParenthesisOpen);
var t4 = new Token(TokenType.ParenthesisClose);
var t5 = new Token(TokenType.Newline, 1);
// mod.Logger.Information($"tokens: {t1}{t2}{t3}{t4}{t5} at {token}");

mod.Logger.Information($"tokens: {t1}{t2}{t3}{t4}{t5} at {token}");

mod.Logger.Information("#################### FOUND REFRESH FUNC ######################"); // C
mod.Logger.Information("WAWAWAWAWAWAWWA");
// mod.Logger.Information("#################### FOUND REFRESH FUNC ######################"); // C
// mod.Logger.Information("WAWAWAWAWAWAWWA");
yield return new Token(TokenType.Dollar);
yield return new ConstantToken(new StringVariant("/root/NeoQOLPack"));
yield return t1;
yield return t2;
yield return t3;
yield return t4;
yield return t5;
yield return new Token(TokenType.Period);
yield return new IdentifierToken("_stack_items");
yield return new Token(TokenType.ParenthesisOpen);
yield return new Token(TokenType.ParenthesisClose);
yield return new Token(TokenType.Newline, 1);
}

else if (skipperWaiter.Check(token))
{
mod.Logger.Information("#################### FOUND SKIP FUNC ######################"); // C
// mod.Logger.Information("#################### FOUND SKIP FUNC ######################"); // C
yield return token;

yield return new Token(TokenType.CfIf);
Expand Down
4 changes: 2 additions & 2 deletions NeoQOLPack/Mods/InventoryStackerItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
{
if (varWaiter.Check(token))
{
mod.Logger.Information("#################### FOUND VAR SPOT ######################"); // C
// mod.Logger.Information("#################### FOUND VAR SPOT ######################"); // C
yield return token;

yield return new Token(TokenType.PrVar);
Expand All @@ -51,7 +51,7 @@ public IEnumerable<Token> Modify(string path, IEnumerable<Token> tokens)
}
else if (updateWaiter.Check(token))
{
mod.Logger.Information("#################### FOUND UPDATE FUNC ######################"); // C
// mod.Logger.Information("#################### FOUND UPDATE FUNC ######################"); // C
yield return token;

yield return new Token(TokenType.Newline, 1);
Expand Down
Loading

0 comments on commit 358aee2

Please sign in to comment.