3 using System.Collections.Generic;
18 namespace droid.Runtime.Managers {
22 [DisallowMultipleComponent]
23 [AddComponentMenu(
"Neodroid/Managers/VanillaManager")]
34 if (this._configuration == null) {
38 return this._configuration;
87 void FetchCommandLineArguments() {
88 var arguments = Environment.GetCommandLineArgs();
90 for (var i = 0; i < arguments.Length; i++) {
91 if (arguments[i] ==
"-ip") {
95 if (arguments[i] ==
"-port") {
103 void CreateMessagingServer() {
113 }
catch (Exception exception) {
114 Debug.Log(exception);
124 void StartMessagingServer(
bool threaded =
false) {
135 this._Message_Server.ListenForClientToConnect(this.OnDebugCallback);
137 if (this.Debugging) {
138 Debug.Log(
" Messaging Server is listening for clients");
143 this.OnListeningCallback();
151 #region PrivateFields 155 [Header(
"Development", order = 110)]
166 bool _testing_Actuators;
169 const int _script_execution_order = -1000;
176 [Header(
"Simulation", order = 80)]
188 bool _syncing_environments;
193 bool _awaiting_reply;
195 [SerializeField]
bool _step;
197 WaitForEndOfFrame _wait_for_end_of_frame =
new WaitForEndOfFrame();
198 WaitForFixedUpdate _wait_for_fixed_update =
new WaitForFixedUpdate();
199 List<Reaction> _sample_reactions =
new List<Reaction>();
203 #region Getter And Setters 209 lock (this._send_lock) {
210 return this._Current_Reactions;
214 lock (this._send_lock) {
215 this._Current_Reactions = value;
222 public float SimulationTimeScale {
223 get {
return Time.timeScale; }
226 Time.timeScale = Math.Min(value, 99);
227 this._last_simulation_time = Math.Min(value, 99);
229 Time.timeScale = value;
230 this._last_simulation_time = value;
234 Time.fixedDeltaTime = 0.02F * Time.timeScale;
239 [SerializeField]
float _last_simulation_time;
243 public bool HasStepped {
get {
return this._has_stepped; }
set { this._has_stepped = value; } }
247 public bool TestActuators {
248 get {
return this._testing_Actuators; }
249 set { this._testing_Actuators = value; }
254 public bool Debugging {
255 get {
return this._debugging; }
257 if (this._Message_Server != null) {
258 this._Message_Server.Debugging = value;
261 this._debugging = value;
267 public bool AwaitingReply {
269 lock (this._send_lock) {
270 return this._awaiting_reply;
274 lock (this._send_lock) {
275 this._awaiting_reply = value;
284 public bool IsSyncingEnvironments {
285 get {
return this._syncing_environments; }
286 set { this._syncing_environments = value; }
291 public bool Stepping {
get {
return this._step; } }
295 #region PrivateMembers 299 protected Dictionary<string, IEnvironment> _Environments =
new Dictionary<string, IEnvironment>();
303 public void Clear() { this._Environments.Clear(); }
313 [SerializeField]
bool _has_stepped;
317 #region UnityCallbacks 322 if (Instance == null) {
324 }
else if (Instance ==
this) {
326 if (this.Debugging) {
327 Debug.Log(
"Using " + Instance);
331 Debug.LogWarning(
"WARNING! There are multiple SimulationManagers in the scene! Only using " 336 if (!Application.isPlaying) {
337 var manager_script = MonoScript.FromMonoBehaviour(
this);
338 if (MonoImporter.GetExecutionOrder(manager_script) != _script_execution_order) {
339 MonoImporter.SetExecutionOrder(manager_script,
340 _script_execution_order);
341 Debug.LogWarning(
"Execution Order changed, you will need to press play again to make everything function correctly!");
342 EditorApplication.isPlaying =
false;
352 this.FetchCommandLineArguments();
361 this.EarlyFixedUpdateEvent += this.OnPreTick;
362 this.FixedUpdateEvent += this.OnTick;
363 this.FixedUpdateEvent += this.Tick;
364 this.LateFixedUpdateEvent += this.OnPostTick;
365 this.StartCoroutine(this.LateFixedUpdateEventGenerator());
367 this.EarlyUpdateEvent += this.OnPreTick;
368 this.UpdateEvent += this.OnTick;
369 this.UpdateEvent += this.Tick;
372 this.LateUpdateEvent += this.OnPostTick;
375 this.OnPostRenderEvent += this.OnPostTick;
378 this.OnRenderImageEvent += this.OnPostTick;
381 this.StartCoroutine(this.EndOfFrameEventGenerator());
382 this.OnEndOfFrameEvent += this.OnPostTick;
384 default:
throw new ArgumentOutOfRangeException();
388 this.CreateMessagingServer();
390 this.StartMessagingServer();
392 this.StartMessagingServer(
true);
400 QualitySettings.SetQualityLevel(configuration.
QualityLevel,
true);
401 QualitySettings.vSyncCount = configuration.
vSyncCount;
404 this.SimulationTimeScale = configuration.
TimeScale;
408 Screen.SetResolution(1, 1,
false);
412 Screen.SetResolution(
413 width : configuration.
Width,
414 height : configuration.
Height,
420 PlayerSettings.colorSpace = configuration.
ColorSpace;
421 PlayerSettings.displayResolutionDialog = ResolutionDialogSetting.Disabled;
428 void OnPostRender() { this.OnPostRenderEvent?.Invoke(); }
434 void OnRenderImage(RenderTexture src, RenderTexture dest) {
435 this.OnRenderImageEvent?.Invoke();
441 this.EarlyFixedUpdateEvent?.Invoke();
442 this.FixedUpdateEvent?.Invoke();
448 IEnumerator LateFixedUpdateEventGenerator() {
450 yield
return this._wait_for_fixed_update;
452 if (this.Debugging) {
453 Debug.Log(
"LateFixedUpdate");
456 this.LateFixedUpdateEvent?.Invoke();
467 yield
return this._wait_for_end_of_frame;
470 if (this.Debugging) {
471 Debug.Log(
"EndOfFrameEvent");
474 this.OnEndOfFrameEvent?.Invoke();
476 #pragma warning disable 162 479 #pragma warning restore 162 486 this.EarlyUpdateEvent?.Invoke();
487 this.UpdateEvent?.Invoke();
492 protected void LateUpdate() { this.LateUpdateEvent?.Invoke(); }
496 #region PrivateMethods 502 if (this.Debugging) {
503 Debug.Log(
"OnPreTick");
516 if (this.Debugging) {
529 if (this.Debugging) {
530 Debug.Log(
"OnPostTick");
534 foreach (var environment
in this._Environments.Values) {
535 environment.PostStep();
542 if (this._has_stepped) {
543 this.ClearCurrentReactions();
550 if (!this._syncing_environments) {
551 this.React(this.CurrentReactions);
554 if (this.AwaitingReply) {
555 var states = this.CollectStates();
556 this.PostReact(states);
559 this.HasStepped =
true;
565 if (this.TestActuators) {
566 this.React(this.SampleRandomReactions());
567 this.CollectStates();
570 foreach (var environment
in this._Environments.Values) {
575 if (this.Debugging) {
585 lock (this._send_lock) {
586 foreach (var env
in this._Environments.Values) {
587 if (env.IsResetting) {
589 if (this.Debugging) {
590 Debug.Log($
"Environment {env} is resetting");
594 this._syncing_environments =
true;
599 this._syncing_environments =
false;
602 if (this.Debugging) {
603 Debug.Log(
"Not skipping frame, replying...");
608 this.AwaitingReply =
false;
609 this._skip_frame_i = 0;
611 this._skip_frame_i += 1;
613 if (this.Debugging) {
614 Debug.Log($
"Skipping frame, {this._skip_frame_i}/{this.Configuration.FrameSkips}");
626 this._sample_reactions.Clear();
627 foreach (var environment
in this._Environments.Values) {
628 this._sample_reactions.Add(environment.SampleReaction());
631 return this._sample_reactions.ToArray();
639 lock (this._send_lock) {
641 var describe =
false;
642 if (this.CurrentReactions != null) {
643 foreach (var reaction
in this.CurrentReactions) {
644 if (reaction.Parameters.Describe) {
651 simulator_configuration_message : configuration_message,
652 do_serialise_unobservables :
654 serialise_individual_observables :
655 describe ||
this.
Configuration.AlwaysSerialiseIndividualObservables,
656 serialise_aggregated_float_array : describe
657 ||
this._configuration
658 .AlwaysSerialiseAggregatedFloatArray);
660 if (this.Debugging) {
661 Debug.Log(
"Replying");
670 void ClearCurrentReactions() {
672 this.CurrentReactions =
new Reaction[] { };
677 #region PublicMethods 684 this.SetStepping(reaction);
687 foreach (var environment
in this._Environments.Values) {
688 if (reaction.RecipientEnvironment !=
"all") {
690 if (this.Debugging) {
691 Debug.Log($
"Applying reaction to {reaction.RecipientEnvironment} environment");
694 if (this._Environments.ContainsKey(reaction.RecipientEnvironment)) {
695 states[i++] = this._Environments[reaction.RecipientEnvironment].ReactAndCollectState(reaction);
699 if (this.Debugging) {
700 Debug.Log($
"Could not find environment: {reaction.RecipientEnvironment}");
706 if (this.Debugging) {
707 Debug.Log(
"Applying reaction to all environments");
710 states[i++] = environment.ReactAndCollectState(reaction);
722 this.SetStepping(reaction);
727 if (this.Debugging) {
728 Debug.Log($
"Could not find an environment with the identifier: {reaction.RecipientEnvironment}");
733 if (this.Debugging) {
734 Debug.Log(
"Applying to all environments");
738 foreach (var environment
in this._Environments.Values) {
739 environment.React(reaction);
749 this.SetStepping(reactions);
750 foreach (var reaction
in reactions) {
751 if (this._Environments.ContainsKey(reaction.RecipientEnvironment)) {
752 this._Environments[reaction.RecipientEnvironment].React(reaction);
753 }
else if (reaction.RecipientEnvironment ==
"all") {
755 if (this.Debugging) {
756 Debug.Log(
"Applying to all environments");
760 foreach (var environment
in this._Environments.Values) {
761 environment.React(reaction);
765 if (this.Debugging) {
766 Debug.Log($
"Could not find an environment with the identifier: {reaction.RecipientEnvironment}");
777 var environments = this._Environments.Values;
780 foreach (var environment
in environments) {
781 states[i++] = environment.CollectState();
787 void SetStepping(
Reaction reaction) {
789 this.SetStepping(
true);
793 void SetStepping(
bool step) {
796 if (this.Debugging) {
797 Debug.Log(
"Stepping from Reaction");
804 if (this.Debugging) {
805 Debug.Log(
"Not stepping from Reaction");
808 if (this.HasStepped) {
814 void SetStepping(
Reaction[] reactions) {
816 this.SetStepping(
true);
820 public void SetTesting(
bool arg0) { this.TestActuators = arg0; }
873 if (this._Message_Server != null) {
896 if (!this._Environments.ContainsKey(identifier)) {
898 if (this.Debugging) {
899 Debug.Log($
"Manager {this.name} already has an environment with the identifier: {identifier}");
903 this._Environments.Add(identifier, environment);
905 Debug.LogWarning($
"WARNING! Please check for duplicates, SimulationManager {this.name} already has envi" 906 + $
"ronment {identifier} registered");
915 if (this._Environments.ContainsKey(identifier)) {
917 if (this.Debugging) {
918 Debug.Log($
"SimulationManager {this.name} unregistered Environment {identifier}");
922 this._Environments.Remove(identifier);
930 this.UnRegister(neodroid_environment, neodroid_environment.
Identifier);
935 #region MessageServerCallbacks 940 void OnReceiveCallback(
Reaction[] reactions) {
941 lock (this._send_lock) {
943 if (this.Debugging) {
944 Debug.Log($
"Received: {reactions.Select(r => r.ToString()).Aggregate((current, next) => $"{current}, {next}
")}");
948 this.SetReactionsFromExternalSource(reactions);
950 this.OnReceiveEvent?.Invoke();
958 lock (this._send_lock) {
959 if (reactions != null) {
960 if (this.AwaitingReply || !this.HasStepped) {
962 if (this.Debugging) {
963 Debug.Log($
"Got new reaction while not having stepped({!this.HasStepped}) or replied({this.AwaitingReply})");
968 this.CurrentReactions = reactions;
969 foreach (var current_reaction
in this.CurrentReactions) {
973 this.
Configuration.StepExecutionPhase = this.CurrentReactions[0].Parameters.Phase;
974 this.AwaitingReply =
true;
975 this.HasStepped =
false;
977 Debug.LogWarning(
"Reaction was null");
984 void OnDisconnectCallback() {
986 if (this.Debugging) {
987 Debug.Log(
"Client disconnected.");
995 void OnDebugCallback(
string error) {
997 if (this.Debugging) {
998 Debug.Log(
"DebugCallback: " + error);
1005 void OnListeningCallback() {
1007 if (this.Debugging) {
1008 Debug.Log(
"Client connected");
1013 this.OnDisconnectCallback,
1014 this.OnDebugCallback);
1019 #region Deconstruction 1023 void OnApplicationQuit() { this._Message_Server.
CleanUp(); }
1027 void OnDestroy() { this._Message_Server.
Destroy(); }
EnvironmentState [] CollectStates()
void React(Reaction reaction)
bool ApplyResolutionSettings
void SetTesting(bool arg0)
String RecipientEnvironment
bool ApplyQualitySettings
abstract void PollData(dynamic data)
void UnRegister(IEnvironment neodroid_environment)
void SendStates(EnvironmentState[] environment_states, bool do_serialise_unobservables=false, bool serialise_individual_observables=false, bool serialise_aggregated_float_array=false, SimulatorConfigurationMessage simulator_configuration_message=null, string api_version=_api_version)
EnvironmentState [] ReactAndCollectStates(Reaction reaction)
void StatusString(DataPoller recipient)
void Register(IEnvironment environment, string identifier)
void UnRegister(IEnvironment environment, string identifier)
Contains everything relevant to configuring simulation environments engine specific settings ...
void StartReceiving(Action< Reaction[]> cmd_callback, Action disconnect_callback, Action< string > debug_callback)
void ApplyConfigurationToUnity(ISimulatorConfiguration configuration)
FrameFinishes
Determines where in the monobehaviour cycle a frame/step is finished
void ResetAllEnvironments()
void Register(IEnvironment environment)
Boolean OptimiseWindowForSpeed
IEnumerator EndOfFrameEventGenerator()
MessageServer _Message_Server
ReactionParameters Parameters
void SetReactionsFromExternalSource(Reaction[] reactions)
void React(Reaction[] reactions)
Reaction [] SampleRandomReactions()
void PostReact(EnvironmentState[] states)
bool _Listening_For_Clients
Configuration(string configurable_name, float configurable_value, bool sample_random=false)
Action LateFixedUpdateEvent
Action EarlyUpdateEvent
Can be subscribed to for pre update events (Will be called before any Update on any script) ...
Action OnRenderImageEvent
SimulationType
Determines the discrete timesteps of the simulation environment.
Action EarlyFixedUpdateEvent
Can be subscribed to for pre fixed update events (Will be called before any FixedUpdate on any script...