This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from modio/v2.3
V2.3.3
- Loading branch information
Showing
5 changed files
with
133 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace ModIO | ||
{ | ||
/// <summary>Attribute for determining the version of and default value for a given field.</summary> | ||
public class VersionedDataAttribute : System.Attribute | ||
{ | ||
// ---------[ Fields ]--------- | ||
/// <summary>Version the field was created.</summary> | ||
public int version; | ||
|
||
/// <summary>Default value for the field.</summary> | ||
public System.Object defaultValue; | ||
|
||
// ---------[ Initialization ]--------- | ||
public VersionedDataAttribute(int version, System.Object defaultValue) | ||
{ | ||
this.version = version; | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
// ---------[ Utility ]--------- | ||
/// <summary>Creates an updated version of the data structure passed.</summary> | ||
public static T UpdateStructFields<T>(int dataVersion, T dataValues) | ||
where T : struct | ||
{ | ||
// set up data | ||
T updatedValues = dataValues; | ||
System.Object boxedData = updatedValues; | ||
|
||
// iterate over the typed fields | ||
var fieldList = typeof(T).GetFields(); | ||
foreach(var field in fieldList) | ||
{ | ||
// check for the VersionedDataAttribute attribute | ||
var attributeList = field.GetCustomAttributes(typeof(VersionedDataAttribute), false); | ||
if(attributeList != null | ||
&& attributeList.Length == 1) | ||
{ | ||
// set the default value if attribute is newer than the dataVersion | ||
VersionedDataAttribute dataAttribute = (VersionedDataAttribute)attributeList[0]; | ||
if(dataAttribute.version > dataVersion) | ||
{ | ||
field.SetValue(boxedData, dataAttribute.defaultValue); | ||
} | ||
} | ||
} | ||
|
||
// unbox the updatedValues | ||
updatedValues = (T)boxedData; | ||
|
||
return updatedValues; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.