Skip to content

Commit ba8b350

Browse files
committed
Refactored versioning logic and logging methods for improved clarity and error handling
1 parent b54ef73 commit ba8b350

File tree

2 files changed

+97
-26
lines changed

2 files changed

+97
-26
lines changed

TLibrary/Models/Plugin/PluginBase.cs

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,105 @@ public string PluginName
7979
/// </summary>
8080
public static TLogger Logger => _logger;
8181

82-
public static Version Version { get; } = Assembly.GetExecutingAssembly().GetName().Version;
83-
84-
public static DateTime BuildDate { get; } = new DateTime(2000, 1, 1).AddDays(Version.Build).AddSeconds(Version.Revision * 2);
82+
private static DateTime _versioningDate = new DateTime(2000, 1, 1);
83+
private static Version _libraryVersion;
84+
private static Version _version;
85+
private static DateTime _buildDate;
86+
private static Version _displayVersion;
87+
88+
public static Version LibraryVersion => _libraryVersion;
89+
public static Version Version => _displayVersion;
90+
public static DateTime BuildDate => _buildDate;
8591

8692
/// <summary>
8793
/// Used when the plugin loads
8894
/// </summary>
8995
protected override void Load()
9096
{
91-
if (!this.Name.IsNullOrEmpty())
92-
_pluginName = this.Name;
93-
_rootDirectory = System.IO.Directory.GetCurrentDirectory();
94-
_pluginDirectory = Path.Combine(_rootDirectory, "Plugins", GetPluginName());
95-
_logger = TLogger.CreateInstance(this, false);
96-
9797
try
9898
{
99-
base.Load();
100-
CheckPluginFiles();
101-
OnLoad();
99+
if (!this.Name.IsNullOrEmpty())
100+
_pluginName = this.Name;
101+
_rootDirectory = System.IO.Directory.GetCurrentDirectory();
102+
_pluginDirectory = Path.Combine(_rootDirectory, "Plugins", GetPluginName());
103+
_logger = TLogger.CreateInstance(this, false);
104+
105+
Assembly assembly = Assembly.GetExecutingAssembly();
106+
object[] versionAttributes;
107+
108+
// Get Library Version
109+
try
110+
{
111+
versionAttributes = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
112+
if (versionAttributes.Length > 0)
113+
{
114+
AssemblyFileVersionAttribute versionAttribute =
115+
(AssemblyFileVersionAttribute)versionAttributes[0];
116+
_logger.Debug("Loading library version: " + versionAttribute.Version);
117+
_libraryVersion = new Version(versionAttribute.Version);
118+
}
119+
}
120+
catch (Exception ex)
121+
{
122+
_logger.Exception("Failed to get library version:");
123+
_logger.Error(ex);
124+
}
125+
126+
assembly = this.Assembly;
127+
128+
// Get Plugin Build Version
129+
try
130+
{
131+
_version = assembly.GetName().Version;
132+
_buildDate = _versioningDate.AddDays(_version.Build).AddSeconds(_version.Revision * 2);
133+
}
134+
catch (Exception ex)
135+
{
136+
_logger.Exception("Failed to get plugin build version:");
137+
_logger.Error(ex);
138+
}
139+
140+
// Get Plugin Display Version
141+
try
142+
{
143+
versionAttributes =
144+
assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
145+
if (versionAttributes.Length > 0)
146+
{
147+
AssemblyInformationalVersionAttribute informationalVersionAttribute =
148+
(AssemblyInformationalVersionAttribute)versionAttributes[0];
149+
_logger.Debug("Loading plugin display version: " + informationalVersionAttribute.InformationalVersion);
150+
_displayVersion = new Version(informationalVersionAttribute.InformationalVersion);
151+
}
152+
else
153+
{
154+
_logger.Debug("No plugin display version found, using default version.");
155+
_displayVersion = new Version(1, 0, 0, 0);
156+
}
157+
}
158+
catch (Exception ex)
159+
{
160+
_logger.Exception("Failed to get plugin display version:");
161+
_logger.Error(ex);
162+
}
163+
164+
try
165+
{
166+
base.Load();
167+
CheckPluginFiles();
168+
OnLoad();
102169

170+
}
171+
catch (Exception ex)
172+
{
173+
_logger.Exception($"Failed to load {Name}");
174+
_logger.Error(ex);
175+
}
103176
}
104-
catch (Exception ex)
177+
catch (Exception e)
105178
{
106-
_logger.LogException($"Failed to load {Name}");
107-
_logger.LogError(ex);
179+
_logger.Exception($"Unexpected error while loading {Name}");
180+
_logger.Error(e);
108181
}
109182
}
110183

@@ -203,7 +276,7 @@ public virtual void CheckPluginFiles()
203276
if (field.GetValue(Config) != null)
204277
continue;
205278

206-
Logger.LogWarning($"The config field '{field.Name}' is missing in '{PluginName}' configuration.");
279+
Logger.Warning($"The config field '{field.Name}' is missing in '{PluginName}' configuration.");
207280
}
208281

209282
foreach (var field in Config.GetType().GetFields())
@@ -214,7 +287,7 @@ public virtual void CheckPluginFiles()
214287
if (field.GetValue(Config) != null)
215288
continue;
216289

217-
Logger.LogWarning($"The config field '{field.Name}' is missing in '{PluginName}' configuration.");
290+
Logger.Warning($"The config field '{field.Name}' is missing in '{PluginName}' configuration.");
218291
}
219292

220293

@@ -249,7 +322,7 @@ public virtual void CheckPluginFiles()
249322
|| www.result == UnityWebRequest.Result.DataProcessingError
250323
|| www.result == UnityWebRequest.Result.ProtocolError)
251324
{
252-
Logger.LogError("Failed to download language packs.");
325+
Logger.Error("Failed to download language packs.");
253326
}
254327
else
255328
File.WriteAllText(path, www.downloadHandler.text);
@@ -318,7 +391,7 @@ private static IEnumerator InvokeRoutine(Action f, float delay)
318391
[Obsolete("Use Localize instead", true)]
319392
protected new string Translate(string translationKey, params object[] placeholder)
320393
{
321-
Logger.LogWarning($"OLD TRANSLATION METHOD WAS USED FOR '{translationKey}'");
394+
Logger.Warning($"OLD TRANSLATION METHOD WAS USED FOR '{translationKey}'");
322395
throw new Exception("The 'Translate' method was used instead of 'Localize'.");
323396
}
324397

@@ -348,8 +421,8 @@ public string Localize(bool AddPrefix, string translationKey, params object[] ar
348421
}
349422
catch (Exception ex)
350423
{
351-
Logger.LogException($"Failed to localize '{translationKey}' key:");
352-
Logger.LogError(ex);
424+
Logger.Exception($"Failed to localize '{translationKey}' key:");
425+
Logger.Error(ex);
353426
return string.Empty;
354427
}
355428
}

TLibrary/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
// Build Number
2929
// Revision
3030
//
31-
// You can specify all the values or you can default the Build and Revision Numbers
32-
// by using the '*' as shown below:
33-
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.1.*")]
35-
//[assembly: AssemblyFileVersion("1.0.0.0")]
31+
[assembly: AssemblyVersion("1.0.0.0")]
32+
[assembly: AssemblyFileVersion("1.2.0")]
33+
[assembly: AssemblyInformationalVersion("1.2.0")]

0 commit comments

Comments
 (0)