Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server-specific .roo file loading. #115

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Meridian59.Ogre.Client/OgreClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ namespace Meridian59 { namespace Ogre
ResourceManager->Preload(
Config->PreloadObjects,
Config->PreloadRoomTextures,
Config->PreloadRooms,
Config->PreloadSound,
Config->PreloadMusic);

Expand Down
3 changes: 3 additions & 0 deletions Meridian59/Client/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public virtual void Connect()
Config.SelectedConnectionInfo.StringDictionary,
LanguageCode.English); // todo: from config

// Load server-specific rooms if needed
ResourceManager.AddServerRooms(Config.SelectedConnectionInfo.Name, Config.PreloadRooms);

// fill ignore list in datacontroller with ignored
// playernames for this connectionentry.
Data.IgnoreList.Clear();
Expand Down
124 changes: 92 additions & 32 deletions Meridian59/Files/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public class ResourceManager
/// </summary>
public string RoomsFolder { get; set; }

/// <summary>
/// Folder containing server-specific .roo files.
/// </summary>
public string RoomsSubFolder { get; set; }

/// <summary>
/// Folder containing all object BGFs
/// </summary>
Expand Down Expand Up @@ -221,9 +226,13 @@ public RooFile GetRoom(string File)
{
// haven't loaded it yet?
if (rooFile == null)
{
{
// load it
rooFile = new RooFile(RoomsFolder + "/" + File);
if (!String.IsNullOrEmpty(RoomsSubFolder)
&& System.IO.File.Exists(RoomsFolder + "/" + RoomsSubFolder + "/" + File))
rooFile = new RooFile(RoomsFolder + "/" + RoomsSubFolder + "/" + File);
else
rooFile = new RooFile(RoomsFolder + "/" + File);

// resolve resource references (may load texture bgfs)
rooFile.ResolveResources(this);
Expand Down Expand Up @@ -523,19 +532,97 @@ public void SelectStringDictionary(string RsbFile, LanguageCode Language)
StringDictionarySelected(this, new EventArgs());
}

/// <summary>
/// Adds server-specific rooms to the Rooms dictionary, and
/// loads all rooms if room preloading is enabled. If we already
/// have server-specific rooms loaded, unload them and reload
/// the correct room (if one exists).
/// </summary>
/// <param name="serverNum"></param>
/// <param name="preloadRooms"></param>
public void AddServerRooms(string serverNum, bool preloadRooms)
{
string[] files;
string subFolder;
RooFile r;

// RoomsSubFolder is set and not equal to the server number (subfolder) we
// now have. Need to remove the previous server-specific rooms and add the
// default ones in place of them.
if (!String.IsNullOrEmpty(RoomsSubFolder) && !RoomsSubFolder.Equals(serverNum))
{
subFolder = Path.Combine(RoomsFolder, RoomsSubFolder);
if (Directory.Exists(subFolder))
{
files = Directory.GetFiles(subFolder, '*' + FileExtensions.ROO);
foreach (string s in files)
{
// Remove the old server-specific files.
Rooms.TryRemove(Path.GetFileName(s), out r);
// Try to add from default folder. Check for existence as the
// previous room may not have a default.
if (System.IO.File.Exists(Path.Combine(RoomsFolder, Path.GetFileName(s))))
Rooms.TryAdd(Path.GetFileName(s), null);
}
}
}

subFolder = Path.Combine(RoomsFolder, serverNum);
// Now check for a new subfolder, and add any .roo files.
if (Directory.Exists(subFolder))
{
files = Directory.GetFiles(subFolder, '*' + FileExtensions.ROO);

foreach (string s in files)
{
// Try to remove the room if already added to the dictionary,
// then add the server-specific one.
Rooms.TryRemove(Path.GetFileName(s), out r);
Rooms.TryAdd(Path.GetFileName(s), null);
}
}

// Save the subfolder identifying string for future room loading/resetting.
RoomsSubFolder = serverNum;

// Preload if user has chosen to do so.
if (preloadRooms)
LoadRooms();
}

/// <summary>
/// Loads rooms before connecting if user has chosen to preload.
/// </summary>
protected void LoadRooms()
{
IEnumerator<KeyValuePair<string, RooFile>> it = Rooms.GetEnumerator();
RooFile file;

while (it.MoveNext())
{
// load
if (!String.IsNullOrEmpty(RoomsSubFolder)
&& System.IO.File.Exists(Path.Combine(RoomsFolder, RoomsSubFolder, it.Current.Key)))
file = new RooFile(Path.Combine(RoomsFolder, RoomsSubFolder, it.Current.Key));
else
file = new RooFile(Path.Combine(RoomsFolder, it.Current.Key));

// update
Rooms.TryUpdate(it.Current.Key, file, null);
}
}

/// <summary>
/// Starts preloading resources in several threads.
/// </summary>
/// <param name="Objects"></param>
/// <param name="RoomTextures"></param>
/// <param name="Rooms"></param>
/// <param name="Sounds"></param>
/// <param name="Music"></param>
public void Preload(bool Objects, bool RoomTextures, bool Rooms, bool Sounds, bool Music)
public void Preload(bool Objects, bool RoomTextures, bool Sounds, bool Music)
{
Thread threadObjects = null;
Thread threadRoomTextures = null;
Thread threadRooms = null;
Thread threadSounds = null;
Thread threadMusic = null;

Expand All @@ -554,12 +641,6 @@ public void Preload(bool Objects, bool RoomTextures, bool Rooms, bool Sounds, bo
threadRoomTextures.Start();
}

if (Rooms)
{
threadRooms = new Thread(LoadThreadRooms);
threadRooms.Start();
}

if (Sounds)
{
threadSounds = new Thread(LoadThreadSounds);
Expand All @@ -577,7 +658,6 @@ public void Preload(bool Objects, bool RoomTextures, bool Rooms, bool Sounds, bo
// lock until all loaders are finished
while ( (threadObjects != null && threadObjects.IsAlive) ||
(threadRoomTextures != null && threadRoomTextures.IsAlive) ||
(threadRooms != null && threadRooms.IsAlive) ||
(threadSounds != null && threadSounds.IsAlive) ||
(threadMusic != null && threadMusic.IsAlive))
{
Expand Down Expand Up @@ -637,26 +717,6 @@ protected void LoadThreadRoomTextures()
}
}

/// <summary>
/// Stars loading all rooms in a background thread.
/// </summary>
protected void LoadThreadRooms()
{
IEnumerator<KeyValuePair<string, RooFile>> it = Rooms.GetEnumerator();
RooFile file;

while (it.MoveNext())
{
// load
file = new RooFile(Path.Combine(RoomsFolder, it.Current.Key));

// update
Rooms.TryUpdate(it.Current.Key, file, null);

queueAsyncFilesLoaded.Enqueue(it.Current.Key);
}
}

/// <summary>
/// Stars loading all sounds in a background thread.
/// </summary>
Expand Down