the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "ShutdownManager.h"
3#include "..\..\Common\Leaderboards\LeaderboardManager.h"
4#include "..\..\MinecraftServer.h"
5#ifdef __PS3__
6#include "C4JSpursJob.h"
7
8
9bool ShutdownManager::s_threadShouldRun[ShutdownManager::eThreadIdCount];
10int ShutdownManager::s_threadRunning[ShutdownManager::eThreadIdCount];
11CRITICAL_SECTION ShutdownManager::s_threadRunningCS;
12C4JThread::EventArray *ShutdownManager::s_eventArray[eThreadIdCount];
13#endif
14
15// Initialises the shutdown manager - this needs to be called as soon as the game is started so it can respond as quickly as possible to shut down requests
16void ShutdownManager::Initialise()
17{
18#ifdef __PS3__
19 cellSysutilRegisterCallback( 1, SysUtilCallback, NULL );
20 for( int i = 0; i < eThreadIdCount; i++ )
21 {
22 s_threadShouldRun[i] = true;
23 s_threadRunning[i] = 0;
24 s_eventArray[i] = NULL;
25 }
26 // Special case for storage manager, which we will manually set now to be considered as running - this will be unset by StorageManager.ExitRequest if required
27 s_threadRunning[eStorageManagerThreads] = true;
28 InitializeCriticalSection(&s_threadRunningCS);
29#endif
30}
31
32// Called in response to a system request to exit the game. This just requests that the main thread should stop, and then the main thread is responsible for calling MainThreadHandleShutdown which
33// starts the rest of the shut down process, then waits until it is complete.
34void ShutdownManager::StartShutdown()
35{
36#ifdef __PS3__
37 s_threadShouldRun[ eMainThread ] = false;
38#endif
39}
40
41// This should be called from the main thread after it has been requested to shut down (ShouldRun for main thread returns false), and before it returns control to the kernel. This is responsible for
42// signalling to all other threads to stop, and wait until their completion before returning.
43void ShutdownManager::MainThreadHandleShutdown()
44{
45#ifdef __PS3__
46 // Set flags for each thread which will be reset when they are complete
47 s_threadRunning[ eMainThread ] = false;
48
49 // Second wave of things we would like to shut down (after main)
50 LeaderboardManager::Instance()->CancelOperation();
51 RequestThreadToStop( eLeaderboardThread );
52 RequestThreadToStop( eCommerceThread );
53 RequestThreadToStop( ePostProcessThread );
54 RequestThreadToStop( eRunUpdateThread );
55 RequestThreadToStop( eRenderChunkUpdateThread );
56 RequestThreadToStop( eConnectionReadThreads );
57 RequestThreadToStop( eConnectionWriteThreads );
58 RequestThreadToStop( eEventQueueThreads );
59 app.DebugPrintf("Shutdown manager: waiting on first batch of threads requested to terminate...\n");
60 WaitForSignalledToComplete();
61 app.DebugPrintf("Shutdown manager: terminated.\n");
62
63 // Now shut down the server thread
64 MinecraftServer::HaltServer();
65 RequestThreadToStop( eServerThread );
66 app.DebugPrintf("Shutdown manager: waiting on server to terminate...\n");
67 WaitForSignalledToComplete();
68 app.DebugPrintf("Shutdown manager: terminated.\n");
69
70 //And shut down the storage manager
71 RequestThreadToStop( eStorageManagerThreads );
72 StorageManager.ExitRequest(&StorageManagerCompleteFn);
73 app.DebugPrintf("Shutdown manager: waiting on storage manager to terminate...\n");
74 WaitForSignalledToComplete();
75 app.DebugPrintf("Shutdown manager: terminated.\n");
76
77 // Audio system shutdown
78 app.DebugPrintf("Shutdown manager: Audio shutdown.\n");
79 AIL_shutdown();
80
81 // Trophy system shutdown
82 app.DebugPrintf("Shutdown manager: Trophy system shutdown.\n");
83 ProfileManager.Terminate();
84
85 // Network manager shutdown
86 app.DebugPrintf("Shutdown manager: Network manager shutdown.\n");
87 g_NetworkManager.Terminate();
88
89 // Finally shut down the spurs job queue - leaving until last so there should be nothing else dependent on this still running
90 app.DebugPrintf("Shutdown manager: SPURS shutdown.\n");
91 C4JSpursJobQueue::getMainJobQueue().shutdown();
92 app.DebugPrintf("Shutdown manager: Complete.\n");
93#endif
94}
95
96void ShutdownManager::HasStarted(ShutdownManager::EThreadId threadId)
97{
98#ifdef __PS3__
99 EnterCriticalSection(&s_threadRunningCS);
100 s_threadRunning[threadId]++;
101 LeaveCriticalSection(&s_threadRunningCS);
102#endif
103}
104
105void ShutdownManager::HasStarted(ShutdownManager::EThreadId threadId, C4JThread::EventArray *eventArray)
106{
107#ifdef __PS3__
108 EnterCriticalSection(&s_threadRunningCS);
109 s_threadRunning[threadId]++;
110 LeaveCriticalSection(&s_threadRunningCS);
111 s_eventArray[threadId] = eventArray;
112#endif
113}
114
115bool ShutdownManager::ShouldRun(ShutdownManager::EThreadId threadId)
116{
117#ifdef __PS3__
118 return s_threadShouldRun[threadId];
119#else
120 return true;
121#endif
122}
123
124void ShutdownManager::HasFinished(ShutdownManager::EThreadId threadId)
125{
126#ifdef __PS3__
127 EnterCriticalSection(&s_threadRunningCS);
128 s_threadRunning[threadId]--;
129 LeaveCriticalSection(&s_threadRunningCS);
130#endif
131}
132
133#ifdef __PS3__
134void ShutdownManager::SysUtilCallback(uint64_t status, uint64_t param, void *userdata)
135{
136 Minecraft *minecraft = Minecraft::GetInstance();
137 switch(status)
138 {
139 case CELL_SYSUTIL_REQUEST_EXITGAME:
140 app.DebugPrintf("CELL_SYSUTIL_REQUEST_EXITGAME\n");
141 StartShutdown();
142 break;
143 case CELL_SYSUTIL_SYSTEM_MENU_OPEN:
144 // Tell the game UI to stop processing
145 StorageManager.SetSystemUIDisplaying(true);
146 break;
147 case CELL_SYSUTIL_DRAWING_END:
148 StorageManager.SetSystemUIDisplaying(false);
149 break;
150 case CELL_SYSUTIL_DRAWING_BEGIN:
151 case CELL_SYSUTIL_SYSTEM_MENU_CLOSE:
152 break;
153 case CELL_SYSUTIL_BGMPLAYBACK_PLAY:
154 if( minecraft )
155 {
156 minecraft->soundEngine->updateSystemMusicPlaying(true);
157 }
158 app.DebugPrintf("BGM playing\n");
159 break;
160 case CELL_SYSUTIL_BGMPLAYBACK_STOP:
161 if( minecraft )
162 {
163 minecraft->soundEngine->updateSystemMusicPlaying(false);
164 }
165 app.DebugPrintf("BGM stopped\n");
166 break;
167 }
168}
169#endif
170
171#ifdef __PS3__
172void ShutdownManager::WaitForSignalledToComplete()
173{
174 bool allComplete;
175 do
176 {
177 cellSysutilCheckCallback();
178 Sleep(10);
179 allComplete = true;
180 for( int i = 0; i < eThreadIdCount; i++ )
181 {
182 if( !s_threadShouldRun[i] )
183 {
184 if( s_threadRunning[i] != 0 ) allComplete = false;
185 }
186 }
187 } while( !allComplete);
188
189}
190
191void ShutdownManager::RequestThreadToStop(int i)
192{
193 s_threadShouldRun[i] = false;
194 if( s_eventArray[i] )
195 {
196 s_eventArray[i]->Cancel();
197 }
198}
199
200void ShutdownManager::StorageManagerCompleteFn()
201{
202 HasFinished(eStorageManagerThreads);
203}
204#endif