internal void InputPluginHandlerExceptionThrown(TerrariaPlugin plugin, string hookName, Exception exception) { if (plugin == null) { throw new ArgumentNullException("plugin"); } if (string.IsNullOrWhiteSpace(hookName)) { throw new ArgumentException("hookName"); } if (exception == null) { throw new ArgumentNullException("exception"); } if (this.WrappedProfiler == null) { return; } try { this.WrappedProfiler.InputPluginHandlerExceptionThrown(plugin, hookName, exception); } catch (Exception ex) { ServerApi.LogWriter.ServerWriteLine( string.Format("Profiler \"{0}\" has thrown an unexpected exception:\n{1}", this.ProfilerName, ex), TraceLevel.Error); } }
public WorldWatchdog(TerrariaPlugin pluginInstance) { this.instance = pluginInstance; ServerApi.Hooks.NetGetData.Register(pluginInstance, NetHooks_GetData); ServerApi.Hooks.NetSendData.Register(pluginInstance, NetHooks_SendData); ServerApi.Hooks.GameUpdate.Register(pluginInstance, Game_Update); }
public void InputPluginCustomProcessingTime(TerrariaPlugin plugin, string operation, TimeSpan processingTime) { if (plugin == null) { throw new ArgumentNullException("plugin"); } if (string.IsNullOrWhiteSpace(operation)) { throw new ArgumentException("operation"); } if (this.WrappedProfiler == null) { return; } try { this.WrappedProfiler.InputPluginCustomProcessingTime(plugin, operation, processingTime); } catch (Exception ex) { ServerApi.LogWriter.ServerWriteLine( string.Format("Profiler \"{0}\" has thrown an unexpected exception:\n{1}", this.ProfilerName, ex), TraceLevel.Error); } }
public GetDataHookHandler(TerrariaPlugin plugin, bool invokeTileEditOnChestKill = false, int hookPriority = 0) { Contract.Requires<ArgumentNullException>(plugin != null); this.Plugin = plugin; this.InvokeTileEditOnChestKill = invokeTileEditOnChestKill; ServerApi.Hooks.NetGetData.Register(plugin, this.NetHooks_GetData, hookPriority); }
public void PluginWriteLine(TerrariaPlugin plugin, string message, TraceLevel kind) { try { this.WrappedLogWriter.PluginWriteLine(plugin, message, kind); } catch (Exception ex) { DefaultLogWriter.ServerWriteLine(string.Format( "The attached log writer \"{0}\" has thrown an unexpected exception:\n{1}", this.LogWriterName, ex), TraceLevel.Error); } }
public void InputPluginCustomProcessingTime(TerrariaPlugin plugin, string operation, TimeSpan processingTime) { if (plugin == null) throw new ArgumentNullException("plugin"); if (string.IsNullOrWhiteSpace(operation)) throw new ArgumentException("operation"); if (this.WrappedProfiler == null) return; try { this.WrappedProfiler.InputPluginCustomProcessingTime(plugin, operation, processingTime); } catch (Exception ex) { ServerApi.LogWriter.ServerWriteLine( string.Format("Profiler \"{0}\" has thrown an unexpected exception:\n{1}", this.ProfilerName, ex), TraceLevel.Error); } }
public bool Deregister(TerrariaPlugin registrator, HookHandler <ArgsType> handler) { if (registrator == null) { throw new ArgumentNullException("registrator"); } if (handler == null) { throw new ArgumentNullException("handler"); } var registration = new HandlerRegistration <ArgsType> { Registrator = registrator, Handler = handler }; lock (this.alterRegistrationsLock) { int registrationIndex = this.registrations.IndexOf(registration); if (registrationIndex == -1) { return(false); } var registrationsClone = new List <HandlerRegistration <ArgsType> >(this.registrations.Count); for (int i = 0; i < this.registrations.Count; i++) { if (i != registrationIndex) { registrationsClone.Add(this.registrations[i]); } } Interlocked.Exchange(ref this.registrations, registrationsClone); } return(true); }
public void Register(TerrariaPlugin registrator, HookHandler <ArgsType> handler, int priority) { if (registrator == null) { throw new ArgumentNullException("registrator"); } if (handler == null) { throw new ArgumentNullException("handler"); } var newRegistration = new HandlerRegistration <ArgsType> { Registrator = registrator, Handler = handler, Priority = priority }; lock (this.alterRegistrationsLock) { var registrationsClone = new List <HandlerRegistration <ArgsType> >(this.registrations.Count); registrationsClone.AddRange(this.registrations); // Highest priority goes up in the list, first registered wins if priority equals. int insertionIndex = registrations.Count; for (int i = 0; i < registrationsClone.Count; i++) { if (registrationsClone[i].Priority < priority) { insertionIndex = i; break; } } registrationsClone.Insert(insertionIndex, newRegistration); Interlocked.Exchange(ref this.registrations, registrationsClone); } }
public void PluginWriteLine(TerrariaPlugin plugin, string message, TraceLevel kind) { this.WriteLine(plugin.Name, message, kind); }
public void Register(TerrariaPlugin registrator, HookHandler <ArgsType> handler) { this.Register(registrator, handler, registrator.Order); }
public PluginContainer(TerrariaPlugin plugin, bool dll) { this.Plugin = plugin; this.Initialized = false; this.Dll = dll; }
internal void InputPluginHandlerExceptionThrown(TerrariaPlugin plugin, string hookName, Exception exception) { if (plugin == null) throw new ArgumentNullException("plugin"); if (string.IsNullOrWhiteSpace(hookName)) throw new ArgumentException("hookName"); if (exception == null) throw new ArgumentNullException("exception"); if (this.WrappedProfiler == null) return; try { this.WrappedProfiler.InputPluginHandlerExceptionThrown(plugin, hookName, exception); } catch (Exception ex) { ServerApi.LogWriter.ServerWriteLine( string.Format("Profiler \"{0}\" has thrown an unexpected exception:\n{1}", this.ProfilerName, ex), TraceLevel.Error); } }
public PluginContainer(TerrariaPlugin plugin) : this(plugin, true) { }
/// <summary>TShock - The constructor for the TShock plugin.</summary> /// <param name="game">game - The Terraria main game.</param> public TShock(Main game) : base(game) { Config = new ConfigFile(); ServerSideCharacterConfig = new ServerSideConfig(); ServerSideCharacterConfig.StartingInventory.Add(new NetItem(-15, 1, 0)); ServerSideCharacterConfig.StartingInventory.Add(new NetItem(-13, 1, 0)); ServerSideCharacterConfig.StartingInventory.Add(new NetItem(-16, 1, 0)); Order = 0; instance = this; }
///<summary> /// Method to Initialize plugin's code. ///</summary> public override void Initialize() { that = this; ConsoleLOG("Initializing TSGeoIP!"); if (!Directory.Exists(dataDir)) { ConsoleLOG("Didn't found TSGeoIP folder!"); Directory.CreateDirectory(dataDir); ConsoleLOG("Created TSGeoIP folder!"); } else { ConsoleLOG("Found TSGeoIP folder!"); } iSettings = new Settings(); Settings.LoadSettings(); if (iSettings.GeoIP_API.ToLower() == "geoip") { if (File.Exists(this.geoIPDB)) { this.geoIPLS = new LookupService(this.geoIPDB, LookupService.GEOIP_STANDARD); } else { ConsoleLOG("There is no GeoLiteCity.dat", TraceLevel.Error); this.geoIPLS = null; } } if (iSettings.GeoIP_API.ToLower() == "geoip2") { if (File.Exists(this.geoIP2DB)) { this.geoIP2DBR = new DatabaseReader(this.geoIP2DB); } else { ConsoleLOG("There is no GeoLite2-City.mmdb", TraceLevel.Error); this.geoIP2DBR = null; } } ServerApi.Hooks.GameInitialize.Register(this, this.OnInitialize); ServerApi.Hooks.ServerJoin.Register(this, this.OnJoin); ServerApi.Hooks.NetGreetPlayer.Register(this, this.OnNetGreet); ServerApi.Hooks.ServerChat.Register(this, this.OnChat); ServerApi.Hooks.ServerLeave.Register(this, this.OnLeave); }
public TShock(Main game) : base(game) { Config = new ConfigFile(); ServerSideCharacterConfig = new ServerSideConfig(); ServerSideCharacterConfig.StartingInventory.Add(new NetItem { netID = -15, prefix = 0, stack = 1 }); ServerSideCharacterConfig.StartingInventory.Add(new NetItem { netID = -13, prefix = 0, stack = 1 }); ServerSideCharacterConfig.StartingInventory.Add(new NetItem { netID = -16, prefix = 0, stack = 1 }); Order = 0; instance = this; }
internal static void LoadPlugins() { string ignoredPluginsFilePath = Path.Combine(ServerPluginsDirectoryPath, "ignoredplugins.txt"); List <string> ignoredFiles = new List <string>(); if (File.Exists(ignoredPluginsFilePath)) { ignoredFiles.AddRange(File.ReadAllLines(ignoredPluginsFilePath)); } List <FileInfo> fileInfos = new DirectoryInfo(ServerPluginsDirectoryPath).GetFiles("*.dll").ToList(); fileInfos.AddRange(new DirectoryInfo(ServerPluginsDirectoryPath).GetFiles("*.dll-plugin")); Dictionary <TerrariaPlugin, Stopwatch> pluginInitWatches = new Dictionary <TerrariaPlugin, Stopwatch>(); foreach (FileInfo fileInfo in fileInfos) { string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.Name); if (ignoredFiles.Contains(fileNameWithoutExtension)) { LogWriter.ServerWriteLine( string.Format("{0} was ignored from being loaded.", fileNameWithoutExtension), TraceLevel.Verbose); continue; } try { Assembly assembly; // The plugin assembly might have been resolved by another plugin assembly already, so no use to // load it again, but we do still have to verify it and create plugin instances. if (!loadedAssemblies.TryGetValue(fileNameWithoutExtension, out assembly)) { try { assembly = Assembly.Load(File.ReadAllBytes(fileInfo.FullName)); } catch (BadImageFormatException) { continue; } loadedAssemblies.Add(fileNameWithoutExtension, assembly); } if (!InvalidateAssembly(assembly, fileInfo.Name)) { continue; } foreach (Type type in assembly.GetExportedTypes()) { if (!type.IsSubclassOf(typeof(TerrariaPlugin)) || !type.IsPublic || type.IsAbstract) { continue; } object[] customAttributes = type.GetCustomAttributes(typeof(ApiVersionAttribute), false); if (customAttributes.Length == 0) { continue; } if (!IgnoreVersion) { var apiVersionAttribute = (ApiVersionAttribute)customAttributes[0]; Version apiVersion = apiVersionAttribute.ApiVersion; if (apiVersion.Major != ApiVersion.Major || apiVersion.Minor != ApiVersion.Minor) { LogWriter.ServerWriteLine( string.Format("Plugin \"{0}\" is designed for a different Server API version ({1}) and was ignored.", type.FullName, apiVersion.ToString(2)), TraceLevel.Warning); continue; } } TerrariaPlugin pluginInstance; try { Stopwatch initTimeWatch = new Stopwatch(); initTimeWatch.Start(); pluginInstance = (TerrariaPlugin)Activator.CreateInstance(type, game); initTimeWatch.Stop(); pluginInitWatches.Add(pluginInstance, initTimeWatch); } catch (Exception ex) { // Broken plugins better stop the entire server init. throw new InvalidOperationException( string.Format("Could not create an instance of plugin class \"{0}\".", type.FullName), ex); } plugins.Add(new PluginContainer(pluginInstance)); } } catch (Exception ex) { // Broken assemblies / plugins better stop the entire server init. throw new InvalidOperationException( string.Format("Failed to load assembly \"{0}\".", fileInfo.Name), ex); } } IOrderedEnumerable <PluginContainer> orderedPluginSelector = from x in Plugins orderby x.Plugin.Order, x.Plugin.Name select x; foreach (PluginContainer current in orderedPluginSelector) { Stopwatch initTimeWatch = pluginInitWatches[current.Plugin]; initTimeWatch.Start(); try { current.Initialize(); } catch (Exception ex) { // Broken plugins better stop the entire server init. throw new InvalidOperationException(string.Format( "Plugin \"{0}\" has thrown an exception during initialization.", current.Plugin.Name), ex); } initTimeWatch.Stop(); LogWriter.ServerWriteLine(string.Format( "Plugin {0} v{1} (by {2}) initiated.", current.Plugin.Name, current.Plugin.Version, current.Plugin.Author), TraceLevel.Info); } if (Profiler.WrappedProfiler != null) { foreach (var pluginWatchPair in pluginInitWatches) { TerrariaPlugin plugin = pluginWatchPair.Key; Stopwatch initTimeWatch = pluginWatchPair.Value; Profiler.InputPluginInitTime(plugin, initTimeWatch.Elapsed); } } }