2021-04-15 08:49:56 +02:00
|
|
|
import { ConfigurationEvent, LegacyExternalInterface, Nitro, NitroCommunicationDemoEvent, NitroEvent, NitroLocalizationEvent, RoomEngineEvent, WebGL } from 'nitro-renderer';
|
|
|
|
import { useCallback, useState } from 'react';
|
2021-04-14 20:24:24 +02:00
|
|
|
import { useConfigurationEvent } from './hooks/events/core/configuration/configuration-event';
|
|
|
|
import { useLocalizationEvent } from './hooks/events/nitro/localization/localization-event';
|
|
|
|
import { dispatchMainEvent, useMainEvent } from './hooks/events/nitro/main-event';
|
|
|
|
import { useRoomEngineEvent } from './hooks/events/nitro/room/room-engine-event';
|
2021-05-05 00:47:36 +02:00
|
|
|
import { GetConfiguration } from './utils/GetConfiguration';
|
2021-04-14 20:24:24 +02:00
|
|
|
import { LoadingView } from './views/loading/LoadingView';
|
|
|
|
import { MainView } from './views/main/MainView';
|
|
|
|
|
|
|
|
export function App(): JSX.Element
|
|
|
|
{
|
|
|
|
const [ isReady, setIsReady ] = useState(false);
|
|
|
|
const [ isError, setIsError ] = useState(false);
|
|
|
|
const [ message, setMessage ] = useState('Getting Ready');
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
if(!NitroConfig) throw new Error('NitroConfig is not defined!');
|
|
|
|
|
|
|
|
if(!Nitro.instance) Nitro.bootstrap();
|
|
|
|
|
|
|
|
function getPreloadAssetUrls(): string[]
|
|
|
|
{
|
|
|
|
const urls: string[] = [];
|
|
|
|
|
2021-05-05 00:47:36 +02:00
|
|
|
const assetUrls = GetConfiguration<string[]>('preload.assets.urls');
|
2021-04-14 20:24:24 +02:00
|
|
|
|
|
|
|
if(assetUrls && assetUrls.length)
|
|
|
|
{
|
|
|
|
for(const url of assetUrls)
|
|
|
|
{
|
|
|
|
urls.push(Nitro.instance.core.configuration.interpolate(url));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
2021-04-15 08:49:56 +02:00
|
|
|
const handler = useCallback((event: NitroEvent) =>
|
2021-04-14 20:24:24 +02:00
|
|
|
{
|
|
|
|
switch(event.type)
|
|
|
|
{
|
|
|
|
case ConfigurationEvent.LOADED:
|
|
|
|
Nitro.instance.localization.init();
|
|
|
|
return;
|
|
|
|
case ConfigurationEvent.FAILED:
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('Configuration Failed');
|
|
|
|
return;
|
|
|
|
case Nitro.WEBGL_UNAVAILABLE:
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('WebGL Required');
|
|
|
|
return;
|
|
|
|
case Nitro.WEBGL_CONTEXT_LOST:
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('WebGL Context Lost - Reloading');
|
2021-04-14 04:42:50 +02:00
|
|
|
|
2021-04-14 20:24:24 +02:00
|
|
|
setTimeout(() => window.location.reload(), 1500);
|
|
|
|
return;
|
|
|
|
case NitroCommunicationDemoEvent.CONNECTION_HANDSHAKING:
|
|
|
|
return;
|
|
|
|
case NitroCommunicationDemoEvent.CONNECTION_HANDSHAKE_FAILED:
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('Handshake Failed');
|
|
|
|
return;
|
|
|
|
case NitroCommunicationDemoEvent.CONNECTION_AUTHENTICATED:
|
|
|
|
setMessage('Finishing Up');
|
|
|
|
|
|
|
|
Nitro.instance.init();
|
|
|
|
return;
|
|
|
|
case NitroCommunicationDemoEvent.CONNECTION_ERROR:
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('Connection Error');
|
|
|
|
return;
|
|
|
|
case NitroCommunicationDemoEvent.CONNECTION_CLOSED:
|
|
|
|
if(Nitro.instance.roomEngine) Nitro.instance.roomEngine.dispose();
|
|
|
|
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('Connection Error');
|
|
|
|
|
|
|
|
LegacyExternalInterface.call('disconnect', -1, 'client.init.handshake.fail');
|
|
|
|
return;
|
|
|
|
case RoomEngineEvent.ENGINE_INITIALIZED:
|
|
|
|
setIsReady(true);
|
|
|
|
return;
|
|
|
|
case NitroLocalizationEvent.LOADED:
|
|
|
|
Nitro.instance.core.asset.downloadAssets(getPreloadAssetUrls(), (status: boolean) =>
|
|
|
|
{
|
|
|
|
if(status)
|
|
|
|
{
|
|
|
|
setMessage('Connecting');
|
|
|
|
|
|
|
|
Nitro.instance.communication.init();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setIsError(true);
|
|
|
|
setMessage('Assets Failed');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2021-04-15 08:49:56 +02:00
|
|
|
}, []);
|
2021-04-14 20:24:24 +02:00
|
|
|
|
|
|
|
useMainEvent(Nitro.WEBGL_UNAVAILABLE, handler);
|
|
|
|
useMainEvent(Nitro.WEBGL_CONTEXT_LOST, handler);
|
|
|
|
useMainEvent(NitroCommunicationDemoEvent.CONNECTION_HANDSHAKING, handler);
|
|
|
|
useMainEvent(NitroCommunicationDemoEvent.CONNECTION_HANDSHAKE_FAILED, handler);
|
|
|
|
useMainEvent(NitroCommunicationDemoEvent.CONNECTION_AUTHENTICATED, handler);
|
|
|
|
useMainEvent(NitroCommunicationDemoEvent.CONNECTION_ERROR, handler);
|
|
|
|
useMainEvent(NitroCommunicationDemoEvent.CONNECTION_CLOSED, handler);
|
|
|
|
useRoomEngineEvent(RoomEngineEvent.ENGINE_INITIALIZED, handler);
|
|
|
|
useLocalizationEvent(NitroLocalizationEvent.LOADED, handler);
|
|
|
|
useConfigurationEvent(ConfigurationEvent.LOADED, handler);
|
|
|
|
useConfigurationEvent(ConfigurationEvent.FAILED, handler);
|
|
|
|
|
|
|
|
if(!WebGL.isWebGLAvailable())
|
|
|
|
{
|
|
|
|
dispatchMainEvent(new NitroEvent(Nitro.WEBGL_UNAVAILABLE));
|
|
|
|
}
|
2021-04-16 05:44:54 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Nitro.instance.core.configuration.init();
|
|
|
|
}
|
2021-04-14 20:24:24 +02:00
|
|
|
|
|
|
|
return (
|
2021-04-19 18:34:31 +02:00
|
|
|
<div className="nitro-app">
|
2021-04-14 20:24:24 +02:00
|
|
|
{ (!isReady || isError) && <LoadingView isError={ isError } message={ message } /> }
|
|
|
|
{ (isReady && !isError) && <MainView /> }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|