Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #82 from modio/v2.3
Browse files Browse the repository at this point in the history
V2.3.3
  • Loading branch information
modio-jackson authored Jun 15, 2021
2 parents 34c56b9 + 803f202 commit 4f13d3e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Runtime/API/RequestCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private struct Entry
private const int ENTRY_LIFETIME = 120;

/// <summary>Max cache size.</summary>
private static readonly uint MAX_CACHE_SIZE = PluginSettings.CACHE_SIZE*1024;
private static readonly uint MAX_CACHE_SIZE = PluginSettings.CACHE_SIZE_BYTES;

// ---------[ Fields ]---------
/// <summary>Map of url to saved responses.</summary>
Expand Down
2 changes: 1 addition & 1 deletion Runtime/ModIOVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public struct ModIOVersion : System.IComparable<ModIOVersion>
{
// ---------[ Singleton ]---------
/// <summary>Singleton instance for current version.</summary>
public static readonly ModIOVersion Current = new ModIOVersion(2, 3, 2);
public static readonly ModIOVersion Current = new ModIOVersion(2, 3, 3);

// ---------[ Fields ]---------
/// <summary>Major version number.</summary>
Expand Down
91 changes: 67 additions & 24 deletions Runtime/PluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace ModIO
{
/// <summary>Stores the settings used by various classes that are unique to the game/app.</summary>
public class PluginSettings : ScriptableObject
public class PluginSettings : ScriptableObject, ISerializationCallbackReceiver
{
// ---------[ NESTED CLASSES ]---------
/// <summary>Attribute for denoting a field as containing directory variables.</summary>
Expand All @@ -33,46 +33,64 @@ public struct RequestLoggingOptions
[System.Serializable]
public struct Data
{
// ---------[ Versioning ]---------
internal const int VERSION = 1;

[HideInInspector]
[VersionedData(VERSION, VERSION)]
public int version;

// ---------[ Fields ]---------
[Header("API Settings")]
[Tooltip("API URL to use when making requests")]
[VersionedData(0, "")]
public string apiURL;

[Tooltip("Game Id assigned to your game profile")]
[VersionedData(0, GameProfile.NULL_ID)]
public int gameId;

[Tooltip("API Key assigned to your game profile")]
[VersionedData(0, "")]
public string gameAPIKey;

[Tooltip("Amount of memory the request cache is permitted to grow to (KB)")]
public uint requestCacheSizeKB;
[Tooltip("Amount of memory the request cache is permitted to grow to (KB)."
+ "\nA negative value indicates an unlimited cache size.")]
[VersionedData(1, (int)-1)]
public int requestCacheSizeKB;

/// <summary>Request logging options.</summary>
public RequestLoggingOptions requestLogging;

[Header("Standalone Directories")]
[Tooltip("Directory to use for mod installations")]
[VersionedData(0, @"$DATA_PATH$/mod.io/mods")]
[VariableDirectory]
public string installationDirectory;

[Tooltip("Directory to use for cached server data")]
[VersionedData(0, @"$DATA_PATH$/mod.io/cache")]
[VariableDirectory]
public string cacheDirectory;

[Tooltip("Directory to use for user data")]
[VersionedData(0, @"$PERSISTENT_DATA_PATH$/mod.io-$GAME_ID$")]
[VariableDirectory]
public string userDirectory;

[Header("Editor Directories")]
[Tooltip("Directory to use for mod installations")]
[VersionedData(0, @"$CURRENT_DIRECTORY$/mod.io/editor/$GAME_ID$/mods")]
[VariableDirectory]
public string installationDirectoryEditor;

[Tooltip("Directory to use for cached server data")]
[VersionedData(0, @"$CURRENT_DIRECTORY$/mod.io/editor/$GAME_ID$/cache")]
[VariableDirectory]
public string cacheDirectoryEditor;

[Tooltip("Directory to use for user data")]
[VersionedData(0, @"$CURRENT_DIRECTORY$/mod.io/editor/$GAME_ID$/user")]
[VariableDirectory]
public string userDirectoryEditor;

Expand Down Expand Up @@ -216,9 +234,19 @@ public static RequestLoggingOptions REQUEST_LOGGING
{
get { return PluginSettings.data.requestLogging; }
}
public static uint CACHE_SIZE
public static uint CACHE_SIZE_BYTES
{
get { return PluginSettings.data.requestCacheSizeKB; }
get
{
if(PluginSettings.data.requestCacheSizeKB < 0)
{
return uint.MaxValue;
}
else
{
return (uint)PluginSettings.data.requestCacheSizeKB * 1024;
}
}
}

// ---------[ FUNCTIONALITY ]---------
Expand Down Expand Up @@ -338,6 +366,31 @@ public static string ReplaceDirectoryVariables(string directory, int gameId)
return directory;
}

/// <summary>Creates an updated version of passed PluginSettings.Data.</summary>
public static PluginSettings.Data UpdateVersionedValues(int dataVersion,
PluginSettings.Data dataValues)
{
// early out
if(dataVersion >= PluginSettings.Data.VERSION)
{
return dataValues;
}
else
{
return VersionedDataAttribute.UpdateStructFields(dataVersion, dataValues);
}
}

// ---------[ ISerializationCallbackReceiver ]---------
/// <summary>Implement this method to receive a callback after Unity deserializes your object.</summary>
public void OnAfterDeserialize()
{
this.m_data = PluginSettings.UpdateVersionedValues(this.m_data.version, this.m_data);
}

/// <summary>Implement this method to receive a callback before Unity serializes your object.</summary>
public void OnBeforeSerialize() {}

// ---------[ EDITOR CODE ]---------
#if UNITY_EDITOR
/// <summary>Locates the PluginSettings asset used at runtime.</summary>
Expand All @@ -359,26 +412,16 @@ public static void FocusAsset()
/// <summary>Generates a PluginSettings.Data instance with runtime defaults.</summary>
public static PluginSettings.Data GenerateDefaultData()
{
PluginSettings.Data data = new PluginSettings.Data()
PluginSettings.Data data = new PluginSettings.Data();
data = PluginSettings.UpdateVersionedValues(-1, data);

// non-constant defaults
data.apiURL = APIClient.API_URL_PRODUCTIONSERVER + APIClient.API_VERSION;
data.requestLogging = new RequestLoggingOptions()
{
apiURL = APIClient.API_URL_PRODUCTIONSERVER + APIClient.API_VERSION,
gameId = GameProfile.NULL_ID,
gameAPIKey = string.Empty,
requestCacheSizeKB = 4*1024,
requestLogging = new RequestLoggingOptions()
{
errorsAsWarnings = true,
logAllResponses = false,
logOnSend = false,
},

installationDirectory = IOUtilities.CombinePath("$DATA_PATH$","mod.io","mods"),
cacheDirectory = IOUtilities.CombinePath("$DATA_PATH$","mod.io","cache"),
userDirectory = IOUtilities.CombinePath("$PERSISTENT_DATA_PATH$","mod.io-$GAME_ID$"),

installationDirectoryEditor = IOUtilities.CombinePath("$CURRENT_DIRECTORY$","mod.io","editor","$GAME_ID$","mods"),
cacheDirectoryEditor = IOUtilities.CombinePath("$CURRENT_DIRECTORY$","mod.io","editor","$GAME_ID$","cache"),
userDirectoryEditor = IOUtilities.CombinePath("$CURRENT_DIRECTORY$","mod.io","editor","$GAME_ID$","user"),
errorsAsWarnings = true,
logAllResponses = false,
logOnSend = false,
};

return data;
Expand Down
53 changes: 53 additions & 0 deletions Runtime/Utility/VersionedDataAttribute.cs
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;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Utility/VersionedDataAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4f13d3e

Please sign in to comment.