//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, 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; //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."); //} }
private void CreatePlayerThread(object createPlayerObject) { CreatePlayerThreadParams createPlayerParams = (CreatePlayerThreadParams)createPlayerObject; Console.WriteLine($"Creating player: {createPlayerParams.playerName}"); //if (_teamDirectorPlayerInfoByPlayerName.ContainsKey(playerName) == true) // _teamDirectorPlayerInfoByPlayerName.Remove(playerName); //_teamDirectorPlayerInfoByPlayerName.Add(playerName, new TeamDirectorPlayerInfo() //{ // IsCommander = isCommander, // IsGameController = isGameController, // PlayerName = playerName, // SideIndex = sideIndex //}); AllegianceInterop.ClientConnection clientConnection = new AllegianceInterop.ClientConnection(Configuration.ArtPath); //StrategyBase strategy; //if (_strategiesByStrategyID.TryGetValue(StrategyID.ConnectToGame, out strategy) == true) //{ // strategy.Attach(clientConnection, _botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); // strategy.OnStrategyComplete += Strategy_OnConnectToGameStrategyComplete; // // 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. // clientConnection.OnAppMessage += new AllegianceInterop.ClientConnection.OnAppMessageDelegate((AllegianceInterop.ClientConnection messageClientConnection, byte[] bytes) => // { // strategy.OnAppMessage(bytes); // }); //} //else //{ // throw new Exception("Error, couldn't find the ConnectToGame strategy in the loaded strategy list."); //} ChangeStrategy(clientConnection, StrategyID.ConnectToGame, createPlayerParams.playerName, createPlayerParams.sideIndex, createPlayerParams.isGameController, createPlayerParams.isCommander); bool connected = false; for (int i = 0; i < 30; i++) { if (clientConnection.ConnectToLobby(_cancellationTokenSource, _lobbyAddress, createPlayerParams.playerName, _botAuthenticationGuid) == true) { lock (_connectedClientsByPlayerName) { _connectedClientsByPlayerName.Add(createPlayerParams.playerName, clientConnection); } connected = true; break; } Log(createPlayerParams.playerName, "Couldn't connect, retrying."); Thread.Sleep(100); } if (connected == false) { Log(createPlayerParams.playerName, "Couldn't connect. Giving up!"); } else { Log(createPlayerParams.playerName, "Starting message processing loop."); //Task.Run(() => //{ while (_cancellationTokenSource.IsCancellationRequested == false) { try { clientConnection.SendAndReceiveUpdate(); } catch (Exception ex) { Log(createPlayerParams.playerName, ex); } StrategyBase currentStrategy; if (_currentStrategyByPlayerName.TryGetValue(createPlayerParams.playerName, out currentStrategy) == true) { if (DateTime.Now - currentStrategy.StartTime > currentStrategy.Timeout) { ResetClient(currentStrategy.PlayerName); break; } } Thread.Sleep(100); } //}); } }
//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); }