AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AirplaneSelector.cs
1 using System.IO;
2 using UnityEngine;
3 using Commons;
4 using System;
5 
6 
7 
8 namespace AirControl {
9  public class AirplaneSelector : MonoBehaviour
10  {
11  public enum availableAirplanes { Cessna152, F4UCorsair };
12  public availableAirplanes activeAirplane;
13 
14 
15 
16 
17  void Awake()
18  {
19  // Activate selected plane and deactivate others
20  // if it is not running in editor then select airplane supplied by the json/default config
21 
22  foreach(availableAirplanes currentAirplane in Enum.GetValues(typeof(availableAirplanes))){
23  // disable all the non selected airplanes
24  if (activeAirplane != currentAirplane)
25  {
26  Debug.Log("Disabled Airplane : "+currentAirplane);
27  GameObject.Find(currentAirplane.ToString()).SetActive(false);
28  }
29  else
30  {
31  Debug.Log("Active Airplane : "+ activeAirplane);
32  GameObject.Find(activeAirplane.ToString()).SetActive(true);
33  }
34  }
35 
36  //disable all audio listners
37  AudioListener [] listeners = GameObject.FindObjectsOfType<AudioListener>();
38  foreach(var eachListner in listeners){
39  eachListner.enabled = false;
40  }
41 
42  // log the streaming asset path
43  Debug.Log("Assets will be saved at : "+ Application.streamingAssetsPath);
44 
45  //Loading settings from json
46  AirplaneProperties.initAirplaneJsonObject();
47  Debug.Log(CommonFunctions.airplanePreset);
48  if (CommonFunctions.ifExists(CommonFunctions.presetFilepath)){
49  // json is not present Write it to disk
50  if (!AirplaneProperties.readJson(CommonFunctions.presetFilepath)) // tocheck if the json is there but empty
51  {
52  try{
53  AirplaneProperties.saveJson(CommonFunctions.presetFilepath);
54  }
55  catch (IOException ioExp){
56  Debug.Log(ioExp.Message);
57  }
58  }
59  }
60  else{
61  try{
62  AirplaneProperties.saveJson(CommonFunctions.presetFilepath);
63  }
64  catch (IOException ioExp){
65  Debug.Log(ioExp.Message);
66  }
67  }
68  CheckRecency();
69 // once everything is done, load
70 #if !UNITY_EDITOR
71  CommonFunctions.ActiveAirplane = (string)CommonFunctions.airplanePreset["General/activeAirplane"];
72 #elif UNITY_EDITOR
73  CommonFunctions.ActiveAirplane = activeAirplane.ToString();
74 #endif
75  }
76 
81  void CheckRecency()
82  {
83  //check aircontrolversion
84  var jsonAirControlVersion = CommonFunctions.jsonPreset["General/airControlVersion"];
85  var currentAirControlVersion = CommonFunctions.airplanePreset["General/airControlVersion"];
86  if (jsonAirControlVersion != currentAirControlVersion){
87  Debug.Log("Saved version and current version are different, Writting defaults to the json");
88  AirplaneProperties.saveJson(CommonFunctions.presetFilepath);
89  }
90  //check priority
91  var jsonPriority = (int)CommonFunctions.jsonPreset["General/priority"];
92  var currentPriority = (int)CommonFunctions.airplanePreset["General/priority"];
93  if (jsonPriority > currentPriority ){
94  // if the jsonDocVersion greater then read properties from json
95  Debug.Log("json doc version has higher priority, reading value from json");
96  CommonFunctions.airplanePreset = CommonFunctions.jsonPreset;
97  }
98  else{
99  // if the jsonDocVersion is lower then default then read properties from default
100  Debug.Log("json doc version has lower priority, reading value from defaults");
101  }
102 
103  }
104  }
105 
106 }
107 
108 
AirControl
Definition: AirplaneSelector.cs:8
AirControl.AirplaneSelector
Definition: AirplaneSelector.cs:9
Commons
Definition: AirplaneProperties.cs:14