Sfoglia il codice sorgente

Merge pull request #114 from Azaezel/alpha40DBCache

Alpha40 db cache
Areloch 5 anni fa
parent
commit
3af902c911

+ 25 - 14
Engine/source/T3D/fx/lightning.cpp

@@ -240,9 +240,15 @@ LightningData::LightningData()
 {
 {
    strikeSound = NULL;
    strikeSound = NULL;
 
 
-   dMemset( strikeTextureNames, 0, sizeof( strikeTextureNames ) );
-   dMemset( strikeTextures, 0, sizeof( strikeTextures ) );
-   dMemset( thunderSounds, 0, sizeof( thunderSounds ) );
+   for (S32 i = 0; i < MaxThunders; i++)
+      thunderSounds[i] = NULL;
+
+   for (S32 i = 0; i < MaxTextures; i++)
+   {
+      strikeTextureNames[i] = NULL;
+      strikeTextures[i] = NULL;
+   }
+
    mNumStrikeTextures = 0;
    mNumStrikeTextures = 0;
 }
 }
 
 
@@ -282,9 +288,10 @@ bool LightningData::preload(bool server, String &errorStr)
    if (Parent::preload(server, errorStr) == false)
    if (Parent::preload(server, errorStr) == false)
       return false;
       return false;
 
 
-   dQsort(thunderSounds, MaxThunders, sizeof(SFXTrack*), cmpSounds);
-   for (numThunders = 0; numThunders < MaxThunders && thunderSounds[numThunders] != NULL; numThunders++) {
-      //
+   //dQsort(thunderSounds, MaxThunders, sizeof(SFXTrack*), cmpSounds);
+
+   for (S32 i = 0; i < MaxThunders; i++) {
+	   if (thunderSounds[i]!= NULL) numThunders++;
    }
    }
 
 
    if (server == false) 
    if (server == false) 
@@ -321,14 +328,15 @@ void LightningData::packData(BitStream* stream)
 
 
    U32 i;
    U32 i;
    for (i = 0; i < MaxThunders; i++)
    for (i = 0; i < MaxThunders; i++)
-      sfxWrite( stream, thunderSounds[ i ] );
+   {
+      if (stream->writeFlag(thunderSounds[i]))
+         sfxWrite(stream, thunderSounds[i]);
+   }
 
 
    stream->writeInt(mNumStrikeTextures, 4);
    stream->writeInt(mNumStrikeTextures, 4);
 
 
-   for (i = 0; i < MaxTextures; i++) 
-   {
+   for (i = 0; i < MaxTextures; i++)
       stream->writeString(strikeTextureNames[i]);
       stream->writeString(strikeTextureNames[i]);
-   }
 
 
    sfxWrite( stream, strikeSound );
    sfxWrite( stream, strikeSound );
 }
 }
@@ -339,14 +347,17 @@ void LightningData::unpackData(BitStream* stream)
 
 
    U32 i;
    U32 i;
    for (i = 0; i < MaxThunders; i++)
    for (i = 0; i < MaxThunders; i++)
-      sfxRead( stream, &thunderSounds[ i ] );
+   {
+      if (stream->readFlag())
+         sfxRead(stream, &thunderSounds[i]);
+      else
+         thunderSounds[i] = NULL;
+   }
 
 
    mNumStrikeTextures = stream->readInt(4);
    mNumStrikeTextures = stream->readInt(4);
 
 
-   for (i = 0; i < MaxTextures; i++) 
-   {
+   for (i = 0; i < MaxTextures; i++)
       strikeTextureNames[i] = stream->readSTString();
       strikeTextureNames[i] = stream->readSTString();
-   }
 
 
    sfxRead( stream, &strikeSound );
    sfxRead( stream, &strikeSound );
 }
 }

+ 118 - 2
Templates/BaseGame/game/core/clientServer/scripts/client/levelDownload.cs

@@ -38,7 +38,73 @@
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 // Phase 1 
 // Phase 1 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
-function clientCmdMissionStartPhase1(%seq, %missionName)
+$pref::Client::EnableDatablockCache = true;
+$pref::Client::DatablockCacheFilename = "data/cache/client/datablock_cache_c.dbc";
+
+function clientCmdMissionStartPhase1_LoadCache(%seq, %missionName)
+{
+  if ($pref::Client::EnableDatablockCache && $loadFromDatablockCache)
+  {
+    if (!$pref::Video::disableVerticalSync)
+    {
+      warn("Disabling Vertical Sync during datablock cache load to avoid significant slowdown.");
+      $AFX_tempDisableVSync = true;
+
+      $pref::Video::disableVerticalSync = true;
+      Canvas.resetVideoMode();
+    }
+
+    echo("<<<< Loading Datablocks From Cache >>>>");
+    if (ServerConnection.loadDatablockCache_Begin())
+    {
+      schedule(10, 0, "updateLoadDatablockCacheProgress", %seq, %missionName);
+    }
+  }
+}
+
+function updateLoadDatablockCacheProgress(%seq, %missionName)
+{
+   if (ServerConnection.loadDatablockCache_Continue())
+   {
+      $loadDatablockCacheProgressThread = schedule(10, 0, "updateLoadDatablockCacheProgress", %seq, %missionName);
+      return;
+   }
+ 
+   if ($AFX_tempDisableVSync)
+   {
+     warn("Restoring Vertical Sync setting.");
+     $AFX_tempDisableVSync = false;
+
+     $pref::Video::disableVerticalSync = false;
+     Canvas.resetVideoMode();
+   }
+
+   echo("<<<< Finished Loading Datablocks From Cache >>>>");
+   clientCmdMissionStartPhase2(%seq,%missionName);
+}
+
+function updateLoadDatablockCacheProgress(%seq, %missionName)
+{
+   if (ServerConnection.loadDatablockCache_Continue())
+   {
+      $loadDatablockCacheProgressThread = schedule(10, 0, "updateLoadDatablockCacheProgress", %seq, %missionName);
+      return;
+   }
+ 
+   if ($AFX_tempDisableVSync)
+   {
+     warn("Restoring Vertical Sync setting.");
+     $AFX_tempDisableVSync = false;
+
+     $pref::Video::disableVerticalSync = false;
+     Canvas.resetVideoMode();
+   }
+
+   echo("<<<< Finished Loading Datablocks From Cache >>>>");
+   clientCmdMissionStartPhase2(%seq,%missionName);
+}
+
+function clientCmdMissionStartPhase1(%seq, %missionName, %cache_crc)
 {
 {
    // These need to come after the cls.
    // These need to come after the cls.
    echo ("*** New Mission: " @ %missionName);
    echo ("*** New Mission: " @ %missionName);
@@ -61,6 +127,56 @@ function clientCmdMissionStartPhase1(%seq, %missionName)
       PostFXManager::settingsApplyDefaultPreset();
       PostFXManager::settingsApplyDefaultPreset();
    }
    }
    
    
+  $loadFromDatablockCache = false;
+  if ($pref::Client::EnableDatablockCache)
+  {
+    %cache_filename = $pref::Client::DatablockCacheFilename;
+
+    // if cache CRC is provided, check for validity
+    if (%cache_crc !$= "")
+    {
+      // check for existence of cache file
+      if (isFile(%cache_filename))
+      { 
+        // here we are not comparing the CRC of the cache itself, but the CRC of
+        // the server cache (stored in the header) when these datablocks were
+        // transmitted.
+        %my_cache_crc = extractDatablockCacheCRC(%cache_filename);
+        echo("<<<< client cache CRC:" SPC %my_cache_crc SPC ">>>>");
+        echo("<<<< comparing CRC codes:" SPC "s:" @ %cache_crc SPC "c:" @ %my_cache_crc SPC ">>>>");
+        if (%my_cache_crc == %cache_crc)
+        {
+          echo("<<<< cache CRC codes match, datablocks will be loaded from local cache. >>>>");
+          $loadFromDatablockCache = true;
+        }
+        else
+        {
+          echo("<<<< cache CRC codes differ, datablocks will be transmitted and cached. >>>>" SPC %cache_crc);
+          setDatablockCacheCRC(%cache_crc);
+        }
+      }
+      else
+      {
+        echo("<<<< client datablock cache does not exist, datablocks will be transmitted and cached. >>>>");
+        setDatablockCacheCRC(%cache_crc);
+      }
+    }
+    else
+    {
+      echo("<<<< server datablock caching is disabled, datablocks will be transmitted. >>>>");
+    }
+    if ($loadFromDatablockCache)
+    {
+      // skip datablock transmission and initiate a cache load
+      commandToServer('MissionStartPhase1Ack_UseCache', %seq);
+      return;
+    }
+  }
+  else if (%cache_crc !$= "")
+  {
+    echo("<<<< client datablock caching is disabled, datablocks will be transmitted. >>>>");
+  }
+  
    onMissionDownloadPhase("LOADING DATABLOCKS");
    onMissionDownloadPhase("LOADING DATABLOCKS");
    
    
    commandToServer('MissionStartPhase1Ack', %seq);
    commandToServer('MissionStartPhase1Ack', %seq);
@@ -164,7 +280,7 @@ function connect(%server)
 {
 {
    %conn = new GameConnection(ServerConnection);
    %conn = new GameConnection(ServerConnection);
    RootGroup.add(ServerConnection);
    RootGroup.add(ServerConnection);
-   %conn.setConnectArgs($pref::Player::Name);
+   %conn.setConnectArgs($pref::Player::Name, $ConncetInfoKey);
    %conn.setJoinPassword($Client::Password);
    %conn.setJoinPassword($Client::Password);
    %conn.connect(%server);
    %conn.connect(%server);
 }
 }

+ 0 - 2
Templates/BaseGame/game/core/clientServer/scripts/server/defaults.cs

@@ -46,8 +46,6 @@ $Pref::Server::ConnectionError =
 // overrides pref::net::port for dedicated servers
 // overrides pref::net::port for dedicated servers
 $Pref::Server::Port = 28000;
 $Pref::Server::Port = 28000;
 
 
-$Pref::Server::EnableDatablockCache = true;
-$Pref::Server::DatablockCacheFilename = "core/clientServer/scripts/server/afx/cache/afx_datablock_cache.dbc";
 
 
 // If the password is set, clients must provide it in order
 // If the password is set, clients must provide it in order
 // to connect to the server
 // to connect to the server

+ 48 - 9
Templates/BaseGame/game/core/clientServer/scripts/server/levelDownload.cs

@@ -38,8 +38,26 @@
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
 // Phase 1 
 // Phase 1 
 //----------------------------------------------------------------------------
 //----------------------------------------------------------------------------
+$Pref::Server::EnableDatablockCache = true;
+$pref::Server::DatablockCacheFilename = "data/cache/server/datablock_cache_c.dbc";
 function GameConnection::loadMission(%this)
 function GameConnection::loadMission(%this)
 {
 {
+  %cache_crc = "";
+
+  if ($Pref::Server::EnableDatablockCache)
+  {
+    if (!isDatablockCacheSaved())
+    {
+      echo("<<<< saving server datablock cache >>>>");
+      %this.saveDatablockCache();
+    }
+
+    if (isFile($Pref::Server::DatablockCacheFilename))
+    {
+      %cache_crc = getDatablockCacheCRC();
+      echo("    <<<< sending CRC to client:" SPC %cache_crc SPC ">>>>");
+    }
+  }
    // Send over the information that will display the server info
    // Send over the information that will display the server info
    // when we learn it got there, we'll send the data blocks
    // when we learn it got there, we'll send the data blocks
    %this.currentPhase = 0;
    %this.currentPhase = 0;
@@ -50,12 +68,41 @@ function GameConnection::loadMission(%this)
    }
    }
    else
    else
    {
    {
-      commandToClient(%this, 'MissionStartPhase1', $missionSequence, $Server::MissionFile);
+      commandToClient(%this, 'MissionStartPhase1', $missionSequence, $Server::MissionFile, %cache_crc);
          
          
       echo("*** Sending mission load to client: " @ $Server::MissionFile);
       echo("*** Sending mission load to client: " @ $Server::MissionFile);
    }
    }
 }
 }
 
 
+function serverCmdMissionStartPhase1Ack_UseCache(%client, %seq)
+{
+  echo("<<<< client will load datablocks from a cache >>>>");
+  echo("    <<<< skipping datablock transmission >>>>");
+
+  // Make sure to ignore calls from a previous mission load
+  if (%seq != $missionSequence || !$MissionRunning)
+    return;
+  if (%client.currentPhase != 0)
+    return;
+  %client.currentPhase = 1;
+
+  // Start with the CRC
+  %client.setMissionCRC( $missionCRC );
+
+  %client.onBeginDatablockCacheLoad($missionSequence);
+}
+
+function GameConnection::onBeginDatablockCacheLoad( %this, %missionSequence )
+{
+   // Make sure to ignore calls from a previous mission load
+   if (%missionSequence != $missionSequence)
+      return;
+   if (%this.currentPhase != 1)
+      return;
+   %this.currentPhase = 1.5;
+   commandToClient(%this, 'MissionStartPhase1_LoadCache', $missionSequence, $Server::MissionFile);
+}
+
 function serverCmdMissionStartPhase1Ack(%client, %seq)
 function serverCmdMissionStartPhase1Ack(%client, %seq)
 {
 {
    // Make sure to ignore calls from a previous mission load
    // Make sure to ignore calls from a previous mission load
@@ -157,14 +204,6 @@ function serverCmdMissionStartPhase3Ack(%client, %seq)
       // Set the control object to the default camera
       // Set the control object to the default camera
       if (!isObject(%client.camera))
       if (!isObject(%client.camera))
       {
       {
-         if(!isObject(Observer))
-         {
-            datablock CameraData(Observer)
-            {
-               mode = "Observer";
-            };  
-         }
-         
          //if (isDefined("$Game::DefaultCameraClass"))
          //if (isDefined("$Game::DefaultCameraClass"))
             %client.camera = spawnObject("Camera", Observer);
             %client.camera = spawnObject("Camera", Observer);
       }
       }

+ 6 - 2
Templates/BaseGame/game/core/clientServer/scripts/server/server.cs

@@ -204,10 +204,11 @@ function onServerCreated()
    
    
    loadDatablockFiles( DatablockFilesList, true );
    loadDatablockFiles( DatablockFilesList, true );
    
    
+   callOnModules("onServerScriptExec", "Core");
+   callOnModules("onServerScriptExec", "Game");   
+   
    // Keep track of when the game started
    // Keep track of when the game started
    $Game::StartTime = $Sim::Time;
    $Game::StartTime = $Sim::Time;
-
-   onServerCreatedAFX();
 }
 }
 
 
 /// Shut down the server
 /// Shut down the server
@@ -283,6 +284,9 @@ function onServerDestroyed()
    MissionCleanup.delete();
    MissionCleanup.delete();
    
    
    clearServerPaths();
    clearServerPaths();
+   
+  if ($Pref::Server::EnableDatablockCache)
+    resetDatablockCache();
 }
 }
 
 
 /// Guid list maintenance functions
 /// Guid list maintenance functions