internal static StrategyBase GetInstance(StrategyID strategyID, ClientConnection clientConnection, GameInfo gameInfo, string botAuthenticationGuid, string playerName, short sideIndex, bool isGameController, bool isCommander) { StrategyBase returnValue = null; if (_strategyTypesByStrategyID == null) { var types = from type in Assembly.GetAssembly(typeof(StrategyBase)).GetTypes() where typeof(StrategyBase).IsAssignableFrom(type) && type.IsAbstract == false select type; _strategyTypesByStrategyID = types .ToDictionary(p => ((StrategyBase)Activator.CreateInstance(p)).StrategyID, r => r); } if (_strategyTypesByStrategyID.ContainsKey(strategyID) == false) { throw new NotSupportedException(strategyID.ToString()); } returnValue = (StrategyBase)Activator.CreateInstance(_strategyTypesByStrategyID[strategyID]); returnValue.Attach(clientConnection, gameInfo, botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); return(returnValue); }
public void Log(string message) { Console.WriteLine($"{StrategyID.ToString()} {PlayerName}: {message}"); string filename = $"c:\\1\\Logs\\{PlayerName}_strategy.txt"; Exception lastException = null; for (int i = 0; i < 30; i++) { try { File.AppendAllText(filename, $"{DateTime.Now} {StrategyID.ToString()} {PlayerName}: {message}\n"); lastException = null; break; } catch (Exception ex) { lastException = ex; Thread.Sleep(10); } } if (lastException != null) { throw lastException; } }
public void Attach(AllegianceInterop.ClientConnection client, GameInfo gameInfo, string botAuthenticationGuid, string playerName, short sideIndex, bool isGameController, bool isCommander) { ClientConnection = client; PlayerName = playerName; SideIndex = sideIndex; IsGameController = isGameController; IsCommander = isCommander; BotAuthenticationGuid = botAuthenticationGuid; GameInfo = gameInfo; _messageReceiver = new MessageReceiver(playerName); client.OnAppMessage += _messageReceiver.OnAppMessage; _messageReceiver.FMD_S_JOIN_SIDE += _messageReceiver_FMD_S_JOIN_SIDE; _messageReceiver.FMD_S_SHIP_STATUS += _messageReceiver_FMD_S_SHIP_STATUS; //_messageReceiver.FMD_S_EXPORT += _messageReceiver_FMD_S_EXPORT; _messageReceiver.FMD_S_CLUSTERINFO += _messageReceiver_FMD_S_CLUSTERINFO; _messageReceiver.FMD_CS_PING += _messageReceiver_FMD_CS_PING; // We'll get one of these every 5 seconds, using this to drive "less frequent" updates. _messageReceiver.FMD_S_DOCKED += _messageReceiver_FMD_S_DOCKED; _messageReceiver.FMD_S_MISSION_STAGE += _messageReceiver_FMD_S_MISSION_STAGE; _messageReceiver.FMD_S_BALLOT += _messageReceiver_FMD_S_BALLOT; _messageReceiver.FMD_S_GAME_OVER += _messageReceiver_FMD_S_GAME_OVER; AttachMessages(_messageReceiver, botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); Log($"Starting strategy: {StrategyID.ToString()}"); Start(); }
public StrategyBase(StrategyID strategyID, TimeSpan timeout) { StrategyID = strategyID; Timeout = timeout; StartTime = DateTime.Now; _cancellationTokenSource = new CancellationTokenSource(); }
//public Dictionary<StrategyID, Type> LoadPlugins(string assemblyName) //{ // var assemb = Assembly.LoadFrom(assemblyName); // var types = from type in assemb.GetTypes() // where typeof(StrategyBase).IsAssignableFrom(type) // select type; // //Dictionary<StrategyID, StrategyInstanceCreationDelegate> instances = types.Select( // // v => new StrategyInstanceCreationDelegate(() => // // { // // return (StrategyBase)Activator.CreateInstance(v); // // })) // // .ToDictionary(p => ((StrategyBase)Activator.CreateInstance(p)).StrategyID, r => r); // Dictionary<StrategyID, Type> instances = types // .ToDictionary(p => ((StrategyBase)Activator.CreateInstance(p)).StrategyID, r => r); // return instances; //} public StrategyBase CreateInstance(StrategyID strategyID) { Type strategyType; if (_strategyTypesByStrategyID.TryGetValue(strategyID, out strategyType) == false) { throw new NotSupportedException(strategyID.ToString()); } return((StrategyBase)Activator.CreateInstance(strategyType)); }
public void ChangeStrategy(AllegianceInterop.ClientConnection clientConnection, StrategyID strategyID, string playerName, short sideIndex, bool isGameController, bool isCommander) { //AllegianceInterop.ClientConnection clientConnection; //if (_connectedClientsByPlayerName.TryGetValue(playerName, out clientConnection) == false) //{ // throw new Exception("Couldn't get connection for playerName: " + playerName); //} //AllegianceInterop.ClientConnection.OnAppMessageDelegate currentStrategyOnAppMessageDelegate; //if (_currentStrategyAppMessageDelegateByPlayerName.TryGetValue(playerName, out currentStrategyOnAppMessageDelegate) == true) //{ // clientConnection.OnAppMessage -= currentStrategyOnAppMessageDelegate; // _currentStrategyAppMessageDelegateByPlayerName.Remove(playerName); //} StrategyBase currentStrategy; if (_currentStrategyByPlayerName.TryGetValue(playerName, out currentStrategy) == false) { currentStrategy = null; } else { //currentStrategy.OnStrategyComplete -= Strategy_OnStrategyComplete; _currentStrategyByPlayerName.Remove(playerName); } StrategyBase strategy = StrategyBase.GetInstance(strategyID, clientConnection, _gameInfo, _botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); strategy.OnStrategyComplete += Strategy_OnStrategyComplete; strategy.OnGameComplete += Strategy_OnGameComplete; //StrategyBase strategy = _assemblyLoader.CreateInstance(strategyID); //strategy.Attach(clientConnection, _gameInfos[sideIndex], _botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); // Can't hook the transparent object directly to the client, the events raised by c++/cli won't trigger on the managed end // becuase they are attached to a copy of the object in the far application domain, so we will attach to the event on the // near application domain, then directly call the MessageReceiver handler in the far domain passing the byte array // through instead of the tranparent proxied object. //AllegianceInterop.ClientConnection.OnAppMessageDelegate onAppMessageDelegate = new AllegianceInterop.ClientConnection.OnAppMessageDelegate((AllegianceInterop.ClientConnection messageClientConnection, byte[] bytes) => // { // strategy.OnAppMessage(bytes); // }); // clientConnection.OnAppMessage += onAppMessageDelegate; // _currentStrategyAppMessageDelegateByPlayerName.Add(playerName, onAppMessageDelegate); // _currentStrategyByPlayerName.Add(playerName, strategy); //} //else //{ // throw new Exception($"Error, couldn't find the {strategyID.ToString()} strategy in the loaded strategy list."); //} }
//public void LoadStrategies() //{ // // To load strategies into the current app domain without using transparent proxy, you can uncomment this, but it // // will break dynamic strategy updates. // // // //_currentAppDomain = AppDomain.CurrentDomain; // //string target = Path.Combine(_currentAppDomain.BaseDirectory, "Strategies.dll"); // //_assemblyLoader = new AssemblyLoader(target); // //return; // // Load strategies into a separate app domain so that they can be hot-swapped while a game is running. // var an = Assembly.GetExecutingAssembly().GetName(); // var appDomainSetup = new AppDomainSetup // { // PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory, // ShadowCopyDirectories = AppDomain.CurrentDomain.BaseDirectory, // ShadowCopyFiles = "true" // }; // _currentAppDomain = AppDomain.CreateDomain("V1", (Evidence)null, appDomainSetup); // //Console.WriteLine(_currentAppDomain.BaseDirectory); // _currentAppDomain.Load(typeof(AssemblyLoader).Assembly.FullName); // string target = Path.Combine(_currentAppDomain.BaseDirectory, "Strategies.dll"); // _assemblyLoader = (AssemblyLoader)Activator.CreateInstance( // _currentAppDomain, // typeof(AssemblyLoader).Assembly.FullName, // typeof(AssemblyLoader).FullName, // false, // BindingFlags.Public | BindingFlags.Instance, // null, // new object[] { target }, // null, // null).Unwrap(); //} public void ChangeStrategy(AllegianceInterop.ClientConnection clientConnection, StrategyID strategyID, StrategyBase lastStrategy) { ChangeStrategy(clientConnection, strategyID, lastStrategy.PlayerName, lastStrategy.SideIndex, lastStrategy.IsGameController, lastStrategy.IsCommander); }