Browse Source

Add graphics settings load from json

master
NGnius (Graham) 4 years ago
parent
commit
423f256640
5 changed files with 277 additions and 59 deletions
  1. +0
    -56
      HelloModdingWorld/MyPlugin.cs
  2. +1
    -1
      Kompressor.sln
  3. +5
    -2
      Kompressor/Kompressor.csproj
  4. +134
    -0
      Kompressor/MyPlugin.cs
  5. +137
    -0
      Kompressor/QualitySettings.cs

+ 0
- 56
HelloModdingWorld/MyPlugin.cs View File

@@ -1,56 +0,0 @@
using System.Reflection;

using IllusionPlugin;
//using GamecraftModdingAPI;

namespace HelloModdingWorld
{
public class MyPlugin : IPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin'
{
public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name; // HelloModdingWorld by default
// To change the name, change the project's name

public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString(); // 0.0.1 by default
// To change the version, change <Version>0.0.1</Version> in HelloModdingWorld.csproj

// called when Gamecraft shuts down
public void OnApplicationQuit()
{
// Shutdown this mod
GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has shutdown");

// Shutdown the Gamecraft modding API last
GamecraftModdingAPI.Main.Shutdown();
}

// called when Gamecraft starts up
public void OnApplicationStart()
{
// Initialize the Gamecraft modding API first
GamecraftModdingAPI.Main.Init();
// check out the modding API docs here: https://mod.exmods.org/

// Initialize this mod
// create HelloWorld command
// this writes "Hello modding world!" when you execute it in Gamecraft's console
// (use the forward-slash key '/' to open the console in Gamecraft when in a game)
GamecraftModdingAPI.Commands.CommandBuilder.Builder()
.Name("HelloWorld") // command name (used to invoke it in the console)
.Description("Says Hello modding world!") // command description (displayed in help and hint toolbar)
.Action(() => { GamecraftModdingAPI.Utility.Logging.CommandLog("Hello modding world!"); })
.Build(); // construct and automatically register the command so the modding API knows about it

GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has started up");
}

// unused methods

public void OnFixedUpdate() { } // called once per physics update

public void OnLevelWasInitialized(int level) { } // called after a level is initialized

public void OnLevelWasLoaded(int level) { } // called after a level is loaded

public void OnUpdate() { } // called once per rendered frame (frame update)
}
}

HelloModdingWorld.sln → Kompressor.sln View File

@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29609.76
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelloModdingWorld", "HelloModdingWorld\HelloModdingWorld.csproj", "{E0EEA15D-AB3C-4C73-A000-C49B5AE9EA66}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kompressor", "Kompressor\Kompressor.csproj", "{E0EEA15D-AB3C-4C73-A000-C49B5AE9EA66}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

HelloModdingWorld/HelloModdingWorld.csproj → Kompressor/Kompressor.csproj View File

@@ -4,9 +4,9 @@
<TargetFramework>net472</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Version>0.0.1</Version>
<Authors>Me</Authors>
<Authors>NGnius</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://git.exmods.org/modtainers/HelloModdingWorld</PackageProjectUrl>
<PackageProjectUrl>https://git.exmods.org/NGnius/Kompressor</PackageProjectUrl>
<NeutralLanguage>en-CA</NeutralLanguage>
</PropertyGroup>

@@ -16,6 +16,9 @@
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Assembly-CSharp">
<HintPath>..\..\ref\Gamecraft_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
</ItemGroup>

<!--Start Dependencies-->

+ 134
- 0
Kompressor/MyPlugin.cs View File

@@ -0,0 +1,134 @@
using System;
using System.Reflection;

//using UnityEngine;

using IllusionPlugin;
//using GamecraftModdingAPI;
using GamecraftModdingAPI.Commands;

namespace Kompressor
{
public class MyPlugin : IPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin'
{
public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name; // Kompressor by default
// To change the name, change the project's name

public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString(); // 0.0.1.0 by default
// To change the version, change <Version>0.0.1</Version> in Kompressor.csproj

private const string SETTINGS_PATH = "kompressor.json";

// called when Gamecraft shuts down
public void OnApplicationQuit()
{
// Shutdown this mod
GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has shutdown");

// Shutdown the Gamecraft modding API last
GamecraftModdingAPI.Main.Shutdown();
}

// called when Gamecraft starts up
public void OnApplicationStart()
{
// Initialize the Gamecraft modding API first
GamecraftModdingAPI.Main.Init();
// check out the modding API docs here: https://mod.exmods.org/

// reload command
CommandBuilder.Builder()
.Name("ReloadKompressor")
.Description("Reload Kompressor quality settings.")
.Action(() => { LoadAndApplySettings(); })
.Build();

// apply settings after the game starts up (but only once)
bool IsGamecraftStarted = false;
GamecraftModdingAPI.App.Client.EnterMenu += (_, __) =>
{
if (IsGamecraftStarted) return;
IsGamecraftStarted = true;
try
{
LoadAndApplySettings();
}
catch (Exception)
{
GamecraftModdingAPI.Utility.Logging.LogWarning("Failed to load kompressor.json, generated default file.");
}

};
GamecraftModdingAPI.Utility.Logging.MetaLog($"{Name} has started up");
}

internal void LoadAndApplySettings(bool suppress = false)
{
QualitySettings qs = QualitySettingsUtility.Default();
try
{
qs = QualitySettingsUtility.Load(SETTINGS_PATH);
}
catch (Exception e)
{
QualitySettingsUtility.Save(qs, SETTINGS_PATH);
if (!suppress) throw e;
}

ApplySettings(qs);
}

internal void ApplySettings(QualitySettings qs)
{
UnityEngine.QualitySettings.SetQualityLevel(qs.QualitySettingIndex, true);
// TODO
UnityEngine.QualitySettings.anisotropicFiltering = (UnityEngine.AnisotropicFiltering)System.Enum.Parse(typeof(UnityEngine.AnisotropicFiltering), qs.AnisotropicFiltering);
UnityEngine.QualitySettings.antiAliasing = qs.AntiAliasing;
UnityEngine.QualitySettings.asyncUploadBufferSize = qs.AsyncUploadBufferSize;
UnityEngine.QualitySettings.asyncUploadPersistentBuffer = qs.AsyncUploadPersistentBuffer;
UnityEngine.QualitySettings.asyncUploadTimeSlice = qs.AsyncUploadTimeSlice;
UnityEngine.QualitySettings.billboardsFaceCameraPosition = qs.BillBoardsFaceCameraPosition;
UnityEngine.QualitySettings.lodBias = qs.LODBias;
UnityEngine.QualitySettings.masterTextureLimit = qs.MasterTextureLimit;
UnityEngine.QualitySettings.maximumLODLevel = qs.MaximumLODLevel;
UnityEngine.QualitySettings.maxQueuedFrames = qs.MaxQueuedFrames;
UnityEngine.QualitySettings.particleRaycastBudget = qs.ParticleRaycastBudget;
UnityEngine.QualitySettings.pixelLightCount = qs.PixelLightCount;
UnityEngine.QualitySettings.realtimeReflectionProbes = qs.RealtimeReflectionProbes;
UnityEngine.QualitySettings.resolutionScalingFixedDPIFactor = qs.ResolutionScalingFixedDPIFactor;
UnityEngine.QualitySettings.shadowCascade2Split = qs.ShadowCascade2Split;
// TODO shadowCascade4Split
UnityEngine.QualitySettings.shadowCascades = qs.ShadowCascades;
UnityEngine.QualitySettings.shadowDistance = qs.ShadowDistance;
UnityEngine.QualitySettings.shadowmaskMode = (UnityEngine.ShadowmaskMode)Enum.Parse(typeof(UnityEngine.ShadowmaskMode), qs.ShadowmaskMode);
UnityEngine.QualitySettings.shadowNearPlaneOffset = qs.ShadowNearPlaneOffset;
UnityEngine.QualitySettings.shadowProjection = (UnityEngine.ShadowProjection)Enum.Parse(typeof(UnityEngine.ShadowProjection), qs.ShadowProjection);
UnityEngine.QualitySettings.shadowResolution = (UnityEngine.ShadowResolution)Enum.Parse(typeof(UnityEngine.ShadowResolution), qs.ShadowResolution);
UnityEngine.QualitySettings.shadows = (UnityEngine.ShadowQuality)Enum.Parse(typeof(UnityEngine.ShadowQuality), qs.Shadows);
UnityEngine.QualitySettings.skinWeights = (UnityEngine.SkinWeights)Enum.Parse(typeof(UnityEngine.SkinWeights), qs.SkinWeights);
UnityEngine.QualitySettings.softParticles = qs.SoftParticles;
UnityEngine.QualitySettings.softVegetation = qs.SoftVegetation;
UnityEngine.QualitySettings.streamingMipmapsActive = qs.StreamingMipmapsActive;
UnityEngine.QualitySettings.streamingMipmapsAddAllCameras = qs.StreamingMipmapsAddAllCameras;
UnityEngine.QualitySettings.streamingMipmapsMaxFileIORequests = qs.StreamingMipmapsMaxFileIORequests;
UnityEngine.QualitySettings.streamingMipmapsMaxLevelReduction = qs.StreamingMipmapsMaxLevelReduction;
UnityEngine.QualitySettings.streamingMipmapsMemoryBudget = qs.StreamingMipmapsMemoryBudget;
//UnityEngine.QualitySettings.streamingMipmapsRenderersPerFrame = qs.StreamingMipmapsRendersPerFrame;
UnityEngine.QualitySettings.vSyncCount = qs.VSyncCount;
UnityEngine.Application.targetFrameRate = qs.TargetFrameRate;

UnityEngine.Camera.main.fieldOfView = qs.FieldOfView;
}

// unused methods

public void OnFixedUpdate() { } // called once per physics update

public void OnLevelWasInitialized(int level) { } // called after a level is initialized

public void OnLevelWasLoaded(int level) { } // called after a level is loaded

public void OnUpdate() { } // called once per rendered frame (frame update)
}
}

+ 137
- 0
Kompressor/QualitySettings.cs View File

@@ -0,0 +1,137 @@
using System;
using System.IO;
using Newtonsoft.Json;

namespace Kompressor
{
public struct QualitySettings
{
public int QualitySettingIndex;

public string AnisotropicFiltering;

public int AntiAliasing;

public int AsyncUploadBufferSize;

public bool AsyncUploadPersistentBuffer;

public int AsyncUploadTimeSlice;

public bool BillBoardsFaceCameraPosition;

public float LODBias;

public int MasterTextureLimit;

public int MaximumLODLevel;

public int MaxQueuedFrames;

public int ParticleRaycastBudget;

public int PixelLightCount;

public bool RealtimeReflectionProbes;

public float ResolutionScalingFixedDPIFactor;

public float ShadowCascade2Split;

//public float[] ShadowCascade4Split;

public int ShadowCascades;

public float ShadowDistance;

public string ShadowmaskMode;

public float ShadowNearPlaneOffset;

public string ShadowProjection;

public string ShadowResolution;

public string Shadows;

public string SkinWeights;

public bool SoftParticles;

public bool SoftVegetation;

public bool StreamingMipmapsActive;

public bool StreamingMipmapsAddAllCameras;

public int StreamingMipmapsMaxFileIORequests;

public int StreamingMipmapsMaxLevelReduction;

public float StreamingMipmapsMemoryBudget;

//public int StreamingMipmapsRendersPerFrame;

public int VSyncCount;

public int TargetFrameRate;

public float FieldOfView;
}

public static class QualitySettingsUtility
{
internal static QualitySettings Load(string filepath)
{
string contents = File.ReadAllText(filepath);
return JsonConvert.DeserializeObject<QualitySettings>(contents);
}

internal static void Save(QualitySettings qs, string filepath)
{
string contents = JsonConvert.SerializeObject(qs);
File.WriteAllText(filepath, contents);
}

internal static QualitySettings Default()
{
QualitySettings qs = new QualitySettings
{
AnisotropicFiltering = "Disable",
AntiAliasing = 0,
AsyncUploadBufferSize = 16,
AsyncUploadPersistentBuffer = true,
AsyncUploadTimeSlice = 2,
BillBoardsFaceCameraPosition = false,
LODBias = 0.3f,
MasterTextureLimit = 3,
MaximumLODLevel = 0,
MaxQueuedFrames = 2,
ParticleRaycastBudget = 4,
PixelLightCount = 0,
RealtimeReflectionProbes = false,
ResolutionScalingFixedDPIFactor = 1,
ShadowCascade2Split = 1/3,
ShadowCascades = 1,
ShadowDistance = 22,
ShadowmaskMode = "Shadowmask",
ShadowNearPlaneOffset = 3,
ShadowProjection = "StableFit",
ShadowResolution = "Low",
Shadows = "Disable",
SkinWeights = "OneBone",
SoftParticles = false,
SoftVegetation = false,
StreamingMipmapsActive = false,
StreamingMipmapsAddAllCameras = true,
StreamingMipmapsMaxFileIORequests = 1024,
StreamingMipmapsMaxLevelReduction = 4,
StreamingMipmapsMemoryBudget = 512,
VSyncCount = 0,
TargetFrameRate = -1,
FieldOfView = 60,
};
return qs;
}
}
}