Leviathan  0.8.0.0
Leviathan game engine
SyncedResource.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "SyncedResource.h"
3 
4 #include "SyncedVariables.h"
5 #include "Exceptions.h"
6 using namespace Leviathan;
7 using namespace std;
8 // ------------------------------------ //
9 DLLEXPORT Leviathan::SyncedResource::SyncedResource(const std::string &uniquename) :
10  Name(uniquename)
11 {
12 
13 }
14 
16 
17  GUARD_LOCK();
18 
19  // Unregister (if not already done) //
20  ReleaseParentHooks(guard);
21 }
22 
24 {
25  ConnectToNotifier(&variablesync);
26 }
27 // ------------------------------------ //
29  sf::Packet &packet)
30 {
31 
32  // Load the custom data //
33  try{
34  UpdateCustomDataFromPacket(guard, packet);
35 
36  } catch(const InvalidArgument &e){
37 
38  e.PrintToLog();
39  return false;
40  }
41 
42  // Notify us about the update //
43  OnValueUpdated(guard);
44 
45  // Notify listeners //
46  NotifyAll(guard);
47  return true;
48 }
49 // ------------------------------------ //
51  GUARD_LOCK();
52 
53  // Update the networked value //
54  UpdateOurNetworkValue(guard);
55 
56  // Notify us about the update //
57  OnValueUpdated(guard);
58 
59  // Notify listeners //
60  NotifyAll(guard);
61 }
62 // ------------------------------------ //
64 
65 }
66 // ------------------------------------ //
68 
69  // First add the name //
70  packet << Name;
71 
72  // Then add our data //
73  SerializeCustomDataToPacket(guard, packet);
74 }
75 
77  // Get the name from the packet //
78  std::string tmpstr;
79 
80  packet >> tmpstr;
81 
82  if(!packet)
83  throw InvalidArgument("resource sync packet has invalid format");
84 
85  return tmpstr;
86 }
87 // ------------------------------------ //
89 
90  if (ConnectedToParents.size() < 1) {
91 
92  LOG_WARNING("Updating SyncedResource that isn't attached to SyncedVariables");
93  return;
94  }
95 
96  static_cast<SyncedVariables*>(ConnectedToParents[0]->GetActualPointerToNotifierObject())
97  ->_NotifyUpdatedValue(guard, this);
98 }
99 
virtual DLLEXPORT bool UpdateDataFromPacket(Lock &guard, sf::Packet &packet)
Assigns data from a packet to this resource.
DLLEXPORT SyncedResource(const std::string &uniquename)
Constructs a base class for synced variables that requires a unique name.
Class that synchronizes some key variables with another instance.
virtual DLLEXPORT ~SyncedResource()
virtual void OnValueUpdated(Lock &guard)
virtual DLLEXPORT void AddDataToPacket(Lock &guard, sf::Packet &packet)
Serializes the name to a packet.
#define LOG_WARNING(x)
Definition: Define.h:89
virtual DLLEXPORT void UpdateOurNetworkValue(Lock &guard)
Notifies our SyncedVariables of an update.
static DLLEXPORT std::string GetSyncedResourceNameFromPacket(sf::Packet &packet)
Gets a name from packet leaving only the variable data there.
virtual DLLEXPORT void StartSync(SyncedVariables &variablesync)
Registers this resource with the SyncedVariables instance.
#define DLLEXPORT
Definition: Include.h:84
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT void NotifyUpdatedValue()
Notify that this is changed and we want a local message, too.
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18
virtual DLLEXPORT void PrintToLog() const noexcept
Definition: Exceptions.cpp:35