the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#ifdef __PS3__
3#include <sys/sys_time.h>
4#endif
5#include "System.h"
6
7template <class T> void System::arraycopy(arrayWithLength<T> src, unsigned int srcPos, arrayWithLength<T> *dst, unsigned int dstPos, unsigned int length)
8{
9 assert( srcPos >=0 && srcPos <= src.length);
10 assert( srcPos + length <= src.length );
11 assert( dstPos + length <= dst->length );
12
13 std::copy( src.data + srcPos, src.data + srcPos + length, dst->data + dstPos );
14}
15
16ArrayCopyFunctionDefinition(Node *)
17ArrayCopyFunctionDefinition(Biome *)
18
19void System::arraycopy(arrayWithLength<BYTE> src, unsigned int srcPos, arrayWithLength<BYTE> *dst, unsigned int dstPos, unsigned int length)
20{
21 assert( srcPos >=0 && srcPos <= src.length);
22 assert( srcPos + length <= src.length );
23 assert( dstPos + length <= dst->length );
24
25 memcpy( dst->data + dstPos, src.data + srcPos, length);
26}
27
28void System::arraycopy(arrayWithLength<int> src, unsigned int srcPos, arrayWithLength<int> *dst, unsigned int dstPos, unsigned int length)
29{
30 assert( srcPos >=0 && srcPos <= src.length);
31 assert( srcPos + length <= src.length );
32 assert( dstPos + length <= dst->length );
33
34 memcpy( dst->data + dstPos, src.data + srcPos, length * sizeof(int) );
35}
36
37// TODO 4J Stu - These time functions may suffer from accuracy and we might have to use a high-resolution timer
38//Returns the current value of the most precise available system timer, in nanoseconds.
39//This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time.
40//The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative).
41//This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how
42//frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds)
43//will not accurately compute elapsed time due to numerical overflow.
44//
45//For example, to measure how long some code takes to execute:
46//
47// long startTime = System.nanoTime();
48// // ... the code being measured ...
49// long estimatedTime = System.nanoTime() - startTime;
50//
51//Returns:
52//The current value of the system timer, in nanoseconds.
53__int64 System::nanoTime()
54{
55#if defined _WINDOWS64 || defined _XBOX || defined _WIN32
56 static LARGE_INTEGER s_frequency = { 0 };
57 if (s_frequency.QuadPart == 0)
58 {
59 QueryPerformanceFrequency(&s_frequency);
60 }
61
62 LARGE_INTEGER counter;
63 QueryPerformanceCounter(&counter);
64
65 // Using double to avoid 64-bit overflow during multiplication for long uptime
66 // Precision is sufficient for ~100 days of uptime.
67 return (__int64)((double)counter.QuadPart * 1000000000.0 / (double)s_frequency.QuadPart);
68#else
69 return GetTickCount() * 1000000LL;
70#endif
71}
72
73//Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond,
74//the granularity of the value depends on the underlying operating system and may be larger. For example,
75//many operating systems measure time in units of tens of milliseconds.
76//See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time"
77//and coordinated universal time (UTC).
78//
79//Returns:
80//the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
81__int64 System::currentTimeMillis()
82{
83#ifdef __PS3__
84// sys_time_get_current_time() obtains the elapsed time since Epoch (1970/01/01 00:00:00 UTC).
85// The value is separated into two parts: sec stores the elapsed time in seconds, and nsec
86// stores the value that is smaller than a second in nanoseconds.
87 sys_time_sec_t sec;
88 sys_time_nsec_t nsec;
89 sys_time_get_current_time(&sec, &nsec);
90 __int64 msec = (sec * 1000) + (nsec / (1000*1000));
91 return msec;
92
93#elif defined __ORBIS__
94 SceRtcTick tick;
95 int err = sceRtcGetCurrentTick(&tick);
96
97 return (int64_t)(tick.tick / 1000);
98#elif defined __PSVITA__
99 // AP - TRC states we can't use the RTC for measuring elapsed game time
100 return sceKernelGetProcessTimeWide() / 1000;
101/* SceDateTime Time;
102 sceRtcGetCurrentClockLocalTime(&Time);
103 __int64 systTime = (((((((Time.day * 24) + Time.hour) * 60) + Time.minute) * 60) + Time.second) * 1000) + (Time.microsecond / 1000);
104 return systTime;*/
105
106#else
107
108 SYSTEMTIME UTCSysTime;
109 GetSystemTime( &UTCSysTime );
110
111 //Represents as a 64-bit value the number of 100-nanosecond intervals since January 1, 1601
112 FILETIME UTCFileTime;
113 SystemTimeToFileTime( &UTCSysTime, &UTCFileTime );
114
115 LARGE_INTEGER li;
116 li.HighPart = UTCFileTime.dwHighDateTime;
117 li.LowPart = UTCFileTime.dwLowDateTime;
118
119 return li.QuadPart/10000;
120#endif // __PS3__
121}
122
123// 4J Stu - Added this so that we can use real-world timestamps in PSVita saves. Particularly required for the save transfers to be smooth
124__int64 System::currentRealTimeMillis()
125{
126#ifdef __PSVITA__
127 SceFiosDate fileTime = sceFiosDateGetCurrent();
128
129 return fileTime/1000000;
130#else
131 return currentTimeMillis();
132#endif
133}
134
135
136void System::ReverseUSHORT(unsigned short *pusVal)
137{
138 unsigned short usValue=*pusVal;
139 unsigned char *pchVal1=(unsigned char *)pusVal;
140 unsigned char *pchVal2=(unsigned char *)&usValue;
141
142 pchVal1[0]=pchVal2[1];
143 pchVal1[1]=pchVal2[0];
144}
145
146void System::ReverseSHORT(short *pusVal)
147{
148 short usValue=*pusVal;
149 unsigned char *pchVal1=(unsigned char *)pusVal;
150 unsigned char *pchVal2=(unsigned char *)&usValue;
151
152 pchVal1[0]=pchVal2[1];
153 pchVal1[1]=pchVal2[0];
154}
155
156void System::ReverseULONG(unsigned long *pulVal)
157{
158 unsigned long ulValue=*pulVal;
159 unsigned char *pchVal1=(unsigned char *)pulVal;
160 unsigned char *pchVal2=(unsigned char *)&ulValue;
161
162 pchVal1[0]=pchVal2[3];
163 pchVal1[1]=pchVal2[2];
164 pchVal1[2]=pchVal2[1];
165 pchVal1[3]=pchVal2[0];
166}
167
168void System::ReverseULONG(unsigned int *pulVal)
169{
170 unsigned int ulValue=*pulVal;
171 unsigned char *pchVal1=(unsigned char *)pulVal;
172 unsigned char *pchVal2=(unsigned char *)&ulValue;
173
174 pchVal1[0]=pchVal2[3];
175 pchVal1[1]=pchVal2[2];
176 pchVal1[2]=pchVal2[1];
177 pchVal1[3]=pchVal2[0];
178}
179
180void System::ReverseINT(int *piVal)
181{
182 int ulValue=*piVal;
183 unsigned char *pchVal1=(unsigned char *)piVal;
184 unsigned char *pchVal2=(unsigned char *)&ulValue;
185
186 pchVal1[0]=pchVal2[3];
187 pchVal1[1]=pchVal2[2];
188 pchVal1[2]=pchVal2[1];
189 pchVal1[3]=pchVal2[0];
190}
191
192void System::ReverseULONGLONG(__int64 *pullVal)
193{
194 __int64 ullValue=*pullVal;
195 unsigned char *pchVal1=(unsigned char *)pullVal;
196 unsigned char *pchVal2=(unsigned char *)&ullValue;
197
198 pchVal1[0]=pchVal2[7];
199 pchVal1[1]=pchVal2[6];
200 pchVal1[2]=pchVal2[5];
201 pchVal1[3]=pchVal2[4];
202 pchVal1[4]=pchVal2[3];
203 pchVal1[5]=pchVal2[2];
204 pchVal1[6]=pchVal2[1];
205 pchVal1[7]=pchVal2[0];
206}
207
208void System::ReverseWCHARA(WCHAR *pwch,int iLen)
209{
210 for(int i=0;i<iLen;i++)
211 {
212 ReverseUSHORT((unsigned short *)&pwch[i]);
213 }
214}