<commit_before>﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
namespace TGServiceInterface
{
	public class Server
	{
		/// <summary>
		/// List of types that can be used with GetComponen
		/// </summary>
		public static readonly IList<Type> ValidInterfaces = new List<Type> { typeof(ITGByond), typeof(ITGChat), typeof(ITGCompiler), typeof(ITGConfig), typeof(ITGDreamDaemon), typeof(ITGRepository), typeof(ITGServerUpdater), typeof(ITGSService) };

		/// <summary>
		/// Base name of the communication pipe
		/// they are formatted as MasterPipeName/ComponentName
		/// </summary>
		public static string MasterPipeName = "TGStationServerService";

		/// <summary>
		/// Returns the requested server component interface. This does not guarantee a successful connection
		/// </summary>
		/// <typeparam name="T">The type of the component to retrieve</typeparam>
		/// <returns></returns>
		public static T GetComponent<T>()
		{
			var ToT = typeof(T);
			if (!ValidInterfaces.Contains(ToT))
				throw new Exception("Invalid type!");

			return new ChannelFactory<T>(new NetNamedPipeBinding { SendTimeout = new TimeSpan(0, 10, 0) }, new EndpointAddress(String.Format("net.pipe://localhost/{0}/{1}", MasterPipeName, typeof(T).Name))).CreateChannel();
		}
		
		/// <summary>
		/// Used to test if the service is avaiable on the machine
		/// Note that state can technically change at any time
		/// and any call to the service may throw an exception because it failed
		/// </summary>
		/// <returns>null on successful connection, error message on failure</returns>
		public static string VerifyConnection()
		{
			try
			{
				GetComponent<ITGSService>().VerifyConnection();
				return null;
			}
			catch(Exception e)
			{
				return e.ToString();
			}
		}
	}

	/// <summary>
	/// Interface for managing the service
	/// </summary>
	[ServiceContract]
	public interface ITGSService
	{
		/// <summary>
		/// Does nothing on the server end, but if the call completes, you can be sure you are connected. WCF won't throw until you try until you actually use the API
		/// </summary>
		[OperationContract]
		void VerifyConnection();

		/// <summary>
		/// Next stop of the service will not close DD and sets a flag for it to reattach once it restarts
		/// </summary>
		[OperationContract]
		void PrepareForUpdate();

		[OperationContract]
		[Obsolete("StopForUpdate is deprecated, please use PrepareForUpdate instead")]
		void StopForUpdate();

		/// <summary>
		/// Retrieve's the service's version
		/// </summary>
		/// <returns>The service's version</returns>
		[OperationContract]
		string Version();
	}

	/// <summary>
	/// How to modify the repo during the UpdateServer operation
	/// </summary>
	public enum TGRepoUpdateMethod
	{
		/// <summary>
		/// Do not update the repo
		/// </summary>
		None,
		/// <summary>
		/// Update the repo by merging the origin branch
		/// </summary>
		Merge,
		/// <summary>
		/// Update the repo by hard resetting to the remote branch
		/// </summary>
		Hard,
		/// <summary>
		/// Clean the repo by hard resetting to the origin branch
		/// </summary>
		Reset,
	}

	/// <summary>
	/// One stop shop for server updates
	/// </summary>
	[ServiceContract]
	public interface ITGServerUpdater
	{
		/// <summary>
		/// Updates the server fully with various options as a blocking operation
		/// </summary>
		/// <param name="updateType">How to handle the repository during the update</param>
		/// <param name="push_changelog_if_enabled">true if the changelog should be pushed to git</param>
		/// <param name="testmerge_pr">If not zero, will testmerge the designated pull request</param>
		/// <returns>null on success, error message on failure</returns>
		[OperationContract]
		string UpdateServer(TGRepoUpdateMethod updateType, bool push_changelog_if_enabled, ushort testmerge_pr = 0);
	}

}<commit_msg>Adds the interface for DD<commit_after>﻿using RGiesecke.DllExport;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ServiceModel;
namespace TGServiceInterface
{
	public class Server
	{
		/// <summary>
		/// List of types that can be used with GetComponen
		/// </summary>
		public static readonly IList<Type> ValidInterfaces = new List<Type> { typeof(ITGByond), typeof(ITGChat), typeof(ITGCompiler), typeof(ITGConfig), typeof(ITGDreamDaemon), typeof(ITGRepository), typeof(ITGServerUpdater), typeof(ITGSService), typeof(ITGServiceBridge) };

		/// <summary>
		/// Base name of the communication pipe
		/// they are formatted as MasterPipeName/ComponentName
		/// </summary>
		public static string MasterPipeName = "TGStationServerService";

		/// <summary>
		/// Returns the requested server component interface. This does not guarantee a successful connection
		/// </summary>
		/// <typeparam name="T">The type of the component to retrieve</typeparam>
		/// <returns></returns>
		public static T GetComponent<T>()
		{
			var ToT = typeof(T);
			if (!ValidInterfaces.Contains(ToT))
				throw new Exception("Invalid type!");

			return new ChannelFactory<T>(new NetNamedPipeBinding { SendTimeout = new TimeSpan(0, 10, 0) }, new EndpointAddress(String.Format("net.pipe://localhost/{0}/{1}", MasterPipeName, typeof(T).Name))).CreateChannel();
		}

		/// <summary>
		/// Used to test if the service is avaiable on the machine
		/// Note that state can technically change at any time
		/// and any call to the service may throw an exception because it failed
		/// </summary>
		/// <returns>null on successful connection, error message on failure</returns>
		public static string VerifyConnection()
		{
			try
			{
				GetComponent<ITGSService>().VerifyConnection();
				return null;
			}
			catch (Exception e)
			{
				return e.ToString();
			}
		}
	}

	/// <summary>
	/// Interface for managing the service
	/// </summary>
	[ServiceContract]
	public interface ITGSService
	{
		/// <summary>
		/// Does nothing on the server end, but if the call completes, you can be sure you are connected. WCF won't throw until you try until you actually use the API
		/// </summary>
		[OperationContract]
		void VerifyConnection();

		/// <summary>
		/// Next stop of the service will not close DD and sets a flag for it to reattach once it restarts
		/// </summary>
		[OperationContract]
		void PrepareForUpdate();

		[OperationContract]
		[Obsolete("StopForUpdate is deprecated, please use PrepareForUpdate instead")]
		void StopForUpdate();

		/// <summary>
		/// Retrieve's the service's version
		/// </summary>
		/// <returns>The service's version</returns>
		[OperationContract]
		string Version();
	}

	/// <summary>
	/// How to modify the repo during the UpdateServer operation
	/// </summary>
	public enum TGRepoUpdateMethod
	{
		/// <summary>
		/// Do not update the repo
		/// </summary>
		None,
		/// <summary>
		/// Update the repo by merging the origin branch
		/// </summary>
		Merge,
		/// <summary>
		/// Update the repo by hard resetting to the remote branch
		/// </summary>
		Hard,
		/// <summary>
		/// Clean the repo by hard resetting to the origin branch
		/// </summary>
		Reset,
	}

	/// <summary>
	/// One stop shop for server updates
	/// </summary>
	[ServiceContract]
	public interface ITGServerUpdater
	{
		/// <summary>
		/// Updates the server fully with various options as a blocking operation
		/// </summary>
		/// <param name="updateType">How to handle the repository during the update</param>
		/// <param name="push_changelog_if_enabled">true if the changelog should be pushed to git</param>
		/// <param name="testmerge_pr">If not zero, will testmerge the designated pull request</param>
		/// <returns>null on success, error message on failure</returns>
		[OperationContract]
		string UpdateServer(TGRepoUpdateMethod updateType, bool push_changelog_if_enabled, ushort testmerge_pr = 0);
	}

	/// <summary>
	/// Used by DD to access the interop API with call()()
	/// </summary>
	[ServiceContract]
	public interface ITGServiceBridge
	{
		/// <summary>
		/// Called from /world/ExportService(command)
		/// </summary>
		/// <param name="command">The command to run</param>
		/// <returns></returns>
		[OperationContract]
		string InteropMessage(string command);
	}

	/// <summary>
	/// Holds the proc that DD calls to access <see cref="ITGServiceBridge"/>
	/// </summary>
	class DDInteropCallHolder
	{
		/// <summary>
		/// The proc that DD calls to access <see cref="ITGServiceBridge"/>
		/// </summary>
		/// <param name="args">The arguments passed</param>
		/// <returns>The result of the command</returns>
		[DllExport("DDEntryPoint", CallingConvention = CallingConvention.Cdecl)]
		public static string DDEntryPoint(string[] args)
		{
			try
			{
				return Server.GetComponent<ITGServiceBridge>().InteropMessage(String.Join(" ", args));
			}
			catch (Exception e)
			{
				return "Service Error: " + e.Message;
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12977787800202953, 'fraction_numerical': 0.0015785319652722968, 'mean_word_length': 3.337408312958435, 'pattern_counts': {'":': 0, '<': 124, '<?xml version=': 0, '>': 124, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3073947', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2562, 'n_words': 858}