Skip to content

Commit

Permalink
Fix tag parsing; add process filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Feb 25, 2020
1 parent b9e6026 commit 19a9bf6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
11 changes: 6 additions & 5 deletions ScriptLoader/ScriptInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace ScriptLoader
{
Expand All @@ -12,20 +13,22 @@ internal class ScriptInfo
["name"] = (si, c) => si.Name = c,
["author"] = (si, c) => si.Author = c,
["desc"] = (si, c) => si.Description = c,
["ref"] = (si, c) => si.References.Add(c.Template(Utilities.KnownPaths))
["ref"] = (si, c) => si.References.Add(c.Template(Utilities.KnownPaths)),
["proc_filter"] = (si, c) => si.ProcessFilters.Add(c)
};

public string Author { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<string> References { get; set; } = new List<string>();
public List<string> ProcessFilters { get; set; } = new List<string>();

public static ScriptInfo FromTextFile(string path)
{
using (var tw = File.OpenText(path))
{
string line;
ScriptInfo si = null;
var si = new ScriptInfo();
while ((line = tw.ReadLine()) != null)
{
line = line.Trim();
Expand All @@ -41,11 +44,9 @@ public static ScriptInfo FromTextFile(string path)
if (nextSpace < 0)
continue;

var command = line.Substring(1, nextSpace);
var command = line.Substring(1, nextSpace - 1);
var value = line.Substring(nextSpace).Trim();

if (si == null)
si = new ScriptInfo();
if (CommandParsers.TryGetValue(command, out var parser))
parser(si, value);
}
Expand Down
18 changes: 14 additions & 4 deletions ScriptLoader/ScriptLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace ScriptLoader
{
[BepInPlugin("horse.coder.tools.scriptloader", "C# Script Loader", "1.2")]
[BepInPlugin("horse.coder.tools.scriptloader", "C# Script Loader", "1.2.1")]
public class ScriptLoader : BaseUnityPlugin
{
private readonly string scriptsPath = Path.Combine(Paths.GameRootPath, "scripts");
Expand Down Expand Up @@ -82,11 +82,21 @@ private void CompileScripts()
if (!File.Exists(ignoresPath))
File.WriteAllText(ignoresPath, "");

bool IsValidProcess(string scriptFile)
{
var si = availableScripts[scriptFile];

if (si.ProcessFilters.Count == 0)
return true;
return si.ProcessFilters.Any(p => string.Equals(p.ToLowerInvariant().Replace(".exe", ""), Paths.ProcessName,
StringComparison.InvariantCultureIgnoreCase));
}

var ignores = new HashSet<string>(File.ReadAllLines(ignoresPath).Select(s => s.Trim()));
var scriptsToCompile = files.Where(f => !ignores.Contains(Path.GetFileName(f))).ToList();
var scriptsToCompile = files.Where(f => IsValidProcess(f) && !ignores.Contains(Path.GetFileName(f))).ToList();

Logger.LogInfo(
$"Found {files.Length} scripts to compile, skipping {files.Length - scriptsToCompile.Count} scripts because of `scriptignores`");
$"Found {files.Length} scripts to compile, skipping {files.Length - scriptsToCompile.Count} scripts because of `scriptignores` or process filters");

var md5 = MD5.Create();
var scriptDict = new Dictionary<string, byte[]>();
Expand All @@ -108,7 +118,7 @@ private void CompileScripts()

foreach (var scriptFile in scriptsToCompile)
{
if (!availableScripts.TryGetValue(scriptFile, out var info) || info == null) continue;
if (!availableScripts.TryGetValue(scriptFile, out var info)) continue;
foreach (var infoReference in info.References)
Assembly.LoadFile(infoReference);
}
Expand Down

0 comments on commit 19a9bf6

Please sign in to comment.