private static void HealthManager_Start(On.HealthManager.orig_Start orig, HealthManager self) { bool destroyed = false; try { self.gameObject.AddComponent <Enemy>(); var replacement = (Enemy)Registry.GetAllFeatures <IObjectReplacement>(r => r is Enemy && r.ThingToReplace == self.gameObject.name).FirstOrDefault(); if (replacement != null) { var instance = GameObject.Instantiate(replacement.gameObject); GameObject.Destroy(self.gameObject); destroyed = true; } } catch (Exception e) { WeaverLog.LogError("Exception occured while spawning enemy replacement : " + e); } finally { if (!destroyed) { orig(self); } } }
public override T LoadAssetFromBundle <T>(string bundleName, string name) { foreach (var bundle in AssetBundle.GetAllLoadedAssetBundles()) { WeaverLog.Log("Found Bundle = " + bundle.name); if (bundle.name.Contains(bundleName)) { WeaverLog.Log("Loading Asset from bundle " + bundle.name); return(LoadAssetFromBundle <T>(bundle, name)); } } return(default(T)); }
void OnEventSendInternal(string eventName, GameObject source, GameObject destination, EventManager.EventType eventType) { OnEventSent(eventName, source, destination, eventType); foreach (var listener in listeners) { try { listener.Value(eventName, source, destination); } catch (Exception e) { WeaverLog.LogError("Event Listener Error: " + e); } } }
private void ShakePosition_UpdateShaking(On.HutongGames.PlayMaker.Actions.ShakePosition.orig_UpdateShaking orig, ShakePosition self) { try { if (CustomShake && self.State.Name.Contains("Shake") && self.State.Fsm == shakerFSM.Fsm) { bool value = self.isLooping.Value; float num = Mathf.Clamp01(1f - GetTimer(self) / ShakeDuration); Vector3 a = Vector3.Scale(ShakeExtents, new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f))); transform.position = GetStartingWorldPosition(self) + a * ((!value) ? num : 1f); //this.timer += Time.deltaTime; SetTimer(self, GetTimer(self) + Time.deltaTime); if (!value && GetTimer(self) > ShakeDuration) { CustomShake = false; StopAndReset(self); self.Fsm.Event(self.stopEvent); self.Finish(); } } else if (CustomRumble && self.State.Name.Contains("Rumbling") && self.State.Fsm == shakerFSM.Fsm) { bool value = self.isLooping.Value; float num = Mathf.Clamp01(1f - GetTimer(self)); Vector3 a = Vector3.Scale(RumbleExtents, new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-1f, 1f))); transform.position = GetStartingWorldPosition(self) + a * ((!value) ? num : 1f); //this.timer += Time.deltaTime; SetTimer(self, GetTimer(self) + Time.deltaTime); if (!value && GetTimer(self) > 1f) { CustomRumble = false; StopAndReset(self); self.Fsm.Event(self.stopEvent); self.Finish(); } } else { orig(self); } } catch (Exception e) { WeaverLog.LogError("Camera Shaker Exception = " + e); } }
static void RuntimeInit() { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { try { foreach (var type in assembly.GetTypes()) { if (typeof(WeaverMod).IsAssignableFrom(type) && !type.IsAbstract && !type.ContainsGenericParameters) { var mod = (WeaverMod)Activator.CreateInstance(type); mod.Initialize(); } } } catch (Exception e) { WeaverLog.Log("Error Loading Mod [" + assembly.GetName().Name + " : " + e); } } }
public static void ExecuteMethodsWithAttribute <T>(Assembly assembly, Func <MethodInfo, T, bool> ExecuteIf = null, BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, bool throwOnError = false) where T : Attribute { List <ValueTuple <MethodInfo, T> > methods = new List <ValueTuple <MethodInfo, T> >(); methods.AddRange(GetMethodsWithAttribute <T>(assembly, flags)); if (methods.Count == 0) { return; } if (methods[0].Item2 is PriorityAttribute) { methods.Sort(new PriorityAttribute.PairSorter <T>()); } while (methods.Count > 0) { var method = methods[0]; methods.RemoveAt(0); try { if (ExecuteIf == null || ExecuteIf(method.Item1, method.Item2)) { method.Item1.Invoke(null, null); } } catch (Exception e) { if (throwOnError) { throw; } else { WeaverLog.LogError("Error running function [" + method.Item1.DeclaringType.FullName + ":" + method.Item1.Name + "\n" + e); } } } }
public static void ExecuteMethodsWithAttribute <T>(Func <MethodInfo, T, bool> ExecuteIf = null, BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, bool throwOnError = false) where T : Attribute { List <ValueTuple <MethodInfo, T> > methods = new List <ValueTuple <MethodInfo, T> >(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { methods.AddRange(GetMethodsWithAttribute <T>(assembly, flags)); } if (methods.Count == 0) { return; } //bool sortable = false; if (typeof(PriorityAttribute).IsAssignableFrom(typeof(T))) { //sortable = true; methods.Sort(new PriorityAttribute.PairSorter <T>()); } /*if (methods[0].Item2 is PriorityAttribute) * { * * }*/ /*AssemblyLoadEventHandler NewAssemblyLoad = (s, args) => * { * methods.AddRange(GetMethodsWithAttribute<T>(args.LoadedAssembly, flags)); * if (sortable) * { * methods.Sort(new PriorityAttribute.PairSorter<T>()); * } * };*/ //try //{ //AppDomain.CurrentDomain.AssemblyLoad += NewAssemblyLoad; while (methods.Count > 0) { var method = methods[0]; methods.RemoveAt(0); try { if (ExecuteIf == null || ExecuteIf(method.Item1, method.Item2)) { method.Item1.Invoke(null, null); } } catch (Exception e) { if (throwOnError) { throw; } else { WeaverLog.LogError("Error running function [" + method.Item1.DeclaringType.FullName + ":" + method.Item1.Name + "\n" + e); } } } /*} * finally * { * AppDomain.CurrentDomain.AssemblyLoad -= NewAssemblyLoad; * }*/ }
public static IEnumerable <ValueTuple <MethodInfo, T> > GetMethodsWithAttribute <T>(Assembly assembly, Type[] paramTypes, BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static) where T : Attribute { if (assembly == null) { yield break; } Type[] types = null; try { types = assembly.GetTypes(); } catch (Exception) { #if !UNITY_EDITOR if (assembly.GetName().Name != "Assembly-CSharp") { WeaverLog.Log("Broken Assembly Found [" + assembly.GetName().Name + "]"); } #endif yield break; } foreach (var type in types) { foreach (var method in type.GetMethods(flags)) { if (paramTypes != null) { var parameters = method.GetParameters(); if (parameters.Length != paramTypes.GetLength(0)) { continue; } for (int i = 0; i < paramTypes.GetLength(0); i++) { if (parameters[i].ParameterType != paramTypes[i]) { continue; } } } object[] attributes = null; try { attributes = method.GetCustomAttributes(typeof(T), false); } catch (Exception e) { WeaverLog.LogWarning($"There was an error reading attribute info from {method.DeclaringType.FullName}:{method.Name}() in assembly {method.DeclaringType.Assembly.GetName().Name}, see output.log for more details"); UnityEngine.Debug.LogException(e); } if (attributes != null && attributes.GetLength(0) > 0) { yield return(new ValueTuple <MethodInfo, T>(method, (T)attributes[0])); } /*if (method.GetCustomAttributes(typeof(T), false).GetLength(0) > 0) * { * yield return method; * }*/ } } }
private static void Fsm_Event_FsmEventTarget_FsmEvent(On.HutongGames.PlayMaker.Fsm.orig_Event_FsmEventTarget_FsmEvent orig, Fsm self, FsmEventTarget eventTarget, FsmEvent fsmEvent) { try { if (eventTarget == null) { eventTarget = SelfTarget; } var source = self.GameObject; if (customSource != null) { source = customSource; } if (fsmEvent != null) { //WeaverLog.Log($"FSM Event Triggered {fsmEvent.Name} from object {source}"); switch (eventTarget.target) { case FsmEventTarget.EventTarget.Self: TriggerEvent(self.GameObject, fsmEvent.Name, source); break; case FsmEventTarget.EventTarget.GameObject: TriggerEvent(self.GetOwnerDefaultTarget(eventTarget.gameObject), fsmEvent.Name, source); break; case FsmEventTarget.EventTarget.GameObjectFSM: TriggerEvent(self.GetOwnerDefaultTarget(eventTarget.gameObject), fsmEvent.Name, source); break; case FsmEventTarget.EventTarget.FSMComponent: if (eventTarget.fsmComponent != null) { TriggerEvent(eventTarget.fsmComponent.gameObject, fsmEvent.Name, source); } break; case FsmEventTarget.EventTarget.BroadcastAll: BroadcastEvent(fsmEvent.Name, source); break; case FsmEventTarget.EventTarget.HostFSM: if (self.Host != null) { TriggerEvent(self.Host.GameObject, fsmEvent.Name, source); } break; case FsmEventTarget.EventTarget.SubFSMs: foreach (var fsm in new List <Fsm>(self.SubFsmList)) { TriggerEvent(fsm.GameObject, fsmEvent.Name, source); } break; } } } catch (Exception e) { WeaverLog.Log("EVENT ERROR = " + e); } finally { orig(self, eventTarget, fsmEvent); } }
/*public static IEnumerable<Registry> GetModRegistries<Mod>() where Mod : WeaverMod { return GetModRegistries(typeof(Mod)); } public static IEnumerable<Registry> GetModRegistries(Type ModType) { var findings = Registry.FindAll(ModType); if (findings != null) { foreach (var registry in findings) { yield return registry; } } var loader = ImplFinder.GetImplementation<RegistryLoader_I>(); var registries = loader.LoadRegistries(ModType); foreach (var registry in registries) { registry.Start(); yield return registry; } }*/ /// <summary> /// Loads any registries that are embedded inside of the assembly as an embedded resource asset bundle /// </summary> /// <param name="assembly">The assembly to load from</param> public static void LoadEmbeddedRegistries(Assembly assembly) { //WeaverLog.Log("Assembly = " + assembly); //WeaverLog.Log("Assembly Null = " + (assembly != null)); var assemblyName = assembly.GetName().Name; //WeaverLog.Log("Loading Embedded Registries for mod [" + modType.FullName + "]"); //if (!loaded) //{ string extension = null; if (SystemInfo.operatingSystem.Contains("Windows")) { extension = ".bundle.win"; } else if (SystemInfo.operatingSystem.Contains("Mac")) { extension = ".bundle.mac"; } else if (SystemInfo.operatingSystem.Contains("Linux")) { extension = ".bundle.unix"; } //foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) //{ try { if (assembly != typeof(WeaverMod).Assembly) { foreach (var name in assembly.GetManifestResourceNames()) { if (name.EndsWith(extension)) { //WeaverLog.Log("Loading Mod Bundle = " + name); var bundle = AssetBundle.LoadFromStream(assembly.GetManifestResourceStream(name)); //WeaverLog.Log("Bundle = " + (bundle != null)); if (bundle != null) { WeaverLog.Log("Loading bundle for Weaver Mod :" + bundle.name); foreach (var registry in bundle.LoadAllAssets<Registry>()) { //WeaverLog.Log("Registry = " + (registry != null)); if (registry.ModAssemblyName == assemblyName) { registry.Initialize(); } } } } } } } catch (NotSupportedException error) { if (!error.Message.Contains("not supported in a dynamic module")) { throw; } } //} //loaded = true; //} /*foreach (var bundle in AssetBundle.GetAllLoadedAssetBundles()) { foreach (var registry in bundle.LoadAllAssets<Registry>()) { if (modType.IsAssignableFrom(registry.ModType)) { yield return registry; } } }*/ }
static IEnumerator DoTextureModeUnravelling(TextureUnravelSettings settings) { string sheetData = File.ReadAllText(settings.SheetPath); var sheet = JsonUtility.FromJson <TextureSheet>(sheetData); var folder = EditorUtility.OpenFolderPanel("Select the folder where you want to textures to be dumped to", "Assets", ""); if (folder == "") { yield break; } folder = PathUtilities.MakePathRelative(new DirectoryInfo("Assets\\..").FullName, folder); if (folder == "") { throw new Exception("The folder specified is not within the Assets Folder"); } //Debugger.Log("Folder = " + folder); var projectFolder = new DirectoryInfo("Assets\\..\\"); List <SpriteLocation> AddedSprites = new List <SpriteLocation>(); try { AssetDatabase.StartAssetEditing(); //for (int i = 0; i < sheet.Sprites.Count; i++) foreach (var spriteData in ReadTexturesFromSheet(sheet, settings)) { //Debugger.Log("D"); /*Progress = i / (float)sheet.Sprites.Count; * * var sprite = sheet.Sprites[i]; * * Vector2 uvOffset = new Vector2(0.001f, 0.001f); * * Vector2 PostProcessedBL = Vector2.zero; * Vector2 PostProcessedTR = Vector2.zero; * * bool rotated = false; * * if (sprite.UVs[0].x == sprite.UVs[1].x && sprite.UVs[2].x == sprite.UVs[3].x) * { * rotated = true; * } * * * if (rotated) * { * PostProcessedBL = sprite.UVs[1]; * PostProcessedTR = sprite.UVs[2]; * } * else * { * PostProcessedBL = sprite.UVs[0]; * PostProcessedTR = sprite.UVs[3]; * } * * int PreBLx = Mathf.RoundToInt((PostProcessedBL.x * settings.texture.width) - uvOffset.x); * int PreTRy = Mathf.RoundToInt(((PostProcessedBL.y) * settings.texture.height) - uvOffset.y); * //float PreTRy = ((-PostProcessedBL.y + 1.0f) * settings.texture.height) - uvOffset.y; * int PreTRx = Mathf.RoundToInt((PostProcessedTR.x * settings.texture.width) + uvOffset.x); * int PreBLy = Mathf.RoundToInt(((PostProcessedTR.y) * settings.texture.height) + uvOffset.y); * * * int PreWidth = Difference(PreBLx, PreTRx); * int PreHeight = Difference(PreBLy, PreTRy); * * Orientation orientation = Orientation.Up; * * if (PostProcessedBL.x < PostProcessedTR.x && PostProcessedBL.y < PostProcessedTR.y) * { * orientation = Orientation.Up; * } * else if (PostProcessedBL.x < PostProcessedTR.x && PostProcessedBL.y > PostProcessedTR.y) * { * orientation = Orientation.Right; * } * else if (PostProcessedBL.x > PostProcessedTR.x && PostProcessedBL.y > PostProcessedTR.y) * { * orientation = Orientation.Down; * } * else if (PostProcessedBL.x > PostProcessedTR.x && PostProcessedBL.y < PostProcessedTR.y) * { * orientation = Orientation.Left; * } * * Vector2Int Min = new Vector2Int(Mathf.RoundToInt(Mathf.Min(PreBLx, PreTRx)), Mathf.RoundToInt(Mathf.Min(PreBLy, PreTRy))); * * Vector2Int Max = new Vector2Int(Mathf.RoundToInt(Mathf.Max(PreBLx, PreTRx)), Mathf.RoundToInt(Mathf.Max(PreBLy, PreTRy))); * * Vector2Int SpriteDimensions = new Vector2Int(Difference(Min.x, Max.x) + 1, Difference(Min.y, Max.y) + 1); * * Color[,] colorMatrix = new Color[SpriteDimensions.x, SpriteDimensions.y]; * * for (int x = Min.x; x <= Max.x; x++) * { * for (int y = Min.y; y <= Max.y; y++) * { * Color reading = settings.texture.GetPixel(x, y); * colorMatrix[x - Min.x, y - Min.y] = reading; * } * } * * Texture2D texture = null; * switch (orientation) * { * case Orientation.Up: * break; * case Orientation.Right: * colorMatrix = RotateLeft(colorMatrix); * break; * case Orientation.Down: * colorMatrix = RotateLeft(colorMatrix); * colorMatrix = RotateLeft(colorMatrix); * break; * case Orientation.Left: * colorMatrix = RotateLeft(colorMatrix); * colorMatrix = RotateLeft(colorMatrix); * colorMatrix = RotateLeft(colorMatrix); * break; * default: * break; * } * * if (sprite.Flipped) * { * colorMatrix = HorizontalFlip(colorMatrix); * } * * texture = new Texture2D(colorMatrix.GetLength(0), colorMatrix.GetLength(1)); * for (int x = 0; x < texture.width; x++) * { * for (int y = 0; y < texture.height; y++) * { * texture.SetPixel(x, y, colorMatrix[x, y]); * } * }*/ var fileName = spriteData.Texture.name; if (fileName == "") { fileName = "unknown_" + AddedSprites.Count; } var filePath = folder + "/" + fileName + ".png"; using (var file = File.Create(filePath)) { using (var writer = new BinaryWriter(file)) { var png = spriteData.Texture.EncodeToPNG(); writer.Write(png); } } AssetDatabase.ImportAsset(filePath); AddedSprites.Add(new SpriteLocation() { Sprite = spriteData.Sheet, FileLocation = filePath, UVWidth = spriteData.UVDimensions.x, UVHeight = spriteData.UVDimensions.y, SpriteDimensions = new Vector2Int(spriteData.Texture.width, spriteData.Texture.height) }); } } finally { AssetDatabase.StopAssetEditing(); Progress = 0.0f; } yield return(null); yield return(null); try { AssetDatabase.StartAssetEditing(); foreach (var sprite in AddedSprites) { var importer = (TextureImporter)AssetImporter.GetAtPath(sprite.FileLocation); TextureImporterSettings importSettings = new TextureImporterSettings(); importer.ReadTextureSettings(importSettings); importSettings.spritePixelsPerUnit = sprite.SpriteDimensions.x / sprite.Sprite.WorldSize.x; importSettings.spriteAlignment = (int)SpriteAlignment.Custom; importSettings.spritePivot = new Vector2(sprite.Sprite.Pivot.x, 1 - sprite.Sprite.Pivot.y); importer.SetTextureSettings(importSettings); importer.SaveAndReimport(); } } finally { AssetDatabase.StopAssetEditing(); } yield return(null); yield return(null); WeaverLog.Log("<b>Unravel Complete</b>"); //Debugger.Log("Project Folder = " + projectFolder.FullName); }