Skip to content

Commit

Permalink
v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Dec 4, 2020
1 parent 2dd1fa2 commit e537ca5
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 241 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ Build
*.sln
.vs
obj/
bin/
bin/
Doxyfile
Docs
133 changes: 58 additions & 75 deletions VinaFrameworkClient/Core/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,16 @@
namespace VinaFrameworkClient.Core
{
/// <summary>
/// Extend your Client script with this class and add modules to your constructor
/// Extend your Client class with BaseClient class and add your modules into the constructor.
/// </summary>
public abstract class BaseClient : BaseScript
{
/// <summary>
/// This current client resource name
/// </summary>
public static string Name { get; private set; }

/// <summary>
/// This current client resource name
/// </summary>
public string ResourceName { get { return Name; } }

private List<Module> modules;

/// <summary>
/// Extend your Client script with this class and add modules to your constructor
/// Extend your Client class with BaseClient class and add your modules into the constructor.
/// </summary>
public BaseClient()
{
Name = API.GetCurrentResourceName();
ResourceName = API.GetCurrentResourceName();
modules = new List<Module>();

Debug.WriteLine("============================================================");
Expand All @@ -38,7 +26,22 @@ public BaseClient()
Tick += initialize;
}

#region Base Events
#region VARIABLES

/// <summary>
/// This current client resource name
/// </summary>
public static string ResourceName { get; private set; }

/// <summary>
/// This current client resource name
/// </summary>
public string Name { get { return ResourceName; } }

private List<Module> modules;

#endregion
#region BASE EVENTS

private async Task initialize()
{
Expand All @@ -51,14 +54,13 @@ private async Task initialize()
}

#endregion

#region Methods
#region CLIENT METHODS

/// <summary>
/// Verify if the module has been loaded.
/// Check if a Module has been added to the Client instance.
/// </summary>
/// <param name="moduleType">The module class type. Ex: typeof(MyModule)</param>
/// <returns></returns>
/// <param name="moduleType">Your module type. Ex: typeof(MyModule)</param>
/// <returns>True if it was found.</returns>
public bool HasModule(Type moduleType)
{
foreach (Module module in modules)
Expand All @@ -73,10 +75,10 @@ public bool HasModule(Type moduleType)
}

/// <summary>
/// Add module by referencing the type of your module class.
/// Add a module by passing your Module type.
/// </summary>
/// <param name="moduleType">The module class type. Ex: typeof(MyModule)</param>
public void AddModule(Type moduleType)
/// <param name="moduleType">Your module type. Ex: typeof(MyModule)</param>
protected void AddModule(Type moduleType)
{
bool error = false;
foreach (Module _module in modules)
Expand Down Expand Up @@ -105,9 +107,9 @@ public void AddModule(Type moduleType)
}

/// <summary>
/// Get a module instance from anywhere, cannot be called inside a module constructor.
/// Get a module instance. Cannot be called inside a Module constructor.
/// </summary>
/// <typeparam name="T">The module class type.</typeparam>
/// <typeparam name="T">Your module type. Ex: &lt;MyModule&gt;</typeparam>
/// <returns>Return the module or throw an exception if the module is not found.</returns>
public T GetModule<T>()
{
Expand All @@ -123,91 +125,72 @@ public T GetModule<T>()
}

/// <summary>
/// Register an event.
/// Add an event.
/// </summary>
/// <param name="eventName">The event name to register.</param>
/// <param name="action">The event delegate to register.</param>
public void RegisterEvent(string eventName, Delegate action)
/// <param name="eventName">Event name to add.</param>
/// <param name="action">Event delegate to add.</param>
protected void AddEvent(string eventName, Delegate action)
{
EventHandlers[eventName] += action;
Log($"Event {eventName} registered!");
Log($"Event {eventName} added!");
}

/// <summary>
/// Un-register an event.
/// Remove an event.
/// </summary>
/// <param name="eventName">The event name to un-register.</param>
/// <param name="action">The event delegate to un-register.</param>
public void UnregisterEvent(string eventName, Delegate action)
/// <param name="eventName">Event name to remove.</param>
/// <param name="action">Event delegate to remove.</param>
protected void RemoveEvent(string eventName, Delegate action)
{
EventHandlers[eventName] -= action;
Log($"Event {eventName} deregistered!");
}

/// <summary>
/// Add a tick to the resource.
/// </summary>
/// <param name="action">The tick delegate to add.</param>
public void AddTick(Func<Task> action)
{
Tick += action;
Log($"{action.Method.DeclaringType.Name} added Tick {action.Method.Name}!");
}

/// <summary>
/// Remove a tick from the resource.
/// </summary>
/// <param name="action">The tick delegate to remove.</param>
public void RemoveTick(Func<Task> action)
{
Tick -= action;
Log($"{action.Method.DeclaringType.Name} removed Tick {action.Method.Name}!");
Log($"Event {eventName} removed!");
}

/// <summary>
/// Un-register an event.
/// Add a tick.
/// </summary>
/// <param name="eventName">The event name to un-register.</param>
/// <param name="action">The event delegate to un-register.</param>
internal void AddInternalTick(Func<Task> action)
/// <param name="action">Tick delegate to add.</param>
protected void AddTick(Func<Task> action)
{
Tick += action;
Log($"Added Tick {action.Method.Name}!");
}

/// <summary>
/// Remove a tick from the resource.
/// Remove a tick.
/// </summary>
/// <param name="action">The tick delegate to remove.</param>
internal void RemoveInternalTick(Func<Task> action)
/// <param name="action">Tick delegate to remove.</param>
protected void RemoveTick(Func<Task> action)
{
Tick -= action;
Log($"Removed Tick {action.Method.Name}!");
}

/// <summary>
/// Get the ExportDictionary.
/// </summary>
/// <returns>Return the ExportDictionary.</returns>
public ExportDictionary GetExports()
protected ExportDictionary GetExports()
{
return Exports;
}

/// <summary>
/// Get a specific Export from a resource.
/// Get a resource Export.
/// </summary>
/// <param name="resourceName"></param>
/// <returns>Return the dynamic export.</returns>
public dynamic GetExport(string resourceName)
/// <returns>Return a dynamic export.</returns>
protected dynamic GetExport(string resourceName)
{
return Exports[resourceName];
}

/// <summary>
/// Set an Export from this resource.
/// Export a delegate method.
/// </summary>
/// <param name="name">The name of the Exported method.</param>
/// <param name="method">The delegate of the Exported method.</param>
public void SetExport(string name, Delegate method)
/// <param name="name">Name of the Export.</param>
/// <param name="method">Delegate of the Export.</param>
protected void SetExport(string name, Delegate method)
{
Exports.Add(name, method);
}
Expand All @@ -216,20 +199,20 @@ public void SetExport(string name, Delegate method)
/// Log a message.
/// </summary>
/// <param name="message">The message to log.</param>
public void Log(string message)
protected void Log(string message)
{
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {ResourceName.ToUpper()}: {message}");
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {BaseClient.ResourceName.ToUpper()}: {message}");
}

/// <summary>
/// Log an exception.
/// </summary>
/// <param name="exception">The Exception to log.</param>
/// <param name="prefix">Some text to add before the log message.</param>
public void LogError(Exception exception, string prefix = "")
protected void LogError(Exception exception, string prefix = "")
{
string pre = (prefix != "") ? $" {prefix}" : "";
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [ERROR] {ResourceName.ToUpper()}{pre}: {exception.Message}\n{exception.StackTrace}");
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [ERROR] {BaseClient.ResourceName.ToUpper()}{pre}: {exception.Message}\n{exception.StackTrace}");
}

#endregion
Expand Down
73 changes: 29 additions & 44 deletions VinaFrameworkClient/Core/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,49 @@
namespace VinaFrameworkClient.Core
{
/// <summary>
/// Extend your module class with this class and add it to your client constructor.
/// Extend all your modules with the Module class and add them to your Client class constructor.
/// </summary>
public abstract class Module
{
/// <summary>
/// This current module name.
/// Extend all your modules with the Module class and add them to your Client class constructor.
/// </summary>
protected string Name { get; }
/// <param name="client">A BaseClient class</param>
public Module(BaseClient client)
{
Name = this.GetType().Name;
this.client = client;
script = new ModuleScript(this);
BaseClient.RegisterScript(script);
script.AddInternalTick(initialize);
script.Log($"Instance created!");
}

#region VARIABLES

/// <summary>
/// Current module name.
/// </summary>
public string Name { get; }

/// <summary>
/// Read-only reference to the client instance.
/// </summary>
protected BaseClient client { get; }

/// <summary>
/// Extend your module class with this class and add it to your client constructor.
/// Read-only reference to the module script instance.
/// </summary>
/// <param name="client"></param>
public Module(BaseClient client)
{
Name = this.GetType().Name;
this.client = client;
client.AddInternalTick(initialize);
Log($"Instance created!");
}
protected ModuleScript script { get; }

#endregion
#region BASE EVENTS

private async Task initialize()
{
Log($"Initializing...");
script.Log($"Initializing...");

client.RemoveInternalTick(initialize);
script.RemoveInternalTick(initialize);

await BaseClient.Delay(0);

Expand All @@ -46,7 +58,7 @@ private async Task initialize()
}
catch (Exception exception)
{
LogError(exception, " in OnModuleInitialized");
script.LogError(exception, " in OnModuleInitialized");
}
}

Expand All @@ -55,36 +67,9 @@ private async Task initialize()
/// </summary>
protected virtual void OnModuleInitialized()
{
Log($"Initialized!");
script.Log($"Initialized!");
}

/// <summary>
///
/// </summary>
/// <param name="msecs"></param>
public static Task Delay(int msecs)
{
return BaseClient.Delay(msecs);
}

/// <summary>
/// Log a message from this module.
/// </summary>
/// <param name="message">The message to log.</param>
protected void Log(string message)
{
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {client.ResourceName.ToUpper()} > {Name.ToUpper()}: {message}");
}

/// <summary>
/// Log an exception from this module.
/// </summary>
/// <param name="exception">The Exception to log.</param>
/// <param name="prefix">Some text to add before the log message.</param>
protected void LogError(Exception exception, string prefix = "")
{
string pre = (prefix != "") ? $" {prefix}" : "";
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [ERROR] {client.ResourceName.ToUpper()} > {Name.ToUpper()}{pre}: {exception.Message}\n{exception.StackTrace}");
}
#endregion
}
}
Loading

0 comments on commit e537ca5

Please sign in to comment.