the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1//
2// ApplicationView.cpp -
3//
4
5#include "stdafx.h"
6#include "ApplicationView.h"
7#include "MinecraftServer.h"
8
9using namespace Windows::Foundation;
10using namespace Windows::ApplicationModel;
11using namespace Windows::ApplicationModel::Core;
12using namespace Windows::ApplicationModel::Activation;
13using namespace Windows::UI::Core;
14
15ApplicationView::ApplicationView()
16{
17 m_activationComplete = false;
18 m_windowClosed = false;
19 m_inviteProcessed = false;
20}
21
22XALLOC_ATTRIBUTES ExpandAllocAttributes( _In_ LONGLONG dwAttributes )
23{
24 XALLOC_ATTRIBUTES attr;
25 attr = *((XALLOC_ATTRIBUTES *)&dwAttributes);
26 return attr;
27}
28
29SIZE_T totalTracked = 0;
30unordered_map<uintptr_t, SIZE_T> tracker;
31static volatile bool memDump = false;
32static volatile bool memReset = false;
33static bool memDumpInit = false;
34static CRITICAL_SECTION memTrackCS;
35static volatile SIZE_T memSizeComp = 0;
36static long long lastTrackTime = 0;
37
38PVOID WINAPI XMemAllocLog( _In_ SIZE_T dwSize, _In_ ULONGLONG dwAttributes )
39{
40 if( !memDumpInit )
41 {
42 InitializeCriticalSection(&memTrackCS);
43 memDumpInit = true;
44 }
45
46 PVOID result = XMemAllocDefault(dwSize,dwAttributes);
47 XALLOC_ATTRIBUTES expanded_attributes = ExpandAllocAttributes(dwAttributes);
48 EnterCriticalSection(&memTrackCS);
49 tracker[(uintptr_t)result] = dwSize;
50 totalTracked += dwSize;
51 LeaveCriticalSection(&memTrackCS);
52// app.DebugPrintf("XMemAllocLog %p, Size: %d, AllocatorId: 0x%02x, ObjectType: 0x%04x, PageSize: %d, Alignment: %d, MemoryType: %d\n", result, dwSize, expanded_attributes.s.dwAllocatorId, expanded_attributes.s.dwObjectType, expanded_attributes.s.dwPageSize, expanded_attributes.s.dwAlignment, expanded_attributes.s.dwMemoryType );
53 return result;
54}
55
56VOID WINAPI XMemFreeLog( _In_ PVOID lpAddress, _In_ ULONGLONG dwAttributes )
57{
58 EnterCriticalSection(&memTrackCS);
59 auto it = tracker.find((uintptr_t)lpAddress);
60 if( it != tracker.end() )
61 {
62 totalTracked -= it->second;
63 tracker.erase(it);
64 }
65 LeaveCriticalSection(&memTrackCS);
66 XALLOC_ATTRIBUTES expanded_attributes = ExpandAllocAttributes(dwAttributes);
67// app.DebugPrintf("XMemFreeLog %p, AllocatorId: 0x%02x, ObjectType: 0x%04x, PageSize: %d, Alignment: %d, MemoryType: %d\n", lpAddress, expanded_attributes.s.dwAllocatorId, expanded_attributes.s.dwObjectType, expanded_attributes.s.dwPageSize, expanded_attributes.s.dwAlignment, expanded_attributes.s.dwMemoryType );
68 XMemFreeDefault(lpAddress,dwAttributes);
69
70 long long currentTime = System::currentTimeMillis();
71 if( ( currentTime - lastTrackTime ) > 1000 )
72 {
73 app.DebugPrintf("Total memory tracked: %d\n", totalTracked);
74 lastTrackTime = currentTime;
75 }
76
77 if( memReset )
78 {
79 tracker.clear();
80 memReset = false;
81 totalTracked = 0;
82 }
83
84 if( memDump )
85 {
86 for( auto it = tracker.begin(); it != tracker.end(); it++ )
87 {
88 app.DebugPrintf("0x%x : %d\n",it->first,it->second);
89 }
90 memDump = false;
91 }
92}
93
94// Called by the system. Perform application initialization here,
95// hooking application wide events, etc.
96void ApplicationView::Initialize(CoreApplicationView^ applicationView)
97{
98// XMemSetAllocationHooks( &XMemAllocLog, &XMemFreeLog );
99 applicationView->Activated += ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &ApplicationView::OnActivated);
100
101 CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs^>(this, &ApplicationView::OnSuspending);
102
103 CoreApplication::Resuming += ref new EventHandler<Platform::Object^>(this, &ApplicationView::OnResuming);
104
105 CoreApplication::ResourceAvailabilityChanged += ref new EventHandler< Platform::Object^ >( this, &ApplicationView::OnResourceAvailabilityChanged );
106}
107
108// Called when we are provided a window.
109void ApplicationView::SetWindow(CoreWindow^ window)
110{
111 window->Closed += ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &ApplicationView::OnWindowClosed);
112}
113
114// The purpose of this method is to get the application entry point.
115void ApplicationView::Load(Platform::String^ entryPoint)
116{
117}
118
119void InitializeDurango(Windows::UI::Core::CoreWindow^ window);
120void oldWinMainInit();
121void oldWinMainTick();
122
123// Called by the system after initialization is complete. This
124// implements the traditional game loop
125void ApplicationView::Run()
126{
127// m_game = ref new Game();
128 InitializeDurango(CoreWindow::GetForCurrentThread());
129
130 // Ensure we finish activation and let the system know we�re responsive
131 while (!m_activationComplete)
132 {
133 CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
134 }
135
136#ifndef _CONTENT_PACKAGE
137 char buf[256];
138 sprintf(buf,"0x%llx this\n",this);
139 OutputDebugStringA(buf);
140#endif
141 oldWinMainInit();
142
143
144 while (!app.getShutdownFlag() && !m_windowClosed)
145 {
146 CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
147
148 oldWinMainTick();
149 }
150
151 Windows::ApplicationModel::Core::CoreApplication::Exit();
152}
153
154void ApplicationView::Uninitialize()
155{
156}
157
158// Called when the application is activated. For now, there is just one activation
159// kind - Launch.
160void ApplicationView::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
161{
162 CoreWindow::GetForCurrentThread()->Activate();
163
164 Windows::Xbox::Multiplayer::PartyConfig::SuppressGameSessionReadyToast = true;
165 Windows::Xbox::Multiplayer::PartyConfig::SuppressGameSessionReadyToastMode = Windows::Xbox::Multiplayer::SuppressGameSessionReadyToastMode::Always;
166
167 // On starting up the game for the first time, we'll get one kind of onActivated message through. This will also
168 // be called at other times though, so don't rely on it only happening once for anything
169 if( !m_activationComplete )
170 {
171 if (args->Kind == Windows::ApplicationModel::Activation::ActivationKind::Launch)
172 {
173 // We get ActivationKind::Launch of we've just been started up from the user selecting us in the system UI, or if we are run normally from the debugger
174 ILaunchActivatedEventArgs^ newArgs = static_cast< ILaunchActivatedEventArgs^>(args);
175
176
177 }
178 else if (args->Kind == Windows::ApplicationModel::Activation::ActivationKind::Protocol)
179 {
180 // We get Windows::ApplicationModel::Activation::ActivationKind::Protocol if we have been started from accepting a party invite (including joining an open party ourselves), or from a game session being ready
181 IProtocolActivatedEventArgs^ proArgs = static_cast< IProtocolActivatedEventArgs^>(args);
182
183 Platform::String^ uriType = proArgs->Uri->Host;
184 if( uriType == L"partyInviteAccept")
185 {
186 if( !m_inviteProcessed )
187 {
188 DQRNetworkManager::SetPartyProcessJoinParty();
189 DQRNetworkManager::SetInviteReceivedFlag();
190 m_inviteProcessed = true;
191 }
192 }
193 else if( uriType == L"gameSessionReady" )
194 {
195 Platform::String^ uriCommand = ref new Platform::String(proArgs->Uri->RawUri->Data());
196 auto uri = ref new Windows::Foundation::Uri(uriCommand);
197 WwwFormUrlDecoder^ query = uri->QueryParsed;
198
199 DQRNetworkManager::SetPartyProcessJoinSession(0,query->GetFirstValueByName(L"sessionName"),query->GetFirstValueByName(L"serviceConfigurationId"),query->GetFirstValueByName(L"sessionTemplateName"));
200 }
201 }
202
203 m_activationComplete = true;
204 }
205 else
206 {
207 // This type of activation protocol can come in at any time during the game. Not sure of the best way to handle both this and gamesessionready events, so for now going to only process these
208 // (1) once
209 // (2) until the main menu is reached
210 // To try and minimise any conflict of them coming in in the middle of the game when we are already handling this gamesessionready.
211 if (args->Kind == Windows::ApplicationModel::Activation::ActivationKind::Protocol)
212 {
213 if( !app.HasReachedMainMenu() )
214 {
215 app.DebugPrintf("Accepting party invite after initial activation\n");
216 // We get Windows::ApplicationModel::Activation::ActivationKind::Protocol if we have been started from accepting a party invite (including joining an open party ourselves), or from a game session being ready
217 IProtocolActivatedEventArgs^ proArgs = static_cast< IProtocolActivatedEventArgs^>(args);
218
219 Platform::String^ uriType = proArgs->Uri->Host;
220 if( uriType == L"partyInviteAccept")
221 {
222 if( !m_inviteProcessed )
223 {
224 DQRNetworkManager::SetPartyProcessJoinParty();
225 DQRNetworkManager::SetInviteReceivedFlag();
226 m_inviteProcessed = true;
227 }
228 }
229 }
230 else
231 {
232 DQRNetworkManager::SetInviteReceivedFlag(); // This just informs the network manager that an invite has been received, not to start processing it
233 }
234 }
235 }
236}
237
238// Called when the application is suspending.
239void ApplicationView::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
240{
241 // TODO: Save game progress using the ConnectedStroage API.
242 app.DebugPrintf("###################################\n\n\n");
243 app.DebugPrintf("Suspending!");
244 app.DebugPrintf("\n\n\n###################################");
245
246 // Get the frequency of the timer
247 LARGE_INTEGER qwTicksPerSec, qwTime, qwNewTime, qwDeltaTime;
248 float fElapsedTime = 0.0f;
249 QueryPerformanceFrequency( &qwTicksPerSec );
250 float fSecsPerTick = 1.0f / (float)qwTicksPerSec.QuadPart;
251 // Save the start time
252 QueryPerformanceCounter( &qwTime );
253
254 // Request deferral
255 SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
256
257 MinecraftServer *server = MinecraftServer::getInstance();
258 if(server)
259 {
260 server->Suspend();
261 }
262
263 // Start suspend process for libraries
264 StorageManager.Suspend();
265 RenderManager.Suspend();
266
267 // Wait for libraries to finish suspending
268 while(!StorageManager.Suspended() || !RenderManager.Suspended())
269 {
270 Sleep(10);
271 }
272
273 deferral->Complete();
274
275 QueryPerformanceCounter( &qwNewTime );
276
277 qwDeltaTime.QuadPart = qwNewTime.QuadPart - qwTime.QuadPart;
278 fElapsedTime = fSecsPerTick * ((FLOAT)(qwDeltaTime.QuadPart));
279
280 app.DebugPrintf("Entire suspend process: Elapsed time %f\n", fElapsedTime);
281}
282
283// Called when the application is resuming from suspended.
284void ApplicationView::OnResuming(Platform::Object^ sender, Platform::Object^ args)
285{
286 app.DebugPrintf("###################################\n\n\n");
287 app.DebugPrintf("Resuming!");
288 app.DebugPrintf("\n\n\n###################################");
289
290 // Start resume process for libraries
291 RenderManager.Resume();
292 InputManager.Resume();
293
294 // Switch to offline if we were in an online game
295 if (!g_NetworkManager.IsLocalGame() && g_NetworkManager.IsInGameplay())
296 {
297 // Bit specific but we want to avoid switching to offline if the primary player has signed out
298 // (and AFAIK there's no other way to find out until a few ticks time)
299 if (app.GetXuiAction(0) != eAppAction_PrimaryPlayerSignedOut)
300 {
301 // We've lost the party so switch to offline, previously we exited the world here but this seems friendlier
302 app.SetAction(ProfileManager.GetPrimaryPad(), eAppAction_EthernetDisconnected);
303 }
304 }
305
306 // 4J-PB - clear all DLC info, and recheck, since someone may have suspended after starting a DLCinstall, and we won't have received any install/ license change event
307 app.ClearDLCInstalled();
308 ui.HandleDLCInstalled(ProfileManager.GetPrimaryPad());
309
310}
311
312void ApplicationView::OnResourceAvailabilityChanged( Platform::Object^ /*sender*/, Platform::Object^ /*args*/ )
313{
314 app.DebugPrintf("###################################\n\n\n");
315 if( Windows::ApplicationModel::Core::CoreApplication::ResourceAvailability == Windows::ApplicationModel::Core::ResourceAvailability::Full )
316 {
317 app.DebugPrintf( "Entered the 'Full Running' state." );
318 }
319 else
320 {
321 app.DebugPrintf( "Entered the 'Constrained' PLM state." );
322 }
323 app.DebugPrintf("\n\n\n###################################");
324}
325
326void ApplicationView::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
327{
328 m_windowClosed = true;
329}
330
331// Implements a IFrameworkView factory.
332IFrameworkView^ ApplicationViewSource::CreateView()
333{
334 return ref new ApplicationView();
335}
336
337// Application entry point
338[Platform::MTAThread]
339int main(Platform::Array<Platform::String^>^)
340{
341 auto applicationViewSource = ref new ApplicationViewSource();
342
343 CoreApplication::Run(applicationViewSource);
344
345 return 0;
346}