Skip to content

Commit

Permalink
Directories Config
Browse files Browse the repository at this point in the history
- new configuration available for the config.json for all mods
- it is possible to define directories as optional and connect it to a condition like a mod variable or an installed mod
- it is possible to define more than one Data directory and connect them to a condition like a mod variable or an installed mod
  • Loading branch information
cech12 committed May 16, 2019
1 parent de0110c commit 6b49ee4
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 13 deletions.
4 changes: 4 additions & 0 deletions DigglesModManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<Compile Include="Mod.cs" />
<Compile Include="Model\AppSettings.cs" />
<Compile Include="Model\ModConfig.cs" />
<Compile Include="Model\ModDirectory.cs" />
<Compile Include="Model\ModDirectoryType.cs" />
<Compile Include="Model\ModDirectroyCondition.cs" />
<Compile Include="Model\ModDirectroyConditionType.cs" />
<Compile Include="Model\ModLinks.cs" />
<Compile Include="Model\ModTranslationString.cs" />
<Compile Include="Model\ModSettingsVariable.cs" />
Expand Down
3 changes: 1 addition & 2 deletions FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ private void button_mod_Click(object sender, EventArgs e)
var error = false;
foreach (var mod in _activeMods)
{
var modDir = new DirectoryInfo(Paths.ModPath + "\\" + Paths.ModDirectoryName + "\\" + mod.ModDirectoryName);
var returnValue = _moddingService.LetsMod(mod, modDir, new DirectoryInfo(Paths.ExePath), _activeMods, _progressBarManipulator);
var returnValue = _moddingService.LetsMod(mod, mod.ModDirectoryInfo, new DirectoryInfo(Paths.ExePath), _activeMods, _progressBarManipulator);
warning = warning || returnValue == ModdingService.WARNING_CODE;
error = error || returnValue == ModdingService.ERROR_CODE;
//cancel when error occured
Expand Down
19 changes: 17 additions & 2 deletions Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,34 @@ class Log
private const string ERROR = "ERROR";
private const string WARNING = "WARNING";

private static void LogToErrorFile(string type, string message, FileInfo fileInfo, int line)
private static void LogToErrorFile(string text)
{
var writer = new StreamWriter(Paths.ExePath + "\\" + Paths.ErrorLogFileName, true, Encoding.Default);
writer.WriteLine(DateTime.Now + " - " + type + " in \"" + fileInfo.FullName + "\" at line " + line + ": " + message);
writer.WriteLine(DateTime.Now + " - " + text);
writer.Flush();
writer.Close();
}

private static void LogToErrorFile(string type, string message, FileInfo fileInfo, int line)
{
LogToErrorFile(type + " in \"" + fileInfo.FullName + "\" at line " + line + ": " + message);
}

public static void Error(string message)
{
LogToErrorFile(ERROR + " " + message);
}

public static void Error(string message, FileInfo fileInfo, int line)
{
LogToErrorFile(ERROR, message, fileInfo, line);
}

public static void Warning(string message)
{
LogToErrorFile(WARNING + " " + message);
}

public static void Warning(string message, FileInfo fileInfo, int line)
{
LogToErrorFile(WARNING, message, fileInfo, line);
Expand Down
13 changes: 9 additions & 4 deletions Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class Mod : IToolTipDisplayer, IComparable
{
public string ModDirectoryName { get; private set; }

public string ModDirectoryPath { get; private set; }

public DirectoryInfo ModDirectoryInfo { get; private set; }

public ModConfig Config { get; private set; }

public string ToolTipText { get; private set; }
Expand All @@ -28,15 +32,16 @@ public class Mod : IToolTipDisplayer, IComparable
public Mod(string modDirectory, Dictionary<string, object> oldSettings)
{
ModDirectoryName = modDirectory;

ModDirectoryPath = Paths.ModPath + "\\" + Paths.ModDirectoryName + "\\" + ModDirectoryName + "\\";
ModDirectoryInfo = new DirectoryInfo(ModDirectoryPath);
FileCount = ModDirectoryInfo.GetFiles("*", SearchOption.AllDirectories).Length;

//get description
var modDirectoryInfo = new DirectoryInfo(Paths.ExePath + "\\" + Paths.ModDirectoryName + "\\" + ModDirectoryName);
FileCount = modDirectoryInfo.GetFiles("*", SearchOption.AllDirectories).Length;
ToolTipText = "";
Author = "";

//test for config file and read it
var modFiles = modDirectoryInfo.GetFiles();
var modFiles = ModDirectoryInfo.GetFiles();

foreach (var modFile in modFiles)
{
Expand Down
6 changes: 6 additions & 0 deletions Model/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,11 @@ public class ModConfig
/// </summary>
[JsonProperty(PropertyName = "settings")]
public List<ModSettingsVariable> SettingsVariables { get; set; } = new List<ModSettingsVariable>();

/// <summary>
/// A list of directories, that are handled in a different way.
/// </summary>
[JsonProperty(PropertyName = "directories")]
public List<ModDirectory> Directories { get; set; } = new List<ModDirectory>();
}
}
41 changes: 41 additions & 0 deletions Model/ModDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Newtonsoft.Json;
using System.ComponentModel;

namespace DigglesModManager.Model
{
public class ModDirectory
{
/// <summary>
/// The type of the directory. [REQUIRED]
/// </summary>
[JsonProperty(PropertyName = "type", Required = Required.Always)]
public ModDirectoryType Type { get; set; }

/// <summary>
/// Path of directory relative to mod root. [REQUIRED]
/// </summary>
[JsonProperty(PropertyName = "path", Required = Required.Always)]
public string Path
{
get
{
return path;
}
set
{
//replace backslashes with slashes
path = value.Replace("\\", "/");
}
}

private string path;

/// <summary>
/// Condition that has to be true to add this directory.
/// </summary>
[DefaultValue(null)]
[JsonProperty(PropertyName = "condition", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.Populate)]
public ModDirectroyCondition Condition { get; set; }

}
}
11 changes: 11 additions & 0 deletions Model/ModDirectoryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace DigglesModManager.Model
{
/// <summary>
/// The available and possible types of mod directories.
/// </summary>
public enum ModDirectoryType
{
Data, Optional
}

}
66 changes: 66 additions & 0 deletions Model/ModDirectroyCondition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel;

namespace DigglesModManager.Model
{
public class ModDirectroyCondition
{
/// <summary>
/// The type of the directory condition. [REQUIRED]
/// </summary>
[JsonProperty(PropertyName = "type", Required = Required.Always)]
public ModDirectroyConditionType Type { get; set; }

/// <summary>
/// ID of the variable or mod that has to be checked. [REQUIRED]
/// (Type Mod: mod directory name; Type Variable: mod variable id)
/// </summary>
[JsonProperty(PropertyName = "id", Required = Required.Always)]
public string Id { get; set; }

/// <summary>
/// The variable has to have this value to evaluate the condtion as true.
/// (required for type "variable")
/// </summary>
[DefaultValue(null)]
[JsonProperty(PropertyName = "value", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.Populate)]
public object Value { get; set; }

public bool isTrue(Mod mod, List<Mod> activeMods)
{
switch (Type)
{
case ModDirectroyConditionType.Mod:
foreach (var activeMod in activeMods)
{
if (activeMod.ModDirectoryName.Equals(this.Id))
{
return true;
}
}
//no warning, because the existence of a mod is checked.
break;
case ModDirectroyConditionType.Variable:
if (this.Value == null)
{
Log.Warning(mod.ModDirectoryName + ": Variable with ID \"" + this.Id + "\" needs a value.");
break;
}
foreach (var modVariable in mod.Config.SettingsVariables)
{
if (modVariable.ID.Equals(this.Id))
{
return modVariable.Value.Equals(this.Value);
}
}
Log.Warning(mod.ModDirectoryName + ": Variable with ID \"" + this.Id + "\" not found");
break;
default:
Log.Warning(mod.ModDirectoryName + ": Unhandled ModDirectroyConditionType \"" + Type + "\"");
break;
}
return false;
}
}
}
7 changes: 7 additions & 0 deletions Model/ModDirectroyConditionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DigglesModManager.Model
{
public enum ModDirectroyConditionType
{
Mod, Variable
}
}
1 change: 1 addition & 0 deletions Paths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Paths
public static string ModPath = ExePath; //dyn: ExePath | local: @"D:\Projekte\DigglesMods\DigglesModManager"
public static string ModDirectoryName = "Mods";
public static string[] DigglesExecutableNames = { "Diggles.exe", "Wiggles.exe" };
public static string DataPath = ExePath + "\\Data";

//.dm
public static string RestoreFileName = "restore.dm";
Expand Down
61 changes: 56 additions & 5 deletions Service/ModdingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,14 +482,65 @@ public int LetsMod(Mod mod, DirectoryInfo modDirectory, DirectoryInfo gameDirect
continue;
}

//search for game directory
DirectoryInfo rightGameDir = null;
foreach (var gameDir in gameDirectories)

//check for mod directories, that are handled in a different way
if (mod.Config.Directories.Count > 0)
{
if (gameDir.Name == modDir.Name)
var relativePath = modDir.FullName.Replace(mod.ModDirectoryPath, "");
//replace all backslashes with slashes
relativePath = relativePath.Replace("\\", "/");
var skipDirectory = false;
foreach (var directory in mod.Config.Directories)
{
rightGameDir = gameDir;
break;
if (directory.Path.Equals(relativePath))
{
// if this is a special directory, check the condition
if (directory.Condition != null && !directory.Condition.isTrue(mod, activeMods))
{
//if a condition exists and is false, skip this directory
skipDirectory = true;
break;
}
// if the condition is true or no condition exists
switch (directory.Type)
{
case ModDirectoryType.Data:
//set the data directory as game directory
rightGameDir = new DirectoryInfo(Paths.DataPath);
break;
case ModDirectoryType.Optional:
if (directory.Condition != null)
{
Log.Warning(mod.ModDirectoryName + ": The optional directory " + directory.Path + " should have a condition");
returnValue = WARNING_CODE;
}
//do nothing, because it is now a normal directory
break;
default:
Log.Warning(mod.ModDirectoryName + ": Unhandled ModDirectoryType \"" + directory.Type + "\"");
returnValue = WARNING_CODE;
break;
}
break;
}
}
if (skipDirectory)
{
continue;
}
}

if (rightGameDir == null)
{
//search for game directory
foreach (var gameDir in gameDirectories)
{
if (gameDir.Name == modDir.Name)
{
rightGameDir = gameDir;
break;
}
}
}
//if game directory does not exist, create it
Expand Down

0 comments on commit 6b49ee4

Please sign in to comment.