Skip to content

Commit

Permalink
Mod config (#48)
Browse files Browse the repository at this point in the history
* mod config
  • Loading branch information
amazingalek authored Jan 5, 2020
1 parent 596ff69 commit 688aa4d
Show file tree
Hide file tree
Showing 33 changed files with 260 additions and 73 deletions.
8 changes: 8 additions & 0 deletions OWML.Common/IModBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OWML.Common
{
public interface IModBehaviour
{
IModHelper ModHelper { get; }
void Configure(IModConfig config);
}
}
13 changes: 5 additions & 8 deletions OWML.Common/IModConfig.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
namespace OWML.Common
using System.Collections.Generic;

namespace OWML.Common
{
public interface IModConfig
{
string GamePath { get; set; }
string ManagedPath { get; }
string OWMLPath { get; }
string ModsPath { get; }
string OutputFilePath { get; }
string LogFilePath { get; }
bool Verbose { get; }
Dictionary<string, object> Settings { get; }
T GetSetting<T>(string key);
}
}
2 changes: 1 addition & 1 deletion OWML.Common/IModHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{
public interface IModHelper
{
IModConfig Config { get; }
IModLogger Logger { get; }
IModConsole Console { get; }
IModEvents Events { get; }
Expand All @@ -11,5 +10,6 @@ public interface IModHelper
IModStorage Storage { get; }
IModMenus Menus { get; }
IModManifest Manifest { get; }
IModConfig Config { get; }
}
}
13 changes: 13 additions & 0 deletions OWML.Common/IOwmlConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OWML.Common
{
public interface IOwmlConfig
{
string GamePath { get; set; }
string ManagedPath { get; }
string OWMLPath { get; }
string ModsPath { get; }
string OutputFilePath { get; }
string LogFilePath { get; }
bool Verbose { get; }
}
}
2 changes: 2 additions & 0 deletions OWML.Common/OWML.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@
<Compile Include="IModAssets.cs" />
<Compile Include="IModAsset.cs" />
<Compile Include="IModConfig.cs" />
<Compile Include="IOwmlConfig.cs" />
<Compile Include="IModEvents.cs" />
<Compile Include="IModFinder.cs" />
<Compile Include="IModHelper.cs" />
<Compile Include="IModConsole.cs" />
<Compile Include="IModBehaviour.cs" />
<Compile Include="IModMenu.cs" />
<Compile Include="IModMenus.cs" />
<Compile Include="IModLogger.cs" />
Expand Down
8 changes: 4 additions & 4 deletions OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace OWML.Launcher
{
public class App
{
private const string Version = "0.3.11";
private const string Version = "0.3.13";

private readonly IModConfig _config;
private readonly IOwmlConfig _config;
private readonly IModConsole _writer;
private readonly IModFinder _modFinder;
private readonly OutputListener _listener;
private readonly PathFinder _pathFinder;

public App(IModConfig config, IModConsole writer, IModFinder modFinder, OutputListener listener, PathFinder pathFinder)
public App(IOwmlConfig config, IModConsole writer, IModFinder modFinder, OutputListener listener, PathFinder pathFinder)
{
_config = config;
_writer = writer;
Expand Down Expand Up @@ -85,7 +85,7 @@ private void ShowModList()
foreach (var manifest in manifests)
{
var stateText = manifest.Enabled ? "" : " (disabled)";
var versionText = manifest.OWMLVersion == Version ? "" : $" (Warning: made for other version of OWML: {manifest.OWMLVersion})";
var versionText = manifest.OWMLVersion == Version ? "" : $" (warning: made for OWML {manifest.OWMLVersion})";
_writer.WriteLine($"* {manifest.UniqueName} ({manifest.Version}){stateText}{versionText}");
}
}
Expand Down
4 changes: 2 additions & 2 deletions OWML.Launcher/OWML.Config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"gamePath": "C:/Program Files (x86)/Outer Wilds",
"verbose": false
"gamePath": "C:/Program Files/Epic Games/OuterWilds",
"verbose": false
}
4 changes: 2 additions & 2 deletions OWML.Launcher/OutputListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class OutputListener

public Action<string> OnOutput;

private readonly IModConfig _config;
private readonly IOwmlConfig _config;
private FileStream _reader;

public OutputListener(IModConfig config)
public OutputListener(IOwmlConfig config)
{
_config = config;
}
Expand Down
4 changes: 2 additions & 2 deletions OWML.Launcher/PathFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace OWML.Launcher
{
public class PathFinder
{
private readonly IModConfig _config;
private readonly IOwmlConfig _config;
private readonly IModConsole _writer;

public PathFinder(IModConfig config, IModConsole writer)
public PathFinder(IOwmlConfig config, IModConsole writer)
{
_config = config;
_writer = writer;
Expand Down
4 changes: 2 additions & 2 deletions OWML.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ static void Main(string[] args)
app.Run();
}

private static IModConfig GetConfig()
private static IOwmlConfig GetConfig()
{
var json = File.ReadAllText("OWML.Config.json")
.Replace("\\", "/");
var config = JsonConvert.DeserializeObject<ModConfig>(json);
var config = JsonConvert.DeserializeObject<OwmlConfig>(json);
config.OWMLPath = AppDomain.CurrentDomain.BaseDirectory;
return config;
}
Expand Down
6 changes: 3 additions & 3 deletions OWML.ModHelper.Menus/ModMainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ public Button AddButton(string name, int index)

var copy = GameObject.Instantiate(original, _menu.transform);
copy.name = name;
copy.transform.SetSiblingIndex(index);
copy.transform.SetSiblingIndex(index + 2);

GameObject.Destroy(copy.GetComponentInChildren<LocalizedText>());
GameObject.Destroy(copy.GetComponent<SubmitAction>());

copy.GetComponentInChildren<Text>().text = name;

var fadeController = new CanvasGroupFadeController
var fadeController = new CanvasGroupFadeController
{
group = copy.GetComponent<CanvasGroup>()
};
_fadeControllers.Insert(index, fadeController);
_anim.SetValue("_buttonFadeControllers", _fadeControllers.ToArray());

return copy;
}

Expand Down
4 changes: 2 additions & 2 deletions OWML.ModHelper.Menus/ModPauseMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Button AddButton(string name, int index)

var copy = GameObject.Instantiate(original, _layoutGroup.transform);
copy.name = name;
copy.transform.SetSiblingIndex(index);
copy.transform.SetSiblingIndex(index + 2);

GameObject.Destroy(copy.GetComponentInChildren<LocalizedText>());
GameObject.Destroy(copy.GetComponent<SubmitAction>());
Expand All @@ -63,7 +63,7 @@ public Button AddButton(string name, int index)

private void Initialize()
{
_console.WriteLine("Trying to initialize pause menu");
_logger.Log("Trying to initialize pause menu");
var pauseMenuManager = GameObject.FindObjectOfType<PauseMenuManager>();
if (pauseMenuManager == null)
{
Expand Down
59 changes: 59 additions & 0 deletions OWML.ModHelper.Menus/ModsMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Collections.Generic;
using OWML.Common;
using UnityEngine;
using UnityEngine.UI;

namespace OWML.ModHelper.Menus
{
public class ModsMenu : IModMenu
{
private readonly IModLogger _logger;
private readonly IModConsole _console;
private readonly List<IModBehaviour> _registeredMods;

public ModsMenu(IModLogger logger, IModConsole console)
{
_logger = logger;
_console = console;
_registeredMods = new List<IModBehaviour>();
}

public void Register(IModBehaviour modBehaviour)
{
_console.WriteLine("Registering " + modBehaviour.ModHelper.Manifest.UniqueName);
_registeredMods.Add(modBehaviour);
var button = AddButton(modBehaviour.ModHelper.Manifest.UniqueName, 0);
button.onClick.AddListener(() =>
{
var config = ParseConfig(button);
modBehaviour.ModHelper.Storage.Save(config, "config.json");
modBehaviour.Configure(config);
});
}

private IModConfig ParseConfig(Button button)
{
_console.WriteLine("todo: implement ModsMenu.ParseConfig");
return new ModConfig();
}

public Button AddButton(string name, int index)
{
_console.WriteLine("todo: implement ModsMenu.AddButton");
var go = new GameObject();
return go.AddComponent<Button>(); // todo
}

public List<Button> GetButtons()
{
_console.WriteLine("todo: implement ModsMenu.GetButtons");
return new List<Button>();
}

public void Open()
{
_console.WriteLine("todo: implement ModsMenu.Open");
}

}
}
5 changes: 5 additions & 0 deletions OWML.ModHelper.Menus/OWML.ModHelper.Menus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ModsMenu.cs" />
<Compile Include="ModPauseMenu.cs" />
<Compile Include="ModMenus.cs" />
<Compile Include="ModMainMenu.cs" />
Expand All @@ -80,6 +81,10 @@
<Project>{B771615E-6B51-44F8-B862-D7543C12C0FF}</Project>
<Name>OWML.ModHelper.Events</Name>
</ProjectReference>
<ProjectReference Include="..\OWML.ModHelper\OWML.ModHelper.csproj">
<Project>{cb57bab8-d70e-4fce-9bf1-328a924173a7}</Project>
<Name>OWML.ModHelper</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
8 changes: 7 additions & 1 deletion OWML.ModHelper/ModBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

namespace OWML.ModHelper
{
public class ModBehaviour : MonoBehaviour
public class ModBehaviour : MonoBehaviour, IModBehaviour
{
public IModHelper ModHelper { get; private set; }

public void Init(IModHelper modHelper)
{
ModHelper = modHelper;
Configure(modHelper.Config);
DontDestroyOnLoad(gameObject);
}

public virtual void Configure(IModConfig config)
{
}

}
}
34 changes: 16 additions & 18 deletions OWML.ModHelper/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
using OWML.Common;

namespace OWML.ModHelper
{
public class ModConfig : IModConfig
{
[JsonProperty("gamePath")]
public string GamePath { get; set; }
[JsonProperty("settings")]
public Dictionary<string, object> Settings { get; set; }

[JsonProperty("verbose")]
public bool Verbose { get; private set; }
public ModConfig()
{
Settings = new Dictionary<string, object>();
}

[JsonIgnore]
public string ManagedPath => $"{GamePath}/OuterWilds_Data/Managed";
public T GetSetting<T>(string key)
{
if (Settings.ContainsKey(key))
{
return (T)Settings[key];
}
return default;
}

[JsonProperty("owmlPath")]
public string OWMLPath { get; set; }

[JsonIgnore]
public string LogFilePath => $"{OWMLPath}Logs/OWML.Log.txt";

[JsonIgnore]
public string OutputFilePath => $"{OWMLPath}Logs/OWML.Output.txt";

[JsonIgnore]
public string ModsPath => $"{OWMLPath}Mods";
}
}
2 changes: 1 addition & 1 deletion OWML.ModHelper/ModConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ModConsole : IModConsole
private readonly FileStream _writer;
private readonly IModLogger _logger;

public ModConsole(IModConfig config, IModLogger logger)
public ModConsole(IOwmlConfig config, IModLogger logger)
{
Instance = this;
_logger = logger;
Expand Down
7 changes: 4 additions & 3 deletions OWML.ModHelper/ModHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace OWML.ModHelper
{
public class ModHelper : IModHelper
{
public IModConfig Config { get; }
public IModLogger Logger { get; }
public IModConsole Console { get; }
public IHarmonyHelper HarmonyHelper { get; }
Expand All @@ -14,10 +13,11 @@ public class ModHelper : IModHelper
public IModStorage Storage { get; }
public IModMenus Menus { get; }
public IModManifest Manifest { get; }
public IModConfig Config { get; }

public ModHelper(IModConfig config, IModLogger logger, IModConsole console, IHarmonyHelper harmonyHelper, IModEvents events, IModAssets assets, IModStorage storage, IModMenus menus, IModManifest manifest)
public ModHelper(IModLogger logger, IModConsole console, IHarmonyHelper harmonyHelper, IModEvents events,
IModAssets assets, IModStorage storage, IModMenus menus, IModManifest manifest, IModConfig config)
{
Config = config;
Logger = logger;
Console = console;
HarmonyHelper = harmonyHelper;
Expand All @@ -26,6 +26,7 @@ public ModHelper(IModConfig config, IModLogger logger, IModConsole console, IHar
Storage = storage;
Menus = menus;
Manifest = manifest;
Config = config;
}

}
Expand Down
4 changes: 2 additions & 2 deletions OWML.ModHelper/ModLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class ModLogger : IModLogger
{
public static ModLogger Instance { get; private set; }

private readonly IModConfig _config;
private readonly IOwmlConfig _config;

public ModLogger(IModConfig config)
public ModLogger(IOwmlConfig config)
{
Instance = this;
_config = config;
Expand Down
5 changes: 5 additions & 0 deletions OWML.ModHelper/ModStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public T Load<T>(string filename)
{
var path = _manifest.FolderPath + filename;
_logger.Log($"Loading {path}...");
if (!File.Exists(path))
{
_logger.Log("File not found: " + path);
return default;
}
try
{
var json = File.ReadAllText(path);
Expand Down
Loading

0 comments on commit 688aa4d

Please sign in to comment.