Bearsampp 2026.3.26
API documentation
Loading...
Searching...
No Matches
ActionLoading Class Reference

Public Member Functions

 __construct ($args)
 incrProgressBar ($nb=1)
 processLoading ($window, $id, $ctrl, $param1, $param2)

Data Fields

const GAUGE = 20
const WINDOW_HEIGHT = 90
const WINDOW_WIDTH = 360

Private Member Functions

 checkAllServicesStarted ()
 updateLabelFromStatusFile ()
 updateLoadingText ($text)

Private Attributes

 $wbLabel
 $wbProgressBar
 $wbWindow

Detailed Description

Class ActionLoading

This class handles the loading action, including the creation and management of a progress bar window.

Definition at line 16 of file class.action.loading.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( $args)

ActionLoading constructor.

Initializes the loading action, creates the progress bar window, and starts the main loop.

Parameters
array$argsThe arguments passed to the constructor.

Definition at line 43 of file class.action.loading.php.

44 {
45 global $bearsamppCore, $bearsamppLang, $bearsamppWinbinder;
46
47 $currentPid = Win32Ps::getCurrentPid();
48 Util::logTrace('ActionLoading constructor started - PID: ' . $currentPid);
49
50 $bearsamppWinbinder->reset();
51 Util::logTrace('WinBinder reset complete');
52
53 $bearsamppCore->addLoadingPid($currentPid);
54 Util::logTrace('Loading PID added to tracking file: ' . $currentPid);
55
56 // Screen information
57 Util::logTrace('Getting screen information');
58 $screenArea = explode(' ', $bearsamppWinbinder->getSystemInfo(WinBinder::SYSINFO_WORKAREA));
59 $screenWidth = intval($screenArea[2]);
60 $screenHeight = intval($screenArea[3]);
61 $xPos = $screenWidth - self::WINDOW_WIDTH;
62 $yPos = $screenHeight - self::WINDOW_HEIGHT - 5;
63 Util::logTrace('Screen dimensions: ' . $screenWidth . 'x' . $screenHeight . ', Window position: (' . $xPos . ',' . $yPos . ')');
64
65 // Create the window and progress bar
66 Util::logTrace('Creating loading window...');
67 $this->wbWindow = $bearsamppWinbinder->createWindow(null, ToolDialog, null, $xPos, $yPos, self::WINDOW_WIDTH, self::WINDOW_HEIGHT, WBC_TOP, null);
68
69 // Check if window was created successfully
70 if ($this->wbWindow === false || $this->wbWindow === null) {
71 Util::logError('CRITICAL: Failed to create loading window - window handle is: ' . var_export($this->wbWindow, true));
72 Util::logError('WinBinder extension loaded: ' . (extension_loaded('winbinder') ? 'YES' : 'NO'));
73 Util::logError('wb_create_window function exists: ' . (function_exists('wb_create_window') ? 'YES' : 'NO'));
74 return;
75 }
76
77 Util::logTrace('Loading window created successfully - handle: ' . $this->wbWindow);
78
79 // CRITICAL: wb_set_visible() must be called AFTER window creation in PHP 8.4
80 // The WS_VISIBLE flag during creation doesn't work
81 Util::logTrace('Making window visible with wb_set_visible()');
82 wb_set_visible($this->wbWindow, true);
83 Util::logTrace('Window set to visible');
84
85 Util::logTrace('Creating label control...');
86 $this->wbLabel = $bearsamppWinbinder->createLabel($this->wbWindow, $bearsamppLang->getValue(Lang::LOADING), 42, 2, 295, null, WBC_LEFT);
87 Util::logTrace('Label created: ' . var_export($this->wbLabel, true));
88
89 Util::logTrace('Creating progress bar...');
90 $this->wbProgressBar = $bearsamppWinbinder->createProgressBar($this->wbWindow, self::GAUGE, 42, 20, 290, 15);
91 Util::logTrace('Progress bar created: ' . var_export($this->wbProgressBar, true));
92
93 // Set the handler and start the main loop
94 Util::logTrace('Setting window handler...');
95 $bearsamppWinbinder->setHandler($this->wbWindow, $this, 'processLoading', 10);
96 Util::logTrace('Handler set, starting main loop...');
97 $bearsamppWinbinder->mainLoop();
98 Util::logTrace('Main loop exited');
99 }
global $bearsamppLang
global $bearsamppCore
const LOADING
static logError($data, $file=null)
static logTrace($data, $file=null)
static getCurrentPid()
const SYSINFO_WORKAREA

References $bearsamppCore, $bearsamppLang, Win32Ps\getCurrentPid(), Lang\LOADING, Util\logError(), Util\logTrace(), and WinBinder\SYSINFO_WORKAREA.

Member Function Documentation

◆ checkAllServicesStarted()

checkAllServicesStarted ( )
private

Checks if all services have started successfully

Returns
bool True if all services are running, false otherwise

Definition at line 228 of file class.action.loading.php.

229 {
231
232 Util::logTrace('Checking if all services have started successfully');
233
234 $allStarted = true;
235 foreach ($bearsamppBins->getServices() as $sName => $service) {
236 // Skip if service is not enabled
237 if (!$service->isEnable()) {
238 Util::logTrace('Service ' . $sName . ' is disabled, skipping check');
239 continue;
240 }
241
242 // Update the loading text to show which service we're checking
243 $serviceName = $service->getName();
244 $this->updateLoadingText('Checking ' . $serviceName . '...');
245
246 // Add timeout for service status check
247 $checkStartTime = microtime(true);
248 $checkTimeout = 5; // 5 seconds timeout
249 $serviceRunning = false;
250
251 try {
252 // Use a non-blocking check with timeout
253 $tempFile = $bearsamppCore->getTmpPath() . '/service_check_' . uniqid() . '.tmp';
254
255 // Start a background process to check the service
256 $checkCmd = 'php -r "' .
257 'require \'' . $bearsamppRoot->getLibsPath() . '/classes/class.win32service.php\'; ' .
258 '$service = new Win32Service(\'' . $service->getName() . '\'); ' .
259 '$status = $service->status(); ' .
260 'file_put_contents(\'' . $tempFile . '\', $status == Win32Service::STATE_RUNNING ? \'1\' : \'0\'); ' .
261 'exit(0);" > nul 2>&1';
262
263 // Execute the command in background
264 pclose(popen('start /B ' . $checkCmd, 'r'));
265
266 // Wait for the result with timeout
267 $startWait = microtime(true);
268 while (!file_exists($tempFile) && (microtime(true) - $startWait < $checkTimeout)) {
269 usleep(100000); // 100ms
270 }
271
272 // Check if we got a result
273 if (file_exists($tempFile)) {
274 $result = file_get_contents($tempFile);
275 $serviceRunning = ($result === '1');
276 unlink($tempFile);
277 Util::logTrace('Service ' . $sName . ' status check: ' . ($serviceRunning ? 'running' : 'not running'));
278 } else {
279 Util::logTrace('Service ' . $sName . ' status check timed out');
280 $serviceRunning = false;
281 }
282 } catch (\Exception $e) {
283 Util::logTrace('Exception during service status check for ' . $sName . ': ' . $e->getMessage());
284 $serviceRunning = false;
285 }
286
287 if (!$serviceRunning) {
288 Util::logTrace('Service ' . $sName . ' is not running');
289 $allStarted = false;
290 break;
291 }
292 }
293
294 Util::logTrace('All services started check result: ' . ($allStarted ? 'true' : 'false'));
295 return $allStarted;
296 }
$result
global $bearsamppBins
global $bearsamppRoot

References $bearsamppBins, $bearsamppCore, $bearsamppRoot, $result, Util\logTrace(), and updateLoadingText().

Referenced by processLoading().

◆ incrProgressBar()

incrProgressBar ( $nb = 1)

Increments the progress bar by a specified number of steps.

Parameters
int$nbThe number of steps to increment the progress bar by. Default is 1.

Definition at line 106 of file class.action.loading.php.

107 {
108 global $bearsamppCore, $bearsamppWinbinder;
109
110 for ($i = 0; $i < $nb; $i++) {
111 $bearsamppWinbinder->incrProgressBar($this->wbProgressBar);
112 $bearsamppWinbinder->drawImage($this->wbWindow, $bearsamppCore->getImagesPath() . '/bearsampp.bmp', 4, 2, 32, 32);
113 }
114
115 $bearsamppWinbinder->wait();
116 $bearsamppWinbinder->wait($this->wbWindow);
117 }

References $bearsamppCore.

Referenced by processLoading().

◆ processLoading()

processLoading ( $window,
$id,
$ctrl,
$param1,
$param2 )

Processes the loading action, including handling window events and updating the progress bar.

Parameters
mixed$windowThe window object.
int$idThe ID of the event.
mixed$ctrlThe control object.
mixed$param1The first parameter of the event.
mixed$param2The second parameter of the event.

Definition at line 128 of file class.action.loading.php.

129 {
130 global $bearsamppRoot, $bearsamppWinbinder;
131
132 switch ($id) {
133 case IDCLOSE:
135 break;
136 }
137
138 // Set a maximum number of iterations to prevent infinite loops
139 $maxIterations = 10;
140 $iterations = 0;
141
142 // Set a timeout for the entire loading process
143 $startTime = microtime(true); // Use microtime for more precise timing
144 $maxLoadingTime = 15; // 15 seconds maximum
145
146 while ($iterations < $maxIterations && (microtime(true) - $startTime) < $maxLoadingTime) {
147 $bearsamppRoot->removeErrorHandling();
148 $bearsamppWinbinder->resetProgressBar($this->wbProgressBar);
149
150 usleep(100000);
151
152 for ($i = 0; $i < self::GAUGE && (microtime(true) - $startTime) < $maxLoadingTime; $i++) {
153 $this->incrProgressBar();
154
155 // Check for status file updates to show current service being processed
157
158 usleep(100000);
159 }
160
161 // Check if all services have started successfully
162 $allServicesStarted = $this->checkAllServicesStarted();
163 if ($allServicesStarted) {
164 Util::logTrace('All services started successfully');
165 break;
166 }
167
168 $iterations++;
169 Util::logTrace('Loading iteration ' . $iterations . ' completed, checking services again');
170 }
171
172 if ($iterations >= $maxIterations) {
173 Util::logTrace('Maximum iterations reached (' . $maxIterations . '), some services may not have started properly');
174 }
175
176 if ((microtime(true) - $startTime) >= $maxLoadingTime) {
177 Util::logTrace('Loading timeout reached (' . $maxLoadingTime . ' seconds), some services may not have started properly');
178 }
179
180 // Close the loading window
181 Util::logTrace('Closing loading window');
183 }
static kill($pid)

References $bearsamppRoot, checkAllServicesStarted(), Win32Ps\getCurrentPid(), incrProgressBar(), Win32Ps\kill(), Util\logTrace(), and updateLabelFromStatusFile().

◆ updateLabelFromStatusFile()

updateLabelFromStatusFile ( )
private

Updates the label text from status file if it exists This allows external processes to update the loading screen text dynamically

Definition at line 204 of file class.action.loading.php.

205 {
206 global $bearsamppCore, $bearsamppWinbinder;
207
208 $statusFile = $bearsamppCore->getTmpPath() . '/loading_status.txt';
209
210 if (file_exists($statusFile)) {
211 $content = @file_get_contents($statusFile);
212 if ($content !== false && !empty($content)) {
213 $status = @json_decode($content, true);
214 if ($status && isset($status['text']) && !empty($status['text'])) {
215 // Update the label with new text
216 $bearsamppWinbinder->setText($this->wbLabel[WinBinder::CTRL_OBJ], $status['text']);
217 $bearsamppWinbinder->refresh($this->wbWindow);
218 }
219 }
220 }
221 }

References $bearsamppCore, and WinBinder\CTRL_OBJ.

Referenced by processLoading().

◆ updateLoadingText()

updateLoadingText ( $text)
private

Updates the loading text on the window

Parameters
string$textThe text to display

Definition at line 190 of file class.action.loading.php.

191 {
192 global $bearsamppWinbinder;
193
194 if ($this->wbLabel) {
195 wb_set_text($this->wbLabel, $text);
196 wb_refresh($this->wbWindow);
197 }
198 }

Referenced by checkAllServicesStarted().

Field Documentation

◆ $wbLabel

$wbLabel
private

Definition at line 31 of file class.action.loading.php.

◆ $wbProgressBar

$wbProgressBar
private

Definition at line 34 of file class.action.loading.php.

◆ $wbWindow

$wbWindow
private

Definition at line 28 of file class.action.loading.php.

◆ GAUGE

const GAUGE = 20

Definition at line 25 of file class.action.loading.php.

◆ WINDOW_HEIGHT

const WINDOW_HEIGHT = 90

Definition at line 22 of file class.action.loading.php.

◆ WINDOW_WIDTH

const WINDOW_WIDTH = 360

Definition at line 19 of file class.action.loading.php.


The documentation for this class was generated from the following file: