Skip to content

Commit

Permalink
improved error handling, add createtowerattackmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
silentstorm committed Jan 11, 2023
1 parent 4544260 commit 3dc18e1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
30 changes: 26 additions & 4 deletions Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static T GetModel<T>(this Il2CppReferenceArray<Model>array)where T:Model{
try{
return array.First(a=>a.GetIl2CppType()==Il2CppType.Of<T>()).Cast<T>();
}catch(Exception error){
Log("Failed to get model type from array");
Log("Failed to get model "+Il2CppType.Of<T>().Name+" from array");
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
Expand All @@ -15,7 +15,18 @@ public static T GetModel<T>(this Il2CppReferenceArray<Model>array,string modelNa
try{
return array.First(a=>a.name.Contains(modelName)).Cast<T>();
}catch(Exception error){
Log("Failed to get model "+modelName+" from array");
Log("Failed to get model "+modelName+" with type "+Il2CppType.Of<T>().Name+" from array");
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
return null;
}
}
public static Il2CppReferenceArray<Model>GetModels<T>(this Il2CppReferenceArray<Model>array)where T:Model{
try{
return array.Where(a=>a.GetIl2CppType()==Il2CppType.Of<T>()).ToArray();
}catch(Exception error){
Log("Failed to get models "+Il2CppType.Of<T>().Name+" from array");
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
Expand All @@ -28,7 +39,7 @@ public static T GetModel<T>(this List<Model>list)where T:Model{
try{
return list.First(a=>a.GetIl2CppType()==Il2CppType.Of<T>()).Cast<T>();
}catch(Exception error){
Log("Failed to get model type from list");
Log("Failed to get model "+Il2CppType.Of<T>().Name+" from list");
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
Expand All @@ -39,7 +50,18 @@ public static T GetModel<T>(this List<Model>list,string modelName)where T:Model{
try{
return list.First(a=>a.name.Contains(modelName)).Cast<T>();
}catch(Exception error){
Log("Failed to get model "+modelName+" from list");
Log("Failed to get model "+modelName+" with type "+Il2CppType.Of<T>().Name+" from list");
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
return null;
}
}
public static List<Model>GetModels<T>(this List<Model>list)where T:Model{
try{
return list.Where(a=>a.GetIl2CppType()==Il2CppType.Of<T>()).ToList();
}catch(Exception error){
Log("Failed to get models "+Il2CppType.Of<T>().Name+" from list");
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
Expand Down
4 changes: 4 additions & 0 deletions GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
global using System.Reflection;
global using SC2ExpansionLoader;
global using Il2CppInterop.Runtime.InteropTypes.Arrays;
global using Il2CppAssets.Scripts.Models.Towers.Behaviors.Attack;
global using Il2CppAssets.Scripts.Models.GenericBehaviors;
global using Il2CppAssets.Scripts.Models.Towers.Behaviors.Attack.Behaviors;
global using Il2CppAssets.Scripts.Models.Towers.Projectiles.Behaviors;
namespace SC2ExpansionLoader{}
2 changes: 1 addition & 1 deletion ModHelperData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace SC2ExpansionLoader{
public static class ModHelperData{
public const string WorksOnVersion="34.3";
public const string Version="2.3.0";
public const string Version="2.4.0";
public const string Name="SC2ExpansionLoader";
public const string Description="Loader for any SC2Expansion mods, this mod adds nothing by itself. Will be automatically downloaded if required";
public const string RepoOwner = "Onixiya";
Expand Down
22 changes: 17 additions & 5 deletions ModMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class ModMain:MelonMod{
public static AbilityModel BlankAbilityModel;
private static MelonLogger.Instance mllog;
public static string BundleDir;
public static AttackModel CreateTowerAttackModel;
public static void Log(object thingtolog,string type="msg"){
switch(type){
case"msg":
Expand All @@ -25,6 +26,7 @@ public static void SaveIl2CppObj(string fileName,Il2CppSystem.Object obj){
public override void OnInitializeMelon(){
mllog=LoggerInstance;
BundleDir=MelonEnvironment.UserDataDirectory+"/SC2ExpansionBundles/";
BundleDir=BundleDir.Replace('\\','/');
Directory.CreateDirectory(BundleDir);
foreach(MelonMod mod in RegisteredMelons){
if(mod.Info.Name.StartsWith("SC2Expansion")){
Expand Down Expand Up @@ -54,17 +56,25 @@ public override void OnInitializeMelon(){
}
}
}
public static uObject LoadAsset<T>(string Asset,AssetBundle Bundle){
public static T LoadAsset<T>(string Asset,AssetBundle Bundle)where T:uObject{
try{
return Bundle.LoadAssetAsync(Asset,Il2CppType.Of<T>()).asset;
return Bundle.LoadAssetAsync(Asset,Il2CppType.Of<T>()).asset.Cast<T>();
}catch{
foreach(KeyValuePair<string,SC2Tower>tower in TowerTypes){
tower.Value.LoadedBundle=UnityEngine.AssetBundle.LoadFromFileAsync(BundleDir+tower.Key.ToLower()).assetBundle;
}
try{
return Bundle.LoadAssetAsync(Asset,Il2CppType.Of<T>()).asset;
return Bundle.LoadAssetAsync(Asset,Il2CppType.Of<T>()).asset.Cast<T>();
}catch(Exception error){
Log("Failed to load "+Asset+" from "+Bundle.name);
try{
Log("Attempting to get available assets");
foreach(string asset in Bundle.GetAllAssetNames()){
Log(asset);
}
}catch{
Log("Bundle is null");
}
string message=error.Message;
message+="@\n"+error.StackTrace;
Log(message,"error");
Expand All @@ -82,9 +92,11 @@ public static void PlaySound(string name){
Log(message,"error");
}
}
public static void PlayAnimation(UnityDisplayNode udn,string anim){
public static void PlayAnimation(UnityDisplayNode udn,string anim,float duration=0.2f){
try{
udn.GetComponent<Animator>().CrossFade(anim,0.2f,0,0);
if(udn!=null){
udn.GetComponent<Animator>().CrossFade(anim,duration,0,0);
}
}catch(Exception error){
Log("Failed to play animation "+anim);
string message=error.Message;
Expand Down
39 changes: 28 additions & 11 deletions Patches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Il2CppAssets.Scripts.Data;

namespace SC2ExpansionLoader{
public class HarmonyPatches{
[HarmonyPatch(typeof(Btd6Player),"CheckForNewParagonPipEvent")]
Expand All @@ -13,11 +11,13 @@ public static bool Prefix(){
public class ProfileModelValidate_Patch{
[HarmonyPostfix]
public static void Postfix(ProfileModel __instance){
foreach(string tower in TowerTypes.Keys){
foreach(KeyValuePair<string,SC2Tower>tower in TowerTypes){
try{
__instance.unlockedTowers.Add(tower);
foreach(UpgradeModel upgrade in TowerTypes[tower].Upgrades()){
__instance.acquiredUpgrades.Add(upgrade.name);
__instance.unlockedTowers.Add(tower.Value.Name);
if(tower.Value.Upgradable){
foreach(UpgradeModel upgrade in tower.Value.Upgrades()){
__instance.acquiredUpgrades.Add(upgrade.name);
}
}
}catch(Exception error){
Log("Failed to add "+tower+" to unlocked towers or upgrades");
Expand All @@ -33,6 +33,20 @@ public class TitleScreenStart_Patch{
[HarmonyPostfix]
public static void Postfix(){
try{
//mostly suited for protoss warp things
CreateTowerAttackModel=Game.instance.model.GetTowerFromId("EngineerMonkey-100").behaviors.GetModel<AttackModel>().Clone<AttackModel>();
List<Model>createTowerBehav=CreateTowerAttackModel.behaviors.ToList();
createTowerBehav.Remove(createTowerBehav.First(a=>a.GetIl2CppType().Name=="RotateToTargetModel"));
createTowerBehav.GetModel<RandomPositionModel>().minDistance=70;
createTowerBehav.GetModel<RandomPositionModel>().maxDistance=90;
createTowerBehav.GetModel<RandomPositionModel>().idealDistanceWithinTrack=0;
createTowerBehav.GetModel<RandomPositionModel>().useInverted=false;
CreateTowerAttackModel.behaviors=createTowerBehav.ToArray();
CreateTowerAttackModel.weapons[0].projectile.display=new(){guidRef=""};
CreateTowerAttackModel.weapons[0].projectile.behaviors.GetModel<ArriveAtTargetModel>().expireOnArrival=false;
CreateTowerAttackModel.weapons[0].projectile.behaviors.GetModel<ArriveAtTargetModel>().altSpeed=400;
CreateTowerAttackModel.weapons[0].projectile.behaviors.GetModel<DisplayModel>().delayedReveal=1;
CreateTowerAttackModel.weapons[0].projectile.behaviors.GetModel<DisplayModel>().positionOffset=new(0,0,190);
BlankAbilityModel=Game.instance.model.GetTowerFromId("Quincy 4").Cast<TowerModel>().behaviors.
First(a=>a.GetIl2CppType().Name=="AbilityModel").Clone().Cast<AbilityModel>();
BlankAbilityModel.description="AbilityDescription";
Expand All @@ -59,9 +73,13 @@ public static void Postfix(){
foreach(TowerModel towerModel in tower.TowerModels()){
towers.Add(towerModel);
}
towerSet.Add(tower.ShopDetails());
foreach(UpgradeModel upgrade in tower.Upgrades()){
upgrades.Add(upgrade);
if(tower.AddToShop){
towerSet.Add(tower.ShopDetails());
}
if(tower.Upgradable){
foreach(UpgradeModel upgrade in tower.Upgrades()){
upgrades.Add(upgrade);
}
}
Game.instance.model.towers=towers.ToArray();
Game.instance.model.towerSet=towerSet.ToArray();
Expand Down Expand Up @@ -119,8 +137,7 @@ public static void Postfix(string name,ref Sprite __result){
}catch{}
if(TowerTypes.ContainsKey(towerName)){
try{
SC2Tower tower=TowerTypes[towerName];
Texture2D texture=LoadAsset<Texture2D>(name,tower.LoadedBundle).Cast<Texture2D>();
Texture2D texture=LoadAsset<Texture2D>(name,TowerTypes[towerName].LoadedBundle).Cast<Texture2D>();
__result=Sprite.Create(texture,new(0,0,texture.width,texture.height),new());
}catch(Exception error){
Log("Failed to set "+name+" up");
Expand Down
2 changes: 2 additions & 0 deletions SC2Tower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public virtual void Sell(Tower tower){}
public virtual int MaxUpgradeQuote=>0;
public virtual Dictionary<string,string>SoundNames=>null;
public virtual Dictionary<string,Il2CppSystem.Type>Behaviours=>new();
public virtual bool AddToShop=>true;
public virtual bool Upgradable=>true;
}
[RegisterTypeInIl2Cpp]
public class SC2Sound:MonoBehaviour{
Expand Down

0 comments on commit 3dc18e1

Please sign in to comment.