Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Unity.cpp
Go to the documentation of this file.
1 #include <Unity.h>
2 #include <iostream>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <errno.h>
9 
10 using namespace std;
11 
12 #define MOCK_SIZE 513 // number of lines with mocked values
13 
14 static double mockData[MOCK_SIZE];
15 
17 {
18  this->buildPid = 0;
19  cout << "Initiating Unity" << endl;
20 }
21 
23 {
24  if(this->buildPid > 0)
25  {
26  cout << "Killing unity process: " << this->buildPid << endl;
27  kill(this->buildPid, SIGTERM);
28  }
29 
30  if(!this->infoFile.is_open())
31  this->infoFile.close();
32 
33  if(!this->altitudeFile.is_open())
34  this->altitudeFile.close();
35 
36  if(!this->rotationFile.is_open())
37  this->rotationFile.close();
38 
39  if(!this->positionFile.is_open())
40  this->positionFile.close();
41 
42  cout << "Terminating Unity" << endl;
43 }
44 
46 {
47  // Creates a whole new process
48  this->buildPid = fork();
49  if(this->buildPid == -1)
50  {
51  cout << "Problems while initiazing a new process!!!" << endl;
52  perror("Reason");
53  exit(-1);
54  }
55 
56  // pid > 0 means this is still parent process
57  if(this->buildPid > 0)
58  {
59  cout << "Initiating unity build with the process: " << this->buildPid << endl;
60  return; // so we just keep doing our amazing job
61  }
62 
63  // If we got here, from the line bellow until exit(0) means totaly the child process
64  cout << "$ " << CURRENT_UNITY_BUILD << endl;
65  int rc = execl(CURRENT_UNITY_BUILD, "currentBuild", NULL);
66  if(rc == -1)
67  {
68  cout << "Error while initiating unity build" << endl;
69  perror("Reason");
70  }
71  exit(0);
72 }
73 
75 {
76  // First forks this process do load the heavy unity application
77  this->initBuild();
78 
79  // Now open some useful files
80  this->altitudeFile.open(UNITY_ALTITUDE_FILE, std::ios::in | std::ios::binary | std::ios::ate);
81  this->infoFile.open(UNITY_INFO_FILE);
82  this->rotationFile.open(UNITY_ROTATION_FILE);
83  this->positionFile.open(UNITY_POSITION_FILE);
84 
85  if(!this->infoFile.is_open() || !this->altitudeFile.is_open() ||
86  !this->rotationFile.is_open() || this->positionFile.is_open())
87  {
88  cout << "Couldn't open unity communication files";
89  }
90 }
91 
93 {
94  static std::ifstream file;
95  int returned = -1;
96 
97  #ifdef MOCK_DATA
98  std::cout << "Mocaaaando" << std::endl;
99  static int mockIndex = 0;
100 
101  // First time
102  if(MOCK_SIZE == 0)
103  {
104  file.open("files/unity_altitude_mock.txt", std::ios::in | std::ios::binary | std::ios::ate);
105  if(file.is_open())
106  {
107  for(int i = 0; i < MOCK_SIZE; i++)
108  file >> mockData[i];
109  file.close();
110  std::cout << "Unity mockData loaded: " << MOCK_SIZE * sizeof(double) << " bytes long" << std::endl;
111  }
112  else
113  {
114  std::cout << "Failed to load unity mockData" << std::endl;
115  mockData[0] = -1.0;
116  }
117  }
118 
119  if(mockIndex >= MOCK_SIZE)
120  mockIndex = 0;
121 
122  returned = (int)mockData[mockIndex];
123  mockIndex++;
124 
125  #else
126  if(this->altitudeFile.is_open())
127  {
128  // Make sure to read new data
129  this->altitudeFile.sync();
130 
131  this->altitudeFile >> returned;
132 
133  // Rewinds it, so next time it will read from the beginning again
134  this->altitudeFile.seekg(0, ios_base::beg);
135  }
136  else
137  std::cout << "Failed to load unity altitude" << std::endl;
138 
139  #endif
140 
141  return returned;
142 }
143 
144 void Unity::setPlayerPosition(double x, double z)
145 {
146  if(this->positionFile.is_open())
147  {
148  // Rewinds it, so it will write to the beginning again
149  // TODO: how to come back to the beginning of the output file?
150  // this->positionFile.seekg(0);
151 
152  this->positionFile << x << " " << z;
153 
154  // Make sure to write new data
155  this->positionFile.flush();
156  }
157  else
158  std::cout << "Failed to write unity position" << std::endl;
159 }
160 
161 void Unity::setPlayerRotation(double x, double y, double z)
162 {
163  if(this->rotationFile.is_open())
164  {
165  // Rewinds it, so it will write to the beginning again
166  // TODO: how to come back to the beginning of the output file?
167  //this->rotationFile.seekg(0);
168 
169  this->rotationFile << x << " " << y << " " << z;
170 
171  // Make sure to write new data
172  this->rotationFile.flush();
173  }
174  else
175  std::cout << "Failed to write unity rotation" << std::endl;
176 }
177 
178 void Unity::setInfo(const char * info, int chars_written)
179 {
180  if(this->infoFile.is_open())
181  {
182  // Rewinds it, so it will write to the beginning again
183  // TODO: how to come back to the beginning of the output file?
184  //this->infoFile.seekg(0);
185 
186  this->infoFile << info;
187 
188  // Make sure to write new data
189  this->infoFile.flush();
190  }
191  else
192  std::cout << "Failed to write unity information" << std::endl;
193 }
194 
196 {
197  // TODO: somehow tell unity to keep rendering the frames
198 }
#define UNITY_ROTATION_FILE
Definition: Unity.h:10
void render()
Definition: Unity.cpp:195
void init()
Definition: Unity.cpp:74
#define NULL
void initBuild()
Definition: Unity.cpp:45
#define MOCK_SIZE
Definition: Unity.cpp:12
#define UNITY_POSITION_FILE
Definition: Unity.h:11
Unity()
Definition: Unity.cpp:16
void setPlayerRotation(double x, double y, double z)
Definition: Unity.cpp:161
#define CURRENT_UNITY_BUILD
Definition: Unity.h:6
#define UNITY_ALTITUDE_FILE
Definition: Unity.h:9
void setInfo(const char *info, int chars_written)
Definition: Unity.cpp:178
~Unity()
Definition: Unity.cpp:22
#define UNITY_INFO_FILE
Definition: Unity.h:8
void setPlayerPosition(double x, double z)
Definition: Unity.cpp:144
static double mockData[MOCK_SIZE]
Definition: Unity.cpp:14
int getPlayerAltitude()
Definition: Unity.cpp:92