initial config loading system, shadows cvar, assetmanager

This commit is contained in:
2022-06-12 18:49:19 +03:00
parent 81d754c865
commit ba1688ebbf
18 changed files with 168 additions and 38 deletions

Binary file not shown.

Binary file not shown.

2
Content/config.cfg Normal file
View File

@@ -0,0 +1,2 @@
// comment
r_lighting 0

View File

@@ -220,8 +220,7 @@ namespace Game
AudioInfo audio = new AudioInfo();
string workDir = Directory.GetCurrentDirectory();
string audioBasePath = Path.Combine(workDir, "Content", "Audio");
string audioBasePath = Path.Combine(AssetManager.ContentPath, "Audio");
AudioClip audioClip = Content.Load<AudioClip>(Path.Combine(audioBasePath, soundName + ".flax"));
if (audioClip != null)
{

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
namespace Game
{
public class Config
{
private Dictionary<string, string> dictionary = new Dictionary<string, string>();
public Config()
{
}
public string this[string key]
{
get => dictionary[key];
set => dictionary[key] = value;
}
public string[] GetLines()
{
string[] lines = new string[dictionary.Count];
int lineIndex = 0;
foreach (var kvp in dictionary)
{
lines[lineIndex] = $"{kvp.Key} {kvp.Value}";
lineIndex++;
}
return lines;
}
}
}

View File

@@ -0,0 +1,39 @@
using System.IO;
namespace Game
{
public class ConfigParser
{
private ConfigParser()
{
}
public static Config ParseFile(string path)
{
Config config = new Config();
using FileStream file = File.OpenRead(path);
using StreamReader sr = new StreamReader(file);
string line;
while ((line = sr.ReadLine()?.Trim()) != null)
{
if (line.StartsWith(@"//"))
continue;
int spacePos = line.IndexOf(' ');
if (spacePos == -1)
continue;
string key = line.Substring(0, spacePos);
string value = line.Substring(spacePos+1);
value = value.Trim('"');
config[key] = value;
}
return config;
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq;
using System.Reflection;
using FlaxEditor;
using FlaxEngine;
namespace Game
{
@@ -89,6 +90,12 @@ namespace Game
ScriptsBuilder.ScriptsReload += Destroy;
Editor.Instance.PlayModeEnd += OnEditorPlayModeChanged;
#endif
AssetManager.Init(); // TODO: move these elsewhere
AssetManager.Globals.ResetValues();
foreach (var line in AssetManager.Config.GetLines())
Console.Execute(line, false, true);
}
public static void Destroy()
@@ -108,6 +115,7 @@ namespace Game
#if FLAX_EDITOR
private static void OnEditorPlayModeChanged()
{
//AssetManager.Globals.ResetValues();
// Clear console buffer when leaving play mode
if (instance != null)
Clear();
@@ -173,9 +181,9 @@ namespace Game
instance.Clear();
}
public static void Execute(string str, bool bufferInput = false)
public static void Execute(string str, bool bufferInput = false, bool noOutput = false)
{
instance.Execute(str, bufferInput);
instance.Execute(str, bufferInput, noOutput);
}
public static string GetVariable(string variableName)
@@ -455,14 +463,14 @@ namespace Game
consoleLines.Clear();
}
public void Execute(string str, bool bufferInput = false)
public void Execute(string str, bool bufferInput = false, bool noOutput = false)
{
if (bufferInput)
consoleBufferHistory.Insert(0, str);
str = str.Trim();
if (ShowExecutedLines)
if (ShowExecutedLines && !noOutput)
Console.Print(LinePrefix + str);
string[] strs = str.Split(' ');
@@ -488,7 +496,8 @@ namespace Game
if (value != null)
cvar.SetValue(value);
Console.Print("'" + execute + "' is '" + cvar.GetValueString() + "'");
//if (!noOutput)
Console.Print("'" + execute + "' is '" + cvar.GetValueString() + "'");
}
else
{

View File

@@ -195,29 +195,17 @@ namespace Game
{
get
{
string workDir = Directory.GetCurrentDirectory();
string matBasePath = Path.Combine(workDir, "Content");
string assetPath = Path.Combine(matBasePath, "GameplayGlobals.flax");
GameplayGlobals globals = Content.Load<GameplayGlobals>(assetPath);
return ((bool)globals.GetValue("Scene Lighting") ? "1" : "0");
return ((bool)AssetManager.Globals.GetValue("Scene Lighting") ? "1" : "0");
}
set
{
string workDir = Directory.GetCurrentDirectory();
string matBasePath = Path.Combine(workDir, "Content");
string assetPath = Path.Combine(matBasePath, "GameplayGlobals.flax");
GameplayGlobals globals = Content.Load<GameplayGlobals>(assetPath);
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
globals.SetValue("Scene Lighting", boolValue);
AssetManager.Globals.SetValue("Scene Lighting", boolValue);
PostFxVolume postFx = Level.FindActor<PostFxVolume>();
@@ -234,10 +222,26 @@ namespace Game
}
// TODO: disable GI
Graphics.EnableGlobalSDF = boolValue;
}
}
Light[] lights = Level.GetActors<Light>();
foreach (Light light in lights)
light.IsActive = boolValue;
[ConsoleVariable("r_shadows")]
public static string SceneShadows
{
get
{
return ((bool)AssetManager.Globals.GetValue("Scene Shadows") ? "1" : "0");
}
set
{
bool boolValue = false;
if (int.TryParse(value, out int intValue))
boolValue = intValue != 0;
else if (float.TryParse(value, out float valueFloat))
boolValue = valueFloat != 0f;
AssetManager.Globals.SetValue("Scene Shadows", boolValue);
}
}

View File

@@ -384,8 +384,7 @@ namespace Game
spawned = true;
string workDir = Directory.GetCurrentDirectory();
string prefabPath = Path.Combine(workDir, "Content", "Common");
string prefabPath = Path.Combine(AssetManager.ContentPath, "Common");
var playerPrefab = Content.Load<Prefab>(Path.Combine(prefabPath, "PlayerPrefab.prefab"));
if (playerPrefab == null)
Console.PrintError("GameModeManager: Failed to find PlayerPrefab");

View File

@@ -49,6 +49,7 @@ namespace Game
private Model model;
private MaterialBase missingMaterial;
private bool resetLights = false;
private bool dirtyLights = false;
private float brightnessMultiplier_ = 0.82f;
@@ -62,28 +63,28 @@ namespace Game
public float BrightnessMultiplier
{
get => brightnessMultiplier_;
set { brightnessMultiplier_ = value; dirtyLights = true; }
set { brightnessMultiplier_ = value; resetLights = true; }
}
[Range(0.1f, 40f)]
public float LightRadiusMultiplier
{
get => lightRadiusMultiplier_;
set { lightRadiusMultiplier_ = value; dirtyLights = true; }
set { lightRadiusMultiplier_ = value; resetLights = true; }
}
[Range(2f, 8f)]
public float FallOffExponent
{
get => fallOffExponent_;
set { fallOffExponent_ = value; dirtyLights = true; }
set { fallOffExponent_ = value; resetLights = true; }
}
[Range(0.01f, 1f)]
public float SaturationMultiplier
{
get => saturationMultiplier_;
set { saturationMultiplier_ = value; dirtyLights = true; }
set { saturationMultiplier_ = value; resetLights = true; }
}
@@ -249,6 +250,7 @@ namespace Game
{
if (worldSpawnActor != null)
worldSpawnActor.HideFlags |= HideFlags.DontSave;
dirtyLights = true;
}
catch (Exception e)
{
@@ -274,9 +276,24 @@ namespace Game
LoadMap(false);
}
private bool lastSceneLighting = false;
private bool lastSceneShadows = false;
public override void OnUpdate()
{
if (dirtyLights)
bool sceneLighting = EngineSubsystem.SceneLighting == "1";
if (lastSceneLighting != sceneLighting)
{
lastSceneLighting = sceneLighting;
dirtyLights = true;
}
bool sceneShadows = EngineSubsystem.SceneShadows == "1";
if (lastSceneShadows != sceneShadows)
{
lastSceneShadows = sceneShadows;
dirtyLights = true;
}
if (resetLights)
{
if (worldSpawnActor == null || !worldSpawnActor || root == null)
return;
@@ -297,6 +314,18 @@ namespace Game
// break;
}
resetLights = false;
}
if (dirtyLights)
{
foreach (var light in worldSpawnActor.GetChildren<Light>())
{
light.IsActive = sceneLighting;
if (light is PointLight pointLight)
pointLight.ShadowsStrength = sceneShadows ? 1.0f : 0.0f;
}
dirtyLights = false;
}
}
@@ -314,7 +343,7 @@ namespace Game
else
{
FlaxEngine.Debug.Log("Map already loaded in the scene");
dirtyLights = false;
resetLights = false;
return;
}
}
@@ -322,8 +351,7 @@ namespace Game
FlaxEngine.Debug.Log("Loading map");
{
string workDir = Directory.GetCurrentDirectory();
string matBasePath = Path.Combine(workDir, "Content", "Materials");
string matBasePath = Path.Combine(AssetManager.ContentPath, "Materials");
string assetPath = Path.Combine(matBasePath, "missing.flax");
missingMaterial = Content.Load<MaterialBase>(assetPath);
}
@@ -395,8 +423,7 @@ namespace Game
if (!materials.TryGetValue(textureName, out MaterialBase brushMaterial))
{
string workDir = Directory.GetCurrentDirectory();
string matBasePath = Path.Combine(workDir, "Content", "Materials");
string matBasePath = Path.Combine(AssetManager.ContentPath, "Materials");
string assetPath = Path.Combine(matBasePath, textureName + ".flax");
brushMaterial = Content.Load<MaterialBase>(assetPath);
if (brushMaterial != null)
@@ -768,8 +795,7 @@ namespace Game
childModel.Model = model;
//childModel.SetMaterial(0, missingMaterial);
string workDir = Directory.GetCurrentDirectory();
string matBasePath = Path.Combine(workDir, "Content", "Materials");
string matBasePath = Path.Combine(AssetManager.ContentPath, "Materials");
string assetPath = Path.Combine(matBasePath, "dev/dev_128_gray" + ".flax");
var brushMaterial = Content.Load<MaterialBase>(assetPath);
if (brushMaterial != null)

View File

@@ -0,0 +1,20 @@
using System.IO;
using FlaxEngine;
namespace Game
{
public static class AssetManager
{
public static string ContentPath { get; private set; } =
Path.Combine(Directory.GetCurrentDirectory(), "Content");
public static GameplayGlobals Globals { get; private set; }
public static Config Config { get; private set; }
public static void Init()
{
Globals = Content.Load<GameplayGlobals>(Path.Combine(ContentPath, "Settings", "GameSettings", "GameplayGlobals.flax"));
Config = ConfigParser.ParseFile(Path.Combine(ContentPath, "config.cfg"));
}
}
}