/// <summary> /// will return null if grid is closed or CubeGridCache cannot be created /// </summary> public static CubeGridCache GetFor(IMyCubeGrid grid) { if (grid.Closed || grid.MarkedForClose || Globals.WorldClosed) { return(null); } CubeGridCache value; if (Registrar.TryGetValue(grid.EntityId, out value)) { return(value); } using (lock_constructing.AcquireExclusiveUsing()) { if (Registrar.TryGetValue(grid.EntityId, out value)) { return(value); } try { return(new CubeGridCache(grid)); } catch (Exception e) { Logger.AlwaysLog("Exception on creation: " + e, Logger.severity.WARNING); return(null); } } }
/// <summary> /// Invokes static, no argument methods with the specified Attribute /// </summary> public static void InvokeMethodsWithAttribute <T>() where T : Attribute, Ordered { if (MyAPIGateway.Entities == null) { Logger.DebugLog("MyAPIGateway.Entities == null", Logger.severity.INFO); return; } SortedDictionary <Sorter, MethodInfo> sorted = new SortedDictionary <Sorter, MethodInfo>(); foreach (Type type in Assembly.GetExecutingAssembly().GetTypes()) { foreach (MethodInfo method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)) { if (method.IsDefined(typeof(T))) { if (method.GetParameters().Length != 0) { Logger.AlwaysLog("method has parameters: " + type + "." + method, Logger.severity.ERROR, primaryState: typeof(T).ToString()); } else { sorted.Add(new Sorter(method.GetCustomAttribute <T>().Order, method.GetHashCode()), method); } } } } foreach (MethodInfo method in sorted.Values) { //Logger.DebugLog("invoking " + method.DeclaringType + "." + method, Logger.severity.TRACE, primaryState: typeof(T).ToString()); method.Invoke(null, null); } }
protected void alwaysLog(string toLog, Logger.severity level = Logger.severity.TRACE, bool condition = true, [CallerFilePath] string filePath = null, [CallerMemberName] string member = null, [CallerLineNumber] int lineNumber = 0) { if (condition) { Logger.AlwaysLog(toLog, level, GetContext(), GetPrimary(), GetSecondary(), filePath, member, lineNumber); } }
/// <summary> /// Try to perform an Action on game thread, if it fails do not crash the game. /// </summary> public static void TryInvokeOnGameThread(this IMyUtilities util, Action invoke, [CallerFilePath] string callerFile = null, [CallerMemberName] string callerMember = null, [CallerLineNumber] int callerLineNumber = 0) { util.InvokeOnGameThread(() => { try { invoke.Invoke(); } catch (Exception ex) { Logger.AlwaysLog("Exception: " + ex, Logger.severity.ERROR, filePath: callerFile, member: callerMember, lineNumber: callerLineNumber); } }); }
private static void SwitchTerminalWhenNoInput() { if (Globals.WorldClosed) { return; } Logger.DebugLog("MyAPIGateway.Input == null", Logger.severity.FATAL, condition: MyAPIGateway.Input == null); if (MyAPIGateway.Input.IsAnyMouseOrJoystickPressed()) { return; } if (MyAPIGateway.Input.IsAnyKeyPress()) { Static.pressedKeys.Clear(); MyAPIGateway.Input.GetPressedKeys(Static.pressedKeys); foreach (MyKeys key in Static.importantKeys) { if (Static.pressedKeys.Contains(key)) { return; } } } UpdateManager.Unregister(1, SwitchTerminalWhenNoInput); Type type = typeof(MyGuiScreenTerminal); object obj = type.GetField("m_instance", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null); Logger.DebugLog("m_instance not found", Logger.severity.ERROR, condition: obj == null); obj = type.GetField("m_controllerControlPanel", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj); Logger.DebugLog("m_controllerControlPanel not found", Logger.severity.ERROR, condition: obj == null); type = type.Assembly.GetType("Sandbox.Game.Gui.MyTerminalControlPanel", true); Logger.DebugLog("MyTerminalControlPanel not found", Logger.severity.ERROR, condition: type == null); MethodInfo method = type.GetMethod("SelectBlocks", BindingFlags.Instance | BindingFlags.NonPublic); if (method == null) { Logger.AlwaysLog("SelectBlocks not found", Logger.severity.ERROR); } else { method.Invoke(obj, null); } }
/// <summary> /// Force vanilla terminal controls to be created for a given type of block. Always invoke this before adding controls. /// MyTerminalControlFactory.EnsureControlsAreCreated does not work, use this method. /// </summary> public static void EnsureTerminalControlCreated <TBlock>() where TBlock : MyTerminalBlock { if (MyTerminalControlFactory.AreControlsCreated <TBlock>()) { return; } Type blockType = typeof(TBlock); MethodInfo method = blockType.GetMethod("CreateTerminalControls", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (method != null) { if (method.GetParameters().Length != 0) { Logger.AlwaysLog("Method has parameters: " + method.Name + " of " + blockType.FullName, Logger.severity.ERROR); return; } Logger.DebugLog("Invoking CreateTerminalControls for " + typeof(TBlock).Name, Logger.severity.DEBUG); // if CreateTerminalControls is changed to access fields this will throw an exception method.Invoke(FormatterServices.GetUninitializedObject(typeof(TBlock)), null); return; } method = blockType.GetMethod("CreateTerminalControls", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); if (method != null) { if (method.GetParameters().Length != 0) { Logger.AlwaysLog("Method has parameters: " + method.Name + " of " + blockType.FullName, Logger.severity.ERROR); return; } Logger.DebugLog("Invoking CreateTerminalControls for " + typeof(TBlock).Name, Logger.severity.DEBUG); method.Invoke(null, null); return; } Logger.DebugLog("No CreateTerminalControls method for " + typeof(TBlock).Name, Logger.severity.DEBUG); }
public static MySessionComponentBase FindModSessionComponent(string modName, string modScriptsFolder, string typeName) { string assemblyName = $"{modName}_{modScriptsFolder}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"; CachingDictionary <Type, MySessionComponentBase> sessionComponents = (CachingDictionary <Type, MySessionComponentBase>) typeof(MySession).GetField("m_sessionComponents", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(MySession.Static); if (sessionComponents == null) { Logger.AlwaysLog("Failed to get m_sessionComponents", Logger.severity.ERROR); return(null); } // Type.GetType(string typeName) with the fully qualified name doesn't seem to work, maybe it's something to do with CodeDOM // there can be more than one assembly with the right name foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName == assemblyName)) { Type componentType = assembly.GetType(typeName); if (componentType == null) { Logger.DebugLog($"Failed to get type from assembly. Assembly: {assemblyName}, Type: {typeName}", Logger.severity.TRACE); continue; } MySessionComponentBase component; if (!sessionComponents.TryGetValue(componentType, out component)) { Logger.DebugLog($"Failed to get MySessionComponentBase. Assembly: {assemblyName}, Type: {typeName}", Logger.severity.TRACE); continue; } return(component); } return(null); }