Skip to content
Permalink
Browse files

Converted PluginSettings to a scriptable object

  • Loading branch information...
Jackson Wood
Jackson Wood committed Feb 5, 2019
1 parent 1b453f7 commit f5d7b3f07fad4182d72a422049dd7d0efe947d52
Showing with 81 additions and 63 deletions.
  1. +2 −2 .gitignore
  2. +3 −3 browser example/Editor/EditorSettings.cs
  3. +1 −1 src/APIClient.cs
  4. +1 −1 src/CacheClient.cs
  5. +1 −1 src/ModManager.cs
  6. +68 −50 src/PluginSettings.cs
  7. +5 −5 src/UI/ModBrowser.cs
@@ -47,7 +47,7 @@ Thumbs.db

# Plugin Specific
_nonRepo/
Resources/modio_settings.asset*
/*.meta
*LICENSE*.meta
*README*.meta
/*.meta
csc.rsp
@@ -3,14 +3,14 @@ namespace ModIO.InEditor
{
public static class EditorSettings
{
public static readonly PluginSettings TEST_SERVER = new PluginSettings()
public static readonly PluginSettingsData TEST_SERVER = new PluginSettingsData()
{
apiURL = APIClient.API_URL_TESTSERVER + APIClient.API_VERSION,
cacheDirectory = IOUtilities.CombinePath(UnityEngine.Application.persistentDataPath, "modio_test"),
gameId = 0,
gameAPIKey = string.Empty,
};
public static readonly PluginSettings PRODUCTION_SERVER = new PluginSettings()
public static readonly PluginSettingsData PRODUCTION_SERVER = new PluginSettingsData()
{
apiURL = APIClient.API_URL_PRODUCTIONSERVER + APIClient.API_VERSION,
cacheDirectory = IOUtilities.CombinePath(UnityEngine.Application.persistentDataPath, "modio"),
@@ -24,7 +24,7 @@ public static class EditorSettings
public static void Load()
{
#pragma warning disable 0162
PluginSettings settings;
PluginSettingsData settings;
if(USE_TEST_SERVER)
{
settings = TEST_SERVER;
@@ -84,7 +84,7 @@ public static class APIClient
// ---------[ INITIALIZATION ]---------
static APIClient()
{
PluginSettings settings = PluginSettings.LoadDefaults();
PluginSettingsData settings = PluginSettings.data;
apiURL = settings.apiURL;
gameId = settings.gameId;
gameAPIKey = settings.gameAPIKey;
@@ -23,7 +23,7 @@ public static class CacheClient
/// <summary>Initializes the CacheClient settings.</summary>
static CacheClient()
{
PluginSettings settings = PluginSettings.LoadDefaults();
PluginSettingsData settings = PluginSettings.data;
CacheClient.cacheDirectory = settings.cacheDirectory;
}

@@ -40,7 +40,7 @@ private struct PersistentData
/// <summary>Initialzes the ModManager settings.</summary>
static ModManager()
{
PluginSettings settings = PluginSettings.LoadDefaults();
PluginSettingsData settings = PluginSettings.data;
ModManager.installDirectory = settings.installDirectory;

string dataPath = IOUtilities.CombinePath(CacheClient.cacheDirectory, PERSISTENTDATA_FILENAME);
@@ -2,70 +2,88 @@

namespace ModIO
{
[System.Serializable]
public struct PluginSettings
/// <summary>Wrapper object for the PluginSettingsData.</summary>
public class PluginSettings : ScriptableObject
{
// ---------[ FIELDS ]---------
public string apiURL;
public int gameId;
public string gameAPIKey;
public string cacheDirectory;
public string installDirectory;
/// <summary>Location of the settings file.</summary>
public static readonly string FILE_PATH = "modio_settings";

// ---------[ SAVE/LOAD ]---------
/// <summary>Instance for removing need to load.</summary>
private static PluginSettings _defaults;
/// <summary>Has the asset been loaded.</summary>
private static bool _loaded = false;

/// <summary>Location of the settings file.</summary>
public static readonly string FILE_LOCATION = IOUtilities.CombinePath(Application.persistentDataPath,
"modio",
"settings.data");
/// <summary>Singleton instance.</summary>
private static PluginSettingsData _data;

/// <summary>Writes a PluginSettings file to disk.</summary>
public static void SaveDefaults(PluginSettings settings)
/// <summary>The values that the plugin should use.</summary>
public static PluginSettingsData data
{
AssertUniqueProjectDetails();

if(!PluginSettings._defaults.Equals(settings))
get
{
PluginSettings._defaults = settings;
IOUtilities.WriteJsonObjectFile(FILE_LOCATION, settings);
if(!PluginSettings._loaded)
{
PluginSettings wrapper = Resources.Load<PluginSettings>(PluginSettings.FILE_PATH);

if(wrapper == null)
{
PluginSettings._data = new PluginSettingsData();
}
else
{
PluginSettings._data = wrapper.values;
}

PluginSettings._loaded = true;
}

return PluginSettings._data;
}
}

/// <summary>Loads the PluginSettings from disk.</summary>
public static PluginSettings LoadDefaults()
{
AssertUniqueProjectDetails();
/// <summary>Settings data.</summary>
public PluginSettingsData values;

if(PluginSettings._defaults.Equals(default(PluginSettings)))
{
PluginSettings._defaults = IOUtilities.ReadJsonObjectFile<PluginSettings>(PluginSettings.FILE_LOCATION);
}
/// <summary>Writes a PluginSettings file to disk.</summary>
[System.Obsolete]
public static void SaveDefaults(PluginSettingsData settings)
{
WriteSettingsAsset(settings);
}

return PluginSettings._defaults;
/// <summary>Loads the PluginSettings from disk.</summary>
[System.Obsolete]
public static PluginSettingsData LoadDefaults()
{
return PluginSettings._data;
}

/// <summary>Assets that the BundleIdentifier and CacheLocation are unique.</summary>
private static void AssertUniqueProjectDetails()

#if UNITY_EDITOR
public static void WriteSettingsAsset(PluginSettingsData settings)
{
#if DEBUG
if(Application.isPlaying)
{
if((Application.identifier.ToUpper().Contains("PRODUCTNAME")
&& Application.identifier.ToUpper().Contains("COMPANY"))
|| (PluginSettings.FILE_LOCATION.ToUpper().Contains("PRODUCTNAME")
&& PluginSettings.FILE_LOCATION.ToUpper().Contains("COMPANY")))
{
Debug.LogError("[mod.io] Implementing ModIO in a project that uses the default"
+ " bundle identifier will cause conflicts with other projects"
+ " using mod.io. Please open \'Build Settings' > \'Player Settings\'"
+ " and assign a unique Company Name, Project Name, and Bundle"
+ " Identifier (under \'Other Settings\') to utilize the mod.io "
+ " Unity Plugin.");
}
}
#endif
// TODO(@jackson)
Debug.LogWarning("Ensure we save with the vars (Application.persistentDataPath for example)");

PluginSettings asset = ScriptableObject.CreateInstance<PluginSettings>();
asset.values = settings;

UnityEditor.AssetDatabase.CreateFolder("Assets", "Resources");
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
UnityEditor.AssetDatabase.CreateAsset(asset,
"Assets/Resources/" + PluginSettings.FILE_PATH + ".asset");
UnityEditor.AssetDatabase.SaveAssets();
}
#endif
}

[System.Serializable]
public struct PluginSettingsData
{
// ---------[ FIELDS ]---------
public string apiURL;
public int gameId;
public string gameAPIKey;
public string cacheDirectory;
public string installDirectory;
}
}
@@ -112,23 +112,23 @@ private class SubscriptionViewFilter
// ---------[ FIELDS ]---------
[Header("Settings")]
public ServerType connectTo = ServerType.TestServer;
public PluginSettings testPluginSettings = new PluginSettings()
public PluginSettingsData testPluginSettings = new PluginSettingsData()
{
apiURL = APIClient.API_URL_TESTSERVER + APIClient.API_VERSION,
gameId = 0,
gameAPIKey = string.Empty,
cacheDirectory = "$PERSISTENT_DATA_PATH$/modio_test",
installDirectory = "$PERSISTENT_DATA_PATH$/modio_test/_installedMods",
};
public PluginSettings productionPluginSettings = new PluginSettings()
public PluginSettingsData productionPluginSettings = new PluginSettingsData()
{
apiURL = APIClient.API_URL_PRODUCTIONSERVER + APIClient.API_VERSION,
gameId = 0,
gameAPIKey = string.Empty,
cacheDirectory = "$PERSISTENT_DATA_PATH$/modio",
installDirectory = "$PERSISTENT_DATA_PATH$/modio/_installedMods",
};
public PluginSettings customPluginSettings = new PluginSettings()
public PluginSettingsData customPluginSettings = new PluginSettingsData()
{
apiURL = string.Empty,
gameId = 0,
@@ -224,7 +224,7 @@ private void Start()
private void LoadLocalData()
{
// - Server Settings -
PluginSettings settings;
PluginSettingsData settings;
switch(connectTo)
{
case ServerType.TestServer:
@@ -244,7 +244,7 @@ private void LoadLocalData()
break;
default:
{
settings = new PluginSettings();
settings = new PluginSettingsData();
}
break;
}

0 comments on commit f5d7b3f

Please sign in to comment.
You can’t perform that action at this time.