Skip to content

Commit

Permalink
Fix zip file bug and difficulty cost bug
Browse files Browse the repository at this point in the history
  • Loading branch information
doombubbles committed Apr 26, 2021
1 parent 7998d15 commit 98720d9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 27 deletions.
22 changes: 20 additions & 2 deletions BTD Mod Helper Core/Api/Updater/UpdaterHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine.PlayerLoop;

namespace BTD_Mod_Helper.Api.Updater
{
Expand Down Expand Up @@ -169,9 +169,27 @@ public async Task<bool> Download(string modDir)
await response.Content.CopyToAsync(fs);
}

var helperDir = $"{modDir}\\{Assembly.GetExecutingAssembly().GetName().Name}";
if (fileName.EndsWith(".zip"))
{
ZipFile.ExtractToDirectory(newFile, modDir);
var zipTemp = $"{helperDir}\\Zip Temp";
if (Directory.Exists(zipTemp))
{
Directory.Delete(zipTemp, true);
}
Directory.CreateDirectory(zipTemp);
ZipFile.ExtractToDirectory(newFile, zipTemp);
foreach (var enumerateFile in Directory.EnumerateFiles(zipTemp))
{
var name = Path.GetFileName(enumerateFile);
var targetFile = Path.Combine(modDir, name);
if (File.Exists(targetFile))
{
File.Delete(targetFile);
}
File.Copy(enumerateFile, targetFile);
File.Delete(enumerateFile);
}
File.Delete(newFile);
}

Expand Down
66 changes: 49 additions & 17 deletions BTD Mod Helper Core/BloonsMod.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using System;
using System.Collections.Generic;
using Assets.Scripts.Models;
using Assets.Scripts.Models.Towers.Mods;
using Assets.Scripts.Unity.UI_New.InGame;
using BTD_Mod_Helper.Api.InGame_Mod_Options;
using BTD_Mod_Helper.Extensions;
using MelonLoader;

namespace BTD_Mod_Helper
{
public abstract class BloonsMod : MelonMod
{
internal Dictionary<string, ModSetting> ModSettings;

/// <summary>
/// Github API URL used to check if this mod is up to date.
///
/// For example: "https://api.github.com/repos/gurrenm3/BTD-Mod-Helper/releases"
/// </summary>
public virtual string GithubReleaseURL => "";


/// <summary>
/// As an alternative to a GithubReleaseURL, a direct link to a web-hosted version of the .cs file that
/// has the "MelonInfo" attribute with the version of your mod
Expand All @@ -30,40 +32,70 @@ public abstract class BloonsMod : MelonMod
/// [assembly: MelonInfo(typeof(MegaKnowledge.Main), "Mega Knowledge", "1.0.1", "doombubbles")]
/// </summary>
public virtual string MelonInfoCsURL => "";


/// <summary>
/// Link that people should be prompted to go to when this mod is out of date.
///
/// For example: "https://github.com/gurrenm3/BTD-Mod-Helper/releases/latest"
/// </summary>
public virtual string LatestURL => "";


public static int CostForDifficulty(int cost, string difficulty)
{
float price = cost;
MelonLogger.Msg(difficulty);
switch (difficulty)
{
case "Easy":
price *= .85f;
break;
return CostForDifficulty(cost, .85f);
case "Hard":
price *= 1.08f;
break;
return CostForDifficulty(cost, 1.08f);
case "Impoppable":
price *= 1.2f;
break;
return CostForDifficulty(cost, 1.2f);
default:
return cost;
}
}

public static int CostForDifficulty(int cost, float multiplier)
{
var price = cost * multiplier;
return (int) (5 * Math.Round(price / 5));
}


public static int CostForDifficulty(int cost, Il2CppSystem.Collections.Generic.List<ModModel> mods)
{
var mult = 1f;
foreach (var gameModelMod in mods)
{
if (gameModelMod.mutatorMods != null)
{
foreach (var mutatorModModel in gameModelMod.mutatorMods)
{
if (mutatorModModel != null && mutatorModModel.IsType<GlobalCostModModel>())
{
var mod = mutatorModModel.Cast<GlobalCostModModel>();
mult = mod.multiplier;
}
}
}
}

return CostForDifficulty(cost, mult);
}

public static int CostForDifficulty(int cost, GameModel gameModel)
{
var difficulty = $"{gameModel.difficultyId}";
if (string.IsNullOrEmpty(difficulty))
{
MelonLogger.Warning("Difficulty cannot be determined at this stage of creating the GameModel");
MelonLogger.Warning("Use the list of ModModels to find the difficulty instead");
}

return CostForDifficulty(cost, gameModel.difficultyId);
}

public static int CostForDifficulty(int cost, InGame inGame)
{
return CostForDifficulty(cost, inGame.SelectedDifficulty);
Expand Down
12 changes: 12 additions & 0 deletions BloonsTD6 Mod Helper/BloonsTD6Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Assets.Scripts.Models;
using Assets.Scripts.Models.Profile;
using Assets.Scripts.Models.Towers;
using Assets.Scripts.Models.Towers.Mods;
using Assets.Scripts.Models.TowerSets;
using Assets.Scripts.Simulation;
using Assets.Scripts.Simulation.Bloons;
Expand All @@ -12,6 +13,7 @@
using Assets.Scripts.Simulation.Towers.Weapons;
using Assets.Scripts.Unity;
using BTD_Mod_Helper.Extensions;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using NinjaKiwi.NKMulti;
using UnhollowerBaseLib;
Expand Down Expand Up @@ -62,6 +64,16 @@ public virtual bool ActOnMessage(Message message)
public virtual void OnNewGameModel(GameModel result)
{

}

/// <summary>
/// Called when a new GameModel is created, aka when things like Monkey Knowledge are applied to towers
///
/// Equivalent to a HarmonyPostFix on GameModel_CreatedModded
/// </summary>
public virtual void OnNewGameModel(GameModel result, List<ModModel> mods)
{

}

#endregion
Expand Down
7 changes: 1 addition & 6 deletions BloonsTD6 Mod Helper/MelonMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
using UnityEngine;
using Assets.Scripts.Unity.UI_New.Popups;
using BTD_Mod_Helper.Api.Updater;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Assets.Main.Scenes;
using Assets.Scripts.Unity.Menu;
using BTD_Mod_Helper.Extensions;
using Newtonsoft.Json;

namespace BTD_Mod_Helper
{
Expand All @@ -28,7 +23,7 @@ internal class MelonMain : BloonsTD6Mod
internal static readonly string ModSettingsDir = Path.Combine(modDir, "Mod Settings");

public const string coopMessageCode = "BTD6_ModHelper";
public const string currentVersion = "1.0.1";
public const string currentVersion = "1.0.3";

private bool useModOptionsDEBUG = false;
private ModOptionsMenu modOptionsUI;
Expand Down
7 changes: 5 additions & 2 deletions BloonsTD6 Mod Helper/Patches/GameModel_CreateModded.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Assets.Scripts.Models;
using Assets.Scripts.Models.Towers.Mods;
using BTD_Mod_Helper.Extensions;
using Harmony;
using Il2CppSystem.Collections.Generic;
using MelonLoader;

namespace BTD_Mod_Helper.Patches
{
[HarmonyPatch(typeof(GameModel), nameof(GameModel.CreateModded), typeof(GameModel), typeof(Il2CppSystem.Collections.Generic.List<ModModel>))]
[HarmonyPatch(typeof(GameModel), nameof(GameModel.CreateModded), typeof(GameModel), typeof(List<ModModel>))]
internal class GameModel_CreateModded
{
[HarmonyPostfix]
internal static void Postfix(GameModel result)
internal static void Postfix(GameModel result, List<ModModel> mods)
{
MelonMain.DoPatchMethods(mod => mod.OnNewGameModel(result, mods));
MelonMain.DoPatchMethods(mod => mod.OnNewGameModel(result));
}
}
Expand Down

0 comments on commit 98720d9

Please sign in to comment.